]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory_resource/resource_adaptor.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory_resource / resource_adaptor.cc
1 // { dg-do run { target c++14 } }
2 // { dg-require-cstdint "" }
3
4 // Copyright (C) 2016-2024 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <experimental/memory_resource>
22 #include <cstdint>
23 #include <ext/debug_allocator.h>
24 #include <ext/new_allocator.h>
25 #include <ext/malloc_allocator.h>
26 #include <testsuite_hooks.h>
27 #include <testsuite_allocator.h>
28
29 using std::experimental::pmr::memory_resource;
30 using std::experimental::pmr::resource_adaptor;
31
32 template<typename T>
33 struct Allocator : __gnu_test::SimpleAllocator<T>
34 {
35 Allocator(int) { } // not default constructible
36
37 template<typename U>
38 Allocator(const Allocator<U>&) { }
39 };
40
41 template<std::size_t A>
42 bool aligned(void* p)
43 {
44 return (reinterpret_cast<std::uintptr_t>(p) % A) == 0;
45 }
46
47 template<typename T>
48 bool aligned(void* p)
49 { return aligned<alignof(T)>(p); }
50
51 // resource_adaptor
52 void
53 test05()
54 {
55 using std::max_align_t;
56 using std::size_t;
57 void* p = nullptr;
58
59 Allocator<int> a1(1), a2(2); // minimal interface allocators
60 resource_adaptor<decltype(a1)> r1(a1), r2(a2);
61 VERIFY( r1 == r1 );
62 #if __cpp_rtti
63 VERIFY( r1 == r2 );
64 #endif
65 p = r1.allocate(1);
66 VERIFY( aligned<max_align_t>(p) );
67 r1.deallocate(p, 1);
68 p = r1.allocate(1, alignof(short));
69 VERIFY( aligned<short>(p) );
70 r1.deallocate(p, 1, alignof(short));
71 p = r1.allocate(1, alignof(long));
72 VERIFY( aligned<long>(p) );
73 r1.deallocate(p, 1, alignof(long));
74 constexpr size_t big_al = alignof(max_align_t) * 8;
75 p = r1.allocate(1, big_al);
76 VERIFY( aligned<big_al>(p) );
77 r1.deallocate(p, 1, big_al);
78
79 __gnu_test::uneq_allocator<double> a3(3), a4(4); // non-equal allocators
80 resource_adaptor<decltype(a3)> r3(a3), r4(a4);
81 VERIFY( r3 == r3 );
82 VERIFY( r4 == r4 );
83 VERIFY( r3 != r4 );
84 VERIFY( r3 != r1 );
85 VERIFY( r3 != r2 );
86 p = r3.allocate(1);
87 VERIFY( aligned<max_align_t>(p) );
88 r3.deallocate(p, 1);
89 p = r3.allocate(1, alignof(short));
90 VERIFY( aligned<short>(p) );
91 r3.deallocate(p, 1, alignof(short));
92 p = r3.allocate(1, alignof(long));
93 VERIFY( aligned<long>(p) );
94 r3.deallocate(p, 1, alignof(long));
95 p = r3.allocate(1, big_al);
96 VERIFY( aligned<big_al>(p) );
97 r3.deallocate(p, 1, big_al);
98
99 __gnu_cxx::debug_allocator<std::allocator<short>> a5, a6;
100 resource_adaptor<decltype(a5)> r5(a5), r6(a6);
101 VERIFY( r5 == r5 );
102 #if __cpp_rtti
103 VERIFY( r5 == r6 );
104 #endif
105 VERIFY( r5 != r1 );
106 VERIFY( r5 != r3 );
107 p = r5.allocate(1);
108 VERIFY( aligned<max_align_t>(p) );
109 r5.deallocate(p, 1);
110 p = r5.allocate(1, alignof(short));
111 VERIFY( aligned<short>(p) );
112 r5.deallocate(p, 1, alignof(short));
113 p = r5.allocate(1, alignof(long));
114 VERIFY( aligned<long>(p) );
115 r5.deallocate(p, 1, alignof(long));
116 p = r5.allocate(1, big_al);
117 VERIFY( aligned<big_al>(p) );
118 r5.deallocate(p, 1, big_al);
119
120 // Test extended alignments
121 constexpr size_t al6 = (1ul << 6), al12 = (1ul << 12), al18 = (1ul << 18);
122 p = r5.allocate(1024, al6);
123 VERIFY( aligned<al6>(p) );
124 r5.deallocate(p, 1024, al6);
125 p = r5.allocate(1024, al12);
126 VERIFY( aligned<al12>(p) );
127 r5.deallocate(p, 1024, al12);
128 p = r5.allocate(1024, al18);
129 VERIFY( aligned<al18>(p) );
130 r5.deallocate(p, 1024, al18);
131
132 __gnu_cxx::new_allocator<short> a7, a8;
133 resource_adaptor<decltype(a7)> r7(a7), r8(a8);
134 VERIFY( r7 == r7 );
135 #if __cpp_rtti
136 VERIFY( r7 == r8 );
137 #endif
138 VERIFY( r7 != r1 );
139 VERIFY( r7 != r3 );
140 VERIFY( r7 != r5 );
141 p = r7.allocate(1);
142 VERIFY( aligned<max_align_t>(p) );
143 r7.deallocate(p, 1);
144 p = r7.allocate(1, alignof(short));
145 VERIFY( aligned<short>(p) );
146 r7.deallocate(p, 1, alignof(short));
147 p = r7.allocate(1, alignof(long));
148 VERIFY( aligned<long>(p) );
149 r7.deallocate(p, 1, alignof(long));
150 p = r7.allocate(1, big_al);
151 VERIFY( aligned<big_al>(p) );
152 r7.deallocate(p, 1, big_al);
153 // Test extended alignments
154 p = r7.allocate(1024, al6);
155 VERIFY( aligned<al6>(p) );
156 r7.deallocate(p, 1024, al6);
157 p = r7.allocate(1024, al12);
158 VERIFY( aligned<al12>(p) );
159 r7.deallocate(p, 1024, al12);
160 p = r7.allocate(1024, al18);
161 VERIFY( aligned<al18>(p) );
162 r7.deallocate(p, 1024, al18);
163
164 __gnu_cxx::malloc_allocator<short> a9, a10;
165 resource_adaptor<decltype(a9)> r9(a9), r10(a10);
166 VERIFY( r9 == r9 );
167 #if __cpp_rtti
168 VERIFY( r9 == r10 );
169 #endif
170 VERIFY( r9 != r1 );
171 VERIFY( r9 != r3 );
172 VERIFY( r9 != r5 );
173 VERIFY( r9 != r7 );
174 p = r9.allocate(1);
175 VERIFY( aligned<max_align_t>(p) );
176 r9.deallocate(p, 1);
177 p = r9.allocate(1, alignof(short));
178 VERIFY( aligned<short>(p) );
179 r9.deallocate(p, 1, alignof(short));
180 p = r9.allocate(1, alignof(long));
181 VERIFY( aligned<long>(p) );
182 r9.deallocate(p, 1, alignof(long));
183 p = r9.allocate(1, big_al);
184 VERIFY( aligned<big_al>(p) );
185 r9.deallocate(p, 1, big_al);
186 // Test extended alignments
187 p = r9.allocate(1024, al6);
188 VERIFY( aligned<al6>(p) );
189 r9.deallocate(p, 1024, al6);
190 p = r9.allocate(1024, al12);
191 VERIFY( aligned<al12>(p) );
192 r9.deallocate(p, 1024, al12);
193 p = r9.allocate(1024, al18);
194 VERIFY( aligned<al18>(p) );
195 r9.deallocate(p, 1024, al18);
196
197 std::allocator<short> a11, a12;
198 resource_adaptor<decltype(a11)> r11(a11), r12(a12);
199 VERIFY( r11 == r11 );
200 #if __cpp_rtti
201 VERIFY( r11 == r12 );
202 #endif
203 VERIFY( r11 != r1 );
204 VERIFY( r11 != r3 );
205 VERIFY( r11 != r5 );
206 VERIFY( r11 != r7 );
207 VERIFY( r11 != r9 );
208 p = r11.allocate(1);
209 VERIFY( aligned<max_align_t>(p) );
210 r11.deallocate(p, 1);
211 p = r11.allocate(1, alignof(short));
212 VERIFY( aligned<short>(p) );
213 r11.deallocate(p, 1, alignof(short));
214 p = r11.allocate(1, alignof(long));
215 VERIFY( aligned<long>(p) );
216 r11.deallocate(p, 1, alignof(long));
217 p = r11.allocate(1, big_al);
218 VERIFY( aligned<big_al>(p) );
219 r11.deallocate(p, 1, big_al);
220 // Test extended alignments
221 p = r11.allocate(1024, al6);
222 VERIFY( aligned<al6>(p) );
223 r11.deallocate(p, 1024, al6);
224 p = r11.allocate(1024, al12);
225 VERIFY( aligned<al12>(p) );
226 r11.deallocate(p, 1024, al12);
227 p = r11.allocate(1024, al18);
228 VERIFY( aligned<al18>(p) );
229 r11.deallocate(p, 1024, al18);
230 }
231
232 int main()
233 {
234 test05();
235 }