Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_linked_list.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013 Tobias Brunner
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include "test_suite.h"
        !            17: 
        !            18: #include <collections/linked_list.h>
        !            19: 
        !            20: /*******************************************************************************
        !            21:  * test fixture
        !            22:  */
        !            23: 
        !            24: static linked_list_t *list;
        !            25: 
        !            26: START_SETUP(setup_list)
        !            27: {
        !            28:        void *x = NULL;
        !            29: 
        !            30:        list = linked_list_create();
        !            31:        ck_assert_int_eq(list->get_count(list), 0);
        !            32:        ck_assert(list->get_first(list, &x) == NOT_FOUND);
        !            33:        ck_assert(list->get_last(list, &x) == NOT_FOUND);
        !            34: }
        !            35: END_SETUP
        !            36: 
        !            37: START_TEARDOWN(teardown_list)
        !            38: {
        !            39:        list->destroy(list);
        !            40: }
        !            41: END_TEARDOWN
        !            42: 
        !            43: /*******************************************************************************
        !            44:  * insert first/last
        !            45:  */
        !            46: 
        !            47: START_TEST(test_insert_first)
        !            48: {
        !            49:        void *a = (void*)1, *b = (void*)2, *x = NULL;
        !            50: 
        !            51:        list->insert_first(list, a);
        !            52:        ck_assert_int_eq(list->get_count(list), 1);
        !            53:        ck_assert(list->get_first(list, &x) == SUCCESS);
        !            54:        ck_assert(x == a);
        !            55:        ck_assert(list->get_last(list, &x) == SUCCESS);
        !            56:        ck_assert(x == a);
        !            57: 
        !            58:        list->insert_first(list, b);
        !            59:        ck_assert_int_eq(list->get_count(list), 2);
        !            60:        ck_assert(list->get_first(list, &x) == SUCCESS);
        !            61:        ck_assert(x == b);
        !            62:        ck_assert(list->get_last(list, &x) == SUCCESS);
        !            63:        ck_assert(x == a);
        !            64: }
        !            65: END_TEST
        !            66: 
        !            67: START_TEST(test_insert_last)
        !            68: {
        !            69:        void *a = (void*)1, *b = (void*)2, *x = NULL;
        !            70: 
        !            71:        list->insert_last(list, a);
        !            72:        ck_assert_int_eq(list->get_count(list), 1);
        !            73:        ck_assert(list->get_first(list, &x) == SUCCESS);
        !            74:        ck_assert(x == a);
        !            75:        ck_assert(list->get_last(list, &x) == SUCCESS);
        !            76:        ck_assert(x == a);
        !            77: 
        !            78:        list->insert_last(list, b);
        !            79:        ck_assert_int_eq(list->get_count(list), 2);
        !            80:        ck_assert(list->get_first(list, &x) == SUCCESS);
        !            81:        ck_assert(x == a);
        !            82:        ck_assert(list->get_last(list, &x) == SUCCESS);
        !            83:        ck_assert(x == b);
        !            84: }
        !            85: END_TEST
        !            86: 
        !            87: /*******************************************************************************
        !            88:  * remove first/last
        !            89:  */
        !            90: 
        !            91: START_TEST(test_remove_first)
        !            92: {
        !            93:        void *a = (void*)1, *b = (void*)2, *x = NULL;
        !            94: 
        !            95:        list->insert_first(list, a);
        !            96:        list->insert_first(list, b);
        !            97:        ck_assert(list->remove_first(list, &x) == SUCCESS);
        !            98:        ck_assert_int_eq(list->get_count(list), 1);
        !            99:        ck_assert(x == b);
        !           100:        ck_assert(list->remove_first(list, &x) == SUCCESS);
        !           101:        ck_assert_int_eq(list->get_count(list), 0);
        !           102:        ck_assert(x == a);
        !           103:        ck_assert(list->remove_first(list, &x) == NOT_FOUND);
        !           104:        ck_assert(list->remove_last(list, &x) == NOT_FOUND);
        !           105: }
        !           106: END_TEST
        !           107: 
        !           108: START_TEST(test_remove_last)
        !           109: {
        !           110:        void *a = (void*)1, *b = (void*)2, *x = NULL;
        !           111: 
        !           112:        list->insert_first(list, a);
        !           113:        list->insert_first(list, b);
        !           114:        ck_assert(list->remove_last(list, &x) == SUCCESS);
        !           115:        ck_assert_int_eq(list->get_count(list), 1);
        !           116:        ck_assert(x == a);
        !           117:        ck_assert(list->remove_last(list, &x) == SUCCESS);
        !           118:        ck_assert_int_eq(list->get_count(list), 0);
        !           119:        ck_assert(x == b);
        !           120:        ck_assert(list->remove_first(list, &x) == NOT_FOUND);
        !           121:        ck_assert(list->remove_last(list, &x) == NOT_FOUND);
        !           122: }
        !           123: END_TEST
        !           124: 
        !           125: /*******************************************************************************
        !           126:  * helper function for remove and find tests
        !           127:  */
        !           128: 
        !           129: static bool match_a(void *item, void *a)
        !           130: {
        !           131:        ck_assert(a == (void*)1);
        !           132:        return item == a;
        !           133: }
        !           134: 
        !           135: static bool match_b(void *item, void *b)
        !           136: {
        !           137:        ck_assert(b == (void*)2);
        !           138:        return item == b;
        !           139: }
        !           140: 
        !           141: /*******************************************************************************
        !           142:  * remove
        !           143:  */
        !           144: 
        !           145: START_TEST(test_remove)
        !           146: {
        !           147:        void *a = (void*)1, *b = (void*)2;
        !           148: 
        !           149:        list->insert_first(list, a);
        !           150:        ck_assert(list->remove(list, a, NULL) == 1);
        !           151:        ck_assert_int_eq(list->get_count(list), 0);
        !           152: 
        !           153:        list->insert_last(list, a);
        !           154:        list->insert_last(list, a);
        !           155:        list->insert_last(list, a);
        !           156:        list->insert_last(list, b);
        !           157:        ck_assert(list->remove(list, a, NULL) == 3);
        !           158:        ck_assert(list->remove(list, a, NULL) == 0);
        !           159:        ck_assert_int_eq(list->get_count(list), 1);
        !           160:        ck_assert(list->remove(list, b, NULL) == 1);
        !           161:        ck_assert(list->remove(list, b, NULL) == 0);
        !           162: }
        !           163: END_TEST
        !           164: 
        !           165: START_TEST(test_remove_callback)
        !           166: {
        !           167:        void *a = (void*)1, *b = (void*)2;
        !           168: 
        !           169:        list->insert_last(list, a);
        !           170:        list->insert_last(list, b);
        !           171:        list->insert_last(list, a);
        !           172:        list->insert_last(list, b);
        !           173:        ck_assert(list->remove(list, a, match_a) == 2);
        !           174:        ck_assert(list->remove(list, a, match_a) == 0);
        !           175:        ck_assert_int_eq(list->get_count(list), 2);
        !           176:        ck_assert(list->remove(list, b, match_b) == 2);
        !           177:        ck_assert(list->remove(list, b, match_b) == 0);
        !           178:        ck_assert_int_eq(list->get_count(list), 0);
        !           179: }
        !           180: END_TEST
        !           181: 
        !           182: /*******************************************************************************
        !           183:  * find
        !           184:  */
        !           185: 
        !           186: CALLBACK(find_a_b, bool,
        !           187:        void *item, va_list args)
        !           188: {
        !           189:        void *a, *b;
        !           190: 
        !           191:        VA_ARGS_VGET(args, a, b);
        !           192:        ck_assert(a == (void*)1);
        !           193:        ck_assert(b == (void*)2);
        !           194:        return item == a || item == b;
        !           195: }
        !           196: 
        !           197: CALLBACK(find_a, bool,
        !           198:        void *item, va_list args)
        !           199: {
        !           200:        void *a;
        !           201: 
        !           202:        VA_ARGS_VGET(args, a);
        !           203:        return match_a(item, a);
        !           204: }
        !           205: 
        !           206: CALLBACK(find_b, bool,
        !           207:        void *item, va_list args)
        !           208: {
        !           209:        void *b;
        !           210: 
        !           211:        VA_ARGS_VGET(args, b);
        !           212:        return match_b(item, b);
        !           213: }
        !           214: 
        !           215: START_TEST(test_find)
        !           216: {
        !           217:        void *a = (void*)1, *b = (void*)2;
        !           218: 
        !           219:        ck_assert(!list->find_first(list, NULL, &a));
        !           220:        list->insert_last(list, a);
        !           221:        ck_assert(list->find_first(list, NULL, &a));
        !           222:        ck_assert(!list->find_first(list, NULL, &b));
        !           223:        list->insert_last(list, b);
        !           224:        ck_assert(list->find_first(list, NULL, &a));
        !           225:        ck_assert(list->find_first(list, NULL, &b));
        !           226: 
        !           227:        ck_assert(!list->find_first(list, NULL, NULL));
        !           228: }
        !           229: END_TEST
        !           230: 
        !           231: START_TEST(test_find_callback)
        !           232: {
        !           233:        void *a = (void*)1, *b = (void*)2, *x = NULL;
        !           234: 
        !           235:        ck_assert(!list->find_first(list, find_a_b, &x, a, b));
        !           236:        list->insert_last(list, a);
        !           237:        ck_assert(list->find_first(list, find_a, NULL, a));
        !           238:        x = NULL;
        !           239:        ck_assert(list->find_first(list, find_a, &x, a));
        !           240:        ck_assert(a == x);
        !           241:        ck_assert(!list->find_first(list, find_b, &x, b));
        !           242:        ck_assert(a == x);
        !           243:        x = NULL;
        !           244:        ck_assert(list->find_first(list, find_a_b, &x, a, b));
        !           245:        ck_assert(a == x);
        !           246: 
        !           247:        list->insert_last(list, b);
        !           248:        ck_assert(list->find_first(list, find_a, &x, a));
        !           249:        ck_assert(a == x);
        !           250:        ck_assert(list->find_first(list, find_b, &x, b));
        !           251:        ck_assert(b == x);
        !           252:        x = NULL;
        !           253:        ck_assert(list->find_first(list, find_a_b, &x, a, b));
        !           254:        ck_assert(a == x);
        !           255: }
        !           256: END_TEST
        !           257: 
        !           258: CALLBACK(find_args, bool,
        !           259:        void *item, va_list args)
        !           260: {
        !           261:        uint64_t d, e;
        !           262:        level_t c;
        !           263:        int *a, b;
        !           264: 
        !           265:        VA_ARGS_VGET(args, a, b, c, d, e);
        !           266:        ck_assert_int_eq(*a, 1);
        !           267:        ck_assert_int_eq(b, 2);
        !           268:        ck_assert_int_eq(c, LEVEL_PRIVATE);
        !           269:        ck_assert_int_eq(d, UINT64_MAX);
        !           270:        ck_assert_int_eq(e, UINT64_MAX-1);
        !           271:        return item == a;
        !           272: }
        !           273: 
        !           274: START_TEST(test_find_callback_args)
        !           275: {
        !           276:        int a = 1, b = 2, *x;
        !           277:        uint64_t d = UINT64_MAX;
        !           278: 
        !           279:        list->insert_last(list, &a);
        !           280:        ck_assert(list->find_first(list, find_args, (void**)&x, &a, b,
        !           281:                                                           LEVEL_PRIVATE, d, UINT64_MAX-1));
        !           282:        ck_assert_int_eq(a, *x);
        !           283: }
        !           284: END_TEST
        !           285: 
        !           286: /*******************************************************************************
        !           287:  * invoke
        !           288:  */
        !           289: 
        !           290: typedef struct invoke_t invoke_t;
        !           291: 
        !           292: struct invoke_t {
        !           293:        int val;
        !           294:        void (*invoke)(invoke_t *item);
        !           295: };
        !           296: 
        !           297: CALLBACK(invoke, void,
        !           298:        intptr_t item, va_list args)
        !           299: {
        !           300:        void *a, *b, *c, *d;
        !           301:        int *sum;
        !           302: 
        !           303:        VA_ARGS_VGET(args, a, b, c, d, sum);
        !           304:        ck_assert_int_eq((uintptr_t)a, 1);
        !           305:        ck_assert_int_eq((uintptr_t)b, 2);
        !           306:        ck_assert_int_eq((uintptr_t)c, 3);
        !           307:        ck_assert_int_eq((uintptr_t)d, 4);
        !           308:        *sum += item;
        !           309: }
        !           310: 
        !           311: static void invoke_offset(invoke_t *item)
        !           312: {
        !           313:        item->val++;
        !           314: }
        !           315: 
        !           316: START_TEST(test_invoke_function)
        !           317: {
        !           318:        int sum = 0;
        !           319: 
        !           320:        list->insert_last(list, (void*)1);
        !           321:        list->insert_last(list, (void*)2);
        !           322:        list->insert_last(list, (void*)3);
        !           323:        list->insert_last(list, (void*)4);
        !           324:        list->insert_last(list, (void*)5);
        !           325:        list->invoke_function(list, invoke, (uintptr_t)1, (uintptr_t)2,
        !           326:                                                  (uintptr_t)3, (uintptr_t)4, &sum);
        !           327:        ck_assert_int_eq(sum, 15);
        !           328: }
        !           329: END_TEST
        !           330: 
        !           331: START_TEST(test_invoke_offset)
        !           332: {
        !           333:        invoke_t items[] = {
        !           334:                { .val = 1, .invoke = invoke_offset, },
        !           335:                { .val = 2, .invoke = invoke_offset, },
        !           336:                { .val = 3, .invoke = invoke_offset, },
        !           337:                { .val = 4, .invoke = invoke_offset, },
        !           338:                { .val = 5, .invoke = invoke_offset, },
        !           339:        }, *item;
        !           340:        int i;
        !           341: 
        !           342:        for (i = 0; i < countof(items); i++)
        !           343:        {
        !           344:                list->insert_last(list, &items[i]);
        !           345:        }
        !           346:        list->invoke_offset(list, offsetof(invoke_t, invoke));
        !           347:        i = 2;
        !           348:        while (list->remove_first(list, (void**)&item) == SUCCESS)
        !           349:        {
        !           350:                ck_assert_int_eq(item->val, i++);
        !           351:        }
        !           352: }
        !           353: END_TEST
        !           354: 
        !           355: /*******************************************************************************
        !           356:  * clone
        !           357:  */
        !           358: 
        !           359: typedef struct clone_t clone_t;
        !           360: 
        !           361: struct clone_t {
        !           362:        void *val;
        !           363:        void *(*clone)(clone_t *item);
        !           364: };
        !           365: 
        !           366: static void *clonefn(clone_t *item)
        !           367: {
        !           368:        return item->val;
        !           369: }
        !           370: 
        !           371: static void test_clone(linked_list_t *list)
        !           372: {
        !           373:        intptr_t x;
        !           374:        int round = 1;
        !           375: 
        !           376:        ck_assert_int_eq(list->get_count(list), 5);
        !           377:        while (list->remove_first(list, (void*)&x) == SUCCESS)
        !           378:        {
        !           379:                ck_assert_int_eq(round, x);
        !           380:                round++;
        !           381:        }
        !           382:        ck_assert_int_eq(round, 6);
        !           383: }
        !           384: 
        !           385: START_TEST(test_clone_offset)
        !           386: {
        !           387:        linked_list_t *other;
        !           388:        clone_t items[] = {
        !           389:                { .val = (void*)1, .clone = clonefn, },
        !           390:                { .val = (void*)2, .clone = clonefn, },
        !           391:                { .val = (void*)3, .clone = clonefn, },
        !           392:                { .val = (void*)4, .clone = clonefn, },
        !           393:                { .val = (void*)5, .clone = clonefn, },
        !           394:        };
        !           395:        int i;
        !           396: 
        !           397:        for (i = 0; i < countof(items); i++)
        !           398:        {
        !           399:                list->insert_last(list, &items[i]);
        !           400:        }
        !           401:        other = list->clone_offset(list, offsetof(clone_t, clone));
        !           402:        test_clone(other);
        !           403:        other->destroy(other);
        !           404: }
        !           405: END_TEST
        !           406: 
        !           407: 
        !           408: /*******************************************************************************
        !           409:  * equals
        !           410:  */
        !           411: 
        !           412: typedef struct equals_t equals_t;
        !           413: 
        !           414: struct equals_t {
        !           415:        int val;
        !           416:        bool (*equals)(equals_t *a, equals_t *b);
        !           417: };
        !           418: 
        !           419: static bool equalsfn(equals_t *a, equals_t *b)
        !           420: {
        !           421:        return a->val == b->val;
        !           422: }
        !           423: 
        !           424: START_TEST(test_equals_offset)
        !           425: {
        !           426:        linked_list_t *other;
        !           427:        equals_t *x, items[] = {
        !           428:                { .val = 1, .equals = equalsfn, },
        !           429:                { .val = 2, .equals = equalsfn, },
        !           430:                { .val = 3, .equals = equalsfn, },
        !           431:                { .val = 4, .equals = equalsfn, },
        !           432:                { .val = 5, .equals = equalsfn, },
        !           433:        };
        !           434:        int i;
        !           435: 
        !           436:        for (i = 0; i < countof(items); i++)
        !           437:        {
        !           438:                list->insert_last(list, &items[i]);
        !           439:        }
        !           440:        ck_assert(list->equals_offset(list, list, offsetof(equals_t, equals)));
        !           441:        other = linked_list_create_from_enumerator(list->create_enumerator(list));
        !           442:        ck_assert(list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           443:        other->remove_last(other, (void**)&x);
        !           444:        ck_assert(!list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           445:        list->remove_last(list, (void**)&x);
        !           446:        ck_assert(list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           447:        other->remove_first(other, (void**)&x);
        !           448:        ck_assert(!list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           449:        list->remove_first(list, (void**)&x);
        !           450:        ck_assert(list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           451:        while (list->remove_first(list, (void**)&x) == SUCCESS);
        !           452:        while (other->remove_first(other, (void**)&x) == SUCCESS);
        !           453:        ck_assert(list->equals_offset(list, other, offsetof(equals_t, equals)));
        !           454:        other->destroy(other);
        !           455: }
        !           456: END_TEST
        !           457: 
        !           458: START_TEST(test_equals_function)
        !           459: {
        !           460:        linked_list_t *other;
        !           461:        equals_t *x, items[] = {
        !           462:                { .val = 1, },
        !           463:                { .val = 2, },
        !           464:                { .val = 3, },
        !           465:                { .val = 4, },
        !           466:                { .val = 5, },
        !           467:        };
        !           468:        int i;
        !           469: 
        !           470:        for (i = 0; i < countof(items); i++)
        !           471:        {
        !           472:                list->insert_last(list, &items[i]);
        !           473:        }
        !           474:        ck_assert(list->equals_function(list, list, (void*)equalsfn));
        !           475:        other = linked_list_create_from_enumerator(list->create_enumerator(list));
        !           476:        ck_assert(list->equals_function(list, other, (void*)equalsfn));
        !           477:        other->remove_last(other, (void**)&x);
        !           478:        ck_assert(!list->equals_function(list, other, (void*)equalsfn));
        !           479:        list->remove_last(list, (void**)&x);
        !           480:        ck_assert(list->equals_function(list, other, (void*)equalsfn));
        !           481:        other->remove_first(other, (void**)&x);
        !           482:        ck_assert(!list->equals_function(list, other, (void*)equalsfn));
        !           483:        list->remove_first(list, (void**)&x);
        !           484:        ck_assert(list->equals_function(list, other, (void*)equalsfn));
        !           485:        while (list->remove_first(list, (void**)&x) == SUCCESS);
        !           486:        while (other->remove_first(other, (void**)&x) == SUCCESS);
        !           487:        ck_assert(list->equals_function(list, other, (void*)equalsfn));
        !           488:        other->destroy(other);
        !           489: }
        !           490: END_TEST
        !           491: 
        !           492: Suite *linked_list_suite_create()
        !           493: {
        !           494:        Suite *s;
        !           495:        TCase *tc;
        !           496: 
        !           497:        s = suite_create("linked list");
        !           498: 
        !           499:        tc = tcase_create("insert/get");
        !           500:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           501:        tcase_add_test(tc, test_insert_first);
        !           502:        tcase_add_test(tc, test_insert_last);
        !           503:        suite_add_tcase(s, tc);
        !           504: 
        !           505:        tc = tcase_create("remove");
        !           506:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           507:        tcase_add_test(tc, test_remove_first);
        !           508:        tcase_add_test(tc, test_remove_last);
        !           509:        tcase_add_test(tc, test_remove);
        !           510:        tcase_add_test(tc, test_remove_callback);
        !           511:        suite_add_tcase(s, tc);
        !           512: 
        !           513:        tc = tcase_create("find");
        !           514:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           515:        tcase_add_test(tc, test_find);
        !           516:        tcase_add_test(tc, test_find_callback);
        !           517:        tcase_add_test(tc, test_find_callback_args);
        !           518:        suite_add_tcase(s, tc);
        !           519: 
        !           520:        tc = tcase_create("invoke");
        !           521:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           522:        tcase_add_test(tc, test_invoke_function);
        !           523:        tcase_add_test(tc, test_invoke_offset);
        !           524:        suite_add_tcase(s, tc);
        !           525: 
        !           526:        tc = tcase_create("clone");
        !           527:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           528:        tcase_add_test(tc, test_clone_offset);
        !           529:        suite_add_tcase(s, tc);
        !           530: 
        !           531:        tc = tcase_create("equals");
        !           532:        tcase_add_checked_fixture(tc, setup_list, teardown_list);
        !           533:        tcase_add_test(tc, test_equals_offset);
        !           534:        tcase_add_test(tc, test_equals_function);
        !           535:        suite_add_tcase(s, tc);
        !           536: 
        !           537:        return s;
        !           538: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>