6d61107d50a6bf97b285739b735edf1c2730eec5
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 */
27 #include "testing.h"
28 #include "collectd.h"
29 #include "common.h" /* for STATIC_ARRAY_SIZE */
30 #include "meta_data.h"
32 DEF_TEST(base)
33 {
34 meta_data_t *m;
36 char *s;
37 int64_t si;
38 uint64_t ui;
39 double d;
40 _Bool b;
42 CHECK_NOT_NULL (m = meta_data_create ());
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);
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);
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);
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);
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);
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);
72 /* retrieve and check all values */
73 CHECK_ZERO (meta_data_get_string (m, "string", &s));
74 STREQ ("foobar", s);
75 sfree (s);
77 CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si));
78 EXPECT_INTEQ (-1, (int) si);
80 CHECK_ZERO (meta_data_get_unsigned_int (m, "unsigned_int", &ui));
81 EXPECT_INTEQ (1, (int) ui);
83 CHECK_ZERO (meta_data_get_double (m, "double", &d));
84 DBLEQ (47.11, d);
86 CHECK_ZERO (meta_data_get_boolean (m, "boolean", &b));
87 OK1 (b, "b evaluates to true");
89 /* retrieving the wrong type always fails */
90 EXPECT_INTEQ (-2, meta_data_get_boolean (m, "string", &b));
91 EXPECT_INTEQ (-2, meta_data_get_string (m, "signed_int", &s));
92 EXPECT_INTEQ (-2, meta_data_get_string (m, "unsigned_int", &s));
93 EXPECT_INTEQ (-2, meta_data_get_string (m, "double", &s));
94 EXPECT_INTEQ (-2, meta_data_get_string (m, "boolean", &s));
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);
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_INTEQ (666, (int) si);
104 /* deleting keys */
105 CHECK_ZERO (meta_data_delete (m, "signed_int"));
106 EXPECT_INTEQ (-2, meta_data_delete (m, "doesnt exist"));
108 meta_data_destroy (m);
109 return 0;
110 }
112 int main (void)
113 {
114 RUN_TEST(base);
116 END_TEST;
117 }
119 /* vim: set sw=2 sts=2 et : */