tests/test_properties.c

changeset 928
d2d42cb1d59e
parent 924
3c90dfc35f06
child 929
192a440b99df
equal deleted inserted replaced
927:71e7e9ba4b97 928:d2d42cb1d59e
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/test.h" 29 #include "cx/test.h"
30 #include "util_allocator.h"
30 31
31 #include "cx/properties.h" 32 #include "cx/properties.h"
33 #include "cx/hash_map.h"
32 34
33 CX_TEST(test_cx_properties_init) { 35 CX_TEST(test_cx_properties_init) {
34 CxProperties prop; 36 CxProperties prop;
35 CX_TEST_DO { 37 CX_TEST_DO {
36 cxPropertiesInitDefault(&prop); 38 cxPropertiesInitDefault(&prop);
87 "value" 89 "value"
88 }; 90 };
89 91
90 CxProperties prop; 92 CxProperties prop;
91 cxPropertiesInitDefault(&prop); 93 cxPropertiesInitDefault(&prop);
92 enum cx_properties_status result; 94 CxPropertiesStatus result;
93 cxstring key; 95 cxstring key;
94 cxstring value; 96 cxstring value;
95 CX_TEST_DO { 97 CX_TEST_DO {
96 for (int i = 0; i < 10; i++) { 98 for (int i = 0; i < 10; i++) {
97 cxPropertiesInput(&prop, tests[i], strlen(tests[i])); 99 cxPropertiesInput(&prop, tests[i], strlen(tests[i]));
153 "key2 = value2\n" 155 "key2 = value2\n"
154 "\n\n\n \n key3=value3\n"; 156 "\n\n\n \n key3=value3\n";
155 157
156 CxProperties prop; 158 CxProperties prop;
157 cxPropertiesInitDefault(&prop); 159 cxPropertiesInitDefault(&prop);
158 enum cx_properties_status result; 160 CxPropertiesStatus result;
159 cxstring key; 161 cxstring key;
160 cxstring value; 162 cxstring value;
161 163
162 CX_TEST_DO { 164 CX_TEST_DO {
163 result = cxPropertiesNext(&prop, &key, &value); 165 result = cxPropertiesNext(&prop, &key, &value);
176 } 178 }
177 179
178 CX_TEST(test_cx_properties_next_part) { 180 CX_TEST(test_cx_properties_next_part) {
179 CxProperties prop; 181 CxProperties prop;
180 cxPropertiesInitDefault(&prop); 182 cxPropertiesInitDefault(&prop);
181 enum cx_properties_status result; 183 CxPropertiesStatus result;
182 cxstring key; 184 cxstring key;
183 cxstring value; 185 cxstring value;
184 const char *str; 186 const char *str;
185 187
186 CX_TEST_DO { 188 CX_TEST_DO {
285 } 287 }
286 288
287 CX_TEST(test_ucx_properties_next_long_lines) { 289 CX_TEST(test_ucx_properties_next_long_lines) {
288 CxProperties prop; 290 CxProperties prop;
289 cxPropertiesInitDefault(&prop); 291 cxPropertiesInitDefault(&prop);
290 enum cx_properties_status result; 292 CxPropertiesStatus result;
291 cxstring key; 293 cxstring key;
292 cxstring value; 294 cxstring value;
293 295
294 size_t key_len = 512; 296 size_t key_len = 512;
295 char *long_key = (char*)malloc(key_len); 297 char *long_key = (char*)malloc(key_len);
365 367
366 free(long_key); 368 free(long_key);
367 free(long_value); 369 free(long_value);
368 } 370 }
369 371
372 CX_TEST(test_cx_properties_load_string_to_map) {
373 CxTestingAllocator talloc;
374 cx_testing_allocator_init(&talloc);
375 CxAllocator *alloc = &talloc.base;
376 CX_TEST_DO {
377 const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n";
378
379 CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
380 cxDefineAdvancedDestructor(map, cxFree, alloc);
381 CxProperties prop;
382 cxPropertiesInitDefault(&prop);
383 CxPropertiesSink sink = cxPropertiesMapSink(map);
384 sink.data = alloc; // use the testing allocator
385 CxPropertiesSource src = cxPropertiesCstrSource(str);
386 CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
387
388 CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
389 CX_TEST_ASSERT(cxMapSize(map) == 3);
390
391 char *v1 = cxMapGet(map, "key1");
392 char *v2 = cxMapGet(map, "key2");
393 char *v3 = cxMapGet(map, "key3");
394
395 CX_TEST_ASSERTM(v1, "value for key1 not found");
396 CX_TEST_ASSERTM(v2, "value for key2 not found");
397 CX_TEST_ASSERTM(v3, "value for key3 not found");
398
399 CX_TEST_ASSERT(!strcmp(v1, "value1"));
400 CX_TEST_ASSERT(!strcmp(v2, "value2"));
401 CX_TEST_ASSERT(!strcmp(v3, "value3"));
402
403 // second test
404 cxMapClear(map);
405
406 str = "\n#comment\n";
407 src = cxPropertiesCstrnSource(str, strlen(str));
408 status = cxPropertiesLoad(&prop, sink, src);
409
410 CX_TEST_ASSERT(status == CX_PROPERTIES_NO_DATA);
411 CX_TEST_ASSERT(cxMapSize(map) == 0);
412
413 str = "key1 = value1\nsyntax error line\n";
414 src = cxPropertiesStringSource(cx_str(str));
415 status = cxPropertiesLoad(&prop, sink, src);
416
417 CX_TEST_ASSERT(status == CX_PROPERTIES_INVALID_MISSING_DELIMITER);
418
419 // the successfully read k/v-pair is in the map, nevertheless
420 CX_TEST_ASSERT(cxMapSize(map) == 1);
421 char *v = cxMapGet(map, "key1");
422 CX_TEST_ASSERT(!strcmp(v, "value1"));
423
424 cxMapDestroy(map);
425 cxPropertiesDestroy(&prop);
426
427 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
428 }
429 cx_testing_allocator_destroy(&talloc);
430 }
431
432 CX_TEST(test_cx_properties_load_file_to_map) {
433 CxTestingAllocator talloc;
434 cx_testing_allocator_init(&talloc);
435 CxAllocator *alloc = &talloc.base;
436 CX_TEST_DO {
437 FILE *f = tmpfile();
438 CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
439 fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
440 fprintf(f, "\n\nkey3 = value3\n\n");
441
442 size_t key_len = 512;
443 char *long_key = (char *) malloc(key_len);
444 memset(long_key, 'k', 512);
445
446 size_t value_len = 2048;
447 char *long_value = (char *) malloc(value_len);
448 memset(long_value, 'v', 2048);
449
450 fwrite(long_key, 1, key_len, f);
451 fprintf(f, " = ");
452 fwrite(long_value, 1, value_len, f);
453 fprintf(f, " \n");
454
455 fprintf(f, "\n\n\n\nlast_key = property value\n");
456
457 fflush(f);
458 fseek(f, 0, SEEK_SET);
459
460 // preparation of test file complete
461
462 CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
463 cxDefineAdvancedDestructor(map, cxFree, alloc);
464 CxProperties prop;
465 cxPropertiesInitDefault(&prop);
466 CxPropertiesSink sink = cxPropertiesMapSink(map);
467 sink.data = alloc; // use the testing allocator
468 CxPropertiesSource src = cxPropertiesFileSource(f, 512);
469 CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
470 fclose(f);
471
472 CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
473 CX_TEST_ASSERT(cxMapSize(map) == 5);
474
475 char *v1 = cxMapGet(map, "key1");
476 char *v2 = cxMapGet(map, cx_str("key2"));
477 char *v3 = cxMapGet(map, "key3");
478 char *lv = cxMapGet(map, cx_strn(long_key, key_len));
479 char *lk = cxMapGet(map, "last_key");
480
481 CX_TEST_ASSERTM(v1, "value for key1 not found");
482 CX_TEST_ASSERTM(v2, "value for key2 not found");
483 CX_TEST_ASSERTM(v3, "value for key3 not found");
484 CX_TEST_ASSERTM(lv, "value for long key not found");
485 CX_TEST_ASSERTM(lk, "value for last_key not found");
486
487 CX_TEST_ASSERT(!strcmp(v1, "value1"));
488 CX_TEST_ASSERT(!strcmp(v2, "value2"));
489 CX_TEST_ASSERT(!strcmp(v3, "value3"));
490 cxstring expected = cx_strn(long_value, value_len);
491 cxstring actual = cx_str(lv);
492 CX_TEST_ASSERT(!cx_strcmp(expected, actual));
493 CX_TEST_ASSERT(!strcmp(lk, "property value"));
494
495 free(long_key);
496 free(long_value);
497 cxMapDestroy(map);
498 cxPropertiesDestroy(&prop);
499
500 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
501 }
502 cx_testing_allocator_destroy(&talloc);
503 }
504
370 CxTestSuite *cx_test_suite_properties(void) { 505 CxTestSuite *cx_test_suite_properties(void) {
371 CxTestSuite *suite = cx_test_suite_new("properties"); 506 CxTestSuite *suite = cx_test_suite_new("properties");
372 507
373 cx_test_register(suite, test_cx_properties_init); 508 cx_test_register(suite, test_cx_properties_init);
374 cx_test_register(suite, test_cx_properties_next); 509 cx_test_register(suite, test_cx_properties_next);
375 cx_test_register(suite, test_cx_properties_next_multi); 510 cx_test_register(suite, test_cx_properties_next_multi);
376 cx_test_register(suite, test_cx_properties_next_part); 511 cx_test_register(suite, test_cx_properties_next_part);
377 cx_test_register(suite, test_ucx_properties_next_long_lines); 512 cx_test_register(suite, test_ucx_properties_next_long_lines);
513 cx_test_register(suite, test_cx_properties_load_string_to_map);
514 cx_test_register(suite, test_cx_properties_load_file_to_map);
378 515
379 return suite; 516 return suite;
380 } 517 }

mercurial