| 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 | // pair<iterator, iterator> equal_range(const key_type& k); |
| 16 | |
| 17 | #include <unordered_map> |
| 18 | #include <string> |
| 19 | #include <set> |
| 20 | #include <cassert> |
| 21 | #include <iterator> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "min_allocator.h" |
| 25 | |
| 26 | int main(int, char**) { |
| 27 | { |
| 28 | typedef std::unordered_multimap<int, std::string> C; |
| 29 | typedef C::iterator I; |
| 30 | typedef std::pair<int, std::string> P; |
| 31 | P a[] = { |
| 32 | P(10, "ten" ), |
| 33 | P(20, "twenty" ), |
| 34 | P(30, "thirty" ), |
| 35 | P(40, "forty" ), |
| 36 | P(50, "fifty" ), |
| 37 | P(50, "fiftyA" ), |
| 38 | P(50, "fiftyB" ), |
| 39 | P(60, "sixty" ), |
| 40 | P(70, "seventy" ), |
| 41 | P(80, "eighty" ), |
| 42 | }; |
| 43 | C c(std::begin(arr&: a), std::end(arr&: a)); |
| 44 | std::pair<I, I> r = c.equal_range(x: 30); |
| 45 | assert(std::distance(r.first, r.second) == 1); |
| 46 | assert(r.first->first == 30); |
| 47 | assert(r.first->second == "thirty" ); |
| 48 | r = c.equal_range(x: 5); |
| 49 | assert(std::distance(r.first, r.second) == 0); |
| 50 | r = c.equal_range(x: 50); |
| 51 | std::set<std::string> s; |
| 52 | s.insert(x: "fifty" ); |
| 53 | s.insert(x: "fiftyA" ); |
| 54 | s.insert(x: "fiftyB" ); |
| 55 | for (int i = 0; i < 3; ++i) { |
| 56 | assert(r.first->first == 50); |
| 57 | assert(s.find(r.first->second) != s.end()); |
| 58 | s.erase(position: s.find(x: r.first->second)); |
| 59 | ++r.first; |
| 60 | } |
| 61 | } |
| 62 | #if TEST_STD_VER >= 11 |
| 63 | { |
| 64 | typedef std::unordered_multimap<int, |
| 65 | std::string, |
| 66 | std::hash<int>, |
| 67 | std::equal_to<int>, |
| 68 | min_allocator<std::pair<const int, std::string>>> |
| 69 | C; |
| 70 | typedef C::iterator I; |
| 71 | typedef std::pair<int, std::string> P; |
| 72 | P a[] = { |
| 73 | P(10, "ten" ), |
| 74 | P(20, "twenty" ), |
| 75 | P(30, "thirty" ), |
| 76 | P(40, "forty" ), |
| 77 | P(50, "fifty" ), |
| 78 | P(50, "fiftyA" ), |
| 79 | P(50, "fiftyB" ), |
| 80 | P(60, "sixty" ), |
| 81 | P(70, "seventy" ), |
| 82 | P(80, "eighty" ), |
| 83 | }; |
| 84 | C c(std::begin(a), std::end(a)); |
| 85 | std::pair<I, I> r = c.equal_range(30); |
| 86 | assert(std::distance(r.first, r.second) == 1); |
| 87 | assert(r.first->first == 30); |
| 88 | assert(r.first->second == "thirty" ); |
| 89 | r = c.equal_range(5); |
| 90 | assert(std::distance(r.first, r.second) == 0); |
| 91 | r = c.equal_range(50); |
| 92 | std::set<std::string> s; |
| 93 | s.insert("fifty" ); |
| 94 | s.insert("fiftyA" ); |
| 95 | s.insert("fiftyB" ); |
| 96 | for (int i = 0; i < 3; ++i) { |
| 97 | assert(r.first->first == 50); |
| 98 | assert(s.find(r.first->second) != s.end()); |
| 99 | s.erase(s.find(r.first->second)); |
| 100 | ++r.first; |
| 101 | } |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |