Debian Codegraph @

libgomp/testsuite/libgomp.c++/target-flex-11.C

raw
1 /* Check constructors/destructors are called in containers. */
2
3 #include <vector>
4 #include <deque>
5 #include <list>
6 #include <set>
7 #include <map>
8 #include <utility>
9 #if __cplusplus >= 201103L
10 #include <array>
11 #include <forward_list>
12 #include <unordered_set>
13 #include <unordered_map>
14 #endif
15
16 #include "target-flex-common.h"
17
18 struct indirect_counter
19 {
20 typedef int counter_value_type;
21 counter_value_type *_count_ptr;
22
23 indirect_counter(counter_value_type *count_ptr) BL_NOEXCEPT : _count_ptr(count_ptr) {
24 ++(*_count_ptr);
25 }
26 indirect_counter(const indirect_counter& other) BL_NOEXCEPT : _count_ptr(other._count_ptr) {
27 ++(*_count_ptr);
28 }
29 /* Don't declare a move constructor, we want to copy no matter what. */
30 ~indirect_counter() {
31 --(*_count_ptr);
32 }
33 };
34
35 bool operator==(indirect_counter const& lhs, indirect_counter const& rhs) BL_NOEXCEPT
36 { return lhs._count_ptr == rhs._count_ptr; }
37 bool operator<(indirect_counter const& lhs, indirect_counter const& rhs) BL_NOEXCEPT
38 { return lhs._count_ptr < rhs._count_ptr; }
39
40 #if __cplusplus >= 201103L
41 template<>
42 struct std::hash<indirect_counter>
43 {
44 std::size_t operator()(const indirect_counter& ic) const noexcept
45 { return std::hash<indirect_counter::counter_value_type *>{}(ic._count_ptr); }
46 };
47 #endif
48
49 /* Not a container, just a sanity check really. */
50 bool automatic_lifetime_test()
51 {
52 bool ok;
53 #pragma omp target map(from: ok)
54 {
55 bool inner_ok = true;
56 int counter = 0;
57 {
58 indirect_counter c = indirect_counter(&counter);
59 indirect_counter(static_cast<int*>(&counter));
60 }
61 VERIFY (counter == 0);
62 end:
63 ok = inner_ok;
64 }
65 return ok;
66 }
67
68 bool vector_test()
69 {
70 bool ok;
71 #pragma omp target map(from: ok)
72 {
73 bool inner_ok = true;
74 int counter = 0;
75 {
76 std::vector<indirect_counter> vec(42, indirect_counter(&counter));
77 VERIFY (counter == 42);
78 vec.resize(32, indirect_counter(&counter));
79 VERIFY (counter == 32);
80 vec.push_back(indirect_counter(&counter));
81 VERIFY (counter == 33);
82 vec.pop_back();
83 VERIFY (counter == 32);
84 vec.pop_back();
85 VERIFY (counter == 31);
86 vec.resize(100, indirect_counter(&counter));
87 VERIFY (counter == 100);
88 }
89 VERIFY (counter == 0);
90 end:
91 ok = inner_ok;
92 }
93 return ok;
94 }
95
96 bool deque_test()
97 {
98 bool ok;
99 #pragma omp target map(from: ok)
100 {
101 bool inner_ok = true;
102 int counter = 0;
103 {
104 std::deque<indirect_counter> vec(42, indirect_counter(&counter));
105 VERIFY (counter == 42);
106 vec.resize(32, indirect_counter(&counter));
107 VERIFY (counter == 32);
108 vec.push_back(indirect_counter(&counter));
109 VERIFY (counter == 33);
110 vec.pop_back();
111 VERIFY (counter == 32);
112 vec.pop_back();
113 VERIFY (counter == 31);
114 vec.resize(100, indirect_counter(&counter));
115 VERIFY (counter == 100);
116 }
117 VERIFY (counter == 0);
118 end:
119 ok = inner_ok;
120 }
121 return ok;
122 }
123
124 bool list_test()
125 {
126 bool ok;
127 #pragma omp target map(from: ok)
128 {
129 bool inner_ok = true;
130 int counter = 0;
131 {
132 std::list<indirect_counter> list(42, indirect_counter(&counter));
133 VERIFY (counter == 42);
134 list.resize(32, indirect_counter(&counter));
135 VERIFY (counter == 32);
136 list.push_back(indirect_counter(&counter));
137 VERIFY (counter == 33);
138 list.pop_back();
139 VERIFY (counter == 32);
140 list.pop_back();
141 VERIFY (counter == 31);
142 list.resize(100, indirect_counter(&counter));
143 VERIFY (counter == 100);
144 }
145 VERIFY (counter == 0);
146 end:
147 ok = inner_ok;
148 }
149 return ok;
150 }
151
152 bool map_test()
153 {
154 bool ok;
155 #pragma omp target map(from: ok)
156 {
157 bool inner_ok = true;
158 int counter = 0;
159 {
160 std::map<int, indirect_counter> map;
161 map.insert(std::make_pair(1, indirect_counter(&counter)));
162 VERIFY (counter == 1);
163 map.insert(std::make_pair(1, indirect_counter(&counter)));
164 VERIFY (counter == 1);
165 map.insert(std::make_pair(2, indirect_counter(&counter)));
166 VERIFY (counter == 2);
167 }
168 VERIFY (counter == 0);
169 end:
170 ok = inner_ok;
171 }
172 return ok;
173 }
174
175 bool set_test()
176 {
177 bool ok;
178 #pragma omp target map(from: ok)
179 {
180 bool inner_ok = true;
181 int counter0 = 0;
182 int counter1 = 0;
183 {
184 std::set<indirect_counter> set;
185 set.insert(indirect_counter(&counter0));
186 VERIFY (counter0 == 1);
187 set.insert(indirect_counter(&counter0));
188 VERIFY (counter0 == 1);
189 set.insert(indirect_counter(&counter1));
190 VERIFY (counter0 == 1 && counter1 == 1);
191 }
192 VERIFY (counter0 == 0 && counter1 == 0);
193 end:
194 ok = inner_ok;
195 }
196 return ok;
197 }
198
199 bool multimap_test()
200 {
201 bool ok;
202 #pragma omp target map(from: ok)
203 {
204 bool inner_ok = true;
205 int counter = 0;
206 {
207 std::multimap<int, indirect_counter> multimap;
208 multimap.insert(std::make_pair(1, indirect_counter(&counter)));
209 VERIFY (counter == 1);
210 multimap.insert(std::make_pair(1, indirect_counter(&counter)));
211 VERIFY (counter == 2);
212 multimap.insert(std::make_pair(2, indirect_counter(&counter)));
213 VERIFY (counter == 3);
214 }
215 VERIFY (counter == 0);
216 end:
217 ok = inner_ok;
218 }
219 return ok;
220 }
221
222 bool multiset_test()
223 {
224 bool ok;
225 #pragma omp target map(from: ok)
226 {
227 bool inner_ok = true;
228 int counter0 = 0;
229 int counter1 = 0;
230 {
231 std::multiset<indirect_counter> multiset;
232 multiset.insert(indirect_counter(&counter0));
233 VERIFY (counter0 == 1);
234 multiset.insert(indirect_counter(&counter0));
235 VERIFY (counter0 == 2);
236 multiset.insert(indirect_counter(&counter1));
237 VERIFY (counter0 == 2 && counter1 == 1);
238 }
239 VERIFY (counter0 == 0 && counter1 == 0);
240 end:
241 ok = inner_ok;
242 }
243 return ok;
244 }
245
246 #if __cplusplus >= 201103L
247
248 bool array_test()
249 {
250 bool ok;
251 #pragma omp target map(from: ok)
252 {
253 bool inner_ok = true;
254 int counter = 0;
255 {
256 indirect_counter ic(&counter);
257 std::array<indirect_counter, 10> array{ic, ic, ic, ic, ic,
258 ic, ic, ic, ic, ic};
259 VERIFY (counter == 11);
260 }
261 VERIFY (counter == 0);
262 end:
263 ok = inner_ok;
264 }
265 return ok;
266 }
267
268 bool forward_list_test()
269 {
270 bool ok;
271 #pragma omp target map(from: ok)
272 {
273 bool inner_ok = true;
274 int counter = 0;
275 {
276 std::forward_list<indirect_counter> forward_list(42, indirect_counter(&counter));
277 VERIFY (counter == 42);
278 forward_list.resize(32, indirect_counter(&counter));
279 VERIFY (counter == 32);
280 forward_list.push_front(indirect_counter(&counter));
281 VERIFY (counter == 33);
282 forward_list.pop_front();
283 VERIFY (counter == 32);
284 forward_list.pop_front();
285 VERIFY (counter == 31);
286 forward_list.resize(100, indirect_counter(&counter));
287 VERIFY (counter == 100);
288 }
289 VERIFY (counter == 0);
290 end:
291 ok = inner_ok;
292 }
293 return ok;
294 }
295
296 bool unordered_map_test()
297 {
298 bool ok;
299 #pragma omp target map(from: ok)
300 {
301 bool inner_ok = true;
302 int counter = 0;
303 {
304 std::unordered_map<int, indirect_counter> unordered_map;
305 unordered_map.insert({1, indirect_counter(&counter)});
306 VERIFY (counter == 1);
307 unordered_map.insert({1, indirect_counter(&counter)});
308 VERIFY (counter == 1);
309 unordered_map.insert({2, indirect_counter(&counter)});
310 VERIFY (counter == 2);
311 }
312 VERIFY (counter == 0);
313 end:
314 ok = inner_ok;
315 }
316 return ok;
317 }
318
319 bool unordered_set_test()
320 {
321 bool ok;
322 #pragma omp target map(from: ok)
323 {
324 bool inner_ok = true;
325 int counter0 = 0;
326 int counter1 = 0;
327 {
328 std::unordered_set<indirect_counter> unordered_set;
329 unordered_set.insert(indirect_counter(&counter0));
330 VERIFY (counter0 == 1);
331 unordered_set.insert(indirect_counter(&counter0));
332 VERIFY (counter0 == 1);
333 unordered_set.insert(indirect_counter(&counter1));
334 VERIFY (counter0 == 1 && counter1 == 1);
335 }
336 VERIFY (counter0 == 0 && counter1 == 0);
337 end:
338 ok = inner_ok;
339 }
340 return ok;
341 }
342
343 bool unordered_multimap_test()
344 {
345 bool ok;
346 #pragma omp target map(from: ok)
347 {
348 bool inner_ok = true;
349 int counter = 0;
350 {
351 std::unordered_multimap<int, indirect_counter> unordered_multimap;
352 unordered_multimap.insert({1, indirect_counter(&counter)});
353 VERIFY (counter == 1);
354 unordered_multimap.insert({1, indirect_counter(&counter)});
355 VERIFY (counter == 2);
356 unordered_multimap.insert({2, indirect_counter(&counter)});
357 VERIFY (counter == 3);
358 }
359 VERIFY (counter == 0);
360 end:
361 ok = inner_ok;
362 }
363 return ok;
364 }
365
366 bool unordered_multiset_test()
367 {
368 bool ok;
369 #pragma omp target map(from: ok)
370 {
371 bool inner_ok = true;
372 int counter0 = 0;
373 int counter1 = 0;
374 {
375 std::unordered_multiset<indirect_counter> unordered_multiset;
376 unordered_multiset.insert(indirect_counter(&counter0));
377 VERIFY (counter0 == 1);
378 unordered_multiset.insert(indirect_counter(&counter0));
379 VERIFY (counter0 == 2);
380 unordered_multiset.insert(indirect_counter(&counter1));
381 VERIFY (counter0 == 2 && counter1 == 1);
382 }
383 VERIFY (counter0 == 0 && counter1 == 0);
384 end:
385 ok = inner_ok;
386 }
387 return ok;
388 }
389
390 #else
391 bool array_test() { return true; }
392 bool forward_list_test() { return true; }
393 bool unordered_map_test() { return true; }
394 bool unordered_set_test() { return true; }
395 bool unordered_multimap_test() { return true; }
396 bool unordered_multiset_test() { return true; }
397 #endif
398
399 int main()
400 {
401 const bool auto_res = automatic_lifetime_test();
402 const bool vec_res = vector_test();
403 const bool deque_res = deque_test();
404 const bool list_res = list_test();
405 const bool map_res = map_test();
406 const bool set_res = set_test();
407 const bool multimap_res = multimap_test();
408 const bool multiset_res = multiset_test();
409 const bool array_res = array_test();
410 const bool forward_list_res = forward_list_test();
411 const bool unordered_map_res = unordered_map_test();
412 const bool unordered_set_res = unordered_set_test();
413 const bool unordered_multimap_res = unordered_multimap_test();
414 const bool unordered_multiset_res = unordered_multiset_test();
415 std::printf("sanity check : %s\n", auto_res ? "PASS" : "FAIL");
416 std::printf("vector : %s\n", vec_res ? "PASS" : "FAIL");
417 std::printf("deque : %s\n", deque_res ? "PASS" : "FAIL");
418 std::printf("list : %s\n", list_res ? "PASS" : "FAIL");
419 std::printf("map : %s\n", map_res ? "PASS" : "FAIL");
420 std::printf("set : %s\n", set_res ? "PASS" : "FAIL");
421 std::printf("multimap : %s\n", multimap_res ? "PASS" : "FAIL");
422 std::printf("multiset : %s\n", multiset_res ? "PASS" : "FAIL");
423 std::printf("array : %s\n", array_res ? "PASS" : "FAIL");
424 std::printf("forward_list : %s\n", forward_list_res ? "PASS" : "FAIL");
425 std::printf("unordered_map : %s\n", unordered_map_res ? "PASS" : "FAIL");
426 std::printf("unordered_set : %s\n", unordered_set_res ? "PASS" : "FAIL");
427 std::printf("unordered_multimap: %s\n", unordered_multimap_res ? "PASS" : "FAIL");
428 std::printf("unordered_multiset: %s\n", unordered_multiset_res ? "PASS" : "FAIL");
429 const bool ok = auto_res
430 && vec_res
431 && deque_res
432 && list_res
433 && map_res
434 && set_res
435 && multimap_res
436 && multiset_res
437 && array_res
438 && forward_list_res
439 && unordered_map_res
440 && unordered_set_res
441 && unordered_multimap_res
442 && unordered_multiset_res;
443 return ok ? 0 : 1;
444 }