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