OSDN Git Service

implement abstruct id and control block manager.
[bbk/bchanf.git] / src / coll / test_idtocb.c
1 /*
2  * test_idtocb.c
3  *
4  * Copyright (c) 2012 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include "test_coll.h"
28
29 #include "idtocb.h"
30
31 #include        <basic.h>
32 #include        <bstdio.h>
33 #include        <bstdlib.h>
34 #include        <bstring.h>
35 #include        <errcode.h>
36
37 #include    <unittest_driver.h>
38
39 typedef struct {
40         idtocb_entry_t base;
41         W i;
42 } test_idtocb_1_t;
43
44 LOCAL UNITTEST_RESULT test_idtocb_1()
45 {
46 #define TEST_IDTOCB_1_TESTSIZE 10
47         idtocb_t *cb;
48         ID ids[TEST_IDTOCB_1_TESTSIZE];
49         test_idtocb_1_t *p;
50         idtocb_iterator_t iter;
51         W i, j, err;
52         Bool cont;
53         UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
54
55         cb = idtocb_new(sizeof(test_idtocb_1_t), TEST_IDTOCB_1_TESTSIZE);
56         if (cb == NULL) {
57                 return UNITTEST_RESULT_FAIL;
58         }
59
60         for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
61                 ids[i] = idtocb_allocate(cb);
62                 if (ids[i] < 0) {
63                         printf("idtocb_allocate %d error\n", i);
64                         ret = UNITTEST_RESULT_FAIL;
65                         break;
66                 }
67         }
68
69         for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
70                 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
71                 if (err < 0) {
72                         printf("idtocb_getcontrolblock error: %08x\n", err);
73                         ret = UNITTEST_RESULT_FAIL;
74                         break;
75                 }
76                 p->i = i;
77         }
78
79         for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
80                 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
81                 if (err < 0) {
82                         printf("idtocb_getcontrolblock error: %08x\n", err);
83                         ret = UNITTEST_RESULT_FAIL;
84                         break;
85                 }
86                 if (p->i != i) {
87                         printf("idtocb_getcontrolblock result is not expected\n");
88                         ret = UNITTEST_RESULT_FAIL;
89                         break;
90                 }
91         }
92
93         i = 0;
94         idtocb_iterator_initialize(&iter, cb);
95         for (;;) {
96                 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
97                 if (cont == False) {
98                         break;
99                 }
100                 i++;
101                 for (j = 0; j < TEST_IDTOCB_1_TESTSIZE; j++) {
102                         if (p->base.id == ids[j]) {
103                                 if (p->i != j) {
104                                         printf("idtocb_iterator return value irregal\n");
105                                         ret = UNITTEST_RESULT_FAIL;
106                                         break;
107                                 }
108                         }
109                 }
110         }
111         idtocb_iterator_finalize(&iter);
112         if (i != TEST_IDTOCB_1_TESTSIZE) {
113                 printf("idtocb_iterator iteration number is not expected:1\n");
114                 ret = UNITTEST_RESULT_FAIL;
115         }
116
117         for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
118                 idtocb_free(cb, ids[i]);
119         }
120
121         i = 0;
122         idtocb_iterator_initialize(&iter, cb);
123         for (;;) {
124                 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
125                 if (cont == False) {
126                         break;
127                 }
128                 i++;
129         }
130         idtocb_iterator_finalize(&iter);
131         if (i != 0) {
132                 printf("idtocb_iterator iteration number is not expected:2\n");
133                 ret = UNITTEST_RESULT_FAIL;
134         }
135
136         idtocb_delete(cb);
137
138         return ret;
139 }
140
141 typedef struct {
142         idtocb_entry_t base;
143         W i;
144         ID id;
145 } test_idtocb_2_t;
146
147 LOCAL Bool test_idtocb_2_setvalue(idtocb_t *cb, ID *ids, W len)
148 {
149         test_idtocb_2_t *p;
150         W i, err;
151
152         for (i = 0; i < len; i++) {
153                 if (ids[i] < 0) {
154                         continue;
155                 }
156
157                 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
158                 if (err < 0) {
159                         printf("idtocb_getcontrolblock error: %08x\n", err);
160                         return False;
161                 }
162                 p->i = i;
163                 p->id = p->base.id;
164         }
165
166         return True;
167 }
168
169 LOCAL Bool test_idtocb_2_checkvalue(idtocb_t *cb, ID *ids, W len)
170 {
171         test_idtocb_2_t *p;
172         W i, j, err;
173         idtocb_iterator_t iter;
174         Bool cont;
175
176         for (i = 0; i < len; i++) {
177                 if (ids[i] < 0) {
178                         continue;
179                 }
180
181                 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
182                 if (err < 0) {
183                         printf("idtocb_getcontrolblock error: %08x\n", err);
184                         return False;
185                 }
186                 if (p->i != i) {
187                         return False;
188                 }
189                 if (p->id != p->base.id) {
190                         return False;
191                 }
192         }
193
194         idtocb_iterator_initialize(&iter, cb);
195         for (;;) {
196                 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
197                 if (cont == False) {
198                         break;
199                 }
200                 for (j = 0; j < len; j++) {
201                         if (p->base.id == ids[j]) {
202                                 if (p->i != j) {
203                                         printf("idtocb_iterator return value irregal: i\n");
204                                         idtocb_iterator_finalize(&iter);
205                                         return False;
206                                 }
207                                 if (p->id != ids[j]) {
208                                         printf("idtocb_iterator return value irregal: id\n");
209                                         idtocb_iterator_finalize(&iter);
210                                         return False;
211                                 }
212                         }
213                 }
214         }
215         idtocb_iterator_finalize(&iter);
216
217         return True;
218 }
219
220 LOCAL Bool test_idtocb_2_checklength(idtocb_t *cb, W expected_len)
221 {
222         test_idtocb_2_t *p;
223         W i;
224         idtocb_iterator_t iter;
225         Bool cont;
226
227         i = 0;
228         idtocb_iterator_initialize(&iter, cb);
229         for (;;) {
230                 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
231                 if (cont == False) {
232                         break;
233                 }
234                 i++;
235         }
236         idtocb_iterator_finalize(&iter);
237
238         if (i != expected_len) {
239                 printf("iteration number is not expected\n");
240                 return False;
241         }
242         return True;
243 }
244
245 LOCAL UNITTEST_RESULT test_idtocb_2()
246 {
247 #define TEST_IDTOCB_2_TESTSIZE 15
248         idtocb_t *cb;
249         ID ids[TEST_IDTOCB_2_TESTSIZE];
250         W i;
251         Bool is_pass;
252         UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
253
254         cb = idtocb_new(sizeof(test_idtocb_2_t), TEST_IDTOCB_2_TESTSIZE);
255         if (cb == NULL) {
256                 return UNITTEST_RESULT_FAIL;
257         }
258
259         is_pass = test_idtocb_2_checklength(cb, 0);
260         if (is_pass == False) {
261                 ret = UNITTEST_RESULT_FAIL;
262         }
263
264         for (i = 0; i < TEST_IDTOCB_2_TESTSIZE; i++) {
265                 ids[i] = idtocb_allocate(cb);
266                 if (ids[i] < 0) {
267                         printf("idtocb_allocate %d error\n", i);
268                         ret = UNITTEST_RESULT_FAIL;
269                         break;
270                 }
271         }
272
273         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE);
274         if (is_pass == False) {
275                 ret = UNITTEST_RESULT_FAIL;
276         }
277         is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
278         if (is_pass == False) {
279                 ret = UNITTEST_RESULT_FAIL;
280         }
281         is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
282         if (is_pass == False) {
283                 ret = UNITTEST_RESULT_FAIL;
284         }
285
286         idtocb_free(cb, ids[3]);
287         ids[3] = -1;
288         idtocb_free(cb, ids[5]);
289         ids[5] = -1;
290         idtocb_free(cb, ids[7]);
291         ids[7] = -1;
292         idtocb_free(cb, ids[8]);
293         ids[8] = -1;
294
295         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 4);
296         if (is_pass == False) {
297                 ret = UNITTEST_RESULT_FAIL;
298         }
299         is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
300         if (is_pass == False) {
301                 ret = UNITTEST_RESULT_FAIL;
302         }
303
304         ids[3] = idtocb_allocate(cb);
305         if (ids[3] < 0) {
306                 printf("idtocb_allocate %d error\n", 3);
307                 ret = UNITTEST_RESULT_FAIL;
308         }
309         ids[7] = idtocb_allocate(cb);
310         if (ids[7] < 0) {
311                 printf("idtocb_allocate %d error\n", 7);
312                 ret = UNITTEST_RESULT_FAIL;
313         }
314         ids[8] = idtocb_allocate(cb);
315         if (ids[8] < 0) {
316                 printf("idtocb_allocate %d error\n", 8);
317                 ret = UNITTEST_RESULT_FAIL;
318         }
319         ids[5] = idtocb_allocate(cb);
320         if (ids[5] < 0) {
321                 printf("idtocb_allocate %d error\n", 5);
322                 ret = UNITTEST_RESULT_FAIL;
323         }
324         is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
325         if (is_pass == False) {
326                 ret = UNITTEST_RESULT_FAIL;
327         }
328         is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
329         if (is_pass == False) {
330                 ret = UNITTEST_RESULT_FAIL;
331         }
332         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE);
333         if (is_pass == False) {
334                 ret = UNITTEST_RESULT_FAIL;
335         }
336
337         idtocb_free(cb, ids[1]);
338         ids[1] = -1;
339         idtocb_free(cb, ids[2]);
340         ids[2] = -1;
341         idtocb_free(cb, ids[9]);
342         ids[9] = -1;
343         idtocb_free(cb, ids[6]);
344         ids[6] = -1;
345         is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
346         if (is_pass == False) {
347                 ret = UNITTEST_RESULT_FAIL;
348         }
349         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 4);
350         if (is_pass == False) {
351                 ret = UNITTEST_RESULT_FAIL;
352         }
353
354         ids[2] = idtocb_allocate(cb);
355         if (ids[2] < 0) {
356                 printf("idtocb_allocate %d error\n", 2);
357                 ret = UNITTEST_RESULT_FAIL;
358         }
359         is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
360         if (is_pass == False) {
361                 ret = UNITTEST_RESULT_FAIL;
362         }
363         is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
364         if (is_pass == False) {
365                 ret = UNITTEST_RESULT_FAIL;
366         }
367         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 3);
368         if (is_pass == False) {
369                 ret = UNITTEST_RESULT_FAIL;
370         }
371
372         for (i = 0; i < TEST_IDTOCB_2_TESTSIZE; i++) {
373                 idtocb_free(cb, ids[i]);
374         }
375
376         idtocb_delete(cb);
377
378         return ret;
379 }
380
381 LOCAL UNITTEST_RESULT test_idtocb_3()
382 {
383 #define TEST_IDTOCB_3_TESTSIZE 15
384         idtocb_t *cb;
385         ID id0, ids[TEST_IDTOCB_3_TESTSIZE];
386         W i;
387         UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
388         Bool is_pass;
389
390         cb = idtocb_new(sizeof(test_idtocb_2_t), TEST_IDTOCB_3_TESTSIZE);
391         if (cb == NULL) {
392                 return UNITTEST_RESULT_FAIL;
393         }
394
395         for (i = 0; i < TEST_IDTOCB_3_TESTSIZE * 2; i++) {
396                 id0 = idtocb_allocate(cb);
397                 if (i < TEST_IDTOCB_3_TESTSIZE) {
398                         if (id0 < 0) {
399                                 printf("idtocb_allocate %d error\n", i);
400                                 ret = UNITTEST_RESULT_FAIL;
401                                 break;
402                         }
403                         is_pass = test_idtocb_2_checklength(cb, i+1);
404                         if (is_pass == False) {
405                                 ret = UNITTEST_RESULT_FAIL;
406                                 break;
407                         }
408                 } else {
409                         if ((id0 & 0xFFFF0000) != ER_NOMEM) {
410                                 printf("idtocb_allocate %d is not error\n", i);
411                                 ret = UNITTEST_RESULT_FAIL;
412                                 break;
413                         }
414                         is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_3_TESTSIZE);
415                         if (is_pass == False) {
416                                 ret = UNITTEST_RESULT_FAIL;
417                                 break;
418                         }
419                 }
420         }
421
422         for (i = 0; i < TEST_IDTOCB_3_TESTSIZE; i++) {
423                 idtocb_free(cb, ids[i]);
424         }
425
426         idtocb_delete(cb);
427
428         return ret;
429 }
430
431 EXPORT VOID test_idtocb_main(unittest_driver_t *driver)
432 {
433         UNITTEST_DRIVER_REGIST(driver, test_idtocb_1);
434         UNITTEST_DRIVER_REGIST(driver, test_idtocb_2);
435         UNITTEST_DRIVER_REGIST(driver, test_idtocb_3);
436 }