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

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