xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/alloc.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // <string>
10 
11 // explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++20
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void test() {
22   {
23 #if TEST_STD_VER > 14
24     static_assert((noexcept(S{})), "");
25 #elif TEST_STD_VER >= 11
26     static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "");
27 #endif
28     S s;
29     LIBCPP_ASSERT(s.__invariants());
30     assert(s.data());
31     assert(s.size() == 0);
32     assert(s.capacity() >= s.size());
33     assert(s.get_allocator() == typename S::allocator_type());
34   }
35   {
36 #if TEST_STD_VER > 14
37     static_assert((noexcept(S{typename S::allocator_type{}})), "");
38 #elif TEST_STD_VER >= 11
39     static_assert((noexcept(S(typename S::allocator_type())) ==
40                    std::is_nothrow_copy_constructible<typename S::allocator_type>::value),
41                   "");
42 #endif
43     S s(typename S::allocator_type(5));
44     LIBCPP_ASSERT(s.__invariants());
45     assert(s.data());
46     assert(s.size() == 0);
47     assert(s.capacity() >= s.size());
48     assert(s.get_allocator() == typename S::allocator_type(5));
49   }
50 }
51 
52 #if TEST_STD_VER >= 11
53 
54 template <class S>
55 TEST_CONSTEXPR_CXX20 void test2() {
56   {
57 #  if TEST_STD_VER > 14
58     static_assert((noexcept(S{})), "");
59 #  elif TEST_STD_VER >= 11
60     static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "");
61 #  endif
62     S s;
63     LIBCPP_ASSERT(s.__invariants());
64     assert(s.data());
65     assert(s.size() == 0);
66     assert(s.capacity() >= s.size());
67     assert(s.get_allocator() == typename S::allocator_type());
68   }
69   {
70 #  if TEST_STD_VER > 14
71     static_assert((noexcept(S{typename S::allocator_type{}})), "");
72 #  elif TEST_STD_VER >= 11
73     static_assert((noexcept(S(typename S::allocator_type())) ==
74                    std::is_nothrow_copy_constructible<typename S::allocator_type>::value),
75                   "");
76 #  endif
77     S s(typename S::allocator_type{});
78     LIBCPP_ASSERT(s.__invariants());
79     assert(s.data());
80     assert(s.size() == 0);
81     assert(s.capacity() >= s.size());
82     assert(s.get_allocator() == typename S::allocator_type());
83   }
84 }
85 
86 #endif
87 
88 TEST_CONSTEXPR_CXX20 bool test() {
89   test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
90 #if TEST_STD_VER >= 11
91   test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
92   test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();
93 #endif
94 
95   return true;
96 }
97 
98 int main(int, char**) {
99   test();
100 #if TEST_STD_VER > 17
101   static_assert(test());
102 #endif
103 
104   return 0;
105 }
106