Annotation of embedaddon/confuse/tests/suite_list.c, revision 1.1.1.1

1.1       misho       1: #include "check_confuse.h"
                      2: #include <string.h>
                      3: 
                      4: static cfg_t *cfg;
                      5: static int numopts = 0;
                      6: 
                      7: static void list_setup(void)
                      8: {
                      9:     cfg_opt_t subsec_opts[] = 
                     10:     {
                     11:         CFG_STR_LIST("subsubstring", 0, CFGF_NONE),
                     12:         CFG_INT_LIST("subsubinteger", 0, CFGF_NONE),
                     13:         CFG_FLOAT_LIST("subsubfloat", 0, CFGF_NONE),
                     14:         CFG_BOOL_LIST("subsubbool", 0, CFGF_NONE),
                     15:         CFG_END()
                     16:     };
                     17: 
                     18:     cfg_opt_t sec_opts[] =
                     19:     {
                     20:         CFG_STR_LIST("substring", "{subdefault1, subdefault2}", CFGF_NONE),
                     21:         CFG_INT_LIST("subinteger", "{17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 300}", CFGF_NONE), /* 14 values */
                     22:         CFG_FLOAT_LIST("subfloat", "{8.37}", CFGF_NONE),
                     23:         CFG_BOOL_LIST("subbool", "{true}", CFGF_NONE),
                     24:         CFG_SEC("subsection", subsec_opts, CFGF_MULTI | CFGF_TITLE),
                     25:         CFG_END()
                     26:     };
                     27: 
                     28:     cfg_opt_t opts[] = 
                     29:     {
                     30:         CFG_STR_LIST("string", "{default1, default2, default3}", CFGF_NONE),
                     31:         CFG_INT_LIST("integer", "{4711, 123456789}", CFGF_NONE),
                     32:         CFG_FLOAT_LIST("float", "{0.42}", CFGF_NONE),
                     33:         CFG_BOOL_LIST("bool", "{false, true, no, yes, off, on}", CFGF_NONE),
                     34:         CFG_SEC("section", sec_opts, CFGF_MULTI),
                     35:         CFG_END()
                     36:     };
                     37: 
                     38:     cfg = cfg_init(opts, 0);
                     39:     numopts = cfg_numopts(opts);
                     40:     fail_unless(numopts == 5);
                     41: 
                     42:     memset(opts, 0, sizeof(opts));
                     43:     memset(sec_opts, 0, sizeof(sec_opts));
                     44:     memset(subsec_opts, 0, sizeof(subsec_opts));
                     45: }
                     46: 
                     47: static void list_teardown(void)
                     48: {
                     49:     cfg_free(cfg);
                     50: }
                     51: 
                     52: static void list_string_test(void)
                     53: {
                     54:     char *buf;
                     55: 
                     56:     fail_unless(cfg_size(cfg, "string") == 3);
                     57:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "string")) == 3);
                     58:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 0), "default1") == 0);
                     59:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 1), "default2") == 0);
                     60:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 2), "default3") == 0);
                     61:     buf = "string = {\"manually\", 'set'}";
                     62:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                     63:     fail_unless(cfg_size(cfg, "string") == 2);
                     64:     fail_unless(strcmp(cfg_getstr(cfg, "string"), "manually") == 0);
                     65:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 1), "set") == 0);
                     66:     cfg_setstr(cfg, "string", "manually set");
                     67:     fail_unless(strcmp(cfg_getstr(cfg, "string"), "manually set") == 0);
                     68:     cfg_setnstr(cfg, "string", "foobar", 1);
                     69:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 1), "foobar") == 0);
                     70: 
                     71:     cfg_addlist(cfg, "string", 3, "foo", "bar", "baz");
                     72:     fail_unless(cfg_size(cfg, "string") == 5);
                     73:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 3), "bar") == 0);
                     74: 
                     75:     cfg_setlist(cfg, "string", 3, "baz", "foo", "bar");
                     76:     fail_unless(cfg_size(cfg, "string") == 3);
                     77:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 0), "baz") == 0);
                     78: 
                     79:     buf = "string += {gaz, 'onk'}";
                     80:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                     81:     fail_unless(cfg_size(cfg, "string") == 5);
                     82:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 3), "gaz") == 0);
                     83:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 4), "onk") == 0);
                     84: }
                     85: 
                     86: static void list_integer_test(void)
                     87: {
                     88:     char *buf;
                     89: 
                     90:     fail_unless(cfg_size(cfg, "integer") == 2);
                     91:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "integer")) == 2);
                     92: 
                     93:     fail_unless(cfg_getint(cfg, "integer") == 4711);
                     94:     fail_unless(cfg_getnint(cfg, "integer", 1) == 123456789);
                     95:     buf = "integer = {-46}";
                     96:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                     97:     fail_unless(cfg_size(cfg, "integer") == 1);
                     98:     fail_unless(cfg_getnint(cfg, "integer", 0) == -46);
                     99:     cfg_setnint(cfg, "integer", 999999, 1);
                    100:     fail_unless(cfg_size(cfg, "integer") == 2);
                    101:     fail_unless(cfg_getnint(cfg, "integer", 1) == 999999);
                    102: 
                    103:     cfg_addlist(cfg, "integer", 3, 11, 22, 33);
                    104:     fail_unless(cfg_size(cfg, "integer") == 5);
                    105:     fail_unless(cfg_getnint(cfg, "integer", 3) == 22);
                    106: 
                    107:     cfg_setlist(cfg, "integer", 3, 11, 22, 33);
                    108:     fail_unless(cfg_size(cfg, "integer") == 3);
                    109:     fail_unless(cfg_getnint(cfg, "integer", 0) == 11);
                    110: 
                    111:     buf = "integer += {-1234567890, 1234567890}";
                    112:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    113:     fail_unless(cfg_size(cfg, "integer") == 5);
                    114:     fail_unless(cfg_getnint(cfg, "integer", 3) == -1234567890);
                    115:     fail_unless(cfg_getnint(cfg, "integer", 4) == 1234567890);
                    116: }
                    117: 
                    118: static void list_float_test(void)
                    119: {
                    120:     char *buf;
                    121: 
                    122:     fail_unless(cfg_size(cfg, "float") == 1);
                    123:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "float")) == 1);
                    124: 
                    125:     fail_unless(cfg_getfloat(cfg, "float") == 0.42);
                    126:     fail_unless(cfg_getnfloat(cfg, "float", 0) == 0.42);
                    127: 
                    128:     buf = "float = {-46.777, 0.1, 0.2, 0.17, 17.123}";
                    129:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    130:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "float")) == 5);
                    131:     fail_unless(cfg_getnfloat(cfg, "float", 0) == -46.777);
                    132:     fail_unless(cfg_getnfloat(cfg, "float", 1) == 0.1);
                    133:     fail_unless(cfg_getnfloat(cfg, "float", 2) == 0.2);
                    134:     fail_unless(cfg_getnfloat(cfg, "float", 3) == 0.17);
                    135:     fail_unless(cfg_getnfloat(cfg, "float", 4) == 17.123);
                    136: 
                    137:     cfg_setnfloat(cfg, "float", 5.1234e2, 1);
                    138:     fail_unless(cfg_getnfloat(cfg, "float", 1) == 5.1234e2);
                    139: 
                    140:     cfg_addlist(cfg, "float", 1, 11.2233);
                    141:     fail_unless(cfg_size(cfg, "float") == 6);
                    142:     fail_unless(cfg_getnfloat(cfg, "float", 5) == 11.2233);
                    143: 
                    144:     cfg_setlist(cfg, "float", 2, .3, -18.17e-7);
                    145:     fail_unless(cfg_size(cfg, "float") == 2);
                    146:     fail_unless(cfg_getnfloat(cfg, "float", 0) == 0.3);
                    147:     fail_unless(cfg_getnfloat(cfg, "float", 1) == -18.17e-7);
                    148: 
                    149:     buf = "float += {64.64, 1234.567890}";
                    150:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    151:     fail_unless(cfg_size(cfg, "float") == 4);
                    152:     fail_unless(cfg_getnfloat(cfg, "float", 2) == 64.64);
                    153:     fail_unless(cfg_getnfloat(cfg, "float", 3) == 1234.567890);
                    154: }
                    155: 
                    156: static void list_bool_test(void)
                    157: {
                    158:     char *buf;
                    159: 
                    160:     fail_unless(cfg_size(cfg, "bool") == 6);
                    161:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "bool")) == 6);
                    162:     fail_unless(cfg_getnbool(cfg, "bool", 0) == cfg_false);
                    163:     fail_unless(cfg_getnbool(cfg, "bool", 1) == cfg_true);
                    164:     fail_unless(cfg_getnbool(cfg, "bool", 2) == cfg_false);
                    165:     fail_unless(cfg_getnbool(cfg, "bool", 3) == cfg_true);
                    166:     fail_unless(cfg_getnbool(cfg, "bool", 4) == cfg_false);
                    167:     fail_unless(cfg_getnbool(cfg, "bool", 5) == cfg_true);
                    168: 
                    169:     buf = "bool = {yes, yes, no, false, true}";
                    170:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    171:     fail_unless(cfg_size(cfg, "bool") == 5);
                    172:     fail_unless(cfg_getbool(cfg, "bool") == cfg_true);
                    173:     fail_unless(cfg_getnbool(cfg, "bool", 1) == cfg_true);
                    174:     fail_unless(cfg_getnbool(cfg, "bool", 2) == cfg_false);
                    175:     fail_unless(cfg_getnbool(cfg, "bool", 3) == cfg_false);
                    176:     fail_unless(cfg_getnbool(cfg, "bool", 4) == cfg_true);
                    177: 
                    178:     cfg_setbool(cfg, "bool", cfg_false);
                    179:     fail_unless(cfg_getbool(cfg, "bool") == cfg_false);
                    180:     cfg_setnbool(cfg, "bool", cfg_false, 1);
                    181:     fail_unless(cfg_getnbool(cfg, "bool", 1) == cfg_false);
                    182: 
                    183:     cfg_addlist(cfg, "bool", 2, cfg_true, cfg_false);
                    184:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "bool")) == 7);
                    185:     fail_unless(cfg_getnbool(cfg, "bool", 5) == cfg_true);
                    186: 
                    187:     cfg_setlist(cfg, "bool", 4, cfg_true, cfg_true, cfg_false, cfg_true);
                    188:     fail_unless(cfg_size(cfg, "bool") == 4);
                    189:     fail_unless(cfg_getnbool(cfg, "bool", 0) == cfg_true);
                    190:     fail_unless(cfg_getnbool(cfg, "bool", 1) == cfg_true);
                    191:     fail_unless(cfg_getnbool(cfg, "bool", 2) == cfg_false);
                    192:     fail_unless(cfg_getnbool(cfg, "bool", 3) == cfg_true);
                    193: 
                    194:     buf = "bool += {false, false}";
                    195:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    196:     fail_unless(cfg_size(cfg, "bool") == 6);
                    197:     fail_unless(cfg_getnbool(cfg, "bool", 4) == cfg_false);
                    198:     fail_unless(cfg_getnbool(cfg, "bool", 5) == cfg_false);
                    199: }
                    200: 
                    201: static void list_section_test(void)
                    202: {
                    203:     char *buf;
                    204:     cfg_t *sec, *subsec;
                    205:     cfg_opt_t *opt;
                    206: 
                    207:     /* size should be 0 before any section has been parsed. Since the
                    208:      * CFGF_MULTI flag is set, there are no default sections.
                    209:      */
                    210:     fail_unless(cfg_size(cfg, "section") == 0);
                    211:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "section")) == 0);
                    212:     fail_unless(cfg_size(cfg, "section|subsection") == 0);
                    213:     fail_unless(cfg_opt_size(cfg_getopt(cfg, "section|subsection")) == 0);
                    214: 
                    215:     buf = "section {}"; /* add a section with default values */
                    216:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    217:     fail_unless(cfg_size(cfg, "section") == 1);
                    218:     
                    219:     sec = cfg_getsec(cfg, "section");
                    220:     fail_unless(sec != 0);
                    221:     fail_unless(strcmp(sec->name, "section") == 0);
                    222:     fail_unless(cfg_title(sec) == 0);
                    223: 
                    224:     opt = cfg_getopt(sec, "subsection");
                    225:     fail_unless(opt != 0);
                    226:     fail_unless(cfg_opt_size(opt) == 0);
                    227:     fail_unless(cfg_size(sec, "subsection") == 0);
                    228:     
                    229:     fail_unless(strcmp(cfg_getnstr(sec, "substring", 0), "subdefault1") == 0);
                    230:     subsec = cfg_getsec(cfg, "section|subsection");
                    231:     fail_unless(subsec == 0);
                    232: 
                    233:     buf = "section { subsection 'foo' { subsubfloat = {1.2, 3.4, 5.6} } }";
                    234:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    235:     fail_unless(cfg_size(cfg, "section") == 2);
                    236: 
                    237:     sec = cfg_getnsec(cfg, "section", 1);
                    238:     fail_unless(sec != 0);
                    239:     fail_unless(strcmp(cfg_title(cfg_getnsec(sec, "subsection", 0)), "foo") == 0);
                    240:     fail_unless(cfg_size(sec, "subinteger") == 14);
                    241: 
                    242:     subsec = cfg_getsec(sec, "subsection");
                    243:     fail_unless(cfg_size(subsec, "subsubfloat") == 3);
                    244:     fail_unless(cfg_getnfloat(subsec, "subsubfloat", 2) == 5.6);
                    245:     fail_unless(cfg_getnstr(subsec, "subsubstring", 0) == 0);
                    246: 
                    247:     sec = cfg_getnsec(cfg, "section", 0);
                    248:     fail_unless(sec != 0);
                    249:     fail_unless(cfg_size(sec, "subsection") == 0);
                    250:     buf = "subsection 'bar' {}";
                    251:     fail_unless(cfg_parse_buf(sec, buf) == CFG_SUCCESS);
                    252:     fail_unless(cfg_size(sec, "subsection") == 1);
                    253:     subsec = cfg_getnsec(sec, "subsection", 0);
                    254:     fail_unless(subsec != 0);
                    255:     fail_unless(strcmp(cfg_title(subsec), "bar") == 0);
                    256:     fail_unless(cfg_getnfloat(subsec, "subsubfloat", 2) == 0);
                    257: 
                    258:     buf = "subsection 'baz' {}";
                    259:     fail_unless(cfg_parse_buf(sec, buf) == CFG_SUCCESS);
                    260:     fail_unless(cfg_gettsec(sec, "subsection", "bar") == subsec);
                    261:     opt = cfg_getopt(sec, "subsection");
                    262:     fail_unless(opt != 0);
                    263:     fail_unless(cfg_gettsec(sec, "subsection", "baz") == cfg_opt_gettsec(opt, "baz"));
                    264:     fail_unless(cfg_opt_gettsec(opt, "bar") == subsec);
                    265:     fail_unless(cfg_opt_gettsec(opt, "foo") == 0);
                    266:     fail_unless(cfg_gettsec(sec, "subsection", "section") == 0);
                    267: 
                    268:     fail_unless(cfg_gettsec(cfg, "section", "baz") == 0);
                    269: }
                    270: 
                    271: static void parse_buf_test(void)
                    272: {
                    273:     char *buf;
                    274: 
                    275:     fail_unless(cfg_parse_buf(cfg, 0) == CFG_SUCCESS);
                    276:     fail_unless(cfg_parse_buf(cfg, "") == CFG_SUCCESS);
                    277: 
                    278:     buf = "bool = {true, true, false, wrong}";
                    279:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
                    280:     buf = "string = {foo, bar";
                    281:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
                    282: 
                    283:     buf = "/* this is a comment */ bool = {true} /*// another comment */";
                    284:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    285: 
                    286:     buf = "/*/*/ bool = {true}//  */";
                    287:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    288: 
                    289:     buf = "/////// this is a comment\nbool = {true} // another /* comment */";
                    290:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    291: 
                    292:     buf = "# this is a comment\nbool = {true} # another //* comment *//";
                    293:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    294: 
                    295:     buf = "string={/usr/local/}";
                    296:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    297:     fail_unless(strcmp(cfg_getnstr(cfg, "string", 0), "/usr/local/") == 0);
                    298: }
                    299: 
                    300: static void nonexistent_option_test(void)
                    301: {
                    302:     char *buf;
                    303: 
                    304:     fail_unless(cfg_numopts(cfg->opts) == numopts);
                    305:     fail_unless(cfg_getopt(cfg, "nonexistent") == 0);
                    306: 
                    307:     buf = "section {}";
                    308:     fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
                    309:     fail_unless(cfg_getopt(cfg, "section|subnonexistent") == 0);
                    310: }
                    311: 
                    312: int
                    313: main(void)
                    314: {
                    315:     list_setup();
                    316: 
                    317:     list_string_test();
                    318:     list_integer_test();
                    319:     list_float_test();
                    320:     list_bool_test();
                    321:     list_section_test();
                    322:     parse_buf_test();
                    323:     nonexistent_option_test();
                    324: 
                    325:     list_teardown();
                    326: 
                    327:     return 0;
                    328: }
                    329: 

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