Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_enumerator.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2013 Tobias Brunner
! 3: * Copyright (C) 2007 Martin Willi
! 4: * HSR Hochschule fuer Technik Rapperswil
! 5: *
! 6: * This program is free software; you can redistribute it and/or modify it
! 7: * under the terms of the GNU General Public License as published by the
! 8: * Free Software Foundation; either version 2 of the License, or (at your
! 9: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 10: *
! 11: * This program is distributed in the hope that it will be useful, but
! 12: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 13: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 14: * for more details.
! 15: */
! 16:
! 17: #include "test_suite.h"
! 18:
! 19: #include <collections/enumerator.h>
! 20: #include <collections/linked_list.h>
! 21:
! 22: /*******************************************************************************
! 23: * token test
! 24: */
! 25:
! 26: static const char *token_results1[] = { "abc", "cde", "efg" };
! 27: static const char *token_results2[] = { "a", "b", "c" };
! 28:
! 29: static struct {
! 30: char *string;
! 31: char *sep;
! 32: char *trim;
! 33: const char **results;
! 34: } token_tests[] = {
! 35: {"abc, cde, efg", ",", " ", token_results1},
! 36: {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345", token_results1},
! 37: {"abc.cde,efg", ",.", "", token_results1},
! 38: {" abc cde efg ", " ", " ", token_results1},
! 39: {"a'abc' c 'cde' cefg", " ", " abcd", token_results1},
! 40: {"'abc' abc 'cde'd 'efg'", " ", " abcd", token_results1},
! 41:
! 42: {"a, b, c", ",", " ", token_results2},
! 43: {"a,b,c", ",", " ", token_results2},
! 44: {" a 1:2 b;3 4c5. ", ":;.,", " 12345", token_results2},
! 45: {"a.b,c", ",.", "", token_results2},
! 46: {" a b c ", " ", " ", token_results2},
! 47: };
! 48:
! 49: START_TEST(test_token)
! 50: {
! 51: enumerator_t *enumerator;
! 52: const char **results;
! 53: char *token;
! 54: int tok = 0;
! 55:
! 56: enumerator = enumerator_create_token(token_tests[_i].string,
! 57: token_tests[_i].sep, token_tests[_i].trim);
! 58: results = token_tests[_i].results;
! 59: while (enumerator->enumerate(enumerator, &token))
! 60: {
! 61: switch (tok)
! 62: {
! 63: case 0:
! 64: case 1:
! 65: case 2:
! 66: ck_assert_str_eq(token, results[tok]);
! 67: break;
! 68: default:
! 69: fail("unexpected token '%s'", token);
! 70: }
! 71: tok++;
! 72: }
! 73: fail_if(tok != 3, "not enough tokens (%d) extracted from '%s'",
! 74: tok, token_tests[_i].string);
! 75: enumerator->destroy(enumerator);
! 76: }
! 77: END_TEST
! 78:
! 79: /*******************************************************************************
! 80: * utilities for filtered, nested and cleaner tests
! 81: */
! 82:
! 83: static int destroy_data_called;
! 84:
! 85: START_SETUP(setup_destroy_data)
! 86: {
! 87: destroy_data_called = 0;
! 88: }
! 89: END_SETUP
! 90:
! 91: START_TEARDOWN(teardown_destroy_data)
! 92: {
! 93: ck_assert_int_eq(destroy_data_called, 1);
! 94: }
! 95: END_TEARDOWN
! 96:
! 97: static void destroy_data(void *data)
! 98: {
! 99: fail_if(data != (void*)101, "data does not match '101' in destructor");
! 100: destroy_data_called++;
! 101: }
! 102:
! 103: /*******************************************************************************
! 104: * filtered test
! 105: */
! 106:
! 107: CALLBACK(filter, bool,
! 108: int *data, enumerator_t *orig, va_list args)
! 109: {
! 110: int *item, *vo, *wo, *xo, *yo, *zo;
! 111:
! 112: VA_ARGS_VGET(args, vo, wo, xo, yo, zo);
! 113:
! 114: if (orig->enumerate(orig, &item))
! 115: {
! 116: int val = *item;
! 117: *vo = val++;
! 118: *wo = val++;
! 119: *xo = val++;
! 120: *yo = val++;
! 121: *zo = val++;
! 122: fail_if(data != (void*)101, "data does not match '101' in filter function");
! 123: return TRUE;
! 124: }
! 125: return FALSE;
! 126: }
! 127:
! 128: CALLBACK(filter_odd, bool,
! 129: void *data, enumerator_t *orig, va_list args)
! 130: {
! 131: int *item, *out;
! 132:
! 133: VA_ARGS_VGET(args, out);
! 134:
! 135: fail_if(data != (void*)101, "data does not match '101' in filter function");
! 136:
! 137: while (orig->enumerate(orig, &item))
! 138: {
! 139: if (*item % 2 == 0)
! 140: {
! 141: *out = *item;
! 142: return TRUE;
! 143: }
! 144: }
! 145: return FALSE;
! 146: }
! 147:
! 148: START_TEST(test_filtered)
! 149: {
! 150: int data[5] = {1,2,3,4,5}, round, v, w, x, y, z;
! 151: linked_list_t *list;
! 152: enumerator_t *enumerator;
! 153:
! 154: list = linked_list_create_with_items(&data[0], &data[1], &data[2], &data[3],
! 155: &data[4], NULL);
! 156:
! 157: round = 1;
! 158: enumerator = enumerator_create_filter(list->create_enumerator(list),
! 159: filter, (void*)101, destroy_data);
! 160: while (enumerator->enumerate(enumerator, &v, &w, &x, &y, &z))
! 161: {
! 162: ck_assert_int_eq(v, round);
! 163: ck_assert_int_eq(w, round + 1);
! 164: ck_assert_int_eq(x, round + 2);
! 165: ck_assert_int_eq(y, round + 3);
! 166: ck_assert_int_eq(z, round + 4);
! 167: round++;
! 168: }
! 169: enumerator->destroy(enumerator);
! 170: ck_assert_int_eq(round, 6);
! 171:
! 172: list->destroy(list);
! 173: }
! 174: END_TEST
! 175:
! 176: START_TEST(test_filtered_filter)
! 177: {
! 178: int data[5] = {1,2,3,4,5}, count, x;
! 179: linked_list_t *list;
! 180: enumerator_t *enumerator;
! 181:
! 182: list = linked_list_create_with_items(&data[0], &data[1], &data[2], &data[3],
! 183: &data[4], NULL);
! 184:
! 185: count = 0;
! 186: /* should also work without destructor, so set this manually */
! 187: destroy_data_called = 1;
! 188: enumerator = enumerator_create_filter(list->create_enumerator(list),
! 189: filter_odd, (void*)101, NULL);
! 190: while (enumerator->enumerate(enumerator, &x))
! 191: {
! 192: ck_assert(x % 2 == 0);
! 193: count++;
! 194: }
! 195: enumerator->destroy(enumerator);
! 196: ck_assert_int_eq(count, 2);
! 197:
! 198: list->destroy(list);
! 199: }
! 200: END_TEST
! 201:
! 202: /*******************************************************************************
! 203: * nested test
! 204: */
! 205:
! 206: static enumerator_t* create_inner(linked_list_t *outer, void *data)
! 207: {
! 208: fail_if(data != (void*)101, "data does not match '101' in nested constr.");
! 209: return outer->create_enumerator(outer);
! 210: }
! 211:
! 212: static enumerator_t* create_inner_null(void *outer, void *data)
! 213: {
! 214: ck_assert(outer == (void*)1);
! 215: fail_if(data != (void*)101, "data does not match '101' in nested constr.");
! 216: return NULL;
! 217: }
! 218:
! 219: START_TEST(test_nested)
! 220: {
! 221: linked_list_t *list, *l1, *l2, *l3;
! 222: enumerator_t *enumerator;
! 223: intptr_t x;
! 224: int round;
! 225:
! 226: l1 = linked_list_create_with_items((void*)1, (void*)2, NULL);
! 227: l2 = linked_list_create();
! 228: l3 = linked_list_create_with_items((void*)3, (void*)4, (void*)5, NULL);
! 229: list = linked_list_create_with_items(l1, l2, l3, NULL);
! 230:
! 231: round = 1;
! 232: enumerator = enumerator_create_nested(list->create_enumerator(list),
! 233: (void*)create_inner, (void*)101, destroy_data);
! 234: while (enumerator->enumerate(enumerator, &x))
! 235: {
! 236: ck_assert_int_eq(round, x);
! 237: round++;
! 238: }
! 239: enumerator->destroy(enumerator);
! 240: ck_assert_int_eq(round, 6);
! 241:
! 242: list->destroy(list);
! 243: l1->destroy(l1);
! 244: l2->destroy(l2);
! 245: l3->destroy(l3);
! 246: }
! 247: END_TEST
! 248:
! 249: START_TEST(test_nested_reset)
! 250: {
! 251: linked_list_t *list, *l1, *l2, *l3;
! 252: enumerator_t *outer, *enumerator;
! 253: intptr_t x;
! 254: int count = 0;
! 255:
! 256: l1 = linked_list_create_with_items((void*)1, (void*)2, NULL);
! 257: l2 = linked_list_create();
! 258: l3 = linked_list_create_with_items((void*)3, (void*)4, (void*)5, NULL);
! 259: list = linked_list_create_with_items(l1, l2, l3, NULL);
! 260:
! 261: outer = list->create_enumerator(list);
! 262: enumerator = enumerator_create_nested(outer, (void*)create_inner,
! 263: (void*)101, destroy_data);
! 264: while (enumerator->enumerate(enumerator, &x))
! 265: {
! 266: count++;
! 267: }
! 268: ck_assert_int_eq(count, 5);
! 269:
! 270: list->reset_enumerator(list, outer);
! 271: ck_assert(enumerator->enumerate(enumerator, &x));
! 272: ck_assert_int_eq(x, 1);
! 273: enumerator->destroy(enumerator);
! 274:
! 275: list->destroy(list);
! 276: l1->destroy(l1);
! 277: l2->destroy(l2);
! 278: l3->destroy(l3);
! 279: }
! 280: END_TEST
! 281:
! 282: START_TEST(test_nested_empty)
! 283: {
! 284: linked_list_t *list;
! 285: enumerator_t *enumerator;
! 286: intptr_t x;
! 287: int count;
! 288:
! 289: list = linked_list_create();
! 290: count = 0;
! 291: enumerator = enumerator_create_nested(list->create_enumerator(list),
! 292: (void*)create_inner, (void*)101, destroy_data);
! 293: while (enumerator->enumerate(enumerator, &x))
! 294: {
! 295: count++;
! 296: }
! 297: enumerator->destroy(enumerator);
! 298: ck_assert_int_eq(count, 0);
! 299:
! 300: list->destroy(list);
! 301: }
! 302: END_TEST
! 303:
! 304: START_TEST(test_nested_null)
! 305: {
! 306: linked_list_t *list;
! 307: enumerator_t *enumerator;
! 308: intptr_t x;
! 309: int count;
! 310:
! 311: list = linked_list_create_with_items((void*)1, NULL);
! 312:
! 313: count = 0;
! 314: /* should also work without destructor, so set this manually */
! 315: destroy_data_called = 1;
! 316: enumerator = enumerator_create_nested(list->create_enumerator(list),
! 317: (void*)create_inner_null, (void*)101, NULL);
! 318: while (enumerator->enumerate(enumerator, &x))
! 319: {
! 320: count++;
! 321: }
! 322: enumerator->destroy(enumerator);
! 323: ck_assert_int_eq(count, 0);
! 324:
! 325: list->destroy(list);
! 326: }
! 327: END_TEST
! 328:
! 329: /*******************************************************************************
! 330: * cleaner test
! 331: */
! 332:
! 333: START_TEST(test_cleaner)
! 334: {
! 335: enumerator_t *enumerator;
! 336: linked_list_t *list;
! 337: intptr_t x;
! 338: int round;
! 339:
! 340: list = linked_list_create_with_items((void*)1, (void*)2, NULL);
! 341:
! 342: round = 1;
! 343: enumerator = enumerator_create_cleaner(list->create_enumerator(list),
! 344: destroy_data, (void*)101);
! 345: while (enumerator->enumerate(enumerator, &x))
! 346: {
! 347: ck_assert_int_eq(round, x);
! 348: round++;
! 349: }
! 350: ck_assert_int_eq(round, 3);
! 351: enumerator->destroy(enumerator);
! 352: list->destroy(list);
! 353: }
! 354: END_TEST
! 355:
! 356: /*******************************************************************************
! 357: * single test
! 358: */
! 359:
! 360: static void single_cleanup(void *data)
! 361: {
! 362: ck_assert_int_eq((intptr_t)data, 1);
! 363: }
! 364:
! 365: static void do_test_single(enumerator_t *enumerator)
! 366: {
! 367: intptr_t x;
! 368:
! 369: ck_assert(enumerator->enumerate(enumerator, &x));
! 370: ck_assert_int_eq(x, 1);
! 371: ck_assert(!enumerator->enumerate(enumerator, &x));
! 372: enumerator->destroy(enumerator);
! 373: }
! 374:
! 375: START_TEST(test_single)
! 376: {
! 377: enumerator_t *enumerator;
! 378:
! 379: enumerator = enumerator_create_single((void*)1, NULL);
! 380: do_test_single(enumerator);
! 381: }
! 382: END_TEST
! 383:
! 384: START_TEST(test_single_cleanup)
! 385: {
! 386: enumerator_t *enumerator;
! 387:
! 388: enumerator = enumerator_create_single((void*)1, single_cleanup);
! 389: do_test_single(enumerator);
! 390: }
! 391: END_TEST
! 392:
! 393: Suite *enumerator_suite_create()
! 394: {
! 395: Suite *s;
! 396: TCase *tc;
! 397:
! 398: s = suite_create("enumerator");
! 399:
! 400: tc = tcase_create("tokens");
! 401: tcase_add_loop_test(tc, test_token, 0, countof(token_tests));
! 402: suite_add_tcase(s, tc);
! 403:
! 404: tc = tcase_create("filtered");
! 405: tcase_add_checked_fixture(tc, setup_destroy_data, teardown_destroy_data);
! 406: tcase_add_test(tc, test_filtered);
! 407: tcase_add_test(tc, test_filtered_filter);
! 408: suite_add_tcase(s, tc);
! 409:
! 410: tc = tcase_create("nested");
! 411: tcase_add_checked_fixture(tc, setup_destroy_data, teardown_destroy_data);
! 412: tcase_add_test(tc, test_nested);
! 413: tcase_add_test(tc, test_nested_reset);
! 414: tcase_add_test(tc, test_nested_empty);
! 415: tcase_add_test(tc, test_nested_null);
! 416: suite_add_tcase(s, tc);
! 417:
! 418: tc = tcase_create("cleaner");
! 419: tcase_add_checked_fixture(tc, setup_destroy_data, teardown_destroy_data);
! 420: tcase_add_test(tc, test_cleaner);
! 421: suite_add_tcase(s, tc);
! 422:
! 423: tc = tcase_create("single");
! 424: tcase_add_test(tc, test_single);
! 425: tcase_add_test(tc, test_single_cleanup);
! 426: suite_add_tcase(s, tc);
! 427:
! 428: return s;
! 429: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>