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>& operator+=(const charT* s); // constexpr since C++20
12
13 #include <string>
14 #include <cassert>
15
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 #include "asan_testing.h"
19
20 template <class S>
test(S s,const typename S::value_type * str,S expected)21 TEST_CONSTEXPR_CXX20 void test(S s, const typename S::value_type* str, S expected) {
22 s += str;
23 LIBCPP_ASSERT(s.__invariants());
24 assert(s == expected);
25 LIBCPP_ASSERT(is_string_asan_correct(s));
26 }
27
28 template <class S>
test_string()29 TEST_CONSTEXPR_CXX20 void test_string() {
30 test(S(), "", S());
31 test(S(), "12345", S("12345"));
32 test(S(), "1234567890", S("1234567890"));
33 test(S(), "12345678901234567890", S("12345678901234567890"));
34
35 test(S("12345"), "", S("12345"));
36 test(S("12345"), "12345", S("1234512345"));
37 test(S("12345"), "1234567890", S("123451234567890"));
38 test(S("12345"), "12345678901234567890", S("1234512345678901234567890"));
39
40 test(S("1234567890"), "", S("1234567890"));
41 test(S("1234567890"), "12345", S("123456789012345"));
42 test(S("1234567890"), "1234567890", S("12345678901234567890"));
43 test(S("1234567890"), "12345678901234567890", S("123456789012345678901234567890"));
44
45 test(S("12345678901234567890"), "", S("12345678901234567890"));
46 test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
47 test(S("12345678901234567890"), "1234567890", S("123456789012345678901234567890"));
48 test(S("12345678901234567890"), "12345678901234567890", S("1234567890123456789012345678901234567890"));
49
50 // Starting from long string (no SSO)
51 test(S("1234567890123456789012345678901234567890"), "", S("1234567890123456789012345678901234567890"));
52 test(S("1234567890123456789012345678901234567890"), "12345", S("123456789012345678901234567890123456789012345"));
53 test(S("1234567890123456789012345678901234567890"),
54 "12345678901234567890",
55 S("123456789012345678901234567890123456789012345678901234567890"));
56 test(S("1234567890123456789012345678901234567890"),
57 "1234567890123456789012345678901234567890",
58 S("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
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