1.1 --- a/test/prop_tests.c Fri Jul 12 20:50:18 2013 +0200 1.2 +++ b/test/prop_tests.c Sun Jul 14 17:11:34 2013 +0200 1.3 @@ -43,7 +43,7 @@ 1.4 } 1.5 1.6 UCX_TEST_IMPLEMENT(test_ucx_prop_parse) { 1.7 - char *tests[] = { 1.8 + const char *tests[] = { 1.9 "name = value\n", 1.10 "name=value\n", 1.11 "n=value\n", 1.12 @@ -56,7 +56,7 @@ 1.13 "name = value\n\n" 1.14 }; 1.15 1.16 - char *names[] = { 1.17 + const char *names[] = { 1.18 "name", 1.19 "name", 1.20 "n", 1.21 @@ -69,7 +69,7 @@ 1.22 "name" 1.23 }; 1.24 1.25 - char *values[] = { 1.26 + const char *values[] = { 1.27 "value", 1.28 "value", 1.29 "value", 1.30 @@ -90,13 +90,13 @@ 1.31 for(int i=0;i<10;i++) { 1.32 UcxPropParser *parser = ucx_prop_new(); 1.33 1.34 - ucx_prop_fill(parser, tests[i], strlen(tests[i])); 1.35 + ucx_prop_fill(parser, (char*)tests[i], strlen(tests[i])); 1.36 UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed"); 1.37 UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen"); 1.38 1.39 int r = ucx_prop_parse(parser, &name, &value); 1.40 - sstr_t n = sstr(names[i]); 1.41 - sstr_t v = sstr(values[i]); 1.42 + sstr_t n = sstr((char*)names[i]); 1.43 + sstr_t v = sstr((char*)values[i]); 1.44 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.45 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name"); 1.46 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value"); 1.47 @@ -114,7 +114,7 @@ 1.48 } 1.49 1.50 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_multi) { 1.51 - char *names[] = { 1.52 + const char *names[] = { 1.53 "a", 1.54 "b", 1.55 "c", 1.56 @@ -125,7 +125,7 @@ 1.57 "key3" 1.58 }; 1.59 1.60 - char *values[] = { 1.61 + const char *values[] = { 1.62 "a value", 1.63 "b value", 1.64 "core", 1.65 @@ -136,7 +136,7 @@ 1.66 "value3" 1.67 }; 1.68 1.69 - char *str = "#\n" 1.70 + const char *str = "#\n" 1.71 "# properties\n" 1.72 "# contains key/value pairs\n" 1.73 "#\n" 1.74 @@ -157,15 +157,16 @@ 1.75 1.76 UCX_TEST_BEGIN 1.77 1.78 - ucx_prop_fill(parser, str, strlen(str)); 1.79 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.80 1.81 sstr_t name; 1.82 sstr_t value; 1.83 for(int i=0;i<8;i++) { 1.84 int r = ucx_prop_parse(parser, &name, &value); 1.85 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.86 - UCX_TEST_ASSERT((!sstrcmp(name, sstr(names[i]))), "wrong name"); 1.87 - UCX_TEST_ASSERT((!sstrcmp(value, sstr(values[i]))), "wrong value"); 1.88 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)names[i]))), "wrong name"); 1.89 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)values[i]))), 1.90 + "wrong value"); 1.91 } 1.92 int r = ucx_prop_parse(parser, &name, &value); 1.93 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.94 @@ -177,7 +178,7 @@ 1.95 1.96 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_part) { 1.97 UcxPropParser *parser = ucx_prop_new(); 1.98 - char *str; 1.99 + const char *str; 1.100 int r; 1.101 sstr_t name; 1.102 sstr_t value; 1.103 @@ -185,100 +186,100 @@ 1.104 UCX_TEST_BEGIN 1.105 1.106 str = ""; 1.107 - ucx_prop_fill(parser, str, strlen(str)); 1.108 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.109 r = ucx_prop_parse(parser, &name, &value); 1.110 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.111 1.112 str = " \n"; 1.113 - ucx_prop_fill(parser, str, strlen(str)); 1.114 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.115 r = ucx_prop_parse(parser, &name, &value); 1.116 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.117 1.118 str = "name"; 1.119 - ucx_prop_fill(parser, str, strlen(str)); 1.120 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.121 r = ucx_prop_parse(parser, &name, &value); 1.122 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.123 1.124 str = " "; 1.125 - ucx_prop_fill(parser, str, strlen(str)); 1.126 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.127 r = ucx_prop_parse(parser, &name, &value); 1.128 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.129 1.130 str = "= "; 1.131 - ucx_prop_fill(parser, str, strlen(str)); 1.132 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.133 r = ucx_prop_parse(parser, &name, &value); 1.134 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.135 1.136 str = "value"; 1.137 - ucx_prop_fill(parser, str, strlen(str)); 1.138 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.139 r = ucx_prop_parse(parser, &name, &value); 1.140 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.141 1.142 str = "\n"; 1.143 - ucx_prop_fill(parser, str, strlen(str)); 1.144 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.145 r = ucx_prop_parse(parser, &name, &value); 1.146 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.147 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name"); 1.148 - UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 1.149 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name"); 1.150 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); 1.151 1.152 // second round 1.153 str = "#comment\n"; 1.154 - ucx_prop_fill(parser, str, strlen(str)); 1.155 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.156 r = ucx_prop_parse(parser, &name, &value); 1.157 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.158 1.159 str = "#comment\nname = "; 1.160 - ucx_prop_fill(parser, str, strlen(str)); 1.161 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.162 r = ucx_prop_parse(parser, &name, &value); 1.163 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.164 1.165 str = "value\na = b\n"; 1.166 - ucx_prop_fill(parser, str, strlen(str)); 1.167 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.168 r = ucx_prop_parse(parser, &name, &value); 1.169 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.170 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name"); 1.171 - UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 1.172 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name"); 1.173 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); 1.174 1.175 r = ucx_prop_parse(parser, &name, &value); 1.176 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.177 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name"); 1.178 - UCX_TEST_ASSERT((!sstrcmp(value, sstr("b"))), "wrong value"); 1.179 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name"); 1.180 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"b"))), "wrong value"); 1.181 1.182 str = "# comment\n#\n#\ntests = "; 1.183 - ucx_prop_fill(parser, str, strlen(str)); 1.184 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.185 r = ucx_prop_parse(parser, &name, &value); 1.186 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.187 1.188 str = "test1 "; 1.189 - ucx_prop_fill(parser, str, strlen(str)); 1.190 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.191 r = ucx_prop_parse(parser, &name, &value); 1.192 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.193 1.194 str = "test2 test3 test4\n"; 1.195 - sstr_t testv = sstr("test1 test2 test3 test4"); 1.196 - ucx_prop_fill(parser, str, strlen(str)); 1.197 + sstr_t testv = sstr((char*)"test1 test2 test3 test4"); 1.198 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.199 r = ucx_prop_parse(parser, &name, &value); 1.200 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.201 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("tests"))), "wrong name"); 1.202 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"tests"))), "wrong name"); 1.203 UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value"); 1.204 1.205 // test if parse finds a name/value after a tmp comment 1.206 str = "# just a comment"; 1.207 - ucx_prop_fill(parser, str, strlen(str)); 1.208 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.209 r = ucx_prop_parse(parser, &name, &value); 1.210 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.211 1.212 str = " in 3"; 1.213 - ucx_prop_fill(parser, str, strlen(str)); 1.214 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.215 r = ucx_prop_parse(parser, &name, &value); 1.216 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.217 1.218 str = " parts\na = 1\n"; 1.219 - ucx_prop_fill(parser, str, strlen(str)); 1.220 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.221 r = ucx_prop_parse(parser, &name, &value); 1.222 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.223 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name"); 1.224 - UCX_TEST_ASSERT((!sstrcmp(value, sstr("1"))), "wrong value"); 1.225 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name"); 1.226 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"1"))), "wrong value"); 1.227 1.228 UCX_TEST_END 1.229 1.230 @@ -320,8 +321,8 @@ 1.231 r = ucx_prop_parse(parser, &name, &value); 1.232 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.233 1.234 - char *str = " = "; 1.235 - ucx_prop_fill(parser, str, strlen(str)); 1.236 + const char *str = " = "; 1.237 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.238 r = ucx_prop_parse(parser, &name, &value); 1.239 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.240 1.241 @@ -338,7 +339,7 @@ 1.242 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.243 1.244 str = "\n#comment\nkey = value\n"; 1.245 - ucx_prop_fill(parser, str, strlen(str)); 1.246 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.247 r = ucx_prop_parse(parser, &name, &value); 1.248 sstr_t n = sstrn(long_name, name_len); 1.249 sstr_t v = sstrn(long_value, value_len); 1.250 @@ -348,8 +349,8 @@ 1.251 1.252 r = ucx_prop_parse(parser, &name, &value); 1.253 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 1.254 - UCX_TEST_ASSERT((!sstrcmp(name, sstr("key"))), "wrong name"); 1.255 - UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 1.256 + UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"key"))), "wrong name"); 1.257 + UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); 1.258 1.259 r = ucx_prop_parse(parser, &name, &value); 1.260 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 1.261 @@ -360,3 +361,182 @@ 1.262 free(long_value); 1.263 ucx_prop_free(parser); 1.264 } 1.265 + 1.266 +UCX_TEST_IMPLEMENT(test_ucx_prop_parse2map) { 1.267 + UcxMap *map = ucx_map_new(16); 1.268 + UcxPropParser *parser = ucx_prop_new(); 1.269 + 1.270 + UCX_TEST_BEGIN 1.271 + 1.272 + const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n"; 1.273 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.274 + 1.275 + int r = ucx_prop_parse2map(parser, map); 1.276 + 1.277 + UCX_TEST_ASSERT(r == 0, "parse2map failed"); 1.278 + UCX_TEST_ASSERT(map->count == 3, "wrong number of properties"); 1.279 + 1.280 + char *v1 = (char*)ucx_map_cstr_get(map, "key1"); 1.281 + char *v2 = (char*)ucx_map_cstr_get(map, "key2"); 1.282 + char *v3 = (char*)ucx_map_cstr_get(map, "key3"); 1.283 + 1.284 + UCX_TEST_ASSERT(v1, "value for key1 not found"); 1.285 + UCX_TEST_ASSERT(v2, "value for key2 not found"); 1.286 + UCX_TEST_ASSERT(v3, "value for key3 not found"); 1.287 + 1.288 + UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1"); 1.289 + UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2"); 1.290 + UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3"); 1.291 + 1.292 + // second test 1.293 + ucx_map_free(map); 1.294 + free(v1); 1.295 + free(v2); 1.296 + free(v3); 1.297 + map = ucx_map_new(16); 1.298 + 1.299 + str = "\n#comment\n"; 1.300 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.301 + 1.302 + r = ucx_prop_parse2map(parser, map); 1.303 + UCX_TEST_ASSERT(r == 0, "parse2map failed"); 1.304 + UCX_TEST_ASSERT(map->count == 0, "wrong number of properties"); 1.305 + 1.306 + str = "key1 = value1\nsyntax error line\n"; 1.307 + ucx_prop_fill(parser, (char*)str, strlen(str)); 1.308 + 1.309 + r = ucx_prop_parse2map(parser, map); 1.310 + UCX_TEST_ASSERT(r == 1, "parse2map should return 1"); 1.311 + UCX_TEST_ASSERT(map->count == 1, "wrong number of properties"); 1.312 + 1.313 + char *v = (char*)ucx_map_cstr_get(map, "key1"); 1.314 + UCX_TEST_ASSERT((!strcmp(v, "value1")), "wrong value"); 1.315 + 1.316 + UCX_TEST_END 1.317 + 1.318 + ucx_prop_free(parser); 1.319 +} 1.320 + 1.321 +UCX_TEST_IMPLEMENT(test_ucx_properties_load) { 1.322 + UCX_TEST_BEGIN 1.323 + FILE *f = tmpfile(); 1.324 + UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted"); 1.325 + 1.326 + fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n"); 1.327 + fprintf(f, "\n\nkey3 = value3\n\n"); 1.328 + 1.329 + size_t name_len = 512; 1.330 + char *long_name = (char*)malloc(name_len); 1.331 + memset(long_name, 'k', 512); 1.332 + 1.333 + size_t value_len = 2048; 1.334 + char *long_value = (char*)malloc(value_len); 1.335 + memset(long_value, 'v', 2048); 1.336 + 1.337 + fwrite(long_name, 1, name_len, f); 1.338 + fprintf(f, " = "); 1.339 + fwrite(long_value, 1, value_len, f); 1.340 + fprintf(f, " \n"); 1.341 + 1.342 + fprintf(f, "\n\n\n\nlast_key = property value\n"); 1.343 + 1.344 + fflush(f); 1.345 + fseek(f, 0, SEEK_SET); 1.346 + 1.347 + UcxMap *map = ucx_map_new(8); 1.348 + int r = ucx_properties_load(map, f); 1.349 + 1.350 + UCX_TEST_ASSERT(r == 0, "ucx_properties_load failed"); 1.351 + UCX_TEST_ASSERT(map->count == 5, "wrong number of properties"); 1.352 + 1.353 + char *v1 = (char*)ucx_map_cstr_get(map, "key1"); 1.354 + char *v2 = (char*)ucx_map_cstr_get(map, "key2"); 1.355 + char *v3 = (char*)ucx_map_cstr_get(map, "key3"); 1.356 + char *lv = (char*)ucx_map_sstr_get(map, sstrn(long_name, name_len)); 1.357 + char *lk = (char*)ucx_map_cstr_get(map, "last_key"); 1.358 + 1.359 + UCX_TEST_ASSERT(v1, "value for key1 not found"); 1.360 + UCX_TEST_ASSERT(v2, "value for key2 not found"); 1.361 + UCX_TEST_ASSERT(v3, "value for key3 not found"); 1.362 + UCX_TEST_ASSERT(lv, "value for long key not found"); 1.363 + UCX_TEST_ASSERT(lk, "value for last_key not found"); 1.364 + 1.365 + UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1"); 1.366 + UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2"); 1.367 + UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3"); 1.368 + sstr_t long1 = sstrn(long_value, value_len); 1.369 + sstr_t long2 = sstr(lv); 1.370 + UCX_TEST_ASSERT((!sstrcmp(long1, long2)), "wrong value for long key"); 1.371 + UCX_TEST_ASSERT(!strcmp(lk, "property value"), "wrong value for last_key"); 1.372 + 1.373 + free(v1); 1.374 + free(v2); 1.375 + free(v3); 1.376 + free(lv); 1.377 + free(lk); 1.378 + ucx_map_free(map); 1.379 + fclose(f); 1.380 + 1.381 + UCX_TEST_END 1.382 +} 1.383 + 1.384 +UCX_TEST_IMPLEMENT(test_ucx_properties_store) { 1.385 + UcxMap *map1 = ucx_map_new(16); 1.386 + ucx_map_cstr_put(map1, "key1", "value1"); 1.387 + ucx_map_cstr_put(map1, "key2", "value2"); 1.388 + ucx_map_cstr_put(map1, "key3", "value3"); 1.389 + ucx_map_cstr_put(map1, "key4", "value4"); 1.390 + ucx_map_cstr_put(map1, "property.key1", "some value 1"); 1.391 + ucx_map_cstr_put(map1, "property.key2", "some value 2"); 1.392 + ucx_map_cstr_put(map1, "property.key3", "some value 3"); 1.393 + ucx_map_cstr_put(map1, "property.key4", "some value 4"); 1.394 + 1.395 + UCX_TEST_BEGIN 1.396 + 1.397 + FILE *f = tmpfile(); 1.398 + fprintf(f, "#\n# test property file\n#\n#\n"); 1.399 + ucx_properties_store(map1, f); 1.400 + 1.401 + fflush(f); 1.402 + fseek(f, 0, SEEK_SET); 1.403 + UcxMap *map2 = ucx_map_new(16); 1.404 + ucx_properties_load(map2, f); 1.405 + 1.406 + UCX_TEST_ASSERT(map2->count == 8, "wrong number of properties in map2"); 1.407 + 1.408 + char *v1 = (char*)ucx_map_cstr_get(map2, "key1"); 1.409 + char *v2 = (char*)ucx_map_cstr_get(map2, "key2"); 1.410 + char *v3 = (char*)ucx_map_cstr_get(map2, "key3"); 1.411 + char *v4 = (char*)ucx_map_cstr_get(map2, "key4"); 1.412 + char *v5 = (char*)ucx_map_cstr_get(map2, "property.key1"); 1.413 + char *v6 = (char*)ucx_map_cstr_get(map2, "property.key2"); 1.414 + char *v7 = (char*)ucx_map_cstr_get(map2, "property.key3"); 1.415 + char *v8 = (char*)ucx_map_cstr_get(map2, "property.key4"); 1.416 + 1.417 + UCX_TEST_ASSERT(v1 && v2 && v3 && v4 && v5 && v6 && v7 && v8, 1.418 + "missing values"); 1.419 + 1.420 + UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value 1"); 1.421 + UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value 2"); 1.422 + UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value 3"); 1.423 + UCX_TEST_ASSERT((!strcmp(v4, "value4")), "wrong value 4"); 1.424 + UCX_TEST_ASSERT((!strcmp(v5, "some value 1")), "wrong value 5"); 1.425 + UCX_TEST_ASSERT((!strcmp(v6, "some value 2")), "wrong value 6"); 1.426 + UCX_TEST_ASSERT((!strcmp(v7, "some value 3")), "wrong value 7"); 1.427 + UCX_TEST_ASSERT((!strcmp(v8, "some value 4")), "wrong value 8"); 1.428 + 1.429 + free(v1); 1.430 + free(v2); 1.431 + free(v3); 1.432 + free(v4); 1.433 + free(v5); 1.434 + free(v6); 1.435 + free(v7); 1.436 + free(v8); 1.437 + ucx_map_free(map2); 1.438 + fclose(f); 1.439 + 1.440 + UCX_TEST_END 1.441 + 1.442 + ucx_map_free(map1); 1.443 +}