xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/max_size.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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: libcpp-no-exceptions
10 // <string>
11 
12 // size_type max_size() const;
13 
14 // NOTE: asan and msan will fail for one of two reasons
15 // 1. If allocator_may_return_null=0 then they will fail because the allocation
16 //    returns null.
17 // 2. If allocator_may_return_null=1 then they will fail because the allocation
18 //    is too large to succeed.
19 // UNSUPPORTED: sanitizer-new-delete
20 
21 #include <string>
22 #include <cassert>
23 
24 #include "min_allocator.h"
25 
26 template <class S>
27 void
28 test1(const S& s)
29 {
30     S s2(s);
31     const size_t sz = s2.max_size() - 1;
32     try { s2.resize(sz, 'x'); }
33     catch ( const std::bad_alloc & ) { return ; }
34     assert ( s2.size() ==  sz );
35 }
36 
37 template <class S>
38 void
39 test2(const S& s)
40 {
41     S s2(s);
42     const size_t sz = s2.max_size();
43     try { s2.resize(sz, 'x'); }
44     catch ( const std::bad_alloc & ) { return ; }
45     assert ( s.size() ==  sz );
46 }
47 
48 template <class S>
49 void
50 test(const S& s)
51 {
52     assert(s.max_size() >= s.size());
53     test1(s);
54     test2(s);
55 }
56 
57 int main()
58 {
59     {
60     typedef std::string S;
61     test(S());
62     test(S("123"));
63     test(S("12345678901234567890123456789012345678901234567890"));
64     }
65 #if TEST_STD_VER >= 11
66     {
67     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
68     test(S());
69     test(S("123"));
70     test(S("12345678901234567890123456789012345678901234567890"));
71     }
72 #endif
73 }
74