[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / support / variant_test_helpers.h
blob0e040931e0cd278d1013c2897fd0247b5e0b397b
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
10 #define SUPPORT_VARIANT_TEST_HELPERS_H
12 #include <type_traits>
13 #include <utility>
14 #include <cassert>
16 #include "test_macros.h"
18 #if TEST_STD_VER <= 14
19 #error This file requires C++17
20 #endif
22 // FIXME: Currently the variant<T&> tests are disabled using this macro.
23 #define TEST_VARIANT_HAS_NO_REFERENCES
24 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
25 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
26 #endif
28 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
29 constexpr bool VariantAllowsNarrowingConversions = true;
30 #else
31 constexpr bool VariantAllowsNarrowingConversions = false;
32 #endif
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 struct CopyThrows {
36 CopyThrows() = default;
37 CopyThrows(CopyThrows const&) { throw 42; }
38 CopyThrows& operator=(CopyThrows const&) { throw 42; }
41 struct MoveThrows {
42 static int alive;
43 MoveThrows() { ++alive; }
44 MoveThrows(MoveThrows const&) {++alive;}
45 MoveThrows(MoveThrows&&) { throw 42; }
46 MoveThrows& operator=(MoveThrows const&) { return *this; }
47 MoveThrows& operator=(MoveThrows&&) { throw 42; }
48 ~MoveThrows() { --alive; }
51 int MoveThrows::alive = 0;
53 struct MakeEmptyT {
54 static int alive;
55 MakeEmptyT() { ++alive; }
56 MakeEmptyT(MakeEmptyT const&) {
57 ++alive;
58 // Don't throw from the copy constructor since variant's assignment
59 // operator performs a copy before committing to the assignment.
61 MakeEmptyT(MakeEmptyT &&) {
62 throw 42;
64 MakeEmptyT& operator=(MakeEmptyT const&) {
65 throw 42;
67 MakeEmptyT& operator=(MakeEmptyT&&) {
68 throw 42;
70 ~MakeEmptyT() { --alive; }
72 static_assert(std::is_swappable_v<MakeEmptyT>, ""); // required for test
74 int MakeEmptyT::alive = 0;
76 template <class Variant>
77 void makeEmpty(Variant& v) {
78 Variant v2(std::in_place_type<MakeEmptyT>);
79 try {
80 v = std::move(v2);
81 assert(false);
82 } catch (...) {
83 assert(v.valueless_by_exception());
86 #endif // TEST_HAS_NO_EXCEPTIONS
89 #endif // SUPPORT_VARIANT_TEST_HELPERS_H