libcxx

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

move_alloc.pass.cpp (10573B)


      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(unordered_multimap&& u, const allocator_type& a);
     19 
     20 #include <iostream>
     21 
     22 #include <unordered_map>
     23 #include <string>
     24 #include <cassert>
     25 #include <cfloat>
     26 #include <cmath>
     27 #include <cstddef>
     28 
     29 #include "test_macros.h"
     30 #include "../../../test_compare.h"
     31 #include "../../../test_hash.h"
     32 #include "test_allocator.h"
     33 #include "min_allocator.h"
     34 
     35 int main()
     36 {
     37     {
     38         typedef std::pair<int, std::string> P;
     39         typedef test_allocator<std::pair<const int, std::string>> A;
     40         typedef std::unordered_multimap<int, std::string,
     41                                    test_hash<std::hash<int> >,
     42                                    test_compare<std::equal_to<int> >,
     43                                    A
     44                                    > C;
     45         P a[] =
     46         {
     47             P(1, "one"),
     48             P(2, "two"),
     49             P(3, "three"),
     50             P(4, "four"),
     51             P(1, "four"),
     52             P(2, "four"),
     53         };
     54         C c0(a, a + sizeof(a)/sizeof(a[0]),
     55             7,
     56             test_hash<std::hash<int> >(8),
     57             test_compare<std::equal_to<int> >(9),
     58             A(10)
     59            );
     60         C c(std::move(c0), A(12));
     61         assert(c.bucket_count() >= 7);
     62         assert(c.size() == 6);
     63         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
     64         Eq eq = c.equal_range(1);
     65         assert(std::distance(eq.first, eq.second) == 2);
     66         C::const_iterator i = eq.first;
     67         assert(i->first == 1);
     68         assert(i->second == "one");
     69         ++i;
     70         assert(i->first == 1);
     71         assert(i->second == "four");
     72         eq = c.equal_range(2);
     73         assert(std::distance(eq.first, eq.second) == 2);
     74         i = eq.first;
     75         assert(i->first == 2);
     76         assert(i->second == "two");
     77         ++i;
     78         assert(i->first == 2);
     79         assert(i->second == "four");
     80 
     81         eq = c.equal_range(3);
     82         assert(std::distance(eq.first, eq.second) == 1);
     83         i = eq.first;
     84         assert(i->first == 3);
     85         assert(i->second == "three");
     86         eq = c.equal_range(4);
     87         assert(std::distance(eq.first, eq.second) == 1);
     88         i = eq.first;
     89         assert(i->first == 4);
     90         assert(i->second == "four");
     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(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     94         assert(c.max_load_factor() == 1);
     95         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     96         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     97         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(12)));
     98 
     99         assert(c0.empty());
    100     }
    101     {
    102         typedef std::pair<int, std::string> P;
    103         typedef test_allocator<std::pair<const int, std::string>> A;
    104         typedef std::unordered_multimap<int, std::string,
    105                                    test_hash<std::hash<int> >,
    106                                    test_compare<std::equal_to<int> >,
    107                                    A
    108                                    > C;
    109         P a[] =
    110         {
    111             P(1, "one"),
    112             P(2, "two"),
    113             P(3, "three"),
    114             P(4, "four"),
    115             P(1, "four"),
    116             P(2, "four"),
    117         };
    118         C c0(a, a + sizeof(a)/sizeof(a[0]),
    119             7,
    120             test_hash<std::hash<int> >(8),
    121             test_compare<std::equal_to<int> >(9),
    122             A(10)
    123            );
    124         C c(std::move(c0), A(10));
    125         LIBCPP_ASSERT(c.bucket_count() == 7);
    126         assert(c.size() == 6);
    127         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    128         Eq eq = c.equal_range(1);
    129         assert(std::distance(eq.first, eq.second) == 2);
    130         C::const_iterator i = eq.first;
    131         assert(i->first == 1);
    132         assert(i->second == "one");
    133         ++i;
    134         assert(i->first == 1);
    135         assert(i->second == "four");
    136         eq = c.equal_range(2);
    137         assert(std::distance(eq.first, eq.second) == 2);
    138         i = eq.first;
    139         assert(i->first == 2);
    140         assert(i->second == "two");
    141         ++i;
    142         assert(i->first == 2);
    143         assert(i->second == "four");
    144 
    145         eq = c.equal_range(3);
    146         assert(std::distance(eq.first, eq.second) == 1);
    147         i = eq.first;
    148         assert(i->first == 3);
    149         assert(i->second == "three");
    150         eq = c.equal_range(4);
    151         assert(std::distance(eq.first, eq.second) == 1);
    152         i = eq.first;
    153         assert(i->first == 4);
    154         assert(i->second == "four");
    155         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    156         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    157         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    158         assert(c.max_load_factor() == 1);
    159         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    160         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    161         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10)));
    162 
    163         assert(c0.empty());
    164     }
    165     {
    166         typedef std::pair<int, std::string> P;
    167         typedef min_allocator<std::pair<const int, std::string>> A;
    168         typedef std::unordered_multimap<int, std::string,
    169                                    test_hash<std::hash<int> >,
    170                                    test_compare<std::equal_to<int> >,
    171                                    A
    172                                    > C;
    173         P a[] =
    174         {
    175             P(1, "one"),
    176             P(2, "two"),
    177             P(3, "three"),
    178             P(4, "four"),
    179             P(1, "four"),
    180             P(2, "four"),
    181         };
    182         C c0(a, a + sizeof(a)/sizeof(a[0]),
    183             7,
    184             test_hash<std::hash<int> >(8),
    185             test_compare<std::equal_to<int> >(9),
    186             A()
    187            );
    188         C c(std::move(c0), A());
    189         LIBCPP_ASSERT(c.bucket_count() == 7);
    190         assert(c.size() == 6);
    191         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    192         Eq eq = c.equal_range(1);
    193         assert(std::distance(eq.first, eq.second) == 2);
    194         C::const_iterator i = eq.first;
    195         assert(i->first == 1);
    196         assert(i->second == "one");
    197         ++i;
    198         assert(i->first == 1);
    199         assert(i->second == "four");
    200         eq = c.equal_range(2);
    201         assert(std::distance(eq.first, eq.second) == 2);
    202         i = eq.first;
    203         assert(i->first == 2);
    204         assert(i->second == "two");
    205         ++i;
    206         assert(i->first == 2);
    207         assert(i->second == "four");
    208 
    209         eq = c.equal_range(3);
    210         assert(std::distance(eq.first, eq.second) == 1);
    211         i = eq.first;
    212         assert(i->first == 3);
    213         assert(i->second == "three");
    214         eq = c.equal_range(4);
    215         assert(std::distance(eq.first, eq.second) == 1);
    216         i = eq.first;
    217         assert(i->first == 4);
    218         assert(i->second == "four");
    219         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    220         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    221         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    222         assert(c.max_load_factor() == 1);
    223         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    224         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    225         assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
    226 
    227         assert(c0.empty());
    228     }
    229     {
    230         typedef std::pair<int, std::string> P;
    231         typedef explicit_allocator<std::pair<const int, std::string>> A;
    232         typedef std::unordered_multimap<int, std::string,
    233                                    test_hash<std::hash<int> >,
    234                                    test_compare<std::equal_to<int> >,
    235                                    A
    236                                    > C;
    237         P a[] =
    238         {
    239             P(1, "one"),
    240             P(2, "two"),
    241             P(3, "three"),
    242             P(4, "four"),
    243             P(1, "four"),
    244             P(2, "four"),
    245         };
    246         C c0(a, a + sizeof(a)/sizeof(a[0]),
    247             7,
    248             test_hash<std::hash<int> >(8),
    249             test_compare<std::equal_to<int> >(9),
    250             A{}
    251            );
    252         C c(std::move(c0), A());
    253         LIBCPP_ASSERT(c.bucket_count() == 7);
    254         assert(c.size() == 6);
    255         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
    256         Eq eq = c.equal_range(1);
    257         assert(std::distance(eq.first, eq.second) == 2);
    258         C::const_iterator i = eq.first;
    259         assert(i->first == 1);
    260         assert(i->second == "one");
    261         ++i;
    262         assert(i->first == 1);
    263         assert(i->second == "four");
    264         eq = c.equal_range(2);
    265         assert(std::distance(eq.first, eq.second) == 2);
    266         i = eq.first;
    267         assert(i->first == 2);
    268         assert(i->second == "two");
    269         ++i;
    270         assert(i->first == 2);
    271         assert(i->second == "four");
    272 
    273         eq = c.equal_range(3);
    274         assert(std::distance(eq.first, eq.second) == 1);
    275         i = eq.first;
    276         assert(i->first == 3);
    277         assert(i->second == "three");
    278         eq = c.equal_range(4);
    279         assert(std::distance(eq.first, eq.second) == 1);
    280         i = eq.first;
    281         assert(i->first == 4);
    282         assert(i->second == "four");
    283         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    284         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    285         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    286         assert(c.max_load_factor() == 1);
    287         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    288         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    289         assert(c.get_allocator() == A{});
    290 
    291         assert(c0.empty());
    292     }
    293 }