Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / daemon / meta_data_test.c
1 /**
2  * collectd - src/daemon/meta_data_test.c
3  * Copyright (C) 2015       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #include "common.h" /* for STATIC_ARRAY_SIZE */
28 #include "collectd.h"
29
30 #include "meta_data.h"
31 #include "testing.h"
32
33 DEF_TEST(base) {
34   meta_data_t *m;
35
36   char *s;
37   int64_t si;
38   uint64_t ui;
39   double d;
40   _Bool b;
41
42   CHECK_NOT_NULL(m = meta_data_create());
43
44   /* all of these are absent */
45   OK(meta_data_get_string(m, "string", &s) != 0);
46   OK(meta_data_get_signed_int(m, "signed_int", &si) != 0);
47   OK(meta_data_get_unsigned_int(m, "unsigned_int", &ui) != 0);
48   OK(meta_data_get_double(m, "double", &d) != 0);
49   OK(meta_data_get_boolean(m, "boolean", &b) != 0);
50
51   /* populate structure */
52   CHECK_ZERO(meta_data_add_string(m, "string", "foobar"));
53   OK(meta_data_exists(m, "string"));
54   OK(meta_data_type(m, "string") == MD_TYPE_STRING);
55
56   CHECK_ZERO(meta_data_add_signed_int(m, "signed_int", -1));
57   OK(meta_data_exists(m, "signed_int"));
58   OK(meta_data_type(m, "signed_int") == MD_TYPE_SIGNED_INT);
59
60   CHECK_ZERO(meta_data_add_unsigned_int(m, "unsigned_int", 1));
61   OK(meta_data_exists(m, "unsigned_int"));
62   OK(meta_data_type(m, "unsigned_int") == MD_TYPE_UNSIGNED_INT);
63
64   CHECK_ZERO(meta_data_add_double(m, "double", 47.11));
65   OK(meta_data_exists(m, "double"));
66   OK(meta_data_type(m, "double") == MD_TYPE_DOUBLE);
67
68   CHECK_ZERO(meta_data_add_boolean(m, "boolean", 1));
69   OK(meta_data_exists(m, "boolean"));
70   OK(meta_data_type(m, "boolean") == MD_TYPE_BOOLEAN);
71
72   /* retrieve and check all values */
73   CHECK_ZERO(meta_data_get_string(m, "string", &s));
74   EXPECT_EQ_STR("foobar", s);
75   sfree(s);
76
77   CHECK_ZERO(meta_data_get_signed_int(m, "signed_int", &si));
78   EXPECT_EQ_INT(-1, (int)si);
79
80   CHECK_ZERO(meta_data_get_unsigned_int(m, "unsigned_int", &ui));
81   EXPECT_EQ_INT(1, (int)ui);
82
83   CHECK_ZERO(meta_data_get_double(m, "double", &d));
84   EXPECT_EQ_DOUBLE(47.11, d);
85
86   CHECK_ZERO(meta_data_get_boolean(m, "boolean", &b));
87   OK1(b, "b evaluates to true");
88
89   /* retrieving the wrong type always fails */
90   EXPECT_EQ_INT(-2, meta_data_get_boolean(m, "string", &b));
91   EXPECT_EQ_INT(-2, meta_data_get_string(m, "signed_int", &s));
92   EXPECT_EQ_INT(-2, meta_data_get_string(m, "unsigned_int", &s));
93   EXPECT_EQ_INT(-2, meta_data_get_string(m, "double", &s));
94   EXPECT_EQ_INT(-2, meta_data_get_string(m, "boolean", &s));
95
96   /* replace existing keys */
97   CHECK_ZERO(meta_data_add_signed_int(m, "string", 666));
98   OK(meta_data_type(m, "string") == MD_TYPE_SIGNED_INT);
99
100   CHECK_ZERO(meta_data_add_signed_int(m, "signed_int", 666));
101   CHECK_ZERO(meta_data_get_signed_int(m, "signed_int", &si));
102   EXPECT_EQ_INT(666, (int)si);
103
104   /* deleting keys */
105   CHECK_ZERO(meta_data_delete(m, "signed_int"));
106   EXPECT_EQ_INT(-2, meta_data_delete(m, "doesnt exist"));
107
108   meta_data_destroy(m);
109   return 0;
110 }
111
112 int main(void) {
113   RUN_TEST(base);
114
115   END_TEST;
116 }