Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_enum.c, revision 1.1.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 <utils/utils.h>
                     19: 
                     20: /*******************************************************************************
                     21:  * continuous enum
                     22:  */
                     23: enum {
                     24:        CONT1,
                     25:        CONT2,
                     26:        CONT3,
                     27:        CONT4,
                     28:        CONT5,
                     29: } test_enum_cont;
                     30: 
                     31: ENUM_BEGIN(test_enum_cont_names, CONT1, CONT5,
                     32:        "CONT1", "CONT2", "CONT3", "CONT4", "CONT5");
                     33: ENUM_END(test_enum_cont_names, CONT5);
                     34: 
                     35: /*******************************************************************************
                     36:  * split enum
                     37:  */
                     38: enum {
                     39:        SPLIT1 = 1,
                     40:        SPLIT2,
                     41:        SPLIT3 = 5,
                     42:        SPLIT4,
                     43:        SPLIT5 = 255,
                     44: } test_enum_split;
                     45: 
                     46: ENUM_BEGIN(test_enum_split_names, SPLIT1, SPLIT2,
                     47:        "SPLIT1", "SPLIT2");
                     48: ENUM_NEXT(test_enum_split_names, SPLIT3, SPLIT4, SPLIT2,
                     49:        "SPLIT3", "SPLIT4");
                     50: ENUM_NEXT(test_enum_split_names, SPLIT5, SPLIT5, SPLIT4,
                     51:        "SPLIT5");
                     52: ENUM_END(test_enum_split_names, SPLIT5);
                     53: 
                     54: /*******************************************************************************
                     55:  * enum flags
                     56:  */
                     57: enum {
                     58:        FLAG1 = (1 << 0),
                     59:        FLAG2 = (1 << 1),
                     60:        FLAG3 = (1 << 2),
                     61:        FLAG4 = (1 << 3),
                     62:        FLAG5 = (1 << 4),
                     63:        FLAG6 = (1 << 5),
                     64:        FLAG7 = (1 << 6),
                     65:        FLAG8 = (1 << 7),
                     66:        FLAG9 = (1 << 8),
                     67:        FLAG10 = (1 << 9),
                     68:        FLAG11 = (1 << 10),
                     69:        FLAG12 = (1 << 11),
                     70: } test_enum_flags;
                     71: 
                     72: ENUM_FLAGS(test_enum_flags_names, FLAG1, FLAG5,
                     73:        "FLAG1", "FLAG2", "FLAG3", "FLAG4", "FLAG5");
                     74: 
                     75: ENUM_FLAGS(test_enum_flags_incomplete_names, FLAG3, FLAG4,
                     76:        "FLAG3", "FLAG4");
                     77: 
                     78: ENUM_FLAGS(test_enum_flags_null_names, FLAG1, FLAG4,
                     79:        "FLAG1", NULL, "FLAG3", NULL);
                     80: 
                     81: ENUM_FLAGS(test_enum_flags_overflow_names, FLAG1, FLAG12,
                     82:        "OVERFLOWFLAGLONGNAME1",  "OVERFLOWFLAGLONGNAME2",  "OVERFLOWFLAGLONGNAME3",
                     83:        "OVERFLOWFLAGLONGNAME4",  "OVERFLOWFLAGLONGNAME5",  "OVERFLOWFLAGLONGNAME6",
                     84:        "OVERFLOWFLAGLONGNAME7",  "OVERFLOWFLAGLONGNAME8",  "OVERFLOWFLAGLONGNAME9",
                     85:        "OVERFLOWFLAGLONGNAME10", "OVERFLOWFLAGLONGNAME11", "OVERFLOWFLAGLONGNAME12");
                     86: 
                     87: /*******************************************************************************
                     88:  * enum_to_name
                     89:  */
                     90: 
                     91: static struct {
                     92:        int val;
                     93:        char *str;
                     94: } name_tests_cont[] = {
                     95:        {-1, NULL},
                     96:        {CONT1, "CONT1"},
                     97:        {CONT2, "CONT2"},
                     98:        {CONT3, "CONT3"},
                     99:        {CONT4, "CONT4"},
                    100:        {CONT5, "CONT5"},
                    101:        {5, NULL},
                    102: }, name_tests_split[] = {
                    103:        {-1, NULL},
                    104:        {0, NULL},
                    105:        {SPLIT1, "SPLIT1"},
                    106:        {SPLIT2, "SPLIT2"},
                    107:        {3, NULL},
                    108:        {4, NULL},
                    109:        {SPLIT3, "SPLIT3"},
                    110:        {SPLIT4, "SPLIT4"},
                    111:        {7, NULL},
                    112:        {254, NULL},
                    113:        {SPLIT5, "SPLIT5"},
                    114:        {256, NULL},
                    115: };
                    116: 
                    117: START_TEST(test_enum_to_name_cont)
                    118: {
                    119:        char *str = enum_to_name(test_enum_cont_names, name_tests_cont[_i].val);
                    120:        if (str)
                    121:        {
                    122:                ck_assert_str_eq(str, name_tests_cont[_i].str);
                    123:        }
                    124:        else
                    125:        {
                    126:                ck_assert(str == name_tests_cont[_i].str);
                    127:        }
                    128: }
                    129: END_TEST
                    130: 
                    131: START_TEST(test_enum_to_name_split)
                    132: {
                    133:        char *str = enum_to_name(test_enum_split_names, name_tests_split[_i].val);
                    134:        if (str)
                    135:        {
                    136:                ck_assert_str_eq(str, name_tests_split[_i].str);
                    137:        }
                    138:        else
                    139:        {
                    140:                ck_assert(str == name_tests_split[_i].str);
                    141:        }
                    142: }
                    143: END_TEST
                    144: 
                    145: /*******************************************************************************
                    146:  * enum_from_name
                    147:  */
                    148: 
                    149: static struct {
                    150:        bool found;
                    151:        int val;
                    152:        char *str;
                    153: } enum_tests_cont[] = {
                    154:        {TRUE, CONT1, "CONT1"},
                    155:        {TRUE, CONT2, "CONT2"},
                    156:        {TRUE, CONT2, "CoNt2"},
                    157:        {TRUE, CONT3, "CONT3"},
                    158:        {TRUE, CONT4, "CONT4"},
                    159:        {TRUE, CONT5, "CONT5"},
                    160:        {FALSE, 0, "asdf"},
                    161:        {FALSE, 0, ""},
                    162:        {FALSE, 0, NULL},
                    163: }, enum_tests_split[] = {
                    164:        {TRUE, SPLIT1, "SPLIT1"},
                    165:        {TRUE, SPLIT1, "split1"},
                    166:        {TRUE, SPLIT2, "SPLIT2"},
                    167:        {TRUE, SPLIT2, "SpLiT2"},
                    168:        {TRUE, SPLIT3, "SPLIT3"},
                    169:        {TRUE, SPLIT4, "SPLIT4"},
                    170:        {TRUE, SPLIT5, "SPLIT5"},
                    171:        {FALSE, 0, "asdf"},
                    172:        {FALSE, 0, ""},
                    173:        {FALSE, 0, NULL},
                    174: };
                    175: 
                    176: START_TEST(test_enum_from_name_cont)
                    177: {
                    178:        int val = 0;
                    179:        bool found;
                    180: 
                    181:        found = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str, &val);
                    182:        ck_assert(enum_tests_cont[_i].found == found);
                    183:        ck_assert_int_eq(val, enum_tests_cont[_i].val);
                    184: }
                    185: END_TEST
                    186: 
                    187: START_TEST(test_enum_from_name_split)
                    188: {
                    189:        int val = 0;
                    190:        bool found;
                    191: 
                    192:        found = enum_from_name(test_enum_split_names, enum_tests_split[_i].str, &val);
                    193:        ck_assert(enum_tests_split[_i].found == found);
                    194:        ck_assert_int_eq(val, enum_tests_split[_i].val);
                    195: }
                    196: END_TEST
                    197: 
                    198: /*******************************************************************************
                    199:  * enum_printf_hook
                    200:  */
                    201: 
                    202: static struct {
                    203:        int val;
                    204:        char *str;
                    205: } printf_tests_cont[] = {
                    206:        {-1, "(-1)"},
                    207:        {CONT1, "CONT1"},
                    208:        {CONT2, "CONT2"},
                    209:        {CONT3, "CONT3"},
                    210:        {CONT4, "CONT4"},
                    211:        {CONT5, "CONT5"},
                    212:        {5, "(5)"},
                    213: }, printf_tests_split[] = {
                    214:        {-1, "(-1)"},
                    215:        {0, "(0)"},
                    216:        {SPLIT1, "SPLIT1"},
                    217:        {SPLIT2, "SPLIT2"},
                    218:        {3, "(3)"},
                    219:        {4, "(4)"},
                    220:        {SPLIT3, "SPLIT3"},
                    221:        {SPLIT4, "SPLIT4"},
                    222:        {7, "(7)"},
                    223:        {254, "(254)"},
                    224:        {SPLIT5, "SPLIT5"},
                    225:        {256, "(256)"},
                    226: };
                    227: 
                    228: /*******************************************************************************
                    229:  * flag_to_name
                    230:  */
                    231: 
                    232: static struct {
                    233:        int val;
                    234:        char *str;
                    235: } printf_tests_flags[] = {
                    236:        {0, "(unset)"},
                    237:        {FLAG1, "FLAG1"},
                    238:        {FLAG2, "FLAG2"},
                    239:        {FLAG3, "FLAG3"},
                    240:        {FLAG4, "FLAG4"},
                    241:        {FLAG5, "FLAG5"},
                    242:        {FLAG1 | FLAG3, "FLAG1 | FLAG3"},
                    243:        {FLAG1 | FLAG3 | 32, "FLAG1 | FLAG3 | (0x20)"},
                    244:        {FLAG1 | FLAG3 | 32 | 64, "FLAG1 | FLAG3 | (0x20) | (0x40)"},
                    245:        {0x20, "(0x20)"},
                    246:        {0x80000000, "(0x80000000)"},
                    247:        {0xFFFFF, "FLAG1 | FLAG2 | FLAG3 | FLAG4 | "
                    248:                          "FLAG5 | (0x20) | (0x40) | (0x80) | "
                    249:                         "(0x100) | (0x200) | (0x400) | (0x800) | "
                    250:                         "(0x1000) | (0x2000) | (0x4000) | (0x8000) | "
                    251:                         "(0x10000) | (0x20000) | (0x40000) | (0x80000)"},
                    252: }, printf_tests_flags_incomplete[] = {
                    253:        {FLAG1, "(0x1)"},
                    254:        {FLAG1 | FLAG2 | FLAG3, "(0x1) | (0x2) | FLAG3"},
                    255:        {FLAG3 | FLAG4 | FLAG5, "FLAG3 | FLAG4 | (0x10)"},
                    256: }, printf_tests_flags_null[] = {
                    257:        {FLAG1 | FLAG2 | FLAG3 | FLAG4, "FLAG1 | FLAG3"},
                    258: }, printf_tests_flags_overflow[] = {
                    259:        {0xFFFFFFFF, "(0xFFFFFFFF)"},
                    260: }, printf_tests_flags_noflagenum[] = {
                    261:        {-1, "(-1)"},
                    262:        {6435, "(6435)"},
                    263: }, enum_flags_to_string_tests[] = {
                    264:        {-1, NULL},
                    265:        {6435, NULL},
                    266: };
                    267: 
                    268: START_TEST(test_enum_printf_hook_cont)
                    269: {
                    270:        char buf[128];
                    271: 
                    272:        snprintf(buf, sizeof(buf), "%N",
                    273:                         test_enum_cont_names, printf_tests_cont[_i].val);
                    274:        ck_assert_str_eq(printf_tests_cont[_i].str, buf);
                    275: }
                    276: END_TEST
                    277: 
                    278: START_TEST(test_enum_printf_hook_split)
                    279: {
                    280:        char buf[128];
                    281: 
                    282:        snprintf(buf, sizeof(buf), "%N",
                    283:                         test_enum_split_names, printf_tests_split[_i].val);
                    284:        ck_assert_str_eq(printf_tests_split[_i].str, buf);
                    285: }
                    286: END_TEST
                    287: 
                    288: START_TEST(test_enum_printf_hook_null)
                    289: {
                    290:        char buf[16];
                    291: 
                    292:        snprintf(buf, sizeof(buf), "%N", NULL, 7);
                    293:        ck_assert_str_eq("(7)", buf);
                    294: }
                    295: END_TEST
                    296: 
                    297: START_TEST(test_enum_printf_hook_flags)
                    298: {
                    299:        char buf[1024];
                    300: 
                    301:        snprintf(buf, sizeof(buf), "%N", test_enum_flags_names,
                    302:                         printf_tests_flags[_i].val);
                    303:        ck_assert_str_eq(printf_tests_flags[_i].str, buf);
                    304: }
                    305: END_TEST
                    306: 
                    307: START_TEST(test_enum_printf_hook_flags_incomplete)
                    308: {
                    309:        char buf[1024];
                    310: 
                    311:        snprintf(buf, sizeof(buf), "%N", test_enum_flags_incomplete_names,
                    312:                         printf_tests_flags_incomplete[_i].val);
                    313:        ck_assert_str_eq(printf_tests_flags_incomplete[_i].str, buf);
                    314: }
                    315: END_TEST
                    316: 
                    317: START_TEST(test_enum_printf_hook_flags_null)
                    318: {
                    319:        char buf[1024];
                    320: 
                    321:        snprintf(buf, sizeof(buf), "%N", test_enum_flags_null_names,
                    322:                         printf_tests_flags_null[_i].val);
                    323:        ck_assert_str_eq(printf_tests_flags_null[_i].str, buf);
                    324: }
                    325: END_TEST
                    326: 
                    327: START_TEST(test_enum_printf_hook_flags_overflow)
                    328: {
                    329:        char buf[1024];
                    330: 
                    331:        snprintf(buf, sizeof(buf), "%N", test_enum_flags_overflow_names,
                    332:                         printf_tests_flags_overflow[_i].val);
                    333:        ck_assert_str_eq(printf_tests_flags_overflow[_i].str, buf);
                    334: }
                    335: END_TEST
                    336: 
                    337: START_TEST(test_enum_printf_hook_flags_noflagenum)
                    338: {
                    339:        char buf[1024];
                    340: 
                    341:        snprintf(buf, sizeof(buf), "%N", test_enum_cont_names,
                    342:                         printf_tests_flags_noflagenum[_i].val);
                    343:        ck_assert_str_eq(printf_tests_flags_noflagenum[_i].str, buf);
                    344: }
                    345: END_TEST
                    346: 
                    347: START_TEST(test_enum_flags_to_string)
                    348: {
                    349:        char buf[1], *str;
                    350: 
                    351:        str = enum_flags_to_string(test_enum_flags_names,
                    352:                        enum_flags_to_string_tests[_i].val, buf, sizeof(buf));
                    353:        if (str)
                    354:        {
                    355:                ck_assert_str_eq(enum_flags_to_string_tests[_i].str, str);
                    356:        }
                    357:        else
                    358:        {
                    359:                ck_assert(str == enum_flags_to_string_tests[_i].str);
                    360:        }
                    361: }
                    362: END_TEST
                    363: 
                    364: START_TEST(test_enum_flags_to_string_noflagenum)
                    365: {
                    366:        char buf[1024];
                    367: 
                    368:        enum_flags_to_string(test_enum_cont_names,
                    369:                        printf_tests_flags_noflagenum[_i].val, buf, sizeof(buf));
                    370:        ck_assert_str_eq(printf_tests_flags_noflagenum[_i].str, buf);
                    371: }
                    372: END_TEST
                    373: 
                    374: START_TEST(test_enum_printf_hook_width)
                    375: {
                    376:        char buf[128];
                    377: 
                    378:        snprintf(buf, sizeof(buf), "%10N", test_enum_cont_names, CONT1);
                    379:        ck_assert_str_eq("     CONT1", buf);
                    380:        snprintf(buf, sizeof(buf), "%-*N", 10, test_enum_cont_names, CONT2);
                    381:        ck_assert_str_eq("CONT2     ", buf);
                    382:        snprintf(buf, sizeof(buf), "%3N", test_enum_cont_names, CONT3);
                    383:        ck_assert_str_eq("CONT3", buf);
                    384: }
                    385: END_TEST
                    386: 
                    387: Suite *enum_suite_create()
                    388: {
                    389:        Suite *s;
                    390:        TCase *tc;
                    391: 
                    392:        s = suite_create("enum");
                    393: 
                    394:        tc = tcase_create("enum_to_name");
                    395:        tcase_add_loop_test(tc, test_enum_to_name_cont, 0, countof(name_tests_cont));
                    396:        tcase_add_loop_test(tc, test_enum_to_name_split, 0, countof(name_tests_split));
                    397:        suite_add_tcase(s, tc);
                    398: 
                    399:        tc = tcase_create("enum_from_name");
                    400:        tcase_add_loop_test(tc, test_enum_from_name_cont, 0, countof(enum_tests_cont));
                    401:        tcase_add_loop_test(tc, test_enum_from_name_split, 0, countof(enum_tests_split));
                    402:        suite_add_tcase(s, tc);
                    403: 
                    404:        tc = tcase_create("enum_flags_to_string");
                    405:        tcase_add_loop_test(tc, test_enum_flags_to_string, 0, countof(enum_flags_to_string_tests));
                    406:        tcase_add_loop_test(tc, test_enum_flags_to_string_noflagenum, 0, countof(printf_tests_flags_noflagenum));
                    407:        suite_add_tcase(s, tc);
                    408: 
                    409:        tc = tcase_create("enum_printf_hook");
                    410:        tcase_add_loop_test(tc, test_enum_printf_hook_cont, 0, countof(printf_tests_cont));
                    411:        tcase_add_loop_test(tc, test_enum_printf_hook_split, 0, countof(printf_tests_split));
                    412:        tcase_add_test(tc, test_enum_printf_hook_null);
                    413:        tcase_add_loop_test(tc, test_enum_printf_hook_flags, 0, countof(printf_tests_flags));
                    414:        tcase_add_loop_test(tc, test_enum_printf_hook_flags_incomplete, 0, countof(printf_tests_flags_incomplete));
                    415:        tcase_add_loop_test(tc, test_enum_printf_hook_flags_null, 0, countof(printf_tests_flags_null));
                    416:        tcase_add_loop_test(tc, test_enum_printf_hook_flags_overflow, 0, countof(printf_tests_flags_overflow));
                    417:        tcase_add_loop_test(tc, test_enum_printf_hook_flags_noflagenum, 0, countof(printf_tests_flags_noflagenum));
                    418:        tcase_add_test(tc, test_enum_printf_hook_width);
                    419:        suite_add_tcase(s, tc);
                    420: 
                    421:        return s;
                    422: }

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