forked from Alinshans/MyTinySTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.h
202 lines (188 loc) · 6.67 KB
/
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
#ifndef MYTINYSTL_MAP_TEST_H_
#define MYTINYSTL_MAP_TEST_H_
// map test : 测试 map, multimap 的接口与它们 insert 的性能
#include <map>
#include "../MyTinySTL/map.h"
#include "../MyTinySTL/vector.h"
#include "test.h"
namespace mystl
{
namespace test
{
namespace map_test
{
// pair 的宏定义
#define PAIR mystl::pair<int, int>
// map 的遍历输出
#define MAP_COUT(m) do { \
std::string m_name = #m; \
std::cout << " " << m_name << " :"; \
for (auto it : m) std::cout << " <" << it.first << "," << it.second << ">"; \
std::cout << std::endl; \
} while(0)
// map 的函数操作
#define MAP_FUN_AFTER(con, fun) do { \
std::string str = #fun; \
std::cout << " After " << str << " :" << std::endl; \
fun; \
MAP_COUT(con); \
} while(0)
// map 的函数值
#define MAP_VALUE(fun) do { \
std::string str = #fun; \
auto it = fun; \
std::cout << " " << str << " : <" << it.first << "," << it.second << ">\n"; \
} while(0)
void map_test()
{
std::cout << "[===============================================================]" << std::endl;
std::cout << "[------------------ Run container test : map -------------------]" << std::endl;
std::cout << "[-------------------------- API test ---------------------------]" << std::endl;
mystl::vector<PAIR> v;
for (int i = 0; i < 5; ++i)
v.push_back(PAIR(i, i));
mystl::map<int, int> m1;
mystl::map<int, int, mystl::greater<int>> m2;
mystl::map<int, int> m3(v.begin(), v.end());
mystl::map<int, int> m4(v.begin(), v.end());
mystl::map<int, int> m5(m3);
mystl::map<int, int> m6(std::move(m3));
mystl::map<int, int> m7;
m7 = m4;
mystl::map<int, int> m8;
m8 = std::move(m4);
mystl::map<int, int> m9{ PAIR(1,1),PAIR(3,2),PAIR(2,3) };
mystl::map<int, int> m10;
m10 = { PAIR(1,1),PAIR(3,2),PAIR(2,3) };
for (int i = 5; i > 0; --i)
{
MAP_FUN_AFTER(m1, m1.emplace(i, i));
}
MAP_FUN_AFTER(m1, m1.emplace_hint(m1.begin(), 0, 0));
MAP_FUN_AFTER(m1, m1.erase(m1.begin()));
MAP_FUN_AFTER(m1, m1.erase(0));
MAP_FUN_AFTER(m1, m1.erase(1));
MAP_FUN_AFTER(m1, m1.erase(m1.begin(), m1.end()));
for (int i = 0; i < 5; ++i)
{
MAP_FUN_AFTER(m1, m1.insert(PAIR(i, i)));
}
MAP_FUN_AFTER(m1, m1.insert(v.begin(), v.end()));
MAP_FUN_AFTER(m1, m1.insert(m1.end(), PAIR(5, 5)));
FUN_VALUE(m1.count(1));
MAP_VALUE(*m1.find(3));
MAP_VALUE(*m1.lower_bound(3));
MAP_VALUE(*m1.upper_bound(2));
auto first = *m1.equal_range(2).first;
auto second = *m1.equal_range(2).second;
std::cout << " m1.equal_range(2) : from <" << first.first << ", " << first.second
<< "> to <" << second.first << ", " << second.second << ">" << std::endl;
MAP_FUN_AFTER(m1, m1.erase(m1.begin()));
MAP_FUN_AFTER(m1, m1.erase(1));
MAP_FUN_AFTER(m1, m1.erase(m1.begin(), m1.find(3)));
MAP_FUN_AFTER(m1, m1.clear());
MAP_FUN_AFTER(m1, m1.swap(m9));
MAP_VALUE(*m1.begin());
MAP_VALUE(*m1.rbegin());
FUN_VALUE(m1[1]);
MAP_FUN_AFTER(m1, m1[1] = 3);
FUN_VALUE(m1.at(1));
std::cout << std::boolalpha;
FUN_VALUE(m1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(m1.size());
FUN_VALUE(m1.max_size());
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(map, LEN1 _L, LEN2 _L, LEN3 _L);
#else
MAP_EMPLACE_TEST(map, LEN1 _M, LEN2 _M, LEN3 _M);
#endif
std::cout << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
PASSED;
#endif
std::cout << "[------------------ End container test : map -------------------]" << std::endl;
}
void multimap_test()
{
std::cout << "[===============================================================]" << std::endl;
std::cout << "[---------------- Run container test : multimap ----------------]" << std::endl;
std::cout << "[-------------------------- API test ---------------------------]" << std::endl;
mystl::vector<PAIR> v;
for (int i = 0; i < 5; ++i)
v.push_back(PAIR(i, i));
mystl::multimap<int, int> m1;
mystl::multimap<int, int, mystl::greater<int>> m2;
mystl::multimap<int, int> m3(v.begin(), v.end());
mystl::multimap<int, int> m4(v.begin(), v.end());
mystl::multimap<int, int> m5(m3);
mystl::multimap<int, int> m6(std::move(m3));
mystl::multimap<int, int> m7;
m7 = m4;
mystl::multimap<int, int> m8;
m8 = std::move(m4);
mystl::multimap<int, int> m9{ PAIR(1,1),PAIR(3,2),PAIR(2,3) };
mystl::multimap<int, int> m10;
m10 = { PAIR(1,1),PAIR(3,2),PAIR(2,3) };
for (int i = 5; i > 0; --i)
{
MAP_FUN_AFTER(m1, m1.emplace(i, i));
}
MAP_FUN_AFTER(m1, m1.emplace_hint(m1.begin(), 0, 0));
MAP_FUN_AFTER(m1, m1.erase(m1.begin()));
MAP_FUN_AFTER(m1, m1.erase(0));
MAP_FUN_AFTER(m1, m1.erase(1));
MAP_FUN_AFTER(m1, m1.erase(m1.begin(), m1.end()));
for (int i = 0; i < 5; ++i)
{
MAP_FUN_AFTER(m1, m1.insert(mystl::make_pair(i, i)));
}
MAP_FUN_AFTER(m1, m1.insert(v.begin(), v.end()));
MAP_FUN_AFTER(m1, m1.insert(PAIR(5, 5)));
MAP_FUN_AFTER(m1, m1.insert(m1.end(), PAIR(5, 5)));
FUN_VALUE(m1.count(3));
MAP_VALUE(*m1.find(3));
MAP_VALUE(*m1.lower_bound(3));
MAP_VALUE(*m1.upper_bound(2));
auto first = *m1.equal_range(2).first;
auto second = *m1.equal_range(2).second;
std::cout << " m1.equal_range(2) : from <" << first.first << ", " << first.second
<< "> to <" << second.first << ", " << second.second << ">" << std::endl;
MAP_FUN_AFTER(m1, m1.erase(m1.begin()));
MAP_FUN_AFTER(m1, m1.erase(1));
MAP_FUN_AFTER(m1, m1.erase(m1.begin(), m1.find(3)));
MAP_FUN_AFTER(m1, m1.clear());
MAP_FUN_AFTER(m1, m1.swap(m9));
MAP_FUN_AFTER(m1, m1.insert(PAIR(3, 3)));
MAP_VALUE(*m1.begin());
MAP_VALUE(*m1.rbegin());
std::cout << std::boolalpha;
FUN_VALUE(m1.empty());
std::cout << std::noboolalpha;
FUN_VALUE(m1.size());
FUN_VALUE(m1.max_size());
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(multimap, LEN1 _M, LEN2 _M, LEN3 _M);
#else
MAP_EMPLACE_TEST(multimap, LEN1 _S, LEN2 _S, LEN3 _S);
#endif
std::cout << std::endl;
std::cout << "|---------------------|-------------|-------------|-------------|" << std::endl;
PASSED;
#endif
std::cout << "[---------------- End container test : multimap ----------------]" << std::endl;
}
} // namespace map_test
} // namespace test
} // namespace mystl
#endif // !MYTINYSTL_MAP_TEST_H_