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());
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 void
test()22 test()
23 {
24 {
25 #if TEST_STD_VER > 14
26 static_assert((noexcept(S{})), "" );
27 #elif TEST_STD_VER >= 11
28 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
29 #endif
30 S s;
31 LIBCPP_ASSERT(s.__invariants());
32 assert(s.data());
33 assert(s.size() == 0);
34 assert(s.capacity() >= s.size());
35 assert(s.get_allocator() == typename S::allocator_type());
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())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
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 void
test2()56 test2()
57 {
58 {
59 #if TEST_STD_VER > 14
60 static_assert((noexcept(S{})), "" );
61 #elif TEST_STD_VER >= 11
62 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
63 #endif
64 S s;
65 LIBCPP_ASSERT(s.__invariants());
66 assert(s.data());
67 assert(s.size() == 0);
68 assert(s.capacity() >= s.size());
69 assert(s.get_allocator() == typename S::allocator_type());
70 }
71 {
72 #if TEST_STD_VER > 14
73 static_assert((noexcept(S{typename S::allocator_type{}})), "" );
74 #elif TEST_STD_VER >= 11
75 static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
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
main(int,char **)88 int main(int, char**)
89 {
90 test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
91 #if TEST_STD_VER >= 11
92 test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
93 test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();
94 #endif
95
96 return 0;
97 }
98