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 // <string>
10
11 // void resize(size_type n); // constexpr since C++20
12
13 #include <string>
14 #include <stdexcept>
15 #include <cassert>
16
17 #include "test_macros.h"
18 #include "min_allocator.h"
19
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void
test(S s,typename S::size_type n,S expected)22 test(S s, typename S::size_type n, S expected)
23 {
24 if (n <= s.max_size())
25 {
26 s.resize(n);
27 LIBCPP_ASSERT(s.__invariants());
28 assert(s == expected);
29 }
30 #ifndef TEST_HAS_NO_EXCEPTIONS
31 else if (!TEST_IS_CONSTANT_EVALUATED)
32 {
33 try
34 {
35 s.resize(n);
36 assert(false);
37 }
38 catch (std::length_error&)
39 {
40 assert(n > s.max_size());
41 }
42 }
43 #endif
44 }
45
test()46 TEST_CONSTEXPR_CXX20 bool test() {
47 {
48 typedef std::string S;
49 test(S(), 0, S());
50 test(S(), 1, S(1, '\0'));
51 test(S(), 10, S(10, '\0'));
52 test(S(), 100, S(100, '\0'));
53 test(S("12345"), 0, S());
54 test(S("12345"), 2, S("12"));
55 test(S("12345"), 5, S("12345"));
56 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
57 test(S("12345678901234567890123456789012345678901234567890"), 0, S());
58 test(S("12345678901234567890123456789012345678901234567890"), 10,
59 S("1234567890"));
60 test(S("12345678901234567890123456789012345678901234567890"), 50,
61 S("12345678901234567890123456789012345678901234567890"));
62 test(S("12345678901234567890123456789012345678901234567890"), 60,
63 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
64 test(S(), S::npos, S("not going to happen"));
65 }
66 #if TEST_STD_VER >= 11
67 {
68 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
69 test(S(), 0, S());
70 test(S(), 1, S(1, '\0'));
71 test(S(), 10, S(10, '\0'));
72 test(S(), 100, S(100, '\0'));
73 test(S("12345"), 0, S());
74 test(S("12345"), 2, S("12"));
75 test(S("12345"), 5, S("12345"));
76 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
77 test(S("12345678901234567890123456789012345678901234567890"), 0, S());
78 test(S("12345678901234567890123456789012345678901234567890"), 10,
79 S("1234567890"));
80 test(S("12345678901234567890123456789012345678901234567890"), 50,
81 S("12345678901234567890123456789012345678901234567890"));
82 test(S("12345678901234567890123456789012345678901234567890"), 60,
83 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
84 test(S(), S::npos, S("not going to happen"));
85 }
86 #endif
87
88 return true;
89 }
90
main(int,char **)91 int main(int, char**)
92 {
93 test();
94 #if TEST_STD_VER > 17
95 static_assert(test());
96 #endif
97
98 return 0;
99 }
100