[mlir][acc] Introduce MappableType interface (#122146)
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / copy_alloc.pass.cpp
blobb0045cb4afbba585274006712371632e260aea90
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 // basic_string(const basic_string& str, const Allocator& alloc); // 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 #ifndef TEST_HAS_NO_EXCEPTIONS
22 struct alloc_imp {
23 bool active;
25 TEST_CONSTEXPR alloc_imp() : active(true) {}
27 template <class T>
28 T* allocate(std::size_t n) {
29 if (active)
30 return static_cast<T*>(std::malloc(n * sizeof(T)));
31 else
32 throw std::bad_alloc();
35 template <class T>
36 void deallocate(T* p, std::size_t) {
37 std::free(p);
39 void activate() { active = true; }
40 void deactivate() { active = false; }
43 template <class T>
44 struct poca_alloc {
45 typedef T value_type;
46 typedef std::true_type propagate_on_container_copy_assignment;
48 alloc_imp* imp;
50 TEST_CONSTEXPR poca_alloc(alloc_imp* imp_) : imp(imp_) {}
52 template <class U>
53 TEST_CONSTEXPR poca_alloc(const poca_alloc<U>& other) : imp(other.imp) {}
55 T* allocate(std::size_t n) { return imp->allocate<T>(n); }
56 void deallocate(T* p, std::size_t n) { imp->deallocate(p, n); }
59 template <typename T, typename U>
60 bool operator==(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs) {
61 return lhs.imp == rhs.imp;
64 template <typename T, typename U>
65 bool operator!=(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs) {
66 return lhs.imp != rhs.imp;
69 template <class S>
70 TEST_CONSTEXPR_CXX20 void test_assign(S& s1, const S& s2) {
71 try {
72 s1 = s2;
73 } catch (std::bad_alloc&) {
74 return;
76 assert(false);
78 #endif
80 template <class S>
81 TEST_CONSTEXPR_CXX20 void test(S s1, const typename S::allocator_type& a) {
82 S s2(s1, a);
83 LIBCPP_ASSERT(s2.__invariants());
84 assert(s2 == s1);
85 assert(s2.capacity() >= s2.size());
86 assert(s2.get_allocator() == a);
87 LIBCPP_ASSERT(is_string_asan_correct(s1));
88 LIBCPP_ASSERT(is_string_asan_correct(s2));
91 template <class Alloc>
92 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
93 typedef std::basic_string<char, std::char_traits<char>, Alloc> S;
94 test(S(), Alloc(a));
95 test(S("1"), Alloc(a));
96 test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), Alloc(a));
99 TEST_CONSTEXPR_CXX20 bool test() {
100 test_string(std::allocator<char>());
101 test_string(test_allocator<char>());
102 test_string(test_allocator<char>(3));
103 #if TEST_STD_VER >= 11
104 test_string(min_allocator<char>());
105 test_string(safe_allocator<char>());
106 #endif
108 #if TEST_STD_VER >= 11
109 # ifndef TEST_HAS_NO_EXCEPTIONS
110 if (!TEST_IS_CONSTANT_EVALUATED) {
111 typedef poca_alloc<char> A;
112 typedef std::basic_string<char, std::char_traits<char>, A> S;
113 const char* p1 = "This is my first string";
114 const char* p2 = "This is my second string";
116 alloc_imp imp1;
117 alloc_imp imp2;
118 S s1(p1, A(&imp1));
119 S s2(p2, A(&imp2));
121 assert(s1 == p1);
122 assert(s2 == p2);
124 imp2.deactivate();
125 test_assign(s1, s2);
126 assert(s1 == p1);
127 assert(s2 == p2);
129 # endif
130 #endif
132 return true;
135 int main(int, char**) {
136 test();
137 #if TEST_STD_VER > 17
138 static_assert(test());
139 #endif
141 return 0;