libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

move_alloc.pass.cpp (5174B)


      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 // UNSUPPORTED: c++98, c++03
     11 
     12 // <unordered_set>
     13 
     14 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
     15 //           class Alloc = allocator<Value>>
     16 // class unordered_set
     17 
     18 // unordered_set(unordered_set&& u, const allocator_type& a);
     19 
     20 #include <unordered_set>
     21 #include <cassert>
     22 #include <cfloat>
     23 #include <cmath>
     24 #include <cstddef>
     25 
     26 #include "../../../test_compare.h"
     27 #include "../../../test_hash.h"
     28 #include "test_allocator.h"
     29 #include "min_allocator.h"
     30 
     31 int main()
     32 {
     33     {
     34         typedef int P;
     35         typedef test_allocator<int> A;
     36         typedef std::unordered_set<int,
     37                                    test_hash<std::hash<int> >,
     38                                    test_compare<std::equal_to<int> >,
     39                                    A
     40                                    > C;
     41         P a[] =
     42         {
     43             P(1),
     44             P(2),
     45             P(3),
     46             P(4),
     47             P(1),
     48             P(2)
     49         };
     50         C c0(a, a + sizeof(a)/sizeof(a[0]),
     51             7,
     52             test_hash<std::hash<int> >(8),
     53             test_compare<std::equal_to<int> >(9),
     54             A(10)
     55            );
     56         C c(std::move(c0), A(12));
     57         assert(c.bucket_count() >= 5);
     58         assert(c.size() == 4);
     59         assert(c.count(1) == 1);
     60         assert(c.count(2) == 1);
     61         assert(c.count(3) == 1);
     62         assert(c.count(4) == 1);
     63         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     64         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     65         assert(c.get_allocator() == A(12));
     66         assert(!c.empty());
     67         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     68         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     69         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     70         assert(c.max_load_factor() == 1);
     71 
     72         assert(c0.empty());
     73     }
     74     {
     75         typedef int P;
     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         P a[] =
     83         {
     84             P(1),
     85             P(2),
     86             P(3),
     87             P(4),
     88             P(1),
     89             P(2)
     90         };
     91         C c0(a, a + sizeof(a)/sizeof(a[0]),
     92             7,
     93             test_hash<std::hash<int> >(8),
     94             test_compare<std::equal_to<int> >(9),
     95             A(10)
     96            );
     97         C c(std::move(c0), A(10));
     98         assert(c.bucket_count() >= 5);
     99         assert(c.size() == 4);
    100         assert(c.count(1) == 1);
    101         assert(c.count(2) == 1);
    102         assert(c.count(3) == 1);
    103         assert(c.count(4) == 1);
    104         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    105         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    106         assert(c.get_allocator() == A(10));
    107         assert(!c.empty());
    108         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    109         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    110         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    111         assert(c.max_load_factor() == 1);
    112 
    113         assert(c0.empty());
    114     }
    115     {
    116         typedef int P;
    117         typedef min_allocator<int> A;
    118         typedef std::unordered_set<int,
    119                                    test_hash<std::hash<int> >,
    120                                    test_compare<std::equal_to<int> >,
    121                                    A
    122                                    > C;
    123         P a[] =
    124         {
    125             P(1),
    126             P(2),
    127             P(3),
    128             P(4),
    129             P(1),
    130             P(2)
    131         };
    132         C c0(a, a + sizeof(a)/sizeof(a[0]),
    133             7,
    134             test_hash<std::hash<int> >(8),
    135             test_compare<std::equal_to<int> >(9),
    136             A()
    137            );
    138         C c(std::move(c0), A());
    139         assert(c.bucket_count() >= 5);
    140         assert(c.size() == 4);
    141         assert(c.count(1) == 1);
    142         assert(c.count(2) == 1);
    143         assert(c.count(3) == 1);
    144         assert(c.count(4) == 1);
    145         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    146         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    147         assert(c.get_allocator() == A());
    148         assert(!c.empty());
    149         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    150         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    151         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    152         assert(c.max_load_factor() == 1);
    153 
    154         assert(c0.empty());
    155     }
    156 }