From 118208d2a248b7df576fb51c86848a61d05976e2 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Sat, 16 Sep 2017 12:48:09 +0200 Subject: add unit test for the internal stream Change-Id: Iedf880acedbad9d42f4926c71360f21f02e34e3c --- configure.ac | 17 ++++++ src/Makefile.am | 4 ++ src/lib/Makefile.am | 8 ++- src/test/.gitignore | 10 ++++ src/test/FHInternalStreamTest.cpp | 108 ++++++++++++++++++++++++++++++++++++++ src/test/Makefile.am | 27 ++++++++++ src/test/test.cpp | 46 ++++++++++++++++ 7 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 src/test/.gitignore create mode 100644 src/test/FHInternalStreamTest.cpp create mode 100644 src/test/Makefile.am create mode 100644 src/test/test.cpp diff --git a/configure.ac b/configure.ac index e1ff29e..a111f44 100644 --- a/configure.ac +++ b/configure.ac @@ -287,6 +287,21 @@ AS_IF([test "x$enable_debug" = "xyes"], [ ]) AC_SUBST(DEBUG_CXXFLAGS) +# ========== +# Unit tests +# ========== +AC_ARG_ENABLE([tests], + [AS_HELP_STRING([--enable-tests], [Build and run unit tests])], + [enable_tests="$enableval"], + [enable_tests=yes] +) +AS_IF([test "x$enable_tests" = "xyes"], [ + PKG_CHECK_MODULES([CPPUNIT], [cppunit]) +], []) +AC_SUBST([CPPUNIT_CFLAGS]) +AC_SUBST([CPPUNIT_LIBS]) +AM_CONDITIONAL([BUILD_TESTS], [test "x$enable_tests" = "xyes"]) + # ============= # Documentation # ============= @@ -321,6 +336,7 @@ src/conv/text/fh2text.rc src/fuzz/Makefile src/lib/Makefile src/lib/libfreehand.rc +src/test/Makefile inc/Makefile inc/libfreehand/Makefile build/Makefile @@ -340,6 +356,7 @@ Build configuration: debug: ${enable_debug} docs: ${build_docs} fuzzers: ${enable_fuzzers} + tests: ${enable_tests} tools: ${enable_tools} werror: ${enable_werror} ============================================================================== diff --git a/src/Makefile.am b/src/Makefile.am index c83c78c..1bfc366 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,3 +7,7 @@ endif if BUILD_FUZZERS SUBDIRS += fuzz endif + +if BUILD_TESTS +SUBDIRS += test +endif diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 20ed0c6..666df67 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -4,6 +4,7 @@ else version_info = -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) endif +noinst_LTLIBRARIES = libfreehand-internal.la lib_LTLIBRARIES = libfreehand-@FH_MAJOR_VERSION@.@FH_MINOR_VERSION@.la AM_CXXFLAGS = \ @@ -18,15 +19,18 @@ AM_CXXFLAGS = \ BUILT_SOURCES = tokens.h tokenhash.h libfreehand_@FH_MAJOR_VERSION@_@FH_MINOR_VERSION@_la_LIBADD = \ + libfreehand-internal.la \ $(REVENGE_LIBS) \ $(ZLIB_LIBS) \ $(LCMS2_LIBS) \ @LIBFREEHAND_WIN32_RESOURCE@ -libfreehand_@FH_MAJOR_VERSION@_@FH_MINOR_VERSION@_la_DEPENDENCIES = @LIBFREEHAND_WIN32_RESOURCE@ +libfreehand_@FH_MAJOR_VERSION@_@FH_MINOR_VERSION@_la_DEPENDENCIES = libfreehand-internal.la @LIBFREEHAND_WIN32_RESOURCE@ libfreehand_@FH_MAJOR_VERSION@_@FH_MINOR_VERSION@_la_LDFLAGS = $(version_info) -export-dynamic -no-undefined libfreehand_@FH_MAJOR_VERSION@_@FH_MINOR_VERSION@_la_SOURCES = \ - FreeHandDocument.cpp \ + FreeHandDocument.cpp + +libfreehand_internal_la_SOURCES = \ FHCollector.cpp \ FHInternalStream.cpp \ FHParser.cpp \ diff --git a/src/test/.gitignore b/src/test/.gitignore new file mode 100644 index 0000000..7742c8b --- /dev/null +++ b/src/test/.gitignore @@ -0,0 +1,10 @@ +Makefile +Makefile.in +test +.libs +.deps +*.lo +*.log +*.o +*.trs +*~ diff --git a/src/test/FHInternalStreamTest.cpp b/src/test/FHInternalStreamTest.cpp new file mode 100644 index 0000000..45a2c61 --- /dev/null +++ b/src/test/FHInternalStreamTest.cpp @@ -0,0 +1,108 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libfreehand project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include + +#include + +#include "FHInternalStream.h" + +namespace test +{ + +using libfreehand::FHInternalStream; + +class FHInternalStreamTest : public CPPUNIT_NS::TestFixture +{ +public: + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE(FHInternalStreamTest); + CPPUNIT_TEST(testRead); + CPPUNIT_TEST(testSeek); + CPPUNIT_TEST_SUITE_END(); + +private: + void testRead(); + void testSeek(); +}; + +void FHInternalStreamTest::setUp() +{ +} + +void FHInternalStreamTest::tearDown() +{ +} + +void FHInternalStreamTest::testRead() +{ + const unsigned char data[] = "abc dee fgh"; + librevenge::RVNGBinaryData binData(data, sizeof(data)); + FHInternalStream strm(binData.getDataStream(), binData.size()); + + CPPUNIT_ASSERT_MESSAGE("stream is already exhausted before starting to read", !strm.isEnd()); + + for (int i = 0; sizeof(data) != i; ++i) + { + unsigned long readBytes = 0; + const unsigned char *s = strm.read(1, readBytes); + + CPPUNIT_ASSERT(1 == readBytes); + CPPUNIT_ASSERT_EQUAL(data[i], s[0]); + CPPUNIT_ASSERT(((sizeof(data) - 1) == i) || !strm.isEnd()); + } + + CPPUNIT_ASSERT_MESSAGE("reading did not exhaust the stream", strm.isEnd()); + + strm.seek(0, librevenge::RVNG_SEEK_SET); + + unsigned long readBytes = 0; + const unsigned char *s = strm.read(sizeof(data), readBytes); + CPPUNIT_ASSERT(sizeof(data) == readBytes); + CPPUNIT_ASSERT(std::equal(data, data + sizeof(data), s)); +} + +void FHInternalStreamTest::testSeek() +{ + const unsigned char data[] = "abc dee fgh"; + librevenge::RVNGBinaryData binData(data, sizeof(data)); + FHInternalStream strm(binData.getDataStream(), binData.size()); + + strm.seek(0, librevenge::RVNG_SEEK_SET); + CPPUNIT_ASSERT(0 == strm.tell()); + strm.seek(2, librevenge::RVNG_SEEK_SET); + CPPUNIT_ASSERT(2 == strm.tell()); + + strm.seek(1, librevenge::RVNG_SEEK_CUR); + CPPUNIT_ASSERT(3 == strm.tell()); + strm.seek(-2, librevenge::RVNG_SEEK_CUR); + CPPUNIT_ASSERT(1 == strm.tell()); + + CPPUNIT_ASSERT(!strm.isEnd()); + CPPUNIT_ASSERT(0 == strm.seek(0, librevenge::RVNG_SEEK_END)); + CPPUNIT_ASSERT(strm.isEnd()); + CPPUNIT_ASSERT(sizeof(data) == strm.tell()); + CPPUNIT_ASSERT(0 != strm.seek(1, librevenge::RVNG_SEEK_END)); // cannot seek after the end + CPPUNIT_ASSERT(strm.isEnd()); + CPPUNIT_ASSERT(0 == strm.seek(-1, librevenge::RVNG_SEEK_END)); // but can seek before it + CPPUNIT_ASSERT(!strm.isEnd()); + CPPUNIT_ASSERT((sizeof(data) - 1) == strm.tell()); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(FHInternalStreamTest); + +} + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ diff --git a/src/test/Makefile.am b/src/test/Makefile.am new file mode 100644 index 0000000..fa87a13 --- /dev/null +++ b/src/test/Makefile.am @@ -0,0 +1,27 @@ +## -*- Mode: make; tab-width: 4; indent-tabs-mode: tabs -*- + +target_test = test + +check_PROGRAMS = $(target_test) + +AM_CXXFLAGS = \ + -I$(top_srcdir)/inc \ + -I$(top_srcdir)/src/lib \ + $(CPPUNIT_CFLAGS) \ + $(REVENGE_CFLAGS) \ + $(DEBUG_CXXFLAGS) + +test_LDFLAGS = -L$(top_srcdir)/src/lib +test_LDADD = \ + $(top_builddir)/src/lib/libfreehand-internal.la \ + $(CPPUNIT_LIBS) \ + $(REVENGE_LIBS) \ + $(ZLIB_LIBS) + +test_SOURCES = \ + FHInternalStreamTest.cpp \ + test.cpp + +TESTS = $(target_test) + +## vim:set shiftwidth=4 tabstop=4 noexpandtab: diff --git a/src/test/test.cpp b/src/test/test.cpp new file mode 100644 index 0000000..96a6769 --- /dev/null +++ b/src/test/test.cpp @@ -0,0 +1,46 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * This file is part of the libcdr project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include +#include +#include +#include + +#include + +int main() +{ + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + + // Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener(&result); + + // Add a listener that print dots as test run. + CPPUNIT_NS::BriefTestProgressListener progress; + controller.addListener(&progress); + + // Add the top suite to the test runner + CPPUNIT_NS::TestRunner runner; + runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); + runner.run(controller); + + // output + CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr); + outputter.write(); + + // return status code + return result.wasSuccessful() ? 0 : 1; +} + +/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ -- cgit v1.2.3