blob: 091a72f0d1600bcef71225f65fac593e7ac2f887 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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(unordered_map&& u, const allocator_type& a);
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000021#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
23#include "../../../test_compare.h"
24#include "../../../test_hash.h"
Marshall Clow1b921882013-12-03 00:18:10 +000025#include "test_allocator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000026#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027
28int main()
29{
Howard Hinnant73d21a42010-09-04 23:28:19 +000030#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031 {
32 typedef std::pair<int, std::string> P;
33 typedef test_allocator<std::pair<const int, std::string>> A;
34 typedef std::unordered_map<int, std::string,
35 test_hash<std::hash<int> >,
36 test_compare<std::equal_to<int> >,
37 A
38 > C;
39 P a[] =
40 {
41 P(1, "one"),
42 P(2, "two"),
43 P(3, "three"),
44 P(4, "four"),
45 P(1, "four"),
46 P(2, "four"),
47 };
48 C c0(a, a + sizeof(a)/sizeof(a[0]),
49 7,
50 test_hash<std::hash<int> >(8),
51 test_compare<std::equal_to<int> >(9),
52 A(10)
53 );
54 C c(std::move(c0), A(12));
55 assert(c.bucket_count() >= 5);
56 assert(c.size() == 4);
57 assert(c.at(1) == "one");
58 assert(c.at(2) == "two");
59 assert(c.at(3) == "three");
60 assert(c.at(4) == "four");
61 assert(c.hash_function() == test_hash<std::hash<int> >(8));
62 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
63 assert(c.get_allocator() == A(12));
64 assert(!c.empty());
65 assert(std::distance(c.begin(), c.end()) == c.size());
66 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -070067 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068 assert(c.max_load_factor() == 1);
69
70 assert(c0.empty());
71 }
72 {
73 typedef std::pair<int, std::string> P;
74 typedef test_allocator<std::pair<const int, std::string>> A;
75 typedef std::unordered_map<int, std::string,
76 test_hash<std::hash<int> >,
77 test_compare<std::equal_to<int> >,
78 A
79 > C;
80 P a[] =
81 {
82 P(1, "one"),
83 P(2, "two"),
84 P(3, "three"),
85 P(4, "four"),
86 P(1, "four"),
87 P(2, "four"),
88 };
89 C c0(a, a + sizeof(a)/sizeof(a[0]),
90 7,
91 test_hash<std::hash<int> >(8),
92 test_compare<std::equal_to<int> >(9),
93 A(10)
94 );
95 C c(std::move(c0), A(10));
96 assert(c.bucket_count() == 7);
97 assert(c.size() == 4);
98 assert(c.at(1) == "one");
99 assert(c.at(2) == "two");
100 assert(c.at(3) == "three");
101 assert(c.at(4) == "four");
102 assert(c.hash_function() == test_hash<std::hash<int> >(8));
103 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
104 assert(c.get_allocator() == A(10));
105 assert(!c.empty());
106 assert(std::distance(c.begin(), c.end()) == c.size());
107 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700108 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109 assert(c.max_load_factor() == 1);
110
111 assert(c0.empty());
112 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000113#if __cplusplus >= 201103L
114 {
115 typedef std::pair<int, std::string> P;
116 typedef min_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> >,
120 A
121 > C;
122 P a[] =
123 {
124 P(1, "one"),
125 P(2, "two"),
126 P(3, "three"),
127 P(4, "four"),
128 P(1, "four"),
129 P(2, "four"),
130 };
131 C c0(a, a + sizeof(a)/sizeof(a[0]),
132 7,
133 test_hash<std::hash<int> >(8),
134 test_compare<std::equal_to<int> >(9),
135 A()
136 );
137 C c(std::move(c0), A());
138 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());
147 assert(!c.empty());
148 assert(std::distance(c.begin(), c.end()) == c.size());
149 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700150 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000151 assert(c.max_load_factor() == 1);
152
153 assert(c0.empty());
154 }
155#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +0000156#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157}