Debian Codegraph @

libstdc++-v3/testsuite/std/concepts/concepts.lang/concept.swappable/swap.cc

raw
1 // Copyright (C) 2019-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 // { dg-do compile { target c++20 } }
19
20 #include <concepts>
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 static_assert(__gnu_test::is_customization_point_object(std::ranges::swap));
25
26 namespace nu
27 {
28 struct S { bool swapped = false; };
29 constexpr void swap(S& l, S& r) { l.swapped = r.swapped = true; }
30 struct T { int i; };
31
32 union U { char c; int i; };
33 constexpr void swap(U& l, U& r) { l.i = r.i = 99; }
34 }
35
36 constexpr bool check_struct_with_adl_swap(int)
37 {
38 nu::S s1, s2;
39 std::ranges::swap(s1, s2);
40 return s1.swapped && s2.swapped;
41 }
42
43 static_assert(check_struct_with_adl_swap(1));
44
45 constexpr bool check_array_with_adl_swap(int)
46 {
47 nu::S s1[2], s2[2];
48 std::ranges::swap(s1, s2);
49 return s1[0].swapped && s2[0].swapped && s1[1].swapped && s2[1].swapped;
50 }
51
52 static_assert(check_array_with_adl_swap(1));
53
54 constexpr bool check_struct_without_adl_swap(int i)
55 {
56 nu::T t1{i}, t2{2*i};
57 std::ranges::swap(t1, t2);
58 return t1.i == 2*i && t2.i == i;
59 }
60
61 static_assert(check_struct_without_adl_swap(1));
62
63 constexpr bool check_array_without_adl_swap(int i)
64 {
65 nu::T t1[2]{i, 2*i}, t2[2]{3*i, 4*i};
66 std::ranges::swap(t1, t2);
67 return t1[0].i == 3*i && t2[0].i == i && t1[1].i == 4*i && t2[1].i == 2*i;
68 }
69
70 static_assert(check_array_without_adl_swap(1));
71
72
73 constexpr bool check_union_with_adl_swap(int i)
74 {
75 nu::U u1{}, u2{};
76 u1.i = u2.i = i;
77 std::ranges::swap(u1, u2);
78 return u1.i == 99 && u2.i == 99;
79 }
80
81 static_assert(check_union_with_adl_swap(1));