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 resize(size_type n); 13 14 #include <string> 15 #include <stdexcept> 16 #include <cassert> 17 18 #include "min_allocator.h" 19 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); 27 assert(s.__invariants()); 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 { 39 { 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")); 57 } 58 #if __cplusplus >= 201103L 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 79 } 80