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