| 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 | // <unordered_map> |
| 10 | |
| 11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 12 | // class Alloc = allocator<pair<const Key, T>>> |
| 13 | // class unordered_multimap |
| 14 | |
| 15 | // template <class InputIterator> |
| 16 | // void insert(InputIterator first, InputIterator last); |
| 17 | |
| 18 | #include <unordered_map> |
| 19 | #include <string> |
| 20 | #include <set> |
| 21 | #include <cassert> |
| 22 | #include <cstddef> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | #include "../../../check_consecutive.h" |
| 26 | #include "test_iterators.h" |
| 27 | #include "min_allocator.h" |
| 28 | |
| 29 | int main(int, char**) { |
| 30 | { |
| 31 | typedef std::unordered_multimap<int, std::string> C; |
| 32 | typedef std::pair<int, std::string> P; |
| 33 | P a[] = { |
| 34 | P(1, "one" ), |
| 35 | P(2, "two" ), |
| 36 | P(3, "three" ), |
| 37 | P(4, "four" ), |
| 38 | P(1, "four" ), |
| 39 | P(2, "four" ), |
| 40 | }; |
| 41 | C c; |
| 42 | c.insert(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a) / sizeof(a[0]))); |
| 43 | assert(c.size() == 6); |
| 44 | typedef std::pair<C::iterator, C::iterator> Eq; |
| 45 | Eq eq = c.equal_range(x: 1); |
| 46 | assert(std::distance(eq.first, eq.second) == 2); |
| 47 | std::multiset<std::string> s; |
| 48 | s.insert(x: "one" ); |
| 49 | s.insert(x: "four" ); |
| 50 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 51 | eq = c.equal_range(x: 2); |
| 52 | assert(std::distance(eq.first, eq.second) == 2); |
| 53 | s.insert(x: "two" ); |
| 54 | s.insert(x: "four" ); |
| 55 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 56 | eq = c.equal_range(x: 3); |
| 57 | assert(std::distance(eq.first, eq.second) == 1); |
| 58 | C::iterator k = eq.first; |
| 59 | assert(k->first == 3); |
| 60 | assert(k->second == "three" ); |
| 61 | eq = c.equal_range(x: 4); |
| 62 | assert(std::distance(eq.first, eq.second) == 1); |
| 63 | k = eq.first; |
| 64 | assert(k->first == 4); |
| 65 | assert(k->second == "four" ); |
| 66 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 67 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 68 | } |
| 69 | #if TEST_STD_VER >= 11 |
| 70 | { |
| 71 | typedef std::unordered_multimap<int, |
| 72 | std::string, |
| 73 | std::hash<int>, |
| 74 | std::equal_to<int>, |
| 75 | min_allocator<std::pair<const int, std::string>>> |
| 76 | C; |
| 77 | typedef std::pair<int, std::string> P; |
| 78 | P a[] = { |
| 79 | P(1, "one" ), |
| 80 | P(2, "two" ), |
| 81 | P(3, "three" ), |
| 82 | P(4, "four" ), |
| 83 | P(1, "four" ), |
| 84 | P(2, "four" ), |
| 85 | }; |
| 86 | C c; |
| 87 | c.insert(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a) / sizeof(a[0]))); |
| 88 | assert(c.size() == 6); |
| 89 | typedef std::pair<C::iterator, C::iterator> Eq; |
| 90 | Eq eq = c.equal_range(1); |
| 91 | assert(std::distance(eq.first, eq.second) == 2); |
| 92 | std::multiset<std::string> s; |
| 93 | s.insert("one" ); |
| 94 | s.insert("four" ); |
| 95 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 96 | eq = c.equal_range(2); |
| 97 | assert(std::distance(eq.first, eq.second) == 2); |
| 98 | s.insert("two" ); |
| 99 | s.insert("four" ); |
| 100 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 101 | eq = c.equal_range(3); |
| 102 | assert(std::distance(eq.first, eq.second) == 1); |
| 103 | C::iterator k = eq.first; |
| 104 | assert(k->first == 3); |
| 105 | assert(k->second == "three" ); |
| 106 | eq = c.equal_range(4); |
| 107 | assert(std::distance(eq.first, eq.second) == 1); |
| 108 | k = eq.first; |
| 109 | assert(k->first == 4); |
| 110 | assert(k->second == "four" ); |
| 111 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 112 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |