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