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(const unordered_multimap& u);
     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     {
     31         typedef std::unordered_multimap<int, std::string,
     32                                    test_hash<std::hash<int> >,
     33                                    test_compare<std::equal_to<int> >,
     34                                    test_allocator<std::pair<const int, std::string> >
     35                                    > C;
     36         typedef std::pair<int, std::string> P;
     37         P a[] =
     38         {
     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         C c0(a, a + sizeof(a)/sizeof(a[0]),
     47             7,
     48             test_hash<std::hash<int> >(8),
     49             test_compare<std::equal_to<int> >(9),
     50             test_allocator<std::pair<const int, std::string> >(10)
     51            );
     52         C c = c0;
     53         assert(c.bucket_count() == 7);
     54         assert(c.size() == 6);
     55         C::const_iterator i = c.cbegin();
     56         assert(i->first == 1);
     57         assert(i->second == "one");
     58         ++i;
     59         assert(i->first == 1);
     60         assert(i->second == "four");
     61         ++i;
     62         assert(i->first == 2);
     63         assert(i->second == "two");
     64         ++i;
     65         assert(i->first == 2);
     66         assert(i->second == "four");
     67         ++i;
     68         assert(i->first == 3);
     69         assert(i->second == "three");
     70         ++i;
     71         assert(i->first == 4);
     72         assert(i->second == "four");
     73         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     74         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     75         assert(c.get_allocator() ==
     76                (test_allocator<std::pair<const int, std::string> >(10)));
     77         assert(!c.empty());
     78         assert(std::distance(c.begin(), c.end()) == c.size());
     79         assert(std::distance(c.cbegin(), c.cend()) == c.size());
     80         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     81         assert(c.max_load_factor() == 1);
     82     }
     83 #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
     84     {
     85         typedef std::unordered_multimap<int, std::string,
     86                                    test_hash<std::hash<int> >,
     87                                    test_compare<std::equal_to<int> >,
     88                                    other_allocator<std::pair<const int, std::string> >
     89                                    > C;
     90         typedef std::pair<int, std::string> P;
     91         P a[] =
     92         {
     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         C c0(a, a + sizeof(a)/sizeof(a[0]),
    101             7,
    102             test_hash<std::hash<int> >(8),
    103             test_compare<std::equal_to<int> >(9),
    104             other_allocator<std::pair<const int, std::string> >(10)
    105            );
    106         C c = c0;
    107         assert(c.bucket_count() == 7);
    108         assert(c.size() == 6);
    109         C::const_iterator i = c.cbegin();
    110         assert(i->first == 1);
    111         assert(i->second == "one");
    112         ++i;
    113         assert(i->first == 1);
    114         assert(i->second == "four");
    115         ++i;
    116         assert(i->first == 2);
    117         assert(i->second == "two");
    118         ++i;
    119         assert(i->first == 2);
    120         assert(i->second == "four");
    121         ++i;
    122         assert(i->first == 3);
    123         assert(i->second == "three");
    124         ++i;
    125         assert(i->first == 4);
    126         assert(i->second == "four");
    127         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    128         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    129         assert(c.get_allocator() ==
    130                (other_allocator<std::pair<const int, std::string> >(-2)));
    131         assert(!c.empty());
    132         assert(std::distance(c.begin(), c.end()) == c.size());
    133         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    134         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    135         assert(c.max_load_factor() == 1);
    136     }
    137 #endif  // _LIBCPP_HAS_NO_ADVANCED_SFINAE
    138 #if __cplusplus >= 201103L
    139     {
    140         typedef std::unordered_multimap<int, std::string,
    141                                    test_hash<std::hash<int> >,
    142                                    test_compare<std::equal_to<int> >,
    143                                    min_allocator<std::pair<const int, std::string> >
    144                                    > C;
    145         typedef std::pair<int, std::string> P;
    146         P a[] =
    147         {
    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         };
    155         C c0(a, a + sizeof(a)/sizeof(a[0]),
    156             7,
    157             test_hash<std::hash<int> >(8),
    158             test_compare<std::equal_to<int> >(9),
    159             min_allocator<std::pair<const int, std::string> >()
    160            );
    161         C c = c0;
    162         assert(c.bucket_count() == 7);
    163         assert(c.size() == 6);
    164         C::const_iterator i = c.cbegin();
    165         assert(i->first == 1);
    166         assert(i->second == "one");
    167         ++i;
    168         assert(i->first == 1);
    169         assert(i->second == "four");
    170         ++i;
    171         assert(i->first == 2);
    172         assert(i->second == "two");
    173         ++i;
    174         assert(i->first == 2);
    175         assert(i->second == "four");
    176         ++i;
    177         assert(i->first == 3);
    178         assert(i->second == "three");
    179         ++i;
    180         assert(i->first == 4);
    181         assert(i->second == "four");
    182         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    183         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    184         assert(c.get_allocator() ==
    185                (min_allocator<std::pair<const int, std::string> >()));
    186         assert(!c.empty());
    187         assert(std::distance(c.begin(), c.end()) == c.size());
    188         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    189         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    190         assert(c.max_load_factor() == 1);
    191     }
    192 #endif
    193 }
    194