Files
libcxx/test/support/variant_test_helpers.hpp
Chandler Carruth 7c3769df62 Update more file headers across all of the LLVM projects in the monorepo
to reflect the new license. These used slightly different spellings that
defeated my regular expressions.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@351648 91177308-0d34-0410-b5e6-96231b3b80d8
2019-01-19 10:56:40 +00:00

81 lines
2.1 KiB
C++

// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef SUPPORT_VARIANT_TEST_HELPERS_HPP
#define SUPPORT_VARIANT_TEST_HELPERS_HPP
#include <type_traits>
#include <utility>
#include <cassert>
#include "test_macros.h"
#if TEST_STD_VER <= 14
#error This file requires C++17
#endif
// FIXME: Currently the variant<T&> tests are disabled using this macro.
#define TEST_VARIANT_HAS_NO_REFERENCES
#ifndef TEST_HAS_NO_EXCEPTIONS
struct CopyThrows {
CopyThrows() = default;
CopyThrows(CopyThrows const&) { throw 42; }
CopyThrows& operator=(CopyThrows const&) { throw 42; }
};
struct MoveThrows {
static int alive;
MoveThrows() { ++alive; }
MoveThrows(MoveThrows const&) {++alive;}
MoveThrows(MoveThrows&&) { throw 42; }
MoveThrows& operator=(MoveThrows const&) { return *this; }
MoveThrows& operator=(MoveThrows&&) { throw 42; }
~MoveThrows() { --alive; }
};
int MoveThrows::alive = 0;
struct MakeEmptyT {
static int alive;
MakeEmptyT() { ++alive; }
MakeEmptyT(MakeEmptyT const&) {
++alive;
// Don't throw from the copy constructor since variant's assignment
// operator performs a copy before committing to the assignment.
}
MakeEmptyT(MakeEmptyT &&) {
throw 42;
}
MakeEmptyT& operator=(MakeEmptyT const&) {
throw 42;
}
MakeEmptyT& operator=(MakeEmptyT&&) {
throw 42;
}
~MakeEmptyT() { --alive; }
};
static_assert(std::is_swappable_v<MakeEmptyT>, ""); // required for test
int MakeEmptyT::alive = 0;
template <class Variant>
void makeEmpty(Variant& v) {
Variant v2(std::in_place_type<MakeEmptyT>);
try {
v = std::move(v2);
assert(false);
} catch (...) {
assert(v.valueless_by_exception());
}
}
#endif // TEST_HAS_NO_EXCEPTIONS
#endif // SUPPORT_VARIANT_TEST_HELPERS_HPP