| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03 |
| 10 | |
| 11 | // <unordered_map> |
| 12 | |
| 13 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 14 | // class Alloc = allocator<pair<const Key, T>>> |
| 15 | // class unordered_multimap |
| 16 | |
| 17 | // unordered_multimap& operator=(initializer_list<value_type> il); |
| 18 | |
| 19 | #include <unordered_map> |
| 20 | #include <string> |
| 21 | #include <set> |
| 22 | #include <cassert> |
| 23 | #include <cfloat> |
| 24 | #include <cmath> |
| 25 | #include <cstddef> |
| 26 | |
| 27 | #include "test_macros.h" |
| 28 | #include "../../../check_consecutive.h" |
| 29 | #include "../../../test_compare.h" |
| 30 | #include "../../../test_hash.h" |
| 31 | #include "test_allocator.h" |
| 32 | #include "min_allocator.h" |
| 33 | |
| 34 | int main(int, char**) { |
| 35 | { |
| 36 | typedef test_allocator<std::pair<const int, std::string> > A; |
| 37 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 38 | typedef std::pair<int, std::string> P; |
| 39 | C c = { |
| 40 | P(4, "four" ), |
| 41 | P(1, "four" ), |
| 42 | P(2, "four" ), |
| 43 | }; |
| 44 | c = { |
| 45 | P(1, "one" ), |
| 46 | P(2, "two" ), |
| 47 | P(3, "three" ), |
| 48 | P(4, "four" ), |
| 49 | P(1, "four" ), |
| 50 | P(2, "four" ), |
| 51 | }; |
| 52 | assert(c.bucket_count() >= 7); |
| 53 | assert(c.size() == 6); |
| 54 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 55 | Eq eq = c.equal_range(1); |
| 56 | assert(std::distance(eq.first, eq.second) == 2); |
| 57 | std::multiset<std::string> s; |
| 58 | s.insert(x: "one" ); |
| 59 | s.insert(x: "four" ); |
| 60 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 61 | eq = c.equal_range(2); |
| 62 | assert(std::distance(eq.first, eq.second) == 2); |
| 63 | s.insert(x: "two" ); |
| 64 | s.insert(x: "four" ); |
| 65 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 66 | |
| 67 | eq = c.equal_range(3); |
| 68 | assert(std::distance(eq.first, eq.second) == 1); |
| 69 | C::const_iterator i = eq.first; |
| 70 | assert(i->first == 3); |
| 71 | assert(i->second == "three" ); |
| 72 | eq = c.equal_range(4); |
| 73 | assert(std::distance(eq.first, eq.second) == 1); |
| 74 | i = eq.first; |
| 75 | assert(i->first == 4); |
| 76 | assert(i->second == "four" ); |
| 77 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 78 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 79 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 80 | assert(c.max_load_factor() == 1); |
| 81 | } |
| 82 | { |
| 83 | typedef min_allocator<std::pair<const int, std::string> > A; |
| 84 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 85 | typedef std::pair<int, std::string> P; |
| 86 | C c = { |
| 87 | P(4, "four" ), |
| 88 | P(1, "four" ), |
| 89 | P(2, "four" ), |
| 90 | }; |
| 91 | c = { |
| 92 | P(1, "one" ), |
| 93 | P(2, "two" ), |
| 94 | P(3, "three" ), |
| 95 | P(4, "four" ), |
| 96 | P(1, "four" ), |
| 97 | P(2, "four" ), |
| 98 | }; |
| 99 | assert(c.bucket_count() >= 7); |
| 100 | assert(c.size() == 6); |
| 101 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 102 | Eq eq = c.equal_range(x: 1); |
| 103 | assert(std::distance(eq.first, eq.second) == 2); |
| 104 | std::multiset<std::string> s; |
| 105 | s.insert(x: "one" ); |
| 106 | s.insert(x: "four" ); |
| 107 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 108 | eq = c.equal_range(x: 2); |
| 109 | assert(std::distance(eq.first, eq.second) == 2); |
| 110 | s.insert(x: "two" ); |
| 111 | s.insert(x: "four" ); |
| 112 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 113 | |
| 114 | eq = c.equal_range(x: 3); |
| 115 | assert(std::distance(eq.first, eq.second) == 1); |
| 116 | C::const_iterator i = eq.first; |
| 117 | assert(i->first == 3); |
| 118 | assert(i->second == "three" ); |
| 119 | eq = c.equal_range(x: 4); |
| 120 | assert(std::distance(eq.first, eq.second) == 1); |
| 121 | i = eq.first; |
| 122 | assert(i->first == 4); |
| 123 | assert(i->second == "four" ); |
| 124 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 125 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 126 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 127 | assert(c.max_load_factor() == 1); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |