Code

data: Return the number of bytes that would have been returned.
[sysdb.git] / t / unit / core / data_test.c
index cf2603575bc0fa3a3ff47f5de1f2225e2d167e53..cd76dff1c458f53994c7cff1383a9c14c5dd8115 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
 #include "core/data.h"
-#include "libsysdb_test.h"
+#include "testutils.h"
 
 #include <assert.h>
 #include <check.h>
@@ -2028,6 +2032,10 @@ START_TEST(test_format)
                sdb_data_t datum;
                const char *expected;
        } golden_data[] = {
+               {
+                       { SDB_TYPE_NULL, { .integer = 0 } },
+                       "NULL",
+               },
                {
                        { SDB_TYPE_INTEGER, { .integer = 4711 } },
                        "4711",
@@ -2096,10 +2104,11 @@ START_TEST(test_format)
        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;
+               size_t check_null, check;
 
                memset(buf, (int)'A', sizeof(buf));
 
+               check_null = sdb_data_format(datum, NULL, 0, SDB_DOUBLE_QUOTED);
                check = sdb_data_format(datum, buf, sizeof(buf) - 1,
                                SDB_DOUBLE_QUOTED);
                fail_unless(check > 0,
@@ -2109,7 +2118,17 @@ START_TEST(test_format)
                                "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,
+               fail_unless(check_null == check,
+                               "sdb_data_format(type=%s, NULL) = %d; "
+                               "expected %d (matching sdb_data_format(type=%s, <buf>))",
+                               SDB_TYPE_TO_STRING(datum->type), check_null,
+                               check, SDB_TYPE_TO_STRING(datum->type));
+               fail_unless(check == strlen(golden_data[i].expected),
+                               "sdb_data_format(type=%s) = %d; expected %zu (strlen)",
+                               SDB_TYPE_TO_STRING(datum->type), check,
+                               strlen(golden_data[i].expected));
+
+               fail_unless(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);
@@ -2184,13 +2203,9 @@ START_TEST(test_parse)
 }
 END_TEST
 
-Suite *
-core_data_suite(void)
+TEST_MAIN("core::data")
 {
-       Suite *s = suite_create("core::data");
-       TCase *tc;
-
-       tc = tcase_create("core");
+       TCase *tc = tcase_create("core");
        tcase_add_test(tc, test_data);
        tcase_add_test(tc, test_cmp);
        tcase_add_test(tc, test_strcmp);
@@ -2200,10 +2215,9 @@ core_data_suite(void)
        tcase_add_test(tc, test_expr_eval);
        tcase_add_test(tc, test_format);
        tcase_add_test(tc, test_parse);
-       suite_add_tcase(s, tc);
-
-       return s;
-} /* core_data_suite */
+       ADD_TCASE(tc);
+}
+TEST_MAIN_END
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */