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); |
| 17 | |
Eric Fiselier | fd9bbf5 | 2015-07-19 03:16:47 +0000 | [diff] [blame] | 18 | // UNSUPPORTED: c++98, c++03 |
| 19 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | #include <string> |
| 22 | #include <cassert> |
Howard Hinnant | f836d53 | 2011-12-02 21:23:14 +0000 | [diff] [blame] | 23 | #include <cfloat> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | |
| 25 | #include "../../../test_compare.h" |
| 26 | #include "../../../test_hash.h" |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 27 | #include "test_allocator.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 28 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | |
| 30 | int main() |
| 31 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | { |
| 33 | typedef std::unordered_map<int, std::string, |
| 34 | test_hash<std::hash<int> >, |
| 35 | test_compare<std::equal_to<int> >, |
| 36 | test_allocator<std::pair<const int, std::string> > |
| 37 | > C; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | C c0(7, |
| 39 | test_hash<std::hash<int> >(8), |
| 40 | test_compare<std::equal_to<int> >(9), |
| 41 | test_allocator<std::pair<const int, std::string> >(10) |
| 42 | ); |
| 43 | C c = std::move(c0); |
| 44 | assert(c.bucket_count() == 7); |
| 45 | assert(c.size() == 0); |
| 46 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 47 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 48 | assert(c.get_allocator() == |
| 49 | (test_allocator<std::pair<const int, std::string> >(10))); |
| 50 | assert(c.empty()); |
| 51 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 52 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
| 53 | assert(c.load_factor() == 0); |
| 54 | assert(c.max_load_factor() == 1); |
| 55 | |
| 56 | assert(c0.empty()); |
| 57 | } |
| 58 | { |
| 59 | typedef std::unordered_map<int, std::string, |
| 60 | test_hash<std::hash<int> >, |
| 61 | test_compare<std::equal_to<int> >, |
| 62 | test_allocator<std::pair<const int, std::string> > |
| 63 | > C; |
| 64 | typedef std::pair<int, std::string> P; |
| 65 | P a[] = |
| 66 | { |
| 67 | P(1, "one"), |
| 68 | P(2, "two"), |
| 69 | P(3, "three"), |
| 70 | P(4, "four"), |
| 71 | P(1, "four"), |
| 72 | P(2, "four"), |
| 73 | }; |
| 74 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
| 75 | 7, |
| 76 | test_hash<std::hash<int> >(8), |
| 77 | test_compare<std::equal_to<int> >(9), |
| 78 | test_allocator<std::pair<const int, std::string> >(10) |
| 79 | ); |
| 80 | C c = std::move(c0); |
| 81 | assert(c.bucket_count() == 7); |
| 82 | assert(c.size() == 4); |
| 83 | assert(c.at(1) == "one"); |
| 84 | assert(c.at(2) == "two"); |
| 85 | assert(c.at(3) == "three"); |
| 86 | assert(c.at(4) == "four"); |
| 87 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 88 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 89 | assert(c.get_allocator() == |
| 90 | (test_allocator<std::pair<const int, std::string> >(10))); |
| 91 | assert(!c.empty()); |
| 92 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 93 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 94 | 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] | 95 | assert(c.max_load_factor() == 1); |
| 96 | |
| 97 | assert(c0.empty()); |
| 98 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 99 | { |
| 100 | typedef std::unordered_map<int, std::string, |
| 101 | test_hash<std::hash<int> >, |
| 102 | test_compare<std::equal_to<int> >, |
| 103 | min_allocator<std::pair<const int, std::string> > |
| 104 | > C; |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 105 | C c0(7, |
| 106 | test_hash<std::hash<int> >(8), |
| 107 | test_compare<std::equal_to<int> >(9), |
| 108 | min_allocator<std::pair<const int, std::string> >() |
| 109 | ); |
| 110 | C c = std::move(c0); |
| 111 | assert(c.bucket_count() == 7); |
| 112 | assert(c.size() == 0); |
| 113 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 114 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 115 | assert(c.get_allocator() == |
| 116 | (min_allocator<std::pair<const int, std::string> >())); |
| 117 | assert(c.empty()); |
| 118 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 119 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
| 120 | assert(c.load_factor() == 0); |
| 121 | assert(c.max_load_factor() == 1); |
| 122 | |
| 123 | assert(c0.empty()); |
| 124 | } |
| 125 | { |
| 126 | typedef std::unordered_map<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 | typedef std::pair<int, std::string> P; |
| 132 | P a[] = |
| 133 | { |
| 134 | P(1, "one"), |
| 135 | P(2, "two"), |
| 136 | P(3, "three"), |
| 137 | P(4, "four"), |
| 138 | P(1, "four"), |
| 139 | P(2, "four"), |
| 140 | }; |
| 141 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
| 142 | 7, |
| 143 | test_hash<std::hash<int> >(8), |
| 144 | test_compare<std::equal_to<int> >(9), |
| 145 | min_allocator<std::pair<const int, std::string> >() |
| 146 | ); |
| 147 | C c = std::move(c0); |
| 148 | assert(c.bucket_count() == 7); |
| 149 | assert(c.size() == 4); |
| 150 | assert(c.at(1) == "one"); |
| 151 | assert(c.at(2) == "two"); |
| 152 | assert(c.at(3) == "three"); |
| 153 | assert(c.at(4) == "four"); |
| 154 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 155 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 156 | assert(c.get_allocator() == |
| 157 | (min_allocator<std::pair<const int, std::string> >())); |
| 158 | assert(!c.empty()); |
| 159 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 160 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 161 | 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] | 162 | assert(c.max_load_factor() == 1); |
| 163 | |
| 164 | assert(c0.empty()); |
| 165 | } |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 166 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 824c199 | 2013-08-02 17:50:49 +0000 | [diff] [blame] | 167 | { |
| 168 | std::unordered_map<int, int> s1 = {{1, 1}, {2, 2}, {3, 3}}; |
| 169 | std::unordered_map<int, int>::iterator i = s1.begin(); |
| 170 | std::pair<const int, int> k = *i; |
| 171 | std::unordered_map<int, int> s2 = std::move(s1); |
| 172 | assert(*i == k); |
| 173 | s2.erase(i); |
| 174 | assert(s2.size() == 2); |
| 175 | } |
| 176 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 177 | } |