-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
312 lines (224 loc) · 5.59 KB
/
test.c
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
// test
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "avltree.h"
// forward references for test cases
// return value 0 means test passed, !0 - test failed
int TestCase1(void);
int TestCase2(void);
int TestCase3(void);
int TestCase4(void);
int TestCase5(void);
struct mydata
{
int myvalue;
struct eg_avltree_node avlnode;
};
#define MYDATAPTR(pnode) BASEPTR_TO_DERIVEDPTR(pnode, struct mydata, avlnode)
int mycmpfunc(struct eg_avltree_node *node1, struct eg_avltree_node *node2)
{
struct mydata *p1 = MYDATAPTR(node1);
struct mydata *p2 = MYDATAPTR(node2);
return p1->myvalue - p2->myvalue;
}
int main(int argc, char **argv)
{
int res;
// init random number generator
srand(2015);
res = TestCase1();
if (res) goto fail;
res = TestCase2();
if (res) goto fail;
res = TestCase3();
if (res) goto fail;
res = TestCase4();
if (res) goto fail;
res = TestCase5();
if (res) goto fail;
printf("All tests passed! \n");
return 0;
fail:
printf("Error found! \n");
return 1;
}
/***************************************************************************\
* helper functions *
\***************************************************************************/
// makes array of `mydata` contain random unique values
static void InitMydataArray(struct mydata *pArr, int nSize)
{
int i, j, tmp;
// zero memory
memset(pArr, 0, nSize * sizeof(struct mydata));
// fill with sequential numbers
for (i=0; i<nSize; i++)
pArr[i].myvalue = i;
// random suffle
for (i=0; i<nSize; i++)
{
j = rand() * (double)(nSize-1) / RAND_MAX;
tmp = pArr[i].myvalue;
pArr[i].myvalue = pArr[j].myvalue;
pArr[j].myvalue = tmp;
}
}
// checks if tree has a given size and its nodes are correctly sorted
//
// return:
// 0 - success (tree is correct)
// 1 - failure (tree is corrupt)
static int CheckSizeAndSort(struct eg_avltree *pTree, int nSize)
{
int i=0, v=0;
struct eg_avltree_node *pn;
struct mydata *pd;
// get first node and value
pn = eg_avltree_first(pTree);
if (!pn) goto checksize;
pd = MYDATAPTR(pn);
v = pd->myvalue;
i = 1;
// go through tree in ascending order
for (;;)
{
pn = eg_avltree_next(pn);
if (!pn) goto checksize;
pd = MYDATAPTR(pn);
if (pd->myvalue <= v) return 1;
v = pd->myvalue;
i++;
}
checksize:
// check size
if (i != nSize)
return 1;
// success
return 0;
}
/***************************************************************************\
* test cases *
\***************************************************************************/
int TestCase1(void)
{
int ret = 1;
int i;
struct mydata tmp, *pmydata;
struct mydata arr[10];
struct eg_avltree tree;
struct eg_avltree_node *pn1;
// print test description
printf("Test case 1: small tree and simple checks. \n");
// init avl tree
eg_avltree_init(&tree, mycmpfunc);
// try to find 5 in the tree, should fail
tmp.myvalue = 5;
pn1 = eg_avltree_find(&tree, &tmp.avlnode);
if (pn1) goto exit;
// insert 10 values into tree
printf("\n ");
InitMydataArray(arr, 10);
for (i=0; i<10; i++)
{
printf("%d, ", arr[i].myvalue);
eg_avltree_insert(&tree, &arr[i].avlnode);
}
printf("\n");
// try now to find 5, should succeed
tmp.myvalue = 5;
pn1 = eg_avltree_find(&tree, &tmp.avlnode);
if (!pn1) goto exit;
// check if value is actually 5
pmydata = MYDATAPTR(pn1);
if (pmydata->myvalue != 5) goto exit;
// check 10 inserted values
printf(" ");
for (pn1=eg_avltree_first(&tree), i=0; pn1 && i<10; pn1=eg_avltree_next(pn1), i++)
{
pmydata = MYDATAPTR(pn1);
if (pmydata->myvalue != i)
goto exit;
printf("%d, ", pmydata->myvalue);
}
printf("\n\n\n");
// must be 10 values in the tree
if (i != 10) goto exit;
// delete nodes one by one and perform checks
for (i=0; i<10; i++)
{
eg_avltree_remove(&tree, &arr[i].avlnode);
if (CheckSizeAndSort(&tree, 9-i)) goto exit;
}
// all tests passed, return success
ret = 0;
exit:
return ret;
}
int TestCase2(void)
{
int ret = 1;
int i, j, repeat, rnd;
struct mydata *arr;
int arr_size = 10000;
int curr_size;
struct eg_avltree tree;
// print test description
printf("Test case 2: medium tree and exhaustive checks. \n");
// init data array
arr = malloc(arr_size * sizeof(struct mydata));
if (!arr) goto exit;
// repeat test 10 times
printf("\n");
for (repeat=0; repeat<10; repeat++)
{
InitMydataArray(arr, arr_size);
eg_avltree_init(&tree, mycmpfunc);
curr_size = 0;
for (i=0,j=0; i<arr_size; i++)
{
// insert value and test tree
eg_avltree_insert(&tree, &arr[i].avlnode);
curr_size++;
if (CheckSizeAndSort(&tree, curr_size)) goto exit;
// remove value with 10% probability and test tree again
rnd = rand() * 99.0 / RAND_MAX;
if (rnd < 10)
{
eg_avltree_remove(&tree, &arr[j++].avlnode);
if (CheckSizeAndSort(&tree, --curr_size)) goto exit;
}
}
printf(" Pass %d is done. Tree size is %d. \n", repeat+1, curr_size);
}
printf("\n\n");
// all tests passed, return success
ret = 0;
exit:
free(arr);
return ret;
}
int TestCase3(void)
{
int ret = 1;
// all tests passed, return success
ret = 0;
//exit:
return ret;
}
int TestCase4(void)
{
int ret = 1;
// all tests passed, return success
ret = 0;
//exit:
return ret;
}
int TestCase5(void)
{
int ret = 1;
// all tests passed, return success
ret = 0;
//exit:
return ret;
}