Skip to content
Snippets Groups Projects
Commit e61edb0a authored by Ramin Yaghoubzadeh Torky's avatar Ramin Yaghoubzadeh Torky
Browse files

C++: Unit tests working using boost unit_test_framework

Also removed stray cout lines
parent 922f3458
No related branches found
No related tags found
No related merge requests found
...@@ -4,4 +4,6 @@ resource.path=${shared.resources}/; ...@@ -4,4 +4,6 @@ resource.path=${shared.resources}/;
rebuild.list= rebuild.list=
publish.resolver=asap.sftp.publish publish.resolver=asap.sftp.publish
dist.dir=../../dist dist.dir=../../dist
deps.dir=../../deps
test.binary=testipaaca
...@@ -141,13 +141,13 @@ IPAACA_EXPORT void Buffer::_allocate_unique_name(const std::string& basename, co ...@@ -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) 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)); IUEventHandler::ptr handler = IUEventHandler::ptr(new IUEventHandler(function, event_mask, categories));
_event_handlers.push_back(handler); _event_handlers.push_back(handler);
} }
IPAACA_EXPORT void Buffer::register_handler(IUEventHandlerFunction function, IUEventType event_mask, const std::string& category) 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)); IUEventHandler::ptr handler = IUEventHandler::ptr(new IUEventHandler(function, event_mask, category));
_event_handlers.push_back(handler); _event_handlers.push_back(handler);
} }
...@@ -640,7 +640,7 @@ IPAACA_EXPORT void InputBuffer::_trigger_resend_request(EventPtr event) { ...@@ -640,7 +640,7 @@ IPAACA_EXPORT void InputBuffer::_trigger_resend_request(EventPtr event) {
uid = update->uid(); uid = update->uid();
writerName = update->writer_name(); writerName = update->writer_name();
} else { } else {
std::cout << "_trigger_resend_request: unhandled event type " << type << std::endl; IPAACA_WARNING("_trigger_resend_request: unhandled event type " << type)
} }
if (!writerName.empty()) { if (!writerName.empty()) {
...@@ -683,7 +683,7 @@ IPAACA_EXPORT void InputBuffer::_handle_iu_events(EventPtr event) ...@@ -683,7 +683,7 @@ IPAACA_EXPORT void InputBuffer::_handle_iu_events(EventPtr event)
if (type == "ipaaca::IUPayloadUpdate") { if (type == "ipaaca::IUPayloadUpdate") {
boost::shared_ptr<IUPayloadUpdate> update = boost::static_pointer_cast<IUPayloadUpdate>(event->getData()); boost::shared_ptr<IUPayloadUpdate> update = boost::static_pointer_cast<IUPayloadUpdate>(event->getData());
//IPAACA_INFO("** writer name: " << update->writer_name) //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) { if (update->writer_name == _unique_name) {
return; return;
} }
...@@ -746,7 +746,7 @@ IPAACA_EXPORT void InputBuffer::_handle_iu_events(EventPtr event) ...@@ -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() ); call_iu_event_handlers(it->second, false, IU_COMMITTED, it->second->category() );
// //
} else { } else {
std::cout << "(Unhandled Event type " << type << " !)" << std::endl; IPAACA_WARNING("(Unhandled Event type " << type << " !)");
return; return;
} }
//IPAACA_INFO( "New RemotePushIU state: " << *(it->second) ) //IPAACA_INFO( "New RemotePushIU state: " << *(it->second) )
......
...@@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES") ...@@ -12,7 +12,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DIPAACA_DEBUG_MESSAGES")
# find cmake modules locally too # find cmake modules locally too
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules) 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}) link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS})
...@@ -20,6 +20,9 @@ find_package(Protobuf REQUIRED) ...@@ -20,6 +20,9 @@ find_package(Protobuf REQUIRED)
link_directories(${PROTOBUF_LIBRARY_DIRS}) link_directories(${PROTOBUF_LIBRARY_DIRS})
include_directories(${PROTOBUF_INCLUDE_DIRS}) include_directories(${PROTOBUF_INCLUDE_DIRS})
# for boost unit_test to create main()
add_definitions(-DBOOST_TEST_DYN_LINK)
#set(RSBLIBS rsc rsbcore) #set(RSBLIBS rsc rsbcore)
set(LIBS ${LIBS} ipaaca ) set(LIBS ${LIBS} ipaaca )
...@@ -75,3 +78,7 @@ install ( ...@@ -75,3 +78,7 @@ install (
ARCHIVE DESTINATION lib ARCHIVE DESTINATION lib
) )
enable_testing()
add_test(TestIpaacaCpp testipaaca)
...@@ -33,121 +33,90 @@ ...@@ -33,121 +33,90 @@
#include <ipaaca/ipaaca.h> #include <ipaaca/ipaaca.h>
#include <typeinfo> #include <typeinfo>
using namespace ipaaca; #define BOOST_TEST_MODULE TestIpaacaCpp
#include <boost/test/unit_test.hpp>
const char RECV_CATEGORY[] = "WORD"; using namespace ipaaca;
const char SEND_CATEGORY[] = "TEXT";
class TextSender { class TestReceiver {
protected:
OutputBuffer::ptr _ob;
InputBuffer::ptr _ib;
public: public:
TextSender(); InputBuffer::ptr _ib;
void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local); std::string received_info;
TestReceiver();
void inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local); 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() { TestReceiver::TestReceiver()
_ob = OutputBuffer::create("TextSenderOut"); {
_ob->register_handler(boost::bind(&TextSender::outbuffer_handle_iu_event, this, _1, _2, _3)); _ib = ipaaca::InputBuffer::create("TestReceiver", "cppTestCategory");
_ib = InputBuffer::create("TextSenderIn", RECV_CATEGORY); _ib->register_handler(boost::bind(&TestReceiver::inbuffer_handle_iu_event, this, _1, _2, _3));
_ib->register_handler(boost::bind(&TextSender::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_ADDED) {
if (event_type == IU_UPDATED) { received_info = (std::string) iu->payload()["word"];
std::set<std::string> parent_uids = iu->get_links("GRIN"); {
if (parent_uids.size() > 0) { ipaaca::Locker locker(iu->payload());
std::string parent_uid = *(parent_uids.begin()); iu->payload()["replyVector"] = std::vector<double> { 1.0, 2.0, 3.0 };
std::cout << "updating parent ..." << std::endl; iu->payload()["replyComment"] = "OK";
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;
} }
} else {
} }
} }
void TextSender::inbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
{ class TestSender {
if (event_type == IU_LINKSUPDATED) { public:
std::cout << "links updated" << std::endl; OutputBuffer::ptr _ob;
} else if (event_type == IU_ADDED) { std::vector<double> double_vec;
std::string word = iu->payload()["WORD"]; std::string comment;
std::cout << "Received new word: " << word << std::endl; long num_replies;
publish_text_to_print(word, iu->uid()); TestSender();
} else if (event_type == IU_RETRACTED) { void publish_one_message();
std::string retracted_uid = iu->uid(); void outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local);
} else { };
std::cout << "(IU event " << iu_event_type_to_str(event_type) << " " << iu->uid() << ")" << std::endl;
} 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() { void TestSender::outbuffer_handle_iu_event(IUInterface::ptr iu, IUEventType event_type, bool local)
std::set<IUInterface::ptr> ius = _ob->get_ius(); {
for (std::set<IUInterface::ptr>::iterator it = ius.begin(); it!=ius.end(); ++it) { if (event_type == IU_UPDATED) {
if ((*it)->get_links("SUCCESSOR").size() == 0) return *it; 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) { void TestSender::publish_one_message()
IUInterface::ptr previous_iu = find_last_iu(); {
if (previous_iu) { ipaaca::IU::ptr iu = ipaaca::IU::create("cppTestCategory");
// insert a blank if we already have words in the buffer iu->payload()["word"] = "OK";
IU::ptr iu = IU::create( SEND_CATEGORY ); _ob->add(iu);
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;
}
} }
int main() { BOOST_AUTO_TEST_SUITE (testIpaacaCpp)
TextSender sender;
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); sleep(1);
sender.publish_text_to_print("(INIT)"); std::cout << "Checking for changes." << std::endl;
std::cout << "Press Ctrl-C to cancel..." << std::endl; BOOST_CHECK( receiver.received_info == "OK" );
while (true) sleep(1); 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() { BOOST_AUTO_TEST_SUITE_END( )
std::cerr << "TODO: implement Ipaaca C++ test cases." << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment