Code

data: Format NULL as NULL and never quote it.
[sysdb.git] / t / unit / core / data_test.c
index 9c736d399e291d12eb61c80ae0c3a83772393d2e..e8567b1ad61c1f2cb2c1a3cf0f72874a93cdfcc9 100644 (file)
@@ -38,6 +38,10 @@ START_TEST(test_data)
        sdb_data_t d1, d2;
        int check;
 
+       int64_t int_values[] = { 47, 11, 23 };
+       char *string_values[] = { "foo", "bar", "qux" "baz" };
+       size_t i;
+
        d2.type = SDB_TYPE_INTEGER;
        d2.data.integer = 4711;
        memset(&d1, 0, sizeof(d1));
@@ -174,18 +178,58 @@ START_TEST(test_data)
        fail_unless(d1.type == d2.type,
                        "sdb_data_copy() didn't copy type; got: %i; expected: %i",
                        d1.type, d2.type);
-       fail_unless(d1.data.re.raw == d2.data.re.raw,
-                       "sdb_data_copy() didn't copy raw regex: got: %s; expected: %s",
-                       d1.data.re.raw, d2.data.re.raw);
 
+       d2.type = SDB_TYPE_ARRAY | SDB_TYPE_INTEGER;
+       d2.data.array.length = SDB_STATIC_ARRAY_LEN(int_values);
+       d2.data.array.values = int_values;
+       check = sdb_data_copy(&d1, &d2);
+       fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
+       fail_unless(d1.type == d2.type,
+                       "sdb_data_copy() didn't copy type; got: %i; expected: %i",
+                       d1.type, d2.type);
+       fail_unless(d1.data.array.values != d2.data.array.values,
+                       "sdb_data_copy() didn't copy values: got: %p; expected: %p",
+                       d1.data.array.values, d2.data.array.values);
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(int_values); ++i) {
+               int *i1 = d1.data.array.values;
+               int *i2 = d2.data.array.values;
+               fail_unless(i1[i] == i2[i],
+                               "sdb_data_copy() modified integer value %d: "
+                               "got: %d; expected: %d", i, i1[i], i2[i]);
+       }
+       sdb_data_free_datum(&d1);
+
+       d2.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
+       d2.data.array.length = SDB_STATIC_ARRAY_LEN(string_values);
+       d2.data.array.values = string_values;
+       check = sdb_data_copy(&d1, &d2);
+       fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
+       fail_unless(d1.type == d2.type,
+                       "sdb_data_copy() didn't copy type; got: %i; expected: %i",
+                       d1.type, d2.type);
+       fail_unless(d1.data.array.values != d2.data.array.values,
+                       "sdb_data_copy() didn't copy values: got: %p; expected: %p",
+                       d1.data.array.values, d2.data.array.values);
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(string_values); ++i) {
+               char **s1 = d1.data.array.values;
+               char **s2 = d2.data.array.values;
+               fail_unless(s1[i] != s2[i],
+                               "sdb_data_copy() didn't copy string value %d", i);
+               fail_unless(!strcmp(s1[i], s2[i]),
+                               "sdb_data_copy() modified string value %d: "
+                               "got: %s; expected: %s", i, s1[i], s2[i]);
+       }
        sdb_data_free_datum(&d1);
-       fail_unless(d1.data.re.raw == NULL,
-                       "sdb_data_free_datum() didn't reset raw regex");
 }
 END_TEST
 
 START_TEST(test_cmp)
 {
+       int64_t int_values1[] = { 1, 2, 3 };
+       int64_t int_values2[] = { 1, 3, 2 };
+       char *string_values1[] = { "a", "b", "c" };
+       char *string_values2[] = { "a", "c", "b" };
+
        struct {
                sdb_data_t d1;
                sdb_data_t d2;
@@ -361,6 +405,72 @@ START_TEST(test_cmp)
                        { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
                        1,
                },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values2), int_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values2), int_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values2), string_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values2), string_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       1,
+               },
        };
 
        size_t i;
@@ -617,6 +727,73 @@ START_TEST(test_strcmp)
 }
 END_TEST
 
+START_TEST(test_inarray)
+{
+       int64_t int_values[] = { 47, 11, 64 };
+       double dec_values[] = { 12.3, 47.11, 64.0 };
+       char *string_values[] = { "foo", "bar", "qux", "baz" };
+
+       sdb_data_t int_array = {
+               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } }
+       };
+       sdb_data_t dec_array = {
+               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+               { .array = { SDB_STATIC_ARRAY_LEN(dec_values), dec_values } }
+       };
+       sdb_data_t string_array = {
+               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } }
+       };
+
+       struct {
+               sdb_data_t value;
+               sdb_data_t array;
+               _Bool expected;
+       } golden_data[] = {
+               { { SDB_TYPE_INTEGER, { .integer = 47    } }, int_array,    1 },
+               { { SDB_TYPE_INTEGER, { .integer = 11    } }, int_array,    1 },
+               { { SDB_TYPE_INTEGER, { .integer = 64    } }, int_array,    1 },
+               { { SDB_TYPE_INTEGER, { .integer = 65    } }, int_array,    0 },
+               { { SDB_TYPE_NULL,    { .integer = 0     } }, int_array,    0 },
+               { int_array, { SDB_TYPE_INTEGER, { .integer = 47    } },    0 },
+               { int_array, int_array, 0 },
+               { { SDB_TYPE_DECIMAL, { .decimal = 12.3  } }, dec_array,    1 },
+               { { SDB_TYPE_DECIMAL, { .decimal = 47.11 } }, dec_array,    1 },
+               { { SDB_TYPE_DECIMAL, { .decimal = 64.0  } }, dec_array,    1 },
+               { { SDB_TYPE_DECIMAL, { .decimal = 60.0  } }, dec_array,    0 },
+               { { SDB_TYPE_INTEGER, { .integer = 64    } }, dec_array,    0 },
+               { { SDB_TYPE_NULL,    { .integer = 0     } }, dec_array,    0 },
+               { { SDB_TYPE_STRING,  { .string  = "Foo" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "FOO" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "foo" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "bar" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "qux" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "baz" } }, string_array, 1 },
+               { { SDB_TYPE_STRING,  { .string  = "ba"  } }, string_array, 0 },
+               { { SDB_TYPE_STRING,  { .string  = "abc" } }, string_array, 0 },
+               { { SDB_TYPE_NULL,    { .integer = 0     } }, string_array, 0 },
+       };
+
+       size_t i;
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               char v_str[1024] = "", a_str[1024] = "";
+               _Bool check;
+
+               sdb_data_format(&golden_data[i].value,
+                               v_str, sizeof(v_str), SDB_UNQUOTED);
+               sdb_data_format(&golden_data[i].array,
+                               a_str, sizeof(a_str), SDB_UNQUOTED);
+
+               check = sdb_data_inarray(&golden_data[i].value, &golden_data[i].array);
+               fail_unless(check == golden_data[i].expected,
+                               "sdb_data_inarray(%s, %s) = %d; expected: %d",
+                               v_str, a_str, check, golden_data[i].expected);
+       }
+}
+END_TEST
+
 START_TEST(test_parse_op)
 {
        struct {
@@ -658,6 +835,12 @@ START_TEST(test_expr_eval)
 {
        sdb_data_t err = { -1, { .integer = 0 } };
 
+       int64_t int_values[] = { 47, 11, 23 };
+       int64_t expected_int_concat[] = { 47, 11, 23, 47, 11, 23 };
+       char *string_values[] = { "foo", "bar", "qux" "baz" };
+       char *expected_string_concat[] =
+               { "foo", "bar", "qux" "baz", "foo", "bar", "qux" "baz" };
+
        struct {
                sdb_data_t d1;
                sdb_data_t d2;
@@ -797,6 +980,50 @@ START_TEST(test_expr_eval)
                        err,
                        err,
                },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = {
+                                               SDB_STATIC_ARRAY_LEN(expected_int_concat),
+                                               expected_int_concat
+                               } },
+                       },
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = {
+                                               SDB_STATIC_ARRAY_LEN(expected_string_concat),
+                                               expected_string_concat
+                               } },
+                       },
+               },
                {
                        { SDB_TYPE_NULL, { .integer = 0 } },
                        { SDB_TYPE_NULL, { .integer = 0 } },
@@ -1058,7 +1285,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_STRING, { .string = NULL } },
-                       "\"<NULL>\"",
+                       "NULL",
                },
                {
                        { SDB_TYPE_STRING, { .string = "this is a test" } },
@@ -1074,7 +1301,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
-                       "\"<NULL>\"",
+                       "NULL",
                },
                {
                        {
@@ -1193,6 +1420,7 @@ core_data_suite(void)
        tcase_add_test(tc, test_data);
        tcase_add_test(tc, test_cmp);
        tcase_add_test(tc, test_strcmp);
+       tcase_add_test(tc, test_inarray);
        tcase_add_test(tc, test_parse_op);
        tcase_add_test(tc, test_expr_eval);
        tcase_add_test(tc, test_format);