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