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 24 #include "test_macros.h" 25 #include "min_allocator.h" 26 27 template <class S> 28 TEST_CONSTEXPR_CXX20 void test1(const S & s)29test1(const S& s) 30 { 31 S s2(s); 32 const size_t sz = s2.max_size() - 1; 33 try { s2.resize(sz, 'x'); } 34 catch ( const std::bad_alloc & ) { return ; } 35 assert ( s2.size() == sz ); 36 } 37 38 template <class S> 39 TEST_CONSTEXPR_CXX20 void test2(const S & s)40test2(const S& s) 41 { 42 S s2(s); 43 const size_t sz = s2.max_size(); 44 try { s2.resize(sz, 'x'); } 45 catch ( const std::bad_alloc & ) { return ; } 46 assert ( s.size() == sz ); 47 } 48 49 template <class S> 50 TEST_CONSTEXPR_CXX20 void test(const S & s)51test(const S& s) 52 { 53 assert(s.max_size() >= s.size()); 54 test1(s); 55 test2(s); 56 } 57 test()58void test() { 59 { 60 typedef std::string S; 61 test(S()); 62 test(S("123")); 63 test(S("12345678901234567890123456789012345678901234567890")); 64 } 65 #if TEST_STD_VER >= 11 66 { 67 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 68 test(S()); 69 test(S("123")); 70 test(S("12345678901234567890123456789012345678901234567890")); 71 } 72 #endif 73 } 74 75 #if TEST_STD_VER > 17 test_constexpr()76constexpr 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 main(int,char **)86int 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