xref: /llvm-project/libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/init.pass.cpp (revision bbd717b9a3b22c6cba1535018dae2ebcee95fc9b)
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(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 
main(int,char **)34 int main(int, char**)
35 {
36     {
37         typedef std::unordered_multimap<int, std::string,
38                                    test_hash<int>,
39                                    test_equal_to<int>,
40                                    test_allocator<std::pair<const int, std::string> >
41                                    > C;
42         typedef std::pair<int, std::string> P;
43         C c = {
44                 P(1, "one"),
45                 P(2, "two"),
46                 P(3, "three"),
47                 P(4, "four"),
48                 P(1, "four"),
49                 P(2, "four"),
50               };
51         assert(c.bucket_count() >= 7);
52         assert(c.size() == 6);
53         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
54         Eq eq = c.equal_range(1);
55         assert(std::distance(eq.first, eq.second) == 2);
56         std::multiset<std::string> s;
57         s.insert("one");
58         s.insert("four");
59         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
60         eq = c.equal_range(2);
61         assert(std::distance(eq.first, eq.second) == 2);
62         s.insert("two");
63         s.insert("four");
64         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
65 
66         eq = c.equal_range(3);
67         assert(std::distance(eq.first, eq.second) == 1);
68         C::const_iterator i = eq.first;
69         assert(i->first == 3);
70         assert(i->second == "three");
71         eq = c.equal_range(4);
72         assert(std::distance(eq.first, eq.second) == 1);
73         i = eq.first;
74         assert(i->first == 4);
75         assert(i->second == "four");
76         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
77         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
78         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
79         assert(c.max_load_factor() == 1);
80         assert(c.hash_function() == test_hash<int>());
81         assert(c.key_eq() == test_equal_to<int>());
82         assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
83     }
84     {
85         typedef std::unordered_multimap<int, std::string,
86                                    test_hash<int>,
87                                    test_equal_to<int>,
88                                    min_allocator<std::pair<const int, std::string> >
89                                    > C;
90         typedef std::pair<int, std::string> P;
91         C c = {
92                 P(1, "one"),
93                 P(2, "two"),
94                 P(3, "three"),
95                 P(4, "four"),
96                 P(1, "four"),
97                 P(2, "four"),
98               };
99         assert(c.bucket_count() >= 7);
100         assert(c.size() == 6);
101         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
102         Eq eq = c.equal_range(1);
103         assert(std::distance(eq.first, eq.second) == 2);
104         std::multiset<std::string> s;
105         s.insert("one");
106         s.insert("four");
107         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
108         eq = c.equal_range(2);
109         assert(std::distance(eq.first, eq.second) == 2);
110         s.insert("two");
111         s.insert("four");
112         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
113 
114         eq = c.equal_range(3);
115         assert(std::distance(eq.first, eq.second) == 1);
116         C::const_iterator i = eq.first;
117         assert(i->first == 3);
118         assert(i->second == "three");
119         eq = c.equal_range(4);
120         assert(std::distance(eq.first, eq.second) == 1);
121         i = eq.first;
122         assert(i->first == 4);
123         assert(i->second == "four");
124         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
125         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
126         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
127         assert(c.max_load_factor() == 1);
128         assert(c.hash_function() == test_hash<int>());
129         assert(c.key_eq() == test_equal_to<int>());
130         assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
131     }
132 #if TEST_STD_VER > 11
133     {
134         typedef std::pair<int, std::string> P;
135         typedef test_allocator<std::pair<const int, std::string>> A;
136         typedef test_hash<int> HF;
137         typedef test_equal_to<int> Comp;
138         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
139 
140         A a(42);
141         C c ({
142                 P(1, "one"),
143                 P(2, "two"),
144                 P(3, "three"),
145                 P(4, "four"),
146                 P(1, "four"),
147                 P(2, "four"),
148               }, 12, a );
149         assert(c.bucket_count() >= 12);
150         assert(c.size() == 6);
151         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
152         Eq eq = c.equal_range(1);
153         assert(std::distance(eq.first, eq.second) == 2);
154         std::multiset<std::string> s;
155         s.insert("one");
156         s.insert("four");
157         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
158         eq = c.equal_range(2);
159         assert(std::distance(eq.first, eq.second) == 2);
160         s.insert("two");
161         s.insert("four");
162         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
163 
164         eq = c.equal_range(3);
165         assert(std::distance(eq.first, eq.second) == 1);
166         C::const_iterator i = eq.first;
167         assert(i->first == 3);
168         assert(i->second == "three");
169         eq = c.equal_range(4);
170         assert(std::distance(eq.first, eq.second) == 1);
171         i = eq.first;
172         assert(i->first == 4);
173         assert(i->second == "four");
174         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
175         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
176         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
177         assert(c.max_load_factor() == 1);
178         assert(c.hash_function() == HF());
179         assert(c.key_eq() == Comp());
180         assert(c.get_allocator() == a);
181         assert(!(c.get_allocator() == A()));
182     }
183     {
184         typedef std::pair<int, std::string> P;
185         typedef test_allocator<std::pair<const int, std::string>> A;
186         typedef test_hash<int> HF;
187         typedef test_equal_to<int> Comp;
188         typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
189 
190         HF hf(42);
191         A a(43);
192         C c ({
193                 P(1, "one"),
194                 P(2, "two"),
195                 P(3, "three"),
196                 P(4, "four"),
197                 P(1, "four"),
198                 P(2, "four"),
199               }, 12, hf, a );
200         assert(c.bucket_count() >= 12);
201         assert(c.size() == 6);
202         typedef std::pair<C::const_iterator, C::const_iterator> Eq;
203         Eq eq = c.equal_range(1);
204         assert(std::distance(eq.first, eq.second) == 2);
205         std::multiset<std::string> s;
206         s.insert("one");
207         s.insert("four");
208         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
209         eq = c.equal_range(2);
210         assert(std::distance(eq.first, eq.second) == 2);
211         s.insert("two");
212         s.insert("four");
213         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
214 
215         eq = c.equal_range(3);
216         assert(std::distance(eq.first, eq.second) == 1);
217         C::const_iterator i = eq.first;
218         assert(i->first == 3);
219         assert(i->second == "three");
220         eq = c.equal_range(4);
221         assert(std::distance(eq.first, eq.second) == 1);
222         i = eq.first;
223         assert(i->first == 4);
224         assert(i->second == "four");
225         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
226         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
227         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
228         assert(c.max_load_factor() == 1);
229         assert(c.hash_function() == hf);
230         assert(!(c.hash_function() == HF()));
231         assert(c.key_eq() == Comp());
232         assert(c.get_allocator() == a);
233         assert(!(c.get_allocator() == A()));
234     }
235 #endif // TEST_STD_VER > 11
236 
237   return 0;
238 }
239