summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 53b6bed)
raw | patch | inline | side by side (parent: 53b6bed)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 12 Jun 2014 16:57:29 +0000 (18:57 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 12 Jun 2014 16:57:29 +0000 (18:57 +0200) |
src/core/data.c | patch | blob | history | |
src/include/core/data.h | patch | blob | history | |
t/unit/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 4e0fd779580f9dd734bf1211dc2769ca326a179f..0e8cfcb744092636be1da4398e95334d945b79ab 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
}
} /* sdb_data_free_datum */
+int
+sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2)
+{
+#define CMP_NULL(a, b) \
+ do { \
+ if (!(a) && !(b)) return 0; \
+ if (!(a)) return -1; \
+ if (!(b)) return 1; \
+ } while (0)
+
+ CMP_NULL(d1, d2);
+
+ if (d1->type != d2->type)
+ return -1;
+
+#define CMP(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
+ switch (d1->type) {
+ case SDB_TYPE_INTEGER:
+ return CMP(d1->data.integer, d2->data.integer);
+ case SDB_TYPE_DECIMAL:
+ return CMP(d1->data.decimal, d2->data.decimal);
+ case SDB_TYPE_STRING:
+ CMP_NULL(d1->data.string, d2->data.string);
+ return strcasecmp(d1->data.string, d2->data.string);
+ case SDB_TYPE_DATETIME:
+ return CMP(d1->data.datetime, d2->data.datetime);
+ case SDB_TYPE_BINARY:
+ {
+ int diff;
+
+ CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
+
+ /* on a common prefix, the shorter datum sorts less */
+ if (d1->data.binary.length < d2->data.binary.length) {
+ diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
+ d1->data.binary.length);
+ diff = diff ? diff : -1;
+ }
+ else if (d1->data.binary.length > d2->data.binary.length) {
+ diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
+ d2->data.binary.length);
+ diff = diff ? diff : 1;
+ }
+ else
+ diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
+ d1->data.binary.length);
+
+ return diff;
+ }
+ default:
+ return -1;
+ }
+#undef CMP
+#undef CMP_NULL
+} /* sdb_data_cmp */
+
size_t
sdb_data_strlen(sdb_data_t *datum)
{
index 0b4b2e7419312e10d68f9725d7db061dea9d0d14..bd102669ce2d2873a2c2f0ef3b7bb64278483d13 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
/*
* SysDB - src/include/core/data.h
- * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2012-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
void
sdb_data_free_datum(sdb_data_t *datum);
+/*
+ * sdb_data_cmp:
+ * Compare two data points. A NULL datum is considered less than any non-NULL
+ * datum. On data-type mismatch, the function always returns a negative value.
+ *
+ * Returns:
+ * - a value less than zero if d1 compares less than d2
+ * - zero if d1 compares equal to d2
+ * - a value greater than zero if d1 compares greater than d2
+ */
+int
+sdb_data_cmp(sdb_data_t *d1, sdb_data_t *d2);
+
/*
* sdb_data_strlen:
* Returns a (worst-case) estimate for the number of bytes required to format
index 432bd75165cb0dbc1bf8c69842849dbe1d790071..e0ff3a88cb9c5d684bcb66345dd3df7977b12e71 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
}
END_TEST
+START_TEST(test_cmp)
+{
+ struct {
+ sdb_data_t d1;
+ sdb_data_t d2;
+ int expected;
+ } golden_data[] = {
+ {
+ { 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 = 471147114711471000 } },
+ { 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 = 471147114711471000 } },
+ 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,
+ },
+ };
+
+ 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);
+ 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_cmp(%s, %s) = %d; expected: %d",
+ d1_str, d2_str, check, golden_data[i].expected);
+ }
+ }
+}
+END_TEST
+
START_TEST(test_format)
{
struct {
tc = tcase_create("core");
tcase_add_test(tc, test_data);
+ tcase_add_test(tc, test_cmp);
tcase_add_test(tc, test_format);
suite_add_tcase(s, tc);