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, size_type n);
17
18 #include <unordered_map>
19 #include <string>
20 #include <cassert>
21 #include <cfloat>
22
23 #include "../../../test_compare.h"
24 #include "../../../test_hash.h"
25 #include "test_allocator.h"
26 #include "min_allocator.h"
27
main()28 int main()
29 {
30 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
31 {
32 typedef std::unordered_multimap<int, std::string,
33 test_hash<std::hash<int> >,
34 test_compare<std::equal_to<int> >,
35 test_allocator<std::pair<const int, std::string> >
36 > C;
37 typedef std::pair<int, std::string> P;
38 C c({
39 P(1, "one"),
40 P(2, "two"),
41 P(3, "three"),
42 P(4, "four"),
43 P(1, "four"),
44 P(2, "four"),
45 },
46 7
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(std::distance(c.begin(), c.end()) == c.size());
79 assert(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 __cplusplus >= 201103L
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 7
103 );
104 assert(c.bucket_count() == 7);
105 assert(c.size() == 6);
106 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
107 Eq eq = c.equal_range(1);
108 assert(std::distance(eq.first, eq.second) == 2);
109 C::const_iterator i = eq.first;
110 assert(i->first == 1);
111 assert(i->second == "one");
112 ++i;
113 assert(i->first == 1);
114 assert(i->second == "four");
115 eq = c.equal_range(2);
116 assert(std::distance(eq.first, eq.second) == 2);
117 i = eq.first;
118 assert(i->first == 2);
119 assert(i->second == "two");
120 ++i;
121 assert(i->first == 2);
122 assert(i->second == "four");
123
124 eq = c.equal_range(3);
125 assert(std::distance(eq.first, eq.second) == 1);
126 i = eq.first;
127 assert(i->first == 3);
128 assert(i->second == "three");
129 eq = c.equal_range(4);
130 assert(std::distance(eq.first, eq.second) == 1);
131 i = eq.first;
132 assert(i->first == 4);
133 assert(i->second == "four");
134 assert(std::distance(c.begin(), c.end()) == c.size());
135 assert(std::distance(c.cbegin(), c.cend()) == c.size());
136 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
137 assert(c.max_load_factor() == 1);
138 assert(c.hash_function() == test_hash<std::hash<int> >());
139 assert(c.key_eq() == test_compare<std::equal_to<int> >());
140 assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
141 }
142 #endif
143 #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
144 }
145