[LoongArch] Supports FP_TO_SINT operation for fp16 (#118303)
[llvm-project.git] / libcxx / test / std / utilities / memory / allocator.traits / allocator.traits.members / construct.pass.cpp
blobb2258ee523f1251ed31ab88e9ddd8b89d67557a7
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 // <memory>
11 // template <class Alloc>
12 // struct allocator_traits
13 // {
14 // template <class Ptr, class... Args>
15 // static constexpr void construct(allocator_type& a, Ptr p, Args&&... args);
16 // ...
17 // };
19 #include <memory>
20 #include <new>
21 #include <type_traits>
22 #include <cassert>
23 #include <utility>
25 #include "test_macros.h"
26 #include "incomplete_type_helper.h"
28 template <class T>
29 struct A
31 typedef T value_type;
35 template <class T>
36 struct B
38 typedef T value_type;
40 TEST_CONSTEXPR_CXX20 B(int& count) : count_(count) {}
42 #if TEST_STD_VER >= 11
43 template <class U, class ...Args>
44 TEST_CONSTEXPR_CXX20 void construct(U* p, Args&& ...args)
46 ++count_;
47 #if TEST_STD_VER > 17
48 std::construct_at(p, std::forward<Args>(args)...);
49 #else
50 ::new ((void*)p) U(std::forward<Args>(args)...);
51 #endif
53 #endif
55 int& count_;
58 struct A0
60 TEST_CONSTEXPR_CXX20 A0(int* count) {++*count;}
63 struct A1
65 TEST_CONSTEXPR_CXX20 A1(int* count, char c)
67 assert(c == 'c');
68 ++*count;
72 struct A2
74 TEST_CONSTEXPR_CXX20 A2(int* count, char c, int i)
76 assert(c == 'd');
77 assert(i == 5);
78 ++*count;
82 TEST_CONSTEXPR_CXX20 bool test()
85 int A0_count = 0;
86 A<A0> a;
87 std::allocator<A0> alloc;
88 A0* a0 = alloc.allocate(1);
89 assert(A0_count == 0);
90 std::allocator_traits<A<A0> >::construct(a, a0, &A0_count);
91 assert(A0_count == 1);
92 alloc.deallocate(a0, 1);
95 int A1_count = 0;
96 A<A1> a;
97 std::allocator<A1> alloc;
98 A1* a1 = alloc.allocate(1);
99 assert(A1_count == 0);
100 std::allocator_traits<A<A1> >::construct(a, a1, &A1_count, 'c');
101 assert(A1_count == 1);
102 alloc.deallocate(a1, 1);
105 int A2_count = 0;
106 A<A2> a;
107 std::allocator<A2> alloc;
108 A2* a2 = alloc.allocate(1);
109 assert(A2_count == 0);
110 std::allocator_traits<A<A2> >::construct(a, a2, &A2_count, 'd', 5);
111 assert(A2_count == 1);
112 alloc.deallocate(a2, 1);
115 typedef IncompleteHolder* VT;
116 typedef A<VT> Alloc;
117 Alloc a;
118 std::allocator<VT> alloc;
119 VT* vt = alloc.allocate(1);
120 std::allocator_traits<Alloc>::construct(a, vt, nullptr);
121 alloc.deallocate(vt, 1);
124 #if TEST_STD_VER >= 11
126 int A0_count = 0;
127 int b_construct = 0;
128 B<A0> b(b_construct);
129 std::allocator<A0> alloc;
130 A0* a0 = alloc.allocate(1);
131 assert(A0_count == 0);
132 assert(b_construct == 0);
133 std::allocator_traits<B<A0> >::construct(b, a0, &A0_count);
134 assert(A0_count == 1);
135 assert(b_construct == 1);
136 alloc.deallocate(a0, 1);
139 int A1_count = 0;
140 int b_construct = 0;
141 B<A1> b(b_construct);
142 std::allocator<A1> alloc;
143 A1* a1 = alloc.allocate(1);
144 assert(A1_count == 0);
145 assert(b_construct == 0);
146 std::allocator_traits<B<A1> >::construct(b, a1, &A1_count, 'c');
147 assert(A1_count == 1);
148 assert(b_construct == 1);
149 alloc.deallocate(a1, 1);
152 int A2_count = 0;
153 int b_construct = 0;
154 B<A2> b(b_construct);
155 std::allocator<A2> alloc;
156 A2* a2 = alloc.allocate(1);
157 assert(A2_count == 0);
158 assert(b_construct == 0);
159 std::allocator_traits<B<A2> >::construct(b, a2, &A2_count, 'd', 5);
160 assert(A2_count == 1);
161 assert(b_construct == 1);
162 alloc.deallocate(a2, 1);
164 #endif
166 return true;
169 int main(int, char**)
171 test();
173 #if TEST_STD_VER > 17
174 static_assert(test());
175 #endif
177 return 0;