Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / t / core / data_test.c
index d6bd01459bc83019a9fee93f51c399933a24a919..261d5fd5a08ef9c60eb15eaf175897aeb27ed7fa 100644 (file)
@@ -109,9 +109,6 @@ END_TEST
 
 START_TEST(test_format)
 {
-       sdb_strbuf_t *buf;
-       size_t i;
-
        struct {
                sdb_data_t datum;
                const char *expected;
@@ -153,27 +150,36 @@ START_TEST(test_format)
                },
        };
 
-       buf = sdb_strbuf_create(1024);
-       fail_unless(buf != NULL,
-               "INTERNAL ERROR: Failed to allocate string buffer");
+       size_t i;
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
-               const char *string;
+               sdb_data_t *datum = &golden_data[i].datum;
+               char buf[sdb_data_strlen(datum) + 2];
                int check;
 
-               check = sdb_data_format(&golden_data[i].datum, buf);
-               fail_unless(! check,
-                               "sdb_data_format(type=%s) = %d; expected: 0",
-                               SDB_TYPE_TO_STRING(golden_data[i].datum.type), check);
-               string = sdb_strbuf_string(buf);
-               fail_unless(! strcmp(string, golden_data[i].expected),
+               memset(buf, (int)'A', sizeof(buf));
+
+               check = sdb_data_format(datum, buf, sizeof(buf) - 1,
+                               SDB_DOUBLE_QUOTED);
+               fail_unless(check > 0,
+                               "sdb_data_format(type=%s) = %d; expected: >0",
+                               SDB_TYPE_TO_STRING(datum->type), check);
+               fail_unless(! strcmp(buf, golden_data[i].expected),
                                "sdb_data_format(type=%s) used wrong format: %s; expected: %s",
-                               SDB_TYPE_TO_STRING(golden_data[i].datum.type),
-                               string, golden_data[i].expected);
-               sdb_strbuf_clear(buf);
+                               SDB_TYPE_TO_STRING(datum->type), buf, golden_data[i].expected);
+
+               fail_unless((size_t)check <= sizeof(buf) - 2,
+                               "sdb_data_format(type=%s) wrote %d bytes; "
+                               "expected <= %zu based on sdb_data_strlen()",
+                               SDB_TYPE_TO_STRING(datum->type), check, sizeof(buf) - 2);
+
+               fail_unless(buf[sizeof(buf) - 2] == '\0',
+                               "sdb_data_format(type=%s) did not nul-terminate the buffer",
+                               SDB_TYPE_TO_STRING(datum->type));
+               fail_unless(buf[sizeof(buf) - 1] == 'A',
+                               "sdb_data_format(type=%s) wrote past the end of the buffer",
+                               SDB_TYPE_TO_STRING(datum->type));
        }
-
-       sdb_strbuf_destroy(buf);
 }
 END_TEST