This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestExtins.cpp
330 lines (292 loc) · 7.52 KB
/
TestExtins.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
#include "TestExtins.h"
#include <assert.h>
#include <exception>
#include <cstdlib>
#include <vector>
#include "Iterator.h"
#include "LO.h"
using namespace std;
//Relatie de ordine - crescator
bool cresc(TElement c1, TElement c2) {
if (c1 <= c2) {
return true;
} else {
return false;
}
}
//Relatie de ordine - descrescator
bool desc(TElement c1, TElement c2) {
if (c1 >= c2) {
return true;
} else {
return false;
}
}
void testCreeaza() {
LO lo = LO(cresc);
assert(lo.dim() == 0);
assert(lo.vida());
Iterator it = lo.iterator();
assert(!it.valid());
it.prim();
for (int i = 0; i < 10; i++) {
assert(!it.valid());
assert(lo.cauta(i) == -1);
try {
assert(lo.element(i));
assert(false);
} catch (exception&) {
assert(true);
}
try {
assert(lo.sterge(i));
assert(false);
} catch (exception&) {
assert(true);
}
try {
it.urmator();
assert(false);
}
catch (exception&) {
assert(true);
}
}
}
//genereaza un vector continand valori >=cMin si <=cMax a.i.:
//1) nicio valoare >=cMin si <=cMax care este multiplu de s nu este continuta
//2) valorile v, v>=cMin si v<=cMax care sunt multiplu de m (dar nu de s) sunt continute de c/m + 1 ori
//3) valorile >=cMin si <=cMax apar in ordine aleatoare
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;
}
//genereaza un vector continand valorile >=cMin si <=cMax, o singura data, in ordine aleatoare
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;
}
//populeaza LO lo cu valori >=cMin si <=cMax a.i.:
//1) nicio valoare >=cMin si <=cMax care este multiplu de s nu este continuta
//2) valorile v, v>=cMin si v<=cMax care sunt multiplu de m (dar nu de s) sunt continute de c/m + 1 ori
//3) valorile >=cMin si <=cMax apar in ordine aleatoare
int populeaza(LO& lo, 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++) {
lo.adauga(v[i]);
}
return n;
}
//populeaza LO lo cu valorile >=cMin si <=cMax in ordine aleatoare, fiecare valoare adaugand-se o singura data
void populeaza(LO& lo, int cMin, int cMax) {
vector<int> v = random(cMin, cMax);
int n = v.size();
for (int i = 0; i < n; i++) {
lo.adauga(v[i]);
}
}
void testAdaugaSiCauta(Relatie r) {
LO lo = LO(r);
int vMin = 10;
int vMax = 30;
int s = 5;
int m = 3;
int n = populeaza(lo, vMin, vMax, s, m);
assert(!lo.vida());
assert(lo.dim() == n);
//nu se vor gasi valori in afara intervalului considerat sau pe pozitii inexistente
int d = 30;
for (int i = 1; i <= d; i++) {
assert(lo.cauta(vMin - i) == -1);
assert(lo.cauta(vMax + i) == -1);
try{
lo.element(n-1+i);
lo.element(-d);
assert(false);
} catch (exception&) {
assert(true);
}
}
//se verifica respectarea relatiei de ordine
Iterator it = lo.iterator();
it.prim();
assert(it.valid());
TElement prec = it.element();
it.urmator();
while (it.valid()) {
TElement curent = it.element();
assert(r(prec, curent));
prec = curent;
it.urmator();
}
//se verifica aparitia valorilor adaugate
for (int v = vMin; v <= vMax; v++) {
int p = lo.cauta(v);
//nu se vor gasi valori care sunt multiplu de s
assert((p != -1) == (v % s != 0));
//valorile care sunt multiplu de m se vor gasi de exact v/m+1 ori
if (p != -1 && v%m == 0){
for (int i=0; i<=v/m; i++){
try{
assert(lo.sterge(p) == v);
} catch (exception&) {
assert(false);
}
}
assert(lo.cauta(v) == -1);
}
}
}
void testStergeCauta(Relatie r) {
LO lo = LO(r);
int vMin = 0;
int vMax = 100;
populeaza(lo, vMin, vMax);
int d = 30;
for (int i = 1; i <= d; i++) {
try {
lo.sterge(lo.cauta(vMax + i));
assert(false);
} catch (exception&) {
assert(true);
}
}
assert(!lo.vida());
assert(lo.dim() == vMax - vMin + 1);
Iterator it1 = lo.iterator();
assert(it1.valid());
it1.prim();
assert(it1.valid());
TElement sters = NULL_TELEMENT;
try {
sters = lo.sterge(lo.cauta(it1.element()));
assert(lo.dim() == vMax - vMin);
it1.prim();
TElement nouPrim = it1.element();
assert(nouPrim != sters);
assert(r(sters, nouPrim));
it1.prim();
Iterator it2 = lo.iterator();
assert(it2.valid());
it2.prim();
assert(it2.valid());
assert(it1.element() == nouPrim && it2.element() == nouPrim);
} catch (exception&) {
assert(false);
}
//se sterg in ordine aleatoare valori, verificandu-se relatia de ordine
vector<int> vs = random(vMin, vMax);
int n = vs.size();
for (int i = 0; i < n; i++) {
int v = vs[i];
try {
int it1 = lo.cauta(v);
TElement stersCurent = lo.sterge(it1);
assert(stersCurent != sters);
assert(stersCurent == v);
assert(lo.cauta(v) == -1);
if (!lo.vida()) {
Iterator it2 = lo.iterator();
it2.prim();
assert(it2.valid());
TElement prec = it2.element();
while (it2.valid()) {
TElement curent = lo.element(lo.cauta(it2.element()));
assert(r(prec, curent));
assert(!r(stersCurent, curent) || !r(curent, stersCurent));
prec = curent;
it2.urmator();
}
}
} catch (exception&) {
assert(v == sters);
}
}
assert(lo.vida());
assert(lo.dim() == 0);
}
void testStergeCauta() {
testStergeCauta(cresc);
testStergeCauta(desc);
}
void testAdaugaSiCauta() {
testAdaugaSiCauta(cresc);
testAdaugaSiCauta(desc);
}
void testCantitativ(){
LO lo = LO(cresc);
int vMin = 3000;
int vMax = 6000;
vector<int> valori = random(vMin, vMax);
int n = valori.size();
for (int i = 0; i < n; i++){
lo.adauga(valori[i]);
}
assert(lo.dim() == vMax - vMin + 1);
for (int v = vMin; v <= vMax; v++){
assert(lo.cauta(v)>= 0 && lo.cauta(v) < lo.dim());
assert(lo.element(lo.cauta(v)) == v);
}
Iterator it = lo.iterator();
it.prim();
assert(it.valid());
TElement primElement = it.element();
it.prim();
assert(it.valid());
assert(it.element() == primElement);
for (int i = 0; i < lo.dim(); i++) {
it.urmator();
}
assert(!it.valid());
it.prim();
while (it.valid()){
TElement v = it.element();
assert(vMin <= v && v<=vMax);
it.urmator();
}
assert(!it.valid());
int d = 100;
//se considera supraintervalul [vMin-d, vMax+d]
for (int v = vMin-d; v <= vMax+d; v++){
//se verifica ca doar valori din [vMin, vMax] sa se gaseasca in LO
assert((lo.cauta(v) != -1) == (vMin <= v && v <= vMax));
try{
assert(lo.sterge(lo.cauta(v)));
assert(vMin <= v && v <= vMax);
} catch (exception&) {
assert(vMin > v || v > vMax);
}
}
assert(lo.dim() == 0);
assert(lo.vida());
}
void testAllExtins() {
testCreeaza();
testAdaugaSiCauta();
testStergeCauta();
testCantitativ();
}