Home | History | Annotate | Download | only in unord.multimap.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_multimap
     15 
     16 // unordered_multimap(initializer_list<value_type> il);
     17 
     18 #include <unordered_map>
     19 #include <string>
     20 #include <cassert>
     21 #include <cfloat>
     22 
     23 #include "../../../test_compare.h"
     24 #include "../../../test_hash.h"
     25 #include "test_allocator.h"
     26 #include "min_allocator.h"
     27 
     28 int main()
     29 {
     30 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
     31     {
     32         typedef std::unordered_multimap<int, std::string,
     33                                    test_hash<std::hash<int> >,
     34                                    test_compare<std::equal_to<int> >,
     35                                    test_allocator<std::pair<const int, std::string> >
     36                                    > C;
     37         typedef std::pair<int, std::string> P;
     38         C c = {
     39                 P(1, "one"),
     40                 P(2, "two"),
     41                 P(3, "three"),
     42                 P(4, "four"),
     43                 P(1, "four"),
     44                 P(2, "four"),
     45               };
     46         assert(c.bucket_count() >= 7);
     47         assert(c.size() == 6);
     48         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
     49         Eq eq = c.equal_range(1);
     50         assert(std::distance(eq.first, eq.second) == 2);
     51         C::const_iterator i = eq.first;
     52         assert(i->first == 1);
     53         assert(i->second == "one");
     54         ++i;
     55         assert(i->first == 1);
     56         assert(i->second == "four");
     57         eq = c.equal_range(2);
     58         assert(std::distance(eq.first, eq.second) == 2);
     59         i = eq.first;
     60         assert(i->first == 2);
     61         assert(i->second == "two");
     62         ++i;
     63         assert(i->first == 2);
     64         assert(i->second == "four");
     65 
     66         eq = c.equal_range(3);
     67         assert(std::distance(eq.first, eq.second) == 1);
     68         i = eq.first;
     69         assert(i->first == 3);
     70         assert(i->second == "three");
     71         eq = c.equal_range(4);
     72         assert(std::distance(eq.first, eq.second) == 1);
     73         i = eq.first;
     74         assert(i->first == 4);
     75         assert(i->second == "four");
     76         assert(std::distance(c.begin(), c.end()) == c.size());
     77         assert(std::distance(c.cbegin(), c.cend()) == c.size());
     78         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     79         assert(c.max_load_factor() == 1);
     80         assert(c.hash_function() == test_hash<std::hash<int> >());
     81         assert(c.key_eq() == test_compare<std::equal_to<int> >());
     82         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
     83     }
     84 #if __cplusplus >= 201103L
     85     {
     86         typedef std::unordered_multimap<int, std::string,
     87                                    test_hash<std::hash<int> >,
     88                                    test_compare<std::equal_to<int> >,
     89                                    min_allocator<std::pair<const int, std::string> >
     90                                    > C;
     91         typedef std::pair<int, std::string> P;
     92         C c = {
     93                 P(1, "one"),
     94                 P(2, "two"),
     95                 P(3, "three"),
     96                 P(4, "four"),
     97                 P(1, "four"),
     98                 P(2, "four"),
     99               };
    100         assert(c.bucket_count() >= 7);
    101         assert(c.size() == 6);
    102         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    103         Eq eq = c.equal_range(1);
    104         assert(std::distance(eq.first, eq.second) == 2);
    105         C::const_iterator i = eq.first;
    106         assert(i->first == 1);
    107         assert(i->second == "one");
    108         ++i;
    109         assert(i->first == 1);
    110         assert(i->second == "four");
    111         eq = c.equal_range(2);
    112         assert(std::distance(eq.first, eq.second) == 2);
    113         i = eq.first;
    114         assert(i->first == 2);
    115         assert(i->second == "two");
    116         ++i;
    117         assert(i->first == 2);
    118         assert(i->second == "four");
    119 
    120         eq = c.equal_range(3);
    121         assert(std::distance(eq.first, eq.second) == 1);
    122         i = eq.first;
    123         assert(i->first == 3);
    124         assert(i->second == "three");
    125         eq = c.equal_range(4);
    126         assert(std::distance(eq.first, eq.second) == 1);
    127         i = eq.first;
    128         assert(i->first == 4);
    129         assert(i->second == "four");
    130         assert(std::distance(c.begin(), c.end()) == c.size());
    131         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    132         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    133         assert(c.max_load_factor() == 1);
    134         assert(c.hash_function() == test_hash<std::hash<int> >());
    135         assert(c.key_eq() == test_compare<std::equal_to<int> >());
    136         assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
    137     }
    138 #if _LIBCPP_STD_VER > 11
    139     {
    140         typedef std::pair<int, std::string> P;
    141         typedef test_allocator<std::pair<const int, std::string>> A;
    142         typedef test_hash<std::hash<int>> HF;
    143         typedef test_compare<std::equal_to<int>> Comp;
    144         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
    145 
    146         A a(42);
    147         C c ({
    148                 P(1, "one"),
    149                 P(2, "two"),
    150                 P(3, "three"),
    151                 P(4, "four"),
    152                 P(1, "four"),
    153                 P(2, "four"),
    154               }, 12, a );
    155         assert(c.bucket_count() >= 12);
    156         assert(c.size() == 6);
    157         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    158         Eq eq = c.equal_range(1);
    159         assert(std::distance(eq.first, eq.second) == 2);
    160         C::const_iterator i = eq.first;
    161         assert(i->first == 1);
    162         assert(i->second == "one");
    163         ++i;
    164         assert(i->first == 1);
    165         assert(i->second == "four");
    166         eq = c.equal_range(2);
    167         assert(std::distance(eq.first, eq.second) == 2);
    168         i = eq.first;
    169         assert(i->first == 2);
    170         assert(i->second == "two");
    171         ++i;
    172         assert(i->first == 2);
    173         assert(i->second == "four");
    174 
    175         eq = c.equal_range(3);
    176         assert(std::distance(eq.first, eq.second) == 1);
    177         i = eq.first;
    178         assert(i->first == 3);
    179         assert(i->second == "three");
    180         eq = c.equal_range(4);
    181         assert(std::distance(eq.first, eq.second) == 1);
    182         i = eq.first;
    183         assert(i->first == 4);
    184         assert(i->second == "four");
    185         assert(std::distance(c.begin(), c.end()) == c.size());
    186         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    187         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    188         assert(c.max_load_factor() == 1);
    189         assert(c.hash_function() == HF());
    190         assert(c.key_eq() == Comp());
    191         assert(c.get_allocator() == a);
    192         assert(!(c.get_allocator() == A()));
    193     }
    194     {
    195         typedef std::pair<int, std::string> P;
    196         typedef test_allocator<std::pair<const int, std::string>> A;
    197         typedef test_hash<std::hash<int>> HF;
    198         typedef test_compare<std::equal_to<int>> Comp;
    199         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
    200 
    201         HF hf(42);
    202         A a(43);
    203         C c ({
    204                 P(1, "one"),
    205                 P(2, "two"),
    206                 P(3, "three"),
    207                 P(4, "four"),
    208                 P(1, "four"),
    209                 P(2, "four"),
    210               }, 12, hf, a );
    211         assert(c.bucket_count() >= 12);
    212         assert(c.size() == 6);
    213         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    214         Eq eq = c.equal_range(1);
    215         assert(std::distance(eq.first, eq.second) == 2);
    216         C::const_iterator i = eq.first;
    217         assert(i->first == 1);
    218         assert(i->second == "one");
    219         ++i;
    220         assert(i->first == 1);
    221         assert(i->second == "four");
    222         eq = c.equal_range(2);
    223         assert(std::distance(eq.first, eq.second) == 2);
    224         i = eq.first;
    225         assert(i->first == 2);
    226         assert(i->second == "two");
    227         ++i;
    228         assert(i->first == 2);
    229         assert(i->second == "four");
    230 
    231         eq = c.equal_range(3);
    232         assert(std::distance(eq.first, eq.second) == 1);
    233         i = eq.first;
    234         assert(i->first == 3);
    235         assert(i->second == "three");
    236         eq = c.equal_range(4);
    237         assert(std::distance(eq.first, eq.second) == 1);
    238         i = eq.first;
    239         assert(i->first == 4);
    240         assert(i->second == "four");
    241         assert(std::distance(c.begin(), c.end()) == c.size());
    242         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    243         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    244         assert(c.max_load_factor() == 1);
    245         assert(c.hash_function() == hf);
    246         assert(!(c.hash_function() == HF()));
    247         assert(c.key_eq() == Comp());
    248         assert(c.get_allocator() == a);
    249         assert(!(c.get_allocator() == A()));
    250     }
    251 #endif
    252 #endif
    253 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
    254 }
    255