| 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 |
#include "asan_testing.h" |
| 20 |
|
| 21 |
template <class S> |
| 22 |
TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type n, S expected) { |
| 23 |
if (n <= s.max_size()) { |
| 24 |
s.resize(n); |
| 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); |
| 33 |
assert(false); |
| 34 |
} catch (std::length_error&) { |
| 35 |
assert(n > s.max_size()); |
| 36 |
} |
| 37 |
} |
| 38 |
#endif |
| 39 |
} |
| 40 |
|
| 41 |
template <class S> |
| 42 |
TEST_CONSTEXPR_CXX20 void test_string() { |
| 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, S("1234567890")); |
| 53 |
test(S("12345678901234567890123456789012345678901234567890"), |
| 54 |
50, |
| 55 |
S("12345678901234567890123456789012345678901234567890")); |
| 56 |
test(S("12345678901234567890123456789012345678901234567890"), |
| 57 |
60, |
| 58 |
S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60)); |
| 59 |
test(S(), S::npos, S("not going to happen")); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_CONSTEXPR_CXX20 bool test() { |
| 63 |
test_string<std::string>(); |
| 64 |
#if TEST_STD_VER >= 11 |
| 65 |
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 66 |
test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>(); |
| 67 |
#endif |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
int main(int, char**) { |
| 73 |
test(); |
| 74 |
#if TEST_STD_VER > 17 |
| 75 |
static_assert(test()); |
| 76 |
#endif |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |