Debian Codegraph @

libstdc++-v3/testsuite/20_util/is_swappable/value.h

raw
1 // Copyright (C) 2015-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 #include <type_traits>
19
20 #if defined(test_std_is_swappable)
21 # ifndef __cpp_lib_is_swappable
22 # error "Feature-test macro for is_swappable missing"
23 # elif __cpp_lib_is_swappable != 201603
24 # error "Feature-test macro for is_swappable has wrong value"
25 # endif
26 // Test std::is_swappable:
27 template<class T>
28 using is_swappable = std::is_swappable<T>;
29 #elif defined(test_std_is_swappable_ext)
30 // Test our __is_swappable extension:
31 template<class T>
32 using is_swappable = std::__is_swappable<T>;
33 #else
34 # error "Either test_std_is_swappable or test_std_is_swappable_ext" \
35 "need to be defined"
36 #endif
37
38 #include <utility>
39 #include <array>
40 #include <tuple>
41 #include <queue>
42 #include <stack>
43 #include <testsuite_tr1.h>
44
45 namespace funny {
46 struct F {};
47 void swap(F&, F&) = delete;
48 void swap(F(&)[5], F(&)[5]);
49
50 struct F2
51 {
52 friend void swap(F2&, F2&) = delete;
53 };
54
55 struct F3
56 {
57 friend void swap(F3&, F3) {}
58 };
59
60 struct DummyCmp
61 {
62 template<class T>
63 bool operator()(const T&, const T&) const
64 { return false; }
65 };
66 }
67 void test01()
68 {
69 using namespace __gnu_test;
70 // Positive tests.
71 static_assert(test_property<is_swappable, int>(true), "");
72 static_assert(test_property<is_swappable, bool>(true), "");
73 static_assert(test_property<is_swappable, decltype(nullptr)>(true), "");
74 static_assert(test_property<is_swappable, int&>(true), "");
75 static_assert(test_property<is_swappable, int&&>(true), "");
76 static_assert(test_property<is_swappable, int[1]>(true), "");
77 static_assert(test_property<is_swappable, int[1][2]>(true), "");
78 static_assert(test_property<is_swappable, int[1][2][3]>(true), "");
79 static_assert(test_property<is_swappable, int(&)[1]>(true), "");
80 static_assert(test_property<is_swappable, funny::F[5]>(true), "");
81 static_assert(test_property<is_swappable, funny::F3>(true), "");
82 static_assert(test_property<is_swappable, funny::F3[1]>(true), "");
83 static_assert(test_property<is_swappable, funny::F3[1][2]>(true), "");
84 static_assert(test_property<is_swappable, funny::F3[1][2][3]>(true), "");
85 static_assert(test_property<is_swappable,
86 ThrowCopyConsClass>(true), "");
87 static_assert(test_property<is_swappable, EnumType>(true), "");
88 static_assert(test_property<is_swappable, PODType>(true), "");
89 static_assert(test_property<is_swappable, UnionType>(true), "");
90 static_assert(test_property<is_swappable, construct::SE>(true), "");
91 static_assert(test_property<is_swappable, construct::Empty>(true), "");
92 static_assert(test_property<is_swappable, void*>(true), "");
93 static_assert(test_property<is_swappable, int const*>(true), "");
94 static_assert(test_property<is_swappable, ClassType*>(true), "");
95 static_assert(test_property<is_swappable, int ClassType::*>(true), "");
96 static_assert(test_property<is_swappable,
97 void (ClassType::*)()>(true), "");
98 static_assert(test_property<is_swappable,
99 construct::Nontrivial>(true), "");
100 static_assert(test_property<is_swappable, construct::Any>(true), "");
101 static_assert(test_property<is_swappable, construct::nAny>(true), "");
102 static_assert(test_property<is_swappable,
103 std::pair<int, int>>(true), "");
104 static_assert(test_property<is_swappable,
105 std::pair<int, int>[1]>(true), "");
106 static_assert(test_property<is_swappable,
107 std::pair<int, int>[1][2]>(true), "");
108 static_assert(test_property<is_swappable,
109 std::pair<int, int>[1][2][3]>(true), "");
110 static_assert(test_property<is_swappable,
111 std::pair<construct::Nontrivial, construct::Nontrivial>>(true), "");
112 static_assert(test_property<is_swappable,
113 std::tuple<int>>(true), "");
114 static_assert(test_property<is_swappable,
115 std::tuple<int>[1]>(true), "");
116 static_assert(test_property<is_swappable,
117 std::tuple<int>[1][2]>(true), "");
118 static_assert(test_property<is_swappable,
119 std::tuple<int>[1][2][3]>(true), "");
120 static_assert(test_property<is_swappable,
121 std::tuple<>>(true), "");
122 static_assert(test_property<is_swappable,
123 std::tuple<>[1]>(true), "");
124 static_assert(test_property<is_swappable,
125 std::tuple<>[1][2]>(true), "");
126 static_assert(test_property<is_swappable,
127 std::tuple<>[1][2][3]>(true), "");
128 static_assert(test_property<is_swappable,
129 std::tuple<construct::Nontrivial>>(true), "");
130 static_assert(test_property<is_swappable,
131 std::array<int, 1>>(true), "");
132 static_assert(test_property<is_swappable,
133 std::array<int, 1>[1]>(true), "");
134 static_assert(test_property<is_swappable,
135 std::array<int, 1>[1][2]>(true), "");
136 static_assert(test_property<is_swappable,
137 std::array<int, 1>[1][2][3]>(true), "");
138 static_assert(test_property<is_swappable,
139 std::array<construct::Nontrivial, 1>>(true), "");
140 static_assert(test_property<is_swappable,
141 std::array<int, 0>>(true), "");
142 static_assert(test_property<is_swappable,
143 std::array<construct::DelCopy, 0>>(true), "");
144 static_assert(test_property<is_swappable,
145 std::queue<int>>(true), "");
146 static_assert(test_property<is_swappable,
147 std::queue<int>[1]>(true), "");
148 static_assert(test_property<is_swappable,
149 std::queue<int>[1][2]>(true), "");
150 static_assert(test_property<is_swappable,
151 std::queue<int>[1][2][3]>(true), "");
152 static_assert(test_property<is_swappable,
153 std::queue<construct::Nontrivial>>(true), "");
154 static_assert(test_property<is_swappable,
155 std::priority_queue<int>>(true), "");
156 static_assert(test_property<is_swappable,
157 std::priority_queue<int>[1]>(true), "");
158 static_assert(test_property<is_swappable,
159 std::priority_queue<int>[1][2]>(true), "");
160 static_assert(test_property<is_swappable,
161 std::priority_queue<int>[1][2][3]>(true), "");
162 static_assert(test_property<is_swappable,
163 std::priority_queue<construct::Nontrivial,
164 std::vector<construct::Nontrivial>,
165 funny::DummyCmp>>(true), "");
166 static_assert(test_property<is_swappable,
167 std::stack<int>>(true), "");
168 static_assert(test_property<is_swappable,
169 std::stack<int>[1]>(true), "");
170 static_assert(test_property<is_swappable,
171 std::stack<int>[1][2]>(true), "");
172 static_assert(test_property<is_swappable,
173 std::stack<int>[1][2][3]>(true), "");
174 static_assert(test_property<is_swappable,
175 std::stack<construct::Nontrivial>>(true), "");
176 // Negative tests.
177 static_assert(test_property<is_swappable, void>(false), "");
178 static_assert(test_property<is_swappable, const void>(false), "");
179 static_assert(test_property<is_swappable, void()>(false), "");
180 static_assert(test_property<is_swappable, void() const>(false), "");
181 static_assert(test_property<is_swappable, void() volatile>(false), "");
182 static_assert(test_property<is_swappable,
183 void() const volatile>(false), "");
184 static_assert(test_property<is_swappable, const int>(false), "");
185 static_assert(test_property<is_swappable, const bool>(false), "");
186 static_assert(test_property<is_swappable, int[]>(false), "");
187 static_assert(test_property<is_swappable, const int[]>(false), "");
188 static_assert(test_property<is_swappable, int[][1]>(false), "");
189 static_assert(test_property<is_swappable, const int[1]>(false), "");
190 static_assert(test_property<is_swappable, const int[1][2]>(false), "");
191 static_assert(test_property<is_swappable, const int[1][2][3]>(false), "");
192 static_assert(test_property<is_swappable, construct::DelCopy>(false), "");
193 static_assert(test_property<is_swappable,
194 construct::Abstract>(false), "");
195 static_assert(test_property<is_swappable,
196 construct::NontrivialUnion>(false), "");
197 static_assert(test_property<is_swappable, funny::F>(false), "");
198 static_assert(test_property<is_swappable, funny::F[1]>(false), "");
199 static_assert(test_property<is_swappable, funny::F[1][2]>(false), "");
200 static_assert(test_property<is_swappable, funny::F[1][2][3]>(false), "");
201 static_assert(test_property<is_swappable, funny::F[4]>(false), "");
202 static_assert(test_property<is_swappable, construct::DelCopy>(false), "");
203 static_assert(test_property<is_swappable,
204 DeletedCopyAssignClass>(false), "");
205 static_assert(test_property<is_swappable,
206 DeletedMoveAssignClass>(false), "");
207 static_assert(test_property<is_swappable, funny::F2>(false), "");
208 static_assert(test_property<is_swappable, funny::F2[1]>(false), "");
209 static_assert(test_property<is_swappable, funny::F2[1][2]>(false), "");
210 static_assert(test_property<is_swappable, funny::F2[1][2][3]>(false), "");
211 static_assert(test_property<is_swappable, funny::F2[4]>(false), "");
212 static_assert(test_property<is_swappable, funny::F2[5]>(false), "");
213 }