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 resize(size_type n); 12 13 // When back-deploying to macosx10.7, the RTTI for exception classes 14 // incorrectly provided by libc++.dylib is mixed with the one in 15 // libc++abi.dylib and exceptions are not caught properly. 16 // XFAIL: with_system_cxx_lib=macosx10.7 17 18 #include <string> 19 #include <stdexcept> 20 #include <cassert> 21 22 #include "test_macros.h" 23 #include "min_allocator.h" 24 25 template <class S> 26 void 27 test(S s, typename S::size_type n, S expected) 28 { 29 if (n <= s.max_size()) 30 { 31 s.resize(n); 32 LIBCPP_ASSERT(s.__invariants()); 33 assert(s == expected); 34 } 35 #ifndef TEST_HAS_NO_EXCEPTIONS 36 else 37 { 38 try 39 { 40 s.resize(n); 41 assert(false); 42 } 43 catch (std::length_error&) 44 { 45 assert(n > s.max_size()); 46 } 47 } 48 #endif 49 } 50 51 int main(int, char**) 52 { 53 { 54 typedef std::string S; 55 test(S(), 0, S()); 56 test(S(), 1, S(1, '\0')); 57 test(S(), 10, S(10, '\0')); 58 test(S(), 100, S(100, '\0')); 59 test(S("12345"), 0, S()); 60 test(S("12345"), 2, S("12")); 61 test(S("12345"), 5, S("12345")); 62 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); 63 test(S("12345678901234567890123456789012345678901234567890"), 0, S()); 64 test(S("12345678901234567890123456789012345678901234567890"), 10, 65 S("1234567890")); 66 test(S("12345678901234567890123456789012345678901234567890"), 50, 67 S("12345678901234567890123456789012345678901234567890")); 68 test(S("12345678901234567890123456789012345678901234567890"), 60, 69 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); 70 test(S(), S::npos, S("not going to happen")); 71 } 72 #if TEST_STD_VER >= 11 73 { 74 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 75 test(S(), 0, S()); 76 test(S(), 1, S(1, '\0')); 77 test(S(), 10, S(10, '\0')); 78 test(S(), 100, S(100, '\0')); 79 test(S("12345"), 0, S()); 80 test(S("12345"), 2, S("12")); 81 test(S("12345"), 5, S("12345")); 82 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); 83 test(S("12345678901234567890123456789012345678901234567890"), 0, S()); 84 test(S("12345678901234567890123456789012345678901234567890"), 10, 85 S("1234567890")); 86 test(S("12345678901234567890123456789012345678901234567890"), 50, 87 S("12345678901234567890123456789012345678901234567890")); 88 test(S("12345678901234567890123456789012345678901234567890"), 60, 89 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); 90 test(S(), S::npos, S("not going to happen")); 91 } 92 #endif 93 94 return 0; 95 } 96