Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / alloc.pass.cpp
blob97a0566ba031b00daf5ed71fd11c67ed884a3428
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++20
13 #include <string>
14 #include <cassert>
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void test() {
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());
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));
52 #if TEST_STD_VER >= 11
54 template <class S>
55 TEST_CONSTEXPR_CXX20 void test2() {
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());
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());
86 #endif
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
95 return true;
98 int main(int, char**) {
99 test();
100 #if TEST_STD_VER > 17
101 static_assert(test());
102 #endif
104 return 0;