From 6f1ea6365001d494aa358727ac031966dad4d426 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 19 May 2015 21:42:20 +0200 Subject: [PATCH] data: Add basic support for a boolean type. This change breaks backward compatibility of the network protocol because it changes the encoding of a datum. I felt this is still acceptable (given the current (non-)compatibility promise) and it made the order of types feel more natural ;-) --- src/core/data.c | 158 +++++++++++------- src/include/core/data.h | 4 + t/unit/core/data_test.c | 299 +++++++++++++++++++++++++++++++++++ t/unit/frontend/query_test.c | 2 +- t/unit/utils/proto_test.c | 26 +-- 5 files changed, 419 insertions(+), 70 deletions(-) diff --git a/src/core/data.c b/src/core/data.c index 4435305..444f5a1 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -55,65 +55,71 @@ /* integer, decimal, string, datetime, binary, regex */ -static int op_matrix[6][6][6] = { +static int op_matrix[6][7][7] = { /* SDB_DATA_ADD */ { - { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, - { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, /* SDB_DATA_SUB */ { - { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, - { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, /* SDB_DATA_MUL */ { - { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, /* SDB_DATA_DIV */ { - { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, - { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, /* SDB_DATA_MOD */ { - { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, - { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, /* SDB_DATA_CONCAT */ { - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, SDB_TYPE_STRING, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 }, - { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, SDB_TYPE_STRING, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, SDB_TYPE_BINARY, -1 }, + { -1, -1, -1, -1, -1, -1, -1 }, }, }; @@ -127,7 +133,8 @@ copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size) { int type = src->type & 0xff; - if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) { + if ((type == SDB_TYPE_BOOLEAN) || (type == SDB_TYPE_INTEGER) + || (type == SDB_TYPE_DECIMAL)) { if (dst != src) memcpy(dst->data.array.values, src->data.array.values, src->data.array.length * elem_size); @@ -208,7 +215,15 @@ array_cmp(const sdb_data_t *a1, const sdb_data_t *a2) len = SDB_MIN(a1->data.array.length, a2->data.array.length); - if (type == SDB_TYPE_INTEGER) { + if (type == SDB_TYPE_BOOLEAN) { + bool *v1 = a1->data.array.values; + bool *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_INTEGER) { int64_t *v1 = a1->data.array.values; int64_t *v2 = a2->data.array.values; @@ -619,7 +634,9 @@ sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2) if (d1->type != d2->type) return SDB_CMP(d1->type, d2->type); - if (d1->type == SDB_TYPE_INTEGER) + if (d1->type == SDB_TYPE_BOOLEAN) + return SDB_CMP(d1->data.boolean, d2->data.boolean); + else if (d1->type == SDB_TYPE_INTEGER) return SDB_CMP(d1->data.integer, d2->data.integer); else if (d1->type == SDB_TYPE_DECIMAL) return SDB_CMP(d1->data.decimal, d2->data.decimal); @@ -726,7 +743,13 @@ sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array) for (i = 0; i < length; ++i) { size_t j; - if (type == SDB_TYPE_INTEGER) { + if (type == SDB_TYPE_BOOLEAN) { + bool *v = array->data.array.values; + for (j = 0; j < array->data.array.length; ++j) + if (((const bool *)values)[i] == v[j]) + break; + } + else if (type == SDB_TYPE_INTEGER) { int64_t *v = array->data.array.values; for (j = 0; j < array->data.array.length; ++j) if (((const int64_t *)values)[i] == v[j]) @@ -769,7 +792,11 @@ sdb_data_array_get(const sdb_data_t *array, size_t i, sdb_data_t *value) return -1; type = array->type & 0xff; - if (type == SDB_TYPE_INTEGER) { + if (type == SDB_TYPE_BOOLEAN) { + bool *v = array->data.array.values; + tmp.data.boolean = v[i]; + } + else if (type == SDB_TYPE_INTEGER) { int64_t *v = array->data.array.values; tmp.data.integer = v[i]; } @@ -888,37 +915,36 @@ sdb_data_strlen(const sdb_data_t *datum) /* NULL */ return 4; } - else if (datum->type == SDB_TYPE_INTEGER) { + switch (datum->type) { + case SDB_TYPE_BOOLEAN: + /* true | false */ + return 5; + case SDB_TYPE_INTEGER: /* log(64) */ return 20; - } - else if (datum->type == SDB_TYPE_DECIMAL) { + case SDB_TYPE_DECIMAL: /* XXX: -d.dddddde+dd or -ddddd.dddddd */ return 42; - } - else if (datum->type == SDB_TYPE_STRING) { + case SDB_TYPE_STRING: if (! datum->data.string) return 6; /* NULL */ /* in the worst case, each character needs to be escaped */ return 2 * strlen(datum->data.string) + 2; - } - else if (datum->type == SDB_TYPE_DATETIME) { + case SDB_TYPE_DATETIME: /* "YYYY-MM-DD HH:MM:SS +zzzz" */ return 27; - } - else if (datum->type == SDB_TYPE_BINARY) { + case SDB_TYPE_BINARY: if (! datum->data.binary.datum) return 6; /* NULL */ /* "\xNN" */ return 4 * datum->data.binary.length + 2; - } - else if (datum->type == SDB_TYPE_REGEX) { + case SDB_TYPE_REGEX: if (! datum->data.re.raw) return 6; /* NULL */ /* "/.../" */ return strlen(datum->data.re.raw) + 4; } - else if (datum->type & SDB_TYPE_ARRAY) { + if (datum->type & SDB_TYPE_ARRAY) { size_t len = 2; /* [] */ size_t i; for (i = 0; i < datum->data.array.length; ++i) { @@ -948,6 +974,16 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) strncpy(buf, "NULL", buflen); ret = 4; } + else if (datum->type == SDB_TYPE_BOOLEAN) { + if (datum->data.boolean) { + strncpy(buf, "true", buflen); + ret = 4; + } + else { + strncpy(buf, "false", buflen); + ret = 5; + } + } else if (datum->type == SDB_TYPE_INTEGER) { ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer); } @@ -1080,7 +1116,15 @@ sdb_data_parse(const char *str, int type, sdb_data_t *data) } errno = 0; - if (type == SDB_TYPE_INTEGER) { + if (type == SDB_TYPE_BOOLEAN) { + if (! strcasecmp(str, "true")) + tmp.data.boolean = true; + else if (! strcasecmp(str, "false")) + tmp.data.boolean = false; + else + return -1; + } + else if (type == SDB_TYPE_INTEGER) { tmp.data.integer = strtoll(str, &endptr, 0); } else if (type == SDB_TYPE_DECIMAL) { @@ -1154,7 +1198,9 @@ size_t sdb_data_sizeof(int type) { sdb_data_t v; - if (type == SDB_TYPE_INTEGER) + if (type == SDB_TYPE_BOOLEAN) + return sizeof(v.data.boolean); + else if (type == SDB_TYPE_INTEGER) return sizeof(v.data.integer); else if (type == SDB_TYPE_DECIMAL) return sizeof(v.data.decimal); diff --git a/src/include/core/data.h b/src/include/core/data.h index e529fb9..558e166 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -43,6 +43,7 @@ extern "C" { enum { SDB_TYPE_NULL = 0, + SDB_TYPE_BOOLEAN, SDB_TYPE_INTEGER, SDB_TYPE_DECIMAL, SDB_TYPE_STRING, @@ -56,12 +57,14 @@ enum { #define SDB_TYPE_TO_STRING(t) \ (((t) == SDB_TYPE_NULL) ? "NULL" \ + : ((t) == SDB_TYPE_BOOLEAN) ? "BOOLEAN" \ : ((t) == SDB_TYPE_INTEGER) ? "INTEGER" \ : ((t) == SDB_TYPE_DECIMAL) ? "DECIMAL" \ : ((t) == SDB_TYPE_STRING) ? "STRING" \ : ((t) == SDB_TYPE_DATETIME) ? "DATETIME" \ : ((t) == SDB_TYPE_BINARY) ? "BINARY" \ : ((t) == SDB_TYPE_REGEX) ? "REGEX" \ + : ((t) == (SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN)) ? "[]BOOLEAN" \ : ((t) == (SDB_TYPE_ARRAY | SDB_TYPE_INTEGER)) ? "[]INTEGER" \ : ((t) == (SDB_TYPE_ARRAY | SDB_TYPE_DECIMAL)) ? "[]DECIMAL" \ : ((t) == (SDB_TYPE_ARRAY | SDB_TYPE_STRING)) ? "[]STRING" \ @@ -74,6 +77,7 @@ union sdb_datum; typedef union sdb_datum sdb_datum_t; union sdb_datum { + bool boolean; /* SDB_TYPE_BOOLEAN */ int64_t integer; /* SDB_TYPE_INTEGER */ double decimal; /* SDB_TYPE_DECIMAL */ char *string; /* SDB_TYPE_STRING */ diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index cd76dff..1955528 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -42,10 +42,23 @@ START_TEST(test_data) sdb_data_t d1, d2; int check; + bool bool_values[] = { true, false, false, true }; int64_t int_values[] = { 47, 11, 23 }; char *string_values[] = { "foo", "bar", "qux" "baz" }; size_t i; + d2.type = SDB_TYPE_BOOLEAN; + d2.data.boolean = true; + memset(&d1, 0, sizeof(d1)); + 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.boolean == d2.data.boolean, + "sdb_data_copy() didn't copy boolean data: got: %d; expected: %d", + d1.data.boolean, d2.data.boolean); + d2.type = SDB_TYPE_INTEGER; d2.data.integer = 4711; memset(&d1, 0, sizeof(d1)); @@ -183,6 +196,26 @@ START_TEST(test_data) "sdb_data_copy() didn't copy type; got: %i; expected: %i", d1.type, d2.type); + d2.type = SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN; + d2.data.array.length = SDB_STATIC_ARRAY_LEN(bool_values); + d2.data.array.values = bool_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(bool_values); ++i) { + bool *b1 = d1.data.array.values; + bool *b2 = d2.data.array.values; + fail_unless(b1[i] == b2[i], + "sdb_data_copy() modified boolean value %d: " + "got: %d; expected: %d", i, b1[i], b2[i]); + } + sdb_data_free_datum(&d1); + d2.type = SDB_TYPE_ARRAY | SDB_TYPE_INTEGER; d2.data.array.length = SDB_STATIC_ARRAY_LEN(int_values); d2.data.array.values = int_values; @@ -230,6 +263,8 @@ END_TEST START_TEST(test_cmp) { regex_t dummy_re; + bool bool_values1[] = { true, false, false, true }; + bool bool_values2[] = { true, false, true, false }; int64_t int_values1[] = { 1, 2, 3 }; int64_t int_values2[] = { 1, 3, 2 }; double dec_values1[] = { 12.34, 47.11 }; @@ -270,6 +305,21 @@ START_TEST(test_cmp) sdb_data_t d2; int expected; } golden_data[] = { + { + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + -1, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + 0, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + 1, + }, { { SDB_TYPE_INTEGER, { .integer = 47 } }, { SDB_TYPE_INTEGER, { .integer = 4711 } }, @@ -435,11 +485,65 @@ START_TEST(test_cmp) { SDB_TYPE_REGEX, { .re = { "b", empty_re } } }, -1, }, + { + { SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, { .array = { 0, NULL } } }, + { SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, { .array = { 0, NULL } } }, + 0, + }, { { SDB_TYPE_REGEX, { .re = { "b", empty_re } } }, { SDB_TYPE_REGEX, { .re = { "a", empty_re } } }, 1, }, + { + { SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, { .array = { 0, NULL } } }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + -1, + }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + { SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, { .array = { 0, NULL } } }, + 1, + }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + 0, + }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values2), bool_values2 } }, + }, + -1, + }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values2), bool_values2 } }, + }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values1), bool_values1 } }, + }, + 1, + }, { { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } }, { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { 0, NULL } } }, @@ -710,6 +814,21 @@ START_TEST(test_strcmp) /* same data as for the sdb_data_cmp test; in case the types match, * both functions should behave the same (except for some loss in * precision, e.g. when formatting datetime values) */ + { + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + -1, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + 0, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + 1, + }, { { SDB_TYPE_INTEGER, { .integer = 47 } }, { SDB_TYPE_INTEGER, { .integer = 4711 } }, @@ -881,6 +1000,16 @@ START_TEST(test_strcmp) 1, }, /* type mismatches */ + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_INTEGER, { .integer = 1 } }, + 1, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_STRING, { .string = "true" } }, + 0, + }, { { SDB_TYPE_INTEGER, { .integer = 123 } }, { SDB_TYPE_STRING, { .string = "123" } }, @@ -939,6 +1068,10 @@ END_TEST START_TEST(test_inarray) { + bool bool_values[] = { true, false, true }; + bool bool_values2[] = { false, true }; + bool bool_values3[] = { true, true, true }; + bool bool_values4[] = { false, false }; int64_t int_values[] = { 47, 11, 64 }; int64_t int_values2[] = { 64, 11 }; int64_t int_values3[] = { 47, 11, 42 }; @@ -949,6 +1082,22 @@ START_TEST(test_inarray) char *string_values2[] = { "qux", "bar" }; char *string_values3[] = { "foo", "bar", "qux", "baz", "bay" }; + sdb_data_t bool_array = { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } } + }; + sdb_data_t bool_array2 = { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values2), bool_values2 } } + }; + sdb_data_t bool_array3 = { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values3), bool_values3 } } + }; + sdb_data_t bool_array4 = { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values4), bool_values4 } } + }; sdb_data_t int_array = { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } } @@ -991,6 +1140,9 @@ START_TEST(test_inarray) sdb_data_t array; _Bool expected; } golden_data[] = { + { { SDB_TYPE_BOOLEAN, { .boolean = true } }, bool_array, 1 }, + { { SDB_TYPE_BOOLEAN, { .boolean = true } }, bool_array4, 0 }, + { { SDB_TYPE_BOOLEAN, { .boolean = false } }, bool_array, 1 }, { { SDB_TYPE_INTEGER, { .integer = 47 } }, int_array, 1 }, { { SDB_TYPE_INTEGER, { .integer = 11 } }, int_array, 1 }, { { SDB_TYPE_INTEGER, { .integer = 64 } }, int_array, 1 }, @@ -1011,7 +1163,17 @@ START_TEST(test_inarray) { { SDB_TYPE_STRING, { .string = "ba" } }, string_array, 0 }, { { SDB_TYPE_STRING, { .string = "abc" } }, string_array, 0 }, { { SDB_TYPE_NULL, { .integer = 0 } }, string_array, 0 }, + { bool_array, { SDB_TYPE_BOOLEAN, { .boolean = true } }, 0 }, { int_array, { SDB_TYPE_INTEGER, { .integer = 47 } }, 0 }, + { bool_array, bool_array, 1 }, + { bool_array2, bool_array, 1 }, + { bool_array, bool_array2, 1 }, + { bool_array, bool_array3, 0 }, + { bool_array, bool_array4, 0 }, + { bool_array2, bool_array3, 0 }, + { bool_array2, bool_array4, 0 }, + { bool_array3, bool_array4, 0 }, + { bool_array4, bool_array3, 0 }, { int_array, int_array, 1 }, { int_array2, int_array, 1 }, { int_array3, int_array, 0 }, @@ -1074,10 +1236,15 @@ END_TEST START_TEST(test_array_get) { + bool bool_values[] = { true, false, false }; 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 bool_array = { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } } + }; sdb_data_t int_array = { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } } @@ -1096,6 +1263,10 @@ START_TEST(test_array_get) size_t i; sdb_data_t expected; } golden_data[] = { + { bool_array, 0, { SDB_TYPE_BOOLEAN, { .boolean = true } } }, + { bool_array, 1, { SDB_TYPE_BOOLEAN, { .boolean = false } } }, + { bool_array, 2, { SDB_TYPE_BOOLEAN, { .boolean = false } } }, + { bool_array, 3, { -1, { .integer = 0 } } }, { int_array, 0, { SDB_TYPE_INTEGER, { .integer = 47 } } }, { int_array, 1, { SDB_TYPE_INTEGER, { .integer = 11 } } }, { int_array, 2, { SDB_TYPE_INTEGER, { .integer = 64 } } }, @@ -1200,6 +1371,10 @@ START_TEST(test_expr_eval) { sdb_data_t err = { -1, { .integer = 0 } }; + bool bool_values[] = { true, false, true }; + bool expected_bool_append[] = { true, false, true, true }; + bool expected_bool_prepend[] = { true, true, false, true }; + bool expected_bool_concat[] = { true, false, true, true, false, true }; int64_t int_values[] = { 47, 11, 23 }; int64_t expected_int_append[] = { 47, 11, 23, 42 }; int64_t expected_int_prepend[] = { 42, 47, 11, 23 }; @@ -1220,6 +1395,16 @@ START_TEST(test_expr_eval) sdb_data_t expected_mod; sdb_data_t expected_concat; } golden_data[] = { + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + err, + err, + err, + err, + err, + err, + }, { { SDB_TYPE_INTEGER, { .integer = 4711 } }, { SDB_TYPE_INTEGER, { .integer = 47 } }, @@ -1349,6 +1534,66 @@ START_TEST(test_expr_eval) err, err, }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } }, + }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } }, + }, + err, + err, + err, + err, + err, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { + SDB_STATIC_ARRAY_LEN(expected_bool_concat), + expected_bool_concat + } }, + }, + }, + { + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } }, + }, + { SDB_TYPE_BOOLEAN, { .boolean = true }, }, + err, + err, + err, + err, + err, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { + SDB_STATIC_ARRAY_LEN(expected_bool_append), + expected_bool_append + } }, + }, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true }, }, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } }, + }, + err, + err, + err, + err, + err, + { + SDB_TYPE_ARRAY | SDB_TYPE_BOOLEAN, + { .array = { + SDB_STATIC_ARRAY_LEN(expected_bool_prepend), + expected_bool_prepend + } }, + }, + }, { { SDB_TYPE_ARRAY | SDB_TYPE_INTEGER, @@ -1709,6 +1954,41 @@ START_TEST(test_expr_eval) err, }, /* unsupported type-mismatches */ + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_DECIMAL, { .decimal = 20.0 } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_STRING, { .string = "20" } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BINARY, { .binary = { 2, (unsigned char *)"20" } } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_BINARY, { .binary = { 3, (unsigned char *)"20" } } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_REGEX, { .re = { ".", empty_re } } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + { SDB_TYPE_REGEX + 1, { .boolean = 0 } }, + err, err, err, err, err, err, + }, + { + { SDB_TYPE_DECIMAL, { .decimal = 20.0 } }, + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + err, err, err, err, err, err, + }, { { SDB_TYPE_INTEGER, { .integer = 20 } }, { SDB_TYPE_DECIMAL, { .decimal = 20.0 } }, @@ -2025,6 +2305,7 @@ END_TEST START_TEST(test_format) { + bool bool_values[] = { false, true, false }; int64_t int_values[] = { 47, 11, 23 }; char *string_values[] = { "foo", "bar", "qux", "baz" }; @@ -2036,6 +2317,14 @@ START_TEST(test_format) { SDB_TYPE_NULL, { .integer = 0 } }, "NULL", }, + { + { SDB_TYPE_BOOLEAN, { .boolean = true } }, + "true", + }, + { + { SDB_TYPE_BOOLEAN, { .boolean = false } }, + "false", + }, { { SDB_TYPE_INTEGER, { .integer = 4711 } }, "4711", @@ -2083,6 +2372,13 @@ START_TEST(test_format) { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = { 0, NULL } } }, "[]", }, + { + { + SDB_TYPE_BOOLEAN | SDB_TYPE_ARRAY, + { .array = { SDB_STATIC_ARRAY_LEN(bool_values), bool_values } }, + }, + "[false, true, false]", + }, { { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, @@ -2150,6 +2446,9 @@ START_TEST(test_parse) sdb_data_t result; int expected; } golden_data[] = { + { "true", { SDB_TYPE_BOOLEAN, { .boolean = true } }, 0 }, + { "FALSE", { SDB_TYPE_BOOLEAN, { .boolean = false } }, 0 }, + { "yes", { SDB_TYPE_BOOLEAN, { .boolean = false } }, -1 }, { "4711", { SDB_TYPE_INTEGER, { .integer = 4711 } }, 0 }, { "0x10", { SDB_TYPE_INTEGER, { .integer = 16 } }, 0 }, { "010", { SDB_TYPE_INTEGER, { .integer = 8 } }, 0 }, diff --git a/t/unit/frontend/query_test.c b/t/unit/frontend/query_test.c index 9cb613f..861313f 100644 --- a/t/unit/frontend/query_test.c +++ b/t/unit/frontend/query_test.c @@ -282,7 +282,7 @@ fail_if_strneq(const char *got, const char *expected, size_t n, const char *fmt, * tests */ -#define VALUE "\0\0\0\3""v1" +#define VALUE "\0\0\0\4""v1" #define VALUE_LEN 7 static struct { diff --git a/t/unit/utils/proto_test.c b/t/unit/utils/proto_test.c index e7e8d44..fa6bfcf 100644 --- a/t/unit/utils/proto_test.c +++ b/t/unit/utils/proto_test.c @@ -48,20 +48,20 @@ streq(const char *s1, const char *s2) START_TEST(test_marshal_data) { -#define INT_TYPE "\0\0\0\1" -#define DECIMAL_TYPE "\0\0\0\2" -#define STRING_TYPE "\0\0\0\3" -#define DATETIME_TYPE "\0\0\0\4" -#define BINARY_TYPE "\0\0\0\5" -#define REGEX_TYPE "\0\0\0\6" +#define INT_TYPE "\0\0\0\2" +#define DECIMAL_TYPE "\0\0\0\3" +#define STRING_TYPE "\0\0\0\4" +#define DATETIME_TYPE "\0\0\0\5" +#define BINARY_TYPE "\0\0\0\6" +#define REGEX_TYPE "\0\0\0\7" #define NULL_ARRAY "\0\0\1\0" -#define INT_ARRAY "\0\0\1\1" -#define DECIMAL_ARRAY "\0\0\1\2" -#define STRING_ARRAY "\0\0\1\3" -#define DATETIME_ARRAY "\0\0\1\4" -#define BINARY_ARRAY "\0\0\1\5" -#define REGEX_ARRAY "\0\0\1\6" +#define INT_ARRAY "\0\0\1\2" +#define DECIMAL_ARRAY "\0\0\1\3" +#define STRING_ARRAY "\0\0\1\4" +#define DATETIME_ARRAY "\0\0\1\5" +#define BINARY_ARRAY "\0\0\1\6" +#define REGEX_ARRAY "\0\0\1\7" regex_t dummy_re; int64_t int_values[] = { 47, 11, 23 }; @@ -440,7 +440,7 @@ END_TEST START_TEST(test_marshal_attribute) { sdb_data_t v = { SDB_TYPE_INTEGER, { .integer = 4711 } }; -#define VAL "\0\0\0\1" "\0\0\0\0\0\0\x12\x67" +#define VAL "\0\0\0\2" "\0\0\0\0\0\0\x12\x67" struct { sdb_proto_attribute_t attr; ssize_t expected_len; -- 2.30.2