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