Code

t/unit/: Create one test binary for each *_test.c file.
[sysdb.git] / t / unit / utils / strbuf_test.c
index c6ba6e8c6115fb4723bc9df8eb29c8550300fcdf..c15c1e9cb21a9b6821d8bfe8b57ed490ea320483 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
 #include "utils/strbuf.h"
-#include "libsysdb_test.h"
+#include "testutils.h"
 
 #include <check.h>
 
@@ -60,6 +64,8 @@ START_TEST(test_null)
        sdb_strbuf_t *b = NULL;
        va_list ap;
 
+       size_t check;
+
        /* check that methods don't crash */
        sdb_strbuf_destroy(b);
        sdb_strbuf_skip(b, 0, 0);
@@ -84,6 +90,14 @@ START_TEST(test_null)
                        "sdb_strbuf_read(NULL) didn't report failure");
        fail_unless(sdb_strbuf_chomp(b) < 0,
                        "sdb_strbuf_chomp(NULL) didn't report failure");
+
+       /* check that methods return no used space */
+       check = sdb_strbuf_len(b);
+       fail_unless(check == 0,
+                       "sdb_strbuf_len(NULL) = %zi; expected: 0", check);
+       check = sdb_strbuf_cap(b);
+       fail_unless(check == 0,
+                       "sdb_strbuf_cap(NULL) = %zi; expected: 0", check);
 }
 END_TEST
 
@@ -91,7 +105,7 @@ START_TEST(test_empty)
 {
        sdb_strbuf_t *b = sdb_strbuf_create(0);
        const char *data;
-       size_t len;
+       size_t check;
 
        /* check that methods don't crash */
        sdb_strbuf_skip(b, 1, 1);
@@ -101,9 +115,12 @@ START_TEST(test_empty)
        data = sdb_strbuf_string(b);
        fail_unless(data && (*data == '\0'),
                        "sdb_strbuf_string(<empty>) = '%s'; expected: ''", data);
-       len = sdb_strbuf_len(b);
-       fail_unless(len == 0,
-                       "sdb_strbuf_len(<empty>) = %zu; expected: 0", len);
+       check = sdb_strbuf_len(b);
+       fail_unless(check == 0,
+                       "sdb_strbuf_len(<empty>) = %zu; expected: 0", check);
+       check = sdb_strbuf_cap(b);
+       fail_unless(check == 0,
+                       "sdb_strbuf_cap(<empty>) = %zu; expected: 0", check);
 
        sdb_strbuf_destroy(b);
 }
@@ -112,129 +129,167 @@ END_TEST
 START_TEST(test_create)
 {
        sdb_strbuf_t *s;
-       size_t len;
+       size_t check;
 
        s = sdb_strbuf_create(0);
        fail_unless(s != NULL,
                        "sdb_strbuf_create() = NULL; expected strbuf object");
-       len = sdb_strbuf_len(s);
-       fail_unless(len == 0,
+       check = sdb_strbuf_len(s);
+       fail_unless(check == 0,
                        "sdb_strbuf_create() created buffer with len = %zu; "
-                       "expected: 0", len);
+                       "expected: 0", check);
+       check = sdb_strbuf_cap(s);
+       fail_unless(check == 0,
+                       "sdb_strbuf_create() created buffer with cap = %zu; "
+                       "expected: 0", check);
        sdb_strbuf_destroy(s);
 
        s = sdb_strbuf_create(128);
        fail_unless(s != NULL,
                        "sdb_strbuf_create() = NULL; expected strbuf object");
-       len = sdb_strbuf_len(s);
+       check = sdb_strbuf_len(s);
        /* len still has to be 0 -- there's no content */
-       fail_unless(len == 0,
+       fail_unless(check == 0,
                        "sdb_strbuf_create() created buffer with len = %zu; "
-                       "expected: 0", len);
+                       "expected: 0", check);
+       check = sdb_strbuf_cap(s);
+       fail_unless(check == 128,
+                       "sdb_strbuf_create() created buffer with cap = %zu; "
+                       "expected: 128", check);
        sdb_strbuf_destroy(s);
 }
 END_TEST
 
 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, "%s", 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", (size_t)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_sprintf)
 {
-       ssize_t n, expected;
-       size_t len;
+       ssize_t n;
+       size_t check;
        const char *test;
 
-       n = sdb_strbuf_sprintf(buf, "1234567890");
-       fail_unless(n == 10,
-                       "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
-       len = sdb_strbuf_len(buf);
-       fail_unless(len == 10,
-                       "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
-                       "expected: 10", len);
+       const char *golden_data[] = {
+               "1234567890",
+               "ABCDE",
+               "",
+               "-",
+               "123456789012345678901234567890",
+               "--",
+       };
 
-       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);
+       size_t i;
 
-       test = sdb_strbuf_string(buf);
-       fail_unless(test[len] == '\0',
-                       "sdb_strbuf_sprintf() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "ABCDE"),
-                       "sdb_strbuf_sprintf() did not format string correctly; "
-                       "got: %s; expected: ABCDE", test);
+       sdb_strbuf_destroy(buf);
+       buf = sdb_strbuf_create(1); /* -> min_size = 1 */
 
-       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,
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               n = sdb_strbuf_sprintf(buf, "%s", 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]));
+               check = sdb_strbuf_len(buf);
+               fail_unless(check == (size_t)n,
+                               "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
+                               "expected: %zi", check, n);
+
+               test = sdb_strbuf_string(buf);
+               fail_unless(test[check] == '\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]);
+
+               check = sdb_strbuf_cap(buf);
+               /* 3 times the current len is the threshold for shrinking,
+                * but it's capped to the initial size (or 64) */
+               if (n)
+                       fail_unless(((size_t)n < check) && (check < (size_t)n * 3),
+                                       "sdb_strbuf_sprintf() did not resize the buffer "
+                                       "correctly (got size %zu; expected %zi < <size> < %d)",
+                                       check, n, n / 3);
+       }
+
+       n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", (size_t)42, 4.2);
+       fail_unless(n == 10,
+                       "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
+       check = sdb_strbuf_len(buf);
+       fail_unless(check == 10,
                        "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
-                       "expected: %zu", len, (size_t)expected);
+                       "expected: 10", check);
 
        test = sdb_strbuf_string(buf);
-       fail_unless(test[len] == '\0',
+       fail_unless(test[check] == '\0',
                        "sdb_strbuf_sprintf() did not nil-terminate the string");
-       fail_unless(!strcmp(test, "5; 0.5000"),
+       fail_unless(!strcmp(test, "42; 4.2000"),
                        "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;
+       size_t i, check;
 
        sdb_strbuf_destroy(buf);
        buf = sdb_strbuf_create(1024);
@@ -245,12 +300,22 @@ START_TEST(test_incremental)
                fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
        }
 
+       check = sdb_strbuf_cap(buf);
+       fail_unless(check == 1024,
+                       "sdb_strbuf_append() resizes the buffer too early "
+                       "(got size %zu; expected: 1024)", check);
+
        /* write another byte; this has to trigger a resize */
        n = sdb_strbuf_append(buf, ".");
        fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
 
+       check = sdb_strbuf_cap(buf);
+       /* the exact size doesn't matter but is an implementation detail */
+       fail_unless(check > 1024,
+                       "sdb_strbuf_append() did not resize the buffer");
+
        /* write more bytes; this should trigger at least one more resize but
-        * that's an implementation detail */
+        * that's an implementation detail as well */
        for (i = 0; i < 1024; ++i) {
                n = sdb_strbuf_append(buf, ".");
                fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
@@ -258,6 +323,16 @@ 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
 
@@ -301,6 +376,12 @@ START_TEST(test_memcpy)
                fail_unless(!memcmp(check, mem_golden_data[i].input,
                                        mem_golden_data[i].size),
                                "sdb_strbuf_memcpy() did not set the buffer correctly");
+
+               n = (ssize_t)sdb_strbuf_cap(buf);
+               fail_unless((size_t)n > mem_golden_data[i].size,
+                               "sdb_strbuf_memcpy() did not resize the buffer correctly "
+                               "(got size %zi; expected: > %zu)", n,
+                               mem_golden_data[i].size);
        }
 }
 END_TEST
@@ -347,6 +428,11 @@ START_TEST(test_memappend)
 
                fail_unless(check[total] == '\0',
                                "sdb_strbuf_memappend() did not nil-terminate the data");
+
+               n = (ssize_t)sdb_strbuf_cap(buf);
+               fail_unless((size_t)n > total,
+                               "sdb_strbuf_memappend() did not resize the buffer correctly "
+                               "(got size %zi; expected: > %zu)", n, total);
        }
 }
 END_TEST
@@ -372,7 +458,7 @@ START_TEST(test_chomp)
                const char *check;
 
                if (golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, golden_data[i].input);
+                       sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
 
                /* empty buffer */
                n = sdb_strbuf_chomp(buf);
@@ -434,7 +520,7 @@ START_TEST(test_skip)
                const char *check;
                size_t n;
 
-               sdb_strbuf_sprintf(buf, input);
+               sdb_strbuf_sprintf(buf, "%s", input);
                sdb_strbuf_skip(buf, golden_data[i].offset,
                                golden_data[i].n);
 
@@ -496,7 +582,7 @@ START_TEST(test_string)
                const char *check;
 
                if (golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, golden_data[i].input);
+                       sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
                check = sdb_strbuf_string(buf);
                fail_unless(!strcmp(check, golden_data[i].expected),
                                "sdb_strbuf_string() = '%s'; expected: '%s'",
@@ -522,7 +608,7 @@ START_TEST(test_len)
                size_t check;
 
                if (golden_data[i].input)
-                       sdb_strbuf_sprintf(buf, golden_data[i].input);
+                       sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
                check = sdb_strbuf_len(buf);
                fail_unless(check == golden_data[i].expected,
                                "sdb_strbuf_len() = %zu; expected: %zu",
@@ -531,16 +617,12 @@ START_TEST(test_len)
 }
 END_TEST
 
-Suite *
-util_strbuf_suite(void)
+TEST_MAIN("utils::strbuf")
 {
-       Suite *s = suite_create("utils::strbuf");
-       TCase *tc;
-
-       tc = tcase_create("empty");
+       TCase *tc = tcase_create("empty");
        tcase_add_test(tc, test_null);
        tcase_add_test(tc, test_empty);
-       suite_add_tcase(s, tc);
+       ADD_TCASE(tc);
 
        tc = tcase_create("core");
        tcase_add_checked_fixture(tc, setup, teardown);
@@ -555,10 +637,9 @@ util_strbuf_suite(void)
        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;
-} /* util_strbuf_suite */
+       ADD_TCASE(tc);
+}
+TEST_MAIN_END
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */