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 | |
| 18 | template <class S> |
| 19 | void |
| 20 | test(S s) |
| 21 | { |
| 22 | typename S::size_type old_cap = s.capacity(); |
| 23 | S s0 = s; |
| 24 | s.reserve(); |
| 25 | assert(s.__invariants()); |
| 26 | assert(s == s0); |
| 27 | assert(s.capacity() <= old_cap); |
| 28 | assert(s.capacity() >= s.size()); |
| 29 | } |
| 30 | |
| 31 | template <class S> |
| 32 | void |
| 33 | test(S s, typename S::size_type res_arg) |
| 34 | { |
| 35 | typename S::size_type old_cap = s.capacity(); |
| 36 | S s0 = s; |
| 37 | try |
| 38 | { |
| 39 | s.reserve(res_arg); |
| 40 | assert(res_arg <= s.max_size()); |
| 41 | assert(s == s0); |
| 42 | assert(s.capacity() >= res_arg); |
| 43 | assert(s.capacity() >= s.size()); |
| 44 | } |
| 45 | catch (std::length_error&) |
| 46 | { |
| 47 | assert(res_arg > s.max_size()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | int main() |
| 52 | { |
| 53 | typedef std::string S; |
| 54 | { |
| 55 | S s; |
| 56 | test(s); |
| 57 | |
| 58 | s.assign(10, 'a'); |
| 59 | s.erase(5); |
| 60 | test(s); |
| 61 | |
| 62 | s.assign(100, 'a'); |
| 63 | s.erase(50); |
| 64 | test(s); |
| 65 | } |
| 66 | { |
| 67 | S s; |
| 68 | test(s, 5); |
| 69 | test(s, 10); |
| 70 | test(s, 50); |
| 71 | } |
| 72 | { |
| 73 | S s(100, 'a'); |
| 74 | s.erase(50); |
| 75 | test(s, 5); |
| 76 | test(s, 10); |
| 77 | test(s, 50); |
| 78 | test(s, 100); |
| 79 | test(s, S::npos); |
| 80 | } |
| 81 | } |