From 552e172997899745a55b081a3198ba1c6b85a765 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 6 Sep 2016 23:27:48 -0400 Subject: [PATCH] t/unit/core/timeseries_test: Add minimalistic test for timeseries. --- t/Makefile.am | 5 +++ t/unit/core/store_json_test.c | 23 +---------- t/unit/core/timeseries_test.c | 78 +++++++++++++++++++++++++++++++++++ t/unit/testutils.c | 24 +++++++++++ t/unit/testutils.h | 7 ++++ 5 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 t/unit/core/timeseries_test.c diff --git a/t/Makefile.am b/t/Makefile.am index e2cde1d..e294333 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -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) diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index a0447b6..ea33ea8 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -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 index 0000000..dff1b53 --- /dev/null +++ b/t/unit/core/timeseries_test.c @@ -0,0 +1,78 @@ +/* + * SysDB - t/unit/core/timeseries_test.c + * Copyright (C) 2016 Sebastian 'tokkee' Harl + * 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 + +#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: "); + + test = sdb_timeseries_tojson(ts, buf); + fail_unless(test == 0, + "sdb_timeseries_tojson(, ) = %d; expected: 0", test); + sdb_diff_strings("sdb_timeseries_tojson(, ) 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 : */ + diff --git a/t/unit/testutils.c b/t/unit/testutils.c index 3b941fc..114fa3f 100644 --- a/t/unit/testutils.c +++ b/t/unit/testutils.c @@ -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 : */ diff --git a/t/unit/testutils.h b/t/unit/testutils.h index 5e0cc85..a550be2 100644 --- a/t/unit/testutils.h +++ b/t/unit/testutils.h @@ -74,6 +74,13 @@ 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 : */ -- 2.30.2