libcxx

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

assign_init.pass.cpp (5139B)


      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& operator=(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_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 test_allocator<std::pair<const int, std::string> > A;
     36         typedef std::unordered_multimap<int, std::string,
     37                                    test_hash<std::hash<int> >,
     38                                    test_compare<std::equal_to<int> >,
     39                                    A
     40                                    > C;
     41         typedef std::pair<int, std::string> P;
     42         C c =   {
     43                     P(4, "four"),
     44                     P(1, "four"),
     45                     P(2, "four"),
     46                 };
     47         c =     {
     48                     P(1, "one"),
     49                     P(2, "two"),
     50                     P(3, "three"),
     51                     P(4, "four"),
     52                     P(1, "four"),
     53                     P(2, "four"),
     54                 };
     55         assert(c.bucket_count() >= 7);
     56         assert(c.size() == 6);
     57         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
     58         Eq eq = c.equal_range(1);
     59         assert(std::distance(eq.first, eq.second) == 2);
     60         C::const_iterator i = eq.first;
     61         assert(i->first == 1);
     62         assert(i->second == "one");
     63         ++i;
     64         assert(i->first == 1);
     65         assert(i->second == "four");
     66         eq = c.equal_range(2);
     67         assert(std::distance(eq.first, eq.second) == 2);
     68         i = eq.first;
     69         assert(i->first == 2);
     70         assert(i->second == "two");
     71         ++i;
     72         assert(i->first == 2);
     73         assert(i->second == "four");
     74 
     75         eq = c.equal_range(3);
     76         assert(std::distance(eq.first, eq.second) == 1);
     77         i = eq.first;
     78         assert(i->first == 3);
     79         assert(i->second == "three");
     80         eq = c.equal_range(4);
     81         assert(std::distance(eq.first, eq.second) == 1);
     82         i = eq.first;
     83         assert(i->first == 4);
     84         assert(i->second == "four");
     85         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     86         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     87         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     88         assert(c.max_load_factor() == 1);
     89     }
     90     {
     91         typedef min_allocator<std::pair<const int, std::string> > A;
     92         typedef std::unordered_multimap<int, std::string,
     93                                    test_hash<std::hash<int> >,
     94                                    test_compare<std::equal_to<int> >,
     95                                    A
     96                                    > C;
     97         typedef std::pair<int, std::string> P;
     98         C c =   {
     99                     P(4, "four"),
    100                     P(1, "four"),
    101                     P(2, "four"),
    102                 };
    103         c =     {
    104                     P(1, "one"),
    105                     P(2, "two"),
    106                     P(3, "three"),
    107                     P(4, "four"),
    108                     P(1, "four"),
    109                     P(2, "four"),
    110                 };
    111         assert(c.bucket_count() >= 7);
    112         assert(c.size() == 6);
    113         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    114         Eq eq = c.equal_range(1);
    115         assert(std::distance(eq.first, eq.second) == 2);
    116         C::const_iterator i = eq.first;
    117         assert(i->first == 1);
    118         assert(i->second == "one");
    119         ++i;
    120         assert(i->first == 1);
    121         assert(i->second == "four");
    122         eq = c.equal_range(2);
    123         assert(std::distance(eq.first, eq.second) == 2);
    124         i = eq.first;
    125         assert(i->first == 2);
    126         assert(i->second == "two");
    127         ++i;
    128         assert(i->first == 2);
    129         assert(i->second == "four");
    130 
    131         eq = c.equal_range(3);
    132         assert(std::distance(eq.first, eq.second) == 1);
    133         i = eq.first;
    134         assert(i->first == 3);
    135         assert(i->second == "three");
    136         eq = c.equal_range(4);
    137         assert(std::distance(eq.first, eq.second) == 1);
    138         i = eq.first;
    139         assert(i->first == 4);
    140         assert(i->second == "four");
    141         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    142         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    143         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    144         assert(c.max_load_factor() == 1);
    145     }
    146 }