ConnectorListenerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log: ConnectorListenerTests.cpp,v $
14  */
15 
16 #ifndef ConnectorListener_cpp
17 #define ConnectorListener_cpp
18 
19 #include <iostream>
20 
21 #include <cppunit/ui/text/TestRunner.h>
22 #include <cppunit/TextOutputter.h>
23 #include <cppunit/extensions/TestFactoryRegistry.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/TestAssert.h>
26 
27 #include <rtm/idl/BasicDataTypeSkel.h>
28 #include <rtm/idl/DataPortSkel.h>
29 #include <rtm/RTC.h>
30 #include <rtm/CdrBufferBase.h>
31 #include <coil/Properties.h>
32 #include <rtm/ConnectorListener.h>
33 
34 // ConnectorDataListenerType count
35 #define cdl_len 10
36 // ConnectorListenerType count
37 #define cl_len 7
38 
45 {
46  // ConnectorDataListenerType
47  static const char* str_cdl[] =
48  {
49  "ON_BUFFER_WRITE",
50  "ON_BUFFER_FULL",
51  "ON_BUFFER_WRITE_TIMEOUT",
52  "ON_BUFFER_OVERWRITE",
53  "ON_BUFFER_READ",
54  "ON_SEND",
55  "ON_RECEIVED",
56  "ON_RECEIVER_FULL",
57  "ON_RECEIVER_TIMEOUT",
58  "ON_RECEIVER_ERROR"
59  };
60 
61  // ConnectorListenerType
62  static const char* str_cl[] =
63  {
64  "ON_BUFFER_EMPTY",
65  "ON_BUFFER_READ_TIMEOUT",
66  "ON_SENDER_EMPTY",
67  "ON_SENDER_TIMEOUT",
68  "ON_SENDER_ERROR",
69  "ON_CONNECT",
70  "ON_DISCONNECT"
71  };
72 
73  static int cdl_count;
74  static int cl_count;
75 
77  : public RTC::ConnectorDataListenerT<RTC::TimedLong>
78  {
79  public:
80  DataListener(const char* name) : m_name(name)
81  {
82  ++cdl_count;
83  }
84  virtual ~DataListener()
85  {
86  --cdl_count;
87  }
88 
89  virtual void operator()(const RTC::ConnectorInfo& info,
90  const RTC::TimedLong& data)
91  {
92  m_data = data;
93  std::cout << "-------------------------------------------" << std::endl;
94  std::cout << "Data Listener: " << m_name << std::endl;
95 // std::cout << " Data: " << data.data << std::endl;
96  std::cout << "-------------------------------------------" << std::endl;
97  }
98 
99  RTC::TimedLong get_data()
100  {
101  RTC::TimedLong ret;
102  ret = m_data;
103  m_data.data = 0;
104  return ret;
105  };
106 
107  std::string m_name;
108  RTC::TimedLong m_data;
109  };
110 
112  : public RTC::ConnectorListener
113  {
114  public:
115  ConnListener(const char* name) : m_name(name)
116  {
117  ++cl_count;
118  }
119  virtual ~ConnListener()
120  {
121  --cl_count;
122  }
123 
124  virtual void operator()(const RTC::ConnectorInfo& info)
125  {
126  std::cout << "-------------------------------------------" << std::endl;
127  std::cout << "Connector Listener: " << m_name << std::endl;
128  std::cout << "-------------------------------------------" << std::endl;
129  };
130 
131  std::string m_name;
132  };
133 
134 
136  : public CppUnit::TestFixture
137  {
138  CPPUNIT_TEST_SUITE(ConnectorListenerTests);
139 
140  CPPUNIT_TEST(test_ConnectorDataListener);
141  CPPUNIT_TEST(test_ConnectorListener);
142 
143  CPPUNIT_TEST_SUITE_END();
144 
145  private:
147  DataListener *datalisteners[cdl_len];
148  ConnListener *connlisteners[cl_len];
149 
150  public:
151 
156  {
157  }
158 
163  {
164  }
165 
169  virtual void setUp()
170  {
171  }
172 
176  virtual void tearDown()
177  {
178  }
179 
184  {
185  //ConnectorDataListeners
186  for (int i(0); i<cdl_len; ++i)
187  {
188  datalisteners[i] = new DataListener(str_cdl[i]);
189  }
190 
191  //ConnectorInfo
193  coil::vstring ports;
194  RTC::ConnectorInfo info("name", "id", ports, prop);
195  RTC::ConnectorInfo info_big("name", "id", ports, prop);
196  info_big.properties.setProperty("serializer.cdr.endian", "big");
197 
198  // addListener()
199  for (int i(0); i<cdl_len; ++i)
200  {
201  listeners.connectorData_[i].addListener(datalisteners[i], true);
202  }
203 
204  // notify() endian: little
205  for (int i(0); i<cdl_len; ++i)
206  {
207  RTC::TimedLong rtd;
208  cdrMemoryStream cdr;
209  RTC::TimedLong td;
210  td.data = i;
211  cdr.setByteSwapFlag(true);
212  td >>= cdr;
213  listeners.connectorData_[i].notify(info, cdr);
214  rtd = datalisteners[i]->get_data();
215  CPPUNIT_ASSERT_EQUAL(i, (int)rtd.data);
216  }
217 
218  // notify() endian: big
219  for (int i(0); i<cdl_len; ++i)
220  {
221  RTC::TimedLong rtd;
222  cdrMemoryStream cdr;
223  RTC::TimedLong td;
224  td.data = i;
225  cdr.setByteSwapFlag(false);
226  td >>= cdr;
227  listeners.connectorData_[i].notify(info_big, cdr);
228  rtd = datalisteners[i]->get_data();
229  CPPUNIT_ASSERT_EQUAL(i, (int)rtd.data);
230  }
231 
232  // removeListener()
233  for (int i(0); i<cdl_len; ++i)
234  {
235  listeners.connectorData_[i].removeListener(datalisteners[i]);
236  }
237 
238  // notify() endian: little
239  for (int i(0); i<cdl_len; ++i)
240  {
241  RTC::TimedLong rtd;
242  cdrMemoryStream cdr;
243  RTC::TimedLong td;
244  td.data = i;
245  cdr.setByteSwapFlag(true);
246  td >>= cdr;
247  listeners.connectorData_[i].notify(info, cdr);
248  rtd = datalisteners[i]->get_data();
249  CPPUNIT_ASSERT_EQUAL(0, (int)rtd.data);
250  }
251 
252  CPPUNIT_ASSERT_EQUAL(0, cdl_count);
253 
254  // デストラクタでdelete しているので不要。
255  // delete datalisteners;
256  }
257 
262  {
263  //ConnectorListeners
264  for (int i(0); i<cl_len; ++i)
265  {
266  connlisteners[i] = new ConnListener(str_cl[i]);
267  }
268 
269  //ConnectorInfo
271  coil::vstring ports;
272  RTC::ConnectorInfo info("name", "id", ports, prop);
273 
274  // addListener()
275  for (int i(0); i<cl_len; ++i)
276  {
277  listeners.connector_[i].addListener(connlisteners[i], true);
278  }
279 
280  // notify()
281  for (int i(0); i<cl_len; ++i)
282  {
283  listeners.connector_[i].notify(info);
284  }
285 
286  // removeListener()
287  for (int i(0); i<cl_len; ++i)
288  {
289  listeners.connector_[i].removeListener(connlisteners[i]);
290  }
291 
292  // notify() endian: little
293  for (int i(0); i<cl_len; ++i)
294  {
295  listeners.connector_[i].notify(info);
296  }
297  CPPUNIT_ASSERT_EQUAL(0, cl_count);
298 
299  // デストラクタでdelete しているので不要。
300  // delete datalisteners;
301  }
302 
303  };
304 }; // namespace ConnectorListener
305 
306 /*
307  * Register test suite
308  */
310 
311 #ifdef LOCAL_MAIN
312 int main(int argc, char* argv[])
313 {
314 
315  FORMAT format = TEXT_OUT;
316  int target = 0;
317  std::string xsl;
318  std::string ns;
319  std::string fname;
320  std::ofstream ofs;
321 
322  int i(1);
323  while (i < argc)
324  {
325  std::string arg(argv[i]);
326  std::string next_arg;
327  if (i + 1 < argc) next_arg = argv[i + 1];
328  else next_arg = "";
329 
330  if (arg == "--text") { format = TEXT_OUT; break; }
331  if (arg == "--xml")
332  {
333  if (next_arg == "")
334  {
335  fname = argv[0];
336  fname += ".xml";
337  }
338  else
339  {
340  fname = next_arg;
341  }
342  format = XML_OUT;
343  ofs.open(fname.c_str());
344  }
345  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
346  if ( arg == "--cerr" ) { target = 1; break; }
347  if ( arg == "--xsl" )
348  {
349  if (next_arg == "") xsl = "default.xsl";
350  else xsl = next_arg;
351  }
352  if ( arg == "--namespace" )
353  {
354  if (next_arg == "")
355  {
356  std::cerr << "no namespace specified" << std::endl;
357  exit(1);
358  }
359  else
360  {
361  xsl = next_arg;
362  }
363  }
364  ++i;
365  }
366  CppUnit::TextUi::TestRunner runner;
367  if ( ns.empty() )
368  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
369  else
370  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
371  CppUnit::Outputter* outputter = 0;
372  std::ostream* stream = target ? &std::cerr : &std::cout;
373  switch ( format )
374  {
375  case TEXT_OUT :
376  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
377  break;
378  case XML_OUT :
379  std::cout << "XML_OUT" << std::endl;
380  outputter = new CppUnit::XmlOutputter(&runner.result(),
381  ofs, "shift_jis");
382  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
383  break;
384  case COMPILER_OUT :
385  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
386  break;
387  }
388  runner.setOutputter(outputter);
389  runner.run();
390  return 0; // runner.run() ? 0 : 1;
391 }
392 #endif // MAIN
393 #endif // ConnectorListener_cpp
void removeListener(ConnectorListener *listener)
Remove the listener.
ConnectorListeners class.
int main(int argc, char **argv)
coil::Properties properties
Connection properties.
virtual void setUp()
Test initialization.
virtual void operator()(const RTC::ConnectorInfo &info, const RTC::TimedLong &data)
Virtual Callback method.
ConnectorListener class.
#define cdl_len
void addListener(ConnectorDataListener *listener, bool autoclean)
Add the listener.
ConnectorListenerHolder connector_[CONNECTOR_LISTENER_NUM]
ConnectorListenerType listener array The ConnectorListenerType listener is stored.
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
void removeListener(ConnectorDataListener *listener)
Remove the listener.
void addListener(ConnectorListener *listener, bool autoclean)
Add the listener.
static const char * str_cl[]
std::vector< std::string > vstring
Definition: stringutil.h:37
ConnectorDataListenerHolder connectorData_[CONNECTOR_DATA_LISTENER_NUM]
ConnectorDataListenerType listener array The ConnectorDataListenerType listener is stored...
#define cl_len
void test_ConnectorDataListener()
ConnectorDataListener test.
ConnectorDataListenerT class.
void notify(const ConnectorInfo &info, const cdrMemoryStream &cdrdata)
Notify listeners.
std::string setProperty(const std::string &key, const std::string &value)
Set a value associated with key in the property list.
Definition: Properties.cpp:236
prop
Organization::get_organization_property ();.
CPPUNIT_TEST_SUITE_REGISTRATION(ConnectorListener::ConnectorListenerTests)
Class represents a set of properties.
Definition: Properties.h:101
RTComponent header.
virtual void tearDown()
Test finalization.
void notify(const ConnectorInfo &info)
Notify listeners.
static const char * str_cdl[]
connector listener class
virtual void operator()(const RTC::ConnectorInfo &info)
Virtual Callback method.
void test_ConnectorListener()
ConnectorListener test.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:50