Home | History | Annotate | Download | only in unord.set.swap
      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& x, unordered_set& y);
     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 
     25 int main()
     26 {
     27     {
     28         typedef test_hash<std::hash<int> > Hash;
     29         typedef test_compare<std::equal_to<int> > Compare;
     30         typedef test_allocator<int> Alloc;
     31         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
     32         typedef int P;
     33         C c1(0, Hash(1), Compare(1), Alloc(1));
     34         C c2(0, Hash(2), Compare(2), Alloc(2));
     35         c2.max_load_factor(2);
     36         swap(c1, c2);
     37 
     38         assert(c1.bucket_count() == 0);
     39         assert(c1.size() == 0);
     40         assert(c1.hash_function() == Hash(2));
     41         assert(c1.key_eq() == Compare(2));
     42         assert(c1.get_allocator() == Alloc(1));
     43         assert(std::distance(c1.begin(), c1.end()) == c1.size());
     44         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
     45         assert(c1.max_load_factor() == 2);
     46 
     47         assert(c2.bucket_count() == 0);
     48         assert(c2.size() == 0);
     49         assert(c2.hash_function() == Hash(1));
     50         assert(c2.key_eq() == Compare(1));
     51         assert(c2.get_allocator() == Alloc(2));
     52         assert(std::distance(c2.begin(), c2.end()) == c2.size());
     53         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
     54         assert(c2.max_load_factor() == 1);
     55     }
     56     {
     57         typedef test_hash<std::hash<int> > Hash;
     58         typedef test_compare<std::equal_to<int> > Compare;
     59         typedef test_allocator<int> Alloc;
     60         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
     61         typedef int P;
     62         P a2[] =
     63         {
     64             P(10),
     65             P(20),
     66             P(30),
     67             P(40),
     68             P(50),
     69             P(60),
     70             P(70),
     71             P(80)
     72         };
     73         C c1(0, Hash(1), Compare(1), Alloc(1));
     74         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
     75         c2.max_load_factor(2);
     76         swap(c1, c2);
     77 
     78         assert(c1.bucket_count() >= 11);
     79         assert(c1.size() == 8);
     80         assert(*c1.find(10) == 10);
     81         assert(*c1.find(20) == 20);
     82         assert(*c1.find(30) == 30);
     83         assert(*c1.find(40) == 40);
     84         assert(*c1.find(50) == 50);
     85         assert(*c1.find(60) == 60);
     86         assert(*c1.find(70) == 70);
     87         assert(*c1.find(80) == 80);
     88         assert(c1.hash_function() == Hash(2));
     89         assert(c1.key_eq() == Compare(2));
     90         assert(c1.get_allocator() == Alloc(1));
     91         assert(std::distance(c1.begin(), c1.end()) == c1.size());
     92         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
     93         assert(c1.max_load_factor() == 2);
     94 
     95         assert(c2.bucket_count() == 0);
     96         assert(c2.size() == 0);
     97         assert(c2.hash_function() == Hash(1));
     98         assert(c2.key_eq() == Compare(1));
     99         assert(c2.get_allocator() == Alloc(2));
    100         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    101         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    102         assert(c2.max_load_factor() == 1);
    103     }
    104     {
    105         typedef test_hash<std::hash<int> > Hash;
    106         typedef test_compare<std::equal_to<int> > Compare;
    107         typedef test_allocator<int> Alloc;
    108         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    109         typedef int P;
    110         P a1[] =
    111         {
    112             P(1),
    113             P(2),
    114             P(3),
    115             P(4),
    116             P(1),
    117             P(2)
    118         };
    119         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    120         C c2(0, Hash(2), Compare(2), Alloc(2));
    121         c2.max_load_factor(2);
    122         swap(c1, c2);
    123 
    124         assert(c1.bucket_count() == 0);
    125         assert(c1.size() == 0);
    126         assert(c1.hash_function() == Hash(2));
    127         assert(c1.key_eq() == Compare(2));
    128         assert(c1.get_allocator() == Alloc(1));
    129         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    130         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    131         assert(c1.max_load_factor() == 2);
    132 
    133         assert(c2.bucket_count() >= 5);
    134         assert(c2.size() == 4);
    135         assert(c2.count(1) == 1);
    136         assert(c2.count(2) == 1);
    137         assert(c2.count(3) == 1);
    138         assert(c2.count(4) == 1);
    139         assert(c2.hash_function() == Hash(1));
    140         assert(c2.key_eq() == Compare(1));
    141         assert(c2.get_allocator() == Alloc(2));
    142         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    143         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    144         assert(c2.max_load_factor() == 1);
    145     }
    146     {
    147         typedef test_hash<std::hash<int> > Hash;
    148         typedef test_compare<std::equal_to<int> > Compare;
    149         typedef test_allocator<int> Alloc;
    150         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    151         typedef int P;
    152         P a1[] =
    153         {
    154             P(1),
    155             P(2),
    156             P(3),
    157             P(4),
    158             P(1),
    159             P(2)
    160         };
    161         P a2[] =
    162         {
    163             P(10),
    164             P(20),
    165             P(30),
    166             P(40),
    167             P(50),
    168             P(60),
    169             P(70),
    170             P(80)
    171         };
    172         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    173         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    174         c2.max_load_factor(2);
    175         swap(c1, c2);
    176 
    177         assert(c1.bucket_count() >= 11);
    178         assert(c1.size() == 8);
    179         assert(*c1.find(10) == 10);
    180         assert(*c1.find(20) == 20);
    181         assert(*c1.find(30) == 30);
    182         assert(*c1.find(40) == 40);
    183         assert(*c1.find(50) == 50);
    184         assert(*c1.find(60) == 60);
    185         assert(*c1.find(70) == 70);
    186         assert(*c1.find(80) == 80);
    187         assert(c1.hash_function() == Hash(2));
    188         assert(c1.key_eq() == Compare(2));
    189         assert(c1.get_allocator() == Alloc(1));
    190         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    191         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    192         assert(c1.max_load_factor() == 2);
    193 
    194         assert(c2.bucket_count() >= 5);
    195         assert(c2.size() == 4);
    196         assert(c2.count(1) == 1);
    197         assert(c2.count(2) == 1);
    198         assert(c2.count(3) == 1);
    199         assert(c2.count(4) == 1);
    200         assert(c2.hash_function() == Hash(1));
    201         assert(c2.key_eq() == Compare(1));
    202         assert(c2.get_allocator() == Alloc(2));
    203         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    204         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    205         assert(c2.max_load_factor() == 1);
    206     }
    207 
    208     {
    209         typedef test_hash<std::hash<int> > Hash;
    210         typedef test_compare<std::equal_to<int> > Compare;
    211         typedef other_allocator<int> Alloc;
    212         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    213         typedef int P;
    214         C c1(0, Hash(1), Compare(1), Alloc(1));
    215         C c2(0, Hash(2), Compare(2), Alloc(2));
    216         c2.max_load_factor(2);
    217         swap(c1, c2);
    218 
    219         assert(c1.bucket_count() == 0);
    220         assert(c1.size() == 0);
    221         assert(c1.hash_function() == Hash(2));
    222         assert(c1.key_eq() == Compare(2));
    223         assert(c1.get_allocator() == Alloc(2));
    224         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    225         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    226         assert(c1.max_load_factor() == 2);
    227 
    228         assert(c2.bucket_count() == 0);
    229         assert(c2.size() == 0);
    230         assert(c2.hash_function() == Hash(1));
    231         assert(c2.key_eq() == Compare(1));
    232         assert(c2.get_allocator() == Alloc(1));
    233         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    234         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    235         assert(c2.max_load_factor() == 1);
    236     }
    237     {
    238         typedef test_hash<std::hash<int> > Hash;
    239         typedef test_compare<std::equal_to<int> > Compare;
    240         typedef other_allocator<int> Alloc;
    241         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    242         typedef int P;
    243         P a2[] =
    244         {
    245             P(10),
    246             P(20),
    247             P(30),
    248             P(40),
    249             P(50),
    250             P(60),
    251             P(70),
    252             P(80)
    253         };
    254         C c1(0, Hash(1), Compare(1), Alloc(1));
    255         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    256         c2.max_load_factor(2);
    257         swap(c1, c2);
    258 
    259         assert(c1.bucket_count() >= 11);
    260         assert(c1.size() == 8);
    261         assert(*c1.find(10) == 10);
    262         assert(*c1.find(20) == 20);
    263         assert(*c1.find(30) == 30);
    264         assert(*c1.find(40) == 40);
    265         assert(*c1.find(50) == 50);
    266         assert(*c1.find(60) == 60);
    267         assert(*c1.find(70) == 70);
    268         assert(*c1.find(80) == 80);
    269         assert(c1.hash_function() == Hash(2));
    270         assert(c1.key_eq() == Compare(2));
    271         assert(c1.get_allocator() == Alloc(2));
    272         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    273         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    274         assert(c1.max_load_factor() == 2);
    275 
    276         assert(c2.bucket_count() == 0);
    277         assert(c2.size() == 0);
    278         assert(c2.hash_function() == Hash(1));
    279         assert(c2.key_eq() == Compare(1));
    280         assert(c2.get_allocator() == Alloc(1));
    281         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    282         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    283         assert(c2.max_load_factor() == 1);
    284     }
    285     {
    286         typedef test_hash<std::hash<int> > Hash;
    287         typedef test_compare<std::equal_to<int> > Compare;
    288         typedef other_allocator<int> Alloc;
    289         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    290         typedef int P;
    291         P a1[] =
    292         {
    293             P(1),
    294             P(2),
    295             P(3),
    296             P(4),
    297             P(1),
    298             P(2)
    299         };
    300         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    301         C c2(0, Hash(2), Compare(2), Alloc(2));
    302         c2.max_load_factor(2);
    303         swap(c1, c2);
    304 
    305         assert(c1.bucket_count() == 0);
    306         assert(c1.size() == 0);
    307         assert(c1.hash_function() == Hash(2));
    308         assert(c1.key_eq() == Compare(2));
    309         assert(c1.get_allocator() == Alloc(2));
    310         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    311         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    312         assert(c1.max_load_factor() == 2);
    313 
    314         assert(c2.bucket_count() >= 5);
    315         assert(c2.size() == 4);
    316         assert(c2.count(1) == 1);
    317         assert(c2.count(2) == 1);
    318         assert(c2.count(3) == 1);
    319         assert(c2.count(4) == 1);
    320         assert(c2.hash_function() == Hash(1));
    321         assert(c2.key_eq() == Compare(1));
    322         assert(c2.get_allocator() == Alloc(1));
    323         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    324         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    325         assert(c2.max_load_factor() == 1);
    326     }
    327     {
    328         typedef test_hash<std::hash<int> > Hash;
    329         typedef test_compare<std::equal_to<int> > Compare;
    330         typedef other_allocator<int> Alloc;
    331         typedef std::unordered_set<int, Hash, Compare, Alloc> C;
    332         typedef int P;
    333         P a1[] =
    334         {
    335             P(1),
    336             P(2),
    337             P(3),
    338             P(4),
    339             P(1),
    340             P(2)
    341         };
    342         P a2[] =
    343         {
    344             P(10),
    345             P(20),
    346             P(30),
    347             P(40),
    348             P(50),
    349             P(60),
    350             P(70),
    351             P(80)
    352         };
    353         C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
    354         C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
    355         c2.max_load_factor(2);
    356         swap(c1, c2);
    357 
    358         assert(c1.bucket_count() >= 11);
    359         assert(c1.size() == 8);
    360         assert(*c1.find(10) == 10);
    361         assert(*c1.find(20) == 20);
    362         assert(*c1.find(30) == 30);
    363         assert(*c1.find(40) == 40);
    364         assert(*c1.find(50) == 50);
    365         assert(*c1.find(60) == 60);
    366         assert(*c1.find(70) == 70);
    367         assert(*c1.find(80) == 80);
    368         assert(c1.hash_function() == Hash(2));
    369         assert(c1.key_eq() == Compare(2));
    370         assert(c1.get_allocator() == Alloc(2));
    371         assert(std::distance(c1.begin(), c1.end()) == c1.size());
    372         assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
    373         assert(c1.max_load_factor() == 2);
    374 
    375         assert(c2.bucket_count() >= 5);
    376         assert(c2.size() == 4);
    377         assert(c2.count(1) == 1);
    378         assert(c2.count(2) == 1);
    379         assert(c2.count(3) == 1);
    380         assert(c2.count(4) == 1);
    381         assert(c2.hash_function() == Hash(1));
    382         assert(c2.key_eq() == Compare(1));
    383         assert(c2.get_allocator() == Alloc(1));
    384         assert(std::distance(c2.begin(), c2.end()) == c2.size());
    385         assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
    386         assert(c2.max_load_factor() == 1);
    387     }
    388 }
    389