summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Tardon <dtardon@redhat.com>2017-09-16 12:48:09 +0200
committerDavid Tardon <dtardon@redhat.com>2017-09-16 13:12:23 +0200
commit118208d2a248b7df576fb51c86848a61d05976e2 (patch)
tree3df97539c72df29289dce6911244cec495f235a9
parentec97e9a9fe40887b800eccf4191e70771e0882bf (diff)
add unit test for the internal stream
Change-Id: Iedf880acedbad9d42f4926c71360f21f02e34e3c
-rw-r--r--configure.ac17
-rw-r--r--src/Makefile.am4
-rw-r--r--src/lib/Makefile.am8
-rw-r--r--src/test/.gitignore10
-rw-r--r--src/test/FHInternalStreamTest.cpp108
-rw-r--r--src/test/Makefile.am27
-rw-r--r--src/test/test.cpp46
7 files changed, 218 insertions, 2 deletions
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 <algorithm>
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <librevenge/librevenge.h>
+
+#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 <iostream>
+
+#include <cppunit/BriefTestProgressListener.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/TestResultCollector.h>
+#include <cppunit/TestRunner.h>
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+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: */