-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
292 lines (234 loc) · 4.54 KB
/
test.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "safelist.hpp"
#include <cassert>
#include <iostream>
#include <iterator>
#include <list>
#include <typeinfo>
template<class T>
void print_list(const T& l)
{
typename T::size_type s = 0;
for (auto x : l) {
++s;
std::cout << x << std::endl;
}
assert(s == l.size());
}
template<class T>
void test_iters(T& sl)
{
std::cout << "Testing iteration methods" << std::endl;
// Test prefix increment
print_list(sl);
// Test postfix increment
for (auto it = sl.begin(); it != sl.end(); it++) {
std::cout << *it << std::endl;
}
// Test reverse iterators
for (auto it = sl.rbegin(); it != sl.rend(); ++it) {
std::cout << *it << std::endl;
}
// Test reverse iteration
auto it = sl.end();
do {
std::cout << *(--it) << std::endl;
} while (it != sl.begin());
}
template<class T>
void test_access(T& sl)
{
std::cout << sl.front() << std::endl;
std::cout << sl.back() << std::endl;
}
template<class T>
void test_compare()
{
std::cout << "Testing comparison operators" << std::endl;
T t1 = {1, 2, 3, 4};
T t2 = {1, 2, 4, 3};
assert(t1 == t1);
assert(t1 != t2);
assert(t1 < t2);
assert(t1 <= t2);
assert(t2 > t1);
assert(t2 >= t1);
}
template<class T>
void setup_list(T& l)
{
l.push_front(2);
l.push_front(1);
l.push_back(3);
}
template<class T>
void test_emplace()
{
T t = {1, 2, 3};
t.emplace(t.end(), 4);
print_list(t);
t.emplace(++(++t.begin()), 0);
print_list(t);
t.emplace_front(-1);
t.emplace_back(5);
print_list(t);
}
template<class T>
void test_erase()
{
std::cout << "Testing erase funciton" << std::endl;
T t = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
t.erase(t.begin());
print_list(t);
auto lBegin = t.begin();
std::advance(lBegin, 6);
t.erase(++t.begin(), lBegin);
print_list(t);
}
template<class T>
void test_constructors()
{
std::cout << "Testing different constructors" << std::endl;
print_list(T(12));
print_list(T({4, 8, 16, 2, 32}));
print_list(T(5, 28));
T t1 = {4, 8, 16, 2, 32};
T t2 = t1;
print_list(t2);
}
template<class T>
void test_pop()
{
std::cout << "testing pop_back" << std::endl;
T t = {1, 2, 3, 4};
while (!t.empty())
{
print_list(t);
t.pop_back();
}
std::cout << "testing pop_front" << std::endl;
t = T({4, 3, 2, 1});
while (!t.empty())
{
print_list(t);
t.pop_front();
}
}
template<class T>
void test_sizing()
{
std::cout << "Testing sizing functions" << std::endl;
T t;
std::cout << t.size() << std::endl;
t.resize(2);
std::cout << t.size() << std::endl;
print_list(t);
t.resize(8, 4);
std::cout << t.size() << std::endl;
print_list(t);
t.resize(3);
std::cout << t.size() << std::endl;
print_list(t);
}
template<class T>
void test_sorting()
{
std::cout << "Testing sorting functions" << std::endl;
T t = {7,1,8,4,4,7,1,5,2};
t.sort();
print_list(t);
t.sort(std::greater<typename T::value_type>());
print_list(t);
}
template<class T>
void test_unique()
{
std::cout << "Testing unique" << std::endl;
T t = {8, 1, 5, 5, 2, 2, 3, 1, 7};
t.unique();
print_list(t);
t = {8, 1, 5, 5, 2, 2, 3, 1, 7};
t.unique(std::not_equal_to<typename T::value_type>());
print_list(t);
}
template<class T>
void test_reverse()
{
std::cout << "Testing list reversing" << std::endl;
T t = {1, 2, 3, 4, 5};
t.reverse();
print_list(t);
t = {5, 6, 7, 8, 9, 10};
t.reverse();
print_list(t);
}
template<class T>
void test_remove()
{
std::cout << "Testing list removal" << std::endl;
T t = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
t.remove(7);
print_list(t);
t.remove_if([] (int a) { return a % 2 == 0; });
print_list(t);
}
template<class T>
void test_merge()
{
T t1 = {0, 2, 4, 6, 8}, t2 = {1, 3, 5, 7, 9};
t1.merge(t2);
print_list(t1);
print_list(t2);
}
template<class T>
void test_insert()
{
T t;
t.insert(t.begin(), 12);
t.insert(t.begin(), 13);
t.insert(++t.begin(), {11, 12, 13, 14});
t.insert(++t.begin(), 60, 2);
print_list(t);
}
template<class T>
void test_splice()
{
std::cout << "Testing splicing" << std::endl;
T t1 = {1, 4, 5, 6, 7};
T t2 = {2, 3};
t1.splice(++t1.begin(), t2);
print_list(t1);
print_list(t2);
T t3 = {8, 9};
t1.splice(t1.end(), t3, t3.begin(), t3.end());
print_list(t1);
print_list(t3);
}
template<class T>
void test()
{
T t;
setup_list(t);
test_iters(t);
test_iters((const T) t);
test_constructors<T>();
test_access(t);
test_access((const T) t);
test_pop<T>();
test_sizing<T>();
test_sorting<T>();
test_erase<T>();
test_unique<T>();
test_reverse<T>();
test_remove<T>();
test_merge<T>();
test_splice<T>();
}
int main(int argc, char**)
{
if (argc == 2) {
test<std::list<int>>();
} else {
test<safelist<int>>();
}
return 0;
}