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 
95a83710eSEric Fiselier // <string>
105a83710eSEric Fiselier 
11*425620ccSNikolas Klauser // explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++20
125a83710eSEric Fiselier 
135a83710eSEric Fiselier #include <string>
145a83710eSEric Fiselier #include <cassert>
155a83710eSEric Fiselier 
16cbf166a2SMarshall Clow #include "test_macros.h"
175a83710eSEric Fiselier #include "test_allocator.h"
185a83710eSEric Fiselier #include "min_allocator.h"
195a83710eSEric Fiselier 
205a83710eSEric Fiselier template <class S>
21e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test()225a83710eSEric Fiselier test()
235a83710eSEric Fiselier {
245a83710eSEric Fiselier     {
25cbf166a2SMarshall Clow #if TEST_STD_VER > 14
26cbf166a2SMarshall Clow     static_assert((noexcept(S{})), "" );
27cbf166a2SMarshall Clow #elif TEST_STD_VER >= 11
28cbf166a2SMarshall Clow     static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
29cbf166a2SMarshall Clow #endif
305a83710eSEric Fiselier     S s;
311f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
325a83710eSEric Fiselier     assert(s.data());
335a83710eSEric Fiselier     assert(s.size() == 0);
345a83710eSEric Fiselier     assert(s.capacity() >= s.size());
355a83710eSEric Fiselier     assert(s.get_allocator() == typename S::allocator_type());
365a83710eSEric Fiselier     }
375a83710eSEric Fiselier     {
38cbf166a2SMarshall Clow #if TEST_STD_VER > 14
39cbf166a2SMarshall Clow     static_assert((noexcept(S{typename S::allocator_type{}})), "" );
40cbf166a2SMarshall Clow #elif TEST_STD_VER >= 11
41cbf166a2SMarshall Clow     static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
42cbf166a2SMarshall Clow #endif
435a83710eSEric Fiselier     S s(typename S::allocator_type(5));
441f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
455a83710eSEric Fiselier     assert(s.data());
465a83710eSEric Fiselier     assert(s.size() == 0);
475a83710eSEric Fiselier     assert(s.capacity() >= s.size());
485a83710eSEric Fiselier     assert(s.get_allocator() == typename S::allocator_type(5));
495a83710eSEric Fiselier     }
505a83710eSEric Fiselier }
515a83710eSEric Fiselier 
52cbf166a2SMarshall Clow #if TEST_STD_VER >= 11
535a83710eSEric Fiselier 
545a83710eSEric Fiselier template <class S>
55e85018b7SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test2()565a83710eSEric Fiselier test2()
575a83710eSEric Fiselier {
585a83710eSEric Fiselier     {
59cbf166a2SMarshall Clow #if TEST_STD_VER > 14
60cbf166a2SMarshall Clow     static_assert((noexcept(S{})), "" );
61cbf166a2SMarshall Clow #elif TEST_STD_VER >= 11
62cbf166a2SMarshall Clow     static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
63cbf166a2SMarshall Clow #endif
645a83710eSEric Fiselier     S s;
651f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
665a83710eSEric Fiselier     assert(s.data());
675a83710eSEric Fiselier     assert(s.size() == 0);
685a83710eSEric Fiselier     assert(s.capacity() >= s.size());
695a83710eSEric Fiselier     assert(s.get_allocator() == typename S::allocator_type());
705a83710eSEric Fiselier     }
715a83710eSEric Fiselier     {
72cbf166a2SMarshall Clow #if TEST_STD_VER > 14
73cbf166a2SMarshall Clow     static_assert((noexcept(S{typename S::allocator_type{}})), "" );
74cbf166a2SMarshall Clow #elif TEST_STD_VER >= 11
75cbf166a2SMarshall Clow     static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
76cbf166a2SMarshall Clow #endif
775a83710eSEric Fiselier     S s(typename S::allocator_type{});
781f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
795a83710eSEric Fiselier     assert(s.data());
805a83710eSEric Fiselier     assert(s.size() == 0);
815a83710eSEric Fiselier     assert(s.capacity() >= s.size());
825a83710eSEric Fiselier     assert(s.get_allocator() == typename S::allocator_type());
835a83710eSEric Fiselier     }
845a83710eSEric Fiselier }
855a83710eSEric Fiselier 
865a83710eSEric Fiselier #endif
875a83710eSEric Fiselier 
test()88*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
895a83710eSEric Fiselier   test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
90cbf166a2SMarshall Clow #if TEST_STD_VER >= 11
915a83710eSEric Fiselier   test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
922a10c960SMarshall Clow   test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();
935a83710eSEric Fiselier #endif
942df59c50SJF Bastien 
95e85018b7SNikolas Klauser   return true;
96e85018b7SNikolas Klauser }
97e85018b7SNikolas Klauser 
main(int,char **)98e85018b7SNikolas Klauser int main(int, char**)
99e85018b7SNikolas Klauser {
100e85018b7SNikolas Klauser   test();
101e85018b7SNikolas Klauser #if TEST_STD_VER > 17
102*425620ccSNikolas Klauser   static_assert(test());
103e85018b7SNikolas Klauser #endif
104e85018b7SNikolas Klauser 
1052df59c50SJF Bastien   return 0;
1065a83710eSEric Fiselier }
107