Home | History | Annotate | Download | only in unord.set
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <unordered_set>
     11 
     12 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
     13 //           class Alloc = allocator<Value>>
     14 // class unordered_set
     15 
     16 // void swap(unordered_set& u);
     17 
     18 #include <unordered_set>
     19 #include <cassert>
     20 
     21 #include "../../test_compare.h"
     22 #include "../../test_hash.h"
     23 #include "test_allocator.h"
     24 #include "min_allocator.h"
     25 
     26 int main()
     27 {
     28     {
     29         typedef test_hash<std::hash<int> > Hash;
     30         typedef test_compare<std::equal_to<int> > Compare;
     31         typedef test_allocator<int> Alloc;
     32         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
     33         typedef int P;
     34         C c1(0, Hash(1), Compare(1), Alloc(1));
     35         C c2(0, Hash(2), Compare(2), Alloc(2));
     36         c2.max_load_factor(2);
     37         c1.swap(c2);
     38 
     39         assert(c1.bucket_count() == 0);
     40         assert(c1.size() == 0);
     41         assert(c1.hash_function() == Hash(2));
     42         assert(c1.key_eq() == Compare(2));
     43         assert(c1.get_allocator() == Alloc(1));
     44         assert(std::distance(c1.begin(), c1.end()) == c1.size());
     45         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
     46         assert(c1.max_load_factor() == 2);
     47 
     48         assert(c2.bucket_count() == 0);
     49         assert(c2.size() == 0);
     50         assert(c2.hash_function() == Hash(1));
     51         assert(c2.key_eq() == Compare(1));
     52         assert(c2.get_allocator() == Alloc(2));
     53         assert(std::distance(c2.begin(), c2.end()) == c2.size());
     54         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
     55         assert(c2.max_load_factor() == 1);
     56     }
     57     {
     58         typedef test_hash<std::hash<int> > Hash;
     59         typedef test_compare<std::equal_to<int> > Compare;
     60         typedef test_allocator<int> Alloc;
     61         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
     62         typedef int P;
     63         P a2[] =
     64         {
     65             P(10),
     66             P(20),
     67             P(30),
     68             P(40),
     69             P(50),
     70             P(60),
     71             P(70),
     72             P(80)
     73         };
     74         C c1(0, Hash(1), Compare(1), Alloc(1));
     75         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
     76         c2.max_load_factor(2);
     77         c1.swap(c2);
     78 
     79         assert(c1.bucket_count() >= 11);
     80         assert(c1.size() == 8);
     81         assert(*c1.find(10) == 10);
     82         assert(*c1.find(20) == 20);
     83         assert(*c1.find(30) == 30);
     84         assert(*c1.find(40) == 40);
     85         assert(*c1.find(50) == 50);
     86         assert(*c1.find(60) == 60);
     87         assert(*c1.find(70) == 70);
     88         assert(*c1.find(80) == 80);
     89         assert(c1.hash_function() == Hash(2));
     90         assert(c1.key_eq() == Compare(2));
     91         assert(c1.get_allocator() == Alloc(1));
     92         assert(std::distance(c1.begin(), c1.end()) == c1.size());
     93         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
     94         assert(c1.max_load_factor() == 2);
     95 
     96         assert(c2.bucket_count() == 0);
     97         assert(c2.size() == 0);
     98         assert(c2.hash_function() == Hash(1));
     99         assert(c2.key_eq() == Compare(1));
    100         assert(c2.get_allocator() == Alloc(2));
    101         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    102         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    103         assert(c2.max_load_factor() == 1);
    104     }
    105     {
    106         typedef test_hash<std::hash<int> > Hash;
    107         typedef test_compare<std::equal_to<int> > Compare;
    108         typedef test_allocator<int> Alloc;
    109         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    110         typedef int P;
    111         P a1[] =
    112         {
    113             P(1),
    114             P(2),
    115             P(3),
    116             P(4),
    117             P(1),
    118             P(2)
    119         };
    120         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    121         C c2(0, Hash(2), Compare(2), Alloc(2));
    122         c2.max_load_factor(2);
    123         c1.swap(c2);
    124 
    125         assert(c1.bucket_count() == 0);
    126         assert(c1.size() == 0);
    127         assert(c1.hash_function() == Hash(2));
    128         assert(c1.key_eq() == Compare(2));
    129         assert(c1.get_allocator() == Alloc(1));
    130         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    131         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    132         assert(c1.max_load_factor() == 2);
    133 
    134         assert(c2.bucket_count() >= 5);
    135         assert(c2.size() == 4);
    136         assert(c2.count(1) == 1);
    137         assert(c2.count(2) == 1);
    138         assert(c2.count(3) == 1);
    139         assert(c2.count(4) == 1);
    140         assert(c2.hash_function() == Hash(1));
    141         assert(c2.key_eq() == Compare(1));
    142         assert(c2.get_allocator() == Alloc(2));
    143         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    144         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    145         assert(c2.max_load_factor() == 1);
    146     }
    147     {
    148         typedef test_hash<std::hash<int> > Hash;
    149         typedef test_compare<std::equal_to<int> > Compare;
    150         typedef test_allocator<int> Alloc;
    151         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    152         typedef int P;
    153         P a1[] =
    154         {
    155             P(1),
    156             P(2),
    157             P(3),
    158             P(4),
    159             P(1),
    160             P(2)
    161         };
    162         P a2[] =
    163         {
    164             P(10),
    165             P(20),
    166             P(30),
    167             P(40),
    168             P(50),
    169             P(60),
    170             P(70),
    171             P(80)
    172         };
    173         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    174         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    175         c2.max_load_factor(2);
    176         c1.swap(c2);
    177 
    178         assert(c1.bucket_count() >= 11);
    179         assert(c1.size() == 8);
    180         assert(*c1.find(10) == 10);
    181         assert(*c1.find(20) == 20);
    182         assert(*c1.find(30) == 30);
    183         assert(*c1.find(40) == 40);
    184         assert(*c1.find(50) == 50);
    185         assert(*c1.find(60) == 60);
    186         assert(*c1.find(70) == 70);
    187         assert(*c1.find(80) == 80);
    188         assert(c1.hash_function() == Hash(2));
    189         assert(c1.key_eq() == Compare(2));
    190         assert(c1.get_allocator() == Alloc(1));
    191         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    192         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    193         assert(c1.max_load_factor() == 2);
    194 
    195         assert(c2.bucket_count() >= 5);
    196         assert(c2.size() == 4);
    197         assert(c2.count(1) == 1);
    198         assert(c2.count(2) == 1);
    199         assert(c2.count(3) == 1);
    200         assert(c2.count(4) == 1);
    201         assert(c2.hash_function() == Hash(1));
    202         assert(c2.key_eq() == Compare(1));
    203         assert(c2.get_allocator() == Alloc(2));
    204         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    205         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    206         assert(c2.max_load_factor() == 1);
    207     }
    208 
    209     {
    210         typedef test_hash<std::hash<int> > Hash;
    211         typedef test_compare<std::equal_to<int> > Compare;
    212         typedef other_allocator<int> Alloc;
    213         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    214         typedef int P;
    215         C c1(0, Hash(1), Compare(1), Alloc(1));
    216         C c2(0, Hash(2), Compare(2), Alloc(2));
    217         c2.max_load_factor(2);
    218         c1.swap(c2);
    219 
    220         assert(c1.bucket_count() == 0);
    221         assert(c1.size() == 0);
    222         assert(c1.hash_function() == Hash(2));
    223         assert(c1.key_eq() == Compare(2));
    224         assert(c1.get_allocator() == Alloc(2));
    225         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    226         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    227         assert(c1.max_load_factor() == 2);
    228 
    229         assert(c2.bucket_count() == 0);
    230         assert(c2.size() == 0);
    231         assert(c2.hash_function() == Hash(1));
    232         assert(c2.key_eq() == Compare(1));
    233         assert(c2.get_allocator() == Alloc(1));
    234         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    235         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    236         assert(c2.max_load_factor() == 1);
    237     }
    238     {
    239         typedef test_hash<std::hash<int> > Hash;
    240         typedef test_compare<std::equal_to<int> > Compare;
    241         typedef other_allocator<int> Alloc;
    242         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    243         typedef int P;
    244         P a2[] =
    245         {
    246             P(10),
    247             P(20),
    248             P(30),
    249             P(40),
    250             P(50),
    251             P(60),
    252             P(70),
    253             P(80)
    254         };
    255         C c1(0, Hash(1), Compare(1), Alloc(1));
    256         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    257         c2.max_load_factor(2);
    258         c1.swap(c2);
    259 
    260         assert(c1.bucket_count() >= 11);
    261         assert(c1.size() == 8);
    262         assert(*c1.find(10) == 10);
    263         assert(*c1.find(20) == 20);
    264         assert(*c1.find(30) == 30);
    265         assert(*c1.find(40) == 40);
    266         assert(*c1.find(50) == 50);
    267         assert(*c1.find(60) == 60);
    268         assert(*c1.find(70) == 70);
    269         assert(*c1.find(80) == 80);
    270         assert(c1.hash_function() == Hash(2));
    271         assert(c1.key_eq() == Compare(2));
    272         assert(c1.get_allocator() == Alloc(2));
    273         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    274         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    275         assert(c1.max_load_factor() == 2);
    276 
    277         assert(c2.bucket_count() == 0);
    278         assert(c2.size() == 0);
    279         assert(c2.hash_function() == Hash(1));
    280         assert(c2.key_eq() == Compare(1));
    281         assert(c2.get_allocator() == Alloc(1));
    282         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    283         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    284         assert(c2.max_load_factor() == 1);
    285     }
    286     {
    287         typedef test_hash<std::hash<int> > Hash;
    288         typedef test_compare<std::equal_to<int> > Compare;
    289         typedef other_allocator<int> Alloc;
    290         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    291         typedef int P;
    292         P a1[] =
    293         {
    294             P(1),
    295             P(2),
    296             P(3),
    297             P(4),
    298             P(1),
    299             P(2)
    300         };
    301         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    302         C c2(0, Hash(2), Compare(2), Alloc(2));
    303         c2.max_load_factor(2);
    304         c1.swap(c2);
    305 
    306         assert(c1.bucket_count() == 0);
    307         assert(c1.size() == 0);
    308         assert(c1.hash_function() == Hash(2));
    309         assert(c1.key_eq() == Compare(2));
    310         assert(c1.get_allocator() == Alloc(2));
    311         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    312         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    313         assert(c1.max_load_factor() == 2);
    314 
    315         assert(c2.bucket_count() >= 5);
    316         assert(c2.size() == 4);
    317         assert(c2.count(1) == 1);
    318         assert(c2.count(2) == 1);
    319         assert(c2.count(3) == 1);
    320         assert(c2.count(4) == 1);
    321         assert(c2.hash_function() == Hash(1));
    322         assert(c2.key_eq() == Compare(1));
    323         assert(c2.get_allocator() == Alloc(1));
    324         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    325         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    326         assert(c2.max_load_factor() == 1);
    327     }
    328     {
    329         typedef test_hash<std::hash<int> > Hash;
    330         typedef test_compare<std::equal_to<int> > Compare;
    331         typedef other_allocator<int> Alloc;
    332         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    333         typedef int P;
    334         P a1[] =
    335         {
    336             P(1),
    337             P(2),
    338             P(3),
    339             P(4),
    340             P(1),
    341             P(2)
    342         };
    343         P a2[] =
    344         {
    345             P(10),
    346             P(20),
    347             P(30),
    348             P(40),
    349             P(50),
    350             P(60),
    351             P(70),
    352             P(80)
    353         };
    354         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    355         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    356         c2.max_load_factor(2);
    357         c1.swap(c2);
    358 
    359         assert(c1.bucket_count() >= 11);
    360         assert(c1.size() == 8);
    361         assert(*c1.find(10) == 10);
    362         assert(*c1.find(20) == 20);
    363         assert(*c1.find(30) == 30);
    364         assert(*c1.find(40) == 40);
    365         assert(*c1.find(50) == 50);
    366         assert(*c1.find(60) == 60);
    367         assert(*c1.find(70) == 70);
    368         assert(*c1.find(80) == 80);
    369         assert(c1.hash_function() == Hash(2));
    370         assert(c1.key_eq() == Compare(2));
    371         assert(c1.get_allocator() == Alloc(2));
    372         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    373         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    374         assert(c1.max_load_factor() == 2);
    375 
    376         assert(c2.bucket_count() >= 5);
    377         assert(c2.size() == 4);
    378         assert(c2.count(1) == 1);
    379         assert(c2.count(2) == 1);
    380         assert(c2.count(3) == 1);
    381         assert(c2.count(4) == 1);
    382         assert(c2.hash_function() == Hash(1));
    383         assert(c2.key_eq() == Compare(1));
    384         assert(c2.get_allocator() == Alloc(1));
    385         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    386         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    387         assert(c2.max_load_factor() == 1);
    388     }
    389 #if __cplusplus >= 201103L
    390     {
    391         typedef test_hash<std::hash<int> > Hash;
    392         typedef test_compare<std::equal_to<int> > Compare;
    393         typedef min_allocator<int> Alloc;
    394         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    395         typedef int P;
    396         C c1(0, Hash(1), Compare(1), Alloc());
    397         C c2(0, Hash(2), Compare(2), Alloc());
    398         c2.max_load_factor(2);
    399         c1.swap(c2);
    400 
    401         assert(c1.bucket_count() == 0);
    402         assert(c1.size() == 0);
    403         assert(c1.hash_function() == Hash(2));
    404         assert(c1.key_eq() == Compare(2));
    405         assert(c1.get_allocator() == Alloc());
    406         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    407         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    408         assert(c1.max_load_factor() == 2);
    409 
    410         assert(c2.bucket_count() == 0);
    411         assert(c2.size() == 0);
    412         assert(c2.hash_function() == Hash(1));
    413         assert(c2.key_eq() == Compare(1));
    414         assert(c2.get_allocator() == Alloc());
    415         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    416         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    417         assert(c2.max_load_factor() == 1);
    418     }
    419     {
    420         typedef test_hash<std::hash<int> > Hash;
    421         typedef test_compare<std::equal_to<int> > Compare;
    422         typedef min_allocator<int> Alloc;
    423         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    424         typedef int P;
    425         P a2[] =
    426         {
    427             P(10),
    428             P(20),
    429             P(30),
    430             P(40),
    431             P(50),
    432             P(60),
    433             P(70),
    434             P(80)
    435         };
    436         C c1(0, Hash(1), Compare(1), Alloc());
    437         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
    438         c2.max_load_factor(2);
    439         c1.swap(c2);
    440 
    441         assert(c1.bucket_count() >= 11);
    442         assert(c1.size() == 8);
    443         assert(*c1.find(10) == 10);
    444         assert(*c1.find(20) == 20);
    445         assert(*c1.find(30) == 30);
    446         assert(*c1.find(40) == 40);
    447         assert(*c1.find(50) == 50);
    448         assert(*c1.find(60) == 60);
    449         assert(*c1.find(70) == 70);
    450         assert(*c1.find(80) == 80);
    451         assert(c1.hash_function() == Hash(2));
    452         assert(c1.key_eq() == Compare(2));
    453         assert(c1.get_allocator() == Alloc());
    454         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    455         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    456         assert(c1.max_load_factor() == 2);
    457 
    458         assert(c2.bucket_count() == 0);
    459         assert(c2.size() == 0);
    460         assert(c2.hash_function() == Hash(1));
    461         assert(c2.key_eq() == Compare(1));
    462         assert(c2.get_allocator() == Alloc());
    463         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    464         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    465         assert(c2.max_load_factor() == 1);
    466     }
    467     {
    468         typedef test_hash<std::hash<int> > Hash;
    469         typedef test_compare<std::equal_to<int> > Compare;
    470         typedef min_allocator<int> Alloc;
    471         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    472         typedef int P;
    473         P a1[] =
    474         {
    475             P(1),
    476             P(2),
    477             P(3),
    478             P(4),
    479             P(1),
    480             P(2)
    481         };
    482         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
    483         C c2(0, Hash(2), Compare(2), Alloc());
    484         c2.max_load_factor(2);
    485         c1.swap(c2);
    486 
    487         assert(c1.bucket_count() == 0);
    488         assert(c1.size() == 0);
    489         assert(c1.hash_function() == Hash(2));
    490         assert(c1.key_eq() == Compare(2));
    491         assert(c1.get_allocator() == Alloc());
    492         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    493         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    494         assert(c1.max_load_factor() == 2);
    495 
    496         assert(c2.bucket_count() >= 5);
    497         assert(c2.size() == 4);
    498         assert(c2.count(1) == 1);
    499         assert(c2.count(2) == 1);
    500         assert(c2.count(3) == 1);
    501         assert(c2.count(4) == 1);
    502         assert(c2.hash_function() == Hash(1));
    503         assert(c2.key_eq() == Compare(1));
    504         assert(c2.get_allocator() == Alloc());
    505         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    506         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    507         assert(c2.max_load_factor() == 1);
    508     }
    509     {
    510         typedef test_hash<std::hash<int> > Hash;
    511         typedef test_compare<std::equal_to<int> > Compare;
    512         typedef min_allocator<int> Alloc;
    513         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    514         typedef int P;
    515         P a1[] =
    516         {
    517             P(1),
    518             P(2),
    519             P(3),
    520             P(4),
    521             P(1),
    522             P(2)
    523         };
    524         P a2[] =
    525         {
    526             P(10),
    527             P(20),
    528             P(30),
    529             P(40),
    530             P(50),
    531             P(60),
    532             P(70),
    533             P(80)
    534         };
    535         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
    536         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
    537         c2.max_load_factor(2);
    538         c1.swap(c2);
    539 
    540         assert(c1.bucket_count() >= 11);
    541         assert(c1.size() == 8);
    542         assert(*c1.find(10) == 10);
    543         assert(*c1.find(20) == 20);
    544         assert(*c1.find(30) == 30);
    545         assert(*c1.find(40) == 40);
    546         assert(*c1.find(50) == 50);
    547         assert(*c1.find(60) == 60);
    548         assert(*c1.find(70) == 70);
    549         assert(*c1.find(80) == 80);
    550         assert(c1.hash_function() == Hash(2));
    551         assert(c1.key_eq() == Compare(2));
    552         assert(c1.get_allocator() == Alloc());
    553         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    554         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    555         assert(c1.max_load_factor() == 2);
    556 
    557         assert(c2.bucket_count() >= 5);
    558         assert(c2.size() == 4);
    559         assert(c2.count(1) == 1);
    560         assert(c2.count(2) == 1);
    561         assert(c2.count(3) == 1);
    562         assert(c2.count(4) == 1);
    563         assert(c2.hash_function() == Hash(1));
    564         assert(c2.key_eq() == Compare(1));
    565         assert(c2.get_allocator() == Alloc());
    566         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    567         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    568         assert(c2.max_load_factor() == 1);
    569     }
    570 #endif
    571 }
    572