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