libcxx

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

move.pass.cpp (8523B)


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