Code

data: Let sdb_data_cmp() support all array types.
authorSebastian Harl <sh@tokkee.org>
Fri, 2 Jan 2015 10:51:37 +0000 (11:51 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 2 Jan 2015 10:51:37 +0000 (11:51 +0100)
src/core/data.c
t/unit/core/data_test.c

index 503db29a36c500ce34a093f465c7ca252348a8c5..2665165a0970e98075a702bbea020e3803e35cab 100644 (file)
@@ -206,9 +206,61 @@ array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
                                return diff;
                }
        }
+       else if (type == SDB_TYPE_DATETIME) {
+               sdb_time_t *v1 = a1->data.array.values;
+               sdb_time_t *v2 = a2->data.array.values;
+
+               for (i = 0; i < len; ++i)
+                       if (v1[i] != v2[i])
+                               return SDB_CMP(v1[i], v2[i]);
+       }
+       else if (type == SDB_TYPE_BINARY) {
+               struct {
+                       size_t length;
+                       unsigned char *datum;
+               } *v1 = a1->data.array.values;
+               struct {
+                       size_t length;
+                       unsigned char *datum;
+               } *v2 = a2->data.array.values;
+
+               for (i = 0; i < len; ++i) {
+                       int diff;
+
+                       /* on a common prefix, the shorter datum sorts less */
+                       if (v1[i].length < v2[i].length) {
+                               diff = memcmp(v1[i].datum, v2[i].datum, v1[i].length);
+                               diff = diff ? diff : -1;
+                       }
+                       else if (v1[i].length > v2[i].length) {
+                               diff = memcmp(v1[i].datum, v2[i].datum, v2[i].length);
+                               diff = diff ? diff : 1;
+                       }
+                       else
+                               diff = memcmp(v1[i].datum, v2[i].datum, v1[i].length);
+
+                       if (diff)
+                               return diff;
+               }
+       }
+       else if (type == SDB_TYPE_REGEX) {
+               struct {
+                       char *raw;
+                       regex_t regex;
+               } *v1 = a1->data.array.values;
+               struct {
+                       char *raw;
+                       regex_t regex;
+               } *v2 = a2->data.array.values;
+
+               for (i = 0; i < len; ++i) {
+                       int diff = strcasecmp(v1[i].raw, v2[i].raw);
+                       if (diff)
+                               return diff;
+               }
+       }
        else {
-               /* TODO */
-               errno = ENOTSUP;
+               errno = EINVAL;
                /* but fall through to ensure stable sorting: */
        }
        return SDB_CMP(a1->data.array.length, a2->data.array.length);
index a848756a185385ae1431fb43c9119fc855bfe181..2b32d8b91a169290d0b6a3a2806d99fe72ad2de4 100644 (file)
@@ -225,10 +225,41 @@ END_TEST
 
 START_TEST(test_cmp)
 {
+       regex_t dummy_re;
        int64_t int_values1[] = { 1, 2, 3 };
        int64_t int_values2[] = { 1, 3, 2 };
+       double dec_values1[] = { 12.34, 47.11 };
+       double dec_values2[] = { 47.11, 12.34 };
        char *string_values1[] = { "a", "b", "c" };
        char *string_values2[] = { "a", "c", "b" };
+       sdb_time_t dt_values1[] = { 4711, 1234567890123456789L };
+       sdb_time_t dt_values2[] = { 1234567890123456789L, 4711 };
+       struct {
+               size_t length;
+               unsigned char *datum;
+       } bin_values1[] = {
+               { 3, (unsigned char *)"\x1\x2\x3" },
+               { 4, (unsigned char *)"\x42\x0\xa\x1b" },
+       };
+       struct {
+               size_t length;
+               unsigned char *datum;
+       } bin_values2[] = {
+               { 4, (unsigned char *)"\x42\x0\xa\x1b" },
+               { 3, (unsigned char *)"\x1\x2\x3" },
+       };
+       struct {
+               char *raw;
+               regex_t regex;
+       } re_values1[] = {
+               { "dummy regex A", dummy_re },
+       };
+       struct {
+               char *raw;
+               regex_t regex;
+       } re_values2[] = {
+               { "dummy regex B", dummy_re },
+       };
 
        struct {
                sdb_data_t d1;
@@ -459,6 +490,39 @@ START_TEST(test_cmp)
                        },
                        1,
                },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values1), dec_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values1), dec_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values1), dec_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values2), dec_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values2), dec_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dec_values1), dec_values1 } },
+                       },
+                       1,
+               },
                {
                        { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
                        { SDB_TYPE_ARRAY | SDB_TYPE_STRING, { .array = { 0, NULL } } },
@@ -513,6 +577,105 @@ START_TEST(test_cmp)
                        },
                        1,
                },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values1), dt_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values1), dt_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values1), dt_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values2), dt_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values2), dt_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_DATETIME,
+                               { .array = { SDB_STATIC_ARRAY_LEN(dt_values1), dt_values1 } },
+                       },
+                       1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values1), bin_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values1), bin_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values1), bin_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values2), bin_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values2), bin_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_BINARY,
+                               { .array = { SDB_STATIC_ARRAY_LEN(bin_values1), bin_values1 } },
+                       },
+                       1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values1), re_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values1), re_values1 } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values1), re_values1 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values2), re_values2 } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values2), re_values2 } },
+                       },
+                       {
+                               SDB_TYPE_ARRAY | SDB_TYPE_REGEX,
+                               { .array = { SDB_STATIC_ARRAY_LEN(re_values1), re_values1 } },
+                       },
+                       1,
+               },
        };
 
        size_t i;