106 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <string>10 11// void reserve(size_type res_arg); // constexpr since C++2012 13// This test relies on https://llvm.org/PR45368 (841132e) being fixed, which isn't in14// older Apple dylibs.15// XFAIL: using-built-library-before-llvm-1216 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 25template <class S>26TEST_CONSTEXPR_CXX20 void27test(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 > 1735 typename S::size_type old_cap = s.capacity();36#endif37 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 > 1746 assert(s.capacity() >= old_cap); // reserve never shrinks as of P0966 (C++20)47#endif48 }49#ifndef TEST_HAS_NO_EXCEPTIONS50 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#endif60}61 62template <class S>63TEST_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 expected80 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 89TEST_CONSTEXPR_CXX20 bool test() {90 test_string<std::string>();91#if TEST_STD_VER >= 1192 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();93#endif94 95 return true;96}97 98int main(int, char**) {99 test();100#if TEST_STD_VER > 17101 static_assert(test());102#endif103 104 return 0;105}106