libcxx

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

init.pass.cpp (6281B)


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