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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
/* Populated with mapped data, validate, mutate, validate again.
The cases using sets do not mutate.
Note: Some of the code in here really sucks due to being made to be
compatible with c++98. */
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <unordered_set>
#include <unordered_map>
#endif
#include <limits>
#include <iterator>
#include "target-flex-common.h"
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
struct identity_func
{
#if __cplusplus < 201103L
template<typename T>
T& operator()(T& arg) const BL_NOEXCEPT { return arg; }
template<typename T>
T const& operator()(T const& arg) const BL_NOEXCEPT { return arg; }
#else
template<typename T>
constexpr T&& operator()(T&& arg) const BL_NOEXCEPT { return std::forward<T>(arg); }
#endif
};
/* Applies projection to the second iterator. */
template<typename It0, typename It1, typename Proj>
bool validate_sequential_elements(const It0 begin0, const It0 end0,
const It1 begin1, const It1 end1,
Proj proj) BL_NOEXCEPT
{
It0 it0 = begin0;
It1 it1 = begin1;
for (; it0 != end0; ++it0, ++it1)
{
/* Sizes mismatch, don't bother aborting though just fail the test. */
if (it1 == end1)
return false;
if (*it0 != proj(*it1))
return false;
}
/* Sizes mismatch, do as above. */
if (it1 != end1)
return false;
return true;
}
template<typename It0, typename It1>
bool validate_sequential_elements(const It0 begin0, const It0 end0,
const It1 begin1, const It1 end1) BL_NOEXCEPT
{
return validate_sequential_elements(begin0, end0, begin1, end1, identity_func());
}
/* Inefficient, but simple. */
template<typename It, typename OutIt>
void simple_copy(const It begin, const It end, OutIt out) BL_NOEXCEPT
{
for (It it = begin; it != end; ++it, ++out)
*out = *it;
}
template<typename It, typename MutateFn>
void simple_mutate(const It begin, const It end, MutateFn mut_fn) BL_NOEXCEPT
{
for (It it = begin; it != end; ++it)
*it = mut_fn(*it);
}
template<typename MutationFunc, typename T, std::size_t Size>
bool vector_test(const T (&arr)[Size])
{
bool ok;
T out_arr[Size];
T out_mut_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::vector<T> vector(arr, arr + Size);
VERIFY (validate_sequential_elements(vector.begin(), vector.end(),
arr, arr + Size));
simple_copy(vector.begin(), vector.end(), out_arr);
simple_mutate(vector.begin(), vector.end(), MutationFunc());
VERIFY (validate_sequential_elements(vector.begin(), vector.end(),
arr, arr + Size, MutationFunc()));
simple_copy(vector.begin(), vector.end(), out_mut_arr);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size,
arr, arr + Size));
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size,
arr, arr + Size, MutationFunc()));
return true;
}
template<typename MutationFunc, typename T, std::size_t Size>
bool deque_test(const T (&arr)[Size])
{
bool ok;
T out_arr[Size];
T out_mut_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::deque<T> deque(arr, arr + Size);
VERIFY (validate_sequential_elements(deque.begin(), deque.end(),
arr, arr + Size));
simple_copy(deque.begin(), deque.end(), out_arr);
simple_mutate(deque.begin(), deque.end(), MutationFunc());
VERIFY (validate_sequential_elements(deque.begin(), deque.end(),
arr, arr + Size, MutationFunc()));
simple_copy(deque.begin(), deque.end(), out_mut_arr);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size,
arr, arr + Size));
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size,
arr, arr + Size, MutationFunc()));
return true;
}
template<typename MutationFunc, typename T, std::size_t Size>
bool list_test(const T (&arr)[Size])
{
bool ok;
T out_arr[Size];
T out_mut_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::list<T> list(arr, arr + Size);
VERIFY (validate_sequential_elements(list.begin(), list.end(),
arr, arr + Size));
simple_copy(list.begin(), list.end(), out_arr);
simple_mutate(list.begin(), list.end(), MutationFunc());
VERIFY (validate_sequential_elements(list.begin(), list.end(),
arr, arr + Size, MutationFunc()));
simple_copy(list.begin(), list.end(), out_mut_arr);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size,
arr, arr + Size));
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size,
arr, arr + Size, MutationFunc()));
return true;
}
template<typename T>
const T& get_key(const T& arg) BL_NOEXCEPT
{ return arg; }
template<typename K, typename V>
const K& get_key(const std::pair<K, V>& pair) BL_NOEXCEPT
{ return pair.first; }
template<typename T>
const T& get_value(const T& arg) BL_NOEXCEPT
{ return arg; }
template<typename K, typename V>
const K& get_value(const std::pair<K, V>& pair) BL_NOEXCEPT
{ return pair.second; }
template<typename T>
struct key_type { typedef T type; };
template<typename K, typename V>
struct key_type<std::pair<K, V> > { typedef K type; };
template<typename Proj, typename Container, typename It>
bool validate_associative(const Container& container,
const It compare_begin,
const It compare_end,
Proj proj) BL_NOEXCEPT
{
const typename Container::const_iterator elem_end = container.end();
for (It compare_it = compare_begin; compare_it != compare_end; ++compare_it)
{
const typename Container::const_iterator elem_it = container.find(get_key(*compare_it));
VERIFY_NON_TARGET (elem_it != elem_end);
VERIFY_NON_TARGET (proj(get_value(*compare_it)) == get_value(*elem_it));
}
return true;
}
template<typename Container, typename It>
bool validate_associative(const Container& container,
const It compare_begin,
const It compare_end) BL_NOEXCEPT
{
return validate_associative(container, compare_begin, compare_end, identity_func());
}
template<typename It, typename MutateFn>
void simple_mutate_map(const It begin, const It end, MutateFn mut_fn) BL_NOEXCEPT
{
for (It it = begin; it != end; ++it)
it->second = mut_fn(it->second);
}
template<typename It, typename OutIter>
void simple_copy_unique(const It begin, const It end, OutIter out) BL_NOEXCEPT
{
/* In case anyone reads this, I want it to be known that I hate c++98. */
typedef typename key_type<typename std::iterator_traits<It>::value_type>::type key_t;
std::set<key_t> already_seen;
for (It it = begin; it != end; ++it, ++out)
{
key_t key = get_key(*it);
if (already_seen.find(key) != already_seen.end())
continue;
already_seen.insert(key);
*out = *it;
}
}
template<typename MutationFunc, typename K, typename V, std::size_t Size>
bool map_test(const std::pair<K, V> (&arr)[Size])
{
std::map<K, V> reference_map(arr, arr + Size);
bool ok;
/* Both sizes should be the same. */
std::pair<K, V> out_pairs[Size];
std::size_t out_size;
std::pair<K, V> out_pairs_mut[Size];
std::size_t out_size_mut;
#pragma omp target map(from: ok, out_pairs[:Size], out_size, \
out_pairs_mut[:Size], out_size_mut) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::vector<std::pair<K, V> > unique_elems;
simple_copy_unique(arr, arr + Size,
std::back_insert_iterator<std::vector<std::pair<K, V> > >(unique_elems));
std::map<K, V> map(arr, arr + Size);
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end()));
simple_copy(map.begin(), map.end(), out_pairs);
out_size = map.size();
simple_mutate_map(map.begin(), map.end(), MutationFunc());
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end(),
MutationFunc()));
simple_copy(map.begin(), map.end(), out_pairs_mut);
out_size_mut = map.size();
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (out_size == out_size_mut);
VERIFY_NON_TARGET (validate_associative(reference_map,
out_pairs, out_pairs + out_size));
simple_mutate_map(reference_map.begin(), reference_map.end(), MutationFunc());
VERIFY_NON_TARGET (validate_associative(reference_map,
out_pairs_mut, out_pairs_mut + out_size_mut));
return true;
}
template<typename T, std::size_t Size>
bool set_test(const T (&arr)[Size])
{
std::set<T> reference_set(arr, arr + Size);
bool ok;
/* Both sizes should be the same. */
T out_arr[Size];
std::size_t out_size;
#pragma omp target map(from: ok, out_arr[:Size], out_size) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::vector<T> unique_elems;
simple_copy_unique(arr, arr + Size,
std::back_insert_iterator<std::vector<T> >(unique_elems));
std::set<T> set(arr, arr + Size);
VERIFY (validate_associative(set, unique_elems.begin(), unique_elems.end()));
simple_copy(set.begin(), set.end(), out_arr);
out_size = set.size();
/* Sets can't be mutated, we could create another set with mutated
but it gets a little annoying and probably isn't an interesting test. */
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_associative(reference_set,
out_arr, out_arr + out_size));
return true;
}
template<typename Proj, typename Container, typename It>
bool validate_multi_associative(const Container& container,
const It compare_begin,
const It compare_end,
Proj proj) BL_NOEXCEPT
{
/* Once again, for the poor soul reviewing these, I hate c++98. */
typedef typename key_type<typename std::iterator_traits<It>::value_type>::type key_t;
typedef std::map<key_t, std::size_t> counter_map;
counter_map key_count_map;
for (It it = compare_begin; it != compare_end; ++it)
{
const key_t& key = get_key(*it);
typename counter_map::iterator counter_it
= key_count_map.find(key);
if (counter_it != key_count_map.end())
++counter_it->second;
else
key_count_map.insert(std::pair<const key_t, std::size_t>(key, std::size_t(1)));
}
const typename Container::const_iterator elem_end = container.end();
for (It compare_it = compare_begin; compare_it != compare_end; ++compare_it)
{
const key_t& key = get_key(*compare_it);
typename counter_map::iterator count_it = key_count_map.find(key);
std::size_t key_count = count_it != key_count_map.end() ? count_it->second
: std::size_t(0);
VERIFY_NON_TARGET (key_count > std::size_t(0) && "this will never happen");
/* This gets tested multiple times but that should be fine. */
VERIFY_NON_TARGET (key_count == container.count(key));
typename Container::const_iterator elem_it = container.find(key);
/* This will never happen if the previous case passed. */
VERIFY_NON_TARGET (elem_it != elem_end);
bool found_element = false;
for (; elem_it != elem_end; ++elem_it)
if (proj(get_value(*compare_it)) == get_value(*elem_it))
{
found_element = true;
break;
}
VERIFY_NON_TARGET (found_element);
}
return true;
}
template<typename Container, typename It>
bool validate_multi_associative(const Container& container,
const It compare_begin,
const It compare_end) BL_NOEXCEPT
{
return validate_multi_associative(container, compare_begin, compare_end, identity_func());
}
template<typename MutationFunc, typename K, typename V, std::size_t Size>
bool multimap_test(const std::pair<K, V> (&arr)[Size])
{
std::multimap<K, V> reference_multimap(arr, arr + Size);
bool ok;
std::pair<K, V> out_pairs[Size];
std::pair<K, V> out_pairs_mut[Size];
#pragma omp target map(from: ok, out_pairs[:Size], out_pairs_mut[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::multimap<K, V> multimap(arr, arr + Size);
VERIFY (validate_multi_associative(multimap, arr, arr + Size));
simple_copy(multimap.begin(), multimap.end(), out_pairs);
simple_mutate_map(multimap.begin(), multimap.end(), MutationFunc());
VERIFY (validate_multi_associative(multimap, arr, arr + Size, MutationFunc()));
simple_copy(multimap.begin(), multimap.end(), out_pairs_mut);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap,
out_pairs, out_pairs + Size));
simple_mutate_map(reference_multimap.begin(), reference_multimap.end(), MutationFunc());
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap,
out_pairs_mut, out_pairs_mut + Size));
return true;
}
template<typename T, std::size_t Size>
bool multiset_test(const T (&arr)[Size])
{
std::multiset<T> reference_multiset(arr, arr + Size);
bool ok;
T out_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::multiset<T> set(arr, arr + Size);
VERIFY (validate_multi_associative(set, arr, arr + Size));
simple_copy(set.begin(), set.end(), out_arr);
/* Sets can't be mutated, we could create another set with mutated
but it gets a little annoying and probably isn't an interesting test. */
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_multi_associative(reference_multiset,
out_arr, out_arr + Size));
return true;
}
#if __cplusplus >= 201103L
template<typename MutationFunc, typename T, std::size_t Size>
bool array_test(const T (&arr)[Size])
{
bool ok;
T out_arr[Size];
T out_mut_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::array<T, Size> std_array{};
/* Special case for std::array since it can't be initialized
with iterators. */
{
T zero_val = T{};
for (auto it = std_array.begin(); it != std_array.end(); ++it)
VERIFY (*it == zero_val);
}
simple_copy(arr, arr + Size, std_array.begin());
VERIFY (validate_sequential_elements(std_array.begin(), std_array.end(),
arr, arr + Size));
simple_copy(std_array.begin(), std_array.end(), out_arr);
simple_mutate(std_array.begin(), std_array.end(), MutationFunc());
VERIFY (validate_sequential_elements(std_array.begin(), std_array.end(),
arr, arr + Size, MutationFunc()));
simple_copy(std_array.begin(), std_array.end(), out_mut_arr);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size,
arr, arr + Size));
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size,
arr, arr + Size, MutationFunc()));
return true;
}
template<typename MutationFunc, typename T, std::size_t Size>
bool forward_list_test(const T (&arr)[Size])
{
bool ok;
T out_arr[Size];
T out_mut_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::forward_list<T> fwd_list(arr, arr + Size);
VERIFY (validate_sequential_elements(fwd_list.begin(), fwd_list.end(),
arr, arr + Size));
simple_copy(fwd_list.begin(), fwd_list.end(), out_arr);
simple_mutate(fwd_list.begin(), fwd_list.end(), MutationFunc());
VERIFY (validate_sequential_elements(fwd_list.begin(), fwd_list.end(),
arr, arr + Size, MutationFunc()));
simple_copy(fwd_list.begin(), fwd_list.end(), out_mut_arr);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size,
arr, arr + Size));
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size,
arr, arr + Size, MutationFunc()));
return true;
}
template<typename MutationFunc, typename K, typename V, std::size_t Size>
bool unordered_map_test(const std::pair<K, V> (&arr)[Size])
{
std::unordered_map<K, V> reference_map(arr, arr + Size);
bool ok;
/* Both sizes should be the same. */
std::pair<K, V> out_pairs[Size];
std::size_t out_size;
std::pair<K, V> out_pairs_mut[Size];
std::size_t out_size_mut;
#pragma omp target map(from: ok, out_pairs[:Size], out_size, \
out_pairs_mut[:Size], out_size_mut) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::vector<std::pair<K, V> > unique_elems;
simple_copy_unique(arr, arr + Size,
std::back_insert_iterator<std::vector<std::pair<K, V> > >(unique_elems));
std::unordered_map<K, V> map(arr, arr + Size);
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end()));
simple_copy(map.begin(), map.end(), out_pairs);
out_size = map.size();
simple_mutate_map(map.begin(), map.end(), MutationFunc());
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end(),
MutationFunc()));
simple_copy(map.begin(), map.end(), out_pairs_mut);
out_size_mut = map.size();
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (out_size == out_size_mut);
VERIFY_NON_TARGET (validate_associative(reference_map,
out_pairs, out_pairs + out_size));
simple_mutate_map(reference_map.begin(), reference_map.end(), MutationFunc());
VERIFY_NON_TARGET (validate_associative(reference_map,
out_pairs_mut, out_pairs_mut + out_size_mut));
return true;
}
template<typename T, std::size_t Size>
bool unordered_set_test(const T (&arr)[Size])
{
std::unordered_set<T> reference_set(arr, arr + Size);
bool ok;
/* Both sizes should be the same. */
T out_arr[Size];
std::size_t out_size;
#pragma omp target map(from: ok, out_arr[:Size], out_size) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::vector<T> unique_elems;
simple_copy_unique(arr, arr + Size,
std::back_insert_iterator<std::vector<T> >(unique_elems));
std::unordered_set<T> set(arr, arr + Size);
VERIFY (validate_associative(set, unique_elems.begin(), unique_elems.end()));
simple_copy(set.begin(), set.end(), out_arr);
out_size = set.size();
/* Sets can't be mutated, we could create another set with mutated
but it gets a little annoying and probably isn't an interesting test. */
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_associative(reference_set,
out_arr, out_arr + out_size));
return true;
}
template<typename MutationFunc, typename K, typename V, std::size_t Size>
bool unordered_multimap_test(const std::pair<K, V> (&arr)[Size])
{
std::unordered_multimap<K, V> reference_multimap(arr, arr + Size);
bool ok;
std::pair<K, V> out_pairs[Size];
std::pair<K, V> out_pairs_mut[Size];
#pragma omp target map(from: ok, out_pairs[:Size], out_pairs_mut[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::unordered_multimap<K, V> multimap(arr, arr + Size);
VERIFY (validate_multi_associative(multimap, arr, arr + Size));
simple_copy(multimap.begin(), multimap.end(), out_pairs);
simple_mutate_map(multimap.begin(), multimap.end(), MutationFunc());
VERIFY (validate_multi_associative(multimap, arr, arr + Size, MutationFunc()));
simple_copy(multimap.begin(), multimap.end(), out_pairs_mut);
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap,
out_pairs, out_pairs + Size));
simple_mutate_map(reference_multimap.begin(), reference_multimap.end(), MutationFunc());
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap,
out_pairs_mut, out_pairs_mut + Size));
return true;
}
template<typename T, std::size_t Size>
bool unordered_multiset_test(const T (&arr)[Size])
{
std::unordered_multiset<T> reference_multiset(arr, arr + Size);
bool ok;
T out_arr[Size];
#pragma omp target map(from: ok, out_arr[:Size]) \
map(to: arr[:Size])
{
bool inner_ok = true;
{
std::unordered_multiset<T> set(arr, arr + Size);
VERIFY (validate_multi_associative(set, arr, arr + Size));
simple_copy(set.begin(), set.end(), out_arr);
/* Sets can't be mutated, we could create another set with mutated
but it gets a little annoying and probably isn't an interesting test. */
}
end:
ok = inner_ok;
}
if (!ok)
return false;
VERIFY_NON_TARGET (validate_multi_associative(reference_multiset,
out_arr, out_arr + Size));
return true;
}
#else
template<typename, typename T, std::size_t Size> bool array_test(const T (&arr)[Size]) { return true; }
template<typename, typename T, std::size_t Size> bool forward_list_test(const T (&arr)[Size]) { return true; }
template<typename, typename T, std::size_t Size> bool unordered_map_test(const T (&arr)[Size]) { return true; }
template<typename T, std::size_t Size> bool unordered_set_test(const T (&arr)[Size]) { return true; }
template<typename, typename T, std::size_t Size> bool unordered_multimap_test(const T (&arr)[Size]) { return true; }
template<typename T, std::size_t Size> bool unordered_multiset_test(const T (&arr)[Size]) { return true; }
#endif
/* This clamps to the maximum value to guard against overflowing,
assuming std::numeric_limits is specialized for T. */
struct multiply_by_2
{
template<typename T>
typename enable_if<std::numeric_limits<T>::is_specialized, T>::type
operator()(T arg) const BL_NOEXCEPT {
if (arg < static_cast<T>(0))
{
if (std::numeric_limits<T>::min() / static_cast<T>(2) >= arg)
return std::numeric_limits<T>::min();
}
else
{
if (std::numeric_limits<T>::max() / static_cast<T>(2) <= arg)
return std::numeric_limits<T>::max();
}
return arg * 2;
}
template<typename T>
typename enable_if<!std::numeric_limits<T>::is_specialized, T>::type
operator()(T arg) const BL_NOEXCEPT {
return arg * 2;
}
};
int main()
{
int data[8] = {0, 1, 2, 3, 4, 5, 6, 7};
std::pair<int, int> pairs[10] = {std::pair<int, int>( 1, 2),
std::pair<int, int>( 2, 4),
std::pair<int, int>( 3, 6),
std::pair<int, int>( 4, 8),
std::pair<int, int>( 5, 10),
std::pair<int, int>( 6, 12),
std::pair<int, int>( 7, 14),
std::pair<int, int>( 8, 16),
std::pair<int, int>( 9, 18),
std::pair<int, int>(10, 20)};
const bool vec_res = vector_test<multiply_by_2>(data);
const bool deque_res = deque_test<multiply_by_2>(data);
const bool list_res = list_test<multiply_by_2>(data);
const bool map_res = map_test<multiply_by_2>(pairs);
const bool set_res = set_test(data);
const bool multimap_res = multimap_test<multiply_by_2>(pairs);
const bool multiset_res = multiset_test(data);
const bool array_res = array_test<multiply_by_2>(data);
const bool forward_list_res = forward_list_test<multiply_by_2>(data);
const bool unordered_map_res = unordered_map_test<multiply_by_2>(pairs);
const bool unordered_set_res = unordered_set_test(data);
const bool unordered_multimap_res = unordered_multimap_test<multiply_by_2>(pairs);
const bool unordered_multiset_res = unordered_multiset_test(data);
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 = 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;
}
|