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);
19
20 #include <unordered_map>
21 #include <string>
22 #include <cassert>
23 #include <cfloat>
24
25 #include "test_iterators.h"
26 #include "../../../NotConstructible.h"
27 #include "../../../test_compare.h"
28 #include "../../../test_hash.h"
29 #include "test_allocator.h"
30 #include "min_allocator.h"
31
main()32 int main()
33 {
34 {
35 typedef std::unordered_multimap<int, std::string,
36 test_hash<std::hash<int> >,
37 test_compare<std::equal_to<int> >,
38 test_allocator<std::pair<const int, std::string> >
39 > C;
40 typedef std::pair<int, std::string> P;
41 P a[] =
42 {
43 P(1, "one"),
44 P(2, "two"),
45 P(3, "three"),
46 P(4, "four"),
47 P(1, "four"),
48 P(2, "four"),
49 };
50 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
51 7,
52 test_hash<std::hash<int> >(8)
53 );
54 assert(c.bucket_count() == 7);
55 assert(c.size() == 6);
56 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
57 Eq eq = c.equal_range(1);
58 assert(std::distance(eq.first, eq.second) == 2);
59 C::const_iterator i = eq.first;
60 assert(i->first == 1);
61 assert(i->second == "one");
62 ++i;
63 assert(i->first == 1);
64 assert(i->second == "four");
65 eq = c.equal_range(2);
66 assert(std::distance(eq.first, eq.second) == 2);
67 i = eq.first;
68 assert(i->first == 2);
69 assert(i->second == "two");
70 ++i;
71 assert(i->first == 2);
72 assert(i->second == "four");
73
74 eq = c.equal_range(3);
75 assert(std::distance(eq.first, eq.second) == 1);
76 i = eq.first;
77 assert(i->first == 3);
78 assert(i->second == "three");
79 eq = c.equal_range(4);
80 assert(std::distance(eq.first, eq.second) == 1);
81 i = eq.first;
82 assert(i->first == 4);
83 assert(i->second == "four");
84 assert(std::distance(c.begin(), c.end()) == c.size());
85 assert(std::distance(c.cbegin(), c.cend()) == c.size());
86 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
87 assert(c.max_load_factor() == 1);
88 assert(c.hash_function() == test_hash<std::hash<int> >(8));
89 assert(c.key_eq() == test_compare<std::equal_to<int> >());
90 assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >()));
91 }
92 #if __cplusplus >= 201103L
93 {
94 typedef std::unordered_multimap<int, std::string,
95 test_hash<std::hash<int> >,
96 test_compare<std::equal_to<int> >,
97 min_allocator<std::pair<const int, std::string> >
98 > C;
99 typedef std::pair<int, std::string> P;
100 P a[] =
101 {
102 P(1, "one"),
103 P(2, "two"),
104 P(3, "three"),
105 P(4, "four"),
106 P(1, "four"),
107 P(2, "four"),
108 };
109 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
110 7,
111 test_hash<std::hash<int> >(8)
112 );
113 assert(c.bucket_count() == 7);
114 assert(c.size() == 6);
115 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
116 Eq eq = c.equal_range(1);
117 assert(std::distance(eq.first, eq.second) == 2);
118 C::const_iterator i = eq.first;
119 assert(i->first == 1);
120 assert(i->second == "one");
121 ++i;
122 assert(i->first == 1);
123 assert(i->second == "four");
124 eq = c.equal_range(2);
125 assert(std::distance(eq.first, eq.second) == 2);
126 i = eq.first;
127 assert(i->first == 2);
128 assert(i->second == "two");
129 ++i;
130 assert(i->first == 2);
131 assert(i->second == "four");
132
133 eq = c.equal_range(3);
134 assert(std::distance(eq.first, eq.second) == 1);
135 i = eq.first;
136 assert(i->first == 3);
137 assert(i->second == "three");
138 eq = c.equal_range(4);
139 assert(std::distance(eq.first, eq.second) == 1);
140 i = eq.first;
141 assert(i->first == 4);
142 assert(i->second == "four");
143 assert(std::distance(c.begin(), c.end()) == c.size());
144 assert(std::distance(c.cbegin(), c.cend()) == c.size());
145 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
146 assert(c.max_load_factor() == 1);
147 assert(c.hash_function() == test_hash<std::hash<int> >(8));
148 assert(c.key_eq() == test_compare<std::equal_to<int> >());
149 assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >()));
150 }
151 #endif
152 }
153