Merge branch 'collectd-5.5'
[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 "testing.h"
31 #include "meta_data.h"
32
33 DEF_TEST(base)
34 {
35   meta_data_t *m;
36
37   char *s;
38   int64_t si;
39   uint64_t ui;
40   double d;
41   _Bool b;
42
43   CHECK_NOT_NULL (m = meta_data_create ());
44
45   /* all of these are absent */
46   OK(meta_data_get_string (m, "string", &s) != 0);
47   OK(meta_data_get_signed_int (m, "signed_int", &si) != 0);
48   OK(meta_data_get_unsigned_int (m, "unsigned_int", &ui) != 0);
49   OK(meta_data_get_double (m, "double", &d) != 0);
50   OK(meta_data_get_boolean (m, "boolean", &b) != 0);
51
52   /* populate structure */
53   CHECK_ZERO (meta_data_add_string (m, "string", "foobar"));
54   OK(meta_data_exists (m, "string"));
55   OK(meta_data_type (m, "string") == MD_TYPE_STRING);
56
57   CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", -1));
58   OK(meta_data_exists (m, "signed_int"));
59   OK(meta_data_type (m, "signed_int") == MD_TYPE_SIGNED_INT);
60
61   CHECK_ZERO (meta_data_add_unsigned_int (m, "unsigned_int", 1));
62   OK(meta_data_exists (m, "unsigned_int"));
63   OK(meta_data_type (m, "unsigned_int") == MD_TYPE_UNSIGNED_INT);
64
65   CHECK_ZERO (meta_data_add_double (m, "double", 47.11));
66   OK(meta_data_exists (m, "double"));
67   OK(meta_data_type (m, "double") == MD_TYPE_DOUBLE);
68
69   CHECK_ZERO (meta_data_add_boolean (m, "boolean", 1));
70   OK(meta_data_exists (m, "boolean"));
71   OK(meta_data_type (m, "boolean") == MD_TYPE_BOOLEAN);
72
73   /* retrieve and check all values */
74   CHECK_ZERO (meta_data_get_string (m, "string", &s));
75   EXPECT_EQ_STR ("foobar", s);
76   sfree (s);
77
78   CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si));
79   EXPECT_EQ_INT (-1, (int) si);
80
81   CHECK_ZERO (meta_data_get_unsigned_int (m, "unsigned_int", &ui));
82   EXPECT_EQ_INT (1, (int) ui);
83
84   CHECK_ZERO (meta_data_get_double (m, "double", &d));
85   EXPECT_EQ_DOUBLE (47.11, d);
86
87   CHECK_ZERO (meta_data_get_boolean (m, "boolean", &b));
88   OK1 (b, "b evaluates to true");
89
90   /* retrieving the wrong type always fails */
91   EXPECT_EQ_INT (-2, meta_data_get_boolean (m, "string", &b));
92   EXPECT_EQ_INT (-2, meta_data_get_string (m, "signed_int", &s));
93   EXPECT_EQ_INT (-2, meta_data_get_string (m, "unsigned_int", &s));
94   EXPECT_EQ_INT (-2, meta_data_get_string (m, "double", &s));
95   EXPECT_EQ_INT (-2, meta_data_get_string (m, "boolean", &s));
96
97   /* replace existing keys */
98   CHECK_ZERO (meta_data_add_signed_int (m, "string", 666));
99   OK(meta_data_type (m, "string") == MD_TYPE_SIGNED_INT);
100
101   CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", 666));
102   CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si));
103   EXPECT_EQ_INT (666, (int) si);
104
105   /* deleting keys */
106   CHECK_ZERO (meta_data_delete (m, "signed_int"));
107   EXPECT_EQ_INT (-2, meta_data_delete (m, "doesnt exist"));
108
109   meta_data_destroy (m);
110   return 0;
111 }
112
113 int main (void)
114 {
115   RUN_TEST(base);
116
117   END_TEST;
118 }
119
120 /* vim: set sw=2 sts=2 et : */