From da73635c64b8634742de3e74afed482937e7e496 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 14 Feb 2011 01:26:23 +0000 Subject: [PATCH] Add an optional cppunit dependency for unit tests. --- Makefile.am | 2 +- configure.ac | 10 +++++++++ test/Makefile.am | 18 ++++++++++++++++ test/lol-test.cpp | 25 ++++++++++++++++++++++ test/matrix.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 test/Makefile.am create mode 100644 test/lol-test.cpp create mode 100644 test/matrix.cpp diff --git a/Makefile.am b/Makefile.am index 17c69854..5a518eb8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ -SUBDIRS = src deushax monsterz tools art gfx +SUBDIRS = src test deushax monsterz tools art gfx DIST_SUBDIRS = $(SUBDIRS) maps EXTRA_DIST = bootstrap build-linux build-mingw diff --git a/configure.ac b/configure.ac index 1e36d672..0ac4c600 100644 --- a/configure.ac +++ b/configure.ac @@ -44,6 +44,9 @@ AC_ARG_ENABLE(debug, AC_ARG_ENABLE(release, [ --enable-release build final release of the game (default no)]) +AC_ARG_ENABLE(cppunit, + [ --enable-cppunit use cppunit for unit tests (autodetected)]) + AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h getopt.h) if test "${enable_debug}" = "yes"; then @@ -114,6 +117,12 @@ if test "${ac_cv_my_have_gtkgl}" != "no"; then fi AM_CONDITIONAL(USE_GTKGL, test "${ac_cv_my_have_gtkgl}" != "no") + +dnl Use cppunit for unit tests? +PKG_CHECK_MODULES(CPPUNIT, cppunit, [CPPUNIT="yes"], [CPPUNIT="no"]) +AM_CONDITIONAL(USE_CPPUNIT, test "$CPPUNIT" = "yes") + + # How to use the Lol Engine inside this tree LOL_CFLAGS="$LOL_CFLAGS -I \$(top_srcdir)/src $SDL_CFLAGS" LOL_LIBS="$LOL_LIBS $SDL_LIBS" @@ -129,6 +138,7 @@ AC_SUBST(LOL_LIBS) AC_CONFIG_FILES([ Makefile src/Makefile + test/Makefile monsterz/Makefile deushax/Makefile tools/Makefile diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 00000000..5d230494 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,18 @@ + +AM_CPPFLAGS = -I$(top_srcdir)/src + +if USE_CPPUNIT +cppunit_tests = lol-test +endif + +noinst_PROGRAMS = $(cppunit_tests) + +TESTS = $(cppunit_tests) + +lol_test_SOURCES = lol-test.cpp matrix.cpp +lol_test_CXXFLAGS = $(CPPUNIT_CFLAGS) +lol_test_CPPFLAGS = @LOL_CFLAGS@ @PIPI_CFLAGS@ +lol_test_LDADD = $(CPPUNIT_LIBS) +lol_test_LDFLAGS = $(top_builddir)/src/liblol.a @LOL_LIBS@ @PIPI_LIBS@ +lol_test_DEPENDENCIES = $(top_builddir)/src/liblol.a + diff --git a/test/lol-test.cpp b/test/lol-test.cpp new file mode 100644 index 00000000..a7984764 --- /dev/null +++ b/test/lol-test.cpp @@ -0,0 +1,25 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +int main(int argc, char *argv[]) +{ + CppUnit::TextTestRunner runner; + runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); + + return !runner.run(); +} + diff --git a/test/matrix.cpp b/test/matrix.cpp new file mode 100644 index 00000000..4cb33587 --- /dev/null +++ b/test/matrix.cpp @@ -0,0 +1,53 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include + +#include "core.h" + +class MatrixTest : public CppUnit::TestCase +{ + CPPUNIT_TEST_SUITE(MatrixTest); + CPPUNIT_TEST(test_mat_mul); + CPPUNIT_TEST_SUITE_END(); + +public: + MatrixTest() : CppUnit::TestCase("Matrix Test") {} + + void setUp() {} + + void tearDown() {} + + void test_mat_mul() + { + float4 v0(1.0f, 0.0f, 0.0f, 0.0f); + float4 v1(0.0f, 1.0f, 0.0f, 0.0f); + float4 v2(0.0f, 0.0f, 1.0f, 0.0f); + float4 v3(0.0f, 0.0f, 0.0f, 1.0f); + float4x4 m0(v0, v1, v2, v3); + float4x4 m1(v0, v1, v2, v3); + float4x4 m2 = m0 * m1; + + CPPUNIT_ASSERT(m2[0][0] == 1.0f); + CPPUNIT_ASSERT(m2[1][1] == 1.0f); + CPPUNIT_ASSERT(m2[2][2] == 1.0f); + CPPUNIT_ASSERT(m2[3][3] == 1.0f); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest); +