Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.modifiers / string_append / pointer.pass.cpp
blob68f5838ccf2f02a660232e15f6a4ff6e6fba7f4d
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // basic_string<charT,traits,Allocator>& append(const charT* s); // constexpr since C++20
13 #include <string>
14 #include <stdexcept>
15 #include <cassert>
17 #include "test_macros.h"
18 #include "min_allocator.h"
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void test(S s, const typename S::value_type* str, S expected) {
22 s.append(str);
23 LIBCPP_ASSERT(s.__invariants());
24 assert(s == expected);
27 template <class S>
28 TEST_CONSTEXPR_CXX20 void test_string() {
29 test(S(), "", S());
30 test(S(), "12345", S("12345"));
31 test(S(), "12345678901234567890", S("12345678901234567890"));
33 test(S("12345"), "", S("12345"));
34 test(S("12345"), "12345", S("1234512345"));
35 test(S("12345"), "1234567890", S("123451234567890"));
37 test(S("12345678901234567890"), "", S("12345678901234567890"));
38 test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
39 test(S("12345678901234567890"), "12345678901234567890", S("1234567890123456789012345678901234567890"));
42 TEST_CONSTEXPR_CXX20 bool test() {
43 test_string<std::string>();
44 #if TEST_STD_VER >= 11
45 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
46 #endif
48 { // test appending to self
49 typedef std::string S;
50 S s_short = "123/";
51 S s_long = "Lorem ipsum dolor sit amet, consectetur/";
53 s_short.append(s_short.c_str());
54 assert(s_short == "123/123/");
55 s_short.append(s_short.c_str());
56 assert(s_short == "123/123/123/123/");
57 s_short.append(s_short.c_str());
58 assert(s_short == "123/123/123/123/123/123/123/123/");
60 s_long.append(s_long.c_str());
61 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
63 return true;
66 int main(int, char**) {
67 test();
68 #if TEST_STD_VER > 17
69 static_assert(test());
70 #endif
72 return 0;