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