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 | { |
32 | typedef std::unordered_multimap<int, std::string> C; |
33 | typedef std::pair<int, std::string> P; |
34 | P a[] = |
35 | { |
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 | C c; |
44 | c.insert(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0]))); |
45 | assert(c.size() == 6); |
46 | typedef std::pair<C::iterator, C::iterator> Eq; |
47 | Eq eq = c.equal_range(x: 1); |
48 | assert(std::distance(eq.first, eq.second) == 2); |
49 | std::multiset<std::string> s; |
50 | s.insert(x: "one" ); |
51 | s.insert(x: "four" ); |
52 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
53 | eq = c.equal_range(x: 2); |
54 | assert(std::distance(eq.first, eq.second) == 2); |
55 | s.insert(x: "two" ); |
56 | s.insert(x: "four" ); |
57 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
58 | eq = c.equal_range(x: 3); |
59 | assert(std::distance(eq.first, eq.second) == 1); |
60 | C::iterator k = eq.first; |
61 | assert(k->first == 3); |
62 | assert(k->second == "three" ); |
63 | eq = c.equal_range(x: 4); |
64 | assert(std::distance(eq.first, eq.second) == 1); |
65 | k = eq.first; |
66 | assert(k->first == 4); |
67 | assert(k->second == "four" ); |
68 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
69 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
70 | } |
71 | #if TEST_STD_VER >= 11 |
72 | { |
73 | typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
74 | min_allocator<std::pair<const int, std::string>>> C; |
75 | typedef std::pair<int, std::string> P; |
76 | P a[] = |
77 | { |
78 | P(1, "one" ), |
79 | P(2, "two" ), |
80 | P(3, "three" ), |
81 | P(4, "four" ), |
82 | P(1, "four" ), |
83 | P(2, "four" ), |
84 | }; |
85 | C c; |
86 | c.insert(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0]))); |
87 | assert(c.size() == 6); |
88 | typedef std::pair<C::iterator, C::iterator> Eq; |
89 | Eq eq = c.equal_range(1); |
90 | assert(std::distance(eq.first, eq.second) == 2); |
91 | std::multiset<std::string> s; |
92 | s.insert("one" ); |
93 | s.insert("four" ); |
94 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
95 | eq = c.equal_range(2); |
96 | assert(std::distance(eq.first, eq.second) == 2); |
97 | s.insert("two" ); |
98 | s.insert("four" ); |
99 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
100 | eq = c.equal_range(3); |
101 | assert(std::distance(eq.first, eq.second) == 1); |
102 | C::iterator k = eq.first; |
103 | assert(k->first == 3); |
104 | assert(k->second == "three" ); |
105 | eq = c.equal_range(4); |
106 | assert(std::distance(eq.first, eq.second) == 1); |
107 | k = eq.first; |
108 | assert(k->first == 4); |
109 | assert(k->second == "four" ); |
110 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
111 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
112 | } |
113 | #endif |
114 | |
115 | return 0; |
116 | } |
117 | |