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 | { |
37 | typedef test_allocator<std::pair<const int, std::string> > A; |
38 | typedef std::unordered_multimap<int, std::string, |
39 | test_hash<int>, |
40 | test_equal_to<int>, |
41 | A |
42 | > C; |
43 | typedef std::pair<int, std::string> P; |
44 | C c = { |
45 | P(4, "four" ), |
46 | P(1, "four" ), |
47 | P(2, "four" ), |
48 | }; |
49 | c = { |
50 | P(1, "one" ), |
51 | P(2, "two" ), |
52 | P(3, "three" ), |
53 | P(4, "four" ), |
54 | P(1, "four" ), |
55 | P(2, "four" ), |
56 | }; |
57 | assert(c.bucket_count() >= 7); |
58 | assert(c.size() == 6); |
59 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
60 | Eq eq = c.equal_range(1); |
61 | assert(std::distance(eq.first, eq.second) == 2); |
62 | std::multiset<std::string> s; |
63 | s.insert(x: "one" ); |
64 | s.insert(x: "four" ); |
65 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
66 | eq = c.equal_range(2); |
67 | assert(std::distance(eq.first, eq.second) == 2); |
68 | s.insert(x: "two" ); |
69 | s.insert(x: "four" ); |
70 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
71 | |
72 | eq = c.equal_range(3); |
73 | assert(std::distance(eq.first, eq.second) == 1); |
74 | C::const_iterator i = eq.first; |
75 | assert(i->first == 3); |
76 | assert(i->second == "three" ); |
77 | eq = c.equal_range(4); |
78 | assert(std::distance(eq.first, eq.second) == 1); |
79 | i = eq.first; |
80 | assert(i->first == 4); |
81 | assert(i->second == "four" ); |
82 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
83 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
84 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
85 | assert(c.max_load_factor() == 1); |
86 | } |
87 | { |
88 | typedef min_allocator<std::pair<const int, std::string> > A; |
89 | typedef std::unordered_multimap<int, std::string, |
90 | test_hash<int>, |
91 | test_equal_to<int>, |
92 | A |
93 | > C; |
94 | typedef std::pair<int, std::string> P; |
95 | C c = { |
96 | P(4, "four" ), |
97 | P(1, "four" ), |
98 | P(2, "four" ), |
99 | }; |
100 | c = { |
101 | P(1, "one" ), |
102 | P(2, "two" ), |
103 | P(3, "three" ), |
104 | P(4, "four" ), |
105 | P(1, "four" ), |
106 | P(2, "four" ), |
107 | }; |
108 | assert(c.bucket_count() >= 7); |
109 | assert(c.size() == 6); |
110 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
111 | Eq eq = c.equal_range(x: 1); |
112 | assert(std::distance(eq.first, eq.second) == 2); |
113 | std::multiset<std::string> s; |
114 | s.insert(x: "one" ); |
115 | s.insert(x: "four" ); |
116 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
117 | eq = c.equal_range(x: 2); |
118 | assert(std::distance(eq.first, eq.second) == 2); |
119 | s.insert(x: "two" ); |
120 | s.insert(x: "four" ); |
121 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
122 | |
123 | eq = c.equal_range(x: 3); |
124 | assert(std::distance(eq.first, eq.second) == 1); |
125 | C::const_iterator i = eq.first; |
126 | assert(i->first == 3); |
127 | assert(i->second == "three" ); |
128 | eq = c.equal_range(x: 4); |
129 | assert(std::distance(eq.first, eq.second) == 1); |
130 | i = eq.first; |
131 | assert(i->first == 4); |
132 | assert(i->second == "four" ); |
133 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
134 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
135 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
136 | assert(c.max_load_factor() == 1); |
137 | } |
138 | |
139 | return 0; |
140 | } |
141 | |