From: Florian Forster Date: Fri, 10 Jul 2015 08:56:58 +0000 (+0200) Subject: src/daemon/meta_data.[ch]: Add unit test. X-Git-Tag: collectd-5.6.0~642 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=2e0fdac6f329c2ddf556aa47f45156e1544332a5;p=collectd.git src/daemon/meta_data.[ch]: Add unit test. --- diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 165609f9..acc0aacb 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -41,7 +41,7 @@ AUTOMAKE_OPTIONS = subdir-objects sbin_PROGRAMS = collectd -noinst_LTLIBRARIES = libavltree.la libcommon.la libheap.la libplugin_mock.la +noinst_LTLIBRARIES = libavltree.la libcommon.la libheap.la libmetadata.la libplugin_mock.la libavltree_la_SOURCES = utils_avltree.c utils_avltree.h @@ -49,6 +49,8 @@ libcommon_la_SOURCES = common.c common.h libheap_la_SOURCES = utils_heap.c utils_heap.h +libmetadata_la_SOURCES = meta_data.c meta_data.h + libplugin_mock_la_SOURCES = plugin_mock.c utils_cache_mock.c utils_time_mock.c collectd_SOURCES = collectd.c collectd.h \ @@ -73,7 +75,7 @@ collectd_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) collectd_CFLAGS = $(AM_CFLAGS) collectd_LDFLAGS = -export-dynamic collectd_LDADD = libavltree.la libcommon.la libheap.la -lm $(COMMON_LIBS) -collectd_DEPENDENCIES = libavltree.la libcommon.la libheap.la +collectd_DEPENDENCIES = libavltree.la libcommon.la libheap.la libmetadata.la # The daemon needs to call sg_init, so we need to link it against libstatgrab, # too. -octo @@ -89,12 +91,15 @@ else collectd_LDADD += -loconfig endif -check_PROGRAMS = test_common test_utils_avltree test_utils_heap test_utils_subst -TESTS = test_common test_utils_avltree test_utils_heap test_utils_subst +check_PROGRAMS = test_common test_meta_data test_utils_avltree test_utils_heap test_utils_subst +TESTS = test_common test_meta_data test_utils_avltree test_utils_heap test_utils_subst test_common_SOURCES = common_test.c ../testing.h test_common_LDADD = libcommon.la libplugin_mock.la $(COMMON_LIBS) +test_meta_data_SOURCES = meta_data_test.c ../testing.h +test_meta_data_LDADD = libmetadata.la libplugin_mock.la $(COMMON_LIBS) + test_utils_avltree_SOURCES = utils_avltree_test.c ../testing.h test_utils_avltree_LDADD = libavltree.la $(COMMON_LIBS) diff --git a/src/daemon/meta_data_test.c b/src/daemon/meta_data_test.c new file mode 100644 index 00000000..0ce8324b --- /dev/null +++ b/src/daemon/meta_data_test.c @@ -0,0 +1,115 @@ +/** + * collectd - src/daemon/meta_data_test.c + * Copyright (C) 2015 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "testing.h" +#include "collectd.h" +#include "common.h" /* for STATIC_ARRAY_SIZE */ +#include "meta_data.h" + +DEF_TEST(base) +{ + meta_data_t *m; + + char *s; + int64_t si; + uint64_t ui; + double d; + _Bool b; + + CHECK_NOT_NULL (m = meta_data_create ()); + + /* all of these are absent */ + OK(meta_data_get_string (m, "string", &s) != 0); + OK(meta_data_get_signed_int (m, "signed_int", &si) != 0); + OK(meta_data_get_unsigned_int (m, "unsigned_int", &ui) != 0); + OK(meta_data_get_double (m, "double", &d) != 0); + OK(meta_data_get_boolean (m, "boolean", &b) != 0); + + /* populate structure */ + CHECK_ZERO (meta_data_add_string (m, "string", "foobar")); + OK(meta_data_exists (m, "string")); + OK(meta_data_type (m, "string") == MD_TYPE_STRING); + + CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", -1)); + OK(meta_data_exists (m, "signed_int")); + OK(meta_data_type (m, "signed_int") == MD_TYPE_SIGNED_INT); + + CHECK_ZERO (meta_data_add_unsigned_int (m, "unsigned_int", 1)); + OK(meta_data_exists (m, "unsigned_int")); + OK(meta_data_type (m, "unsigned_int") == MD_TYPE_UNSIGNED_INT); + + CHECK_ZERO (meta_data_add_double (m, "double", 47.11)); + OK(meta_data_exists (m, "double")); + OK(meta_data_type (m, "double") == MD_TYPE_DOUBLE); + + CHECK_ZERO (meta_data_add_boolean (m, "boolean", 1)); + OK(meta_data_exists (m, "boolean")); + OK(meta_data_type (m, "boolean") == MD_TYPE_BOOLEAN); + + /* retrieve and check all values */ + CHECK_ZERO (meta_data_get_string (m, "string", &s)); + STREQ ("foobar", s); + sfree (s); + + CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si)); + EXPECT_INTEQ (-1, (int) si); + + CHECK_ZERO (meta_data_get_unsigned_int (m, "unsigned_int", &ui)); + EXPECT_INTEQ (1, (int) ui); + + CHECK_ZERO (meta_data_get_double (m, "double", &d)); + DBLEQ (47.11, d); + + CHECK_ZERO (meta_data_get_boolean (m, "boolean", &b)); + OK1 (b, "b evaluates to true"); + + /* retrieving the wrong type always fails */ + OK (meta_data_get_boolean (m, "string", &b) != 0); + OK (meta_data_get_string (m, "signed_int", &s) != 0); + OK (meta_data_get_string (m, "unsigned_int", &s) != 0); + OK (meta_data_get_string (m, "double", &s) != 0); + OK (meta_data_get_string (m, "boolean", &s) != 0); + + /* adding existing keys fails */ + OK (meta_data_add_signed_int (m, "string", 666) != 0); + OK (meta_data_add_signed_int (m, "signed_int", 666) != 0); + + /* deleting, then adding a key works */ + CHECK_ZERO (meta_data_delete (m, "signed_int")); + CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", 42)); + + meta_data_destroy (m); + return 0; +} + +int main (void) +{ + RUN_TEST(base); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ diff --git a/src/testing.h b/src/testing.h index 4056311f..d22af4c2 100644 --- a/src/testing.h +++ b/src/testing.h @@ -1,6 +1,6 @@ /** * collectd - src/tests/macros.h - * Copyright (C) 2013 Florian octo Forster + * Copyright (C) 2013-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -54,6 +54,15 @@ static int check_count__ = 0; printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count__, #actual, expect); \ } while (0) +#define EXPECT_INTEQ(expect, actual) do { \ + if ((expect) != (actual)) {\ + printf ("not ok %i - %s incorrect: expected %d, got %d\n", \ + ++check_count__, #actual, expect, actual); \ + return (-1); \ + } \ + printf ("ok %i - %s evaluates to %d\n", ++check_count__, #actual, expect); \ +} while (0) + #define DBLEQ(expect, actual) do { \ if ((isnan (expect) && !isnan (actual)) || ((expect) != (actual))) {\ printf ("not ok %i - %s incorrect: expected %.15g, got %.15g\n", \