init.pass.cpp revision 6dff618d7db3d0e716638bf8010326b7b128f461
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(initializer_list<value_type> il);
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
28int main()
29{
30#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
31    {
32        typedef std::unordered_map<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        assert(c.bucket_count() >= 5);
47        assert(c.size() == 4);
48        assert(c.at(1) == "one");
49        assert(c.at(2) == "two");
50        assert(c.at(3) == "three");
51        assert(c.at(4) == "four");
52        assert(c.hash_function() == test_hash<std::hash<int> >());
53        assert(c.key_eq() == test_compare<std::equal_to<int> >());
54        assert(c.get_allocator() ==
55               (test_allocator<std::pair<const int, std::string> >()));
56        assert(!c.empty());
57        assert(std::distance(c.begin(), c.end()) == c.size());
58        assert(std::distance(c.cbegin(), c.cend()) == c.size());
59        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
60        assert(c.max_load_factor() == 1);
61    }
62#if __cplusplus >= 201103L
63    {
64        typedef std::unordered_map<int, std::string,
65                                   test_hash<std::hash<int> >,
66                                   test_compare<std::equal_to<int> >,
67                                   min_allocator<std::pair<const int, std::string> >
68                                   > C;
69        typedef std::pair<int, std::string> P;
70        C c = {
71                P(1, "one"),
72                P(2, "two"),
73                P(3, "three"),
74                P(4, "four"),
75                P(1, "four"),
76                P(2, "four"),
77              };
78        assert(c.bucket_count() >= 5);
79        assert(c.size() == 4);
80        assert(c.at(1) == "one");
81        assert(c.at(2) == "two");
82        assert(c.at(3) == "three");
83        assert(c.at(4) == "four");
84        assert(c.hash_function() == test_hash<std::hash<int> >());
85        assert(c.key_eq() == test_compare<std::equal_to<int> >());
86        assert(c.get_allocator() ==
87               (min_allocator<std::pair<const int, std::string> >()));
88        assert(!c.empty());
89        assert(std::distance(c.begin(), c.end()) == c.size());
90        assert(std::distance(c.cbegin(), c.cend()) == c.size());
91        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
92        assert(c.max_load_factor() == 1);
93    }
94#if _LIBCPP_STD_VER > 11
95    {
96        typedef std::pair<int, std::string> P;
97        typedef test_allocator<std::pair<const int, std::string>> A;
98        typedef test_hash<std::hash<int>> HF;
99        typedef test_compare<std::equal_to<int>> Comp;
100        typedef std::unordered_map<int, std::string, HF, Comp, A> C;
101
102        A a(42);
103        C c ( {
104                P(1, "one"),
105                P(2, "two"),
106                P(3, "three"),
107                P(4, "four"),
108                P(1, "four"),
109                P(2, "four"),
110              }, 12, a);
111        assert(c.bucket_count() >= 12);
112        assert(c.size() == 4);
113        assert(c.at(1) == "one");
114        assert(c.at(2) == "two");
115        assert(c.at(3) == "three");
116        assert(c.at(4) == "four");
117        assert(c.hash_function() == test_hash<std::hash<int> >());
118        assert(c.key_eq() == test_compare<std::equal_to<int> >());
119        assert(c.get_allocator() == a);
120        assert(!c.empty());
121        assert(std::distance(c.begin(), c.end()) == c.size());
122        assert(std::distance(c.cbegin(), c.cend()) == c.size());
123        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
124        assert(c.max_load_factor() == 1);
125    }
126    {
127        typedef std::pair<int, std::string> P;
128        typedef test_allocator<std::pair<const int, std::string>> A;
129        typedef test_hash<std::hash<int>> HF;
130        typedef test_compare<std::equal_to<int>> Comp;
131        typedef std::unordered_map<int, std::string, HF, Comp, A> C;
132
133        HF hf(42);
134        A a(43);
135        C c ( {
136                P(1, "one"),
137                P(2, "two"),
138                P(3, "three"),
139                P(4, "four"),
140                P(1, "four"),
141                P(2, "four"),
142              }, 12, hf, a);
143        assert(c.bucket_count() >= 12);
144        assert(c.size() == 4);
145        assert(c.at(1) == "one");
146        assert(c.at(2) == "two");
147        assert(c.at(3) == "three");
148        assert(c.at(4) == "four");
149        assert(c.hash_function() == hf);
150        assert(!(c.hash_function() == test_hash<std::hash<int> >()));
151        assert(c.key_eq() == test_compare<std::equal_to<int> >());
152        assert(c.get_allocator() == a);
153        assert(!c.empty());
154        assert(std::distance(c.begin(), c.end()) == c.size());
155        assert(std::distance(c.cbegin(), c.cend()) == c.size());
156        assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
157        assert(c.max_load_factor() == 1);
158    }
159#endif
160#endif
161#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
162}
163