Add test infrastructure
authorDavid Herrmann <dh.herrmann@gmail.com>
Mon, 19 Aug 2013 21:22:51 +0000 (23:22 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Mon, 19 Aug 2013 21:22:51 +0000 (23:22 +0200)
Add "make check" test suite and some initial hashtable dummy tests. More
comprehensive tests can be added later.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
.gitignore
Makefile.am
configure.ac
test/test_common.h [new file with mode: 0644]
test/test_hashtable.c [new file with mode: 0644]

index cc709e8..0c55793 100644 (file)
@@ -1,8 +1,10 @@
 *.la
 *.lo
+*.log
 *.o
 *.swp
 *.tar.xz
+*.trs
 .deps/
 .dirstamp
 .libs/
@@ -20,3 +22,5 @@ docs/libtsm.pc
 libtool
 m4/
 stamp-h1
+test-suite.log
+test_hashtable
index d81d4e7..d5bc322 100644 (file)
@@ -38,6 +38,7 @@ pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA =
 TPHONY =
 
+TESTS =
 check_PROGRAMS =
 lib_LTLIBRARIES =
 noinst_LTLIBRARIES =
@@ -143,6 +144,31 @@ libtsm_la_CPPFLAGS += $(XKBCOMMON_CFLAGS)
 endif
 
 #
+# Tests
+#
+
+if BUILD_HAVE_CHECK
+check_PROGRAMS += test_hashtable
+TESTS += test_hashtable
+endif
+
+test_sources = \
+       test/test_common.h
+test_libs = \
+       libshl.la \
+       $(CHECK_LIBS)
+test_cflags = \
+       $(AM_CPPFLAGS) \
+       $(CHECK_CFLAGS)
+test_lflags = \
+       $(AM_LDFLAGS)
+
+test_hashtable_SOURCES = test/test_hashtable.c $(test_sources)
+test_hashtable_CPPFLAGS = $(test_cflags)
+test_hashtable_LDADD = $(test_libs)
+test_hashtable_LDFLAGS = $(test_lflags)
+
+#
 # Phony targets
 #
 
index 16e3711..ad11deb 100644 (file)
@@ -61,6 +61,12 @@ if test "x$have_xkbcommon" = "xyes" ; then
 fi
 AM_CONDITIONAL([BUILD_HAVE_XKBCOMMON], [test "x$have_xkbcommon" = "xyes"])
 
+PKG_CHECK_MODULES([CHECK], [check],
+                  [have_check=yes], [have_check=no])
+AC_SUBST(CHECK_CFLAGS)
+AC_SUBST(CHECK_LIBS)
+AM_CONDITIONAL([BUILD_HAVE_CHECK], [test "x$have_check" = "xyes"])
+
 #
 # Parse arguments
 # This parses all arguments that are given via "--enable-XY" or "--with-XY" and
@@ -128,5 +134,6 @@ AC_MSG_NOTICE([Build configuration:
   Miscellaneous Options:
                 debug: $enable_debug
         optimizations: $enable_optimizations
+       building tests: $have_check
 
         Run "${MAKE-make}" to start compilation process])
diff --git a/test/test_common.h b/test/test_common.h
new file mode 100644 (file)
index 0000000..c9939bb
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * TSM - Test Helper
+ *
+ * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * Test Helper
+ * This header includes all kinds of helpers for testing. It tries to include
+ * everything required and provides simple macros to avoid duplicating code in
+ * each test. We try to keep tests as small as possible and move everything that
+ * might be common here.
+ *
+ * We avoid sticking to our usual coding conventions (including headers in
+ * source files, etc. ..) and instead make this the most convenient we can.
+ */
+
+#ifndef TEST_COMMON_H
+#define TEST_COMMON_H
+
+#include <check.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include "libtsm.h"
+#include "shl_hashtable.h"
+
+/* lower address-space is protected from user-allocation, so this is invalid */
+#define TEST_INVALID_PTR ((void*)0x10)
+
+#define TEST_DEFINE_CASE(_name)                                        \
+       static TCase *test_create_case_##_name(void)            \
+       {                                                       \
+               TCase *tc;                                      \
+                                                               \
+               tc = tcase_create(#_name);                      \
+
+#define TEST(_name) tcase_add_test(tc, _name);
+
+#define TEST_END_CASE                                          \
+               return tc;                                      \
+       }                                                       \
+
+#define TEST_END NULL
+
+#define TEST_CASE(_name) test_create_case_##_name
+
+static inline Suite *test_create_suite(const char *name, ...)
+{
+       Suite *s;
+       va_list list;
+       TCase *(*fn)(void);
+
+       s = suite_create(name);
+
+       va_start(list, name);
+       while ((fn = va_arg(list, TCase *(*)(void))))
+               suite_add_tcase(s, fn());
+       va_end(list);
+
+       return s;
+}
+
+#define TEST_SUITE(_name, ...) test_create_suite((#_name), ##__VA_ARGS__)
+
+static inline int test_run_suite(Suite *s)
+{
+       int ret;
+       SRunner *sr;
+
+       sr = srunner_create(s);
+       srunner_run_all(sr, CK_NORMAL);
+       ret = srunner_ntests_failed(sr);
+       srunner_free(sr);
+
+       return ret;
+}
+
+#define TEST_DEFINE(_suite) \
+       int main(int argc, char **argv) \
+       { \
+               return test_run_suite(_suite); \
+       }
+
+#endif /* TEST_COMMON_H */
diff --git a/test/test_hashtable.c b/test/test_hashtable.c
new file mode 100644 (file)
index 0000000..83bf34f
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * TSM - Hashtable Tests
+ *
+ * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
+ *
+ * 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.
+ */
+
+/*
+ * Hashtable Tests
+ * Stress tests for the internal hashtable implementation.
+ */
+
+#include "test_common.h"
+
+START_TEST(test_hashtable_setup_valid)
+{
+       struct shl_hashtable *t = NULL;
+       int r;
+
+       r = shl_hashtable_new(&t, shl_direct_hash, shl_direct_equal,
+                             NULL, NULL);
+       ck_assert(r == 0);
+       ck_assert(t != NULL);
+
+       shl_hashtable_free(t);
+}
+END_TEST
+
+START_TEST(test_hashtable_setup_invalid)
+{
+       struct shl_hashtable *t = TEST_INVALID_PTR;
+       int r;
+
+       r = shl_hashtable_new(NULL, shl_direct_hash, shl_direct_equal,
+                             NULL, NULL);
+       ck_assert(r != 0);
+       ck_assert(t == TEST_INVALID_PTR);
+
+       r = shl_hashtable_new(&t, NULL, shl_direct_equal,
+                             NULL, NULL);
+       ck_assert(r != 0);
+       ck_assert(t == TEST_INVALID_PTR);
+
+       r = shl_hashtable_new(&t, shl_direct_hash, NULL,
+                             NULL, NULL);
+       ck_assert(r != 0);
+       ck_assert(t == TEST_INVALID_PTR);
+
+       r = shl_hashtable_new(&t, NULL, NULL,
+                             NULL, NULL);
+       ck_assert(r != 0);
+       ck_assert(t == TEST_INVALID_PTR);
+
+       r = shl_hashtable_new(NULL, NULL, NULL,
+                             NULL, NULL);
+       ck_assert(r != 0);
+       ck_assert(t == TEST_INVALID_PTR);
+}
+END_TEST
+
+TEST_DEFINE_CASE(setup)
+       TEST(test_hashtable_setup_valid)
+       TEST(test_hashtable_setup_invalid)
+TEST_END_CASE
+
+START_TEST(test_hashtable_add_1)
+{
+}
+END_TEST
+
+TEST_DEFINE_CASE(add)
+       TEST(test_hashtable_add_1)
+TEST_END_CASE
+
+TEST_DEFINE(
+       TEST_SUITE(hashtable,
+               TEST_CASE(setup),
+               TEST_CASE(add),
+               TEST_END
+       )
+)