Code

data: Added support for a "regex" data-type.
[sysdb.git] / t / unit / core / data_test.c
index 0fd61a57539ff7e322be0356666a5375add663ee..742466eec0008c599824de8c49bb37aacefc3d65 100644 (file)
 #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;
@@ -73,6 +76,22 @@ START_TEST(test_data)
        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);
@@ -98,13 +117,70 @@ START_TEST(test_data)
        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
 
@@ -270,6 +346,21 @@ START_TEST(test_cmp)
                        },
                        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,
+               },
        };
 
        size_t i;
@@ -290,6 +381,242 @@ START_TEST(test_cmp)
 }
 END_TEST
 
+START_TEST(test_strcmp)
+{
+       struct {
+               sdb_data_t d1;
+               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_INTEGER, { .integer = 4711 } },
+                       { SDB_TYPE_INTEGER, { .integer = 4711 } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_INTEGER, { .integer = 4711 } },
+                       { SDB_TYPE_INTEGER, { .integer = 47 } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_DECIMAL, { .decimal = 65535.9 } },
+                       { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
+                       { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
+                       { SDB_TYPE_DECIMAL, { .decimal = 65535.9 } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = NULL } },
+                       { SDB_TYPE_STRING, { .string = "" } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = NULL } },
+                       { SDB_TYPE_STRING, { .string = NULL } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "" } },
+                       { SDB_TYPE_STRING, { .string = NULL } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       { SDB_TYPE_STRING, { .string = "b" } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       { SDB_TYPE_STRING, { .string = "ab" } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "b" } },
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_STRING, { .string = "ab" } },
+                       { SDB_TYPE_STRING, { .string = "a" } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_DATETIME, { .datetime = 471147114711471100 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 471047114711471100 } },
+                       1,
+               },
+               {
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
+                       { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } },
+                       -1,
+               },
+               {
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
+                       0,
+               },
+               {
+                       { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } },
+                       { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
+                       1,
+               },
+               {
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0a" } },
+                       },
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0b" } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 1, (unsigned char *)"a" } },
+                       },
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0\0" } },
+                       },
+                       -1,
+               },
+               {
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0a" } },
+                       },
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0a" } },
+                       },
+                       0,
+               },
+               {
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0b" } },
+                       },
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0a" } },
+                       },
+                       1,
+               },
+               {
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 3, (unsigned char *)"a\0\0" } },
+                       },
+                       {
+                               SDB_TYPE_BINARY,
+                               { .binary = { 1, (unsigned char *)"a" } },
+                       },
+                       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_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_data_format(&golden_data[i].d1, d1_str, sizeof(d1_str),
+                                       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",
+                                       d1_str, d2_str, check, golden_data[i].expected);
+               }
+       }
+}
+END_TEST
+
 START_TEST(test_expr_eval)
 {
        struct {
@@ -421,6 +748,61 @@ START_TEST(test_expr_eval)
                                { .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 */
+                       { SDB_TYPE_INTEGER,  { .integer  = 20 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 2 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       { SDB_TYPE_DATETIME, { .datetime = 40 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+               },
+               {
+                       /* datetime * int, datetime / int, datetime % int */
+                       { SDB_TYPE_DATETIME, { .datetime = 20 } },
+                       { SDB_TYPE_INTEGER,  { .integer  = 2 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       { SDB_TYPE_DATETIME, { .datetime = 40 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 10 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 0 } },
+                       SDB_DATA_INIT,
+               },
+               {
+                       /* float * datetime */
+                       { SDB_TYPE_DECIMAL,  { .decimal  = 20.0 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 2 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       { SDB_TYPE_DATETIME, { .datetime = 40 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+               },
+               {
+                       /* datetime * float, datetime / float, datetime % float */
+                       { SDB_TYPE_DATETIME, { .datetime = 20 } },
+                       { SDB_TYPE_DECIMAL,  { .decimal  = 2.0 } },
+                       SDB_DATA_INIT,
+                       SDB_DATA_INIT,
+                       { SDB_TYPE_DATETIME, { .datetime = 40 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 10 } },
+                       { SDB_TYPE_DATETIME, { .datetime = 0 } },
+                       SDB_DATA_INIT,
+               },
        };
 
        size_t i;
@@ -488,11 +870,15 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
-                       "0x1p+16",
+                       "65536",
+               },
+               {
+                       { SDB_TYPE_DECIMAL, { .decimal = 12.3 } },
+                       "12.3",
                },
                {
                        { SDB_TYPE_STRING, { .string = NULL } },
-                       "\"NULL\"",
+                       "\"<NULL>\"",
                },
                {
                        { SDB_TYPE_STRING, { .string = "this is a test" } },
@@ -508,7 +894,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
-                       "\"\"",
+                       "\"<NULL>\"",
                },
                {
                        {
@@ -517,6 +903,10 @@ START_TEST(test_format)
                        },
                        "\"\\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;
@@ -559,18 +949,20 @@ START_TEST(test_parse)
                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;
@@ -601,6 +993,12 @@ START_TEST(test_parse)
                        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
@@ -614,6 +1012,7 @@ core_data_suite(void)
        tc = tcase_create("core");
        tcase_add_test(tc, test_data);
        tcase_add_test(tc, test_cmp);
+       tcase_add_test(tc, test_strcmp);
        tcase_add_test(tc, test_expr_eval);
        tcase_add_test(tc, test_format);
        tcase_add_test(tc, test_parse);