tests/test_properties.c

changeset 1558
fc863c877a75
parent 1557
03fbf1c99e73
child 1561
fcebf53de51c
--- a/tests/test_properties.c	Mon Dec 08 23:09:11 2025 +0100
+++ b/tests/test_properties.c	Tue Dec 09 17:27:58 2025 +0100
@@ -451,6 +451,62 @@
     remove(fname);
 }
 
+CX_TEST(test_properties_load_empty_file) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    char fname[16] = "ucxtestXXXXXX";
+    int tmpfd = mkstemp(fname);
+    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    CX_TEST_DO {
+        CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        fclose(f);
+        f = NULL;
+
+        CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
+        // store something that we don't want to be deleted
+        cxMapPut(map, "test", "value");
+        CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map);
+        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_DATA);
+        CX_TEST_ASSERT(cxMapSize(map) == 1);
+
+        char *v = cxMapGet(map, "test");
+        CX_TEST_ASSERTM(v, "value was removed");
+        CX_TEST_ASSERT(!strcmp(v, "value"));
+        CX_TEST_ASSERT(!cx_testing_allocator_used(&talloc));
+        cxMapFree(map);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+    if (f) fclose(f);
+    remove(fname);
+}
+
+CX_TEST(test_properties_load_only_comments) {
+    CxTestingAllocator talloc;
+    cx_testing_allocator_init(&talloc);
+    char fname[16] = "ucxtestXXXXXX";
+    int tmpfd = mkstemp(fname);
+    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    CX_TEST_DO {
+        CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        fputs("# test file\n\n# contains only comments\n\n# key = value\n", f);
+        fclose(f);
+        f = NULL;
+
+        CxMap *map = cxHashMapCreateSimple(CX_STORE_POINTERS);
+        CxPropertiesStatus status = cxPropertiesLoadDefault(&talloc.base, fname, map);
+        CX_TEST_ASSERT(status == CX_PROPERTIES_NO_DATA);
+        CX_TEST_ASSERT(cxMapSize(map) == 0);
+
+        CX_TEST_ASSERT(!cx_testing_allocator_used(&talloc));
+        cxMapFree(map);
+        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) {
     const char *props1 = "key1 = value1\n";
     const char *props2 = "key2 = value2\n";
@@ -559,7 +615,8 @@
     cx_test_register(suite, test_properties_next_part);
     cx_test_register(suite, test_properties_next_long_lines);
     cx_test_register(suite, test_properties_load);
-    // TODO: test_properties_load_empty_file
+    cx_test_register(suite, test_properties_load_empty_file);
+    cx_test_register(suite, test_properties_load_only_comments);
     // TODO: test_properties_load_invalid_key
     // TODO: test_properties_load_missing_delimiter
     // TODO: test_properties_load_unexpected_end

mercurial