1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03
10
11// <unordered_map>
12
13// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14// class Alloc = allocator<pair<const Key, T>>>
15// class unordered_map
16
17// unordered_map(initializer_list<value_type> il);
18
19#include <unordered_map>
20#include <string>
21#include <cassert>
22#include <cfloat>
23#include <cmath>
24#include <cstddef>
25
26#include "test_macros.h"
27#include "../../../test_compare.h"
28#include "../../../test_hash.h"
29#include "test_allocator.h"
30#include "min_allocator.h"
31
32int main(int, char**)
33{
34 {
35 typedef std::unordered_map<int, std::string,
36 test_hash<int>,
37 test_equal_to<int>,
38 test_allocator<std::pair<const int, std::string> >
39 > C;
40 typedef std::pair<int, std::string> P;
41 C c = {
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 assert(c.bucket_count() >= 5);
50 assert(c.size() == 4);
51 assert(c.at(1) == "one");
52 assert(c.at(2) == "two");
53 assert(c.at(3) == "three");
54 assert(c.at(4) == "four");
55 assert(c.hash_function() == test_hash<int>());
56 assert(c.key_eq() == test_equal_to<int>());
57 assert(c.get_allocator() ==
58 (test_allocator<std::pair<const int, std::string> >()));
59 assert(!c.empty());
60 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
61 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
62 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
63 assert(c.max_load_factor() == 1);
64 }
65 {
66 typedef std::unordered_map<int, std::string,
67 test_hash<int>,
68 test_equal_to<int>,
69 min_allocator<std::pair<const int, std::string> >
70 > C;
71 typedef std::pair<int, std::string> P;
72 C c = {
73 P(1, "one"),
74 P(2, "two"),
75 P(3, "three"),
76 P(4, "four"),
77 P(1, "four"),
78 P(2, "four"),
79 };
80 assert(c.bucket_count() >= 5);
81 assert(c.size() == 4);
82 assert(c.at(1) == "one");
83 assert(c.at(2) == "two");
84 assert(c.at(3) == "three");
85 assert(c.at(4) == "four");
86 assert(c.hash_function() == test_hash<int>());
87 assert(c.key_eq() == test_equal_to<int>());
88 assert(c.get_allocator() ==
89 (min_allocator<std::pair<const int, std::string> >()));
90 assert(!c.empty());
91 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
92 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
93 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
94 assert(c.max_load_factor() == 1);
95 }
96#if TEST_STD_VER > 11
97 {
98 typedef std::pair<int, std::string> P;
99 typedef test_allocator<std::pair<const int, std::string>> A;
100 typedef test_hash<int> HF;
101 typedef test_equal_to<int> Comp;
102 typedef std::unordered_map<int, std::string, HF, Comp, A> C;
103
104 A a(42);
105 C c ( {
106 P(1, "one"),
107 P(2, "two"),
108 P(3, "three"),
109 P(4, "four"),
110 P(1, "four"),
111 P(2, "four"),
112 }, 12, a);
113 assert(c.bucket_count() >= 12);
114 assert(c.size() == 4);
115 assert(c.at(1) == "one");
116 assert(c.at(2) == "two");
117 assert(c.at(3) == "three");
118 assert(c.at(4) == "four");
119 assert(c.hash_function() == test_hash<int>());
120 assert(c.key_eq() == test_equal_to<int>());
121 assert(c.get_allocator() == a);
122 assert(!c.empty());
123 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
124 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
125 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
126 assert(c.max_load_factor() == 1);
127 }
128 {
129 typedef std::pair<int, std::string> P;
130 typedef test_allocator<std::pair<const int, std::string>> A;
131 typedef test_hash<int> HF;
132 typedef test_equal_to<int> Comp;
133 typedef std::unordered_map<int, std::string, HF, Comp, A> C;
134
135 HF hf(42);
136 A a(43);
137 C c ( {
138 P(1, "one"),
139 P(2, "two"),
140 P(3, "three"),
141 P(4, "four"),
142 P(1, "four"),
143 P(2, "four"),
144 }, 12, hf, a);
145 assert(c.bucket_count() >= 12);
146 assert(c.size() == 4);
147 assert(c.at(1) == "one");
148 assert(c.at(2) == "two");
149 assert(c.at(3) == "three");
150 assert(c.at(4) == "four");
151 assert(c.hash_function() == hf);
152 assert(!(c.hash_function() == test_hash<int>()));
153 assert(c.key_eq() == test_equal_to<int>());
154 assert(c.get_allocator() == a);
155 assert(!c.empty());
156 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
157 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
158 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
159 assert(c.max_load_factor() == 1);
160 }
161#endif // TEST_STD_VER > 11
162
163 return 0;
164}
165

source code of libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/init.pass.cpp