libcxx

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

init.pass.cpp (5596B)


      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(initializer_list<value_type> il);
     19 
     20 #include <unordered_set>
     21 #include <cassert>
     22 #include <cfloat>
     23 #include <cmath>
     24 #include <cstddef>
     25 
     26 #include "test_macros.h"
     27 #include "../../../test_compare.h"
     28 #include "../../../test_hash.h"
     29 #include "test_allocator.h"
     30 #include "min_allocator.h"
     31 
     32 int main()
     33 {
     34   {
     35         typedef std::unordered_set<int,
     36                                    test_hash<std::hash<int> >,
     37                                    test_compare<std::equal_to<int> >,
     38                                    test_allocator<int>
     39                                    > C;
     40         typedef int P;
     41         C c = {
     42                 P(1),
     43                 P(2),
     44                 P(3),
     45                 P(4),
     46                 P(1),
     47                 P(2)
     48             };
     49         assert(c.bucket_count() >= 5);
     50         assert(c.size() == 4);
     51         assert(c.count(1) == 1);
     52         assert(c.count(2) == 1);
     53         assert(c.count(3) == 1);
     54         assert(c.count(4) == 1);
     55         assert(c.hash_function() == test_hash<std::hash<int> >());
     56         assert(c.key_eq() == test_compare<std::equal_to<int> >());
     57         assert(c.get_allocator() == test_allocator<int>());
     58         assert(!c.empty());
     59         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     60         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     61         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     62         assert(c.max_load_factor() == 1);
     63     }
     64     {
     65         typedef std::unordered_set<int,
     66                                    test_hash<std::hash<int> >,
     67                                    test_compare<std::equal_to<int> >,
     68                                    min_allocator<int>
     69                                    > C;
     70         typedef int P;
     71         C c = {
     72                 P(1),
     73                 P(2),
     74                 P(3),
     75                 P(4),
     76                 P(1),
     77                 P(2)
     78             };
     79         assert(c.bucket_count() >= 5);
     80         assert(c.size() == 4);
     81         assert(c.count(1) == 1);
     82         assert(c.count(2) == 1);
     83         assert(c.count(3) == 1);
     84         assert(c.count(4) == 1);
     85         assert(c.hash_function() == test_hash<std::hash<int> >());
     86         assert(c.key_eq() == test_compare<std::equal_to<int> >());
     87         assert(c.get_allocator() == min_allocator<int>());
     88         assert(!c.empty());
     89         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     90         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     91         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     92         assert(c.max_load_factor() == 1);
     93     }
     94 #if TEST_STD_VER > 11
     95     {
     96         typedef int T;
     97         typedef test_hash<std::hash<T>> HF;
     98         typedef test_compare<std::equal_to<T>> Comp;
     99         typedef test_allocator<T> A;
    100         typedef std::unordered_set<T, HF, Comp, A> C;
    101 
    102         A a(42);
    103         C c({
    104                 T(1),
    105                 T(2),
    106                 T(3),
    107                 T(4),
    108                 T(1),
    109                 T(2)
    110             }, 12, a);
    111 
    112         assert(c.bucket_count() >= 12);
    113         assert(c.size() == 4);
    114         assert(c.count(1) == 1);
    115         assert(c.count(2) == 1);
    116         assert(c.count(3) == 1);
    117         assert(c.count(4) == 1);
    118         assert(c.hash_function() == HF());
    119         assert(c.key_eq() == Comp());
    120         assert(c.get_allocator() == a);
    121         assert(!(c.get_allocator() == A()));
    122         assert(!c.empty());
    123         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    124         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    125         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    126         assert(c.max_load_factor() == 1);
    127     }
    128     {
    129         typedef int T;
    130         typedef test_hash<std::hash<T>> HF;
    131         typedef test_compare<std::equal_to<T>> Comp;
    132         typedef test_allocator<T> A;
    133         typedef std::unordered_set<T, HF, Comp, A> C;
    134 
    135         A a(42);
    136         HF hf(43);
    137         C c({
    138                 T(1),
    139                 T(2),
    140                 T(3),
    141                 T(4),
    142                 T(1),
    143                 T(2)
    144             }, 12, hf, a);
    145 
    146         assert(c.bucket_count() >= 12);
    147         assert(c.size() == 4);
    148         assert(c.count(1) == 1);
    149         assert(c.count(2) == 1);
    150         assert(c.count(3) == 1);
    151         assert(c.count(4) == 1);
    152         assert(c.hash_function() == hf);
    153         assert(!(c.hash_function() == HF()));
    154         assert(c.key_eq() == Comp());
    155         assert(c.get_allocator() == a);
    156         assert(!(c.get_allocator() == A()));
    157         assert(!c.empty());
    158         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    159         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    160         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    161         assert(c.max_load_factor() == 1);
    162     }
    163 #endif
    164 }