aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c++/target-flex-11.C
blob: 6d55129e6a8723e681ee5741cf43a77a5340f833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* Check constructors/destructors are called in containers.  */

#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <utility>
#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <unordered_set>
#include <unordered_map>
#endif

#include "target-flex-common.h"

struct indirect_counter
{
  typedef int counter_value_type;
  counter_value_type *_count_ptr;

  indirect_counter(counter_value_type *count_ptr) BL_NOEXCEPT : _count_ptr(count_ptr) {
    ++(*_count_ptr);
  }
  indirect_counter(const indirect_counter& other) BL_NOEXCEPT : _count_ptr(other._count_ptr) {
    ++(*_count_ptr);
  }
  /* Don't declare a move constructor, we want to copy no matter what.  */
  ~indirect_counter() {
    --(*_count_ptr);
  }
};

bool operator==(indirect_counter const& lhs, indirect_counter const& rhs) BL_NOEXCEPT
  { return lhs._count_ptr == rhs._count_ptr; }
bool operator<(indirect_counter const& lhs, indirect_counter const& rhs) BL_NOEXCEPT
  { return lhs._count_ptr < rhs._count_ptr; }

#if __cplusplus >= 201103L
template<>
struct std::hash<indirect_counter>
{
  std::size_t operator()(const indirect_counter& ic) const noexcept
    { return std::hash<indirect_counter::counter_value_type *>{}(ic._count_ptr); }
};
#endif

/* Not a container, just a sanity check really.  */
bool automatic_lifetime_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	indirect_counter c = indirect_counter(&counter);
	indirect_counter(static_cast<int*>(&counter));
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool vector_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::vector<indirect_counter> vec(42, indirect_counter(&counter));
	VERIFY (counter == 42);
	vec.resize(32, indirect_counter(&counter));
	VERIFY (counter == 32);
	vec.push_back(indirect_counter(&counter));
	VERIFY (counter == 33);
	vec.pop_back();
	VERIFY (counter == 32);
	vec.pop_back();
	VERIFY (counter == 31);
	vec.resize(100, indirect_counter(&counter));
	VERIFY (counter == 100);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool deque_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::deque<indirect_counter> vec(42, indirect_counter(&counter));
	VERIFY (counter == 42);
	vec.resize(32, indirect_counter(&counter));
	VERIFY (counter == 32);
	vec.push_back(indirect_counter(&counter));
	VERIFY (counter == 33);
	vec.pop_back();
	VERIFY (counter == 32);
	vec.pop_back();
	VERIFY (counter == 31);
	vec.resize(100, indirect_counter(&counter));
	VERIFY (counter == 100);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool list_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::list<indirect_counter> list(42, indirect_counter(&counter));
	VERIFY (counter == 42);
	list.resize(32, indirect_counter(&counter));
	VERIFY (counter == 32);
	list.push_back(indirect_counter(&counter));
	VERIFY (counter == 33);
	list.pop_back();
	VERIFY (counter == 32);
	list.pop_back();
	VERIFY (counter == 31);
	list.resize(100, indirect_counter(&counter));
	VERIFY (counter == 100);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool map_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::map<int, indirect_counter> map;
	map.insert(std::make_pair(1, indirect_counter(&counter)));
	VERIFY (counter == 1);
	map.insert(std::make_pair(1, indirect_counter(&counter)));
	VERIFY (counter == 1);
	map.insert(std::make_pair(2, indirect_counter(&counter)));
	VERIFY (counter == 2);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool set_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter0 = 0;
      int counter1 = 0;
      {
	std::set<indirect_counter> set;
	set.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	set.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	set.insert(indirect_counter(&counter1));
	VERIFY (counter0 == 1 && counter1 == 1);
      }
      VERIFY (counter0 == 0 && counter1 == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool multimap_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::multimap<int, indirect_counter> multimap;
	multimap.insert(std::make_pair(1, indirect_counter(&counter)));
	VERIFY (counter == 1);
	multimap.insert(std::make_pair(1, indirect_counter(&counter)));
	VERIFY (counter == 2);
	multimap.insert(std::make_pair(2, indirect_counter(&counter)));
	VERIFY (counter == 3);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool multiset_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter0 = 0;
      int counter1 = 0;
      {
	std::multiset<indirect_counter> multiset;
	multiset.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	multiset.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 2);
	multiset.insert(indirect_counter(&counter1));
	VERIFY (counter0 == 2 && counter1 == 1);
      }
      VERIFY (counter0 == 0 && counter1 == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

#if __cplusplus >= 201103L

bool array_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	indirect_counter ic(&counter);
	std::array<indirect_counter, 10> array{ic, ic, ic, ic, ic,
					       ic, ic, ic, ic, ic};
	VERIFY (counter == 11);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool forward_list_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::forward_list<indirect_counter> forward_list(42, indirect_counter(&counter));
	VERIFY (counter == 42);
	forward_list.resize(32, indirect_counter(&counter));
	VERIFY (counter == 32);
	forward_list.push_front(indirect_counter(&counter));
	VERIFY (counter == 33);
	forward_list.pop_front();
	VERIFY (counter == 32);
	forward_list.pop_front();
	VERIFY (counter == 31);
	forward_list.resize(100, indirect_counter(&counter));
	VERIFY (counter == 100);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool unordered_map_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::unordered_map<int, indirect_counter> unordered_map;
	unordered_map.insert({1, indirect_counter(&counter)});
	VERIFY (counter == 1);
	unordered_map.insert({1, indirect_counter(&counter)});
	VERIFY (counter == 1);
	unordered_map.insert({2, indirect_counter(&counter)});
	VERIFY (counter == 2);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool unordered_set_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter0 = 0;
      int counter1 = 0;
      {
	std::unordered_set<indirect_counter> unordered_set;
	unordered_set.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	unordered_set.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	unordered_set.insert(indirect_counter(&counter1));
	VERIFY (counter0 == 1 && counter1 == 1);
      }
      VERIFY (counter0 == 0 && counter1 == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool unordered_multimap_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter = 0;
      {
	std::unordered_multimap<int, indirect_counter> unordered_multimap;
	unordered_multimap.insert({1, indirect_counter(&counter)});
	VERIFY (counter == 1);
	unordered_multimap.insert({1, indirect_counter(&counter)});
	VERIFY (counter == 2);
	unordered_multimap.insert({2, indirect_counter(&counter)});
	VERIFY (counter == 3);
      }
      VERIFY (counter == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

bool unordered_multiset_test()
{
  bool ok;
  #pragma omp target map(from: ok)
    {
      bool inner_ok = true;
      int counter0 = 0;
      int counter1 = 0;
      {
	std::unordered_multiset<indirect_counter> unordered_multiset;
	unordered_multiset.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 1);
	unordered_multiset.insert(indirect_counter(&counter0));
	VERIFY (counter0 == 2);
	unordered_multiset.insert(indirect_counter(&counter1));
	VERIFY (counter0 == 2 && counter1 == 1);
      }
      VERIFY (counter0 == 0 && counter1 == 0);
      end:
      ok = inner_ok;
    }
  return ok;
}

#else
bool array_test() { return true; }
bool forward_list_test() { return true; }
bool unordered_map_test() { return true; }
bool unordered_set_test() { return true; }
bool unordered_multimap_test() { return true; }
bool unordered_multiset_test() { return true; }
#endif

int main()
{
  const bool auto_res               = automatic_lifetime_test();
  const bool vec_res                = vector_test();
  const bool deque_res              = deque_test();
  const bool list_res               = list_test();
  const bool map_res                = map_test();
  const bool set_res                = set_test();
  const bool multimap_res           = multimap_test();
  const bool multiset_res           = multiset_test();
  const bool array_res              = array_test();
  const bool forward_list_res       = forward_list_test();
  const bool unordered_map_res      = unordered_map_test();
  const bool unordered_set_res      = unordered_set_test();
  const bool unordered_multimap_res = unordered_multimap_test();
  const bool unordered_multiset_res = unordered_multiset_test();
  std::printf("sanity check      : %s\n", auto_res               ? "PASS" : "FAIL");
  std::printf("vector            : %s\n", vec_res                ? "PASS" : "FAIL");
  std::printf("deque             : %s\n", deque_res              ? "PASS" : "FAIL");
  std::printf("list              : %s\n", list_res               ? "PASS" : "FAIL");
  std::printf("map               : %s\n", map_res                ? "PASS" : "FAIL");
  std::printf("set               : %s\n", set_res                ? "PASS" : "FAIL");
  std::printf("multimap          : %s\n", multimap_res           ? "PASS" : "FAIL");
  std::printf("multiset          : %s\n", multiset_res           ? "PASS" : "FAIL");
  std::printf("array             : %s\n", array_res              ? "PASS" : "FAIL");
  std::printf("forward_list      : %s\n", forward_list_res       ? "PASS" : "FAIL");
  std::printf("unordered_map     : %s\n", unordered_map_res      ? "PASS" : "FAIL");
  std::printf("unordered_set     : %s\n", unordered_set_res      ? "PASS" : "FAIL");
  std::printf("unordered_multimap: %s\n", unordered_multimap_res ? "PASS" : "FAIL");
  std::printf("unordered_multiset: %s\n", unordered_multiset_res ? "PASS" : "FAIL");
  const bool ok = auto_res
		  && vec_res
		  && deque_res
		  && list_res
		  && map_res
		  && set_res
		  && multimap_res
		  && multiset_res
		  && array_res
		  && forward_list_res
		  && unordered_map_res
		  && unordered_set_res
		  && unordered_multimap_res
		  && unordered_multiset_res;
  return ok ? 0 : 1;
}