xref: /llvm-project/libcxx/test/std/utilities/utility/pairs/pairs.pair/swap.pass.cpp (revision 737a4501e815d8dd57e5095dbbbede500dfa8ccb)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <utility>
10 
11 // template <class T1, class T2> struct pair
12 
13 // void swap(pair& p);
14 
15 #include <utility>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 struct S {
21     int i;
SS22     TEST_CONSTEXPR_CXX20 S() : i(0) {}
SS23     TEST_CONSTEXPR_CXX20 S(int j) : i(j) {}
operator ==S24     TEST_CONSTEXPR_CXX20 bool operator==(int x) const { return i == x; }
25 };
26 
test()27 TEST_CONSTEXPR_CXX20 bool test() {
28   {
29     typedef std::pair<int, short> P1;
30     P1 p1(3, static_cast<short>(4));
31     P1 p2(5, static_cast<short>(6));
32     p1.swap(p2);
33     assert(p1.first == 5);
34     assert(p1.second == 6);
35     assert(p2.first == 3);
36     assert(p2.second == 4);
37   }
38   {
39     typedef std::pair<int, S> P1;
40     P1 p1(3, S(4));
41     P1 p2(5, S(6));
42     p1.swap(p2);
43     assert(p1.first == 5);
44     assert(p1.second == 6);
45     assert(p2.first == 3);
46     assert(p2.second == 4);
47   }
48   return true;
49 }
50 
main(int,char **)51 int main(int, char**) {
52   test();
53 #if TEST_STD_VER >= 20
54   static_assert(test());
55 #endif
56 
57   return 0;
58 }
59