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 // <string> 11 12 // void reserve(size_type res_arg=0); 13 14 #include <string> 15 #include <stdexcept> 16 #include <cassert> 17 18 #include "test_macros.h" 19 #include "min_allocator.h" 20 21 template <class S> 22 void 23 test(S s) 24 { 25 typename S::size_type old_cap = s.capacity(); 26 S s0 = s; 27 s.reserve(); 28 LIBCPP_ASSERT(s.__invariants()); 29 assert(s == s0); 30 assert(s.capacity() <= old_cap); 31 assert(s.capacity() >= s.size()); 32 } 33 34 template <class S> 35 void 36 test(S s, typename S::size_type res_arg) 37 { 38 typename S::size_type old_cap = s.capacity(); 39 S s0 = s; 40 if (res_arg <= s.max_size()) 41 { 42 s.reserve(res_arg); 43 assert(s == s0); 44 assert(s.capacity() >= res_arg); 45 assert(s.capacity() >= s.size()); 46 } 47 #ifndef TEST_HAS_NO_EXCEPTIONS 48 else 49 { 50 try 51 { 52 s.reserve(res_arg); 53 assert(false); 54 } 55 catch (std::length_error&) 56 { 57 assert(res_arg > s.max_size()); 58 } 59 } 60 #endif 61 } 62 63 int main() 64 { 65 { 66 typedef std::string S; 67 { 68 S s; 69 test(s); 70 71 s.assign(10, 'a'); 72 s.erase(5); 73 test(s); 74 75 s.assign(100, 'a'); 76 s.erase(50); 77 test(s); 78 } 79 { 80 S s; 81 test(s, 5); 82 test(s, 10); 83 test(s, 50); 84 } 85 { 86 S s(100, 'a'); 87 s.erase(50); 88 test(s, 5); 89 test(s, 10); 90 test(s, 50); 91 test(s, 100); 92 test(s, S::npos); 93 } 94 } 95 #if TEST_STD_VER >= 11 96 { 97 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 98 { 99 S s; 100 test(s); 101 102 s.assign(10, 'a'); 103 s.erase(5); 104 test(s); 105 106 s.assign(100, 'a'); 107 s.erase(50); 108 test(s); 109 } 110 { 111 S s; 112 test(s, 5); 113 test(s, 10); 114 test(s, 50); 115 } 116 { 117 S s(100, 'a'); 118 s.erase(50); 119 test(s, 5); 120 test(s, 10); 121 test(s, 50); 122 test(s, 100); 123 test(s, S::npos); 124 } 125 } 126 #endif 127 } 128