1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <unordered_map>
11
12// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13//           class Alloc = allocator<pair<const Key, T>>>
14// class unordered_multimap
15
16// unordered_multimap(initializer_list<value_type> il);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
21#include <cfloat>
22#include <cstddef>
23
24#include "test_macros.h"
25#include "../../../test_compare.h"
26#include "../../../test_hash.h"
27#include "test_allocator.h"
28#include "min_allocator.h"
29
30int main()
31{
32#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
33    {
34        typedef std::unordered_multimap<int, std::string,
35                                   test_hash<std::hash<int> >,
36                                   test_compare<std::equal_to<int> >,
37                                   test_allocator<std::pair<const int, std::string> >
38                                   > C;
39        typedef std::pair<int, std::string> P;
40        C c = {
41                P(1, "one"),
42                P(2, "two"),
43                P(3, "three"),
44                P(4, "four"),
45                P(1, "four"),
46                P(2, "four"),
47              };
48        assert(c.bucket_count() >= 7);
49        assert(c.size() == 6);
50        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
51        Eq eq = c.equal_range(1);
52        assert(std::distance(eq.first, eq.second) == 2);
53        C::const_iterator i = eq.first;
54        assert(i->first == 1);
55        assert(i->second == "one");
56        ++i;
57        assert(i->first == 1);
58        assert(i->second == "four");
59        eq = c.equal_range(2);
60        assert(std::distance(eq.first, eq.second) == 2);
61        i = eq.first;
62        assert(i->first == 2);
63        assert(i->second == "two");
64        ++i;
65        assert(i->first == 2);
66        assert(i->second == "four");
67
68        eq = c.equal_range(3);
69        assert(std::distance(eq.first, eq.second) == 1);
70        i = eq.first;
71        assert(i->first == 3);
72        assert(i->second == "three");
73        eq = c.equal_range(4);
74        assert(std::distance(eq.first, eq.second) == 1);
75        i = eq.first;
76        assert(i->first == 4);
77        assert(i->second == "four");
78        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
79        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
80        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
81        assert(c.max_load_factor() == 1);
82        assert(c.hash_function() == test_hash<std::hash<int> >());
83        assert(c.key_eq() == test_compare<std::equal_to<int> >());
84        assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
85    }
86#if TEST_STD_VER >= 11
87    {
88        typedef std::unordered_multimap<int, std::string,
89                                   test_hash<std::hash<int> >,
90                                   test_compare<std::equal_to<int> >,
91                                   min_allocator<std::pair<const int, std::string> >
92                                   > C;
93        typedef std::pair<int, std::string> P;
94        C c = {
95                P(1, "one"),
96                P(2, "two"),
97                P(3, "three"),
98                P(4, "four"),
99                P(1, "four"),
100                P(2, "four"),
101              };
102        assert(c.bucket_count() >= 7);
103        assert(c.size() == 6);
104        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
105        Eq eq = c.equal_range(1);
106        assert(std::distance(eq.first, eq.second) == 2);
107        C::const_iterator i = eq.first;
108        assert(i->first == 1);
109        assert(i->second == "one");
110        ++i;
111        assert(i->first == 1);
112        assert(i->second == "four");
113        eq = c.equal_range(2);
114        assert(std::distance(eq.first, eq.second) == 2);
115        i = eq.first;
116        assert(i->first == 2);
117        assert(i->second == "two");
118        ++i;
119        assert(i->first == 2);
120        assert(i->second == "four");
121
122        eq = c.equal_range(3);
123        assert(std::distance(eq.first, eq.second) == 1);
124        i = eq.first;
125        assert(i->first == 3);
126        assert(i->second == "three");
127        eq = c.equal_range(4);
128        assert(std::distance(eq.first, eq.second) == 1);
129        i = eq.first;
130        assert(i->first == 4);
131        assert(i->second == "four");
132        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
133        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
134        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
135        assert(c.max_load_factor() == 1);
136        assert(c.hash_function() == test_hash<std::hash<int> >());
137        assert(c.key_eq() == test_compare<std::equal_to<int> >());
138        assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
139    }
140#if TEST_STD_VER > 11
141    {
142        typedef std::pair<int, std::string> P;
143        typedef test_allocator<std::pair<const int, std::string>> A;
144        typedef test_hash<std::hash<int>> HF;
145        typedef test_compare<std::equal_to<int>> Comp;
146        typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
147
148        A a(42);
149        C c ({
150                P(1, "one"),
151                P(2, "two"),
152                P(3, "three"),
153                P(4, "four"),
154                P(1, "four"),
155                P(2, "four"),
156              }, 12, a );
157        assert(c.bucket_count() >= 12);
158        assert(c.size() == 6);
159        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
160        Eq eq = c.equal_range(1);
161        assert(std::distance(eq.first, eq.second) == 2);
162        C::const_iterator i = eq.first;
163        assert(i->first == 1);
164        assert(i->second == "one");
165        ++i;
166        assert(i->first == 1);
167        assert(i->second == "four");
168        eq = c.equal_range(2);
169        assert(std::distance(eq.first, eq.second) == 2);
170        i = eq.first;
171        assert(i->first == 2);
172        assert(i->second == "two");
173        ++i;
174        assert(i->first == 2);
175        assert(i->second == "four");
176
177        eq = c.equal_range(3);
178        assert(std::distance(eq.first, eq.second) == 1);
179        i = eq.first;
180        assert(i->first == 3);
181        assert(i->second == "three");
182        eq = c.equal_range(4);
183        assert(std::distance(eq.first, eq.second) == 1);
184        i = eq.first;
185        assert(i->first == 4);
186        assert(i->second == "four");
187        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
188        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
189        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
190        assert(c.max_load_factor() == 1);
191        assert(c.hash_function() == HF());
192        assert(c.key_eq() == Comp());
193        assert(c.get_allocator() == a);
194        assert(!(c.get_allocator() == A()));
195    }
196    {
197        typedef std::pair<int, std::string> P;
198        typedef test_allocator<std::pair<const int, std::string>> A;
199        typedef test_hash<std::hash<int>> HF;
200        typedef test_compare<std::equal_to<int>> Comp;
201        typedef std::unordered_multimap<int, std::string, HF, Comp, A> C;
202
203        HF hf(42);
204        A a(43);
205        C c ({
206                P(1, "one"),
207                P(2, "two"),
208                P(3, "three"),
209                P(4, "four"),
210                P(1, "four"),
211                P(2, "four"),
212              }, 12, hf, a );
213        assert(c.bucket_count() >= 12);
214        assert(c.size() == 6);
215        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
216        Eq eq = c.equal_range(1);
217        assert(std::distance(eq.first, eq.second) == 2);
218        C::const_iterator i = eq.first;
219        assert(i->first == 1);
220        assert(i->second == "one");
221        ++i;
222        assert(i->first == 1);
223        assert(i->second == "four");
224        eq = c.equal_range(2);
225        assert(std::distance(eq.first, eq.second) == 2);
226        i = eq.first;
227        assert(i->first == 2);
228        assert(i->second == "two");
229        ++i;
230        assert(i->first == 2);
231        assert(i->second == "four");
232
233        eq = c.equal_range(3);
234        assert(std::distance(eq.first, eq.second) == 1);
235        i = eq.first;
236        assert(i->first == 3);
237        assert(i->second == "three");
238        eq = c.equal_range(4);
239        assert(std::distance(eq.first, eq.second) == 1);
240        i = eq.first;
241        assert(i->first == 4);
242        assert(i->second == "four");
243        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
244        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
245        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
246        assert(c.max_load_factor() == 1);
247        assert(c.hash_function() == hf);
248        assert(!(c.hash_function() == HF()));
249        assert(c.key_eq() == Comp());
250        assert(c.get_allocator() == a);
251        assert(!(c.get_allocator() == A()));
252    }
253#endif
254#endif
255#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
256}
257