X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=t%2Fcore%2Fdata_test.c;h=261d5fd5a08ef9c60eb15eaf175897aeb27ed7fa;hp=fb3b1145bac8001458b95b598a348c92dc15ca20;hb=3ba81224dac4d31ea5ad651db569543825ea5078;hpb=4f8a84025ecb0d230407d54743186c2b4d1941c9 diff --git a/t/core/data_test.c b/t/core/data_test.c index fb3b114..261d5fd 100644 --- a/t/core/data_test.c +++ b/t/core/data_test.c @@ -109,76 +109,77 @@ END_TEST START_TEST(test_format) { - sdb_data_t datum; - sdb_strbuf_t *buf; - const char *string; - const char *expected; - - int check; - - buf = sdb_strbuf_create(1024); - fail_unless(buf != NULL, - "INTERNAL ERROR: Failed to allocate string buffer"); - - datum.type = SDB_TYPE_INTEGER; - datum.data.integer = 4711; - check = sdb_data_format(&datum, buf); - fail_unless(! check, - "sdb_data_format(INTEGER) = %d; expected: 0", check); - string = sdb_strbuf_string(buf); - expected = "4711"; - fail_unless(! strcmp(string, expected), - "sdb_data_format() used wrong format: %s; expected: %s", - string, expected); - - datum.type = SDB_TYPE_DECIMAL; - datum.data.decimal = 65536.0; - sdb_strbuf_clear(buf); - check = sdb_data_format(&datum, buf); - fail_unless(! check, - "sdb_data_format(DECIMAL) = %d; expected: 0", check); - string = sdb_strbuf_string(buf); - expected = "0x1p+16"; - fail_unless(! strcmp(string, expected), - "sdb_data_format() used wrong format: %s; expected: %s", - string, expected); - - datum.type = SDB_TYPE_STRING; - datum.data.string = "this is a test"; - sdb_strbuf_clear(buf); - check = sdb_data_format(&datum, buf); - fail_unless(! check, - "sdb_data_format(STRING) = %d; expected: 0", check); - string = sdb_strbuf_string(buf); - expected = "\"this is a test\""; - fail_unless(! strcmp(string, expected), - "sdb_data_format() used wrong format: %s; expected: %s", - string, expected); - - datum.type = SDB_TYPE_DATETIME; - datum.data.datetime = 471147114711471100; - sdb_strbuf_clear(buf); - check = sdb_data_format(&datum, buf); - fail_unless(! check, - "sdb_data_format(DATETIME) = %d; expected: 0", check); - string = sdb_strbuf_string(buf); - expected = "1984-12-06 02:11:54 +0000"; - fail_unless(! strcmp(string, expected), - "sdb_data_format() used wrong format: %s; expected: %s", - string, expected); - - datum.type = SDB_TYPE_BINARY; - datum.data.binary.datum = (unsigned char *)"binary\0crap\x42"; - datum.data.binary.length = 12; - sdb_strbuf_clear(buf); - check = sdb_data_format(&datum, buf); - fail_unless(! check, - "sdb_data_format(BINARY) = %d; expected: 0", check); - string = sdb_strbuf_string(buf); - expected = "\"\\62\\69\\6e\\61\\72\\79\\0\\63\\72\\61\\70\\42\""; - fail_unless(! strcmp(string, expected), - "sdb_data_format() used wrong format: %s; expected: %s", - string, expected); + 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