service_port_test.cpp
Go to the documentation of this file.
00001 
00002 #include <TaskContext.hpp>
00003 #include <InputPort.hpp>
00004 #include <OutputPort.hpp>
00005 #include <Service.hpp>
00006 #include <ServiceRequester.hpp>
00007 
00008 #include "unit.hpp"
00009 #include "operations_fixture.hpp"
00010 
00011 struct ServicePortFixture {};
00012 
00013 // Registers the suite into the 'registry'
00014 BOOST_FIXTURE_TEST_SUITE(  ServicePortTestSuite,  ServicePortFixture )
00015 
00016 
00020 class TestService : public Service {
00021 public:
00022     InputPort<int> ip;
00023     InputPort<int> ip2;
00024     OutputPort<int> op;
00025     TestService(TaskContext* owner = 0) : Service("portservice", owner)
00026     {
00027         addPort("ip",ip).doc("ip");
00028         addPort("ip",ip2).doc("ip"); // overrides ip
00029         addPort("op",op).doc("op");
00030     }
00031 };
00032 
00033 class TestEventService : public Service {
00034 public:
00035     InputPort<int> ip;
00036     InputPort<int> ip2;
00037     OutputPort<int> op;
00038     TestEventService(TaskContext* owner = 0) : Service("portservice", owner)
00039     {
00040         addEventPort("ip",ip).doc("ip");
00041         addEventPort("ip",ip2).doc("ip"); // overrides ip
00042         addPort("op",op).doc("op");
00043     }
00044 };
00045 
00046 BOOST_AUTO_TEST_CASE(testAddPort)
00047 {
00048     TestService* ts = new TestService();
00049     Service::shared_ptr s( ts );
00050     TaskContext tc("tc");
00051 
00052     tc.provides()->addService( s );
00053 
00054     // check that last port is the real thing:
00055     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00056 
00057     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00058 }
00059 
00060 BOOST_AUTO_TEST_CASE(testAddPortWithOwner)
00061 {
00062     TaskContext tc("tc");
00063     TestService* ts = new TestService( &tc );
00064     Service::shared_ptr s( ts );
00065 
00066     tc.provides()->addService( s );
00067 
00068     // check that last port is the real thing:
00069     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00070 
00071     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00072 }
00073 
00074 
00075 BOOST_AUTO_TEST_CASE(testAddEventPort)
00076 {
00077     TestEventService* ts = new TestEventService();
00078     Service::shared_ptr s( ts );
00079     TaskContext tc("tc");
00080 
00081     tc.provides()->addService( s );
00082 
00083     // check that last port is the real thing:
00084     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00085 
00086     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00087 }
00088 
00089 BOOST_AUTO_TEST_CASE(testAddEventPortWithOwner)
00090 {
00091     TaskContext tc("tc");
00092     TestEventService* ts = new TestEventService(&tc);
00093     Service::shared_ptr s( ts );
00094 
00095     tc.provides()->addService( s );
00096 
00097     // check that last port is the real thing:
00098     BOOST_CHECK( tc.provides("portservice")->getPort("ip") == &ts->ip2 );
00099 
00100     BOOST_CHECK( tc.provides("portservice")->getPort("op") == &ts->op );
00101 }
00102 
00103 
00104 BOOST_AUTO_TEST_CASE(testUsePort)
00105 {
00106     TestService* ts = new TestService();
00107     Service::shared_ptr s( ts );
00108     TaskContext tc("tc");
00109 
00110     tc.provides()->addService( s );
00111 
00112     ts->ip2.connectTo( &ts->op );
00113 
00114     // use operation interface of port:
00115     OperationCaller<void(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
00116     BOOST_CHECK( write.ready() );
00117     write( 3 );
00118 
00119     int result;
00120     OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
00121     BOOST_CHECK( read.ready() );
00122     FlowStatus fs = read( result  );
00123     BOOST_CHECK_EQUAL( result, 3);
00124     BOOST_CHECK_EQUAL( fs, NewData );
00125 }
00126 
00127 BOOST_AUTO_TEST_CASE(testUsePortWithOwner)
00128 {
00129     TaskContext tc("tc");
00130     TestService* ts = new TestService(&tc);
00131     Service::shared_ptr s( ts );
00132 
00133     tc.provides()->addService( s );
00134 
00135     ts->ip2.connectTo( &ts->op );
00136 
00137     // use operation interface of port:
00138     OperationCaller<void(int const&)> write = tc.provides("portservice")->provides("op")->getOperation("write");
00139     BOOST_CHECK( write.ready() );
00140     write( 3 );
00141 
00142     int result;
00143     OperationCaller<FlowStatus(int&)> read = tc.provides("portservice")->provides("ip")->getOperation("read");
00144     BOOST_CHECK( read.ready() );
00145     FlowStatus fs = read( result  );
00146     BOOST_CHECK_EQUAL( result, 3);
00147     BOOST_CHECK_EQUAL( fs, NewData );
00148 }
00149 
00150 BOOST_AUTO_TEST_SUITE_END()


rtt
Author(s): RTT Developers
autogenerated on Thu Jan 2 2014 11:35:25