Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <string> |
| 11 | |
| 12 | // void resize(size_type n); |
| 13 | |
| 14 | #include <string> |
| 15 | #include <stdexcept> |
| 16 | #include <cassert> |
| 17 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 18 | #include "min_allocator.h" |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 19 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | template <class S> |
| 21 | void |
| 22 | test(S s, typename S::size_type n, S expected) |
| 23 | { |
| 24 | try |
| 25 | { |
| 26 | s.resize(n); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 27 | assert(s.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | assert(n <= s.max_size()); |
| 29 | assert(s == expected); |
| 30 | } |
| 31 | catch (std::length_error&) |
| 32 | { |
| 33 | assert(n > s.max_size()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | int main() |
| 38 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 39 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | typedef std::string S; |
| 41 | test(S(), 0, S()); |
| 42 | test(S(), 1, S(1, '\0')); |
| 43 | test(S(), 10, S(10, '\0')); |
| 44 | test(S(), 100, S(100, '\0')); |
| 45 | test(S("12345"), 0, S()); |
| 46 | test(S("12345"), 2, S("12")); |
| 47 | test(S("12345"), 5, S("12345")); |
| 48 | test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); |
| 49 | test(S("12345678901234567890123456789012345678901234567890"), 0, S()); |
| 50 | test(S("12345678901234567890123456789012345678901234567890"), 10, |
| 51 | S("1234567890")); |
| 52 | test(S("12345678901234567890123456789012345678901234567890"), 50, |
| 53 | S("12345678901234567890123456789012345678901234567890")); |
| 54 | test(S("12345678901234567890123456789012345678901234567890"), 60, |
| 55 | S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); |
| 56 | test(S(), S::npos, S("not going to happen")); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 57 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 58 | #if __cplusplus >= 201103L |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 59 | { |
| 60 | typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; |
| 61 | test(S(), 0, S()); |
| 62 | test(S(), 1, S(1, '\0')); |
| 63 | test(S(), 10, S(10, '\0')); |
| 64 | test(S(), 100, S(100, '\0')); |
| 65 | test(S("12345"), 0, S()); |
| 66 | test(S("12345"), 2, S("12")); |
| 67 | test(S("12345"), 5, S("12345")); |
| 68 | test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15)); |
| 69 | test(S("12345678901234567890123456789012345678901234567890"), 0, S()); |
| 70 | test(S("12345678901234567890123456789012345678901234567890"), 10, |
| 71 | S("1234567890")); |
| 72 | test(S("12345678901234567890123456789012345678901234567890"), 50, |
| 73 | S("12345678901234567890123456789012345678901234567890")); |
| 74 | test(S("12345678901234567890123456789012345678901234567890"), 60, |
| 75 | S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); |
| 76 | test(S(), S::npos, S("not going to happen")); |
| 77 | } |
| 78 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | } |