xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_append/string.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // basic_string<charT,traits,Allocator>&
12 //   append(const basic_string<charT,traits>& str); // constexpr since C++20
13 
14 #include <string>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void test(S s, S str, S expected) {
22   s.append(str);
23   LIBCPP_ASSERT(s.__invariants());
24   assert(s == expected);
25 }
26 
27 template <class S>
28 TEST_CONSTEXPR_CXX20 void test_string() {
29   test(S(), S(), S());
30   test(S(), S("12345"), S("12345"));
31   test(S(), S("1234567890"), S("1234567890"));
32   test(S(), S("12345678901234567890"), S("12345678901234567890"));
33 
34   test(S("12345"), S(), S("12345"));
35   test(S("12345"), S("12345"), S("1234512345"));
36   test(S("12345"), S("1234567890"), S("123451234567890"));
37   test(S("12345"), S("12345678901234567890"), S("1234512345678901234567890"));
38 
39   test(S("1234567890"), S(), S("1234567890"));
40   test(S("1234567890"), S("12345"), S("123456789012345"));
41   test(S("1234567890"), S("1234567890"), S("12345678901234567890"));
42   test(S("1234567890"), S("12345678901234567890"), S("123456789012345678901234567890"));
43 
44   test(S("12345678901234567890"), S(), S("12345678901234567890"));
45   test(S("12345678901234567890"), S("12345"), S("1234567890123456789012345"));
46   test(S("12345678901234567890"), S("1234567890"), S("123456789012345678901234567890"));
47   test(S("12345678901234567890"), S("12345678901234567890"), S("1234567890123456789012345678901234567890"));
48 }
49 
50 TEST_CONSTEXPR_CXX20 bool test() {
51   test_string<std::string>();
52 #if TEST_STD_VER >= 11
53   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
54 #endif
55 
56 #if TEST_STD_VER >= 11
57   { // LWG 2946
58     std::string s;
59     s.append({"abc", 1});
60     assert(s.size() == 1);
61     assert(s == "a");
62   }
63 #endif
64 
65   return true;
66 }
67 
68 int main(int, char**) {
69   test();
70 #if TEST_STD_VER > 17
71   static_assert(test());
72 #endif
73 
74   return 0;
75 }
76