libcxx

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

init.pass.cpp (9449B)


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