Code

Added unit testing framework based on ‘check’.
authorSebastian Harl <sh@tokkee.org>
Fri, 23 Aug 2013 18:44:10 +0000 (20:44 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 23 Aug 2013 18:44:10 +0000 (20:44 +0200)
For now, some basic tests for utils::list have been added.

.gitignore
Makefile.am
configure.ac
src/Makefile.am
t/Makefile.am [new file with mode: 0644]
t/libsysdb_test.c [new file with mode: 0644]
t/libsysdb_test.h [new file with mode: 0644]
t/utils/llist_test.c [new file with mode: 0644]

index 847ea1bdb3080a9e8027448f3226b9649dbccfbb..19a424f090bbc794a355c151266b6e4295d5e576 100644 (file)
@@ -41,3 +41,6 @@ sysdbd.1
 sysdbd.conf.5
 sysdb.h
 
+# unit testing output
+t/libsysdb_test
+
index cfca249e2efcdffcfa47d7a30e5d9c60c82f5ab5..94f60f67f8e6d77ae92d6a953102ccaa29d9ac68 100644 (file)
@@ -4,6 +4,9 @@ SUBDIRS = src
 if BUILD_DOCUMENTATION
 SUBDIRS += doc
 endif
+if BUILD_TESTING
+SUBDIRS += t
+endif
 
 EXTRA_DIST = autogen.sh version-gen.sh
 
index 6195d20541ccc82d9c76b3f21db8c52fe3d0d192..0e4305505a3739d09a628024e509a9935204da7f 100644 (file)
@@ -184,6 +184,10 @@ if test "x$enable_strict_checks" = "xyes"; then
 fi
 AC_SUBST([STRICT_CFLAGS])
 
+dnl Testing.
+build_testing="no"
+PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [build_testing="yes"])
+
 AC_CHECK_HEADERS(libgen.h)
 
 dnl Check for dependencies.
@@ -250,9 +254,10 @@ AC_SDB_PLUGIN([cname-dns], [yes],
                [canonicalize hostnames by querying DNS])
 
 AM_CONDITIONAL([BUILD_DOCUMENTATION], test "x$build_documentation" = "xyes")
+AM_CONDITIONAL([BUILD_TESTING], test "x$build_testing" = "xyes")
 
 AC_CONFIG_FILES([Makefile doc/Makefile src/Makefile
-               src/liboconfig/Makefile])
+               src/liboconfig/Makefile t/Makefile])
 AC_OUTPUT
 
 BUILD_DATE="`date --utc '+%F %T'` (UTC)"
@@ -274,6 +279,7 @@ AC_MSG_RESULT([    xmlto / xsltproc: . . . . . $have_xmlto / $have_xsltproc])
 AC_MSG_RESULT()
 AC_MSG_RESULT([  Features:])
 AC_MSG_RESULT([    documentation:  . . . . . . $build_documentation])
+AC_MSG_RESULT([    unit testing: . . . . . . . $build_testing])
 AC_MSG_RESULT()
 AC_MSG_RESULT([  Libraries:])
 AC_MSG_RESULT([    libdbi: . . . . . . . . . . $with_libdbi])
index 3ab6bc616da82c88e4b38ebea79a5093c44b6712..3cdb58e906edbf36483dfb5c7beb81b05e6d7a30 100644 (file)
@@ -1,7 +1,8 @@
 SUBDIRS = liboconfig
 
-AM_CFLAGS = @STRICT_CFLAGS@ -Iinclude
-AM_CPPFLAGS  = -DSYSCONFDIR='"${sysconfdir}"'
+AM_CFLAGS = @STRICT_CFLAGS@
+AM_CPPFLAGS  = -Iinclude
+AM_CPPFLAGS += -DSYSCONFDIR='"${sysconfdir}"'
 AM_CPPFLAGS += -DPKGLIBDIR='"${pkglibdir}"'
 
 BUILT_SOURCES = include/sysdb.h
diff --git a/t/Makefile.am b/t/Makefile.am
new file mode 100644 (file)
index 0000000..7ea2cdd
--- /dev/null
@@ -0,0 +1,10 @@
+AM_CPPFLAGS = -I$(top_builddir)/src/include
+
+TESTS = libsysdb_test
+
+check_PROGRAMS = libsysdb_test
+libsysdb_test_SOURCES = \
+               libsysdb_test.c libsysdb_test.h \
+               utils/llist_test.c
+libsysdb_test_CFLAGS = @CHECK_CFLAGS@
+libsysdb_test_LDADD = $(top_builddir)/src/libsysdb.la @CHECK_LIBS@
diff --git a/t/libsysdb_test.c b/t/libsysdb_test.c
new file mode 100644 (file)
index 0000000..dd5ce8e
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SysDB - t/libsysdb_test.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <check.h>
+
+#include "libsysdb_test.h"
+
+int
+main(void)
+{
+       int failed = 0;
+       Suite *s;
+
+       /* t/utils/llist_test */
+       s = util_llist_suite();
+       SRunner *sr = srunner_create(s);
+       srunner_run_all(sr, CK_NORMAL);
+       failed += srunner_ntests_failed(sr);
+       srunner_free(sr);
+
+       return failed;
+} /* main */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/t/libsysdb_test.h b/t/libsysdb_test.h
new file mode 100644 (file)
index 0000000..fe9f1a8
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SysDB - t/libsysdb_test.h
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef T_LIBSYSDB_H
+#define T_LIBSYSDB_H 1
+
+#include <string.h>
+
+#include "sysdb.h"
+#include "core/object.h"
+
+/*
+ * private testing helpers
+ */
+
+/* static string object:
+ * Any such object may is of type sdb_object_t but may never be destroyed. */
+#define SSTRING_OBJ(name) { \
+       /* type = */ { sizeof(sdb_object_t), NULL, NULL }, \
+       /* ref_cnt = */ 1, /* name = */ (name) }
+
+/*
+ * test suites
+ */
+
+/* t/utils/llist_test */
+Suite *
+util_llist_suite(void);
+
+#endif /* T_LIBSYSDB_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/t/utils/llist_test.c b/t/utils/llist_test.c
new file mode 100644 (file)
index 0000000..82162c5
--- /dev/null
@@ -0,0 +1,218 @@
+/*
+ * SysDB - t/utils/llist_test.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <check.h>
+
+#include "libsysdb_test.h"
+#include "utils/llist.h"
+
+/*
+ * private data types
+ */
+
+static sdb_object_t golden_data[] = {
+       SSTRING_OBJ("abc"),
+       SSTRING_OBJ("bcd"),
+       SSTRING_OBJ("cde"),
+       SSTRING_OBJ("def"),
+       SSTRING_OBJ("efg"),
+       SSTRING_OBJ("fgh"),
+       SSTRING_OBJ("ghi")
+};
+
+static char *unused_names[] = {
+       "xyz",
+       "yza",
+       "zab"
+};
+
+static sdb_llist_t *list;
+
+static void
+setup(void)
+{
+       list = sdb_llist_create();
+} /* setup */
+
+static void
+teardown(void)
+{
+       sdb_llist_destroy(list);
+       list = NULL;
+} /* teardown */
+
+/* populate the list with the golden data in the specified order */
+static void
+populate(void)
+{
+       int i;
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               int check = sdb_llist_append(list, &golden_data[i]);
+               fail_unless(check == 0,
+                               "sdb_llist_append(%s) = %i; expected: 0",
+                               golden_data[i].name, check);
+       }
+} /* populate */
+
+START_TEST(test_append)
+{
+       int i;
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               int check = sdb_llist_append(list, &golden_data[i]);
+               fail_unless(check == 0,
+                               "sdb_llist_append(%s) = %i; expected: 0",
+                               golden_data[i].name, check);
+               fail_unless(golden_data[i].ref_cnt == 2,
+                               "sdb_llist_append(%s) did not take ownership",
+                               golden_data[i].name);
+       }
+}
+END_TEST
+
+START_TEST(test_insert)
+{
+       int i;
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               int check = sdb_llist_insert(list, &golden_data[i], 0);
+               fail_unless(check == 0,
+                               "sdb_llist_insert(%s, 0) = %i; expected: 0",
+                               golden_data[i].name, check);
+               fail_unless(golden_data[i].ref_cnt == 2,
+                               "sdb_llist_insert(%s, 0) did not take ownership",
+                               golden_data[i].name);
+       }
+}
+END_TEST
+
+START_TEST(test_insert_invalid)
+{
+       int i;
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               /* none of these operations will succeed
+                * => 1 is invalid for each case */
+               int check = sdb_llist_insert(list, &golden_data[i], 1);
+               fail_unless(check == -1,
+                               "sdb_llist_insert(%s, 1) = %i; expected: -1",
+                               golden_data[i].name, check);
+               fail_unless(golden_data[i].ref_cnt == 1,
+                               "sdb_llist_insert(%s, 1) took ownership",
+                               golden_data[i].name);
+       }
+}
+END_TEST
+
+START_TEST(test_search)
+{
+       int i;
+       populate();
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_object_t *check = sdb_llist_search_by_name(list,
+                               golden_data[i].name);
+               fail_unless(check == &golden_data[i],
+                               "sdb_llist_search_by_name(%s) = NULL; expected: %p",
+                               golden_data[i].name, &golden_data[i]);
+       }
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(unused_names); ++i) {
+               sdb_object_t *check = sdb_llist_search_by_name(list,
+                               unused_names[i]);
+               fail_unless(check == NULL,
+                               "sdb_llist_search_by_name(%s) = %p; expected: NULL",
+                               unused_names[i], check);
+       }
+}
+END_TEST
+
+START_TEST(test_shift)
+{
+       int i;
+       populate();
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_object_t *check = sdb_llist_shift(list);
+               fail_unless(check == &golden_data[i],
+                               "sdb_llist_shift() = NULL; expected: %p",
+                               &golden_data[i]);
+               fail_unless(check->ref_cnt == 2,
+                               "sdb_llist_shift() changed reference count; got: %i; "
+                               "expected: 2", check->ref_cnt);
+       }
+
+       /* must be empty now */
+       fail_unless(sdb_llist_shift(list) == NULL,
+                       "sdb_llist_shift() returned value; expected: NULL");
+}
+END_TEST
+
+START_TEST(test_iter)
+{
+       sdb_llist_iter_t *iter;
+       int i;
+
+       populate();
+
+       iter = sdb_llist_get_iter(list);
+       fail_unless(iter != NULL,
+                       "sdb_llist_get_iter() did not return an iterator");
+
+       for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               sdb_object_t *check;
+               fail_unless(sdb_llist_iter_has_next(iter),
+                               "sdb_llist_iter_has_next() = FALSE; expected: TRUE");
+               check = sdb_llist_iter_get_next(iter);
+               fail_unless(check == &golden_data[i],
+                               "sdb_llist_iter_get_next() = %p; expected: %p",
+                               check, &golden_data[i]);
+       }
+
+       fail_unless(!sdb_llist_iter_has_next(iter),
+                       "sdb_llist_iter_has_next() = TRUE; expected: FALSE");
+       fail_unless(sdb_llist_iter_get_next(iter) == NULL,
+                       "sdb_llist_iter_get_next() returned value; expected: NULL");
+       sdb_llist_iter_destroy(iter);
+}
+END_TEST
+
+Suite *
+util_llist_suite(void)
+{
+       Suite *s = suite_create("utils::llist");
+
+       TCase *tc_core = tcase_create("core");
+       tcase_add_checked_fixture(tc_core, setup, teardown);
+       tcase_add_test(tc_core, test_append);
+       tcase_add_test(tc_core, test_insert);
+       tcase_add_test(tc_core, test_insert_invalid);
+       tcase_add_test(tc_core, test_search);
+       tcase_add_test(tc_core, test_shift);
+       tcase_add_test(tc_core, test_iter);
+       suite_add_tcase(s, tc_core);
+
+       return s;
+} /* util_llist_suite */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+