test/prop_tests.c

changeset 109
75cb6590358b
parent 108
d2b1e67b2b48
child 110
1cf71e56f01e
equal deleted inserted replaced
108:d2b1e67b2b48 109:75cb6590358b
41 41
42 ucx_prop_free(parser); 42 ucx_prop_free(parser);
43 } 43 }
44 44
45 UCX_TEST_IMPLEMENT(test_ucx_prop_parse) { 45 UCX_TEST_IMPLEMENT(test_ucx_prop_parse) {
46 char *tests[] = { 46 const char *tests[] = {
47 "name = value\n", 47 "name = value\n",
48 "name=value\n", 48 "name=value\n",
49 "n=value\n", 49 "n=value\n",
50 "name=v\n", 50 "name=v\n",
51 "n=v\n", 51 "n=v\n",
54 "# comment1\n# comment2\n\n \n\nname = value\n", 54 "# comment1\n# comment2\n\n \n\nname = value\n",
55 " name = value\n", 55 " name = value\n",
56 "name = value\n\n" 56 "name = value\n\n"
57 }; 57 };
58 58
59 char *names[] = { 59 const char *names[] = {
60 "name", 60 "name",
61 "name", 61 "name",
62 "n", 62 "n",
63 "name", 63 "name",
64 "n", 64 "n",
67 "name", 67 "name",
68 "name", 68 "name",
69 "name" 69 "name"
70 }; 70 };
71 71
72 char *values[] = { 72 const char *values[] = {
73 "value", 73 "value",
74 "value", 74 "value",
75 "value", 75 "value",
76 "v", 76 "v",
77 "v", 77 "v",
88 sstr_t value; 88 sstr_t value;
89 89
90 for(int i=0;i<10;i++) { 90 for(int i=0;i<10;i++) {
91 UcxPropParser *parser = ucx_prop_new(); 91 UcxPropParser *parser = ucx_prop_new();
92 92
93 ucx_prop_fill(parser, tests[i], strlen(tests[i])); 93 ucx_prop_fill(parser, (char*)tests[i], strlen(tests[i]));
94 UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed"); 94 UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed");
95 UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen"); 95 UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen");
96 96
97 int r = ucx_prop_parse(parser, &name, &value); 97 int r = ucx_prop_parse(parser, &name, &value);
98 sstr_t n = sstr(names[i]); 98 sstr_t n = sstr((char*)names[i]);
99 sstr_t v = sstr(values[i]); 99 sstr_t v = sstr((char*)values[i]);
100 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 100 UCX_TEST_ASSERT(r == 1, "parse returned 0");
101 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name"); 101 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name");
102 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value"); 102 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value");
103 103
104 r = ucx_prop_parse(parser, &name, &value); 104 r = ucx_prop_parse(parser, &name, &value);
112 112
113 UCX_TEST_END 113 UCX_TEST_END
114 } 114 }
115 115
116 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_multi) { 116 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_multi) {
117 char *names[] = { 117 const char *names[] = {
118 "a", 118 "a",
119 "b", 119 "b",
120 "c", 120 "c",
121 "uap", 121 "uap",
122 "name", 122 "name",
123 "key1", 123 "key1",
124 "key2", 124 "key2",
125 "key3" 125 "key3"
126 }; 126 };
127 127
128 char *values[] = { 128 const char *values[] = {
129 "a value", 129 "a value",
130 "b value", 130 "b value",
131 "core", 131 "core",
132 "core", 132 "core",
133 "ucx", 133 "ucx",
134 "value1", 134 "value1",
135 "value2", 135 "value2",
136 "value3" 136 "value3"
137 }; 137 };
138 138
139 char *str = "#\n" 139 const char *str = "#\n"
140 "# properties\n" 140 "# properties\n"
141 "# contains key/value pairs\n" 141 "# contains key/value pairs\n"
142 "#\n" 142 "#\n"
143 "a = a value\n" 143 "a = a value\n"
144 "b = b value\n" 144 "b = b value\n"
155 155
156 UcxPropParser *parser = ucx_prop_new(); 156 UcxPropParser *parser = ucx_prop_new();
157 157
158 UCX_TEST_BEGIN 158 UCX_TEST_BEGIN
159 159
160 ucx_prop_fill(parser, str, strlen(str)); 160 ucx_prop_fill(parser, (char*)str, strlen(str));
161 161
162 sstr_t name; 162 sstr_t name;
163 sstr_t value; 163 sstr_t value;
164 for(int i=0;i<8;i++) { 164 for(int i=0;i<8;i++) {
165 int r = ucx_prop_parse(parser, &name, &value); 165 int r = ucx_prop_parse(parser, &name, &value);
166 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 166 UCX_TEST_ASSERT(r == 1, "parse returned 0");
167 UCX_TEST_ASSERT((!sstrcmp(name, sstr(names[i]))), "wrong name"); 167 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)names[i]))), "wrong name");
168 UCX_TEST_ASSERT((!sstrcmp(value, sstr(values[i]))), "wrong value"); 168 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)values[i]))),
169 "wrong value");
169 } 170 }
170 int r = ucx_prop_parse(parser, &name, &value); 171 int r = ucx_prop_parse(parser, &name, &value);
171 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 172 UCX_TEST_ASSERT(r == 0, "parse returned 1");
172 173
173 UCX_TEST_END 174 UCX_TEST_END
175 ucx_prop_free(parser); 176 ucx_prop_free(parser);
176 } 177 }
177 178
178 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_part) { 179 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_part) {
179 UcxPropParser *parser = ucx_prop_new(); 180 UcxPropParser *parser = ucx_prop_new();
180 char *str; 181 const char *str;
181 int r; 182 int r;
182 sstr_t name; 183 sstr_t name;
183 sstr_t value; 184 sstr_t value;
184 185
185 UCX_TEST_BEGIN 186 UCX_TEST_BEGIN
186 187
187 str = ""; 188 str = "";
188 ucx_prop_fill(parser, str, strlen(str)); 189 ucx_prop_fill(parser, (char*)str, strlen(str));
189 r = ucx_prop_parse(parser, &name, &value); 190 r = ucx_prop_parse(parser, &name, &value);
190 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 191 UCX_TEST_ASSERT(r == 0, "parse returned 1");
191 192
192 str = " \n"; 193 str = " \n";
193 ucx_prop_fill(parser, str, strlen(str)); 194 ucx_prop_fill(parser, (char*)str, strlen(str));
194 r = ucx_prop_parse(parser, &name, &value); 195 r = ucx_prop_parse(parser, &name, &value);
195 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 196 UCX_TEST_ASSERT(r == 0, "parse returned 1");
196 197
197 str = "name"; 198 str = "name";
198 ucx_prop_fill(parser, str, strlen(str)); 199 ucx_prop_fill(parser, (char*)str, strlen(str));
199 r = ucx_prop_parse(parser, &name, &value); 200 r = ucx_prop_parse(parser, &name, &value);
200 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 201 UCX_TEST_ASSERT(r == 0, "parse returned 1");
201 202
202 str = " "; 203 str = " ";
203 ucx_prop_fill(parser, str, strlen(str)); 204 ucx_prop_fill(parser, (char*)str, strlen(str));
204 r = ucx_prop_parse(parser, &name, &value); 205 r = ucx_prop_parse(parser, &name, &value);
205 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 206 UCX_TEST_ASSERT(r == 0, "parse returned 1");
206 207
207 str = "= "; 208 str = "= ";
208 ucx_prop_fill(parser, str, strlen(str)); 209 ucx_prop_fill(parser, (char*)str, strlen(str));
209 r = ucx_prop_parse(parser, &name, &value); 210 r = ucx_prop_parse(parser, &name, &value);
210 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 211 UCX_TEST_ASSERT(r == 0, "parse returned 1");
211 212
212 str = "value"; 213 str = "value";
213 ucx_prop_fill(parser, str, strlen(str)); 214 ucx_prop_fill(parser, (char*)str, strlen(str));
214 r = ucx_prop_parse(parser, &name, &value); 215 r = ucx_prop_parse(parser, &name, &value);
215 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 216 UCX_TEST_ASSERT(r == 0, "parse returned 1");
216 217
217 str = "\n"; 218 str = "\n";
218 ucx_prop_fill(parser, str, strlen(str)); 219 ucx_prop_fill(parser, (char*)str, strlen(str));
219 r = ucx_prop_parse(parser, &name, &value); 220 r = ucx_prop_parse(parser, &name, &value);
220 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 221 UCX_TEST_ASSERT(r == 1, "parse returned 0");
221 UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name"); 222 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
222 UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 223 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
223 224
224 // second round 225 // second round
225 str = "#comment\n"; 226 str = "#comment\n";
226 ucx_prop_fill(parser, str, strlen(str)); 227 ucx_prop_fill(parser, (char*)str, strlen(str));
227 r = ucx_prop_parse(parser, &name, &value); 228 r = ucx_prop_parse(parser, &name, &value);
228 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 229 UCX_TEST_ASSERT(r == 0, "parse returned 1");
229 230
230 str = "#comment\nname = "; 231 str = "#comment\nname = ";
231 ucx_prop_fill(parser, str, strlen(str)); 232 ucx_prop_fill(parser, (char*)str, strlen(str));
232 r = ucx_prop_parse(parser, &name, &value); 233 r = ucx_prop_parse(parser, &name, &value);
233 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 234 UCX_TEST_ASSERT(r == 0, "parse returned 1");
234 235
235 str = "value\na = b\n"; 236 str = "value\na = b\n";
236 ucx_prop_fill(parser, str, strlen(str)); 237 ucx_prop_fill(parser, (char*)str, strlen(str));
237 r = ucx_prop_parse(parser, &name, &value); 238 r = ucx_prop_parse(parser, &name, &value);
238 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 239 UCX_TEST_ASSERT(r == 1, "parse returned 0");
239 UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name"); 240 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
240 UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 241 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
241 242
242 r = ucx_prop_parse(parser, &name, &value); 243 r = ucx_prop_parse(parser, &name, &value);
243 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 244 UCX_TEST_ASSERT(r == 1, "parse returned 0");
244 UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name"); 245 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
245 UCX_TEST_ASSERT((!sstrcmp(value, sstr("b"))), "wrong value"); 246 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"b"))), "wrong value");
246 247
247 str = "# comment\n#\n#\ntests = "; 248 str = "# comment\n#\n#\ntests = ";
248 ucx_prop_fill(parser, str, strlen(str)); 249 ucx_prop_fill(parser, (char*)str, strlen(str));
249 r = ucx_prop_parse(parser, &name, &value); 250 r = ucx_prop_parse(parser, &name, &value);
250 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 251 UCX_TEST_ASSERT(r == 0, "parse returned 1");
251 252
252 str = "test1 "; 253 str = "test1 ";
253 ucx_prop_fill(parser, str, strlen(str)); 254 ucx_prop_fill(parser, (char*)str, strlen(str));
254 r = ucx_prop_parse(parser, &name, &value); 255 r = ucx_prop_parse(parser, &name, &value);
255 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 256 UCX_TEST_ASSERT(r == 0, "parse returned 1");
256 257
257 str = "test2 test3 test4\n"; 258 str = "test2 test3 test4\n";
258 sstr_t testv = sstr("test1 test2 test3 test4"); 259 sstr_t testv = sstr((char*)"test1 test2 test3 test4");
259 ucx_prop_fill(parser, str, strlen(str)); 260 ucx_prop_fill(parser, (char*)str, strlen(str));
260 r = ucx_prop_parse(parser, &name, &value); 261 r = ucx_prop_parse(parser, &name, &value);
261 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 262 UCX_TEST_ASSERT(r == 1, "parse returned 0");
262 UCX_TEST_ASSERT((!sstrcmp(name, sstr("tests"))), "wrong name"); 263 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"tests"))), "wrong name");
263 UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value"); 264 UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value");
264 265
265 // test if parse finds a name/value after a tmp comment 266 // test if parse finds a name/value after a tmp comment
266 str = "# just a comment"; 267 str = "# just a comment";
267 ucx_prop_fill(parser, str, strlen(str)); 268 ucx_prop_fill(parser, (char*)str, strlen(str));
268 r = ucx_prop_parse(parser, &name, &value); 269 r = ucx_prop_parse(parser, &name, &value);
269 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 270 UCX_TEST_ASSERT(r == 0, "parse returned 1");
270 271
271 str = " in 3"; 272 str = " in 3";
272 ucx_prop_fill(parser, str, strlen(str)); 273 ucx_prop_fill(parser, (char*)str, strlen(str));
273 r = ucx_prop_parse(parser, &name, &value); 274 r = ucx_prop_parse(parser, &name, &value);
274 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 275 UCX_TEST_ASSERT(r == 0, "parse returned 1");
275 276
276 str = " parts\na = 1\n"; 277 str = " parts\na = 1\n";
277 ucx_prop_fill(parser, str, strlen(str)); 278 ucx_prop_fill(parser, (char*)str, strlen(str));
278 r = ucx_prop_parse(parser, &name, &value); 279 r = ucx_prop_parse(parser, &name, &value);
279 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 280 UCX_TEST_ASSERT(r == 1, "parse returned 0");
280 UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name"); 281 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
281 UCX_TEST_ASSERT((!sstrcmp(value, sstr("1"))), "wrong value"); 282 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"1"))), "wrong value");
282 283
283 UCX_TEST_END 284 UCX_TEST_END
284 285
285 ucx_prop_free(parser); 286 ucx_prop_free(parser);
286 } 287 }
318 319
319 ucx_prop_fill(parser, long_name+412, 100); 320 ucx_prop_fill(parser, long_name+412, 100);
320 r = ucx_prop_parse(parser, &name, &value); 321 r = ucx_prop_parse(parser, &name, &value);
321 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 322 UCX_TEST_ASSERT(r == 0, "parse returned 1");
322 323
323 char *str = " = "; 324 const char *str = " = ";
324 ucx_prop_fill(parser, str, strlen(str)); 325 ucx_prop_fill(parser, (char*)str, strlen(str));
325 r = ucx_prop_parse(parser, &name, &value); 326 r = ucx_prop_parse(parser, &name, &value);
326 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 327 UCX_TEST_ASSERT(r == 0, "parse returned 1");
327 328
328 ucx_prop_fill(parser, long_value, 512); 329 ucx_prop_fill(parser, long_value, 512);
329 r = ucx_prop_parse(parser, &name, &value); 330 r = ucx_prop_parse(parser, &name, &value);
336 ucx_prop_fill(parser, long_value+1536, 512); 337 ucx_prop_fill(parser, long_value+1536, 512);
337 r = ucx_prop_parse(parser, &name, &value); 338 r = ucx_prop_parse(parser, &name, &value);
338 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 339 UCX_TEST_ASSERT(r == 0, "parse returned 1");
339 340
340 str = "\n#comment\nkey = value\n"; 341 str = "\n#comment\nkey = value\n";
341 ucx_prop_fill(parser, str, strlen(str)); 342 ucx_prop_fill(parser, (char*)str, strlen(str));
342 r = ucx_prop_parse(parser, &name, &value); 343 r = ucx_prop_parse(parser, &name, &value);
343 sstr_t n = sstrn(long_name, name_len); 344 sstr_t n = sstrn(long_name, name_len);
344 sstr_t v = sstrn(long_value, value_len); 345 sstr_t v = sstrn(long_value, value_len);
345 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 346 UCX_TEST_ASSERT(r == 1, "parse returned 0");
346 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name"); 347 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name");
347 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value"); 348 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value");
348 349
349 r = ucx_prop_parse(parser, &name, &value); 350 r = ucx_prop_parse(parser, &name, &value);
350 UCX_TEST_ASSERT(r == 1, "parse returned 0"); 351 UCX_TEST_ASSERT(r == 1, "parse returned 0");
351 UCX_TEST_ASSERT((!sstrcmp(name, sstr("key"))), "wrong name"); 352 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"key"))), "wrong name");
352 UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value"); 353 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
353 354
354 r = ucx_prop_parse(parser, &name, &value); 355 r = ucx_prop_parse(parser, &name, &value);
355 UCX_TEST_ASSERT(r == 0, "parse returned 1"); 356 UCX_TEST_ASSERT(r == 0, "parse returned 1");
356 357
357 UCX_TEST_END 358 UCX_TEST_END
358 359
359 free(long_name); 360 free(long_name);
360 free(long_value); 361 free(long_value);
361 ucx_prop_free(parser); 362 ucx_prop_free(parser);
362 } 363 }
364
365 UCX_TEST_IMPLEMENT(test_ucx_prop_parse2map) {
366 UcxMap *map = ucx_map_new(16);
367 UcxPropParser *parser = ucx_prop_new();
368
369 UCX_TEST_BEGIN
370
371 const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n";
372 ucx_prop_fill(parser, (char*)str, strlen(str));
373
374 int r = ucx_prop_parse2map(parser, map);
375
376 UCX_TEST_ASSERT(r == 0, "parse2map failed");
377 UCX_TEST_ASSERT(map->count == 3, "wrong number of properties");
378
379 char *v1 = (char*)ucx_map_cstr_get(map, "key1");
380 char *v2 = (char*)ucx_map_cstr_get(map, "key2");
381 char *v3 = (char*)ucx_map_cstr_get(map, "key3");
382
383 UCX_TEST_ASSERT(v1, "value for key1 not found");
384 UCX_TEST_ASSERT(v2, "value for key2 not found");
385 UCX_TEST_ASSERT(v3, "value for key3 not found");
386
387 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
388 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
389 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
390
391 // second test
392 ucx_map_free(map);
393 free(v1);
394 free(v2);
395 free(v3);
396 map = ucx_map_new(16);
397
398 str = "\n#comment\n";
399 ucx_prop_fill(parser, (char*)str, strlen(str));
400
401 r = ucx_prop_parse2map(parser, map);
402 UCX_TEST_ASSERT(r == 0, "parse2map failed");
403 UCX_TEST_ASSERT(map->count == 0, "wrong number of properties");
404
405 str = "key1 = value1\nsyntax error line\n";
406 ucx_prop_fill(parser, (char*)str, strlen(str));
407
408 r = ucx_prop_parse2map(parser, map);
409 UCX_TEST_ASSERT(r == 1, "parse2map should return 1");
410 UCX_TEST_ASSERT(map->count == 1, "wrong number of properties");
411
412 char *v = (char*)ucx_map_cstr_get(map, "key1");
413 UCX_TEST_ASSERT((!strcmp(v, "value1")), "wrong value");
414
415 UCX_TEST_END
416
417 ucx_prop_free(parser);
418 }
419
420 UCX_TEST_IMPLEMENT(test_ucx_properties_load) {
421 UCX_TEST_BEGIN
422 FILE *f = tmpfile();
423 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
424
425 fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
426 fprintf(f, "\n\nkey3 = value3\n\n");
427
428 size_t name_len = 512;
429 char *long_name = (char*)malloc(name_len);
430 memset(long_name, 'k', 512);
431
432 size_t value_len = 2048;
433 char *long_value = (char*)malloc(value_len);
434 memset(long_value, 'v', 2048);
435
436 fwrite(long_name, 1, name_len, f);
437 fprintf(f, " = ");
438 fwrite(long_value, 1, value_len, f);
439 fprintf(f, " \n");
440
441 fprintf(f, "\n\n\n\nlast_key = property value\n");
442
443 fflush(f);
444 fseek(f, 0, SEEK_SET);
445
446 UcxMap *map = ucx_map_new(8);
447 int r = ucx_properties_load(map, f);
448
449 UCX_TEST_ASSERT(r == 0, "ucx_properties_load failed");
450 UCX_TEST_ASSERT(map->count == 5, "wrong number of properties");
451
452 char *v1 = (char*)ucx_map_cstr_get(map, "key1");
453 char *v2 = (char*)ucx_map_cstr_get(map, "key2");
454 char *v3 = (char*)ucx_map_cstr_get(map, "key3");
455 char *lv = (char*)ucx_map_sstr_get(map, sstrn(long_name, name_len));
456 char *lk = (char*)ucx_map_cstr_get(map, "last_key");
457
458 UCX_TEST_ASSERT(v1, "value for key1 not found");
459 UCX_TEST_ASSERT(v2, "value for key2 not found");
460 UCX_TEST_ASSERT(v3, "value for key3 not found");
461 UCX_TEST_ASSERT(lv, "value for long key not found");
462 UCX_TEST_ASSERT(lk, "value for last_key not found");
463
464 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
465 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
466 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
467 sstr_t long1 = sstrn(long_value, value_len);
468 sstr_t long2 = sstr(lv);
469 UCX_TEST_ASSERT((!sstrcmp(long1, long2)), "wrong value for long key");
470 UCX_TEST_ASSERT(!strcmp(lk, "property value"), "wrong value for last_key");
471
472 free(v1);
473 free(v2);
474 free(v3);
475 free(lv);
476 free(lk);
477 ucx_map_free(map);
478 fclose(f);
479
480 UCX_TEST_END
481 }
482
483 UCX_TEST_IMPLEMENT(test_ucx_properties_store) {
484 UcxMap *map1 = ucx_map_new(16);
485 ucx_map_cstr_put(map1, "key1", "value1");
486 ucx_map_cstr_put(map1, "key2", "value2");
487 ucx_map_cstr_put(map1, "key3", "value3");
488 ucx_map_cstr_put(map1, "key4", "value4");
489 ucx_map_cstr_put(map1, "property.key1", "some value 1");
490 ucx_map_cstr_put(map1, "property.key2", "some value 2");
491 ucx_map_cstr_put(map1, "property.key3", "some value 3");
492 ucx_map_cstr_put(map1, "property.key4", "some value 4");
493
494 UCX_TEST_BEGIN
495
496 FILE *f = tmpfile();
497 fprintf(f, "#\n# test property file\n#\n#\n");
498 ucx_properties_store(map1, f);
499
500 fflush(f);
501 fseek(f, 0, SEEK_SET);
502 UcxMap *map2 = ucx_map_new(16);
503 ucx_properties_load(map2, f);
504
505 UCX_TEST_ASSERT(map2->count == 8, "wrong number of properties in map2");
506
507 char *v1 = (char*)ucx_map_cstr_get(map2, "key1");
508 char *v2 = (char*)ucx_map_cstr_get(map2, "key2");
509 char *v3 = (char*)ucx_map_cstr_get(map2, "key3");
510 char *v4 = (char*)ucx_map_cstr_get(map2, "key4");
511 char *v5 = (char*)ucx_map_cstr_get(map2, "property.key1");
512 char *v6 = (char*)ucx_map_cstr_get(map2, "property.key2");
513 char *v7 = (char*)ucx_map_cstr_get(map2, "property.key3");
514 char *v8 = (char*)ucx_map_cstr_get(map2, "property.key4");
515
516 UCX_TEST_ASSERT(v1 && v2 && v3 && v4 && v5 && v6 && v7 && v8,
517 "missing values");
518
519 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value 1");
520 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value 2");
521 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value 3");
522 UCX_TEST_ASSERT((!strcmp(v4, "value4")), "wrong value 4");
523 UCX_TEST_ASSERT((!strcmp(v5, "some value 1")), "wrong value 5");
524 UCX_TEST_ASSERT((!strcmp(v6, "some value 2")), "wrong value 6");
525 UCX_TEST_ASSERT((!strcmp(v7, "some value 3")), "wrong value 7");
526 UCX_TEST_ASSERT((!strcmp(v8, "some value 4")), "wrong value 8");
527
528 free(v1);
529 free(v2);
530 free(v3);
531 free(v4);
532 free(v5);
533 free(v6);
534 free(v7);
535 free(v8);
536 ucx_map_free(map2);
537 fclose(f);
538
539 UCX_TEST_END
540
541 ucx_map_free(map1);
542 }

mercurial