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 typedef std::unordered_map<int,
35 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() == (test_allocator<std::pair<const int, std::string> >()));
58 assert(!c.empty());
59 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
60 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
61 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
62 assert(c.max_load_factor() == 1);
63 }
64 {
65 typedef std::unordered_map<int,
66 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() == (min_allocator<std::pair<const int, std::string> >()));
89 assert(!c.empty());
90 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
91 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
92 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
93 assert(c.max_load_factor() == 1);
94 }
95#if TEST_STD_VER > 11
96 {
97 typedef std::pair<int, std::string> P;
98 typedef test_allocator<std::pair<const int, std::string>> A;
99 typedef test_hash<int> HF;
100 typedef test_equal_to<int> Comp;
101 typedef std::unordered_map<int, std::string, HF, Comp, A> C;
102
103 A a(42);
104 C c(
105 {
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 },
113 12,
114 a);
115 assert(c.bucket_count() >= 12);
116 assert(c.size() == 4);
117 assert(c.at(1) == "one");
118 assert(c.at(2) == "two");
119 assert(c.at(3) == "three");
120 assert(c.at(4) == "four");
121 assert(c.hash_function() == test_hash<int>());
122 assert(c.key_eq() == test_equal_to<int>());
123 assert(c.get_allocator() == a);
124 assert(!c.empty());
125 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
126 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
127 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
128 assert(c.max_load_factor() == 1);
129 }
130 {
131 typedef std::pair<int, std::string> P;
132 typedef test_allocator<std::pair<const int, std::string>> A;
133 typedef test_hash<int> HF;
134 typedef test_equal_to<int> Comp;
135 typedef std::unordered_map<int, std::string, HF, Comp, A> C;
136
137 HF hf(42);
138 A a(43);
139 C c(
140 {
141 P(1, "one"),
142 P(2, "two"),
143 P(3, "three"),
144 P(4, "four"),
145 P(1, "four"),
146 P(2, "four"),
147 },
148 12,
149 hf,
150 a);
151 assert(c.bucket_count() >= 12);
152 assert(c.size() == 4);
153 assert(c.at(1) == "one");
154 assert(c.at(2) == "two");
155 assert(c.at(3) == "three");
156 assert(c.at(4) == "four");
157 assert(c.hash_function() == hf);
158 assert(!(c.hash_function() == test_hash<int>()));
159 assert(c.key_eq() == test_equal_to<int>());
160 assert(c.get_allocator() == a);
161 assert(!c.empty());
162 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
163 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
164 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
165 assert(c.max_load_factor() == 1);
166 }
167#endif // TEST_STD_VER > 11
168
169 return 0;
170}
171

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