[flang][runtime] Make defined formatted I/O process format elementally (#74150)
[llvm-project.git] / libcxx / test / support / variant_test_helpers.h
blobc174cba3284019b4e9c346c5ab391ac655e4d6dc
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 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
11 #define SUPPORT_VARIANT_TEST_HELPERS_H
13 #include <type_traits>
14 #include <utility>
15 #include <cassert>
17 #include "test_macros.h"
18 #include "type_id.h"
20 #if TEST_STD_VER <= 14
21 #error This file requires C++17
22 #endif
24 // FIXME: Currently the variant<T&> tests are disabled using this macro.
25 #define TEST_VARIANT_HAS_NO_REFERENCES
27 // TODO(LLVM-19): Remove TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
28 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
29 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
30 #endif
31 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
32 constexpr bool VariantAllowsNarrowingConversions = true;
33 #else
34 constexpr bool VariantAllowsNarrowingConversions = false;
35 #endif
37 #ifndef TEST_HAS_NO_EXCEPTIONS
38 struct CopyThrows {
39 CopyThrows() = default;
40 CopyThrows(CopyThrows const&) { throw 42; }
41 CopyThrows& operator=(CopyThrows const&) { throw 42; }
44 struct MoveThrows {
45 static int alive;
46 MoveThrows() { ++alive; }
47 MoveThrows(MoveThrows const&) {++alive;}
48 MoveThrows(MoveThrows&&) { throw 42; }
49 MoveThrows& operator=(MoveThrows const&) { return *this; }
50 MoveThrows& operator=(MoveThrows&&) { throw 42; }
51 ~MoveThrows() { --alive; }
54 int MoveThrows::alive = 0;
56 struct MakeEmptyT {
57 static int alive;
58 MakeEmptyT() { ++alive; }
59 MakeEmptyT(MakeEmptyT const&) {
60 ++alive;
61 // Don't throw from the copy constructor since variant's assignment
62 // operator performs a copy before committing to the assignment.
64 MakeEmptyT(MakeEmptyT &&) {
65 throw 42;
67 MakeEmptyT& operator=(MakeEmptyT const&) {
68 throw 42;
70 MakeEmptyT& operator=(MakeEmptyT&&) {
71 throw 42;
73 ~MakeEmptyT() { --alive; }
75 static_assert(std::is_swappable_v<MakeEmptyT>, ""); // required for test
77 int MakeEmptyT::alive = 0;
79 template <class Variant>
80 void makeEmpty(Variant& v) {
81 Variant v2(std::in_place_type<MakeEmptyT>);
82 try {
83 v = std::move(v2);
84 assert(false);
85 } catch (...) {
86 assert(v.valueless_by_exception());
89 #endif // TEST_HAS_NO_EXCEPTIONS
91 enum CallType : unsigned {
92 CT_None,
93 CT_NonConst = 1,
94 CT_Const = 2,
95 CT_LValue = 4,
96 CT_RValue = 8
99 inline constexpr CallType operator|(CallType LHS, CallType RHS) {
100 return static_cast<CallType>(static_cast<unsigned>(LHS) |
101 static_cast<unsigned>(RHS));
104 struct ForwardingCallObject {
106 template <class... Args>
107 ForwardingCallObject& operator()(Args&&...) & {
108 set_call<Args &&...>(CT_NonConst | CT_LValue);
109 return *this;
112 template <class... Args>
113 const ForwardingCallObject& operator()(Args&&...) const & {
114 set_call<Args &&...>(CT_Const | CT_LValue);
115 return *this;
118 template <class... Args>
119 ForwardingCallObject&& operator()(Args&&...) && {
120 set_call<Args &&...>(CT_NonConst | CT_RValue);
121 return std::move(*this);
124 template <class... Args>
125 const ForwardingCallObject&& operator()(Args&&...) const && {
126 set_call<Args &&...>(CT_Const | CT_RValue);
127 return std::move(*this);
130 template <class... Args> static void set_call(CallType type) {
131 assert(last_call_type == CT_None);
132 assert(last_call_args == nullptr);
133 last_call_type = type;
134 last_call_args = std::addressof(makeArgumentID<Args...>());
137 template <class... Args> static bool check_call(CallType type) {
138 bool result = last_call_type == type && last_call_args &&
139 *last_call_args == makeArgumentID<Args...>();
140 last_call_type = CT_None;
141 last_call_args = nullptr;
142 return result;
145 // To check explicit return type for visit<R>
146 constexpr operator int() const
148 return 0;
151 static CallType last_call_type;
152 static const TypeID *last_call_args;
155 CallType ForwardingCallObject::last_call_type = CT_None;
156 const TypeID *ForwardingCallObject::last_call_args = nullptr;
158 struct ReturnFirst {
159 template <class... Args> constexpr int operator()(int f, Args &&...) const {
160 return f;
164 struct ReturnArity {
165 template <class... Args> constexpr int operator()(Args &&...) const {
166 return sizeof...(Args);
170 #endif // SUPPORT_VARIANT_TEST_HELPERS_H