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// template <class InputIterator>
17//     unordered_multimap(InputIterator first, InputIterator last, size_type n,
18//                   const hasher& hf, const key_equal& eql,
19//                   const allocator_type& a);
20
21#include <unordered_map>
22#include <string>
23#include <cassert>
24#include <cfloat>
25#include <cmath>
26#include <cstddef>
27
28#include "test_macros.h"
29#include "test_iterators.h"
30#include "../../../NotConstructible.h"
31#include "../../../test_compare.h"
32#include "../../../test_hash.h"
33#include "test_allocator.h"
34#include "min_allocator.h"
35
36int main()
37{
38    {
39        typedef std::unordered_multimap<int, std::string,
40                                   test_hash<std::hash<int> >,
41                                   test_compare<std::equal_to<int> >,
42                                   test_allocator<std::pair<const int, std::string> >
43                                   > C;
44        typedef std::pair<int, std::string> P;
45        P a[] =
46        {
47            P(1, "one"),
48            P(2, "two"),
49            P(3, "three"),
50            P(4, "four"),
51            P(1, "four"),
52            P(2, "four"),
53        };
54        C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
55            7,
56            test_hash<std::hash<int> >(8),
57            test_compare<std::equal_to<int> >(9),
58            test_allocator<std::pair<const int, std::string> >(10)
59           );
60        LIBCPP_ASSERT(c.bucket_count() == 7);
61        assert(c.size() == 6);
62        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
63        Eq eq = c.equal_range(1);
64        assert(std::distance(eq.first, eq.second) == 2);
65        C::const_iterator i = eq.first;
66        assert(i->first == 1);
67        assert(i->second == "one");
68        ++i;
69        assert(i->first == 1);
70        assert(i->second == "four");
71        eq = c.equal_range(2);
72        assert(std::distance(eq.first, eq.second) == 2);
73        i = eq.first;
74        assert(i->first == 2);
75        assert(i->second == "two");
76        ++i;
77        assert(i->first == 2);
78        assert(i->second == "four");
79
80        eq = c.equal_range(3);
81        assert(std::distance(eq.first, eq.second) == 1);
82        i = eq.first;
83        assert(i->first == 3);
84        assert(i->second == "three");
85        eq = c.equal_range(4);
86        assert(std::distance(eq.first, eq.second) == 1);
87        i = eq.first;
88        assert(i->first == 4);
89        assert(i->second == "four");
90        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
91        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
92        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
93        assert(c.max_load_factor() == 1);
94        assert(c.hash_function() == test_hash<std::hash<int> >(8));
95        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
96        assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10)));
97    }
98#if TEST_STD_VER >= 11
99    {
100        typedef std::unordered_multimap<int, std::string,
101                                   test_hash<std::hash<int> >,
102                                   test_compare<std::equal_to<int> >,
103                                   min_allocator<std::pair<const int, std::string> >
104                                   > C;
105        typedef std::pair<int, std::string> P;
106        P a[] =
107        {
108            P(1, "one"),
109            P(2, "two"),
110            P(3, "three"),
111            P(4, "four"),
112            P(1, "four"),
113            P(2, "four"),
114        };
115        C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
116            7,
117            test_hash<std::hash<int> >(8),
118            test_compare<std::equal_to<int> >(9),
119            min_allocator<std::pair<const int, std::string> >()
120           );
121        LIBCPP_ASSERT(c.bucket_count() == 7);
122        assert(c.size() == 6);
123        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
124        Eq eq = c.equal_range(1);
125        assert(std::distance(eq.first, eq.second) == 2);
126        C::const_iterator i = eq.first;
127        assert(i->first == 1);
128        assert(i->second == "one");
129        ++i;
130        assert(i->first == 1);
131        assert(i->second == "four");
132        eq = c.equal_range(2);
133        assert(std::distance(eq.first, eq.second) == 2);
134        i = eq.first;
135        assert(i->first == 2);
136        assert(i->second == "two");
137        ++i;
138        assert(i->first == 2);
139        assert(i->second == "four");
140
141        eq = c.equal_range(3);
142        assert(std::distance(eq.first, eq.second) == 1);
143        i = eq.first;
144        assert(i->first == 3);
145        assert(i->second == "three");
146        eq = c.equal_range(4);
147        assert(std::distance(eq.first, eq.second) == 1);
148        i = eq.first;
149        assert(i->first == 4);
150        assert(i->second == "four");
151        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
152        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
153        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
154        assert(c.max_load_factor() == 1);
155        assert(c.hash_function() == test_hash<std::hash<int> >(8));
156        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
157        assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
158    }
159    {
160        typedef explicit_allocator<std::pair<const int, std::string>> A;
161        typedef std::unordered_multimap<int, std::string,
162                                   test_hash<std::hash<int> >,
163                                   test_compare<std::equal_to<int> >,
164                                   A
165                                   > C;
166        typedef std::pair<int, std::string> P;
167        P a[] =
168        {
169            P(1, "one"),
170            P(2, "two"),
171            P(3, "three"),
172            P(4, "four"),
173            P(1, "four"),
174            P(2, "four"),
175        };
176        C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
177            7,
178            test_hash<std::hash<int> >(8),
179            test_compare<std::equal_to<int> >(9),
180            A{}
181           );
182        LIBCPP_ASSERT(c.bucket_count() == 7);
183        assert(c.size() == 6);
184        typedef std::pair<C::const_iterator, C::const_iterator> Eq;
185        Eq eq = c.equal_range(1);
186        assert(std::distance(eq.first, eq.second) == 2);
187        C::const_iterator i = eq.first;
188        assert(i->first == 1);
189        assert(i->second == "one");
190        ++i;
191        assert(i->first == 1);
192        assert(i->second == "four");
193        eq = c.equal_range(2);
194        assert(std::distance(eq.first, eq.second) == 2);
195        i = eq.first;
196        assert(i->first == 2);
197        assert(i->second == "two");
198        ++i;
199        assert(i->first == 2);
200        assert(i->second == "four");
201
202        eq = c.equal_range(3);
203        assert(std::distance(eq.first, eq.second) == 1);
204        i = eq.first;
205        assert(i->first == 3);
206        assert(i->second == "three");
207        eq = c.equal_range(4);
208        assert(std::distance(eq.first, eq.second) == 1);
209        i = eq.first;
210        assert(i->first == 4);
211        assert(i->second == "four");
212        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
213        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
214        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
215        assert(c.max_load_factor() == 1);
216        assert(c.hash_function() == test_hash<std::hash<int> >(8));
217        assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
218        assert(c.get_allocator() == A{});
219    }
220#endif
221}
222