libcxx

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

assign_move.pass.cpp (7754B)


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