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