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