Code

timeseries: Add data-structures and helpers to manage timeseries information.
[sysdb.git] / t / unit / core / timeseries_test.c
1 /*
2  * SysDB - t/unit/core/timeseries_test.c
3  * Copyright (C) 2016 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "core/timeseries.h"
33 #include "testutils.h"
35 #include <check.h>
37 #define TS "1970-01-01 00:00:00 +0000"
38 #define V "0.000000"
40 START_TEST(timeseries_info)
41 {
42         const char * const data_names[] = {"abc", "xyz"};
43         sdb_timeseries_info_t *ts_info = sdb_timeseries_info_create(2, data_names);
45         fail_unless(ts_info != NULL,
46                         "sdb_timeseries_info_create(2, {\"abc\", \"xyz\"}) = NULL; expected: <ts_info>");
47         sdb_timeseries_info_destroy(ts_info);
48 }
49 END_TEST
51 START_TEST(timeseries)
52 {
53         const char * const data_names[] = {"abc", "xyz"};
54         sdb_timeseries_t *ts = sdb_timeseries_create(2, data_names, 2);
55         sdb_strbuf_t *buf = sdb_strbuf_create(0);
56         int test;
58         const char *expected =
59                 "{\"start\": \""TS"\", \"end\": \""TS"\", \"data\": {"
60                         "\"abc\": [{\"timestamp\": \""TS"\", \"value\": \""V"\"},"
61                                 "{\"timestamp\": \""TS"\", \"value\": \""V"\"}],"
62                         "\"xyz\": [{\"timestamp\": \""TS"\", \"value\": \""V"\"},"
63                                 "{\"timestamp\": \""TS"\", \"value\": \""V"\"}]"
64                 "}}";
66         fail_unless(ts != NULL,
67                         "sdb_timeseries_create(2, {\"abc\", \"xyz\"}, 2) = NULL; expected: <ts>");
69         test = sdb_timeseries_tojson(ts, buf);
70         fail_unless(test == 0,
71                         "sdb_timeseries_tojson(<ts>, <buf>) = %d; expected: 0", test);
72         sdb_diff_strings("sdb_timeseries_tojson(<ts>, <buf>) returned unexpected JSON",
73                         sdb_strbuf_string(buf), expected);
75         sdb_timeseries_destroy(ts);
76         sdb_strbuf_destroy(buf);
77 }
78 END_TEST
80 TEST_MAIN("core::timeseries")
81 {
82         TCase *tc = tcase_create("core");
83         tcase_add_test(tc, timeseries_info);
84         tcase_add_test(tc, timeseries);
85         ADD_TCASE(tc);
86 }
87 TEST_MAIN_END
89 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */