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, charT c); // 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 #include "asan_testing.h"
20
21 template <class S>
test(S s,typename S::size_type n,typename S::value_type c,S expected)22 TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type n, typename S::value_type c, S expected) {
23 if (n <= s.max_size()) {
24 s.resize(n, c);
25 LIBCPP_ASSERT(s.__invariants());
26 assert(s == expected);
27 LIBCPP_ASSERT(is_string_asan_correct(s));
28 }
29 #ifndef TEST_HAS_NO_EXCEPTIONS
30 else if (!TEST_IS_CONSTANT_EVALUATED) {
31 try {
32 s.resize(n, c);
33 assert(false);
34 } catch (std::length_error&) {
35 assert(n > s.max_size());
36 }
37 }
38 #endif
39 }
40
41 template <class S>
test_string()42 TEST_CONSTEXPR_CXX20 void test_string() {
43 test(S(), 0, 'a', S());
44 test(S(), 1, 'a', S("a"));
45 test(S(), 10, 'a', S(10, 'a'));
46 test(S(), 100, 'a', S(100, 'a'));
47 test(S("12345"), 0, 'a', S());
48 test(S("12345"), 2, 'a', S("12"));
49 test(S("12345"), 5, 'a', S("12345"));
50 test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
51 test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
52 test(S("12345678901234567890123456789012345678901234567890"), 10, 'a', S("1234567890"));
53 test(S("12345678901234567890123456789012345678901234567890"),
54 50,
55 'a',
56 S("12345678901234567890123456789012345678901234567890"));
57 test(S("12345678901234567890123456789012345678901234567890"),
58 60,
59 'a',
60 S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
61 test(S(), S::npos, 'a', S("not going to happen"));
62 //ASan:
63 test(S(), 21, 'a', S("aaaaaaaaaaaaaaaaaaaaa"));
64 test(S(), 22, 'a', S("aaaaaaaaaaaaaaaaaaaaaa"));
65 test(S(), 23, 'a', S("aaaaaaaaaaaaaaaaaaaaaaa"));
66 test(S(), 24, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaa"));
67 test(S(), 29, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
68 test(S(), 30, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
69 test(S(), 31, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
70 test(S(), 32, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
71 test(S(), 33, 'a', S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
72 }
73
test()74 TEST_CONSTEXPR_CXX20 bool test() {
75 test_string<std::string>();
76 #if TEST_STD_VER >= 11
77 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
78 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
79 #endif
80
81 return true;
82 }
83
main(int,char **)84 int main(int, char**) {
85 test();
86 #if TEST_STD_VER > 17
87 static_assert(test());
88 #endif
89
90 return 0;
91 }
92