diff --git a/t/core/data_test.c b/t/core/data_test.c
index 7efa5549fed5e8a780cc8fe4080d19ca7630f526..261d5fd5a08ef9c60eb15eaf175897aeb27ed7fa 100644 (file)
--- a/t/core/data_test.c
+++ b/t/core/data_test.c
}
END_TEST
+START_TEST(test_format)
+{
+ struct {
+ sdb_data_t datum;
+ const char *expected;
+ } golden_data[] = {
+ {
+ { SDB_TYPE_INTEGER, { .integer = 4711 } },
+ "4711",
+ },
+ {
+ { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
+ "0x1p+16",
+ },
+ {
+ { SDB_TYPE_STRING, { .string = NULL } },
+ "\"NULL\"",
+ },
+ {
+ { SDB_TYPE_STRING, { .string = "this is a test" } },
+ "\"this is a test\"",
+ },
+ {
+ { SDB_TYPE_STRING, { .string = "special \\ \" characters" } },
+ "\"special \\\\ \\\" characters\"",
+ },
+ {
+ { SDB_TYPE_DATETIME, { .datetime= 471147114711471100 } },
+ "\"1984-12-06 02:11:54 +0000\"",
+ },
+ {
+ { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
+ "\"\"",
+ },
+ {
+ {
+ SDB_TYPE_BINARY,
+ { .binary = { 12, (unsigned char *)"binary\0crap\x42" } },
+ },
+ "\"\\x62\\x69\\x6e\\x61\\x72\\x79\\x0\\x63\\x72\\x61\\x70\\x42\"",
+ },
+ };
+
+ size_t i;
+
+ for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+ sdb_data_t *datum = &golden_data[i].datum;
+ char buf[sdb_data_strlen(datum) + 2];
+ int check;
+
+ 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(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));
+ }
+}
+END_TEST
+
Suite *
core_data_suite(void)
{
tc = tcase_create("core");
tcase_add_test(tc, test_data);
+ tcase_add_test(tc, test_format);
suite_add_tcase(s, tc);
return s;