Debian Codegraph @

libcxx/test/std/strings/basic.string/string.modifiers/string_swap/swap.pass.cpp

raw
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 #include "asan_testing.h"
21
22 template <class S>
23 TEST_CONSTEXPR_CXX20 void test(S s1, S s2) {
24 S s1_ = s1;
25 S s2_ = s2;
26 s1.swap(s2);
27 LIBCPP_ASSERT(s1.__invariants());
28 LIBCPP_ASSERT(s2.__invariants());
29 assert(s1 == s2_);
30 assert(s2 == s1_);
31 LIBCPP_ASSERT(is_string_asan_correct(s1));
32 LIBCPP_ASSERT(is_string_asan_correct(s2));
33 }
34
35 template <class S>
36 TEST_CONSTEXPR_CXX20 void test_string() {
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 test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), S(""));
54 test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), S("12345"));
55 test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), S("1234567890"));
56 test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), S("12345678901234567890"));
57 test(S("abcdefghijklmnopqrstabcdefghijklmnopqrst"), S("12345678901234567890abcdefghijklmnopqrst"));
58 }
59
60 TEST_CONSTEXPR_CXX20 bool test() {
61 test_string<std::string>();
62 #if TEST_STD_VER >= 11
63 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
64 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
65 #endif
66
67 return true;
68 }
69
70 int main(int, char**) {
71 test();
72 #if TEST_STD_VER > 17
73 static_assert(test());
74 #endif
75
76 return 0;
77 }