2 * collectd - src/daemon/meta_data_test.c
3 * Copyright (C) 2015 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
29 #include "common.h" /* for STATIC_ARRAY_SIZE */
31 #include "meta_data.h"
43 CHECK_NOT_NULL(m = meta_data_create());
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);
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);
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);
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);
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);
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);
73 /* retrieve and check all values */
74 CHECK_ZERO(meta_data_get_string(m, "string", &s));
75 EXPECT_EQ_STR("foobar", s);
78 CHECK_ZERO(meta_data_get_signed_int(m, "signed_int", &si));
79 EXPECT_EQ_INT(-1, (int)si);
81 CHECK_ZERO(meta_data_get_unsigned_int(m, "unsigned_int", &ui));
82 EXPECT_EQ_INT(1, (int)ui);
84 CHECK_ZERO(meta_data_get_double(m, "double", &d));
85 EXPECT_EQ_DOUBLE(47.11, d);
87 CHECK_ZERO(meta_data_get_boolean(m, "boolean", &b));
88 OK1(b, "b evaluates to true");
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));
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);
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);
106 CHECK_ZERO(meta_data_delete(m, "signed_int"));
107 EXPECT_EQ_INT(-2, meta_data_delete(m, "doesnt exist"));
109 meta_data_destroy(m);