[mlir][acc] Introduce MappableType interface (#122146)
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / alloc.pass.cpp
blob91beac37764db48b43a6694cb02de473bf20296a
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"
19 #include "asan_testing.h"
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void test() {
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));
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));
55 #if TEST_STD_VER >= 11
57 template <class S>
58 TEST_CONSTEXPR_CXX20 void test2() {
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));
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));
91 #endif
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
101 return true;
104 int main(int, char**) {
105 test();
106 #if TEST_STD_VER > 17
107 static_assert(test());
108 #endif
110 return 0;