-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedTest.cpp
375 lines (334 loc) · 8.48 KB
/
ExtendedTest.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "ExtendedTest.h"
#include <assert.h>
#include <exception>
#include <cstdlib>
#include <vector>
#include <iostream>
#include "ListIterator.h"
#include "SortedIndexedList.h"
using namespace std;
//order relation - ascending
bool asc(TComp c1, TComp c2) {
if (c1 <= c2) {
return true;
} else {
return false;
}
}
//order relation - descending
bool desc(TComp c1, TComp c2) {
if (c1 >= c2) {
return true;
} else {
return false;
}
}
void test_remove_by_step(){
cout<<"Test function"<<endl;
SortedIndexedList list = SortedIndexedList(asc);
list.add(10);
list.add(12);
list.add(30);
list.add(34);
list.add(50);
list.add(78);
assert(list.size() == 6);
list.remove_by_step(2);
assert(list.size() == 3);
assert(list.search(12) == -1);
assert(list.search(34) == -1);
assert(list.search(78) == -1);
}
void testIteratorSteps(SortedIndexedList& list, Relation r) {
int count = 0;
ListIterator li = list.iterator();
TComp prev = li.getCurrent();
if (li.valid()) {
count++;
li.next();
}
while (li.valid()) {
TComp current = li.getCurrent();
assert(r(prev, current));
count++;
li.next();
prev = current;
}
assert(count == list.size());
}
void testCreate() {
cout << "Test create" << endl;
SortedIndexedList list = SortedIndexedList(asc);
assert(list.size() == 0);
assert(list.isEmpty());
ListIterator it = list.iterator();
assert(!it.valid());
it.first();
for (int i = 0; i < 10; i++) {
assert(!it.valid());
assert(list.search(i) == -1);
try {
assert(list.getElement(i));
assert(false);
} catch (exception&) {
assert(true);
}
try {
assert(list.remove(i));
assert(false);
} catch (exception&) {
assert(true);
}
try {
it.next();
assert(false);
}
catch (exception&) {
assert(true);
}
}
}
//generate a vector with values between cMin and cMax so that
//1) no value that is >=cMin and <=cMax which is a multiple of s is not included
//2) values v, v>=cMin and v<=cMax which are a multiple of m (but not of s) are included c/m + 1 times
//3) values >=cMin and <=cMax are in random order
vector<int> random(int cMin, int cMax, int s, int m) {
vector<int> v;
for (int c = cMin; c <= cMax; c++) {
if (c % s != 0) {
v.push_back(c);
if (c % m == 0) {
for (int j = 0; j < c / m; j++) {
v.push_back(c);
}
}
}
}
int n = v.size();
for (int i = 0; i < n - 1; i++) {
int j = i + rand() % (n - i);
swap(v[i], v[j]);
}
return v;
}
//generate a vector containing values >=cMin and <=cMax, each included one time, in random order
vector<int> random(int cMin, int cMax) {
vector<int> v;
for (int c = cMin; c <= cMax; c++) {
v.push_back(c);
}
int n = v.size();
for (int i = 0; i < n - 1; i++) {
int j = i + rand() % (n - i);
swap(v[i], v[j]);
}
return v;
}
//populate the sorted list with values >=cMin and <=cMax a.i.:
//1) no value that is >=cMin and <=cMax which is a multiple of s is not included
//2) values v, v>=cMin and v<=cMax which are a multiple of m (but not of s) are included c/m + 1 times
//3) values >=cMin and <=cMax are in random order
int populate(SortedIndexedList& list, int cMin, int cMax, int s, int m) {
vector<int> v = random(cMin, cMax, s, m);
int n = v.size();
for (int i = 0; i < n; i++) {
list.add(v[i]);
}
return n;
}
//populate the sorted list with values >=cMin and <=cMax, each included one time, in random order
void populate(SortedIndexedList& list, int cMin, int cMax) {
vector<int> v = random(cMin, cMax);
int n = v.size();
for (int i = 0; i < n; i++) {
list.add(v[i]);
}
}
void testAddAndSearch(Relation r) {
cout << "Test add and search" << endl;
SortedIndexedList list = SortedIndexedList(r);
int vMin = 10;
int vMax = 30;
int s = 5;
int m = 3;
int n = populate(list, vMin, vMax, s, m);
assert(!list.isEmpty());
assert(list.size() == n);
//we can't find values outside the interval or on invalid positions
int d = 30;
for (int i = 1; i <= d; i++) {
assert(list.search(vMin - i) == -1);
assert(list.search(vMax + i) == -1);
try{
list.getElement(n-1+i);
list.getElement(-d);
assert(false);
} catch (exception&) {
assert(true);
}
}
//check the relation order
ListIterator it = list.iterator();
it.first();
assert(it.valid());
TComp prev = it.getCurrent();
it.next();
while (it.valid()) {
TComp current = it.getCurrent();
assert(r(prev, current));
prev = current;
it.next();
}
testIteratorSteps(list, r);
//check if added values can be found
for (int v = vMin; v <= vMax; v++) {
int p = list.search(v);
//we can't find values which are a multiple of s
assert((p != -1) == (v % s != 0));
//values which are a multiple of m can be found exactly v/m+1 times
if (p != -1 && v%m == 0){
for (int i=0; i<=v/m; i++){
try{
assert(list.remove(p) == v);
} catch (exception&) {
assert(false);
}
}
assert(list.search(v) == -1);
}
}
}
void testDeleteSearch(Relation r) {
cout << "Test delete and search" << endl;
SortedIndexedList list = SortedIndexedList(r);
int vMin = 0;
int vMax = 100;
populate(list, vMin, vMax);
int d = 30;
for (int i = 1; i <= d; i++) {
try {
list.remove(list.search(vMax + i));
assert(false);
} catch (exception&) {
assert(true);
}
}
assert(!list.isEmpty());
assert(list.size() == vMax - vMin + 1);
ListIterator it1 = list.iterator();
assert(it1.valid());
it1.first();
assert(it1.valid());
TComp deleted = NULL_TCOMP;
testIteratorSteps(list, r);
try {
deleted = list.remove(list.search(it1.getCurrent()));
assert(list.size() == vMax - vMin);
it1.first();
TComp newFirst = it1.getCurrent();
assert(newFirst != deleted);
assert(r(deleted, newFirst));
it1.first();
ListIterator it2 = list.iterator();
assert(it2.valid());
it2.first();
assert(it2.valid());
assert(it1.getCurrent() == newFirst && it2.getCurrent() == newFirst);
} catch (exception&) {
assert(false);
}
//delete values in random order while checking the relation order
vector<int> vs = random(vMin, vMax);
int n = vs.size();
for (int i = 0; i < n; i++) {
testIteratorSteps(list, r);
int v = vs[i];
try {
int it1 = list.search(v);
TComp deletedCurrent = list.remove(it1);
assert(deletedCurrent != deleted);
assert(deletedCurrent == v);
assert(list.search(v) == -1);
if (!list.isEmpty()) {
ListIterator it2 = list.iterator();
it2.first();
assert(it2.valid());
TComp prev = it2.getCurrent();
while (it2.valid()) {
TComp current = list.getElement(list.search(it2.getCurrent()));
assert(r(prev, current));
assert(!r(deletedCurrent, current) || !r(current, deletedCurrent));
prev = current;
it2.next();
}
}
} catch (exception&) {
assert(v == deleted);
}
}
assert(list.isEmpty());
assert(list.size() == 0);
}
void testDeleteSearch() {
testDeleteSearch(asc);
testDeleteSearch(desc);
}
void testAddAndSearch() {
testAddAndSearch(asc);
testAddAndSearch(desc);
}
void testQuantity(){
cout << "Test quantity" << endl;
SortedIndexedList list = SortedIndexedList(asc);
int vMin = 3000;
int vMax = 6000;
vector<int> values = random(vMin, vMax);
int n = values.size();
for (int i = 0; i < n; i++){
list.add(values[i]);
}
assert(list.size() == vMax - vMin + 1);
for (int v = vMin; v <= vMax; v++){
assert(list.search(v)>= 0 && list.search(v) < list.size());
assert(list.getElement(list.search(v)) == v);
}
ListIterator it = list.iterator();
it.first();
assert(it.valid());
TComp firstElement = it.getCurrent();
it.first();
assert(it.valid());
assert(it.getCurrent() == firstElement);
for (int i = 0; i < list.size(); i++) {
it.next();
}
assert(!it.valid());
it.first();
while (it.valid()){
TComp v = it.getCurrent();
assert(vMin <= v && v<=vMax);
it.next();
}
assert(!it.valid());
int d = 100;
//consider the interval [vMin-d, vMax+d]
for (int v = vMin-d; v <= vMax+d; v++){
//check that only values from the interval [vMin, vMax] are included in the list
assert((list.search(v) != -1) == (vMin <= v && v <= vMax));
try{
assert(list.remove(list.search(v)));
assert(vMin <= v && v <= vMax);
} catch (exception&) {
assert(vMin > v || v > vMax);
}
}
assert(list.size() == 0);
assert(list.isEmpty());
}
void testAllExtended() {
test_remove_by_step();
testCreate();
testAddAndSearch();
testDeleteSearch();
testQuantity();
}