15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
98c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
105a83710eSEric Fiselier // <string>
115a83710eSEric Fiselier 
12*425620ccSNikolas Klauser // size_type max_size() const; // constexpr since C++20
135a83710eSEric Fiselier 
145a83710eSEric Fiselier // NOTE: asan and msan will fail for one of two reasons
155a83710eSEric Fiselier // 1. If allocator_may_return_null=0 then they will fail because the allocation
165a83710eSEric Fiselier //    returns null.
175a83710eSEric Fiselier // 2. If allocator_may_return_null=1 then they will fail because the allocation
185a83710eSEric Fiselier //    is too large to succeed.
194b7533a1SEric Fiselier // UNSUPPORTED: sanitizer-new-delete
205a83710eSEric Fiselier 
215a83710eSEric Fiselier #include <string>
225a83710eSEric Fiselier #include <cassert>
235a83710eSEric Fiselier 
247fc6a556SMarshall Clow #include "test_macros.h"
255a83710eSEric Fiselier #include "min_allocator.h"
265a83710eSEric Fiselier 
275a83710eSEric Fiselier template <class S>
28*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 void
test1(const S & s)295a83710eSEric Fiselier test1(const S& s)
305a83710eSEric Fiselier {
315a83710eSEric Fiselier     S s2(s);
325a83710eSEric Fiselier     const size_t sz = s2.max_size() - 1;
335a83710eSEric Fiselier     try { s2.resize(sz, 'x'); }
345a83710eSEric Fiselier     catch ( const std::bad_alloc & ) { return ; }
355a83710eSEric Fiselier     assert ( s2.size() ==  sz );
365a83710eSEric Fiselier }
375a83710eSEric Fiselier 
385a83710eSEric Fiselier template <class S>
39*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 void
test2(const S & s)405a83710eSEric Fiselier test2(const S& s)
415a83710eSEric Fiselier {
425a83710eSEric Fiselier     S s2(s);
435a83710eSEric Fiselier     const size_t sz = s2.max_size();
445a83710eSEric Fiselier     try { s2.resize(sz, 'x'); }
455a83710eSEric Fiselier     catch ( const std::bad_alloc & ) { return ; }
465a83710eSEric Fiselier     assert ( s.size() ==  sz );
475a83710eSEric Fiselier }
485a83710eSEric Fiselier 
495a83710eSEric Fiselier template <class S>
50*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(const S & s)515a83710eSEric Fiselier test(const S& s)
525a83710eSEric Fiselier {
535a83710eSEric Fiselier     assert(s.max_size() >= s.size());
545a83710eSEric Fiselier     test1(s);
555a83710eSEric Fiselier     test2(s);
565a83710eSEric Fiselier }
575a83710eSEric Fiselier 
test()58*425620ccSNikolas Klauser void test() {
595a83710eSEric Fiselier   {
605a83710eSEric Fiselier     typedef std::string S;
615a83710eSEric Fiselier     test(S());
625a83710eSEric Fiselier     test(S("123"));
635a83710eSEric Fiselier     test(S("12345678901234567890123456789012345678901234567890"));
645a83710eSEric Fiselier   }
65f2f2a639SEric Fiselier #if TEST_STD_VER >= 11
665a83710eSEric Fiselier   {
675a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
685a83710eSEric Fiselier     test(S());
695a83710eSEric Fiselier     test(S("123"));
705a83710eSEric Fiselier     test(S("12345678901234567890123456789012345678901234567890"));
715a83710eSEric Fiselier   }
725a83710eSEric Fiselier #endif
73*425620ccSNikolas Klauser }
74*425620ccSNikolas Klauser 
75*425620ccSNikolas Klauser #if TEST_STD_VER > 17
test_constexpr()76*425620ccSNikolas Klauser constexpr bool test_constexpr() {
77*425620ccSNikolas Klauser   std::string str;
78*425620ccSNikolas Klauser 
79*425620ccSNikolas Klauser   size_t size = str.max_size();
80*425620ccSNikolas Klauser   assert(size > 0);
812df59c50SJF Bastien 
82e85018b7SNikolas Klauser   return true;
83e85018b7SNikolas Klauser }
84*425620ccSNikolas Klauser #endif
85e85018b7SNikolas Klauser 
main(int,char **)86e85018b7SNikolas Klauser int main(int, char**)
87e85018b7SNikolas Klauser {
88e85018b7SNikolas Klauser   test();
89e85018b7SNikolas Klauser #if TEST_STD_VER > 17
90*425620ccSNikolas Klauser   test_constexpr();
91*425620ccSNikolas Klauser   static_assert(test_constexpr());
92e85018b7SNikolas Klauser #endif
93e85018b7SNikolas Klauser 
942df59c50SJF Bastien   return 0;
955a83710eSEric Fiselier }
96