4 * Copyright (c) 2012 project bchan
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.
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:
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.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
27 #include "test_coll.h"
37 #include <unittest_driver.h>
44 LOCAL UNITTEST_RESULT test_idtocb_1()
46 #define TEST_IDTOCB_1_TESTSIZE 10
48 ID ids[TEST_IDTOCB_1_TESTSIZE];
50 idtocb_iterator_t iter;
53 UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
55 cb = idtocb_new(sizeof(test_idtocb_1_t), TEST_IDTOCB_1_TESTSIZE);
57 return UNITTEST_RESULT_FAIL;
60 for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
61 ids[i] = idtocb_allocate(cb);
63 printf("idtocb_allocate %d error\n", i);
64 ret = UNITTEST_RESULT_FAIL;
69 for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
70 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
72 printf("idtocb_getcontrolblock error: %08x\n", err);
73 ret = UNITTEST_RESULT_FAIL;
79 for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
80 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
82 printf("idtocb_getcontrolblock error: %08x\n", err);
83 ret = UNITTEST_RESULT_FAIL;
87 printf("idtocb_getcontrolblock result is not expected\n");
88 ret = UNITTEST_RESULT_FAIL;
94 idtocb_iterator_initialize(&iter, cb);
96 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
101 for (j = 0; j < TEST_IDTOCB_1_TESTSIZE; j++) {
102 if (p->base.id == ids[j]) {
104 printf("idtocb_iterator return value irregal\n");
105 ret = UNITTEST_RESULT_FAIL;
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;
117 for (i = 0; i < TEST_IDTOCB_1_TESTSIZE; i++) {
118 idtocb_free(cb, ids[i]);
122 idtocb_iterator_initialize(&iter, cb);
124 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
130 idtocb_iterator_finalize(&iter);
132 printf("idtocb_iterator iteration number is not expected:2\n");
133 ret = UNITTEST_RESULT_FAIL;
147 LOCAL Bool test_idtocb_2_setvalue(idtocb_t *cb, ID *ids, W len)
152 for (i = 0; i < len; i++) {
157 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
159 printf("idtocb_getcontrolblock error: %08x\n", err);
169 LOCAL Bool test_idtocb_2_checkvalue(idtocb_t *cb, ID *ids, W len)
173 idtocb_iterator_t iter;
176 for (i = 0; i < len; i++) {
181 err = idtocb_getcontrolblock(cb, ids[i], (idtocb_entry_t**)&p);
183 printf("idtocb_getcontrolblock error: %08x\n", err);
189 if (p->id != p->base.id) {
194 idtocb_iterator_initialize(&iter, cb);
196 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
200 for (j = 0; j < len; j++) {
201 if (p->base.id == ids[j]) {
203 printf("idtocb_iterator return value irregal: i\n");
204 idtocb_iterator_finalize(&iter);
207 if (p->id != ids[j]) {
208 printf("idtocb_iterator return value irregal: id\n");
209 idtocb_iterator_finalize(&iter);
215 idtocb_iterator_finalize(&iter);
220 LOCAL Bool test_idtocb_2_checklength(idtocb_t *cb, W expected_len)
224 idtocb_iterator_t iter;
228 idtocb_iterator_initialize(&iter, cb);
230 cont = idtocb_iterator_next(&iter, (idtocb_entry_t**)&p);
236 idtocb_iterator_finalize(&iter);
238 if (i != expected_len) {
239 printf("iteration number is not expected\n");
245 LOCAL UNITTEST_RESULT test_idtocb_2()
247 #define TEST_IDTOCB_2_TESTSIZE 15
249 ID ids[TEST_IDTOCB_2_TESTSIZE];
252 UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
254 cb = idtocb_new(sizeof(test_idtocb_2_t), TEST_IDTOCB_2_TESTSIZE);
256 return UNITTEST_RESULT_FAIL;
259 is_pass = test_idtocb_2_checklength(cb, 0);
260 if (is_pass == False) {
261 ret = UNITTEST_RESULT_FAIL;
264 for (i = 0; i < TEST_IDTOCB_2_TESTSIZE; i++) {
265 ids[i] = idtocb_allocate(cb);
267 printf("idtocb_allocate %d error\n", i);
268 ret = UNITTEST_RESULT_FAIL;
273 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE);
274 if (is_pass == False) {
275 ret = UNITTEST_RESULT_FAIL;
277 is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
278 if (is_pass == False) {
279 ret = UNITTEST_RESULT_FAIL;
281 is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
282 if (is_pass == False) {
283 ret = UNITTEST_RESULT_FAIL;
286 idtocb_free(cb, ids[3]);
288 idtocb_free(cb, ids[5]);
290 idtocb_free(cb, ids[7]);
292 idtocb_free(cb, ids[8]);
295 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 4);
296 if (is_pass == False) {
297 ret = UNITTEST_RESULT_FAIL;
299 is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
300 if (is_pass == False) {
301 ret = UNITTEST_RESULT_FAIL;
304 ids[3] = idtocb_allocate(cb);
306 printf("idtocb_allocate %d error\n", 3);
307 ret = UNITTEST_RESULT_FAIL;
309 ids[7] = idtocb_allocate(cb);
311 printf("idtocb_allocate %d error\n", 7);
312 ret = UNITTEST_RESULT_FAIL;
314 ids[8] = idtocb_allocate(cb);
316 printf("idtocb_allocate %d error\n", 8);
317 ret = UNITTEST_RESULT_FAIL;
319 ids[5] = idtocb_allocate(cb);
321 printf("idtocb_allocate %d error\n", 5);
322 ret = UNITTEST_RESULT_FAIL;
324 is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
325 if (is_pass == False) {
326 ret = UNITTEST_RESULT_FAIL;
328 is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
329 if (is_pass == False) {
330 ret = UNITTEST_RESULT_FAIL;
332 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE);
333 if (is_pass == False) {
334 ret = UNITTEST_RESULT_FAIL;
337 idtocb_free(cb, ids[1]);
339 idtocb_free(cb, ids[2]);
341 idtocb_free(cb, ids[9]);
343 idtocb_free(cb, ids[6]);
345 is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
346 if (is_pass == False) {
347 ret = UNITTEST_RESULT_FAIL;
349 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 4);
350 if (is_pass == False) {
351 ret = UNITTEST_RESULT_FAIL;
354 ids[2] = idtocb_allocate(cb);
356 printf("idtocb_allocate %d error\n", 2);
357 ret = UNITTEST_RESULT_FAIL;
359 is_pass = test_idtocb_2_setvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
360 if (is_pass == False) {
361 ret = UNITTEST_RESULT_FAIL;
363 is_pass = test_idtocb_2_checkvalue(cb, ids, TEST_IDTOCB_2_TESTSIZE);
364 if (is_pass == False) {
365 ret = UNITTEST_RESULT_FAIL;
367 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_2_TESTSIZE - 3);
368 if (is_pass == False) {
369 ret = UNITTEST_RESULT_FAIL;
372 for (i = 0; i < TEST_IDTOCB_2_TESTSIZE; i++) {
373 idtocb_free(cb, ids[i]);
381 LOCAL UNITTEST_RESULT test_idtocb_3()
383 #define TEST_IDTOCB_3_TESTSIZE 15
385 ID id0, ids[TEST_IDTOCB_3_TESTSIZE];
387 UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
390 cb = idtocb_new(sizeof(test_idtocb_2_t), TEST_IDTOCB_3_TESTSIZE);
392 return UNITTEST_RESULT_FAIL;
395 for (i = 0; i < TEST_IDTOCB_3_TESTSIZE * 2; i++) {
396 id0 = idtocb_allocate(cb);
397 if (i < TEST_IDTOCB_3_TESTSIZE) {
399 printf("idtocb_allocate %d error\n", i);
400 ret = UNITTEST_RESULT_FAIL;
403 is_pass = test_idtocb_2_checklength(cb, i+1);
404 if (is_pass == False) {
405 ret = UNITTEST_RESULT_FAIL;
409 if ((id0 & 0xFFFF0000) != ER_NOMEM) {
410 printf("idtocb_allocate %d is not error\n", i);
411 ret = UNITTEST_RESULT_FAIL;
414 is_pass = test_idtocb_2_checklength(cb, TEST_IDTOCB_3_TESTSIZE);
415 if (is_pass == False) {
416 ret = UNITTEST_RESULT_FAIL;
422 for (i = 0; i < TEST_IDTOCB_3_TESTSIZE; i++) {
423 idtocb_free(cb, ids[i]);
431 EXPORT VOID test_idtocb_main(unittest_driver_t *driver)
433 UNITTEST_DRIVER_REGIST(driver, test_idtocb_1);
434 UNITTEST_DRIVER_REGIST(driver, test_idtocb_2);
435 UNITTEST_DRIVER_REGIST(driver, test_idtocb_3);