| 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_nothrow_swappable) |
| 21 |
# ifndef __cpp_lib_is_swappable |
| 22 |
# error "Feature-test macro for is_nothrow_swappable missing" |
| 23 |
# elif __cpp_lib_is_swappable != 201603 |
| 24 |
# error "Feature-test macro for is_nothrow_swappable has wrong value" |
| 25 |
# endif |
| 26 |
// Test std::is_nothrow_swappable: |
| 27 |
template<class T> |
| 28 |
using is_nothrow_swappable = std::is_nothrow_swappable<T>; |
| 29 |
#elif defined(test_std_is_nothrow_swappable_ext) |
| 30 |
// Test our __is_nothrow_swappable extension: |
| 31 |
template<class T> |
| 32 |
using is_nothrow_swappable = std::__is_nothrow_swappable<T>; |
| 33 |
#else |
| 34 |
# error "Either test_std_is_nothrow_swappable or " \ |
| 35 |
"test_std_is_nothrow_swappable_ext 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]) noexcept; |
| 49 |
void swap(F(&)[6], F(&)[6]); |
| 50 |
struct A {}; |
| 51 |
void swap(A&, A&) noexcept(false); |
| 52 |
} |
| 53 |
namespace std { |
| 54 |
template<> |
| 55 |
void swap<funny::A>(funny::A&, funny::A&) noexcept |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
template<> |
| 60 |
void swap<funny::A>(funny::A(&)[3], funny::A(&)[3]) noexcept(false) |
| 61 |
{ |
| 62 |
} |
| 63 |
} |
| 64 |
namespace ns1 { |
| 65 |
struct SwapThrow {}; |
| 66 |
void swap(SwapThrow&, SwapThrow&); |
| 67 |
void swap(SwapThrow(&)[3], SwapThrow(&)[3]) noexcept; |
| 68 |
} |
| 69 |
|
| 70 |
namespace ns2 { |
| 71 |
struct SwapThrow { |
| 72 |
SwapThrow() noexcept = default; |
| 73 |
SwapThrow(const SwapThrow&) noexcept(false); |
| 74 |
SwapThrow& operator=(const SwapThrow&) noexcept(false); |
| 75 |
}; |
| 76 |
} |
| 77 |
|
| 78 |
namespace ns3 { |
| 79 |
struct SwapNoThrow { |
| 80 |
SwapNoThrow() noexcept = default; |
| 81 |
SwapNoThrow(const SwapNoThrow&) noexcept(false); |
| 82 |
SwapNoThrow& operator =(const SwapNoThrow&) noexcept(false); |
| 83 |
}; |
| 84 |
void swap(SwapNoThrow&, SwapNoThrow&) noexcept; |
| 85 |
} |
| 86 |
|
| 87 |
namespace ns4 { |
| 88 |
struct SwapNoThrow {}; |
| 89 |
} |
| 90 |
|
| 91 |
namespace ns5 { |
| 92 |
struct SwapThrow { |
| 93 |
SwapThrow() noexcept = default; |
| 94 |
SwapThrow(SwapThrow&&) noexcept; |
| 95 |
SwapThrow& operator=(const SwapThrow&) noexcept(false); |
| 96 |
}; |
| 97 |
} |
| 98 |
|
| 99 |
namespace comps { |
| 100 |
struct CompareNoThrowCopyable |
| 101 |
{ |
| 102 |
template<class T> |
| 103 |
bool operator()(const T&, const T&) const |
| 104 |
{ return false; } |
| 105 |
}; |
| 106 |
|
| 107 |
struct CompareNonCopyable |
| 108 |
{ |
| 109 |
CompareNonCopyable() = default; |
| 110 |
CompareNonCopyable(const CompareNonCopyable&) = delete; |
| 111 |
CompareNonCopyable& operator=(const CompareNonCopyable&) noexcept; |
| 112 |
|
| 113 |
template<class T> |
| 114 |
bool operator()(const T&, const T&) const |
| 115 |
{ return false; } |
| 116 |
}; |
| 117 |
|
| 118 |
struct CompareThrowCopyable |
| 119 |
{ |
| 120 |
CompareThrowCopyable() = default; |
| 121 |
CompareThrowCopyable(const CompareThrowCopyable&) noexcept(false); |
| 122 |
CompareThrowCopyable& operator=(const CompareThrowCopyable&); |
| 123 |
|
| 124 |
template<class T> |
| 125 |
bool operator()(const T&, const T&) const |
| 126 |
{ return false; } |
| 127 |
}; |
| 128 |
} |
| 129 |
|
| 130 |
void test01() |
| 131 |
{ |
| 132 |
using namespace __gnu_test; |
| 133 |
// Positive tests. |
| 134 |
static_assert(test_property<is_nothrow_swappable, int>(true), ""); |
| 135 |
static_assert(test_property<is_nothrow_swappable, bool>(true), ""); |
| 136 |
static_assert(test_property<is_nothrow_swappable, |
| 137 |
decltype(nullptr)>(true), ""); |
| 138 |
static_assert(test_property<is_nothrow_swappable, int&>(true), ""); |
| 139 |
static_assert(test_property<is_nothrow_swappable, int&&>(true), ""); |
| 140 |
static_assert(test_property<is_nothrow_swappable, int[1]>(true), ""); |
| 141 |
static_assert(test_property<is_nothrow_swappable, int[1][2]>(true), ""); |
| 142 |
static_assert(test_property<is_nothrow_swappable, int[1][2][3]>(true), ""); |
| 143 |
static_assert(test_property<is_nothrow_swappable, funny::F[5]>(true), ""); |
| 144 |
static_assert(test_property<is_nothrow_swappable, EnumType>(true), ""); |
| 145 |
static_assert(test_property<is_nothrow_swappable, PODType>(true), ""); |
| 146 |
static_assert(test_property<is_nothrow_swappable, UnionType>(true), ""); |
| 147 |
static_assert(test_property<is_nothrow_swappable, |
| 148 |
construct::SE>(true), ""); |
| 149 |
static_assert(test_property<is_nothrow_swappable, |
| 150 |
construct::Empty>(true), ""); |
| 151 |
static_assert(test_property<is_nothrow_swappable, void*>(true), ""); |
| 152 |
static_assert(test_property<is_nothrow_swappable, void(*)()>(true), ""); |
| 153 |
static_assert(test_property<is_nothrow_swappable, int const*>(true), ""); |
| 154 |
static_assert(test_property<is_nothrow_swappable, ClassType*>(true), ""); |
| 155 |
static_assert(test_property<is_nothrow_swappable, |
| 156 |
int ClassType::*>(true), ""); |
| 157 |
static_assert(test_property<is_nothrow_swappable, |
| 158 |
void (ClassType::*)()>(true), ""); |
| 159 |
static_assert(test_property<is_nothrow_swappable, |
| 160 |
int (ClassType::*)() const volatile>(true), ""); |
| 161 |
static_assert(test_property<is_nothrow_swappable, |
| 162 |
ns1::SwapThrow[3]>(true), ""); |
| 163 |
static_assert(!noexcept(std::swap(std::declval<ns1::SwapThrow(&)[3]>(), |
| 164 |
std::declval<ns1::SwapThrow(&)[3]>())), |
| 165 |
""); |
| 166 |
static_assert(test_property<is_nothrow_swappable, |
| 167 |
ns3::SwapNoThrow>(true), ""); |
| 168 |
static_assert(!noexcept(std::swap(std::declval<ns3::SwapNoThrow&>(), |
| 169 |
std::declval<ns3::SwapNoThrow&>())), ""); |
| 170 |
static_assert(test_property<is_nothrow_swappable, |
| 171 |
ns3::SwapNoThrow[1]>(true), ""); |
| 172 |
static_assert(test_property<is_nothrow_swappable, |
| 173 |
ns3::SwapNoThrow[3]>(true), ""); |
| 174 |
static_assert(test_property<is_nothrow_swappable, |
| 175 |
ns3::SwapNoThrow[2][3][4]>(true), ""); |
| 176 |
static_assert(test_property<is_nothrow_swappable, |
| 177 |
ns4::SwapNoThrow>(true), ""); |
| 178 |
static_assert(test_property<is_nothrow_swappable, |
| 179 |
ns4::SwapNoThrow[1]>(true), ""); |
| 180 |
static_assert(test_property<is_nothrow_swappable, |
| 181 |
ns4::SwapNoThrow[3]>(true), ""); |
| 182 |
static_assert(test_property<is_nothrow_swappable, |
| 183 |
ns4::SwapNoThrow[2][3][4]>(true), ""); |
| 184 |
static_assert(test_property<is_nothrow_swappable, |
| 185 |
std::pair<int, int>>(true), ""); |
| 186 |
static_assert(test_property<is_nothrow_swappable, |
| 187 |
std::pair<int, int>[1]>(true), ""); |
| 188 |
static_assert(test_property<is_nothrow_swappable, |
| 189 |
std::pair<int, int>[1][2]>(true), ""); |
| 190 |
static_assert(test_property<is_nothrow_swappable, |
| 191 |
std::tuple<int>>(true), ""); |
| 192 |
static_assert(test_property<is_nothrow_swappable, |
| 193 |
std::tuple<int>[1]>(true), ""); |
| 194 |
static_assert(test_property<is_nothrow_swappable, |
| 195 |
std::tuple<int>[1][2]>(true), ""); |
| 196 |
static_assert(test_property<is_nothrow_swappable, |
| 197 |
std::tuple<>>(true), ""); |
| 198 |
static_assert(test_property<is_nothrow_swappable, |
| 199 |
std::tuple<>[1]>(true), ""); |
| 200 |
static_assert(test_property<is_nothrow_swappable, |
| 201 |
std::tuple<>[1][2]>(true), ""); |
| 202 |
static_assert(test_property<is_nothrow_swappable, |
| 203 |
std::array<int, 1>>(true), ""); |
| 204 |
static_assert(test_property<is_nothrow_swappable, |
| 205 |
std::array<int, 0>>(true), ""); |
| 206 |
static_assert(test_property<is_nothrow_swappable, |
| 207 |
std::array<construct::DelCopy, 0>>(true), ""); |
| 208 |
static_assert(test_property<is_nothrow_swappable, |
| 209 |
std::array<ns1::SwapThrow, 0>>(true), ""); |
| 210 |
static_assert(test_property<is_nothrow_swappable, |
| 211 |
std::queue<int>>(true), ""); |
| 212 |
static_assert(test_property<is_nothrow_swappable, |
| 213 |
std::priority_queue<int>>(true), ""); |
| 214 |
static_assert(test_property<is_nothrow_swappable, |
| 215 |
std::stack<int>>(true), ""); |
| 216 |
static_assert(test_property<is_nothrow_swappable, |
| 217 |
std::priority_queue<int, std::vector<int>, |
| 218 |
comps::CompareNoThrowCopyable>>(true), ""); |
| 219 |
|
| 220 |
// Negative tests. |
| 221 |
static_assert(test_property<is_nothrow_swappable, void>(false), ""); |
| 222 |
static_assert(test_property<is_nothrow_swappable, const void>(false), ""); |
| 223 |
static_assert(test_property<is_nothrow_swappable, void()>(false), ""); |
| 224 |
static_assert(test_property<is_nothrow_swappable, |
| 225 |
void() const>(false), ""); |
| 226 |
static_assert(test_property<is_nothrow_swappable, |
| 227 |
void() volatile>(false), ""); |
| 228 |
static_assert(test_property<is_nothrow_swappable, |
| 229 |
void() const volatile>(false), ""); |
| 230 |
static_assert(test_property<is_nothrow_swappable, const int>(false), ""); |
| 231 |
static_assert(test_property<is_nothrow_swappable, const bool>(false), ""); |
| 232 |
static_assert(test_property<is_nothrow_swappable, |
| 233 |
const int[1]>(false), ""); |
| 234 |
static_assert(test_property<is_nothrow_swappable, |
| 235 |
const int[1][2]>(false), ""); |
| 236 |
static_assert(test_property<is_nothrow_swappable, |
| 237 |
const int[1][2][3]>(false), ""); |
| 238 |
static_assert(test_property<is_nothrow_swappable, int[]>(false), ""); |
| 239 |
static_assert(test_property<is_nothrow_swappable, const int[]>(false), ""); |
| 240 |
static_assert(test_property<is_nothrow_swappable, int[][1]>(false), ""); |
| 241 |
static_assert(test_property<is_nothrow_swappable, |
| 242 |
const funny::F[5]>(false), ""); |
| 243 |
static_assert(test_property<is_nothrow_swappable, |
| 244 |
construct::Abstract>(false), ""); |
| 245 |
static_assert(test_property<is_nothrow_swappable, |
| 246 |
construct::DelCopy>(false), ""); |
| 247 |
static_assert(test_property<is_nothrow_swappable, funny::F>(false), ""); |
| 248 |
static_assert(test_property<is_nothrow_swappable, funny::F[1]>(false), ""); |
| 249 |
static_assert(test_property<is_nothrow_swappable, |
| 250 |
funny::F[1][2]>(false), ""); |
| 251 |
static_assert(test_property<is_nothrow_swappable, |
| 252 |
funny::F[1][2][3]>(false), ""); |
| 253 |
static_assert(test_property<is_nothrow_swappable, funny::F[6]>(false), ""); |
| 254 |
static_assert(test_property<is_nothrow_swappable, funny::A>(false), ""); |
| 255 |
static_assert(noexcept(std::swap(std::declval<funny::A&>(), |
| 256 |
std::declval<funny::A&>())), ""); |
| 257 |
static_assert(test_property<is_nothrow_swappable, funny::A[3]>(false), ""); |
| 258 |
static_assert(test_property<is_nothrow_swappable, |
| 259 |
ns1::SwapThrow>(false), ""); |
| 260 |
static_assert(test_property<is_nothrow_swappable, |
| 261 |
ns1::SwapThrow[1]>(false), ""); |
| 262 |
static_assert(test_property<is_nothrow_swappable, |
| 263 |
ns1::SwapThrow[3][2]>(false), ""); |
| 264 |
static_assert(test_property<is_nothrow_swappable, |
| 265 |
ns1::SwapThrow[2][3][4]>(false), ""); |
| 266 |
static_assert(test_property<is_nothrow_swappable, |
| 267 |
ns2::SwapThrow>(false), ""); |
| 268 |
static_assert(test_property<is_nothrow_swappable, |
| 269 |
ns2::SwapThrow[1]>(false), ""); |
| 270 |
static_assert(test_property<is_nothrow_swappable, |
| 271 |
ns2::SwapThrow[2][3][4]>(false), ""); |
| 272 |
static_assert(test_property<is_nothrow_swappable, |
| 273 |
ns5::SwapThrow>(false), ""); |
| 274 |
static_assert(test_property<is_nothrow_swappable, |
| 275 |
ns5::SwapThrow[1]>(false), ""); |
| 276 |
static_assert(test_property<is_nothrow_swappable, |
| 277 |
ns5::SwapThrow[2][3][4]>(false), ""); |
| 278 |
static_assert(test_property<is_nothrow_swappable, |
| 279 |
ThrowCopyConsClass>(false), ""); |
| 280 |
static_assert(test_property<is_nothrow_swappable, |
| 281 |
std::pair<ThrowCopyConsClass, ThrowCopyConsClass>>(false), ""); |
| 282 |
static_assert(test_property<is_nothrow_swappable, |
| 283 |
std::tuple<ThrowCopyConsClass>>(false), ""); |
| 284 |
static_assert(test_property<is_nothrow_swappable, |
| 285 |
std::array<ThrowCopyConsClass, 1>>(false), ""); |
| 286 |
static_assert(test_property<is_nothrow_swappable, |
| 287 |
std::queue<ThrowCopyConsClass>>(true), ""); |
| 288 |
static_assert(test_property<is_nothrow_swappable, |
| 289 |
std::priority_queue<ThrowCopyConsClass, |
| 290 |
std::vector<ThrowCopyConsClass>, |
| 291 |
comps::CompareNoThrowCopyable>>(true), ""); |
| 292 |
static_assert(test_property<is_nothrow_swappable, |
| 293 |
std::stack<ThrowCopyConsClass>>(true), ""); |
| 294 |
static_assert(test_property<is_nothrow_swappable, |
| 295 |
std::priority_queue<int, std::vector<int>, |
| 296 |
comps::CompareNonCopyable>>(false), ""); |
| 297 |
static_assert(test_property<is_nothrow_swappable, |
| 298 |
std::priority_queue<int, std::vector<int>, |
| 299 |
comps::CompareThrowCopyable>>(false), ""); |
| 300 |
} |