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