Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Marshall Clow | e34f6f6 | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 18 | #include "min_allocator.h" |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 19 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | template <class S> |
| 21 | void |
| 22 | test(S s) |
| 23 | { |
| 24 | typename S::size_type old_cap = s.capacity(); |
| 25 | S s0 = s; |
| 26 | s.reserve(); |
| 27 | assert(s.__invariants()); |
| 28 | assert(s == s0); |
| 29 | assert(s.capacity() <= old_cap); |
| 30 | assert(s.capacity() >= s.size()); |
| 31 | } |
| 32 | |
| 33 | template <class S> |
| 34 | void |
| 35 | test(S s, typename S::size_type res_arg) |
| 36 | { |
| 37 | typename S::size_type old_cap = s.capacity(); |
| 38 | S s0 = s; |
| 39 | try |
| 40 | { |
| 41 | s.reserve(res_arg); |
| 42 | assert(res_arg <= s.max_size()); |
| 43 | assert(s == s0); |
| 44 | assert(s.capacity() >= res_arg); |
| 45 | assert(s.capacity() >= s.size()); |
| 46 | } |
| 47 | catch (std::length_error&) |
| 48 | { |
| 49 | assert(res_arg > s.max_size()); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | int main() |
| 54 | { |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 55 | { |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | typedef std::string S; |
| 57 | { |
| 58 | S s; |
| 59 | test(s); |
| 60 | |
| 61 | s.assign(10, 'a'); |
| 62 | s.erase(5); |
| 63 | test(s); |
| 64 | |
| 65 | s.assign(100, 'a'); |
| 66 | s.erase(50); |
| 67 | test(s); |
| 68 | } |
| 69 | { |
| 70 | S s; |
| 71 | test(s, 5); |
| 72 | test(s, 10); |
| 73 | test(s, 50); |
| 74 | } |
| 75 | { |
| 76 | S s(100, 'a'); |
| 77 | s.erase(50); |
| 78 | test(s, 5); |
| 79 | test(s, 10); |
| 80 | test(s, 50); |
| 81 | test(s, 100); |
| 82 | test(s, S::npos); |
| 83 | } |
Howard Hinnant | eec7218 | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 84 | } |
| 85 | #if __cplusplus >= 201103L |
| 86 | { |
| 87 | typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; |
| 88 | { |
| 89 | S s; |
| 90 | test(s); |
| 91 | |
| 92 | s.assign(10, 'a'); |
| 93 | s.erase(5); |
| 94 | test(s); |
| 95 | |
| 96 | s.assign(100, 'a'); |
| 97 | s.erase(50); |
| 98 | test(s); |
| 99 | } |
| 100 | { |
| 101 | S s; |
| 102 | test(s, 5); |
| 103 | test(s, 10); |
| 104 | test(s, 50); |
| 105 | } |
| 106 | { |
| 107 | S s(100, 'a'); |
| 108 | s.erase(50); |
| 109 | test(s, 5); |
| 110 | test(s, 10); |
| 111 | test(s, 50); |
| 112 | test(s, 100); |
| 113 | test(s, S::npos); |
| 114 | } |
| 115 | } |
| 116 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | } |