xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/resize_size.pass.cpp (revision 8a915ed644b910ae9d7d2aef7ad491791fbd4944)
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 "test_macros.h"
19 #include "min_allocator.h"
20 
21 template <class S>
22 void
23 test(S s, typename S::size_type n, S expected)
24 {
25     if (n <= s.max_size())
26     {
27         s.resize(n);
28         LIBCPP_ASSERT(s.__invariants());
29         assert(s == expected);
30     }
31 #ifndef TEST_HAS_NO_EXCEPTIONS
32     else
33     {
34         try
35         {
36             s.resize(n);
37             assert(false);
38         }
39         catch (std::length_error&)
40         {
41             assert(n > s.max_size());
42         }
43     }
44 #endif
45 }
46 
47 int main()
48 {
49     {
50     typedef std::string S;
51     test(S(), 0, S());
52     test(S(), 1, S(1, '\0'));
53     test(S(), 10, S(10, '\0'));
54     test(S(), 100, S(100, '\0'));
55     test(S("12345"), 0, S());
56     test(S("12345"), 2, S("12"));
57     test(S("12345"), 5, S("12345"));
58     test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
59     test(S("12345678901234567890123456789012345678901234567890"), 0, S());
60     test(S("12345678901234567890123456789012345678901234567890"), 10,
61          S("1234567890"));
62     test(S("12345678901234567890123456789012345678901234567890"), 50,
63          S("12345678901234567890123456789012345678901234567890"));
64     test(S("12345678901234567890123456789012345678901234567890"), 60,
65          S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
66     test(S(), S::npos, S("not going to happen"));
67     }
68 #if TEST_STD_VER >= 11
69     {
70     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
71     test(S(), 0, S());
72     test(S(), 1, S(1, '\0'));
73     test(S(), 10, S(10, '\0'));
74     test(S(), 100, S(100, '\0'));
75     test(S("12345"), 0, S());
76     test(S("12345"), 2, S("12"));
77     test(S("12345"), 5, S("12345"));
78     test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
79     test(S("12345678901234567890123456789012345678901234567890"), 0, S());
80     test(S("12345678901234567890123456789012345678901234567890"), 10,
81          S("1234567890"));
82     test(S("12345678901234567890123456789012345678901234567890"), 50,
83          S("12345678901234567890123456789012345678901234567890"));
84     test(S("12345678901234567890123456789012345678901234567890"), 60,
85          S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
86     test(S(), S::npos, S("not going to happen"));
87     }
88 #endif
89 }
90