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 } |