1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // XFAIL: libcpp-no-exceptions 11 // <string> 12 13 // void reserve(size_type res_arg=0); 14 15 #include <string> 16 #include <stdexcept> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "min_allocator.h" 21 22 template <class S> 23 void 24 test(S s) 25 { 26 typename S::size_type old_cap = s.capacity(); 27 S s0 = s; 28 s.reserve(); 29 LIBCPP_ASSERT(s.__invariants()); 30 assert(s == s0); 31 assert(s.capacity() <= old_cap); 32 assert(s.capacity() >= s.size()); 33 } 34 35 template <class S> 36 void 37 test(S s, typename S::size_type res_arg) 38 { 39 typename S::size_type old_cap = s.capacity(); 40 S s0 = s; 41 try 42 { 43 s.reserve(res_arg); 44 assert(res_arg <= s.max_size()); 45 assert(s == s0); 46 assert(s.capacity() >= res_arg); 47 assert(s.capacity() >= s.size()); 48 } 49 catch (std::length_error&) 50 { 51 assert(res_arg > s.max_size()); 52 } 53 } 54 55 int main() 56 { 57 { 58 typedef std::string S; 59 { 60 S s; 61 test(s); 62 63 s.assign(10, 'a'); 64 s.erase(5); 65 test(s); 66 67 s.assign(100, 'a'); 68 s.erase(50); 69 test(s); 70 } 71 { 72 S s; 73 test(s, 5); 74 test(s, 10); 75 test(s, 50); 76 } 77 { 78 S s(100, 'a'); 79 s.erase(50); 80 test(s, 5); 81 test(s, 10); 82 test(s, 50); 83 test(s, 100); 84 test(s, S::npos); 85 } 86 } 87 #if TEST_STD_VER >= 11 88 { 89 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 90 { 91 S s; 92 test(s); 93 94 s.assign(10, 'a'); 95 s.erase(5); 96 test(s); 97 98 s.assign(100, 'a'); 99 s.erase(50); 100 test(s); 101 } 102 { 103 S s; 104 test(s, 5); 105 test(s, 10); 106 test(s, 50); 107 } 108 { 109 S s(100, 'a'); 110 s.erase(50); 111 test(s, 5); 112 test(s, 10); 113 test(s, 50); 114 test(s, 100); 115 test(s, S::npos); 116 } 117 } 118 #endif 119 } 120