]> 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-2020 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 <ext/debug_allocator.h>
23 #include <ext/new_allocator.h>
24 #include <ext/malloc_allocator.h>
25 #include <testsuite_hooks.h>
26 #include <testsuite_allocator.h>
27
28 using std::experimental::pmr::memory_resource;
29 using std::experimental::pmr::resource_adaptor;
30
31 template<typename T>
32 struct Allocator : __gnu_test::SimpleAllocator<T>
33 {
34 Allocator(int) { } // not default constructible
35
36 template<typename U>
37 Allocator(const Allocator<U>&) { }
38 };
39
40 template<std::size_t A>
41 bool aligned(void* p)
42 {
43 return (reinterpret_cast<std::uintptr_t>(p) % A) == 0;
44 }
45
46 template<typename T>
47 bool aligned(void* p)
48 { return aligned<alignof(T)>(p); }
49
50 // resource_adaptor
51 void
52 test05()
53 {
54 using std::max_align_t;
55 using std::size_t;
56 void* p = nullptr;
57
58 Allocator<int> a1(1), a2(2); // minimal interface allocators
59 resource_adaptor<decltype(a1)> r1(a1), r2(a2);
60 VERIFY( r1 == r1 );
61 VERIFY( r1 == r2 );
62 p = r1.allocate(1);
63 VERIFY( aligned<max_align_t>(p) );
64 r1.deallocate(p, 1);
65 p = r1.allocate(1, alignof(short));
66 VERIFY( aligned<short>(p) );
67 r1.deallocate(p, 1, alignof(short));
68 p = r1.allocate(1, alignof(long));
69 VERIFY( aligned<long>(p) );
70 r1.deallocate(p, 1, alignof(long));
71 constexpr size_t big_al = alignof(max_align_t) * 8;
72 p = r1.allocate(1, big_al);
73 VERIFY( aligned<big_al>(p) );
74 r1.deallocate(p, 1, big_al);
75
76 __gnu_test::uneq_allocator<double> a3(3), a4(4); // non-equal allocators
77 resource_adaptor<decltype(a3)> r3(a3), r4(a4);
78 VERIFY( r3 == r3 );
79 VERIFY( r4 == r4 );
80 VERIFY( r3 != r4 );
81 VERIFY( r3 != r1 );
82 VERIFY( r3 != r2 );
83 p = r3.allocate(1);
84 VERIFY( aligned<max_align_t>(p) );
85 r3.deallocate(p, 1);
86 p = r3.allocate(1, alignof(short));
87 VERIFY( aligned<short>(p) );
88 r3.deallocate(p, 1, alignof(short));
89 p = r3.allocate(1, alignof(long));
90 VERIFY( aligned<long>(p) );
91 r3.deallocate(p, 1, alignof(long));
92 p = r3.allocate(1, big_al);
93 VERIFY( aligned<big_al>(p) );
94 r3.deallocate(p, 1, big_al);
95
96 __gnu_cxx::debug_allocator<std::allocator<short>> a5, a6;
97 resource_adaptor<decltype(a5)> r5(a5), r6(a6);
98 VERIFY( r5 == r5 );
99 VERIFY( r5 == r6 );
100 VERIFY( r5 != r1 );
101 VERIFY( r5 != r3 );
102 p = r5.allocate(1);
103 VERIFY( aligned<max_align_t>(p) );
104 r5.deallocate(p, 1);
105 p = r5.allocate(1, alignof(short));
106 VERIFY( aligned<short>(p) );
107 r5.deallocate(p, 1, alignof(short));
108 p = r5.allocate(1, alignof(long));
109 VERIFY( aligned<long>(p) );
110 r5.deallocate(p, 1, alignof(long));
111 p = r5.allocate(1, big_al);
112 VERIFY( aligned<big_al>(p) );
113 r5.deallocate(p, 1, big_al);
114
115 // Test extended alignments
116 constexpr size_t al6 = (1ul << 6), al12 = (1ul << 12), al18 = (1ul << 18);
117 p = r5.allocate(1024, al6);
118 VERIFY( aligned<al6>(p) );
119 r5.deallocate(p, 1024, al6);
120 p = r5.allocate(1024, al12);
121 VERIFY( aligned<al12>(p) );
122 r5.deallocate(p, 1024, al12);
123 p = r5.allocate(1024, al18);
124 VERIFY( aligned<al18>(p) );
125 r5.deallocate(p, 1024, al18);
126
127 __gnu_cxx::new_allocator<short> a7, a8;
128 resource_adaptor<decltype(a7)> r7(a7), r8(a8);
129 VERIFY( r7 == r7 );
130 VERIFY( r7 == r8 );
131 VERIFY( r7 != r1 );
132 VERIFY( r7 != r3 );
133 VERIFY( r7 != r5 );
134 p = r7.allocate(1);
135 VERIFY( aligned<max_align_t>(p) );
136 r7.deallocate(p, 1);
137 p = r7.allocate(1, alignof(short));
138 VERIFY( aligned<short>(p) );
139 r7.deallocate(p, 1, alignof(short));
140 p = r7.allocate(1, alignof(long));
141 VERIFY( aligned<long>(p) );
142 r7.deallocate(p, 1, alignof(long));
143 p = r7.allocate(1, big_al);
144 VERIFY( aligned<big_al>(p) );
145 r7.deallocate(p, 1, big_al);
146 // Test extended alignments
147 p = r7.allocate(1024, al6);
148 VERIFY( aligned<al6>(p) );
149 r7.deallocate(p, 1024, al6);
150 p = r7.allocate(1024, al12);
151 VERIFY( aligned<al12>(p) );
152 r7.deallocate(p, 1024, al12);
153 p = r7.allocate(1024, al18);
154 VERIFY( aligned<al18>(p) );
155 r7.deallocate(p, 1024, al18);
156
157 __gnu_cxx::malloc_allocator<short> a9, a10;
158 resource_adaptor<decltype(a9)> r9(a9), r10(a10);
159 VERIFY( r9 == r9 );
160 VERIFY( r9 == r10 );
161 VERIFY( r9 != r1 );
162 VERIFY( r9 != r3 );
163 VERIFY( r9 != r5 );
164 VERIFY( r9 != r7 );
165 p = r9.allocate(1);
166 VERIFY( aligned<max_align_t>(p) );
167 r9.deallocate(p, 1);
168 p = r9.allocate(1, alignof(short));
169 VERIFY( aligned<short>(p) );
170 r9.deallocate(p, 1, alignof(short));
171 p = r9.allocate(1, alignof(long));
172 VERIFY( aligned<long>(p) );
173 r9.deallocate(p, 1, alignof(long));
174 p = r9.allocate(1, big_al);
175 VERIFY( aligned<big_al>(p) );
176 r9.deallocate(p, 1, big_al);
177 // Test extended alignments
178 p = r9.allocate(1024, al6);
179 VERIFY( aligned<al6>(p) );
180 r9.deallocate(p, 1024, al6);
181 p = r9.allocate(1024, al12);
182 VERIFY( aligned<al12>(p) );
183 r9.deallocate(p, 1024, al12);
184 p = r9.allocate(1024, al18);
185 VERIFY( aligned<al18>(p) );
186 r9.deallocate(p, 1024, al18);
187
188 std::allocator<short> a11, a12;
189 resource_adaptor<decltype(a11)> r11(a11), r12(a12);
190 VERIFY( r11 == r11 );
191 VERIFY( r11 == r12 );
192 VERIFY( r11 != r1 );
193 VERIFY( r11 != r3 );
194 VERIFY( r11 != r5 );
195 VERIFY( r11 != r7 );
196 VERIFY( r11 != r9 );
197 p = r11.allocate(1);
198 VERIFY( aligned<max_align_t>(p) );
199 r11.deallocate(p, 1);
200 p = r11.allocate(1, alignof(short));
201 VERIFY( aligned<short>(p) );
202 r11.deallocate(p, 1, alignof(short));
203 p = r11.allocate(1, alignof(long));
204 VERIFY( aligned<long>(p) );
205 r11.deallocate(p, 1, alignof(long));
206 p = r11.allocate(1, big_al);
207 VERIFY( aligned<big_al>(p) );
208 r11.deallocate(p, 1, big_al);
209 // Test extended alignments
210 p = r11.allocate(1024, al6);
211 VERIFY( aligned<al6>(p) );
212 r11.deallocate(p, 1024, al6);
213 p = r11.allocate(1024, al12);
214 VERIFY( aligned<al12>(p) );
215 r11.deallocate(p, 1024, al12);
216 p = r11.allocate(1024, al18);
217 VERIFY( aligned<al18>(p) );
218 r11.deallocate(p, 1024, al18);
219 }
220
221 int main()
222 {
223 test05();
224 }