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& operator=(unordered_map&& u);
     17 
     18 #include <unordered_map>
     19 #include <string>
     20 #include <cassert>
     21 #include <cfloat>
     22 
     23 #include "../../../test_compare.h"
     24 #include "../../../test_hash.h"
     25 #include "../../../test_allocator.h"
     26 
     27 int main()
     28 {
     29 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     30     {
     31         typedef test_allocator<std::pair<const int, std::string> > A;
     32         typedef std::unordered_map<int, std::string,
     33                                    test_hash<std::hash<int> >,
     34                                    test_compare<std::equal_to<int> >,
     35                                    A
     36                                    > C;
     37         typedef std::pair<int, std::string> P;
     38         P a[] =
     39         {
     40             P(1, "one"),
     41             P(2, "two"),
     42             P(3, "three"),
     43             P(4, "four"),
     44             P(1, "four"),
     45             P(2, "four"),
     46         };
     47         C c0(a, a + sizeof(a)/sizeof(a[0]),
     48             7,
     49             test_hash<std::hash<int> >(8),
     50             test_compare<std::equal_to<int> >(9),
     51             A(10)
     52            );
     53         C c(a, a + 2,
     54             7,
     55             test_hash<std::hash<int> >(2),
     56             test_compare<std::equal_to<int> >(3),
     57             A(4)
     58            );
     59         c = std::move(c0);
     60         assert(c.bucket_count() == 7);
     61         assert(c.size() == 4);
     62         assert(c.at(1) == "one");
     63         assert(c.at(2) == "two");
     64         assert(c.at(3) == "three");
     65         assert(c.at(4) == "four");
     66         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     67         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     68         assert(c.get_allocator() == A(4));
     69         assert(!c.empty());
     70         assert(std::distance(c.begin(), c.end()) == c.size());
     71         assert(std::distance(c.cbegin(), c.cend()) == c.size());
     72         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     73         assert(c.max_load_factor() == 1);
     74     }
     75     {
     76         typedef test_allocator<std::pair<const int, std::string> > A;
     77         typedef std::unordered_map<int, std::string,
     78                                    test_hash<std::hash<int> >,
     79                                    test_compare<std::equal_to<int> >,
     80                                    A
     81                                    > C;
     82         typedef std::pair<int, std::string> P;
     83         P a[] =
     84         {
     85             P(1, "one"),
     86             P(2, "two"),
     87             P(3, "three"),
     88             P(4, "four"),
     89             P(1, "four"),
     90             P(2, "four"),
     91         };
     92         C c0(a, a + sizeof(a)/sizeof(a[0]),
     93             7,
     94             test_hash<std::hash<int> >(8),
     95             test_compare<std::equal_to<int> >(9),
     96             A(10)
     97            );
     98         C c(a, a + 2,
     99             7,
    100             test_hash<std::hash<int> >(2),
    101             test_compare<std::equal_to<int> >(3),
    102             A(10)
    103            );
    104         c = std::move(c0);
    105         assert(c.bucket_count() == 7);
    106         assert(c.size() == 4);
    107         assert(c.at(1) == "one");
    108         assert(c.at(2) == "two");
    109         assert(c.at(3) == "three");
    110         assert(c.at(4) == "four");
    111         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    112         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    113         assert(c.get_allocator() == A(10));
    114         assert(!c.empty());
    115         assert(std::distance(c.begin(), c.end()) == c.size());
    116         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    117         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    118         assert(c.max_load_factor() == 1);
    119         assert(c0.size() == 0);
    120     }
    121     {
    122         typedef other_allocator<std::pair<const int, std::string> > A;
    123         typedef std::unordered_map<int, std::string,
    124                                    test_hash<std::hash<int> >,
    125                                    test_compare<std::equal_to<int> >,
    126                                    A
    127                                    > C;
    128         typedef std::pair<int, std::string> P;
    129         P a[] =
    130         {
    131             P(1, "one"),
    132             P(2, "two"),
    133             P(3, "three"),
    134             P(4, "four"),
    135             P(1, "four"),
    136             P(2, "four"),
    137         };
    138         C c0(a, a + sizeof(a)/sizeof(a[0]),
    139             7,
    140             test_hash<std::hash<int> >(8),
    141             test_compare<std::equal_to<int> >(9),
    142             A(10)
    143            );
    144         C c(a, a + 2,
    145             7,
    146             test_hash<std::hash<int> >(2),
    147             test_compare<std::equal_to<int> >(3),
    148             A(4)
    149            );
    150         c = std::move(c0);
    151         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() == A(10));
    160         assert(!c.empty());
    161         assert(std::distance(c.begin(), c.end()) == c.size());
    162         assert(std::distance(c.cbegin(), c.cend()) == c.size());
    163         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    164         assert(c.max_load_factor() == 1);
    165         assert(c0.size() == 0);
    166     }
    167 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    168 }
    169