libcxx

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

move_alloc.pass.cpp (7181B)


      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_map
     17 
     18 // unordered_map(unordered_map&& u, const allocator_type& a);
     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::pair<int, std::string> P;
     37         typedef test_allocator<std::pair<const int, std::string>> A;
     38         typedef std::unordered_map<int, std::string,
     39                                    test_hash<std::hash<int> >,
     40                                    test_compare<std::equal_to<int> >,
     41                                    A
     42                                    > C;
     43         P a[] =
     44         {
     45             P(1, "one"),
     46             P(2, "two"),
     47             P(3, "three"),
     48             P(4, "four"),
     49             P(1, "four"),
     50             P(2, "four"),
     51         };
     52         C c0(a, a + sizeof(a)/sizeof(a[0]),
     53             7,
     54             test_hash<std::hash<int> >(8),
     55             test_compare<std::equal_to<int> >(9),
     56             A(10)
     57            );
     58         C c(std::move(c0), A(12));
     59         assert(c.bucket_count() >= 5);
     60         assert(c.size() == 4);
     61         assert(c.at(1) == "one");
     62         assert(c.at(2) == "two");
     63         assert(c.at(3) == "three");
     64         assert(c.at(4) == "four");
     65         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     66         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     67         assert(c.get_allocator() == A(12));
     68         assert(!c.empty());
     69         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     70         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     71         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     72         assert(c.max_load_factor() == 1);
     73 
     74         assert(c0.empty());
     75     }
     76     {
     77         typedef std::pair<int, std::string> P;
     78         typedef test_allocator<std::pair<const int, std::string>> A;
     79         typedef std::unordered_map<int, std::string,
     80                                    test_hash<std::hash<int> >,
     81                                    test_compare<std::equal_to<int> >,
     82                                    A
     83                                    > C;
     84         P a[] =
     85         {
     86             P(1, "one"),
     87             P(2, "two"),
     88             P(3, "three"),
     89             P(4, "four"),
     90             P(1, "four"),
     91             P(2, "four"),
     92         };
     93         C c0(a, a + sizeof(a)/sizeof(a[0]),
     94             7,
     95             test_hash<std::hash<int> >(8),
     96             test_compare<std::equal_to<int> >(9),
     97             A(10)
     98            );
     99         C c(std::move(c0), A(10));
    100         LIBCPP_ASSERT(c.bucket_count() == 7);
    101         assert(c.size() == 4);
    102         assert(c.at(1) == "one");
    103         assert(c.at(2) == "two");
    104         assert(c.at(3) == "three");
    105         assert(c.at(4) == "four");
    106         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    107         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    108         assert(c.get_allocator() == A(10));
    109         assert(!c.empty());
    110         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    111         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    112         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    113         assert(c.max_load_factor() == 1);
    114 
    115         assert(c0.empty());
    116     }
    117     {
    118         typedef std::pair<int, std::string> P;
    119         typedef min_allocator<std::pair<const int, std::string>> A;
    120         typedef std::unordered_map<int, std::string,
    121                                    test_hash<std::hash<int> >,
    122                                    test_compare<std::equal_to<int> >,
    123                                    A
    124                                    > C;
    125         P a[] =
    126         {
    127             P(1, "one"),
    128             P(2, "two"),
    129             P(3, "three"),
    130             P(4, "four"),
    131             P(1, "four"),
    132             P(2, "four"),
    133         };
    134         C c0(a, a + sizeof(a)/sizeof(a[0]),
    135             7,
    136             test_hash<std::hash<int> >(8),
    137             test_compare<std::equal_to<int> >(9),
    138             A()
    139            );
    140         C c(std::move(c0), A());
    141         LIBCPP_ASSERT(c.bucket_count() == 7);
    142         assert(c.size() == 4);
    143         assert(c.at(1) == "one");
    144         assert(c.at(2) == "two");
    145         assert(c.at(3) == "three");
    146         assert(c.at(4) == "four");
    147         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    148         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    149         assert(c.get_allocator() == A());
    150         assert(!c.empty());
    151         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    152         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    153         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    154         assert(c.max_load_factor() == 1);
    155 
    156         assert(c0.empty());
    157     }
    158     {
    159         typedef std::pair<int, std::string> P;
    160         typedef explicit_allocator<std::pair<const int, std::string>> A;
    161         typedef std::unordered_map<int, std::string,
    162                                    test_hash<std::hash<int> >,
    163                                    test_compare<std::equal_to<int> >,
    164                                    A
    165                                    > C;
    166         P a[] =
    167         {
    168             P(1, "one"),
    169             P(2, "two"),
    170             P(3, "three"),
    171             P(4, "four"),
    172             P(1, "four"),
    173             P(2, "four"),
    174         };
    175         C c0(a, a + sizeof(a)/sizeof(a[0]),
    176             7,
    177             test_hash<std::hash<int> >(8),
    178             test_compare<std::equal_to<int> >(9),
    179             A{}
    180            );
    181         C c(std::move(c0), A{});
    182         LIBCPP_ASSERT(c.bucket_count() == 7);
    183         assert(c.size() == 4);
    184         assert(c.at(1) == "one");
    185         assert(c.at(2) == "two");
    186         assert(c.at(3) == "three");
    187         assert(c.at(4) == "four");
    188         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    189         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    190         assert(c.get_allocator() == A{});
    191         assert(!c.empty());
    192         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    193         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    194         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    195         assert(c.max_load_factor() == 1);
    196 
    197         assert(c0.empty());
    198     }
    199 }