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// UNSUPPORTED: no-exceptions
10
11// After changing the alignment of the allocated pointer from 16 to 8, the exception
12// thrown is no longer `bad_alloc` but instead length_error on systems using new
13// headers but a dylib that doesn't contain 04ce0ba.
14//
15// XFAIL: using-built-library-before-llvm-19
16
17// <string>
18
19// size_type max_size() const; // constexpr since C++20
20
21#include <string>
22#include <cassert>
23#include <stdexcept>
24
25#include "test_macros.h"
26#include "min_allocator.h"
27
28template <class S>
29void test(const S& s) {
30 assert(s.max_size() >= s.size());
31 S s2(s);
32 const std::size_t sz = s2.max_size() + 1;
33 try {
34 s2.resize(sz, 'x');
35 } catch (const std::length_error&) {
36 return;
37 }
38 assert(false);
39}
40
41template <class S>
42void test_string() {
43 test(S());
44 test(S("123"));
45 test(S("12345678901234567890123456789012345678901234567890"));
46}
47
48bool test() {
49 test_string<std::string>();
50#if TEST_STD_VER >= 11
51 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
52#endif
53
54 return true;
55}
56
57int main(int, char**) {
58 test();
59
60 return 0;
61}
62

source code of libcxx/test/std/strings/basic.string/string.capacity/over_max_size.pass.cpp