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 // UNSUPPORTED: no-exceptions 10 // <string> 11 12 // size_type max_size() const; // constexpr since C++20 13 14 // NOTE: asan and msan will fail for one of two reasons 15 // 1. If allocator_may_return_null=0 then they will fail because the allocation 16 // returns null. 17 // 2. If allocator_may_return_null=1 then they will fail because the allocation 18 // is too large to succeed. 19 // UNSUPPORTED: sanitizer-new-delete 20 21 #include <string> 22 #include <cassert> 23 #include <new> 24 25 #include "test_macros.h" 26 #include "min_allocator.h" 27 28 template <class S> 29 TEST_CONSTEXPR_CXX20 void 30 test1(const S& s) 31 { 32 S s2(s); 33 const size_t sz = s2.max_size() - 1; 34 try { s2.resize(sz, 'x'); } 35 catch ( const std::bad_alloc & ) { return ; } 36 assert ( s2.size() == sz ); 37 } 38 39 template <class S> 40 TEST_CONSTEXPR_CXX20 void 41 test2(const S& s) 42 { 43 S s2(s); 44 const size_t sz = s2.max_size(); 45 try { s2.resize(sz, 'x'); } 46 catch ( const std::bad_alloc & ) { return ; } 47 assert ( s.size() == sz ); 48 } 49 50 template <class S> 51 TEST_CONSTEXPR_CXX20 void 52 test(const S& s) 53 { 54 assert(s.max_size() >= s.size()); 55 test1(s); 56 test2(s); 57 } 58 59 template <class S> 60 TEST_CONSTEXPR_CXX20 void test_string() { 61 test(S()); 62 test(S("123")); 63 test(S("12345678901234567890123456789012345678901234567890")); 64 } 65 66 TEST_CONSTEXPR_CXX20 bool test() { 67 test_string<std::string>(); 68 #if TEST_STD_VER >= 11 69 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); 70 #endif 71 72 return true; 73 } 74 75 #if TEST_STD_VER > 17 76 constexpr bool test_constexpr() { 77 std::string str; 78 79 size_t size = str.max_size(); 80 assert(size > 0); 81 82 return true; 83 } 84 #endif 85 86 int main(int, char**) 87 { 88 test(); 89 #if TEST_STD_VER > 17 90 test_constexpr(); 91 static_assert(test_constexpr()); 92 #endif 93 94 return 0; 95 } 96