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