tests/test_properties.c

changeset 1555
8972247f54e8
parent 1124
fcd5d86c472f
--- a/tests/test_properties.c	Sun Dec 07 15:33:16 2025 +0100
+++ b/tests/test_properties.c	Sun Dec 07 15:34:46 2025 +0100
@@ -383,75 +383,11 @@
     free(long_value);
 }
 
-CX_TEST(test_properties_load_string_to_map) {
-    CxTestingAllocator talloc;
-    cx_testing_allocator_init(&talloc);
-    CxAllocator *alloc = &talloc.base;
+CX_TEST(test_properties_load) {
+    char fname[16] = "ucxtestXXXXXX";
+    int tmpfd = mkstemp(fname);
+    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
     CX_TEST_DO {
-        char buffer[512];
-        CxProperties prop;
-        cxPropertiesInitDefault(&prop);
-        cxPropertiesUseStack(&prop, buffer, 512);
-
-        const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n";
-
-        CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
-        cxDefineAdvancedDestructor(map, cxFree, alloc);
-        CxPropertiesSink sink = cxPropertiesMapSink(map);
-        sink.data = alloc; // use the testing allocator
-        CxPropertiesSource src = cxPropertiesCstrSource(str);
-        CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
-
-        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
-        CX_TEST_ASSERT(cxMapSize(map) == 3);
-
-        char *v1 = cxMapGet(map, "key1");
-        char *v2 = cxMapGet(map, "key2");
-        char *v3 = cxMapGet(map, "key3");
-
-        CX_TEST_ASSERTM(v1, "value for key1 not found");
-        CX_TEST_ASSERTM(v2, "value for key2 not found");
-        CX_TEST_ASSERTM(v3, "value for key3 not found");
-
-        CX_TEST_ASSERT(!strcmp(v1, "value1"));
-        CX_TEST_ASSERT(!strcmp(v2, "value2"));
-        CX_TEST_ASSERT(!strcmp(v3, "value3"));
-
-        // second test
-        cxMapClear(map);
-
-        str = "\n#comment\n";
-        src = cxPropertiesCstrnSource(str, strlen(str));
-        status = cxPropertiesLoad(&prop, sink, src);
-
-        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_DATA);
-        CX_TEST_ASSERT(cxMapSize(map) == 0);
-
-        str = "key1 = value1\nsyntax error line\n";
-        src = cxPropertiesStringSource(cx_str(str));
-        status = cxPropertiesLoad(&prop, sink, src);
-
-        CX_TEST_ASSERT(status == CX_PROPERTIES_INVALID_MISSING_DELIMITER);
-
-        // the successfully read k/v-pair is in the map, nevertheless
-        CX_TEST_ASSERT(cxMapSize(map) == 1);
-        char *v = cxMapGet(map, "key1");
-        CX_TEST_ASSERT(!strcmp(v, "value1"));
-
-        cxMapFree(map);
-        cxPropertiesDestroy(&prop);
-
-        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
-    }
-    cx_testing_allocator_destroy(&talloc);
-}
-
-CX_TEST(test_properties_load_file_to_map) {
-    CxTestingAllocator talloc;
-    cx_testing_allocator_init(&talloc);
-    CxAllocator *alloc = &talloc.base;
-    CX_TEST_DO {
-        FILE *f = tmpfile();
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
         fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
         fprintf(f, "\n\nkey3    = value3\n\n");
@@ -470,21 +406,14 @@
         fprintf(f, "                         \n");
 
         fprintf(f, "\n\n\n\nlast_key = property value\n");
-
-        fflush(f);
-        fseek(f, 0, SEEK_SET);
-
+        fclose(f);
+        f = NULL;
         // preparation of test file complete
 
+        // we want to load the properties into a map of char* pointers
         CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
-        cxDefineAdvancedDestructor(map, cxFree, alloc);
-        CxProperties prop;
-        cxPropertiesInitDefault(&prop);
-        CxPropertiesSink sink = cxPropertiesMapSink(map);
-        sink.data = alloc; // use the testing allocator
-        CxPropertiesSource src = cxPropertiesFileSource(f, 512);
-        CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
-        fclose(f);
+        cxDefineDestructor(map, cxFreeDefault);
+        CxPropertiesStatus status = cxPropertiesLoadDefault(fname, map);
 
         CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
         CX_TEST_ASSERT(cxMapSize(map) == 5);
@@ -512,69 +441,9 @@
         free(long_key);
         free(long_value);
         cxMapFree(map);
-        cxPropertiesDestroy(&prop);
-
-        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
     }
-    cx_testing_allocator_destroy(&talloc);
-}
-
-CX_TEST(test_properties_load_incomplete) {
-    CxTestingAllocator talloc;
-    cx_testing_allocator_init(&talloc);
-    CxAllocator *alloc = &talloc.base;
-    CX_TEST_DO {
-        char buffer[512];
-        CxProperties prop;
-        cxPropertiesInitDefault(&prop);
-        cxPropertiesUseStack(&prop, buffer, 512);
-
-        CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
-        cxDefineAdvancedDestructor(map, cxFree, alloc);
-        CxPropertiesSink sink = cxPropertiesMapSink(map);
-        sink.data = alloc; // use the testing allocator
-        CxPropertiesSource src = cxPropertiesCstrSource("key1 = value1\nkey2 = value2\n\n#comment\n\nkey3");
-        CxPropertiesStatus status = cxPropertiesLoad(&prop, sink, src);
-
-        CX_TEST_ASSERT(status == CX_PROPERTIES_INCOMPLETE_DATA);
-        CX_TEST_ASSERT(cxMapSize(map) == 2);
-
-        char *v1 = cxMapGet(map, "key1");
-        char *v2 = cxMapGet(map, "key2");
-        char *v3 = cxMapGet(map, "key3");
-
-        CX_TEST_ASSERTM(v1, "value for key1 not found");
-        CX_TEST_ASSERTM(v2, "value for key2 not found");
-        CX_TEST_ASSERT(v3 == NULL);
-
-        CX_TEST_ASSERT(!strcmp(v1, "value1"));
-        CX_TEST_ASSERT(!strcmp(v2, "value2"));
-
-        // provide a source with the remaining data
-        src = cxPropertiesCstrSource(" = value3\n");
-        status = cxPropertiesLoad(&prop, sink, src);
-
-        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_ERROR);
-        CX_TEST_ASSERT(cxMapSize(map) == 3);
-
-        v1 = cxMapGet(map, "key1");
-        v2 = cxMapGet(map, "key2");
-        v3 = cxMapGet(map, "key3");
-
-        CX_TEST_ASSERTM(v1, "value for key1 not found");
-        CX_TEST_ASSERTM(v2, "value for key2 not found");
-        CX_TEST_ASSERTM(v3, "value for key3 not found");
-
-        CX_TEST_ASSERT(!strcmp(v1, "value1"));
-        CX_TEST_ASSERT(!strcmp(v2, "value2"));
-        CX_TEST_ASSERT(!strcmp(v3, "value3"));
-
-        cxMapFree(map);
-        cxPropertiesDestroy(&prop);
-
-        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
-    }
-    cx_testing_allocator_destroy(&talloc);
+    if (f) fclose(f);
+    remove(fname);
 }
 
 CX_TEST(test_properties_multiple_fill) {
@@ -684,9 +553,14 @@
     cx_test_register(suite, test_properties_next_multi);
     cx_test_register(suite, test_properties_next_part);
     cx_test_register(suite, test_properties_next_long_lines);
-    cx_test_register(suite, test_properties_load_string_to_map);
-    cx_test_register(suite, test_properties_load_file_to_map);
-    cx_test_register(suite, test_properties_load_incomplete);
+    cx_test_register(suite, test_properties_load);
+    // TODO: test_properties_load_empty_file
+    // TODO: test_properties_load_invalid_key
+    // TODO: test_properties_load_missing_delimiter
+    // TODO: test_properties_load_unexpected_end
+    // TODO: test_properties_load_file_not_exists
+    // TODO: test_properties_load_exceed_stack
+    // TODO: test_properties_load_incompatible_map
     cx_test_register(suite, test_properties_multiple_fill);
     cx_test_register(suite, test_properties_use_stack);
     cx_test_register(suite, test_properties_empty_key);

mercurial