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(unordered_set&& u, const allocator_type& a);
     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 int P;
     32         typedef test_allocator<int> A;
     33         typedef std::unordered_set<int,
     34                                    test_hash<std::hash<int> >,
     35                                    test_compare<std::equal_to<int> >,
     36                                    A
     37                                    > C;
     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(std::move(c0), A(12));
     54         assert(c.bucket_count() >= 5);
     55         assert(c.size() == 4);
     56         assert(c.count(1) == 1);
     57         assert(c.count(2) == 1);
     58         assert(c.count(3) == 1);
     59         assert(c.count(4) == 1);
     60         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     61         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     62         assert(c.get_allocator() == A(12));
     63         assert(!c.empty());
     64         assert(std::distance(c.begin(), c.end()) == c.size());
     65         assert(std::distance(c.cbegin(), c.cend()) == c.size());
     66         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     67         assert(c.max_load_factor() == 1);
     68 
     69         assert(c0.empty());
     70     }
     71     {
     72         typedef int P;
     73         typedef test_allocator<int> A;
     74         typedef std::unordered_set<int,
     75                                    test_hash<std::hash<int> >,
     76                                    test_compare<std::equal_to<int> >,
     77                                    A
     78                                    > C;
     79         P a[] =
     80         {
     81             P(1),
     82             P(2),
     83             P(3),
     84             P(4),
     85             P(1),
     86             P(2)
     87         };
     88         C c0(a, a + sizeof(a)/sizeof(a[0]),
     89             7,
     90             test_hash<std::hash<int> >(8),
     91             test_compare<std::equal_to<int> >(9),
     92             A(10)
     93            );
     94         C c(std::move(c0), A(10));
     95         assert(c.bucket_count() >= 5);
     96         assert(c.size() == 4);
     97         assert(c.count(1) == 1);
     98         assert(c.count(2) == 1);
     99         assert(c.count(3) == 1);
    100         assert(c.count(4) == 1);
    101         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    102         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    103         assert(c.get_allocator() == A(10));
    104         assert(!c.empty());
    105         assert(std::distance(c.begin(), c.end()) == c.size());
    106         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    107         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    108         assert(c.max_load_factor() == 1);
    109 
    110         assert(c0.empty());
    111     }
    112 #if __cplusplus >= 201103L
    113     {
    114         typedef int P;
    115         typedef min_allocator<int> A;
    116         typedef std::unordered_set<int,
    117                                    test_hash<std::hash<int> >,
    118                                    test_compare<std::equal_to<int> >,
    119                                    A
    120                                    > C;
    121         P a[] =
    122         {
    123             P(1),
    124             P(2),
    125             P(3),
    126             P(4),
    127             P(1),
    128             P(2)
    129         };
    130         C c0(a, a + sizeof(a)/sizeof(a[0]),
    131             7,
    132             test_hash<std::hash<int> >(8),
    133             test_compare<std::equal_to<int> >(9),
    134             A()
    135            );
    136         C c(std::move(c0), A());
    137         assert(c.bucket_count() >= 5);
    138         assert(c.size() == 4);
    139         assert(c.count(1) == 1);
    140         assert(c.count(2) == 1);
    141         assert(c.count(3) == 1);
    142         assert(c.count(4) == 1);
    143         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    144         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    145         assert(c.get_allocator() == A());
    146         assert(!c.empty());
    147         assert(std::distance(c.begin(), c.end()) == c.size());
    148         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    149         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    150         assert(c.max_load_factor() == 1);
    151 
    152         assert(c0.empty());
    153     }
    154 #endif
    155 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    156 }
    157