summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 24dce6d)
raw | patch | inline | side by side (parent: 24dce6d)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 19 May 2015 19:42:20 +0000 (21:42 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 19 May 2015 19:42:20 +0000 (21:42 +0200) |
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 ;-)
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 ;-)
diff --git a/src/core/data.c b/src/core/data.c
index 44353057b5df70bcb8db19da6600166d4786500d..444f5a1c91053d7f30665603fccaea394281d33e 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
/* 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 },
},
};
{
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);
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;
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);
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])
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];
}
/* 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) {
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);
}
}
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) {
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);
index e529fb9ceec24749aca3efdf8841bbc2dd073e23..558e1668272e327f4719ac041c9faa28af8a80e7 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
enum {
SDB_TYPE_NULL = 0,
+ SDB_TYPE_BOOLEAN,
SDB_TYPE_INTEGER,
SDB_TYPE_DECIMAL,
SDB_TYPE_STRING,
#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" \
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 */
index cd76dff1c458f53994c7cff1383a9c14c5dd8115..1955528e919a8c753b1c1c99842a15296890b716 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
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));
"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;
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 };
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 } },
{ 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 } } },
/* 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 } },
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" } },
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 };
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 } }
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 },
{ { 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 },
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 } }
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 } } },
{
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 };
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 } },
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,
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 } },
START_TEST(test_format)
{
+ bool bool_values[] = { false, true, false };
int64_t int_values[] = { 47, 11, 23 };
char *string_values[] = { "foo", "bar", "qux", "baz" };
{ SDB_TYPE_NULL, { .integer = 0 } },
"NULL",
},
+ {
+ { SDB_TYPE_BOOLEAN, { .boolean = true } },
+ "true",
+ },
+ {
+ { SDB_TYPE_BOOLEAN, { .boolean = false } },
+ "false",
+ },
{
{ SDB_TYPE_INTEGER, { .integer = 4711 } },
"4711",
{ 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,
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 },
index 9cb613fa7dcb58a355e6935f04f820bfeb11b1cd..861313f5363d9094897b83f967f5782f9ae772f2 100644 (file)
@@ -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 {
index e7e8d44cca4730959a5cccbf720e3298d5adc345..fa6bfcfb7e133ce705ce8e8ab13e7ea37308ac7b 100644 (file)
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 };
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;