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_map
15 
16 // unordered_map& operator=(const unordered_map& u);
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     {
31         typedef test_allocator<std::pair<const int, std::string> > A;
32         typedef std::unordered_map<int, std::string,
33                                    test_hash<std::hash<int> >,
34                                    test_compare<std::equal_to<int> >,
35                                    A
36                                    > C;
37         typedef std::pair<int, std::string> P;
38         P a[] =
39         {
40             P(1, "one"),
41             P(2, "two"),
42             P(3, "three"),
43             P(4, "four"),
44             P(1, "four"),
45             P(2, "four"),
46         };
47         C c0(a, a + sizeof(a)/sizeof(a[0]),
48             7,
49             test_hash<std::hash<int> >(8),
50             test_compare<std::equal_to<int> >(9),
51             A(10)
52            );
53         C c(a, a + 2,
54             7,
55             test_hash<std::hash<int> >(2),
56             test_compare<std::equal_to<int> >(3),
57             A(4)
58            );
59         c = c0;
60         assert(c.bucket_count() == 7);
61         assert(c.size() == 4);
62         assert(c.at(1) == "one");
63         assert(c.at(2) == "two");
64         assert(c.at(3) == "three");
65         assert(c.at(4) == "four");
66         assert(c.hash_function() == test_hash<std::hash<int> >(8));
67         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
68         assert(c.get_allocator() == A(4));
69         assert(!c.empty());
70         assert(std::distance(c.begin(), c.end()) == c.size());
71         assert(std::distance(c.cbegin(), c.cend()) == c.size());
72         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
73         assert(c.max_load_factor() == 1);
74     }
75     {
76         typedef std::unordered_map<int, std::string> C;
77         typedef std::pair<const int, std::string> P;
78         const P a[] =
79         {
80             P(1, "one"),
81             P(2, "two"),
82             P(3, "three"),
83             P(4, "four"),
84             P(1, "four"),
85             P(2, "four"),
86         };
87         C c(a, a + sizeof(a)/sizeof(a[0]));
88         C *p = &c;
89         c = *p;
90         assert(c.size() == 4);
91         assert(std::is_permutation(c.begin(), c.end(), a));
92     }
93     {
94         typedef other_allocator<std::pair<const int, std::string> > A;
95         typedef std::unordered_map<int, std::string,
96                                    test_hash<std::hash<int> >,
97                                    test_compare<std::equal_to<int> >,
98                                    A
99                                    > C;
100         typedef std::pair<int, std::string> P;
101         P a[] =
102         {
103             P(1, "one"),
104             P(2, "two"),
105             P(3, "three"),
106             P(4, "four"),
107             P(1, "four"),
108             P(2, "four"),
109         };
110         C c0(a, a + sizeof(a)/sizeof(a[0]),
111             7,
112             test_hash<std::hash<int> >(8),
113             test_compare<std::equal_to<int> >(9),
114             A(10)
115            );
116         C c(a, a + 2,
117             7,
118             test_hash<std::hash<int> >(2),
119             test_compare<std::equal_to<int> >(3),
120             A(4)
121            );
122         c = c0;
123         assert(c.bucket_count() >= 5);
124         assert(c.size() == 4);
125         assert(c.at(1) == "one");
126         assert(c.at(2) == "two");
127         assert(c.at(3) == "three");
128         assert(c.at(4) == "four");
129         assert(c.hash_function() == test_hash<std::hash<int> >(8));
130         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
131         assert(c.get_allocator() == A(10));
132         assert(!c.empty());
133         assert(std::distance(c.begin(), c.end()) == c.size());
134         assert(std::distance(c.cbegin(), c.cend()) == c.size());
135         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
136         assert(c.max_load_factor() == 1);
137     }
138 #if __cplusplus >= 201103L
139     {
140         typedef min_allocator<std::pair<const int, std::string> > A;
141         typedef std::unordered_map<int, std::string,
142                                    test_hash<std::hash<int> >,
143                                    test_compare<std::equal_to<int> >,
144                                    A
145                                    > C;
146         typedef std::pair<int, std::string> P;
147         P a[] =
148         {
149             P(1, "one"),
150             P(2, "two"),
151             P(3, "three"),
152             P(4, "four"),
153             P(1, "four"),
154             P(2, "four"),
155         };
156         C c0(a, a + sizeof(a)/sizeof(a[0]),
157             7,
158             test_hash<std::hash<int> >(8),
159             test_compare<std::equal_to<int> >(9),
160             A()
161            );
162         C c(a, a + 2,
163             7,
164             test_hash<std::hash<int> >(2),
165             test_compare<std::equal_to<int> >(3),
166             A()
167            );
168         c = c0;
169         assert(c.bucket_count() == 7);
170         assert(c.size() == 4);
171         assert(c.at(1) == "one");
172         assert(c.at(2) == "two");
173         assert(c.at(3) == "three");
174         assert(c.at(4) == "four");
175         assert(c.hash_function() == test_hash<std::hash<int> >(8));
176         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
177         assert(c.get_allocator() == A());
178         assert(!c.empty());
179         assert(std::distance(c.begin(), c.end()) == c.size());
180         assert(std::distance(c.cbegin(), c.cend()) == c.size());
181         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
182         assert(c.max_load_factor() == 1);
183     }
184 #endif
185 }
186