| 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 | // void insert(initializer_list<value_type> il); |
| 18 | |
| 19 | #include <unordered_map> |
| 20 | #include <string> |
| 21 | #include <set> |
| 22 | #include <cassert> |
| 23 | #include <cstddef> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | #include "../../../check_consecutive.h" |
| 27 | #include "test_iterators.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | int main(int, char**) { |
| 31 | { |
| 32 | typedef std::unordered_multimap<int, std::string> C; |
| 33 | typedef std::pair<int, std::string> P; |
| 34 | C c; |
| 35 | c.insert(l: { |
| 36 | P(1, "one" ), |
| 37 | P(2, "two" ), |
| 38 | P(3, "three" ), |
| 39 | P(4, "four" ), |
| 40 | P(1, "four" ), |
| 41 | P(2, "four" ), |
| 42 | }); |
| 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 | { |
| 70 | typedef std::unordered_multimap<int, |
| 71 | std::string, |
| 72 | std::hash<int>, |
| 73 | std::equal_to<int>, |
| 74 | min_allocator<std::pair<const int, std::string>>> |
| 75 | C; |
| 76 | typedef std::pair<int, std::string> P; |
| 77 | C c; |
| 78 | c.insert({ |
| 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 | assert(c.size() == 6); |
| 87 | typedef std::pair<C::iterator, C::iterator> Eq; |
| 88 | Eq eq = c.equal_range(1); |
| 89 | assert(std::distance(eq.first, eq.second) == 2); |
| 90 | std::multiset<std::string> s; |
| 91 | s.insert(x: "one" ); |
| 92 | s.insert(x: "four" ); |
| 93 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 94 | eq = c.equal_range(2); |
| 95 | assert(std::distance(eq.first, eq.second) == 2); |
| 96 | s.insert(x: "two" ); |
| 97 | s.insert(x: "four" ); |
| 98 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 99 | eq = c.equal_range(3); |
| 100 | assert(std::distance(eq.first, eq.second) == 1); |
| 101 | C::iterator k = eq.first; |
| 102 | assert(k->first == 3); |
| 103 | assert(k->second == "three" ); |
| 104 | eq = c.equal_range(4); |
| 105 | assert(std::distance(eq.first, eq.second) == 1); |
| 106 | k = eq.first; |
| 107 | assert(k->first == 4); |
| 108 | assert(k->second == "four" ); |
| 109 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 110 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |