Home | History | Annotate | Download | only in unord.set.cnstr
      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 // unordered_set& operator=(unordered_set&& u);
     17 
     18 #include <unordered_set>
     19 #include <cassert>
     20 #include <cfloat>
     21 
     22 #include "../../../test_compare.h"
     23 #include "../../../test_hash.h"
     24 #include "test_allocator.h"
     25 #include "min_allocator.h"
     26 
     27 int main()
     28 {
     29 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     30     {
     31         typedef test_allocator<int> A;
     32         typedef std::unordered_set<int,
     33                                    test_hash<std::hash<int> >,
     34                                    test_compare<std::equal_to<int> >,
     35                                    A
     36                                    > C;
     37         typedef int P;
     38         P a[] =
     39         {
     40             P(1),
     41             P(2),
     42             P(3),
     43             P(4),
     44             P(1),
     45             P(2)
     46         };
     47         C c0(a, a + sizeof(a)/sizeof(a[0]),
     48             7,
     49             test_hash<std::hash<int> >(8),
     50             test_compare<std::equal_to<int> >(9),
     51             A(10)
     52            );
     53         C c(a, a + 2,
     54             7,
     55             test_hash<std::hash<int> >(2),
     56             test_compare<std::equal_to<int> >(3),
     57             A(4)
     58            );
     59         c = std::move(c0);
     60         assert(c.bucket_count() == 7);
     61         assert(c.size() == 4);
     62         assert(c.count(1) == 1);
     63         assert(c.count(2) == 1);
     64         assert(c.count(3) == 1);
     65         assert(c.count(4) == 1);
     66         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     67         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     68         assert(c.get_allocator() == A(4));
     69         assert(!c.empty());
     70         assert(std::distance(c.begin(), c.end()) == c.size());
     71         assert(std::distance(c.cbegin(), c.cend()) == c.size());
     72         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     73         assert(c.max_load_factor() == 1);
     74     }
     75     {
     76         typedef test_allocator<int> A;
     77         typedef std::unordered_set<int,
     78                                    test_hash<std::hash<int> >,
     79                                    test_compare<std::equal_to<int> >,
     80                                    A
     81                                    > C;
     82         typedef int P;
     83         P a[] =
     84         {
     85             P(1),
     86             P(2),
     87             P(3),
     88             P(4),
     89             P(1),
     90             P(2)
     91         };
     92         C c0(a, a + sizeof(a)/sizeof(a[0]),
     93             7,
     94             test_hash<std::hash<int> >(8),
     95             test_compare<std::equal_to<int> >(9),
     96             A(10)
     97            );
     98         C c(a, a + 2,
     99             7,
    100             test_hash<std::hash<int> >(2),
    101             test_compare<std::equal_to<int> >(3),
    102             A(10)
    103            );
    104         c = std::move(c0);
    105         assert(c.bucket_count() == 7);
    106         assert(c.size() == 4);
    107         assert(c.count(1) == 1);
    108         assert(c.count(2) == 1);
    109         assert(c.count(3) == 1);
    110         assert(c.count(4) == 1);
    111         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    112         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    113         assert(c.get_allocator() == A(10));
    114         assert(!c.empty());
    115         assert(std::distance(c.begin(), c.end()) == c.size());
    116         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    117         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    118         assert(c.max_load_factor() == 1);
    119     }
    120     {
    121         typedef other_allocator<int> A;
    122         typedef std::unordered_set<int,
    123                                    test_hash<std::hash<int> >,
    124                                    test_compare<std::equal_to<int> >,
    125                                    A
    126                                    > C;
    127         typedef int P;
    128         P a[] =
    129         {
    130             P(1),
    131             P(2),
    132             P(3),
    133             P(4),
    134             P(1),
    135             P(2)
    136         };
    137         C c0(a, a + sizeof(a)/sizeof(a[0]),
    138             7,
    139             test_hash<std::hash<int> >(8),
    140             test_compare<std::equal_to<int> >(9),
    141             A(10)
    142            );
    143         C c(a, a + 2,
    144             7,
    145             test_hash<std::hash<int> >(2),
    146             test_compare<std::equal_to<int> >(3),
    147             A(4)
    148            );
    149         c = std::move(c0);
    150         assert(c.bucket_count() == 7);
    151         assert(c.size() == 4);
    152         assert(c.count(1) == 1);
    153         assert(c.count(2) == 1);
    154         assert(c.count(3) == 1);
    155         assert(c.count(4) == 1);
    156         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    157         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    158         assert(c.get_allocator() == A(10));
    159         assert(!c.empty());
    160         assert(std::distance(c.begin(), c.end()) == c.size());
    161         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    162         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    163         assert(c.max_load_factor() == 1);
    164     }
    165 #if __cplusplus >= 201103L
    166     {
    167         typedef min_allocator<int> A;
    168         typedef std::unordered_set<int,
    169                                    test_hash<std::hash<int> >,
    170                                    test_compare<std::equal_to<int> >,
    171                                    A
    172                                    > C;
    173         typedef int P;
    174         P a[] =
    175         {
    176             P(1),
    177             P(2),
    178             P(3),
    179             P(4),
    180             P(1),
    181             P(2)
    182         };
    183         C c0(a, a + sizeof(a)/sizeof(a[0]),
    184             7,
    185             test_hash<std::hash<int> >(8),
    186             test_compare<std::equal_to<int> >(9),
    187             A()
    188            );
    189         C c(a, a + 2,
    190             7,
    191             test_hash<std::hash<int> >(2),
    192             test_compare<std::equal_to<int> >(3),
    193             A()
    194            );
    195         c = std::move(c0);
    196         assert(c.bucket_count() == 7);
    197         assert(c.size() == 4);
    198         assert(c.count(1) == 1);
    199         assert(c.count(2) == 1);
    200         assert(c.count(3) == 1);
    201         assert(c.count(4) == 1);
    202         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    203         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    204         assert(c.get_allocator() == A());
    205         assert(!c.empty());
    206         assert(std::distance(c.begin(), c.end()) == c.size());
    207         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    208         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    209         assert(c.max_load_factor() == 1);
    210     }
    211 #endif
    212 #if _LIBCPP_DEBUG >= 1
    213     {
    214         std::unordered_set<int> s1 = {1, 2, 3};
    215         std::unordered_set<int>::iterator i = s1.begin();
    216         int k = *i;
    217         std::unordered_set<int> s2;
    218         s2 = std::move(s1);
    219         assert(*i == k);
    220         s2.erase(i);
    221         assert(s2.size() == 2);
    222     }
    223 #endif
    224 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    225 }
    226