From 86d03eb13789c528f45ce67f253e976c378ac54f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 19 Apr 2008 14:07:54 +0000 Subject: [PATCH] * Use CppUnit for C and C++ unit tests. Sorry, Jylam. --- configure.ac | 7 +++ tests/.gitignore | 1 + tests/Makefile.am | 12 ++++- tests/canvas.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++ tests/cucul-test.cpp | 29 ++++++++++++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tests/canvas.cpp create mode 100644 tests/cucul-test.cpp diff --git a/configure.ac b/configure.ac index 882d829..b20e38b 100644 --- a/configure.ac +++ b/configure.ac @@ -84,6 +84,8 @@ AC_ARG_ENABLE(plugins, [ --enable-plugins make X11 and GL drivers plugins (default disabled)]) AC_ARG_ENABLE(doc, [ --enable-doc build documentation (needs doxygen and LaTeX)]) +AC_ARG_ENABLE(cppunit, + [ --enable-cppunit use cppunit for unit tests (autodetected)]) AC_ARG_ENABLE(zzuf, [ --enable-zzuf use zzuf for fuzzing tests (autodetected)]) @@ -451,6 +453,11 @@ fi AM_CONDITIONAL(BUILD_DOCUMENTATION, test "${DOXYGEN}" != "no") AM_CONDITIONAL(USE_LATEX, test "${LATEX}" != "no") +# Use cppunit for unit tests? +CPPUNIT="no" +PKG_CHECK_MODULES(CPPUNIT, cppunit, [CPPUNIT="yes"], [AC_MSG_RESULT(no)]) +AM_CONDITIONAL(USE_CPPUNIT, test "$CPPUNIT" != "no") + # Use zzuf for fuzzing tests? ZZUF="no" if test "${enable_zzuf}" != "no"; then diff --git a/tests/.gitignore b/tests/.gitignore index ab23474..d9b55c2 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,2 @@ simple +cucul-test diff --git a/tests/Makefile.am b/tests/Makefile.am index ba50116..ab8433e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,10 +2,18 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/cucul -I../cucul -TESTS = simple +if USE_CPPUNIT +cppunit_tests = cucul-test +endif -check_PROGRAMS = simple +noinst_PROGRAMS = simple $(cppunit_tests) + +TESTS = $(noinst_PROGRAMS) simple_SOURCES = simple.c simple_LDADD = ../cucul/libcucul.la +cucul_test_SOURCES = cucul-test.cpp canvas.cpp +cucul_test_CPPFLAGS = $(CPPUNIT_CFLAGS) +cucul_test_LDADD = ../cucul/libcucul.la $(CPPUNIT_LIBS) + diff --git a/tests/canvas.cpp b/tests/canvas.cpp new file mode 100644 index 0000000..a1338d9 --- /dev/null +++ b/tests/canvas.cpp @@ -0,0 +1,103 @@ +/* + * cucul-test testsuite program for libcucul + * Copyright (c) 2008 Sam Hocevar + * Copyright (c) 2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. 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/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "cucul.h" + +class CanvasTest : public CppUnit::TestCase +{ + CPPUNIT_TEST_SUITE(CanvasTest); + CPPUNIT_TEST(test_creation); + CPPUNIT_TEST(test_resize); + CPPUNIT_TEST(test_chars); + CPPUNIT_TEST_SUITE_END(); + +public: + CanvasTest() : CppUnit::TestCase("Canvas Test") {} + + void setUp() {} + + void tearDown() {} + + void test_creation() + { + cucul_canvas_t *cv; + + cv = cucul_create_canvas(0, 0); + CPPUNIT_ASSERT(cv != NULL); + + cucul_free_canvas(cv); + } + + void test_resize() + { + cucul_canvas_t *cv; + + cv = cucul_create_canvas(0, 0); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_width(cv), 0U); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_height(cv), 0U); + + cucul_set_canvas_size(cv, 1, 1); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_width(cv), 1U); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_height(cv), 1U); + + cucul_set_canvas_size(cv, 1234, 1001); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_width(cv), 1234U); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_height(cv), 1001U); + + cucul_set_canvas_size(cv, 0, 0); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_width(cv), 0U); + CPPUNIT_ASSERT_EQUAL(cucul_get_canvas_height(cv), 0U); + + cucul_free_canvas(cv); + } + + void test_chars() + { + cucul_canvas_t *cv; + + cv = cucul_create_canvas(0, 0); + CPPUNIT_ASSERT(cucul_get_char(cv, 0, 0) == ' '); + + cucul_put_char(cv, 0, 0, 'x'); + CPPUNIT_ASSERT(cucul_get_char(cv, 0, 0) == ' '); + + cucul_set_canvas_size(cv, 1, 1); + CPPUNIT_ASSERT(cucul_get_char(cv, 0, 0) == ' '); + + cucul_put_char(cv, 0, 0, 'x'); + CPPUNIT_ASSERT(cucul_get_char(cv, 0, 0) == 'x'); + + cucul_put_char(cv, 0, 0, 'y'); + CPPUNIT_ASSERT(cucul_get_char(cv, 0, 0) == 'y'); + + cucul_set_canvas_size(cv, 1000, 1000); + CPPUNIT_ASSERT(cucul_get_char(cv, 999, 999) == ' '); + + cucul_put_char(cv, 999, 999, 'z'); + CPPUNIT_ASSERT(cucul_get_char(cv, 999, 999) == 'z'); + + cucul_free_canvas(cv); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(CanvasTest); + diff --git a/tests/cucul-test.cpp b/tests/cucul-test.cpp new file mode 100644 index 0000000..abadc8c --- /dev/null +++ b/tests/cucul-test.cpp @@ -0,0 +1,29 @@ +/* + * cucul-test testsuite program for libcucul + * Copyright (c) 2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. 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/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include + +int main(int argc, char *argv[]) +{ + CppUnit::TextTestRunner runner; + runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); + + runner.run(); + + return 0; +} +