File: conformance_concurrent_hash_map.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (1620 lines) | stat: -rw-r--r-- 60,141 bytes parent folder | download | duplicates (4)
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
/*
    Copyright (c) 2005-2024 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <common/test.h>
#include <common/utils.h>
#include "common/utils_report.h"
#include <common/spin_barrier.h>
#include <common/state_trackable.h>
#include <common/container_move_support.h>
#include <common/containers_common.h>
#include <common/initializer_list_support.h>
#include <common/vector_types.h>
#include <common/test_comparisons.h>
#include "oneapi/tbb/concurrent_hash_map.h"
#include "oneapi/tbb/global_control.h"
#include "oneapi/tbb/parallel_for.h"

//! \file conformance_concurrent_hash_map.cpp
//! \brief Test for [containers.concurrent_hash_map containers.tbb_hash_compare] specification

/** Has tightly controlled interface so that we can verify
    that concurrent_hash_map uses only the required interface. */
class MyException : public std::bad_alloc {
public:
    virtual const char *what() const noexcept override { return "out of items limit"; }
    virtual ~MyException() noexcept {}
};

/** Has tightly controlled interface so that we can verify
    that concurrent_hash_map uses only the required interface. */
class MyKey {
private:
    int key;
    friend class MyHashCompare;
    friend class YourHashCompare;
public:
    MyKey() = default;
    MyKey( const MyKey& ) = default;
    void operator=( const MyKey&  ) = delete;
    static MyKey make( int i ) {
        MyKey result;
        result.key = i;
        return result;
    }

    int value_of() const { return key; }
};

std::atomic<long> MyDataCount;
long MyDataCountLimit = 0;

class MyData {
protected:
    friend class MyData2;
    int data;
    enum state_t {
        LIVE=0x1234,
        DEAD=0x5678
    } my_state;
    void operator=( const MyData& );    // Deny access
public:
    MyData(int i = 0) {
        my_state = LIVE;
        data = i;
        if (MyDataCountLimit && MyDataCount + 1 >= MyDataCountLimit) {
            TBB_TEST_THROW(MyException{});
        }
        ++MyDataCount;
    }

    MyData( const MyData& other ) {
        CHECK_FAST(other.my_state==LIVE);
        my_state = LIVE;
        data = other.data;
        if(MyDataCountLimit && MyDataCount + 1 >= MyDataCountLimit) {
            TBB_TEST_THROW(MyException{});
        }
        ++MyDataCount;
    }

    ~MyData() {
        --MyDataCount;
        my_state = DEAD;
    }

    static MyData make( int i ) {
        MyData result;
        result.data = i;
        return result;
    }

    int value_of() const {
        CHECK_FAST(my_state==LIVE);
        return data;
    }

    void set_value( int i ) {
        CHECK_FAST(my_state==LIVE);
        data = i;
    }

    bool operator==( const MyData& other ) const {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        return data == other.data;
    }
};

class MyData2 : public MyData {
public:
    MyData2( ) {}

    MyData2( const MyData2& other ) : MyData() {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        data = other.data;
    }

    MyData2( const MyData& other ) {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        data = other.data;
    }

    void operator=( const MyData& other ) {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        data = other.data;
    }

    void operator=( const MyData2& other ) {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        data = other.data;
    }

    bool operator==( const MyData2& other ) const {
        CHECK_FAST(other.my_state==LIVE);
        CHECK_FAST(my_state==LIVE);
        return data == other.data;
    }
};

class MyHashCompare {
public:
    bool equal( const MyKey& j, const MyKey& k ) const {
        return j.key==k.key;
    }

    std::size_t hash( const MyKey& k ) const {
        return k.key;
    }
};

class YourHashCompare {
public:
    bool equal( const MyKey& j, const MyKey& k ) const {
        return j.key==k.key;
    }

    std::size_t hash( const MyKey& ) const {
        return 1;
    }
};

using test_allocator_type = StaticSharedCountingAllocator<std::allocator<std::pair<const MyKey, MyData>>>;
using test_table_type = oneapi::tbb::concurrent_hash_map<MyKey, MyData, MyHashCompare, test_allocator_type>;
using other_test_table_type = oneapi::tbb::concurrent_hash_map<MyKey, MyData2, MyHashCompare>;

template <template <typename...> class ContainerType>
void test_member_types() {
    using container_type = ContainerType<int, int>;
    static_assert(std::is_same<typename container_type::allocator_type, oneapi::tbb::tbb_allocator<std::pair<const int, int>>>::value,
                  "Incorrect default template allocator");

    static_assert(std::is_same<typename container_type::key_type, int>::value,
                  "Incorrect container key_type member type");
    static_assert(std::is_same<typename container_type::value_type, std::pair<const int, int>>::value,
                  "Incorrect container value_type member type");

    static_assert(std::is_unsigned<typename container_type::size_type>::value,
                  "Incorrect container size_type member type");
    static_assert(std::is_signed<typename container_type::difference_type>::value,
                  "Incorrect container difference_type member type");

    using value_type = typename container_type::value_type;
    static_assert(std::is_same<typename container_type::reference, value_type&>::value,
                  "Incorrect container reference member type");
    static_assert(std::is_same<typename container_type::const_reference, const value_type&>::value,
                  "Incorrect container const_reference member type");
    using allocator_type = typename container_type::allocator_type;
    static_assert(std::is_same<typename container_type::pointer, typename std::allocator_traits<allocator_type>::pointer>::value,
                  "Incorrect container pointer member type");
    static_assert(std::is_same<typename container_type::const_pointer, typename std::allocator_traits<allocator_type>::const_pointer>::value,
                  "Incorrect container const_pointer member type");

    static_assert(utils::is_forward_iterator<typename container_type::iterator>::value,
                  "Incorrect container iterator member type");
    static_assert(!std::is_const<typename container_type::iterator::value_type>::value,
                  "Incorrect container iterator member type");
    static_assert(utils::is_forward_iterator<typename container_type::const_iterator>::value,
                  "Incorrect container const_iterator member type");
    static_assert(std::is_const<typename container_type::const_iterator::value_type>::value,
                  "Incorrect container iterator member type");
}

template<typename test_table_type>
void FillTable( test_table_type& x, int n ) {
    for( int i=1; i<=n; ++i ) {
        MyKey key( MyKey::make(-i) ); // hash values must not be specified in direct order
        typename test_table_type::accessor a;
        bool b = x.insert(a,key);
        CHECK_FAST(b);
        a->second.set_value( i*i );
    }
}

template<typename test_table_type>
static void CheckTable( const test_table_type& x, int n ) {
    REQUIRE_MESSAGE( x.size()==size_t(n), "table is different size than expected" );
    CHECK(x.empty()==(n==0));
    CHECK(x.size()<=x.max_size());
    for( int i=1; i<=n; ++i ) {
        MyKey key( MyKey::make(-i) );
        typename test_table_type::const_accessor a;
        bool b = x.find(a,key);
        CHECK_FAST(b);
        CHECK_FAST(a->second.value_of()==i*i);
    }
    int count = 0;
    int key_sum = 0;
    for( typename test_table_type::const_iterator i(x.begin()); i!=x.end(); ++i ) {
        ++count;
        key_sum += -i->first.value_of();
    }
    CHECK(count==n);
    CHECK(key_sum==n*(n+1)/2);
}

void TestCopy() {
    INFO("testing copy\n");
    test_table_type t1;
    for( int i=0; i<10000; i=(i<100 ? i+1 : i*3) ) {
        MyDataCount = 0;

        FillTable(t1,i);
        // Do not call CheckTable(t1,i) before copying, it enforces rehashing

        test_table_type t2(t1);
        // Check that copy constructor did not mangle source table.
        CheckTable(t1,i);
        swap(t1, t2);
        CheckTable(t1,i);
        CHECK(!(t1 != t2));

        // Clear original table
        t2.clear();
        swap(t2, t1);
        CheckTable(t1,0);

        // Verify that copy of t1 is correct, even after t1 is cleared.
        CheckTable(t2,i);
        t2.clear();
        t1.swap( t2 );
        CheckTable(t1,0);
        CheckTable(t2,0);
        REQUIRE_MESSAGE( MyDataCount==0, "data leak?" );
    }
}

void TestRehash() {
    INFO("testing rehashing\n");
    test_table_type w;
    w.insert( std::make_pair(MyKey::make(-5), MyData()) );
    w.rehash(); // without this, assertion will fail
    test_table_type::iterator it = w.begin();
    int i = 0; // check for non-rehashed buckets
    for( ; it != w.end(); i++ )
        w.count( (it++)->first );
    CHECK(i == 1);
    for( i=0; i<1000; i=(i<29 ? i+1 : i*2) ) {
        for( int j=std::max(256+i, i*2); j<10000; j*=3 ) {
            test_table_type v;
            FillTable( v, i );
            CHECK(int(v.size()) == i);
            CHECK(int(v.bucket_count()) <= j);
            v.rehash( j );
            CHECK(int(v.bucket_count()) >= j);
            CheckTable( v, i );
        }
    }
}

void TestAssignment() {
    INFO("testing assignment\n");
    oneapi::tbb::concurrent_hash_map<int, int> test_map({{1, 2}, {2, 4}});
    test_map.operator=(test_map); // suppress self assign warning
    CHECK(!test_map.empty());

    for( int i=0; i<1000; i=(i<30 ? i+1 : i*5) ) {
        for( int j=0; j<1000; j=(j<30 ? j+1 : j*7) ) {
            test_table_type t1;
            test_table_type t2;
            FillTable(t1,i);
            FillTable(t2,j);
            CHECK((t1 == t2) == (i == j));
            CheckTable(t2,j);

            test_table_type& tref = t2=t1;
            CHECK(&tref==&t2);
            CHECK(t1 == t2);
            CheckTable(t1,i);
            CheckTable(t2,i);

            t1.clear();
            CheckTable(t1,0);
            CheckTable(t2,i);
            REQUIRE_MESSAGE( MyDataCount==i, "data leak?" );

            t2.clear();
            CheckTable(t1,0);
            CheckTable(t2,0);
            REQUIRE_MESSAGE( MyDataCount==0, "data leak?" );
        }
    }
}

template<typename Iterator, typename T>
void TestIteratorTraits() {
    T x;
    typename Iterator::reference xr = x;
    typename Iterator::pointer xp = &x;
    CHECK(&xr==xp);
}

template<typename Iterator1, typename Iterator2>
void TestIteratorAssignment( Iterator2 j ) {
    Iterator1 i(j), k;
    CHECK(i==j);
    CHECK(!(i!=j));
    k = j;
    CHECK(k==j);
    CHECK(!(k!=j));
}

template<typename Range1, typename Range2>
void TestRangeAssignment( Range2 r2 ) {
    Range1 r1(r2); r1 = r2;
}

void TestIteratorsAndRanges() {
    INFO("testing iterators compliance\n");
    TestIteratorTraits<test_table_type::iterator,test_table_type::value_type>();
    TestIteratorTraits<test_table_type::const_iterator,const test_table_type::value_type>();

    test_table_type v;
    CHECK(v.range().grainsize() == 1);
    test_table_type const &u = v;

    TestIteratorAssignment<test_table_type::const_iterator>( u.begin() );
    TestIteratorAssignment<test_table_type::const_iterator>( v.begin() );
    TestIteratorAssignment<test_table_type::iterator>( v.begin() );
    // doesn't compile as expected: TestIteratorAssignment<typename V::iterator>( u.begin() );

    // check for non-existing
    CHECK(v.equal_range(MyKey::make(-1)) == std::make_pair(v.end(), v.end()));
    CHECK(u.equal_range(MyKey::make(-1)) == std::make_pair(u.end(), u.end()));

    INFO("testing ranges compliance\n");
    TestRangeAssignment<test_table_type::const_range_type>( u.range() );
    TestRangeAssignment<test_table_type::range_type>( v.range() );
    // doesn't compile as expected: TestRangeAssignment<typename V::range_type>( u.range() );

    INFO("testing construction and insertion from iterators range\n");
    FillTable( v, 1000 );
    other_test_table_type t(v.begin(), v.end());
    v.rehash();
    CheckTable(t, 1000);
    t.insert(v.begin(), v.end()); // do nothing
    CheckTable(t, 1000);
    t.clear();
    t.insert(v.begin(), v.end()); // restore
    CheckTable(t, 1000);

    INFO("testing comparison\n");
    using test_allocator_type2 = StaticSharedCountingAllocator<std::allocator<std::pair<const MyKey, MyData2>>>;
    using YourTable1 = oneapi::tbb::concurrent_hash_map<MyKey,MyData2,YourHashCompare, test_allocator_type2>;
    using YourTable2 = oneapi::tbb::concurrent_hash_map<MyKey,MyData2,YourHashCompare>;
    YourTable1 t1;
    FillTable( t1, 10 );
    CheckTable(t1, 10 );
    YourTable2 t2(t1.begin(), t1.end());
    MyKey key( MyKey::make(-5) ); MyData2 data;
    CHECK(t2.erase(key));
    YourTable2::accessor a;
    CHECK(t2.insert(a, key));
    data.set_value(0);   a->second = data;
    CHECK(t1 != t2);
    data.set_value(5*5); a->second = data;
    CHECK(t1 == t2);
}

struct test_insert {
    template<typename container_type, typename element_type>
    static void test( std::initializer_list<element_type> il, container_type const& expected ) {
        container_type vd;
        vd.insert( il );
        REQUIRE_MESSAGE( vd == expected, "inserting with an initializer list failed" );
    }
};

struct ctor_test {
 template<typename container_type, typename element_type>
    static void test( std::initializer_list<element_type> il, container_type const& expected ) {
        container_type vd(il, tbb::tbb_allocator<std::pair<element_type, element_type>>{});
        REQUIRE_MESSAGE( vd == expected, "inserting with an initializer list failed" );
    }
};

void TestInitList(){
    using namespace initializer_list_support_tests;
    INFO("testing initializer_list methods \n");

    using ch_map_type = oneapi::tbb::concurrent_hash_map<int,int>;
    std::initializer_list<ch_map_type::value_type> pairs_il = {{1,1},{2,2},{3,3},{4,4},{5,5}};

    test_initializer_list_support_without_assign<ch_map_type, test_insert>( pairs_il );
    test_initializer_list_support_without_assign<ch_map_type, test_insert>( {} );
    test_initializer_list_support_without_assign<ch_map_type, ctor_test>(pairs_il);
}

template <typename base_alloc_type>
class only_node_counting_allocator : public StaticSharedCountingAllocator<base_alloc_type> {
    using base_type = StaticSharedCountingAllocator<base_alloc_type>;
    using base_traits = oneapi::tbb::detail::allocator_traits<base_alloc_type>;
public:
    template<typename U>
    struct rebind {
        using other = only_node_counting_allocator<typename base_traits::template rebind_alloc<U>>;
    };

    only_node_counting_allocator() : base_type() {}
    only_node_counting_allocator(const only_node_counting_allocator& a) : base_type(a) {}

    template<typename U>
    only_node_counting_allocator(const only_node_counting_allocator<U>& a) : base_type(a) {}

    typename base_type::value_type* allocate(const std::size_t n) {
        if ( n > 1) {
            return base_alloc_type::allocate(n);
        } else {
            return base_type::allocate(n);
        }
    }
};

#if TBB_USE_EXCEPTIONS
void TestExceptions() {
    using allocator_type = only_node_counting_allocator<oneapi::tbb::tbb_allocator<std::pair<const MyKey, MyData2>>>;
    using throwing_table = oneapi::tbb::concurrent_hash_map<MyKey, MyData2, MyHashCompare, allocator_type>;
    enum methods {
        zero_method = 0,
        ctor_copy, op_assign, op_insert,
        all_methods
    };

    INFO("testing exception-safety guarantees\n");
    throwing_table src;
    FillTable( src, 1000 );
    CHECK(MyDataCount==1000);

    try {
        for(int t = 0; t < 2; t++) // exception type
        for(int m = zero_method+1; m < all_methods; m++)
        {
            allocator_type a;
            allocator_type::init_counters();
            if(t) MyDataCountLimit = 101;
            else a.set_limits(101);
            throwing_table victim(a);
            MyDataCount = 0;

            try {
                switch(m) {
                case ctor_copy: {
                        throwing_table acopy(src, a);
                    } break;
                case op_assign: {
                        victim = src;
                    } break;
                case op_insert: {
                        // Insertion in cpp11 don't make copy constructions
                        // during the insertion, so we need to decrement limit
                        // to throw an exception in the right place and to prevent
                        // successful insertion of one unexpected item
                        if (MyDataCountLimit)
                            --MyDataCountLimit;
                        FillTable( victim, 1000 );
                    } break;
                default:;
                }
                REQUIRE_MESSAGE(false, "should throw an exception");
            } catch(std::bad_alloc &e) {
                MyDataCountLimit = 0;
                size_t size = victim.size();
                switch(m) {
                case op_assign:
                    REQUIRE_MESSAGE( MyDataCount==100, "data leak?" );
                    CHECK(size>=100);
                    utils_fallthrough;
                case ctor_copy:
                    CheckTable(src, 1000);
                    break;
                case op_insert:
                    CHECK(size==size_t(100-t));
                    REQUIRE_MESSAGE( MyDataCount==100-t, "data leak?" );
                    CheckTable(victim, 100-t);
                    break;

                default:; // nothing to check here
                }
                INFO("Exception "<< m << " : " << e.what() << "- ok ()");
            }
            catch ( ... ) {
                REQUIRE_MESSAGE( false, "Unrecognized exception" );
            }
        }
    } catch(...) {
        REQUIRE_MESSAGE(false, "unexpected exception");
    }
    src.clear(); MyDataCount = 0;
    allocator_type::max_items = 0;
}
#endif

struct default_container_traits {
    template <typename container_type, typename iterator_type>
    static container_type& construct_container(typename std::aligned_storage<sizeof(container_type)>::type& storage, iterator_type begin, iterator_type end){
        container_type* ptr = reinterpret_cast<container_type*>(&storage);
        new (ptr) container_type(begin, end);
        return *ptr;
    }

    template <typename container_type, typename iterator_type, typename allocator_type>
    static container_type& construct_container(typename std::aligned_storage<sizeof(container_type)>::type& storage, iterator_type begin, iterator_type end, allocator_type const& a){
        container_type* ptr = reinterpret_cast<container_type*>(&storage);
        new (ptr) container_type(begin, end, a);
        return *ptr;
    }
};

struct hash_map_traits : default_container_traits {
    enum{ expected_number_of_items_to_allocate_for_steal_move = 0 };

    template<typename T>
    struct hash_compare {
        bool equal( const T& lhs, const T& rhs ) const {
            return lhs==rhs;
        }
        size_t hash( const T& k ) const {
            return my_hash_func(k);
        }
        std::hash<T> my_hash_func;
    };

    template <typename T, typename Allocator>
    using container_type = oneapi::tbb::concurrent_hash_map<T, T, hash_compare<T>, Allocator>;

    template <typename T>
    using container_value_type = std::pair<const T, T>;

    template<typename element_type, typename allocator_type>
    struct apply {
        using type = oneapi::tbb::concurrent_hash_map<element_type, element_type, hash_compare<element_type>, allocator_type>;
    };

    using init_iterator_type = move_support_tests::FooPairIterator;
    template <typename hash_map_type, typename iterator>
    static bool equal(hash_map_type const& c, iterator begin, iterator end){
        bool equal_sizes = ( static_cast<size_t>(std::distance(begin, end)) == c.size() );
        if (!equal_sizes)
            return false;

        for (iterator it = begin; it != end; ++it ){
            if (c.count( (*it).first) == 0){
                return false;
            }
        }
        return true;
    }
};

using DataStateTrackedTable = oneapi::tbb::concurrent_hash_map<MyKey, move_support_tests::Foo, MyHashCompare>;

struct RvalueInsert {
    static void apply( DataStateTrackedTable& table, int i ) {
        DataStateTrackedTable::accessor a;
        int next = i + 1;
        CHECK_FAST_MESSAGE((table.insert( a, std::make_pair(MyKey::make(i), move_support_tests::Foo(next)))),
            "already present while should not ?" );
        CHECK_FAST((*a).second == next);
        CHECK_FAST((*a).second.state == StateTrackableBase::MoveInitialized);
    }
};

struct Emplace {
    template <typename Accessor>
    static void apply_impl( DataStateTrackedTable& table, int i) {
        Accessor a;
        CHECK_FAST_MESSAGE((table.emplace( a, MyKey::make(i), (i + 1))),
                "already present while should not ?" );
        CHECK_FAST((*a).second == i + 1);
        CHECK_FAST((*a).second.state == StateTrackableBase::DirectInitialized);
    }

    static void apply( DataStateTrackedTable& table, int i ) {
        // TODO: investigate ability to rewrite apply methods with use apply_imp method.
        if (i % 2) {
            apply_impl<DataStateTrackedTable::accessor>(table, i);
        } else {
            apply_impl<DataStateTrackedTable::const_accessor>(table, i);
        }
    }
};

template<typename Op, typename test_table_type>
class TableOperation {
    test_table_type& my_table;
public:
    void operator()( const oneapi::tbb::blocked_range<int>& range ) const {
        for( int i=range.begin(); i!=range.end(); ++i )
            Op::apply(my_table,i);
    }
    TableOperation( test_table_type& table ) : my_table(table) {}
};

bool UseKey( size_t i ) {
    return (i&3)!=3;
}

struct Insert {
    static void apply( test_table_type& table, int i ) {
        if( UseKey(i) ) {
            if( i&4 ) {
                test_table_type::accessor a;
                table.insert( a, MyKey::make(i) );
                if( i&1 )
                    (*a).second.set_value(i*i);
                else
                    a->second.set_value(i*i);
            } else
                if( i&1 ) {
                    test_table_type::accessor a;
                    table.insert( a, std::make_pair(MyKey::make(i), MyData(i*i)) );
                    CHECK_FAST((*a).second.value_of()==i*i);
                } else {
                    test_table_type::const_accessor ca;
                    table.insert( ca, std::make_pair(MyKey::make(i), MyData(i*i)) );
                    CHECK_FAST(ca->second.value_of()==i*i);
                }
        }
    }
};

struct Find {
    static void apply( test_table_type& table, int i ) {
        test_table_type::accessor a;
        const test_table_type::accessor& ca = a;
        bool b = table.find( a, MyKey::make(i) );
        CHECK_FAST(b==!a.empty());
        if( b ) {
            if( !UseKey(i) )
                REPORT("Line %d: unexpected key %d present\n",__LINE__,i);
            CHECK_FAST(ca->second.value_of()==i*i);
            CHECK_FAST((*ca).second.value_of()==i*i);
            if( i&1 )
                ca->second.set_value( ~ca->second.value_of() );
            else
                (*ca).second.set_value( ~ca->second.value_of() );
        } else {
            if( UseKey(i) )
                REPORT("Line %d: key %d missing\n",__LINE__,i);
        }
    }
};

struct FindConst {
    static void apply( const test_table_type& table, int i ) {
        test_table_type::const_accessor a;
        const test_table_type::const_accessor& ca = a;
        bool b = table.find( a, MyKey::make(i) );
        CHECK_FAST(b==(table.count(MyKey::make(i))>0));
        CHECK_FAST(b==!a.empty());
        CHECK_FAST(b==UseKey(i));
        if( b ) {
            CHECK_FAST(ca->second.value_of()==~(i*i));
            CHECK_FAST((*ca).second.value_of()==~(i*i));
        }
    }
};

struct InsertInitList {
    static void apply( test_table_type& table, int i ) {
        if ( UseKey( i ) ) {
            // TODO: investigate why the following sequence causes an additional allocation sometimes:
            // table.insert( test_table_type::value_type( MyKey::make( i ), i*i ) );
            // table.insert( test_table_type::value_type( MyKey::make( i ), i*i+1 ) );
            std::initializer_list<test_table_type::value_type> il = {
                test_table_type::value_type( MyKey::make( i ), i*i )
                /*, test_table_type::value_type( MyKey::make( i ), i*i+1 ) */
                                                                    };
            table.insert( il );
        }
    }
};

template<typename Op, typename TableType>
void DoConcurrentOperations( TableType& table, int n, const char* what, std::size_t nthread ) {
    INFO("testing " << what << " with " << nthread << " threads");
    oneapi::tbb::parallel_for(oneapi::tbb::blocked_range<int>(0, n ,100), TableOperation<Op, TableType>(table));
}

//! Test traversing the table with an iterator.
void TraverseTable( test_table_type& table, size_t n, size_t expected_size ) {
    INFO("testing traversal\n");
    size_t actual_size = table.size();
    CHECK(actual_size==expected_size);
    size_t count = 0;
    bool* array = new bool[n];
    memset( array, 0, n*sizeof(bool) );
    const test_table_type& const_table = table;
    test_table_type::const_iterator ci = const_table.begin();
    for( test_table_type::iterator i = table.begin(); i!=table.end(); ++i ) {
        // Check iterator
        int k = i->first.value_of();
        CHECK_FAST(UseKey(k));
        CHECK_FAST((*i).first.value_of()==k);
        CHECK_FAST_MESSAGE((0<=k && size_t(k)<n), "out of bounds key" );
        CHECK_FAST_MESSAGE( !array[k], "duplicate key" );
        array[k] = true;
        ++count;

        // Check lower/upper bounds
        std::pair<test_table_type::iterator, test_table_type::iterator> er = table.equal_range(i->first);
        std::pair<test_table_type::const_iterator, test_table_type::const_iterator> cer = const_table.equal_range(i->first);
        CHECK_FAST((cer.first == er.first && cer.second == er.second));
        CHECK_FAST(cer.first == i);
        CHECK_FAST(std::distance(cer.first, cer.second) == 1);

        // Check const_iterator
        test_table_type::const_iterator cic = ci++;
        CHECK_FAST(cic->first.value_of()==k);
        CHECK_FAST((*cic).first.value_of()==k);
    }
    CHECK(ci==const_table.end());
    delete[] array;
    if (count != expected_size) {
        INFO("Line " << __LINE__ << ": count=" << count << " but should be " << expected_size);
    }
}

std::atomic<int> EraseCount;

struct Erase {
    static void apply( test_table_type& table, int i ) {
        bool b;
        if(i&4) {
            if(i&8) {
                test_table_type::const_accessor a;
                b = table.find( a, MyKey::make(i) ) && table.erase( a );
            } else {
                test_table_type::accessor a;
                b = table.find( a, MyKey::make(i) ) && table.erase( a );
            }
        } else
            b = table.erase( MyKey::make(i) );
        if( b ) ++EraseCount;
        CHECK_FAST(table.count(MyKey::make(i)) == 0);
    }
};

using YourTable = oneapi::tbb::concurrent_hash_map<MyKey, MyData, YourHashCompare>;
static const int IE_SIZE = 2;
std::atomic<YourTable::size_type> InsertEraseCount[IE_SIZE];

struct InsertErase  {
    static void apply( YourTable& table, int i ) {
        if ( i%3 ) {
            int key = i%IE_SIZE;
            if ( table.insert( std::make_pair(MyKey::make(key), MyData2()) ) )
                ++InsertEraseCount[key];
        } else {
            int key = i%IE_SIZE;
            if( i&1 ) {
                YourTable::accessor res;
                if(table.find( res, MyKey::make(key) ) && table.erase( res ) )
                    --InsertEraseCount[key];
            } else {
                YourTable::const_accessor res;
                if(table.find( res, MyKey::make(key) ) && table.erase( res ) )
                    --InsertEraseCount[key];
            }
        }
    }
};

struct InnerInsert {
    static void apply( YourTable& table, int i ) {
        YourTable::accessor a1, a2;
        if(i&1) utils::yield();
        table.insert( a1, MyKey::make(1) );
        utils::yield();
        table.insert( a2, MyKey::make(1 + (1<<30)) ); // the same chain
        table.erase( a2 ); // if erase by key it would lead to deadlock for single thread
    }
};

struct FakeExclusive {
    utils::SpinBarrier& barrier;
    YourTable& table;
    FakeExclusive(utils::SpinBarrier& b, YourTable&t) : barrier(b), table(t) {}
    void operator()( std::size_t i ) const {
        if(i) {
            YourTable::const_accessor real_ca;
            // const accessor on non-const table acquired as reader (shared)
            CHECK(table.find(real_ca,MyKey::make(1)));
            barrier.wait(); // item can be erased
            std::this_thread::sleep_for(std::chrono::milliseconds(10)); // let it enter the erase
            real_ca->second.value_of(); // check the state while holding accessor
        } else {
            YourTable::accessor fake_ca;
            const YourTable &const_table = table;
            // non-const accessor on const table acquired as reader (shared)
            CHECK(const_table.find(fake_ca,MyKey::make(1)));
            barrier.wait(); // readers acquired
            // can mistakenly remove the item while other readers still refers to it
            table.erase( fake_ca );
        }
    }
};

using AtomicByte = std::atomic<unsigned char>;

template<typename RangeType>
struct ParallelTraverseBody {
    const size_t n;
    AtomicByte* const array;
    ParallelTraverseBody( AtomicByte array_[], size_t n_ ) :
        n(n_),
        array(array_)
    {}
    void operator()( const RangeType& range ) const {
        for( typename RangeType::iterator i = range.begin(); i!=range.end(); ++i ) {
            int k = i->first.value_of();
            CHECK_FAST((0<=k && size_t(k)<n));
            ++array[k];
        }
    }
};

void Check( AtomicByte array[], size_t n, size_t expected_size ) {
    if( expected_size )
        for( size_t k=0; k<n; ++k ) {
            if( array[k] != int(UseKey(k)) ) {
                REPORT("array[%d]=%d != %d=UseKey(%d)\n",
                       int(k), int(array[k]), int(UseKey(k)), int(k));
                CHECK(false);
            }
        }
}

//! Test traversing the table with a parallel range
void ParallelTraverseTable( test_table_type& table, size_t n, size_t expected_size ) {
    INFO("testing parallel traversal\n");
    CHECK(table.size()==expected_size);
    AtomicByte* array = new AtomicByte[n];

    memset( static_cast<void*>(array), 0, n*sizeof(AtomicByte) );
    test_table_type::range_type r = table.range(10);
    oneapi::tbb::parallel_for( r, ParallelTraverseBody<test_table_type::range_type>( array, n ));
    Check( array, n, expected_size );

    const test_table_type& const_table = table;
    memset( static_cast<void*>(array), 0, n*sizeof(AtomicByte) );
    test_table_type::const_range_type cr = const_table.range(10);
    oneapi::tbb::parallel_for( cr, ParallelTraverseBody<test_table_type::const_range_type>( array, n ));
    Check( array, n, expected_size );

    delete[] array;
}

void TestInsertFindErase( std::size_t nthread ) {
    int n=250000;

    // compute m = number of unique keys
    int m = 0;
    for( int i=0; i<n; ++i )
        m += UseKey(i);
    {
        test_allocator_type alloc;
        test_allocator_type::init_counters();
        CHECK(MyDataCount==0);
        test_table_type table(alloc);
        TraverseTable(table,n,0);
        ParallelTraverseTable(table,n,0);

        for ( int i = 0; i < 2; ++i ) {
            if ( i==0 )
                DoConcurrentOperations<InsertInitList, test_table_type>( table, n, "insert(std::initializer_list)", nthread );
            else
                DoConcurrentOperations<Insert, test_table_type>( table, n, "insert", nthread );
            CHECK(MyDataCount == m);
            TraverseTable( table, n, m );
            ParallelTraverseTable( table, n, m );

            DoConcurrentOperations<Find, test_table_type>( table, n, "find", nthread );
            CHECK(MyDataCount == m);

            DoConcurrentOperations<FindConst, test_table_type>( table, n, "find(const)", nthread );
            CHECK(MyDataCount == m);

            EraseCount = 0;
            DoConcurrentOperations<Erase, test_table_type>( table, n, "erase", nthread );
            CHECK(EraseCount == m);
            CHECK(MyDataCount == 0);
            TraverseTable( table, n, 0 );

            table.clear();
        }

        if( nthread > 1 ) {
            YourTable ie_table;
            for( int i=0; i<IE_SIZE; ++i )
                InsertEraseCount[i] = 0;
            DoConcurrentOperations<InsertErase,YourTable>(ie_table,n/2,"insert_erase",nthread);
            for( int i=0; i<IE_SIZE; ++i )
                CHECK(InsertEraseCount[i]==ie_table.count(MyKey::make(i)));

            DoConcurrentOperations<InnerInsert, YourTable>(ie_table,2000,"inner insert",nthread);
            utils::SpinBarrier barrier(nthread);
            INFO("testing erase on fake exclusive accessor\n");
            utils::NativeParallelFor( nthread, FakeExclusive(barrier, ie_table));
        }
    }
    REQUIRE( test_allocator_type::items_constructed == test_allocator_type::items_destroyed );
    REQUIRE( test_allocator_type::items_allocated == test_allocator_type::items_freed );
    REQUIRE( test_allocator_type::allocations == test_allocator_type::frees );
}

std::atomic<int> Counter;

class AddToTable {
    test_table_type& my_table;
    const std::size_t my_nthread;
    const int my_m;
public:
    AddToTable( test_table_type& table, std::size_t nthread, int m ) : my_table(table), my_nthread(nthread), my_m(m) {}
    void operator()( std::size_t ) const {
        for( int i=0; i<my_m; ++i ) {
            // Busy wait to synchronize threads
            int j = 0;
            while( Counter<i ) {
                if( ++j==1000000 ) {
                    // If Counter<i after a million iterations, then we almost surely have
                    // more logical threads than physical threads, and should yield in
                    // order to let suspended logical threads make progress.
                    j = 0;
                    utils::yield();
                }
            }
            // Now all threads attempt to simultaneously insert a key.
            int k;
            {
                test_table_type::accessor a;
                MyKey key = MyKey::make(i);
                if( my_table.insert( a, key ) )
                    a->second.set_value( 1 );
                else
                    a->second.set_value( a->second.value_of()+1 );
                k = a->second.value_of();
            }
            if( std::size_t(k) == my_nthread )
                Counter=i+1;
        }
    }
};

class RemoveFromTable {
    test_table_type& my_table;
    const int my_m;
public:
    RemoveFromTable( test_table_type& table, int m ) : my_table(table), my_m(m) {}
    void operator()(std::size_t) const {
        for( int i=0; i<my_m; ++i ) {
            bool b;
            if(i&4) {
                if(i&8) {
                    test_table_type::const_accessor a;
                    b = my_table.find( a, MyKey::make(i) ) && my_table.erase( a );
                } else {
                    test_table_type::accessor a;
                    b = my_table.find( a, MyKey::make(i) ) && my_table.erase( a );
                }
            } else
                b = my_table.erase( MyKey::make(i) );
            if( b ) ++EraseCount;
        }
    }
};

void TestConcurrency( std::size_t nthread ) {
    INFO("testing multiple insertions/deletions of same key with " << nthread << " threads");
    test_allocator_type::init_counters();
    {
        CHECK( MyDataCount==0);
        test_table_type table;
        const int m = 1000;
        Counter = 0;
        oneapi::tbb::tick_count t0 = oneapi::tbb::tick_count::now();
        utils::NativeParallelFor( nthread, AddToTable(table,nthread,m) );
        REQUIRE_MESSAGE( MyDataCount==m, "memory leak detected" );

        EraseCount = 0;
        t0 = oneapi::tbb::tick_count::now();
        utils::NativeParallelFor( nthread, RemoveFromTable(table,m) );
        REQUIRE_MESSAGE(MyDataCount==0, "memory leak detected");
        REQUIRE_MESSAGE(EraseCount==m, "return value of erase() is broken");

    }
    REQUIRE( test_allocator_type::items_constructed == test_allocator_type::items_destroyed );
    REQUIRE( test_allocator_type::items_allocated == test_allocator_type::items_freed );
    REQUIRE( test_allocator_type::allocations == test_allocator_type::frees );
    REQUIRE_MESSAGE(MyDataCount==0, "memory leak detected");
}

template<typename Key>
struct non_default_constructible_hash_compare : oneapi::tbb::detail::d1::tbb_hash_compare<Key> {
    non_default_constructible_hash_compare() {
        REQUIRE_MESSAGE(false, "Hash compare object must not default construct during the construction of hash_map with compare argument");
    }

    non_default_constructible_hash_compare(int) {}
};

void TestHashCompareConstructors() {
    using key_type = int;
    using map_type = oneapi::tbb::concurrent_hash_map<key_type, key_type, non_default_constructible_hash_compare<key_type>>;

    non_default_constructible_hash_compare<key_type> compare(0);
    map_type::allocator_type allocator;

    map_type map1(compare);
    map_type map2(compare, allocator);

    map_type map3(1, compare);
    map_type map4(1, compare, allocator);

    std::vector<map_type::value_type> reference_vector;
    map_type map5(reference_vector.begin(), reference_vector.end(), compare);
    map_type map6(reference_vector.begin(), reference_vector.end(), compare, allocator);

    map_type map7({}, compare);
    map_type map8({}, compare, allocator);
}

#if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
template <typename T>
struct debug_hash_compare : public oneapi::tbb::detail::d1::tbb_hash_compare<T> {};

template <template <typename...> typename TMap>
void TestDeductionGuides() {
    using Key = int;
    using Value = std::string;

    using ComplexType = std::pair<Key, Value>;
    using ComplexTypeConst = std::pair<const Key, Value>;

    using DefaultCompare = oneapi::tbb::detail::d1::tbb_hash_compare<Key>;
    using Compare = debug_hash_compare<Key>;
    using DefaultAllocator = oneapi::tbb::tbb_allocator<ComplexTypeConst>;
    using Allocator = std::allocator<ComplexTypeConst>;

    std::vector<ComplexType> v;
    auto l = { ComplexTypeConst(1, "one"), ComplexTypeConst(2, "two") };
    Compare compare;
    Allocator allocator;

    // check TMap(InputIterator, InputIterator)
    TMap m1(v.begin(), v.end());
    static_assert(std::is_same<decltype(m1), TMap<Key, Value, DefaultCompare, DefaultAllocator>>::value);

    // check TMap(InputIterator, InputIterator, HashCompare)
    TMap m2(v.begin(), v.end(), compare);
    static_assert(std::is_same<decltype(m2), TMap<Key, Value, Compare>>::value);

    // check TMap(InputIterator, InputIterator, HashCompare, Allocator)
    TMap m3(v.begin(), v.end(), compare, allocator);
    static_assert(std::is_same<decltype(m3), TMap<Key, Value, Compare, Allocator>>::value);

    // check TMap(InputIterator, InputIterator, Allocator)
    TMap m4(v.begin(), v.end(), allocator);
    static_assert(std::is_same<decltype(m4), TMap<Key, Value, DefaultCompare, Allocator>>::value);

    // check TMap(std::initializer_list)
    TMap m5(l);
    static_assert(std::is_same<decltype(m5), TMap<Key, Value, DefaultCompare, DefaultAllocator>>::value);

    // check TMap(std::initializer_list, HashCompare)
    TMap m6(l, compare);
    static_assert(std::is_same<decltype(m6), TMap<Key, Value, Compare, DefaultAllocator>>::value);

    // check TMap(std::initializer_list, HashCompare, Allocator)
    TMap m7(l, compare, allocator);
    static_assert(std::is_same<decltype(m7), TMap<Key, Value, Compare, Allocator>>::value);

    // check TMap(std::initializer_list, Allocator)
    TMap m8(l, allocator);
    static_assert(std::is_same<decltype(m8), TMap<Key, Value, DefaultCompare, Allocator>>::value);

    // check TMap(TMap &)
    TMap m9(m1);
    static_assert(std::is_same<decltype(m9), decltype(m1)>::value);

    // check TMap(TMap &, Allocator)
    TMap m10(m4, allocator);
    static_assert(std::is_same<decltype(m10), decltype(m4)>::value);

    // check TMap(TMap &&)
    TMap m11(std::move(m1));
    static_assert(std::is_same<decltype(m11), decltype(m1)>::value);

    // check TMap(TMap &&, Allocator)
    TMap m12(std::move(m4), allocator);
    static_assert(std::is_same<decltype(m12), decltype(m4)>::value);
}
#endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT

template <typename CHMapType>
void test_comparisons_basic() {
    using comparisons_testing::testEqualityComparisons;
    CHMapType c1, c2;
    testEqualityComparisons</*ExpectEqual = */true>(c1, c2);

    c1.emplace(1, 1);
    testEqualityComparisons</*ExpectEqual = */false>(c1, c2);

    c2.emplace(1, 1);
    testEqualityComparisons</*ExpectEqual = */true>(c1, c2);
}

template <typename TwoWayComparableContainerType>
void test_two_way_comparable_chmap() {
    TwoWayComparableContainerType c1, c2;
    c1.emplace(1, 1);
    c2.emplace(1, 1);
    comparisons_testing::TwoWayComparable::reset();
    REQUIRE_MESSAGE(c1 == c2, "Incorrect operator == result");
    comparisons_testing::check_equality_comparison();
    REQUIRE_MESSAGE(!(c1 != c2), "Incorrect operator != result");
    comparisons_testing::check_equality_comparison();
}

void TestCHMapComparisons() {
    using integral_container = oneapi::tbb::concurrent_hash_map<int, int>;
    using two_way_comparable_container = oneapi::tbb::concurrent_hash_map<comparisons_testing::TwoWayComparable,
                                                                          comparisons_testing::TwoWayComparable>;

    test_comparisons_basic<integral_container>();
    test_comparisons_basic<two_way_comparable_container>();
    test_two_way_comparable_chmap<two_way_comparable_container>();
}

template <typename Iterator, typename CHMapType>
void TestCHMapIteratorComparisonsBasic( CHMapType& chmap ) {
    REQUIRE_MESSAGE(!chmap.empty(), "Incorrect test setup");
    using namespace comparisons_testing;
    Iterator it1, it2;
    testEqualityComparisons</*ExpectEqual = */true>(it1, it2);
    it1 = chmap.begin();
    testEqualityComparisons</*ExpectEqual = */false>(it1, it2);
    it2 = chmap.begin();
    testEqualityComparisons</*ExpectEqual = */true>(it1, it2);
    it2 = chmap.end();
    testEqualityComparisons</*ExpectEqual = */false>(it1, it2);
}

void TestCHMapIteratorComparisons() {
    using chmap_type = oneapi::tbb::concurrent_hash_map<int, int>;
    using value_type = typename chmap_type::value_type;
    chmap_type chmap = {value_type{1, 1}, value_type{2, 2}, value_type{3, 3}};
    TestCHMapIteratorComparisonsBasic<typename chmap_type::iterator>(chmap);
    const chmap_type& cchmap = chmap;
    TestCHMapIteratorComparisonsBasic<typename chmap_type::const_iterator>(cchmap);
}

template <bool IsConstructible>
class HeterogeneousKey {
public:
    static std::size_t heterogeneous_keys_count;

    int integer_key() const { return my_key; }

    template <bool I = IsConstructible, typename = typename std::enable_if<I>::type>
    HeterogeneousKey(int key) : my_key(key) { ++heterogeneous_keys_count; }

    HeterogeneousKey(const HeterogeneousKey&) = delete;
    HeterogeneousKey& operator=(const HeterogeneousKey&) = delete;

    static void reset() { heterogeneous_keys_count = 0; }

    struct construct_flag {};

    HeterogeneousKey( construct_flag, int key ) : my_key(key) {}

private:
    int my_key;
}; // class HeterogeneousKey

template <bool IsConstructible>
std::size_t HeterogeneousKey<IsConstructible>::heterogeneous_keys_count = 0;

struct HeterogeneousHashCompare {
    using is_transparent = void;

    template <bool IsConstructible>
    std::size_t hash( const HeterogeneousKey<IsConstructible>& key ) const {
        return my_hash_object(key.integer_key());
    }

    std::size_t hash( const int& key ) const {
        return my_hash_object(key);
    }

    bool equal( const int& key1, const int& key2 ) const {
        return key1 == key2;
    }

    template <bool IsConstructible>
    bool equal( const int& key1, const HeterogeneousKey<IsConstructible>& key2 ) const {
        return key1 == key2.integer_key();
    }

    template <bool IsConstructible>
    bool equal( const HeterogeneousKey<IsConstructible>& key1, const int& key2 ) const {
        return key1.integer_key() == key2;
    }

    template <bool IsConstructible>
    bool equal( const HeterogeneousKey<IsConstructible>& key1, const HeterogeneousKey<IsConstructible>& key2 ) const {
        return key1.integer_key() == key2.integer_key();
    }

    std::hash<int> my_hash_object;
}; // struct HeterogeneousHashCompare

class DefaultConstructibleValue {
public:
    DefaultConstructibleValue() : my_i(default_value) {};

    int value() const { return my_i; }
    static constexpr int default_value = -4242;
private:
    int my_i;
}; // class DefaultConstructibleValue

constexpr int DefaultConstructibleValue::default_value;

void test_heterogeneous_find() {
    using key_type = HeterogeneousKey</*IsConstructible = */false>;
    using chmap_type = oneapi::tbb::concurrent_hash_map<key_type, int, HeterogeneousHashCompare>;

    chmap_type chmap;
    using const_accessor = typename chmap_type::const_accessor;
    using accessor = typename chmap_type::accessor;
    const_accessor cacc;
    accessor acc;

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup");

    key_type key(key_type::construct_flag{}, 1);
    bool regular_result = chmap.find(cacc, key);
    bool heterogeneous_result = chmap.find(cacc, int(1));

    REQUIRE(!regular_result);
    REQUIRE_MESSAGE(regular_result == heterogeneous_result,
                    "Incorrect heterogeneous find result with const_accessor (no element)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with const_accessor (no element)");

    regular_result = chmap.find(acc, key);
    heterogeneous_result = chmap.find(acc, int(1));

    REQUIRE(!regular_result);
    REQUIRE_MESSAGE(regular_result == heterogeneous_result,
                    "Incorrect heterogeneous find result with accessor (no element)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with accessor (no element)");

    bool tmp_result = chmap.emplace(cacc, std::piecewise_construct,
                                    std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100));
    REQUIRE(tmp_result);

    regular_result = chmap.find(cacc, key);
    heterogeneous_result = chmap.find(cacc, int(1));

    REQUIRE(regular_result);
    REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with const_accessor (element exists)");
    REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor returned");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with const_accessor (element exists)");
    cacc.release();

    regular_result = chmap.find(acc, key);
    heterogeneous_result = chmap.find(acc, int(1));

    REQUIRE(regular_result);
    REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous find result with accessor (element exists)");
    REQUIRE_MESSAGE(acc->first.integer_key() == 1, "Incorrect accessor returned");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during find call with accessor (element exists)");
    key_type::reset();
}

void test_heterogeneous_count() {
    using key_type = HeterogeneousKey</*IsConstructible = */false>;
    using chmap_type = oneapi::tbb::concurrent_hash_map<key_type, int, HeterogeneousHashCompare>;

    chmap_type chmap;

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup");
    key_type key(key_type::construct_flag{}, 1);

    typename chmap_type::size_type regular_count = chmap.count(key);
    typename chmap_type::size_type heterogeneous_count = chmap.count(int(1));

    REQUIRE(regular_count == 0);
    REQUIRE_MESSAGE(regular_count == heterogeneous_count, "Incorrect heterogeneous count result (no element)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during count call (no element)");

    chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100));

    regular_count = chmap.count(key);
    heterogeneous_count = chmap.count(int(1));

    REQUIRE(regular_count == 1);
    REQUIRE_MESSAGE(regular_count == heterogeneous_count, "Incorrect heterogeneous count result (element exists)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during count call (element exists)");
    key_type::reset();
}

void test_heterogeneous_equal_range() {
    using key_type = HeterogeneousKey</*IsConstructible = */false>;
    using chmap_type = oneapi::tbb::concurrent_hash_map<key_type, int, HeterogeneousHashCompare>;

    chmap_type chmap;
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup");

    using iterator = typename chmap_type::iterator;
    using const_iterator = typename chmap_type::const_iterator;
    using result = std::pair<iterator, iterator>;
    using const_result = std::pair<const_iterator, const_iterator>;
    key_type key(key_type::construct_flag{}, 1);

    result regular_result = chmap.equal_range(key);
    result heterogeneous_result = chmap.equal_range(int(1));

    REQUIRE(regular_result.first == chmap.end());
    REQUIRE(regular_result.second == chmap.end());
    REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous equal_range result (non const, no element)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (non const, no element)");

    const chmap_type& cchmap = chmap;

    const_result regular_const_result = cchmap.equal_range(key);
    const_result heterogeneous_const_result = cchmap.equal_range(int(1));

    REQUIRE(regular_const_result.first == cchmap.end());
    REQUIRE(regular_const_result.second == cchmap.end());
    REQUIRE_MESSAGE(regular_const_result == heterogeneous_const_result,
                    "Incorrect heterogeneous equal_range result (const, no element)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (const, no element)");

    chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100));

    regular_result = chmap.equal_range(key);
    heterogeneous_result = chmap.equal_range(int(1));

    REQUIRE(regular_result.first != chmap.end());
    REQUIRE(regular_result.first->first.integer_key() == 1);
    REQUIRE(regular_result.second == chmap.end());
    REQUIRE_MESSAGE(regular_result == heterogeneous_result, "Incorrect heterogeneous equal_range result (non const, element exists)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (non const, element exists)");

    regular_const_result = cchmap.equal_range(key);
    heterogeneous_const_result = cchmap.equal_range(int(1));
    REQUIRE_MESSAGE(regular_const_result == heterogeneous_const_result,
                    "Incorrect heterogeneous equal_range result (const, element exists)");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Temporary key object was created during equal_range call (const, element exists)");
    key_type::reset();
}

void test_heterogeneous_insert() {
    using key_type = HeterogeneousKey</*IsConstructible = */true>;
    using chmap_type = oneapi::tbb::concurrent_hash_map<key_type, DefaultConstructibleValue, HeterogeneousHashCompare>;

    chmap_type chmap;
    using const_accessor = typename chmap_type::const_accessor;
    using accessor = typename chmap_type::accessor;
    const_accessor cacc;
    accessor acc;

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup");

    bool result = chmap.insert(cacc, int(1));

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 1, "Only one heterogeneous key should be created");
    REQUIRE_MESSAGE(result, "Incorrect heterogeneous insert result (const_accessor)");
    REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor");
    REQUIRE_MESSAGE(cacc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed");

    result = chmap.insert(cacc, int(1));

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 1, "No extra keys should be created");
    REQUIRE_MESSAGE(!result, "Incorrect heterogeneous insert result (const_accessor)");
    REQUIRE_MESSAGE(cacc->first.integer_key() == 1, "Incorrect accessor");
    REQUIRE_MESSAGE(cacc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed");

    result = chmap.insert(acc, int(2));

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 2, "Only one extra heterogeneous key should be created");
    REQUIRE_MESSAGE(result, "Incorrect heterogeneous insert result (accessor)");
    REQUIRE_MESSAGE(acc->first.integer_key() == 2, "Incorrect accessor");
    REQUIRE_MESSAGE(acc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed");

    result = chmap.insert(acc, int(2));

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 2, "No extra keys should be created");
    REQUIRE_MESSAGE(!result, "Incorrect heterogeneous insert result (accessor)");
    REQUIRE_MESSAGE(acc->first.integer_key() == 2, "Incorrect accessor");
    REQUIRE_MESSAGE(acc->second.value() == DefaultConstructibleValue::default_value, "Value should be default constructed");

    key_type::reset();
}

void test_heterogeneous_erase() {
    using key_type = HeterogeneousKey</*IsConstructible = */false>;
    using chmap_type = oneapi::tbb::concurrent_hash_map<key_type, int, HeterogeneousHashCompare>;

    chmap_type chmap;

    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "Incorrect test setup");

    chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 1), std::forward_as_tuple(100));
    chmap.emplace(std::piecewise_construct, std::forward_as_tuple(key_type::construct_flag{}, 2), std::forward_as_tuple(200));

    typename chmap_type::const_accessor cacc;

    REQUIRE(chmap.find(cacc, int(1)));
    REQUIRE(chmap.find(cacc, int(2)));

    cacc.release();

    bool result = chmap.erase(int(1));
    REQUIRE_MESSAGE(result, "Erasure should be successful");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "No extra keys should be created");
    REQUIRE_MESSAGE(!chmap.find(cacc, int(1)), "Element was not erased");


    result = chmap.erase(int(1));
    REQUIRE_MESSAGE(!result, "Erasure should fail");
    REQUIRE_MESSAGE(key_type::heterogeneous_keys_count == 0, "No extra keys should be created");
    key_type::reset();
}

void test_heterogeneous_lookup() {
    test_heterogeneous_find();
    test_heterogeneous_count();
    test_heterogeneous_equal_range();
}

//! Test construction with hash_compare
//! \brief \ref interface \ref requirement
TEST_CASE("testing construction with hash_compare") {
    TestHashCompareConstructors();
}

//! Test concurrent_hash_map member types
//! \brief \ref interface \ref requirement
TEST_CASE("test types"){
    test_member_types<oneapi::tbb::concurrent_hash_map>();
}

//! Test swap and clear operations
//! \brief \ref interface \ref requirement
TEST_CASE("test copy operations") {
    TestCopy();
}

//! Test rehash operation
//! \brief \ref interface \ref requirement
TEST_CASE("test rehash operation") {
    TestRehash();
}

//! Test assignment operation
//! \brief \ref interface \ref requirement
TEST_CASE("test assignment operation") {
    TestAssignment();
}

//! Test iterators and ranges
//! \brief \ref interface \ref requirement
TEST_CASE("test iterators and ranges") {
    TestIteratorsAndRanges();
}

//! Test work with initializer_list
//! \brief \ref interface \ref requirement
TEST_CASE("test work with initializer_list") {
    TestInitList();
}

#if TBB_USE_EXCEPTIONS
//! Test exception safety
//! \brief \ref requirement
TEST_CASE("test exception safety") {
    TestExceptions();
}

//! Test exceptions safety guarantees for move constructor
//! \brief \ref requirement
TEST_CASE("test move support with exceptions") {
    move_support_tests::test_ex_move_ctor_unequal_allocator_memory_failure<hash_map_traits>();
    move_support_tests::test_ex_move_ctor_unequal_allocator_element_ctor_failure<hash_map_traits>();
}
#endif

//! Test move constructor
//! \brief \ref interface \ref requirement
TEST_CASE("testing move constructor"){
    move_support_tests::test_move_constructor<hash_map_traits>();
}

//! Test move assign operator
//! \brief \ref interface \ref requirement
TEST_CASE("testing move assign operator"){
    move_support_tests::test_move_assignment<hash_map_traits>();
}

//! Test insert and empace
//! \brief \ref interface \ref requirement
TEST_CASE("testing concurrent insert and emplace"){
    int n=250000;
    {
        DataStateTrackedTable table;
        DoConcurrentOperations<RvalueInsert, DataStateTrackedTable>( table, n, "rvalue ref insert", 1 );
    }
    {
        DataStateTrackedTable table;
        DoConcurrentOperations<Emplace, DataStateTrackedTable>( table, n, "emplace", 1 );
    }
}

//! Test allocator traits
//! \brief \ref requirement
TEST_CASE("testing allocator traits") {
    test_allocator_traits_support<hash_map_traits>();
}

//! Test concurrent operations
//! \brief \ref requirement
TEST_CASE("testing concurrency"){
    for (std::size_t p = 1; p <= 4; ++p) {
        oneapi::tbb::global_control limit(oneapi::tbb::global_control::max_allowed_parallelism, p);
        TestInsertFindErase(p);
        TestConcurrency(p);
    }
}

#if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
//! Test deduction guides
//! \brief \ref interface
TEST_CASE("testing deduction guides") {
    TestDeductionGuides<oneapi::tbb::concurrent_hash_map>();
}
#endif // __TBB_CPP17_DEDUCTION_GUIDES_PRESENT

//! \brief \ref interface \ref requirement
TEST_CASE("concurrent_hash_map comparisons") {
    TestCHMapComparisons();
}

//! \brief \ref interface \ref requirement
TEST_CASE("concurrent_hash_map iterator comparisons") {
    TestCHMapIteratorComparisons();
}

//! \brief \ref interface \ref requirement
TEST_CASE("test concurrent_hash_map heterogeneous lookup") {
    test_heterogeneous_lookup();
}

//! \brief \ref interface \ref requirement
TEST_CASE("test concurrent_hash_map heterogeneous insert") {
    test_heterogeneous_insert();
}

//! \brief \ref interface \ref requirement
TEST_CASE("test concurrent_hash_map heterogeneous erase") {
    test_heterogeneous_erase();
}