xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_swap/swap.pass.cpp (revision 1cf4113952ae3e4cc75decdf6feb3ce5dd8ca4a1)
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 // <string>
10 
11 // void swap(basic_string& s); // constexpr since C++20
12 
13 #include <string>
14 #include <stdexcept>
15 #include <algorithm>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void
23 test(S s1, S s2)
24 {
25     S s1_ = s1;
26     S s2_ = s2;
27     s1.swap(s2);
28     LIBCPP_ASSERT(s1.__invariants());
29     LIBCPP_ASSERT(s2.__invariants());
30     assert(s1 == s2_);
31     assert(s2 == s1_);
32 }
33 
34 TEST_CONSTEXPR_CXX20 bool test() {
35   {
36     typedef std::string S;
37     test(S(""), S(""));
38     test(S(""), S("12345"));
39     test(S(""), S("1234567890"));
40     test(S(""), S("12345678901234567890"));
41     test(S("abcde"), S(""));
42     test(S("abcde"), S("12345"));
43     test(S("abcde"), S("1234567890"));
44     test(S("abcde"), S("12345678901234567890"));
45     test(S("abcdefghij"), S(""));
46     test(S("abcdefghij"), S("12345"));
47     test(S("abcdefghij"), S("1234567890"));
48     test(S("abcdefghij"), S("12345678901234567890"));
49     test(S("abcdefghijklmnopqrst"), S(""));
50     test(S("abcdefghijklmnopqrst"), S("12345"));
51     test(S("abcdefghijklmnopqrst"), S("1234567890"));
52     test(S("abcdefghijklmnopqrst"), S("12345678901234567890"));
53   }
54 #if TEST_STD_VER >= 11
55   {
56     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
57     test(S(""), S(""));
58     test(S(""), S("12345"));
59     test(S(""), S("1234567890"));
60     test(S(""), S("12345678901234567890"));
61     test(S("abcde"), S(""));
62     test(S("abcde"), S("12345"));
63     test(S("abcde"), S("1234567890"));
64     test(S("abcde"), S("12345678901234567890"));
65     test(S("abcdefghij"), S(""));
66     test(S("abcdefghij"), S("12345"));
67     test(S("abcdefghij"), S("1234567890"));
68     test(S("abcdefghij"), S("12345678901234567890"));
69     test(S("abcdefghijklmnopqrst"), S(""));
70     test(S("abcdefghijklmnopqrst"), S("12345"));
71     test(S("abcdefghijklmnopqrst"), S("1234567890"));
72     test(S("abcdefghijklmnopqrst"), S("12345678901234567890"));
73   }
74 #endif
75 
76   return true;
77 }
78 
79 int main(int, char**)
80 {
81   test();
82 #if TEST_STD_VER > 17
83   static_assert(test());
84 #endif
85 
86   return 0;
87 }
88