changeset 218:9324b9e0e7b7

DynLib: add unit tests
author David Demelier <markand@malikania.fr>
date Thu, 08 May 2014 18:57:47 +0200
parents 5bb5712d400f
children 8fc177bbc4a6
files .hgignore C++/Tests/DynLib/CMakeLists.txt C++/Tests/DynLib/Plugin.cpp C++/Tests/DynLib/TestDynLib.cpp C++/Tests/DynLib/TestDynLib.h C++/Tests/DynLib/main.cpp cmake/FindCppunit.cmake
diffstat 7 files changed, 210 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,1 @@
+_build_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/DynLib/CMakeLists.txt	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,48 @@
+#
+# CMakeLists.txt -- tests for DynLib
+#
+# Copyright (c) 2014 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+# Plugin module
+add_library(Plugin MODULE Plugin.cpp)
+set_target_properties(
+	Plugin
+	PROPERTIES
+	PREFIX ""
+)
+
+set(
+	SOURCES
+	${code_SOURCE_DIR}/C++/DynLib.cpp
+	${code_SOURCE_DIR}/C++/DynLib.h
+	TestDynLib.cpp
+	TestDynLib.h
+)
+
+define_test(dynlib "${SOURCES}")
+
+# The extension for the system
+if(WIN32)
+	set(EXTENSION ".dll")
+elseif(UNIX)
+	set(EXTENSION ".so")
+elseif(APPLE)
+	set(EXTENSION ".dylib")
+else()
+	message(FATAL_ERROR "Unsupported platform")
+endif()
+
+target_compile_definitions(dynlib PRIVATE "EXTENSION=\"${EXTENSION}\"")
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/DynLib/Plugin.cpp	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,12 @@
+#include <string>
+
+#include <DynLib.h>
+
+extern "C" {
+
+void EXPORT initialize(std::string &result)
+{
+	result = "Hello World";
+}
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/DynLib/TestDynLib.cpp	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,70 @@
+/*
+ * TestDynLib.cpp -- test the dynamic library loader
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <iostream>
+
+#include <cppunit/TextTestRunner.h>
+
+#include <DynLib.h>
+
+#include "TestDynLib.h"
+
+/*
+ * NOTE: the EXTENSION is defined by CMake in the form of a string
+ * literal containing the appropriate extension for the given system.
+ */
+
+void TestDynLib::initialize()
+{
+	try {
+		DynLib library("./Plugin" EXTENSION);
+		Initialize init = library.sym<Initialize>("initialize");
+
+		std::string expected("Hello World");
+		std::string result;
+
+		init(result);
+		
+		CPPUNIT_ASSERT_EQUAL(expected, result);
+	} catch (const std::runtime_error &error) {
+		CPPUNIT_ASSERT_MESSAGE(error.what(), false);
+	} catch (const std::out_of_range &error) {
+		CPPUNIT_ASSERT_MESSAGE(error.what(), false);
+	}
+}
+
+void TestDynLib::absent()
+{
+	try {
+		DynLib library("./Plugin" EXTENSION);
+		library.sym<Initialize>("initialize_typo");
+	} catch (const std::out_of_range &error) {
+		return;
+	}
+
+	CPPUNIT_ASSERT_MESSAGE("Expected a failure but succeed", false);
+}
+
+int main()
+{
+	CppUnit::TextTestRunner runnerText;
+
+	runnerText.addTest(TestDynLib::suite());
+
+	return runnerText.run("", false) == 1 ? 0 : 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/DynLib/TestDynLib.h	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,41 @@
+/*
+ * TestDynLib.h -- test the dynamic library loader
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _TEST_DYNLIB_H_
+#define _TEST_DYNLIB_H_
+
+#include <string>
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestFixture.h>
+
+class TestDynLib : public CppUnit::TestFixture {
+private:
+	using Initialize	= void (*)(std::string &);
+
+	CPPUNIT_TEST_SUITE(TestDynLib);
+	CPPUNIT_TEST(initialize);
+	CPPUNIT_TEST(absent);
+	CPPUNIT_TEST_SUITE_END();
+
+public:
+	void initialize();
+	void absent();
+};
+
+#endif // !_TEST_DYNLIB_H_
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/DynLib/main.cpp	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,8 @@
+#include <iostream>
+
+#include <DynLib.h>
+
+int main(void)
+{
+	return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/FindCppunit.cmake	Thu May 08 18:57:47 2014 +0200
@@ -0,0 +1,30 @@
+# Find cppunit, this modules defines:
+# CPPUNIT_INCLUDE_DIR, where to find cppunit/TestCase.h
+# CPPUNIT_LIBRARY, where to find library
+# CPPUNIT_FOUND, if it is found
+
+# find cppunit/TestCase.h
+find_path(
+	CPPUNIT_INCLUDE_DIR cppunit/TestCase.h
+	PATHS
+	/usr/include
+	/usr/local/include
+)
+
+# find libcppunit.so
+find_library(
+	CPPUNIT_LIBRARY NAMES libcppunit cppunit
+	PATHS_SUFFIXES lib lib64
+	PATHS
+	/usr/
+	/usr/local/
+)
+
+include(FindPackageHandleStandardArgs)
+
+find_package_handle_standard_args(
+	Cppunit
+	REQUIRED_VARS CPPUNIT_INCLUDE_DIR CPPUNIT_LIBRARY
+)
+
+mark_as_advanced(CPPUNIT_INCLUDE_DIR CPPUNIT_LIBRARY)