1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
16 // template <class InputIterator>
17 // unordered_map(InputIterator first, InputIterator last, size_type n,
18 // const hasher& hf, const key_equal& eql,
19 // const allocator_type& a);
21 #include <unordered_map>
28 #include "test_macros.h"
29 #include "test_iterators.h"
30 #include "../../../NotConstructible.h"
31 #include "../../../test_compare.h"
32 #include "../../../test_hash.h"
33 #include "test_allocator.h"
34 #include "min_allocator.h"
39 typedef std::unordered_map<int, std::string,
40 test_hash<std::hash<int> >,
41 test_compare<std::equal_to<int> >,
42 test_allocator<std::pair<const int, std::string> >
44 typedef std::pair<int, std::string> P;
54 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
56 test_hash<std::hash<int> >(8),
57 test_compare<std::equal_to<int> >(9),
58 test_allocator<std::pair<const int, std::string> >(10)
60 LIBCPP_ASSERT(c.bucket_count() == 7);
61 assert(c.size() == 4);
62 assert(c.at(1) == "one");
63 assert(c.at(2) == "two");
64 assert(c.at(3) == "three");
65 assert(c.at(4) == "four");
66 assert(c.hash_function() == test_hash<std::hash<int> >(8));
67 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
68 assert(c.get_allocator() ==
69 (test_allocator<std::pair<const int, std::string> >(10)));
71 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
72 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
73 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
74 assert(c.max_load_factor() == 1);
76 #if TEST_STD_VER >= 11
78 typedef std::unordered_map<int, std::string,
79 test_hash<std::hash<int> >,
80 test_compare<std::equal_to<int> >,
81 min_allocator<std::pair<const int, std::string> >
83 typedef std::pair<int, std::string> P;
93 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
95 test_hash<std::hash<int> >(8),
96 test_compare<std::equal_to<int> >(9),
97 min_allocator<std::pair<const int, std::string> >()
99 LIBCPP_ASSERT(c.bucket_count() == 7);
100 assert(c.size() == 4);
101 assert(c.at(1) == "one");
102 assert(c.at(2) == "two");
103 assert(c.at(3) == "three");
104 assert(c.at(4) == "four");
105 assert(c.hash_function() == test_hash<std::hash<int> >(8));
106 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
107 assert(c.get_allocator() ==
108 (min_allocator<std::pair<const int, std::string> >()));
110 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
111 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
112 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
113 assert(c.max_load_factor() == 1);
116 typedef explicit_allocator<std::pair<const int, std::string>> A;
117 typedef std::unordered_map<int, std::string,
118 test_hash<std::hash<int> >,
119 test_compare<std::equal_to<int> >,
122 typedef std::pair<int, std::string> P;
132 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
134 test_hash<std::hash<int> >(8),
135 test_compare<std::equal_to<int> >(9),
138 LIBCPP_ASSERT(c.bucket_count() == 7);
139 assert(c.size() == 4);
140 assert(c.at(1) == "one");
141 assert(c.at(2) == "two");
142 assert(c.at(3) == "three");
143 assert(c.at(4) == "four");
144 assert(c.hash_function() == test_hash<std::hash<int> >(8));
145 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
146 assert(c.get_allocator() == A{});
148 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
149 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
150 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
151 assert(c.max_load_factor() == 1);