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_multimap |
| 15 | |
| 16 | // template <class InputIterator> |
| 17 | // unordered_multimap(InputIterator first, InputIterator last, size_type n, |
| 18 | // const hasher& hf, const key_equal& eql); |
| 19 | |
| 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 | |
Marshall Clow | 83e2c4d | 2013-01-05 03:21:01 +0000 | [diff] [blame] | 25 | #include "test_iterators.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | #include "../../../NotConstructible.h" |
| 27 | #include "../../../test_compare.h" |
| 28 | #include "../../../test_hash.h" |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 29 | #include "test_allocator.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 30 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | |
| 32 | int main() |
| 33 | { |
| 34 | { |
| 35 | typedef std::unordered_multimap<int, std::string, |
| 36 | test_hash<std::hash<int> >, |
| 37 | test_compare<std::equal_to<int> >, |
| 38 | test_allocator<std::pair<const int, std::string> > |
| 39 | > C; |
| 40 | typedef std::pair<int, std::string> P; |
| 41 | P a[] = |
| 42 | { |
| 43 | P(1, "one"), |
| 44 | P(2, "two"), |
| 45 | P(3, "three"), |
| 46 | P(4, "four"), |
| 47 | P(1, "four"), |
| 48 | P(2, "four"), |
| 49 | }; |
| 50 | C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])), |
| 51 | 7, |
| 52 | test_hash<std::hash<int> >(8), |
| 53 | test_compare<std::equal_to<int> >(9) |
| 54 | ); |
| 55 | assert(c.bucket_count() == 7); |
| 56 | assert(c.size() == 6); |
| 57 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 58 | Eq eq = c.equal_range(1); |
| 59 | assert(std::distance(eq.first, eq.second) == 2); |
| 60 | C::const_iterator i = eq.first; |
| 61 | assert(i->first == 1); |
| 62 | assert(i->second == "one"); |
| 63 | ++i; |
| 64 | assert(i->first == 1); |
| 65 | assert(i->second == "four"); |
| 66 | eq = c.equal_range(2); |
| 67 | assert(std::distance(eq.first, eq.second) == 2); |
| 68 | i = eq.first; |
| 69 | assert(i->first == 2); |
| 70 | assert(i->second == "two"); |
| 71 | ++i; |
| 72 | assert(i->first == 2); |
| 73 | assert(i->second == "four"); |
| 74 | |
| 75 | eq = c.equal_range(3); |
| 76 | assert(std::distance(eq.first, eq.second) == 1); |
| 77 | i = eq.first; |
| 78 | assert(i->first == 3); |
| 79 | assert(i->second == "three"); |
| 80 | eq = c.equal_range(4); |
| 81 | assert(std::distance(eq.first, eq.second) == 1); |
| 82 | i = eq.first; |
| 83 | assert(i->first == 4); |
| 84 | assert(i->second == "four"); |
| 85 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 86 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 87 | 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] | 88 | assert(c.max_load_factor() == 1); |
| 89 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 90 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 91 | assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >())); |
| 92 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 93 | #if __cplusplus >= 201103L |
| 94 | { |
| 95 | typedef std::unordered_multimap<int, std::string, |
| 96 | test_hash<std::hash<int> >, |
| 97 | test_compare<std::equal_to<int> >, |
| 98 | min_allocator<std::pair<const int, std::string> > |
| 99 | > C; |
| 100 | typedef std::pair<int, std::string> P; |
| 101 | P a[] = |
| 102 | { |
| 103 | P(1, "one"), |
| 104 | P(2, "two"), |
| 105 | P(3, "three"), |
| 106 | P(4, "four"), |
| 107 | P(1, "four"), |
| 108 | P(2, "four"), |
| 109 | }; |
| 110 | C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])), |
| 111 | 7, |
| 112 | test_hash<std::hash<int> >(8), |
| 113 | test_compare<std::equal_to<int> >(9) |
| 114 | ); |
| 115 | assert(c.bucket_count() == 7); |
| 116 | assert(c.size() == 6); |
| 117 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 118 | Eq eq = c.equal_range(1); |
| 119 | assert(std::distance(eq.first, eq.second) == 2); |
| 120 | C::const_iterator i = eq.first; |
| 121 | assert(i->first == 1); |
| 122 | assert(i->second == "one"); |
| 123 | ++i; |
| 124 | assert(i->first == 1); |
| 125 | assert(i->second == "four"); |
| 126 | eq = c.equal_range(2); |
| 127 | assert(std::distance(eq.first, eq.second) == 2); |
| 128 | i = eq.first; |
| 129 | assert(i->first == 2); |
| 130 | assert(i->second == "two"); |
| 131 | ++i; |
| 132 | assert(i->first == 2); |
| 133 | assert(i->second == "four"); |
| 134 | |
| 135 | eq = c.equal_range(3); |
| 136 | assert(std::distance(eq.first, eq.second) == 1); |
| 137 | i = eq.first; |
| 138 | assert(i->first == 3); |
| 139 | assert(i->second == "three"); |
| 140 | eq = c.equal_range(4); |
| 141 | assert(std::distance(eq.first, eq.second) == 1); |
| 142 | i = eq.first; |
| 143 | assert(i->first == 4); |
| 144 | assert(i->second == "four"); |
| 145 | assert(std::distance(c.begin(), c.end()) == c.size()); |
| 146 | assert(std::distance(c.cbegin(), c.cend()) == c.size()); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 147 | 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] | 148 | assert(c.max_load_factor() == 1); |
| 149 | assert(c.hash_function() == test_hash<std::hash<int> >(8)); |
| 150 | assert(c.key_eq() == test_compare<std::equal_to<int> >(9)); |
| 151 | assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); |
| 152 | } |
| 153 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | } |