Annotation of embedaddon/confuse/tests/modified_flag.c, revision 1.1

1.1     ! misho       1: #include "check_confuse.h"
        !             2: #include <stdlib.h>
        !             3: #include <string.h>
        !             4: 
        !             5: static int parse_ptr(cfg_t *cfg, cfg_opt_t *opt,
        !             6:                     const char *value, void *result)
        !             7: {
        !             8:        char *ptr = strdup(value);
        !             9:        if (!ptr)
        !            10:                return -1;
        !            11: 
        !            12:        *(void **)result = ptr;
        !            13:        return 0;
        !            14: }
        !            15: 
        !            16: static int is_set(cfg_t *cfg, const char *name, int flags)
        !            17: {
        !            18:        cfg_opt_t *opt = cfg_getopt(cfg, name);
        !            19:        if (!opt)
        !            20:                return -1;
        !            21:        return !!(opt->flags & flags);
        !            22: }
        !            23: 
        !            24: int main(void)
        !            25: {
        !            26:        cfg_opt_t sub[] = {
        !            27:                CFG_INT_LIST("int", "{0,1,2}", CFGF_NONE),
        !            28:                CFG_INT_LIST("nodef", NULL, CFGF_NONE),
        !            29:                CFG_FLOAT("float", 42.17, CFGF_NONE),
        !            30:                CFG_BOOL("bool", cfg_true, CFGF_NONE),
        !            31:                CFG_STR("str", "hello", CFGF_NODEFAULT),
        !            32:                CFG_PTR_CB("ptr", "dummy", CFGF_NONE, parse_ptr, free),
        !            33:                CFG_END()
        !            34:        };
        !            35:        cfg_opt_t opts[] = {
        !            36:                CFG_INT_LIST("int", "{0,1,2}", CFGF_NONE),
        !            37:                CFG_INT_LIST("nodef", NULL, CFGF_NONE),
        !            38:                CFG_FLOAT("float", 17.42, CFGF_NONE),
        !            39:                CFG_BOOL("bool", cfg_true, CFGF_NONE),
        !            40:                CFG_STR("str", "hello", CFGF_NONE),
        !            41:                CFG_SEC("sub", sub, CFGF_NONE),
        !            42:                CFG_PTR_CB("ptr", "dummy", CFGF_NONE, parse_ptr, free),
        !            43:                CFG_END()
        !            44:        };
        !            45: 
        !            46:        cfg_t *cfg = cfg_init(opts, 0);
        !            47: 
        !            48:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !            49:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !            50:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !            51:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !            52:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !            53:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !            54:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !            55:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !            56:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !            57:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !            58:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !            59:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !            60: 
        !            61:        fail_unless(cfg_parse_buf(cfg, "") == CFG_SUCCESS);
        !            62: 
        !            63:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !            64:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !            65:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !            66:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !            67:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !            68:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !            69:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !            70:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !            71:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !            72:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !            73:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !            74:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !            75: 
        !            76:        fail_unless(cfg_parse_buf(cfg, "int = {3,4}") == CFG_SUCCESS);
        !            77: 
        !            78:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 1);
        !            79:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !            80:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !            81:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !            82:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !            83:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !            84:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !            85:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !            86:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !            87:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !            88:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !            89:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !            90: 
        !            91:        fail_unless(cfg_parse_buf(cfg, "nodef = {}") == CFG_SUCCESS);
        !            92: 
        !            93:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 1);
        !            94:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !            95:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !            96:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !            97:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !            98:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !            99:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           100:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           101:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           102:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !           103:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           104:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           105: 
        !           106:        fail_unless(cfg_parse_buf(cfg, "float = 3.14") == CFG_SUCCESS);
        !           107: 
        !           108:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 1);
        !           109:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !           110:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 1);
        !           111:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           112:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           113:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           114:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           115:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           116:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           117:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !           118:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           119:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           120: 
        !           121:        cfg_free(cfg);
        !           122:        cfg = cfg_init(opts, 0);
        !           123:        fail_unless(cfg_setnint(cfg, "nodef", 1, 1) == CFG_SUCCESS);
        !           124: 
        !           125:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           126:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !           127:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           128:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           129:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           130:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           131:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           132:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           133:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           134:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !           135:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           136:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           137: 
        !           138:        fail_unless(cfg_setbool(cfg, "bool", cfg_true) == CFG_SUCCESS);
        !           139: 
        !           140:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           141:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !           142:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           143:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 1);
        !           144:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           145:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           146:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           147:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           148:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           149:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !           150:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           151:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           152: 
        !           153:        fail_unless(cfg_setfloat(cfg, "sub|float", 2.718) == CFG_SUCCESS);
        !           154: 
        !           155:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           156:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !           157:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           158:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 1);
        !           159:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           160:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           161:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           162:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           163:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 1);
        !           164:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 0);
        !           165:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           166:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           167: 
        !           168:        fail_unless(cfg_setbool(cfg, "sub|bool", cfg_true) == CFG_SUCCESS);
        !           169: 
        !           170:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           171:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 1);
        !           172:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           173:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 1);
        !           174:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           175:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           176:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           177:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           178:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 1);
        !           179:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           180:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           181:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           182: 
        !           183:        cfg_free(cfg);
        !           184:        cfg = cfg_init(opts, 0);
        !           185:        fail_unless(cfg_parse_buf(cfg, "sub { bool = true }") == CFG_SUCCESS);
        !           186: 
        !           187:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           188:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !           189:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           190:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           191:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           192:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           193:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           194:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           195:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           196:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           197:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 0);
        !           198:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           199: 
        !           200:        fail_unless(cfg_parse_buf(cfg, "sub { str = hello }") == CFG_SUCCESS);
        !           201: 
        !           202:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           203:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !           204:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           205:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           206:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           207:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           208:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           209:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           210:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           211:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           212:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 1);
        !           213:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           214: 
        !           215:        char *bad_ints[] = { "1", "a" };
        !           216:        fail_unless(cfg_setmulti(cfg, "int", 2, bad_ints) == CFG_FAIL);
        !           217: 
        !           218:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 0);
        !           219:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !           220:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           221:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           222:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           223:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           224:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           225:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           226:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           227:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           228:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 1);
        !           229:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           230: 
        !           231:        char *good_ints[] = { "1", "2" };
        !           232:        fail_unless(cfg_setmulti(cfg, "int", 2, good_ints) == CFG_SUCCESS);
        !           233: 
        !           234:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 1);
        !           235:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !           236:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           237:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           238:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           239:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           240:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           241:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           242:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           243:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           244:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 1);
        !           245:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 0);
        !           246: 
        !           247:        fail_unless(cfg_setmulti(cfg, "sub|ptr", 1, good_ints) == CFG_SUCCESS);
        !           248: 
        !           249:        fail_unless(is_set(cfg, "int", CFGF_MODIFIED) == 1);
        !           250:        fail_unless(is_set(cfg, "nodef", CFGF_MODIFIED) == 0);
        !           251:        fail_unless(is_set(cfg, "float", CFGF_MODIFIED) == 0);
        !           252:        fail_unless(is_set(cfg, "bool", CFGF_MODIFIED) == 0);
        !           253:        fail_unless(is_set(cfg, "str", CFGF_MODIFIED) == 0);
        !           254:        fail_unless(is_set(cfg, "ptr", CFGF_MODIFIED) == 0);
        !           255:        fail_unless(is_set(cfg, "sub|int", CFGF_MODIFIED) == 0);
        !           256:        fail_unless(is_set(cfg, "sub|nodef", CFGF_MODIFIED) == 0);
        !           257:        fail_unless(is_set(cfg, "sub|float", CFGF_MODIFIED) == 0);
        !           258:        fail_unless(is_set(cfg, "sub|bool", CFGF_MODIFIED) == 1);
        !           259:        fail_unless(is_set(cfg, "sub|str", CFGF_MODIFIED) == 1);
        !           260:        fail_unless(is_set(cfg, "sub|ptr", CFGF_MODIFIED) == 1);
        !           261: 
        !           262:        cfg_free(cfg);
        !           263: 
        !           264:        return 0;
        !           265: }
        !           266: 
        !           267: /**
        !           268:  * Local Variables:
        !           269:  *  indent-tabs-mode: t
        !           270:  *  c-file-style: "linux"
        !           271:  * End:
        !           272:  */

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