From bdac0e75964b12b5e2a9ee5838ffbcf73e799694 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 28 May 2013 21:23:06 +0200 Subject: [PATCH] utils_heap_test: Add some tests for the heap structure. --- src/Makefile.am | 10 ++++- src/tests/utils_heap_test.c | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/tests/utils_heap_test.c diff --git a/src/Makefile.am b/src/Makefile.am index cba2eb20..9d3379dd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1565,7 +1565,7 @@ uninstall-hook: rm -f $(DESTDIR)$(sysconfdir)/collectd.conf rm -f $(DESTDIR)$(pkgdatadir)/postgresql_default.conf; -check_PROGRAMS = common_test utils_avltree_test utils_vl_lookup_test +check_PROGRAMS = common_test utils_avltree_test utils_heap_test utils_vl_lookup_test common_test_SOURCES = tests/common_test.c \ common.h common.c \ @@ -1582,6 +1582,12 @@ utils_avltree_test_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) utils_avltree_test_LDFLAGS = -export-dynamic utils_avltree_test_LDADD = +utils_heap_test_SOURCES = tests/utils_heap_test.c \ + utils_heap.c utils_heap.h +utils_heap_test_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL) +utils_heap_test_LDFLAGS = -export-dynamic +utils_heap_test_LDADD = + utils_vl_lookup_test_SOURCES = tests/utils_vl_lookup_test.c \ utils_vl_lookup.h utils_vl_lookup.c \ utils_avltree.c utils_avltree.h \ @@ -1594,4 +1600,4 @@ utils_vl_lookup_test_CFLAGS = $(AM_CFLAGS) utils_vl_lookup_test_LDFLAGS = -export-dynamic utils_vl_lookup_test_LDADD = -TESTS = common_test utils_avltree_test utils_vl_lookup_test +TESTS = common_test utils_avltree_test utils_heap_test utils_vl_lookup_test diff --git a/src/tests/utils_heap_test.c b/src/tests/utils_heap_test.c new file mode 100644 index 00000000..125eb8b4 --- /dev/null +++ b/src/tests/utils_heap_test.c @@ -0,0 +1,86 @@ +/** + * collectd - src/tests/utils_heap_test.c + * + * Copyright (C) 2013 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 "collectd.h" +#include "tests/macros.h" +#include "utils_heap.h" + +static int compare (void const *v0, void const *v1) +{ + int const *i0 = v0; + int const *i1 = v1; + + if ((*i0) < (*i1)) + return -1; + else if ((*i0) > (*i1)) + return 1; + else + return 0; +} + +DEF_TEST(simple) +{ + int values[] = { 9, 5, 6, 1, 3, 4, 0, 8, 2, 7 }; + int i; + c_heap_t *h; + + CHECK_NOT_NULL(h = c_heap_create (compare)); + for (i = 0; i < 10; i++) + CHECK_ZERO(c_heap_insert (h, &values[i])); + + for (i = 0; i < 5; i++) + { + int *ret = NULL; + CHECK_NOT_NULL(ret = c_heap_get_root(h)); + OK(*ret == i); + } + + CHECK_ZERO(c_heap_insert (h, &values[6] /* = 0 */)); + CHECK_ZERO(c_heap_insert (h, &values[3] /* = 1 */)); + CHECK_ZERO(c_heap_insert (h, &values[8] /* = 2 */)); + CHECK_ZERO(c_heap_insert (h, &values[4] /* = 3 */)); + CHECK_ZERO(c_heap_insert (h, &values[5] /* = 4 */)); + + for (i = 0; i < 10; i++) + { + int *ret = NULL; + CHECK_NOT_NULL(ret = c_heap_get_root(h)); + OK(*ret == i); + } + + c_heap_destroy(h); + return (0); +} + +int main (void) +{ + RUN_TEST(simple); + + END_TEST; +} + +/* vim: set sw=2 sts=2 et : */ -- 2.30.2