1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // void resize(size_type n); // constexpr since C++20
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 TEST_CONSTEXPR_CXX20 void
24 test(S s, typename S::size_type n, S expected)
25 {
26     if (n <= s.max_size())
27     {
28         s.resize(n);
29         LIBCPP_ASSERT(s.__invariants());
30         assert(s == expected);
31     }
32 #ifndef TEST_HAS_NO_EXCEPTIONS
33     else if (!TEST_IS_CONSTANT_EVALUATED)
34     {
35         try
36         {
37             s.resize(n);
38             assert(false);
39         }
40         catch (std::length_error&)
41         {
42             assert(n > s.max_size());
43         }
44     }
45 #endif
46 }
47 
48 TEST_CONSTEXPR_CXX20 bool test() {
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   return true;
91 }
92 
93 int main(int, char**)
94 {
95   test();
96 #if TEST_STD_VER > 17
97   static_assert(test());
98 #endif
99 
100   return 0;
101 }
102