| 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 reserve(size_type res_arg); // constexpr since C++20 |
| 12 | |
| 13 | // This test relies on https://llvm.org/PR45368 (841132e) being fixed, which isn't in |
| 14 | // older Apple dylibs. |
| 15 | // XFAIL: using-built-library-before-llvm-12 |
| 16 | |
| 17 | #include <string> |
| 18 | #include <stdexcept> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | #include "min_allocator.h" |
| 23 | #include "asan_testing.h" |
| 24 | |
| 25 | template <class S> |
| 26 | TEST_CONSTEXPR_CXX20 void |
| 27 | test(typename S::size_type min_cap, typename S::size_type erased_index, typename S::size_type res_arg) { |
| 28 | S s(min_cap, 'a'); |
| 29 | s.erase(erased_index); |
| 30 | assert(s.size() == erased_index); |
| 31 | assert(s.capacity() >= min_cap); // Check that we really have at least this capacity. |
| 32 | LIBCPP_ASSERT(is_string_asan_correct(s)); |
| 33 | |
| 34 | #if TEST_STD_VER > 17 |
| 35 | typename S::size_type old_cap = s.capacity(); |
| 36 | #endif |
| 37 | S s0 = s; |
| 38 | if (res_arg <= s.max_size()) { |
| 39 | s.reserve(res_arg); |
| 40 | LIBCPP_ASSERT(s.__invariants()); |
| 41 | assert(s == s0); |
| 42 | assert(s.capacity() >= res_arg); |
| 43 | assert(s.capacity() >= s.size()); |
| 44 | LIBCPP_ASSERT(is_string_asan_correct(s)); |
| 45 | #if TEST_STD_VER > 17 |
| 46 | assert(s.capacity() >= old_cap); // reserve never shrinks as of P0966 (C++20) |
| 47 | #endif |
| 48 | } |
| 49 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 50 | else if (!TEST_IS_CONSTANT_EVALUATED) { |
| 51 | try { |
| 52 | s.reserve(res_arg); |
| 53 | LIBCPP_ASSERT(s.__invariants()); |
| 54 | assert(false); |
| 55 | } catch (std::length_error&) { |
| 56 | assert(res_arg > s.max_size()); |
| 57 | } |
| 58 | } |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | template <class S> |
| 63 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 64 | { |
| 65 | test<S>(0, 0, 5); |
| 66 | test<S>(0, 0, 10); |
| 67 | test<S>(0, 0, 50); |
| 68 | } |
| 69 | { |
| 70 | test<S>(100, 1, 5); |
| 71 | test<S>(100, 50, 5); |
| 72 | test<S>(100, 50, 10); |
| 73 | test<S>(100, 50, 50); |
| 74 | test<S>(100, 50, 100); |
| 75 | test<S>(100, 50, 1000); |
| 76 | test<S>(100, 50, S::npos); |
| 77 | } |
| 78 | |
| 79 | { // Check that growing twice works as expected |
| 80 | S str; |
| 81 | str.reserve(50); |
| 82 | assert(str.capacity() >= 50); |
| 83 | size_t old_cap = str.capacity(); |
| 84 | str.reserve(str.capacity() + 1); |
| 85 | assert(str.capacity() > old_cap); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | TEST_CONSTEXPR_CXX20 bool test() { |
| 90 | test_string<std::string>(); |
| 91 | #if TEST_STD_VER >= 11 |
| 92 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 93 | #endif |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | int main(int, char**) { |
| 99 | test(); |
| 100 | #if TEST_STD_VER > 17 |
| 101 | static_assert(test()); |
| 102 | #endif |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |