Code

t/unit/core/timeseries_test: Add minimalistic test for timeseries.
authorSebastian Harl <sh@tokkee.org>
Wed, 7 Sep 2016 03:27:48 +0000 (23:27 -0400)
committerSebastian Harl <sh@tokkee.org>
Wed, 7 Sep 2016 03:27:48 +0000 (23:27 -0400)
t/Makefile.am
t/unit/core/store_json_test.c
t/unit/core/timeseries_test.c [new file with mode: 0644]
t/unit/testutils.c
t/unit/testutils.h

index e2cde1d429503feed3dc0c4430f170dd85350d03..e29433328bb5e30e5db13895e27890f467d481d8 100644 (file)
@@ -38,6 +38,7 @@ UNIT_TESTS = \
                unit/core/store_lookup_test \
                unit/core/store_test \
                unit/core/time_test \
+               unit/core/timeseries_test \
                unit/frontend/connection_test \
                unit/frontend/query_test \
                unit/frontend/sock_test \
@@ -91,6 +92,10 @@ unit_core_time_test_SOURCES = $(UNIT_TEST_SOURCES) unit/core/time_test.c
 unit_core_time_test_CFLAGS = $(UNIT_TEST_CFLAGS)
 unit_core_time_test_LDADD = $(UNIT_TEST_LDADD)
 
+unit_core_timeseries_test_SOURCES = $(UNIT_TEST_SOURCES) unit/core/timeseries_test.c
+unit_core_timeseries_test_CFLAGS = $(UNIT_TEST_CFLAGS)
+unit_core_timeseries_test_LDADD = $(UNIT_TEST_LDADD)
+
 unit_frontend_connection_test_SOURCES = $(UNIT_TEST_SOURCES) unit/frontend/connection_test.c
 unit_frontend_connection_test_CFLAGS = $(UNIT_TEST_CFLAGS)
 unit_frontend_connection_test_LDADD = $(UNIT_TEST_LDADD)
index a0447b639e8b5ea73de65ab8b15f4d9289c3da1a..ea33ea8416b085a3dbac9eb1b3685ba2540a62a1 100644 (file)
@@ -114,27 +114,8 @@ scan_tojson_full(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
 static void
 verify_json_output(sdb_strbuf_t *buf, const char *expected)
 {
-       const char *got = sdb_strbuf_string(buf);
-       size_t len1 = strlen(got);
-       size_t len2 = strlen(expected);
-
-       size_t i;
-       int pos = -1;
-
-       if (len1 != len2)
-               pos = (int)SDB_MIN(len1, len2);
-
-       for (i = 0; i < SDB_MIN(len1, len2); ++i) {
-               if (got[i] != expected[i]) {
-                       pos = (int)i;
-                       break;
-               }
-       }
-
-       fail_unless(pos == -1,
-                       "Serializing hosts to JSON returned unexpected result\n"
-                       "         got: %s\n              %*s\n    expected: %s",
-                       got, pos + 1, "^", expected);
+       sdb_diff_strings("Serializing hosts to JSON returned unexpected result",
+                       sdb_strbuf_string(buf), expected);
 } /* verify_json_output */
 
 struct {
diff --git a/t/unit/core/timeseries_test.c b/t/unit/core/timeseries_test.c
new file mode 100644 (file)
index 0000000..dff1b53
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SysDB - t/unit/core/timeseries_test.c
+ * Copyright (C) 2016 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
+#include "core/timeseries.h"
+#include "testutils.h"
+
+#include <check.h>
+
+#define TS "1970-01-01 00:00:00 +0000"
+#define V "0.000000"
+
+START_TEST(timeseries)
+{
+       const char * const data_names[] = {"abc", "xyz"};
+       sdb_timeseries_t *ts = sdb_timeseries_create(2, data_names, 2);
+       sdb_strbuf_t *buf = sdb_strbuf_create(0);
+       int test;
+
+       const char *expected =
+               "{\"start\": \""TS"\", \"end\": \""TS"\", \"data\": {"
+                       "\"abc\": [{\"timestamp\": \""TS"\", \"value\": \""V"\"},"
+                               "{\"timestamp\": \""TS"\", \"value\": \""V"\"}],"
+                       "\"xyz\": [{\"timestamp\": \""TS"\", \"value\": \""V"\"},"
+                               "{\"timestamp\": \""TS"\", \"value\": \""V"\"}]"
+               "}}";
+
+       fail_unless(ts != NULL,
+                       "sdb_timeseries_create(2, {\"abc\", \"xyz\"}, 3) = NULL; expected: <ts>");
+
+       test = sdb_timeseries_tojson(ts, buf);
+       fail_unless(test == 0,
+                       "sdb_timeseries_tojson(<ts>, <buf>) = %d; expected: 0", test);
+       sdb_diff_strings("sdb_timeseries_tojson(<ts>, <buf>) returned unexpected JSON",
+                       sdb_strbuf_string(buf), expected);
+
+       sdb_timeseries_destroy(ts);
+       sdb_strbuf_destroy(buf);
+}
+END_TEST
+
+TEST_MAIN("core::timeseries")
+{
+       TCase *tc = tcase_create("core");
+       tcase_add_test(tc, timeseries);
+       ADD_TCASE(tc);
+}
+TEST_MAIN_END
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
index 3b941fc4a594afaf1ee3b78232c91eadf788cbf3..114fa3ffd021a9e8cd30f61a5cefb1d99a40e187 100644 (file)
@@ -50,5 +50,29 @@ sdb_regmatches(const char *regex, const char *string)
        return status;
 } /* sdb_regmatches */
 
+void
+sdb_diff_strings(const char *desc, const char *got, const char *expected)
+{
+       size_t len1 = strlen(got);
+       size_t len2 = strlen(expected);
+
+       size_t i;
+       int pos = -1;
+
+       if (len1 != len2)
+               pos = (int)SDB_MIN(len1, len2);
+
+       for (i = 0; i < SDB_MIN(len1, len2); ++i) {
+               if (got[i] != expected[i]) {
+                       pos = (int)i;
+                       break;
+               }
+       }
+
+       fail_unless(pos == -1, "%s:\n"
+                       "         got: %s\n              %*s\n    expected: %s",
+                       desc, got, pos + 1, "^", expected);
+} /* sdb_diff_strings */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index 5e0cc853d101b06c9aca1268665cc5807517d44b..a550be2af27b6529436db16e482c83747f39ea9a 100644 (file)
 int
 sdb_regmatches(const char *regex, const char *string);
 
+/*
+ * sdb_diff_strings:
+ * Compare two strings and fail the test if there's a difference.
+ */
+void
+sdb_diff_strings(const char *desc, const char *got, const char *expected);
+
 #endif /* T_LIBSYSDB_UTILS_H */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */