Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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> |
Howard Hinnant | f836d53 | 2011-12-02 21:23:14 +0000 | [diff] [blame] | 21 | #include <cfloat> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | |
| 23 | #include "../../../test_compare.h" |
| 24 | #include "../../../test_hash.h" |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 25 | #include "test_allocator.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 26 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | |
| 28 | int main() |
| 29 | { |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 30 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 67 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 108 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 109 | assert(c.max_load_factor() == 1); |
| 110 | |
| 111 | assert(c0.empty()); |
| 112 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 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()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 150 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 151 | assert(c.max_load_factor() == 1); |
| 152 | |
| 153 | assert(c0.empty()); |
| 154 | } |
| 155 | #endif |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 156 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 157 | } |