-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_list.cpp
253 lines (203 loc) · 5.08 KB
/
test_list.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
#include <iostream>
#include <string>
#include "List.h"
using namespace std;
using namespace cop4530;
int main() {
// typedef List<int>::iterator ITRINT;
// typedef List<string>::iterator ITRSTR;
List<int> l1;
// ITRINT itr;
List<string> l2;
// ITRSTR itr2;
const int num = 10;
cout << "Testing list with integer values ..." << endl;
cout << "pushing back " << num << " integer values" << endl;
for (int i = 0; i < num; ++i) {
l1.push_back(i);
}
cout << "size " << l1.size() << endl;
auto itr = l1.begin();
cout << *itr++;
for (; itr != l1.end(); ++itr) {
cout << " " << *itr;
}
cout << endl;
cout << "pushing front " << num << " integer values" << endl;
for (int i = 0; i < num; ++i) {
l1.push_front(i);
}
cout << "size " << l1.size() << endl;
itr = l1.begin();
cout << *itr++;
for (; itr != l1.end(); ++itr) {
cout << " " << *itr;
}
cout << endl;
cout << "pop front " << num/2 << " integer values" << endl;
for (int i = 0; i < num/2; ++i) {
l1.pop_front();
}
cout << "size " << l1.size() << endl;
itr = l1.begin();
cout << *itr++;
for (; itr != l1.end(); ++itr) {
cout << " " << *itr;
}
cout << endl;
cout << "pop back " << num/2 << " integer values" << endl;
for (int i = 0; i < num/2; ++i) {
l1.pop_back();
}
cout << "size " << l1.size() << endl;
itr = l1.begin();
cout << *itr++;
for (; itr != l1.end(); ++itr) {
cout << " " << *itr;
}
cout << endl;
cout << "removing 0" << endl;
l1.remove(0);
cout << "size " << l1.size() << endl;
cout << l1 << endl;
cout << "removing first 4 elements" << endl;
itr = l1.begin();
auto itr_t = itr;
for (int i = 0; i < 4; ++i)
++itr_t;
l1.erase(itr, itr_t);
cout << "size " << l1.size() << endl;
cout << l1 << endl;
cout << "reverse list" << endl;
l1.reverse();
cout << "size " << l1.size() << endl;
cout << l1 << endl;
cout << "clearing list" << endl;
l1.clear();
if (l1.empty()) {
cout << "all cleared" << endl;
} else {
cout << "wrong with clear() function" << endl;
}
cout << "testing other constructors" << endl;
List<int> l_t1(8, 5);
List<int> l_t2(l_t1.begin(), l_t1.end());
cout << l_t2 << endl;
cout << "testing comparison operators" << endl;
if (l_t1 == l_t2) {
cout << "they are the same" << endl;
} else {
cout << "wrong" << endl;
}
cout << "remove one element from l_t1" << endl;
l_t1.pop_back();
if (l_t1 == l_t2) {
cout << "wronng" << endl;
} else {
cout << "they contain different values" << endl;
}
l_t2.push_back(10);
// testing move
List<int> l_t3 = std::move(l_t2);
for (auto & x : l_t3) {
cout << x << " ";
}
cout << endl;
// check if l_t2 is empty now
// NOTE that we should NOT access l_t2
// this is just for the purpose of testing
// the implementation of move constructor
if (!l_t2.empty()) {
cout << "wrong" << endl;
} else {
cout << "l_t2 is empty now" << endl;
}
/* testing string list */
cout << "Testing list with string values ..." << endl;
cout << "pushing back " << num << " string values" << endl;
for (int i = 1; i <= num; ++i) {
string str(i, '0');
l2.push_back(str);
}
cout << "size " << l2.size() << endl;
auto itr2 = l2.begin();
cout << *itr2++;
for (; itr2 != l2.end(); ++itr2) {
cout << " " << *itr2;
}
cout << endl;
cout << "pushing front " << num << " integer values" << endl;
for (int i = 1; i <= num; ++i) {
string str(i, '1');
l2.push_front(str);
}
cout << "size " << l2.size() << endl;
itr2 = l2.begin();
cout << *itr2++;
for (; itr2 != l2.end(); ++itr2) {
cout << " " << *itr2;
}
cout << endl;
cout << "pop front " << num/2 << " string values" << endl;
for (int i = 0; i < num/2; ++i) {
l2.pop_front();
}
cout << "size " << l2.size() << endl;
itr2 = l2.begin();
cout << *itr2++;
for (; itr2 != l2.end(); ++itr2) {
cout << " " << *itr2;
}
cout << endl;
cout << "pop back " << num/2 << " string values" << endl;
for (int i = 0; i < num/2; ++i) {
l2.pop_back();
}
cout << "size " << l2.size() << endl;
itr2 = l2.begin();
cout << *itr2++;
for (; itr2 != l2.end(); ++itr2) {
cout << " " << *itr2;
}
cout << endl;
cout << "removing 0" << endl;
l2.remove("0");
cout << "size " << l2.size() << endl;
cout << l2 << endl;
cout << "removing first 4 elements" << endl;
itr2 = l2.begin();
auto itr2_t = itr2;
for (int i = 0; i < 4; ++i)
++itr2_t;
l2.erase(itr2, itr2_t);
cout << "size " << l2.size() << endl;
cout << l2 << endl;
cout << "reverse list" << endl;
l2.reverse();
cout << "size " << l2.size() << endl;
cout << l2 << endl;
cout << "clearing list" << endl;
l2.clear();
if (l2.empty()) {
cout << "all cleared" << endl;
} else {
cout << "wrong with clear() function" << endl;
}
cout << "testing other constructors" << endl;
List<string> l2_t1(8, "2");
List<string> l2_t2(l2_t1.begin(), l2_t1.end());
cout << l2_t2 << endl;
cout << "testing comparison operators" << endl;
if (l2_t1 == l2_t2) {
cout << "they are the same" << endl;
} else {
cout << "wrong" << endl;
}
cout << "remove one element from l2_t1" << endl;
l2_t1.pop_back();
if (l2_t1 == l2_t2) {
cout << "wronng" << endl;
} else {
cout << "they contain different values" << endl;
}
}