From e61edb0a5993703ab720ce305f0081b5bd10e085 Mon Sep 17 00:00:00 2001
From: Ramin Yaghoubzadeh <ryaghoubzadeh@uni-bielefeld.de>
Date: Mon, 16 Nov 2015 14:27:03 +0100
Subject: [PATCH] C++: Unit tests working using boost unit_test_framework

Also removed stray cout lines
---
 ipaacalib/cpp/build.properties       |   2 +
 ipaacalib/cpp/src/ipaaca-buffers.cc  |  10 +-
 ipaacalib/cpp/test/CMakeLists.txt    |   9 +-
 ipaacalib/cpp/test/src/testipaaca.cc | 159 +++++++++++----------------
 4 files changed, 79 insertions(+), 101 deletions(-)

diff --git a/ipaacalib/cpp/build.properties b/ipaacalib/cpp/build.properties
index b3fc60b..7eaaa9b 100644
--- a/ipaacalib/cpp/build.properties
+++ b/ipaacalib/cpp/build.properties
@@ -4,4 +4,6 @@ resource.path=${shared.resources}/;
 rebuild.list=
 publish.resolver=asap.sftp.publish
 dist.dir=../../dist
+deps.dir=../../deps
+test.binary=testipaaca
 
diff --git a/ipaacalib/cpp/src/ipaaca-buffers.cc b/ipaacalib/cpp/src/ipaaca-buffers.cc
index 8c99b7d..ad0462a 100644
--- a/ipaacalib/cpp/src/ipaaca-buffers.cc
+++ b/ipaacalib/cpp/src/ipaaca-buffers.cc
@@ -141,13 +141,13 @@ IPAACA_EXPORT void Buffer::_allocate_unique_name(const std::string& basename, co
 }
 IPAACA_EXPORT void Buffer::register_handler(IUEventHandlerFunction function, IUEventType event_mask, const std::set<std::string>& categories)
 {
-	std::cout << "register_handler " << function << " " << event_mask << " " << categories << std::endl;
+	//std::cout << "register_handler " << function << " " << event_mask << " " << categories << std::endl;
 	IUEventHandler::ptr handler = IUEventHandler::ptr(new IUEventHandler(function, event_mask, categories));
 	_event_handlers.push_back(handler);
 }
 IPAACA_EXPORT void Buffer::register_handler(IUEventHandlerFunction function, IUEventType event_mask, const std::string& category)
 {
-	std::cout << "register_handler " << function << " " << event_mask << " " << category << std::endl;
+	//std::cout << "register_handler " << function << " " << event_mask << " " << category << std::endl;
 	IUEventHandler::ptr handler = IUEventHandler::ptr(new IUEventHandler(function, event_mask, category));
 	_event_handlers.push_back(handler);
 }
@@ -640,7 +640,7 @@ IPAACA_EXPORT void InputBuffer::_trigger_resend_request(EventPtr event) {
 		uid = update->uid();
 		writerName = update->writer_name();
 	} else {
-		std::cout << "_trigger_resend_request: unhandled event type " << type << std::endl;
+		IPAACA_WARNING("_trigger_resend_request: unhandled event type " << type)
 	}
 
 	if (!writerName.empty()) {
@@ -683,7 +683,7 @@ IPAACA_EXPORT void InputBuffer::_handle_iu_events(EventPtr event)
 		if (type == "ipaaca::IUPayloadUpdate") {
 			boost::shared_ptr<IUPayloadUpdate> update = boost::static_pointer_cast<IUPayloadUpdate>(event->getData());
 			//IPAACA_INFO("** writer name: " << update->writer_name)
-			std::cout << "writer name " << update->writer_name << std::endl;
+			//std::cout << "writer name " << update->writer_name << std::endl;
 			if (update->writer_name == _unique_name) {
 				return;
 			}
@@ -746,7 +746,7 @@ IPAACA_EXPORT void InputBuffer::_handle_iu_events(EventPtr event)
 			call_iu_event_handlers(it->second, false, IU_COMMITTED, it->second->category() );
 			//
 		} else {
-			std::cout << "(Unhandled Event type " << type << " !)" << std::endl;
+			IPAACA_WARNING("(Unhandled Event type " << type << " !)");
 			return;
 		}
 		//IPAACA_INFO( "New RemotePushIU state: " << *(it->second) )
diff --git a/ipaacalib/cpp/test/CMakeLists.txt b/ipaacalib/cpp/test/CMakeLists.txt
index 6a56b61..6568c8e 100644
--- a/ipaacalib/cpp/test/CMakeLists.txt
+++ b/ipaacalib/cpp/test/CMakeLists.txt
@@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES")
 # find cmake modules locally too
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
 
-find_package(Boost COMPONENTS system filesystem thread regex REQUIRED)
+find_package(Boost COMPONENTS system filesystem thread regex unit_test_framework REQUIRED)
 link_directories(${Boost_LIBRARY_DIRS})
 include_directories(${Boost_INCLUDE_DIRS})
 
@@ -20,6 +20,9 @@ find_package(Protobuf REQUIRED)
 link_directories(${PROTOBUF_LIBRARY_DIRS})
 include_directories(${PROTOBUF_INCLUDE_DIRS})
 
+# for boost unit_test to create main()
+add_definitions(-DBOOST_TEST_DYN_LINK)
+
 #set(RSBLIBS rsc rsbcore)
 
 set(LIBS ${LIBS} ipaaca )
@@ -75,3 +78,7 @@ install (
 	ARCHIVE DESTINATION lib
 	)
 
+
+enable_testing()
+add_test(TestIpaacaCpp testipaaca)
+
diff --git a/ipaacalib/cpp/test/src/testipaaca.cc b/ipaacalib/cpp/test/src/testipaaca.cc
index 828136b..7769f74 100644
--- a/ipaacalib/cpp/test/src/testipaaca.cc
+++ b/ipaacalib/cpp/test/src/testipaaca.cc
@@ -33,121 +33,90 @@
 #include <ipaaca/ipaaca.h>
 #include <typeinfo>
 
-using namespace ipaaca;
+#define BOOST_TEST_MODULE TestIpaacaCpp
+#include <boost/test/unit_test.hpp>
 
-const char RECV_CATEGORY[] = "WORD";
-const char SEND_CATEGORY[] = "TEXT";
+using namespace ipaaca;
 
-class TextSender {
-	protected:
-		OutputBuffer::ptr _ob;
-		InputBuffer::ptr  _ib;
+class TestReceiver {
 	public:
-		TextSender();
-		void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
+		InputBuffer::ptr  _ib;
+		std::string received_info;
+		TestReceiver();
 		void inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
-		IUInterface::ptr find_last_iu();
-		void publish_text_to_print(const std::string& text, const std::string& parent_iu_uid="");
 };
 
-TextSender::TextSender() {
-	_ob = OutputBuffer::create("TextSenderOut");
-	_ob->register_handler(boost::bind(&TextSender::outbuffer_handle_iu_event, this, _1, _2, _3));
-	_ib = InputBuffer::create("TextSenderIn", RECV_CATEGORY);
-	_ib->register_handler(boost::bind(&TextSender::inbuffer_handle_iu_event, this, _1, _2, _3));
+TestReceiver::TestReceiver()
+{
+	_ib = ipaaca::InputBuffer::create("TestReceiver", "cppTestCategory");
+	_ib->register_handler(boost::bind(&TestReceiver::inbuffer_handle_iu_event, this, _1, _2, _3));
+	received_info = "NOTHING RECEIVED YET";
 }
 
-void TextSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
+void TestReceiver::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
 {
-	std::cout << "(own IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
-	if (event_type == IU_UPDATED) {
-		std::set<std::string> parent_uids = iu->get_links("GRIN");
-		if (parent_uids.size() > 0) {
-			std::string parent_uid = *(parent_uids.begin());
-			std::cout << "updating parent ..." << std::endl;
-			std::set<std::string> next_uids = iu->get_links("SUCCESSOR");
-			if (next_uids.size() > 0) {
-				std::string next_uid = *(next_uids.begin());
-				IUInterface::ptr next_iu = _ob->get(next_uid);
-				std::set<std::string> next_letter_grin_links = next_iu->get_links("GRIN");
-				if (next_letter_grin_links.count(parent_uid) == 0) {
-					// next letter belongs to new word
-					IUInterface::ptr parent_iu = _ib->get(parent_uid);
-					parent_iu->payload()["STATE"] = "REALIZED";
-				} else {
-					IUInterface::ptr parent_iu = _ib->get(parent_uid);
-					parent_iu->payload()["STATE"] = "STARTED";
-				}
-			} else {
-				// there are no more letters, this is the end of the final word
-				IUInterface::ptr parent_iu = _ib->get(parent_uid);
-				parent_iu->payload()["STATE"] = "REALIZED";
-			}
-			std::cout << " ... done." << std::endl;
+	if (event_type == IU_ADDED) {
+		received_info = (std::string) iu->payload()["word"];
+		{
+			ipaaca::Locker locker(iu->payload());
+			iu->payload()["replyVector"] = std::vector<double> { 1.0, 2.0, 3.0 };
+			iu->payload()["replyComment"] = "OK";
 		}
-	} else {
 	}
 }
 
-void TextSender::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
-{
-	if (event_type == IU_LINKSUPDATED) {
-		std::cout << "links updated" << std::endl;
-	} else if (event_type == IU_ADDED) {
-		std::string word = iu->payload()["WORD"];
-		std::cout << "Received new word: " << word << std::endl;
-		publish_text_to_print(word, iu->uid());
-	} else if (event_type == IU_RETRACTED) {
-		std::string retracted_uid = iu->uid();
-	} else {
-		std::cout << "(IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
-	}
+
+class TestSender {
+	public:
+		OutputBuffer::ptr _ob;
+		std::vector<double> double_vec;
+		std::string comment;
+		long num_replies;
+		TestSender();
+		void publish_one_message();
+		void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
+};
+
+TestSender::TestSender() {
+	_ob = OutputBuffer::create("TestSender");
+	_ob->register_handler(boost::bind(&TestSender::outbuffer_handle_iu_event, this, _1, _2, _3));
+	comment = "NO COMMENT YET";
+	num_replies = 0;
 }
 
-IUInterface::ptr TextSender::find_last_iu() {
-	std::set<IUInterface::ptr> ius = _ob->get_ius();
-	for (std::set<IUInterface::ptr>::iterator it = ius.begin(); it!=ius.end(); ++it) {
-		if ((*it)->get_links("SUCCESSOR").size() == 0) return *it;
+void TestSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
+{
+	if (event_type == IU_UPDATED) {
+		num_replies++;
+		double_vec = iu->payload()["replyVector"];
+		comment = (std::string) iu->payload()["replyComment"];
 	}
-	return IUInterface::ptr();
 }
 
-void TextSender::publish_text_to_print(const std::string& text, const std::string& parent_iu_uid) {
-	IUInterface::ptr previous_iu = find_last_iu();
-	if (previous_iu) {
-		// insert a blank if we already have words in the buffer
-		IU::ptr iu = IU::create( SEND_CATEGORY );
-		iu->payload()["CONTENT"] = " ";
-		_ob->add(iu);
-		previous_iu->add_link( "SUCCESSOR", iu->uid() );
-		iu->add_link( "PREDECESSOR", previous_iu->uid() );
-		if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
-		previous_iu = iu;
-	}
-	for (int i=0; i<text.size(); ++i) {
-		IU::ptr iu = IU::create( SEND_CATEGORY );
-		iu->payload()["CONTENT"] = std::string(1, text.at(i));
-		_ob->add(iu);
-		if (previous_iu) {
-			previous_iu->add_link( "SUCCESSOR", iu->uid() );
-			iu->add_link( "PREDECESSOR", previous_iu->uid() );
-			if (parent_iu_uid != "") iu->add_link( "GRIN", parent_iu_uid );
-		}
-		if (previous_iu) std::cout << "previous IU: " << *previous_iu << std::endl;
-		previous_iu = iu;
-	}
+void TestSender::publish_one_message()
+{
+	ipaaca::IU::ptr iu = ipaaca::IU::create("cppTestCategory");
+	iu->payload()["word"] = "OK";
+	_ob->add(iu);
 }
-	
-int main() {
-	TextSender sender;
+
+BOOST_AUTO_TEST_SUITE (testIpaacaCpp)
+
+
+BOOST_AUTO_TEST_CASE( testIpaacaCpp01 )
+{
+	TestSender sender;
+	TestReceiver receiver;
+	std::cout << "Publishing one message and waiting 1s for replies from other module." << std::endl;
+	sender.publish_one_message();
 	sleep(1);
-	sender.publish_text_to_print("(INIT)");
-	std::cout << "Press Ctrl-C to cancel..." << std::endl;
-	while (true) sleep(1);
+	std::cout << "Checking for changes." << std::endl;
+	BOOST_CHECK( receiver.received_info == "OK" );
+	BOOST_CHECK( sender.num_replies == 1 );
+	BOOST_CHECK( sender.comment == "OK" );
+	BOOST_CHECK( sender.double_vec.size() == 3 );
+	std::cout << "Complete." << std::endl;
 }
 
-int old_main() {
-	std::cerr << "TODO: implement Ipaaca C++ test cases." << std::endl;
-	return 0;
-}
+BOOST_AUTO_TEST_SUITE_END( )
 
-- 
GitLab