| 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 | // XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 12 | |
| 13 | // <string> |
| 14 | |
| 15 | // size_type max_size() const; // constexpr since C++20 |
| 16 | |
| 17 | // NOTE: asan and msan will fail for one of two reasons |
| 18 | // 1. If allocator_may_return_null=0 then they will fail because the allocation |
| 19 | // returns null. |
| 20 | // 2. If allocator_may_return_null=1 then they will fail because the allocation |
| 21 | // is too large to succeed. |
| 22 | // UNSUPPORTED: sanitizer-new-delete |
| 23 | |
| 24 | #include <string> |
| 25 | #include <cassert> |
| 26 | #include <new> |
| 27 | |
| 28 | #include "test_macros.h" |
| 29 | #include "min_allocator.h" |
| 30 | |
| 31 | template <class S> |
| 32 | TEST_CONSTEXPR_CXX20 void test_resize_max_size_minus_1(const S& s) { |
| 33 | S s2(s); |
| 34 | const std::size_t sz = s2.max_size() - 1; |
| 35 | try { |
| 36 | s2.resize(sz, 'x'); |
| 37 | } catch (const std::bad_alloc&) { |
| 38 | return; |
| 39 | } |
| 40 | assert(s2.size() == sz); |
| 41 | } |
| 42 | |
| 43 | template <class S> |
| 44 | TEST_CONSTEXPR_CXX20 void test_resize_max_size(const S& s) { |
| 45 | S s2(s); |
| 46 | const std::size_t sz = s2.max_size(); |
| 47 | try { |
| 48 | s2.resize(sz, 'x'); |
| 49 | } catch (const std::bad_alloc&) { |
| 50 | return; |
| 51 | } |
| 52 | assert(s2.size() == sz); |
| 53 | } |
| 54 | |
| 55 | template <class S> |
| 56 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 57 | { |
| 58 | S s; |
| 59 | assert(s.max_size() >= s.size()); |
| 60 | assert(s.max_size() > 0); |
| 61 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 62 | test_resize_max_size_minus_1(s); |
| 63 | test_resize_max_size(s); |
| 64 | } |
| 65 | } |
| 66 | { |
| 67 | S s("123" ); |
| 68 | assert(s.max_size() >= s.size()); |
| 69 | assert(s.max_size() > 0); |
| 70 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 71 | test_resize_max_size_minus_1(s); |
| 72 | test_resize_max_size(s); |
| 73 | } |
| 74 | } |
| 75 | { |
| 76 | S s("12345678901234567890123456789012345678901234567890" ); |
| 77 | assert(s.max_size() >= s.size()); |
| 78 | assert(s.max_size() > 0); |
| 79 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 80 | test_resize_max_size_minus_1(s); |
| 81 | test_resize_max_size(s); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_CONSTEXPR_CXX20 bool test() { |
| 87 | test_string<std::string>(); |
| 88 | #if TEST_STD_VER >= 11 |
| 89 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >(); |
| 90 | test_string<std::basic_string<char, std::char_traits<char>, tiny_size_allocator<64, char> > >(); |
| 91 | #endif |
| 92 | |
| 93 | { // Test resizing where we can assume that the allocation succeeds |
| 94 | std::basic_string<char, std::char_traits<char>, tiny_size_allocator<32, char> > str; |
| 95 | auto max_size = str.max_size(); |
| 96 | str.resize(max_size); |
| 97 | assert(str.size() == max_size); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | int main(int, char**) { |
| 104 | test(); |
| 105 | #if TEST_STD_VER >= 20 |
| 106 | static_assert(test()); |
| 107 | #endif |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |