From: Sebastian Harl Date: Fri, 2 Jan 2015 10:51:37 +0000 (+0100) Subject: data: Let sdb_data_cmp() support all array types. X-Git-Tag: sysdb-0.7.0~91 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bd9863d23596c42cbd1b5d7a63425fce90420c3a;p=sysdb.git data: Let sdb_data_cmp() support all array types. --- diff --git a/src/core/data.c b/src/core/data.c index 503db29..2665165 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -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); diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index a848756..2b32d8b 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -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;