index 12944fceb147a13acfd3b7e0f073253a5cf4c89f..742466eec0008c599824de8c49bb37aacefc3d65 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
#include "core/data.h"
#include "libsysdb_test.h"
+#include <assert.h>
#include <check.h>
+static regex_t empty_re;
+
START_TEST(test_data)
{
sdb_data_t d1, d2;
fail_unless(d1.data.string == NULL,
"sdb_data_free_datum() didn't free string data");
+ d1.type = 0;
+ d2.type = SDB_TYPE_STRING;
+ d2.data.string = NULL;
+ 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.string == d2.data.string,
+ "sdb_data_copy() didn't copy string data: got: %s; expected: %s",
+ d1.data.string, d2.data.string);
+
+ sdb_data_free_datum(&d1);
+ fail_unless(d1.data.string == NULL,
+ "sdb_data_free_datum() didn't free string data");
+
d2.type = SDB_TYPE_DATETIME;
d2.data.datetime = 4711;
check = sdb_data_copy(&d1, &d2);
fail_unless(!memcmp(d1.data.binary.datum, d2.data.binary.datum,
d2.data.binary.length),
"sdb_data_copy() didn't copy binary data: got: %s; expected: %s",
- d1.data.string, d2.data.string);
+ d1.data.binary.datum, d2.data.binary.datum);
sdb_data_free_datum(&d1);
fail_unless(d1.data.binary.length == 0,
"sdb_data_free_datum() didn't reset binary datum length");
fail_unless(d1.data.binary.datum == NULL,
"sdb_data_free_datum() didn't free binary datum");
+
+ d1.type = 0;
+ d2.type = SDB_TYPE_BINARY;
+ d2.data.binary.datum = NULL;
+ d2.data.binary.length = 0;
+ 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.binary.length == d2.data.binary.length,
+ "sdb_data_copy() didn't copy length; got: %d; expected: 5d",
+ d1.data.binary.length, d2.data.binary.length);
+ fail_unless(d1.data.binary.datum == d2.data.binary.datum,
+ "sdb_data_copy() didn't copy binary data: got: %s; expected: %s",
+ d1.data.binary.datum, d2.data.binary.datum);
+
+ sdb_data_free_datum(&d1);
+ fail_unless(d1.data.binary.length == 0,
+ "sdb_data_free_datum() didn't reset binary datum length");
+ fail_unless(d1.data.binary.datum == NULL,
+ "sdb_data_free_datum() didn't free binary datum");
+
+ check = sdb_data_parse(".", SDB_TYPE_REGEX, &d2);
+ fail_unless(check == 0,
+ "INTERNAL ERROR: Failed to parse regex '.'");
+ assert(d2.type == SDB_TYPE_REGEX);
+ 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.re.raw != d2.data.re.raw,
+ "sdb_data_copy() copy string pointer");
+ fail_unless(!strcmp(d1.data.re.raw, d2.data.re.raw),
+ "sdb_data_copy() didn't copy raw regex: got: %s; expected: %s",
+ d1.data.re.raw, d2.data.re.raw);
+ sdb_data_free_datum(&d2);
+
+ sdb_data_free_datum(&d1);
+ fail_unless(d1.data.re.raw == NULL,
+ "sdb_data_free_datum() didn't reset raw regex");
+
+ d2.type = SDB_TYPE_REGEX;
+ d2.data.re.raw = NULL;
+ 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.re.raw == d2.data.re.raw,
+ "sdb_data_copy() didn't copy raw regex: got: %s; expected: %s",
+ d1.data.re.raw, d2.data.re.raw);
+
+ sdb_data_free_datum(&d1);
+ fail_unless(d1.data.re.raw == NULL,
+ "sdb_data_free_datum() didn't reset raw regex");
}
END_TEST
sdb_data_t d2;
int expected;
} golden_data[] = {
- /* 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_INTEGER, { .integer = 47 } },
{ SDB_TYPE_INTEGER, { .integer = 4711 } },
1,
},
{
- { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
+ { SDB_TYPE_DATETIME, { .datetime = 471147114711471000 } },
{ SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
-1,
},
},
{
{ SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
- { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
+ { SDB_TYPE_DATETIME, { .datetime = 471147114711471000 } },
1,
},
{
},
1,
},
- /* type mismatches */
{
- { SDB_TYPE_INTEGER, { .integer = 123 } },
- { SDB_TYPE_STRING, { .string = "123" } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
0,
},
{
- { SDB_TYPE_INTEGER, { .integer = 120 } },
- { SDB_TYPE_STRING, { .string = "123" } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "b", empty_re } } },
-1,
},
{
- { SDB_TYPE_STRING, { .string = "123" } },
- { SDB_TYPE_INTEGER, { .integer = 120 } },
- 1,
- },
- {
- { SDB_TYPE_STRING, { .string = "12.3" } },
- { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
- 0,
- },
- {
- { SDB_TYPE_STRING, { .string = "12.0" } },
- { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
- -1,
- },
- {
- { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
- { SDB_TYPE_STRING, { .string = "12.0" } },
+ { SDB_TYPE_REGEX, { .re = { "b", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
1,
},
};
size_t i;
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- int check = sdb_data_strcmp(&golden_data[i].d1, &golden_data[i].d2);
+ int check = sdb_data_cmp(&golden_data[i].d1, &golden_data[i].d2);
check = check < 0 ? -1 : check > 0 ? 1 : 0;
if (check != golden_data[i].expected) {
char d1_str[64] = "", d2_str[64] = "";
SDB_DOUBLE_QUOTED);
sdb_data_format(&golden_data[i].d2, d2_str, sizeof(d2_str),
SDB_DOUBLE_QUOTED);
- fail("sdb_data_strcmp(%s, %s) = %d; expected: %d",
+ fail("sdb_data_cmp(%s, %s) = %d; expected: %d",
d1_str, d2_str, check, golden_data[i].expected);
}
}
sdb_data_t d2;
int expected;
} golden_data[] = {
+ /* 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_INTEGER, { .integer = 47 } },
{ SDB_TYPE_INTEGER, { .integer = 4711 } },
1,
},
{
- { SDB_TYPE_DATETIME, { .datetime = 471147114711471000 } },
+ { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
{ SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
-1,
},
},
{
{ SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
- { SDB_TYPE_DATETIME, { .datetime = 471147114711471000 } },
+ { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
1,
},
{
},
1,
},
+ {
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ 0,
+ },
+ {
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "b", empty_re } } },
+ -1,
+ },
+ {
+ { SDB_TYPE_REGEX, { .re = { "b", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { "a", empty_re } } },
+ 1,
+ },
+ /* type mismatches */
+ {
+ { SDB_TYPE_INTEGER, { .integer = 123 } },
+ { SDB_TYPE_STRING, { .string = "123" } },
+ 0,
+ },
+ {
+ { SDB_TYPE_INTEGER, { .integer = 120 } },
+ { SDB_TYPE_STRING, { .string = "123" } },
+ -1,
+ },
+ {
+ { SDB_TYPE_STRING, { .string = "123" } },
+ { SDB_TYPE_INTEGER, { .integer = 120 } },
+ 1,
+ },
+ {
+ { SDB_TYPE_STRING, { .string = "12.3" } },
+ { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
+ 0,
+ },
+ {
+ { SDB_TYPE_STRING, { .string = "12.0" } },
+ { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
+ -1,
+ },
+ {
+ { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
+ { SDB_TYPE_STRING, { .string = "12.0" } },
+ 1,
+ },
+ {
+ { SDB_TYPE_REGEX, { .re = { "regex", empty_re } } },
+ { SDB_TYPE_STRING, { .string = "/regex/" } },
+ 0,
+ },
};
size_t i;
for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
- int check = sdb_data_cmp(&golden_data[i].d1, &golden_data[i].d2);
+ int check = sdb_data_strcmp(&golden_data[i].d1, &golden_data[i].d2);
check = check < 0 ? -1 : check > 0 ? 1 : 0;
if (check != golden_data[i].expected) {
char d1_str[64] = "", d2_str[64] = "";
SDB_DOUBLE_QUOTED);
sdb_data_format(&golden_data[i].d2, d2_str, sizeof(d2_str),
SDB_DOUBLE_QUOTED);
- fail("sdb_data_cmp(%s, %s) = %d; expected: %d",
+ fail("sdb_data_strcmp(%s, %s) = %d; expected: %d",
d1_str, d2_str, check, golden_data[i].expected);
}
}
{ .binary = { 6, (unsigned char *)"a\0ab\0b" } },
},
},
+ {
+ { SDB_TYPE_REGEX, { .re = { ".", empty_re } } },
+ { SDB_TYPE_REGEX, { .re = { ".", empty_re } } },
+ SDB_DATA_INIT,
+ SDB_DATA_INIT,
+ SDB_DATA_INIT,
+ SDB_DATA_INIT,
+ SDB_DATA_INIT,
+ SDB_DATA_INIT,
+ },
/* supported type-mismatches */
{
/* int * datetime */
},
"\"\\x62\\x69\\x6e\\x61\\x72\\x79\\x0\\x63\\x72\\x61\\x70\\x42\"",
},
+ {
+ { SDB_TYPE_REGEX, { .re = { "some regex", empty_re } } },
+ "\"/some regex/\"",
+ },
};
size_t i;
sdb_data_t result;
int expected;
} golden_data[] = {
- { "4711", { SDB_TYPE_INTEGER, { .integer = 4711 } }, 0 },
- { "0x10", { SDB_TYPE_INTEGER, { .integer = 16 } }, 0 },
- { "010", { SDB_TYPE_INTEGER, { .integer = 8 } }, 0 },
- { "abc", { SDB_TYPE_INTEGER, { .integer = 0 } }, -1 },
- { "1.2", { SDB_TYPE_DECIMAL, { .decimal = 1.2 } }, 0 },
- { "0x1p+16", { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } }, 0 },
- { "abc", { SDB_TYPE_DECIMAL, { .decimal = 0.0 } }, -1 },
- { "abc", { SDB_TYPE_STRING, { .string = "abc" } }, 0 },
- { ".4", { SDB_TYPE_DATETIME, { .datetime = 400000000 } }, 0 },
- { "abc", { SDB_TYPE_DATETIME, { .datetime = 0 } }, -1 },
+ { "4711", { SDB_TYPE_INTEGER, { .integer = 4711 } }, 0 },
+ { "0x10", { SDB_TYPE_INTEGER, { .integer = 16 } }, 0 },
+ { "010", { SDB_TYPE_INTEGER, { .integer = 8 } }, 0 },
+ { "abc", { SDB_TYPE_INTEGER, { .integer = 0 } }, -1 },
+ { "1.2", { SDB_TYPE_DECIMAL, { .decimal = 1.2 } }, 0 },
+ { "0x1p+16", { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } }, 0 },
+ { "abc", { SDB_TYPE_DECIMAL, { .decimal = 0.0 } }, -1 },
+ { "abc", { SDB_TYPE_STRING, { .string = "abc" } }, 0 },
+ { ".4", { SDB_TYPE_DATETIME, { .datetime = 400000000 } }, 0 },
+ { "abc", { SDB_TYPE_DATETIME, { .datetime = 0 } }, -1 },
{ "abc", { SDB_TYPE_BINARY,
{ .binary = { 3, (unsigned char *)"abc" } } }, 0 },
+ { "abc", { SDB_TYPE_REGEX, { .re = { "abc", empty_re } } }, 0 },
+ { "(|", { SDB_TYPE_REGEX, { .re = { "", empty_re } } }, -1 },
};
size_t i;
fail_unless(golden_data[i].input == (char *)result.data.binary.datum,
"sdb_data_parse(%s, %d, <d>) modified input string",
golden_data[i].input, type);
+ if (type == SDB_TYPE_REGEX) {
+ fail_unless(golden_data[i].input != result.data.re.raw,
+ "sdb_data_parse(%s, %d, <d>) copied input string",
+ golden_data[i].input, type);
+ sdb_data_free_datum(&result);
+ }
}
}
END_TEST