Code

data: Let concat support empty arrays; add various tests for empty arrays.
authorSebastian Harl <sh@tokkee.org>
Wed, 12 Nov 2014 21:59:03 +0000 (22:59 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 12 Nov 2014 21:59:03 +0000 (22:59 +0100)
src/core/data.c
t/unit/core/data_test.c

index 88c7f8246953d58f1af76a01a04e1919eb96f8c2..3186aa2622ecbfdc3caf2fc877dc7ae890c7754f 100644 (file)
@@ -390,8 +390,6 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
        else
                return -1;
 
-       assert(s1 && s2);
-
        new = malloc(len1 + len2 + 1);
        if (! new)
                return -1;
index 956cf3108c0a5d598afcb5eb75e72f938f61c3d8..a848756a185385ae1431fb43c9119fc855bfe181 100644 (file)
@@ -405,6 +405,27 @@ START_TEST(test_cmp)
                        { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
                        1,
                },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } },
+                       { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values1), int_values1 } },
+                       },
+                       { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } },
+                       1,
+               },
                {
                        {
                                SDB_TYPE_ARRAY | SDB_TYPE_INTEGER,
@@ -438,6 +459,27 @@ START_TEST(test_cmp)
                        },
                        1,
                },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
+                       { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_STRING,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values1), string_values1 } },
+                       },
+                       { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
+                       1,
+               },
                {
                        {
                                SDB_TYPE_ARRAY | SDB_TYPE_STRING,
@@ -707,6 +749,7 @@ START_TEST(test_strcmp)
                        { SDB_TYPE_STRING, { .string = "/regex/" } },
                        0,
                },
+               /* TODO: add support for arrays */
        };
 
        size_t i;
@@ -817,6 +860,30 @@ START_TEST(test_inarray)
                { string_array3, string_array, 0 },
                { int_array2,    string_array, 0 },
                { dec_array2,    string_array, 0 },
+               {
+                       { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       int_array, 1,
+               },
+               {
+                       { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       dec_array, 0,
+               },
+               {
+                       { SDB_TYPE_DECIMAL | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       dec_array, 1,
+               },
+               {
+                       { SDB_TYPE_DECIMAL | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       int_array, 0,
+               },
+               {
+                       { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       string_array, 1,
+               },
+               {
+                       { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       dec_array, 0,
+               },
        };
 
        size_t i;
@@ -877,6 +944,18 @@ START_TEST(test_array_get)
                { string_array, 4, { -1, { .integer = 0 } } },
                { { SDB_TYPE_INTEGER, { .integer = 666 } }, 0, { -1, { .integer = 0 } } },
                { { SDB_TYPE_INTEGER, { .integer = 666 } }, 1, { -1, { .integer = 0 } } },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } },
+                       0, { -1, { .integer = 0 } },
+               },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL, { .array = { 0, NULL } } },
+                       0, { -1, { .integer = 0 } },
+               },
+               {
+                       { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
+                       0, { -1, { .integer = 0 } },
+               },
        };
 
        size_t i;
@@ -1223,6 +1302,70 @@ START_TEST(test_expr_eval)
                                } },
                        },
                },
+               {
+                       { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       {
+                               SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+               },
+               {
+                       {
+                               SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+                       { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+                       },
+               },
+               {
+                       { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       {
+                               SDB_TYPE_STRING | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_STRING | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+               },
+               {
+                       {
+                               SDB_TYPE_STRING | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+                       { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       err,
+                       err,
+                       err,
+                       err,
+                       err,
+                       {
+                               SDB_TYPE_STRING | SDB_TYPE_ARRAY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+                       },
+               },
                {
                        { SDB_TYPE_NULL, { .integer = 0 } },
                        { SDB_TYPE_NULL, { .integer = 0 } },
@@ -1765,6 +1908,10 @@ START_TEST(test_format)
                        { SDB_TYPE_REGEX, { .re = { "some regex", empty_re } } },
                        "\"/some regex/\"",
                },
+               {
+                       { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } },
+                       "[]",
+               },
                {
                        {
                                SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,