Code

strbuf_test: Added some more tests.
[sysdb.git] / t / unit / utils / strbuf_test.c
index f9c3e93f3987313d5704ad62ebed2e0898469915..34edbe21a0f95fc0d99caa3767fbf544f6e5e65a 100644 (file)
@@ -109,7 +109,7 @@ START_TEST(test_empty)
 }
 END_TEST
 
-START_TEST(test_sdb_strbuf_create)
+START_TEST(test_create)
 {
        sdb_strbuf_t *s;
        size_t len;
@@ -135,61 +135,100 @@ START_TEST(test_sdb_strbuf_create)
 }
 END_TEST
 
-START_TEST(test_sdb_strbuf_append)
+START_TEST(test_append)
 {
-       ssize_t n, expected;
-       size_t len;
+       ssize_t n;
+       size_t len, total = 0;
        const char *test;
 
-       n = sdb_strbuf_append(buf, "1234567890");
-       fail_unless(n == 10,
-                       "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
-       len = sdb_strbuf_len(buf);
-       fail_unless(len == 10,
-                       "sdb_strbuf_append() left behind buffer with len = %zu; "
-                       "expected: 10", len);
+       struct {
+               const char *input;
+               const char *result;
+       } golden_data[] = {
+               { "1234567890", "1234567890" },
+               { "ABCDE",      "1234567890ABCDE" },
+               { "",           "1234567890ABCDE" },
+               { "-",          "1234567890ABCDE-" },
+               /* when adding anything to this array, the last check has to be
+                * updated accordingly */
+       };
 
-       n = sdb_strbuf_append(buf, "ABCDE");
-       fail_unless(n == 5,
-                       "sdb_strbuf_append() appended %zi bytes; expected: 5", n);
-       len = sdb_strbuf_len(buf);
-       fail_unless(len == 15,
-                       "sdb_strbuf_append() left behind buffer with len = %zu; "
-                       "expected: 15", len);
+       size_t i;
 
-       test = sdb_strbuf_string(buf);
-       fail_unless(test[len] == '\0',
-                       "sdb_strbuf_append() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "1234567890ABCDE"),
-                       "sdb_strbuf_append() did not correctly concatenate two string; "
-                       "got: %s; expected: 1234567890ABCDE", test);
-
-       n = sdb_strbuf_append(buf, "%zu; %5.4f", len, (double)len / 10.0);
-       expected = /* len */ 2 + /* "; " */ 2 + /* decimal len/10 */ 6;
-       fail_unless(n == expected,
-                       "sdb_strbuf_append() appended %zi bytes; expected: %zi",
-                       n, expected);
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               n = sdb_strbuf_append(buf, golden_data[i].input);
+               fail_unless((size_t)n == strlen(golden_data[i].input),
+                               "sdb_strbuf_append() appended %zi bytes; expected: %zu",
+                               n, strlen(golden_data[i].input));
+               total += n;
+               len = sdb_strbuf_len(buf);
+               fail_unless(len == total,
+                               "sdb_strbuf_append() left behind buffer with len = %zu; "
+                               "expected: %zu", len, total);
+
+               test = sdb_strbuf_string(buf);
+               fail_unless(test[len] == '\0',
+                               "sdb_strbuf_append() did not nil-terminate the string");
+
+               test = sdb_strbuf_string(buf);
+               fail_unless(!strcmp(test, golden_data[i].result),
+                               "sdb_strbuf_append() did not correctly concatenate "
+                               "the input; got: %s; expected: %s",
+                               test, golden_data[i].result);
+       }
+
+       n = sdb_strbuf_append(buf, "%zu; %5.4f", 42, 4.2);
+       fail_unless(n == 10,
+                       "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
+       total += n;
        len = sdb_strbuf_len(buf);
-       fail_unless(len == 15 + (size_t)expected,
+       fail_unless(len == total,
                        "sdb_strbuf_append() left behind buffer with len = %zu; "
-                       "expected: %zu", len, 15 + (size_t)expected);
+                       "expected: %zu", len, total);
 
        test = sdb_strbuf_string(buf);
        fail_unless(test[len] == '\0',
                        "sdb_strbuf_append() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "1234567890ABCDE15; 1.5000"),
-                       "sdb_strbuf_append() did not correctly concatenate two string; "
-                       "got: %s; expected: 1234567890ABCDE15; 1.5000", test);
+       fail_unless(!strcmp(test, "1234567890ABCDE-42; 4.2000"),
+                       "sdb_strbuf_append() did not correctly concatenate the input; "
+                       "got: %s; expected: 1234567890ABCDE-42; 4.2000", test);
 }
 END_TEST
 
-START_TEST(test_sdb_strbuf_sprintf)
+START_TEST(test_sprintf)
 {
-       ssize_t n, expected;
+       ssize_t n;
        size_t len;
        const char *test;
 
-       n = sdb_strbuf_sprintf(buf, "1234567890");
+       const char *golden_data[] = {
+               "1234567890",
+               "ABCDE",
+               "",
+               "-",
+       };
+
+       size_t i;
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               n = sdb_strbuf_sprintf(buf, golden_data[i]);
+               fail_unless((size_t)n == strlen(golden_data[i]),
+                               "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zu",
+                               n, strlen(golden_data[i]));
+               len = sdb_strbuf_len(buf);
+               fail_unless(len == (size_t)n,
+                               "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
+                               "expected: %zi", len, n);
+
+               test = sdb_strbuf_string(buf);
+               fail_unless(test[len] == '\0',
+                               "sdb_strbuf_sprintf() did not nil-terminate the string");
+               fail_unless(!strcmp(test, golden_data[i]),
+                               "sdb_strbuf_sprintf() did not format string correctly; "
+                               "got: %s; expected: %s", test, golden_data[i]);
+       }
+
+       n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", 42, 4.2);
        fail_unless(n == 10,
                        "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
        len = sdb_strbuf_len(buf);
@@ -197,42 +236,19 @@ START_TEST(test_sdb_strbuf_sprintf)
                        "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
                        "expected: 10", len);
 
-       n = sdb_strbuf_sprintf(buf, "ABCDE");
-       fail_unless(n == 5,
-                       "sdb_strbuf_sprintf() wrote %zi bytes; expected: 5", n);
-       len = sdb_strbuf_len(buf);
-       fail_unless(len == 5,
-                       "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
-                       "expected: 5", len);
-
        test = sdb_strbuf_string(buf);
        fail_unless(test[len] == '\0',
                        "sdb_strbuf_sprintf() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "ABCDE"),
+       fail_unless(!strcmp(test, "42; 4.2000"),
                        "sdb_strbuf_sprintf() did not format string correctly; "
-                       "got: %s; expected: ABCDE", test);
-
-       n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", len, (double)len / 10.0);
-       expected = /* len */ 1 + /* "; " */ 2 + /* decimal len/10 */ 6;
-       fail_unless(n == expected,
-                       "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zi",
-                       n, expected);
-       len = sdb_strbuf_len(buf);
-       fail_unless(len == (size_t)expected,
-                       "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
-                       "expected: %zu", len, (size_t)expected);
-
-       test = sdb_strbuf_string(buf);
-       fail_unless(test[len] == '\0',
-                       "sdb_strbuf_sprintf() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "5; 0.5000"),
-                       "sdb_strbuf_sprintf() did not format string correctly; "
-                       "got: %s; expected: 5; 0.5000", test);
+                       "got: %s; expected: 42; 4.2000", test);
 }
 END_TEST
 
 START_TEST(test_incremental)
 {
+       const char *data;
+
        ssize_t n;
        size_t i;
 
@@ -258,9 +274,20 @@ START_TEST(test_incremental)
 
        n = (ssize_t)sdb_strbuf_len(buf);
        fail_unless(n == 2048, "sdb_strbuf_len() = %zi; expectd: 2048", n);
+
+       data = sdb_strbuf_string(buf);
+       for (i = 0; i < 2048; ++i)
+               fail_unless(data[i] == '.',
+                               "After sdb_strbuf_append(), found character %x "
+                               "at position %zi; expected %x (.)",
+                               (int)data[i], i, '.');
+       fail_unless(data[i] == '\0',
+                       "After sdb_strbuf_append(), found character %x at end of string; "
+                       "expected '\\0'", (int)data[i]);
 }
 END_TEST
 
+/* used by test_memcpy and test_memappend */
 static struct {
        const char *input;
        size_t size;
@@ -271,7 +298,7 @@ static struct {
        { "", 0 },
 };
 
-START_TEST(test_sdb_strbuf_memcpy)
+START_TEST(test_memcpy)
 {
        size_t i;
 
@@ -304,7 +331,7 @@ START_TEST(test_sdb_strbuf_memcpy)
 }
 END_TEST
 
-START_TEST(test_sdb_strbuf_memappend)
+START_TEST(test_memappend)
 {
        size_t i;
 
@@ -350,98 +377,97 @@ START_TEST(test_sdb_strbuf_memappend)
 }
 END_TEST
 
-static struct {
-       const char *input;
-       ssize_t expected;
-       const char *expected_string;
-} chomp_golden_data[] = {
-       { NULL, 0, "" },
-       { "\n", 1, "" },
-       { "\n\n", 2, "" },
-       { "12345\n\n\n", 3, "12345" },
-       { "abcd", 0, "abcd" },
-};
-
-START_TEST(test_sdb_strbuf_chomp)
+START_TEST(test_chomp)
 {
+       struct {
+               const char *input;
+               ssize_t expected;
+               const char *expected_string;
+       } golden_data[] = {
+               { NULL, 0, "" },
+               { "\n", 1, "" },
+               { "\n\n", 2, "" },
+               { "12345\n\n\n", 3, "12345" },
+               { "abcd", 0, "abcd" },
+       };
+
        size_t i;
 
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(chomp_golden_data); ++i) {
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                ssize_t n;
                const char *check;
 
-               if (chomp_golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, chomp_golden_data[i].input);
+               if (golden_data[i].input)
+                       sdb_strbuf_sprintf(buf, golden_data[i].input);
 
                /* empty buffer */
                n = sdb_strbuf_chomp(buf);
-               fail_unless(n == chomp_golden_data[i].expected,
+               fail_unless(n == golden_data[i].expected,
                                "sdb_strbuf_chomp() = %zi; expected: %zi", n,
-                               chomp_golden_data[i].expected);
+                               golden_data[i].expected);
 
                check = sdb_strbuf_string(buf);
-               fail_unless(!strcmp(check, chomp_golden_data[i].expected_string),
+               fail_unless(!strcmp(check, golden_data[i].expected_string),
                                "sdb_strbuf_chomp() did not correctly remove newlines; "
                                "got string '%s'; expected: '%s'", check,
-                               chomp_golden_data[i].expected_string);
+                               golden_data[i].expected_string);
        }
 }
 END_TEST
 
-/* input is "1234567890" */
-static struct {
-       size_t offset;
-       size_t n;
-       const char *expected;
-       size_t expected_len;
-} skip_golden_data[] = {
-       { 0, 0, "1234567890", 10 },
-       { 0, 1, "234567890", 9 },
-       { 0, 2, "34567890", 8 },
-       { 0, 9, "0", 1 },
-       { 0, 10, "", 0 },
-       { 0, 11, "", 0 },
-       { 0, 100, "", 0 },
-       { 1, 0, "1234567890", 10 },
-       { 1, 1, "134567890", 9 },
-       { 1, 2, "14567890", 8 },
-       { 2, 0, "1234567890", 10 },
-       { 2, 1, "124567890", 9 },
-       { 2, 2, "12567890", 8 },
-       { 2, 3, "1267890", 7 },
-       { 2, 4, "127890", 6 },
-       { 2, 5, "12890", 5 },
-       { 2, 6, "1290", 4 },
-       { 2, 7, "120", 3 },
-       { 2, 8, "12", 2 },
-       { 2, 9, "12", 2 },
-       { 2, 10, "12", 2 },
-       { 8, 1, "123456780", 9 },
-       { 8, 2, "12345678", 8 },
-       { 8, 3, "12345678", 8 },
-       { 9, 1, "123456789", 9 },
-       { 9, 2, "123456789", 9 },
-       { 10, 1, "1234567890", 10 },
-       { 10, 2, "1234567890", 10 },
-};
-
-START_TEST(test_sdb_strbuf_skip)
+START_TEST(test_skip)
 {
        const char *input = "1234567890";
+       struct {
+               size_t offset;
+               size_t n;
+               const char *expected;
+               size_t expected_len;
+       } golden_data[] = {
+               { 0, 0, "1234567890", 10 },
+               { 0, 1, "234567890", 9 },
+               { 0, 2, "34567890", 8 },
+               { 0, 9, "0", 1 },
+               { 0, 10, "", 0 },
+               { 0, 11, "", 0 },
+               { 0, 100, "", 0 },
+               { 1, 0, "1234567890", 10 },
+               { 1, 1, "134567890", 9 },
+               { 1, 2, "14567890", 8 },
+               { 2, 0, "1234567890", 10 },
+               { 2, 1, "124567890", 9 },
+               { 2, 2, "12567890", 8 },
+               { 2, 3, "1267890", 7 },
+               { 2, 4, "127890", 6 },
+               { 2, 5, "12890", 5 },
+               { 2, 6, "1290", 4 },
+               { 2, 7, "120", 3 },
+               { 2, 8, "12", 2 },
+               { 2, 9, "12", 2 },
+               { 2, 10, "12", 2 },
+               { 8, 1, "123456780", 9 },
+               { 8, 2, "12345678", 8 },
+               { 8, 3, "12345678", 8 },
+               { 9, 1, "123456789", 9 },
+               { 9, 2, "123456789", 9 },
+               { 10, 1, "1234567890", 10 },
+               { 10, 2, "1234567890", 10 },
+       };
+
        size_t i;
 
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(skip_golden_data); ++i) {
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                const char *check;
                size_t n;
 
                sdb_strbuf_sprintf(buf, input);
-               sdb_strbuf_skip(buf, skip_golden_data[i].offset,
-                               skip_golden_data[i].n);
+               sdb_strbuf_skip(buf, golden_data[i].offset,
+                               golden_data[i].n);
 
                n = sdb_strbuf_len(buf);
-               fail_unless(n == skip_golden_data[i].expected_len,
+               fail_unless(n == golden_data[i].expected_len,
                                "sdb_strbuf_len() = %zu (after skip); expected: %zu",
-                               n, skip_golden_data[i].expected_len);
+                               n, golden_data[i].expected_len);
 
                check = sdb_strbuf_string(buf);
                fail_unless(!!check,
@@ -450,15 +476,15 @@ START_TEST(test_sdb_strbuf_skip)
                fail_unless(check[n] == '\0',
                                "sdb_strbuf_skip() did not nil-terminate the string");
 
-               fail_unless(! strcmp(skip_golden_data[i].expected, check),
+               fail_unless(! strcmp(golden_data[i].expected, check),
                                "sdb_strbuf_skip('%s', %zu) did not skip correctly; "
                                "got string '%s'; expected: '%s'", input,
-                               skip_golden_data[i].n, check, skip_golden_data[i].expected);
+                               golden_data[i].n, check, golden_data[i].expected);
        }
 }
 END_TEST
 
-START_TEST(test_sdb_strbuf_clear)
+START_TEST(test_clear)
 {
        const char *data;
        size_t len;
@@ -479,54 +505,54 @@ START_TEST(test_sdb_strbuf_clear)
 }
 END_TEST
 
-static struct {
-       const char *input;
-       const char *expected;
-} string_golden_data[] = {
-       { NULL, "" },
-       { "a", "a" },
-       { "abcdef", "abcdef" },
-};
-
-START_TEST(test_sdb_strbuf_string)
+START_TEST(test_string)
 {
+       struct {
+               const char *input;
+               const char *expected;
+       } golden_data[] = {
+               { NULL, "" },
+               { "a", "a" },
+               { "abcdef", "abcdef" },
+       };
+
        size_t i;
 
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(string_golden_data); ++i) {
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                const char *check;
 
-               if (string_golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, string_golden_data[i].input);
+               if (golden_data[i].input)
+                       sdb_strbuf_sprintf(buf, golden_data[i].input);
                check = sdb_strbuf_string(buf);
-               fail_unless(!strcmp(check, string_golden_data[i].expected),
+               fail_unless(!strcmp(check, golden_data[i].expected),
                                "sdb_strbuf_string() = '%s'; expected: '%s'",
-                               check, string_golden_data[i].expected);
+                               check, golden_data[i].expected);
        }
 }
 END_TEST
 
-static struct {
-       const char *input;
-       size_t expected;
-} len_golden_data[] = {
-       { NULL, 0 },
-       { "a", 1 },
-       { "12345", 5 },
-};
-
-START_TEST(test_sdb_strbuf_len)
+START_TEST(test_len)
 {
+       struct {
+               const char *input;
+               size_t expected;
+       } golden_data[] = {
+               { NULL, 0 },
+               { "a", 1 },
+               { "12345", 5 },
+       };
+
        size_t i;
 
-       for (i = 0; i < SDB_STATIC_ARRAY_LEN(len_golden_data); ++i) {
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                size_t check;
 
-               if (len_golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, len_golden_data[i].input);
+               if (golden_data[i].input)
+                       sdb_strbuf_sprintf(buf, golden_data[i].input);
                check = sdb_strbuf_len(buf);
-               fail_unless(check == len_golden_data[i].expected,
+               fail_unless(check == golden_data[i].expected,
                                "sdb_strbuf_len() = %zu; expected: %zu",
-                               check, len_golden_data[i].expected);
+                               check, golden_data[i].expected);
        }
 }
 END_TEST
@@ -544,17 +570,17 @@ util_strbuf_suite(void)
 
        tc = tcase_create("core");
        tcase_add_checked_fixture(tc, setup, teardown);
-       tcase_add_test(tc, test_sdb_strbuf_create);
-       tcase_add_test(tc, test_sdb_strbuf_append);
-       tcase_add_test(tc, test_sdb_strbuf_sprintf);
+       tcase_add_test(tc, test_create);
+       tcase_add_test(tc, test_append);
+       tcase_add_test(tc, test_sprintf);
        tcase_add_test(tc, test_incremental);
-       tcase_add_test(tc, test_sdb_strbuf_memcpy);
-       tcase_add_test(tc, test_sdb_strbuf_memappend);
-       tcase_add_test(tc, test_sdb_strbuf_chomp);
-       tcase_add_test(tc, test_sdb_strbuf_skip);
-       tcase_add_test(tc, test_sdb_strbuf_clear);
-       tcase_add_test(tc, test_sdb_strbuf_string);
-       tcase_add_test(tc, test_sdb_strbuf_len);
+       tcase_add_test(tc, test_memcpy);
+       tcase_add_test(tc, test_memappend);
+       tcase_add_test(tc, test_chomp);
+       tcase_add_test(tc, test_skip);
+       tcase_add_test(tc, test_clear);
+       tcase_add_test(tc, test_string);
+       tcase_add_test(tc, test_len);
        suite_add_tcase(s, tc);
 
        return s;