forked from Alinshans/MyTinySTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unordered_map_test.h
203 lines (193 loc) · 8.01 KB
/
unordered_map_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef MYTINYSTL_UNORDERED_MAP_TEST_H_
#define MYTINYSTL_UNORDERED_MAP_TEST_H_
// unordered_map test : 测试 unordered_map, unordered_multimap 的接口与它们 insert 的性能
#include <unordered_map>
#include "../MyTinySTL/unordered_map.h"
#include "map_test.h"
#include "test.h"
namespace mystl
{
namespace test
{
namespace unordered_map_test
{
void unordered_map_test()
{
std::cout << "[===============================================================]" << std::endl;
std::cout << "[-------------- Run container test : unordered_map -------------]" << std::endl;
std::cout << "[-------------------------- API test ---------------------------]" << std::endl;
mystl::vector<PAIR> v;
for (int i = 0; i < 5; ++i)
v.push_back(PAIR(5 - i, 5 - i));
mystl::unordered_map<int, int> um1;
mystl::unordered_map<int, int> um2(520);
mystl::unordered_map<int, int> um3(520, mystl::hash<int>());
mystl::unordered_map<int, int> um4(520, mystl::hash<int>(), mystl::equal_to<int>());
mystl::unordered_map<int, int> um5(v.begin(), v.end());
mystl::unordered_map<int, int> um6(v.begin(), v.end(), 100);
mystl::unordered_map<int, int> um7(v.begin(), v.end(), 100, mystl::hash<int>());
mystl::unordered_map<int, int> um8(v.begin(), v.end(), 100, mystl::hash<int>(), mystl::equal_to<int>());
mystl::unordered_map<int, int> um9(um5);
mystl::unordered_map<int, int> um10(std::move(um5));
mystl::unordered_map<int, int> um11;
um11 = um6;
mystl::unordered_map<int, int> um12;
um12 = std::move(um6);
mystl::unordered_map<int, int> um13{ PAIR(1,1),PAIR(2,3),PAIR(3,3) };
mystl::unordered_map<int, int> um14;
um14 = { PAIR(1,1),PAIR(2,3),PAIR(3,3) };
MAP_FUN_AFTER(um1, um1.emplace(1, 1));
MAP_FUN_AFTER(um1, um1.emplace_hint(um1.begin(), 1, 2));
MAP_FUN_AFTER(um1, um1.insert(PAIR(2, 2)));
MAP_FUN_AFTER(um1, um1.insert(um1.end(), PAIR(3, 3)));
MAP_FUN_AFTER(um1, um1.insert(v.begin(), v.end()));
MAP_FUN_AFTER(um1, um1.erase(um1.begin()));
MAP_FUN_AFTER(um1, um1.erase(um1.begin(), um1.find(3)));
MAP_FUN_AFTER(um1, um1.erase(1));
std::cout << std::boolalpha;
FUN_VALUE(um1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(um1.size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.max_bucket_count());
FUN_VALUE(um1.bucket(1));
FUN_VALUE(um1.bucket_size(um1.bucket(5)));
MAP_FUN_AFTER(um1, um1.clear());
MAP_FUN_AFTER(um1, um1.swap(um7));
MAP_VALUE(*um1.begin());
FUN_VALUE(um1.at(1));
FUN_VALUE(um1[1]);
std::cout << std::boolalpha;
FUN_VALUE(um1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(um1.size());
FUN_VALUE(um1.max_size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.max_bucket_count());
FUN_VALUE(um1.bucket(1));
FUN_VALUE(um1.bucket_size(um1.bucket(1)));
MAP_FUN_AFTER(um1, um1.reserve(1000));
FUN_VALUE(um1.size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.bucket_size(1));
FUN_VALUE(um1.bucket_size(2));
FUN_VALUE(um1.bucket_size(3));
MAP_FUN_AFTER(um1, um1.rehash(150));
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.count(1));
MAP_VALUE(*um1.find(3));
auto first = *um1.equal_range(3).first;
auto second = *um1.equal_range(3).second;
std::cout << " um1.equal_range(3) : from <" << first.first << ", " << first.second
<< "> to <" << second.first << ", " << second.second << ">" << std::endl;
FUN_VALUE(um1.load_factor());
FUN_VALUE(um1.max_load_factor());
MAP_FUN_AFTER(um1, um1.max_load_factor(1.5f));
FUN_VALUE(um1.max_load_factor());
PASSED;
#if PERFORMANCE_TEST_ON
std::cout << "[--------------------- Performance Testing ---------------------]" << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
std::cout << "| emplace |";
#if LARGER_TEST_DATA_ON
MAP_EMPLACE_TEST(unordered_map, SCALE_M(LEN1), SCALE_M(LEN2), SCALE_M(LEN3));
#else
MAP_EMPLACE_TEST(unordered_map, SCALE_S(LEN1), SCALE_S(LEN2), SCALE_S(LEN3));
#endif
std::cout << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
PASSED;
#endif
std::cout << "[-------------- End container test : unordered_map -------------]" << std::endl;
}
void unordered_multimap_test()
{
std::cout << "[===============================================================]" << std::endl;
std::cout << "[----------- Run container test : unordered_multimap -----------]" << std::endl;
std::cout << "[-------------------------- API test ---------------------------]" << std::endl;
mystl::vector<PAIR> v;
for (int i = 0; i < 5; ++i)
v.push_back(PAIR(5 - i, 5 - i));
mystl::unordered_multimap<int, int> um1;
mystl::unordered_multimap<int, int> um2(520);
mystl::unordered_multimap<int, int> um3(520, mystl::hash<int>());
mystl::unordered_multimap<int, int> um4(520, mystl::hash<int>(), mystl::equal_to<int>());
mystl::unordered_multimap<int, int> um5(v.begin(), v.end());
mystl::unordered_multimap<int, int> um6(v.begin(), v.end(), 100);
mystl::unordered_multimap<int, int> um7(v.begin(), v.end(), 100, mystl::hash<int>());
mystl::unordered_multimap<int, int> um8(v.begin(), v.end(), 100, mystl::hash<int>(), mystl::equal_to<int>());
mystl::unordered_multimap<int, int> um9(um5);
mystl::unordered_multimap<int, int> um10(std::move(um5));
mystl::unordered_multimap<int, int> um11;
um11 = um6;
mystl::unordered_multimap<int, int> um12;
um12 = std::move(um6);
mystl::unordered_multimap<int, int> um13{ PAIR(1,1),PAIR(2,3),PAIR(3,3) };
mystl::unordered_multimap<int, int> um14;
um14 = { PAIR(1,1),PAIR(2,3),PAIR(3,3) };
MAP_FUN_AFTER(um1, um1.emplace(1, 1));
MAP_FUN_AFTER(um1, um1.emplace_hint(um1.begin(), 1, 2));
MAP_FUN_AFTER(um1, um1.insert(PAIR(2, 2)));
MAP_FUN_AFTER(um1, um1.insert(um1.end(), PAIR(3, 3)));
MAP_FUN_AFTER(um1, um1.insert(v.begin(), v.end()));
MAP_FUN_AFTER(um1, um1.erase(um1.begin()));
MAP_FUN_AFTER(um1, um1.erase(um1.begin(), um1.find(3)));
MAP_FUN_AFTER(um1, um1.erase(1));
std::cout << std::boolalpha;
FUN_VALUE(um1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(um1.size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.max_bucket_count());
FUN_VALUE(um1.bucket(1));
FUN_VALUE(um1.bucket_size(um1.bucket(5)));
MAP_FUN_AFTER(um1, um1.clear());
MAP_FUN_AFTER(um1, um1.swap(um7));
MAP_VALUE(*um1.begin());
std::cout << std::boolalpha;
FUN_VALUE(um1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(um1.size());
FUN_VALUE(um1.max_size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.max_bucket_count());
FUN_VALUE(um1.bucket(1));
FUN_VALUE(um1.bucket_size(um1.bucket(1)));
MAP_FUN_AFTER(um1, um1.reserve(1000));
FUN_VALUE(um1.size());
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.bucket_size(1));
FUN_VALUE(um1.bucket_size(2));
FUN_VALUE(um1.bucket_size(3));
MAP_FUN_AFTER(um1, um1.rehash(150));
FUN_VALUE(um1.bucket_count());
FUN_VALUE(um1.count(1));
MAP_VALUE(*um1.find(3));
auto first = *um1.equal_range(3).first;
auto second = *um1.equal_range(3).second;
std::cout << " um1.equal_range(3) : from <" << first.first << ", " << first.second
<< "> to <" << second.first << ", " << second.second << ">" << std::endl;
FUN_VALUE(um1.load_factor());
FUN_VALUE(um1.max_load_factor());
MAP_FUN_AFTER(um1, um1.max_load_factor(1.5f));
FUN_VALUE(um1.max_load_factor());
PASSED;
#if PERFORMANCE_TEST_ON
std::cout << "[--------------------- Performance Testing ---------------------]" << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
std::cout << "| emplace |";
#if LARGER_TEST_DATA_ON
MAP_EMPLACE_TEST(unordered_multimap, SCALE_M(LEN1), SCALE_M(LEN2), SCALE_M(LEN3));
#else
MAP_EMPLACE_TEST(unordered_multimap, SCALE_S(LEN1), SCALE_S(LEN2), SCALE_S(LEN3));
#endif
std::cout << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
PASSED;
#endif
std::cout << "[----------- End container test : unordered_multimap -----------]" << std::endl;
}
} // namespace unordered_map_test
} // namespace test
} // namespace mystl
#endif // !MYTINYSTL_UNORDERED_MAP_TEST_H_