diff C++/tests/Dynlib/main.cpp @ 334:0b576ee64d45

* Create brand new hierarchy * Rename DynLib to Dynlib * Remove some warnings
author David Demelier <markand@malikania.fr>
date Sun, 08 Mar 2015 14:26:33 +0100
parents C++/Tests/DynLib/main.cpp@3b4ae8feca1c
children d5ec1174b707
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/tests/Dynlib/main.cpp	Sun Mar 08 14:26:33 2015 +0100
@@ -0,0 +1,68 @@
+/*
+ * 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 <gtest/gtest.h>
+
+#include <Dynlib.h>
+
+/*
+ * NOTE: the EXTENSION is defined by CMake in the form of a string
+ * literal containing the appropriate extension for the given system.
+ */
+
+using Initialize = void (*)(std::string &s);
+
+TEST(Basic, initialize)
+{
+	try {
+		Dynlib library("./dynlib-plugin" EXTENSION);
+		Initialize init = library.sym<Initialize>("initialize");
+
+		std::string expected("Hello World");
+		std::string result;
+
+		init(result);
+
+		ASSERT_EQ(expected, result);
+	} catch (const std::runtime_error &error) {
+		FAIL() << error.what();
+	} catch (const std::out_of_range &error) {
+		FAIL() << error.what();
+	}
+}
+
+TEST(Basic, absent)
+{
+	try {
+		Dynlib library("./dynlib-plugin" EXTENSION);
+		library.sym<Initialize>("initialize_typo");
+	} catch (const std::out_of_range &error) {
+		return;
+	}
+
+	FAIL() << "Expected a failure but succeed";
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}