[mlir][NFC] Avoid using braced initializer lists to call a constructor. (#123714)
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.capacity / reserve_size.pass.cpp
blob6196fd6836e7d1656efa620887a18e92f88a0f13
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // void reserve(size_type res_arg); // constexpr since C++20
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
17 #include <string>
18 #include <stdexcept>
19 #include <cassert>
21 #include "test_macros.h"
22 #include "min_allocator.h"
23 #include "asan_testing.h"
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));
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
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());
59 #endif
62 template <class S>
63 TEST_CONSTEXPR_CXX20 void test_string() {
65 test<S>(0, 0, 5);
66 test<S>(0, 0, 10);
67 test<S>(0, 0, 50);
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);
80 TEST_CONSTEXPR_CXX20 bool test() {
81 test_string<std::string>();
82 #if TEST_STD_VER >= 11
83 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
84 #endif
86 return true;
89 int main(int, char**) {
90 test();
91 #if TEST_STD_VER > 17
92 static_assert(test());
93 #endif
95 return 0;