xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_append/push_back.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 // void push_back(charT c) // constexpr since C++20
12 
13 #include <string>
14 #include <algorithm>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 #include "asan_testing.h"
20 
21 struct VeryLarge {
22   long long a;
23   char b;
24 };
25 
26 namespace std {
27 template <>
28 struct char_traits<VeryLarge> {
29   using char_type  = VeryLarge;
30   using int_type   = int;
31   using off_type   = streamoff;
32   using pos_type   = streampos;
33   using state_type = mbstate_t;
34 
35   static TEST_CONSTEXPR_CXX20 void assign(char_type& c1, const char_type& c2) { c1 = c2; }
36   static bool eq(char_type c1, char_type c2);
37   static bool lt(char_type c1, char_type c2);
38 
39   static int compare(const char_type* s1, const char_type* s2, std::size_t n);
40   static std::size_t length(const char_type* s);
41   static const char_type* find(const char_type* s, std::size_t n, const char_type& a);
42   static char_type* move(char_type* s1, const char_type* s2, std::size_t n);
43   static TEST_CONSTEXPR_CXX20 char_type* copy(char_type* s1, const char_type* s2, std::size_t n) {
44     std::copy_n(s2, n, s1);
45     return s1;
46   }
47   static TEST_CONSTEXPR_CXX20 char_type* assign(char_type* s, std::size_t n, char_type a) {
48     std::fill_n(s, n, a);
49     return s;
50   }
51 
52   static int_type not_eof(int_type c);
53   static char_type to_char_type(int_type c);
54   static int_type to_int_type(char_type c);
55   static bool eq_int_type(int_type c1, int_type c2);
56   static int_type eof();
57 };
58 } // end namespace std
59 
60 template <class S>
61 TEST_CONSTEXPR_CXX20 void test(S s, typename S::value_type c, S expected) {
62   s.push_back(c);
63   LIBCPP_ASSERT(s.__invariants());
64   assert(s == expected);
65   LIBCPP_ASSERT(is_string_asan_correct(s));
66 }
67 
68 template <class S>
69 TEST_CONSTEXPR_CXX20 void test_string() {
70   test(S(), 'a', S(1, 'a'));
71   test(S("12345"), 'a', S("12345a"));
72   test(S("12345678901234567890"), 'a', S("12345678901234567890a"));
73   test(S("123abcabcdefghabcdefgh"), 'a', S("123abcabcdefghabcdefgha"));
74   test(S("123abcabcdefghabcdefgha"), 'b', S("123abcabcdefghabcdefghab"));
75   test(S("123abcabcdefghabcdefghab"), 'c', S("123abcabcdefghabcdefghabc"));
76   test(S("123abcabcdefghabcdefghabc"), 'd', S("123abcabcdefghabcdefghabcd"));
77 }
78 
79 TEST_CONSTEXPR_CXX20 bool test() {
80   test_string<std::string>();
81 #if TEST_STD_VER >= 11
82   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
83   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char> > >();
84 #endif
85   {
86     // https://llvm.org/PR31454
87     std::basic_string<VeryLarge> s;
88     VeryLarge vl = {};
89     s.push_back(vl);
90     s.push_back(vl);
91     s.push_back(vl);
92   }
93 
94   return true;
95 }
96 
97 int main(int, char**) {
98   test();
99 #if TEST_STD_VER > 17
100   static_assert(test());
101 #endif
102 
103   return 0;
104 }
105