CORBA_SeqUtilTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
21 /*
22  * $Log: CORBA_SeqUtilTests.cpp,v $
23  * Revision 1.2 2008/01/10 11:02:06 arafune
24  * *** empty log message ***
25  *
26  * Revision 1.1 2007/12/20 07:50:18 arafune
27  * *** empty log message ***
28  *
29  * Revision 1.1 2006/11/27 08:26:10 n-ando
30  * TestSuites are devided into each directory.
31  *
32  * Revision 1.1 2006/10/31 13:10:51 kurihara
33  *
34  * test program for CORBA_SeqUtil.h
35  *
36  */
37 
38 #ifndef CORBA_SeqUtil_cpp
39 #define CORBA_SeqUtil_cpp
40 
41 #include <cppunit/ui/text/TestRunner.h>
42 #include <cppunit/TextOutputter.h>
43 #include <cppunit/extensions/TestFactoryRegistry.h>
44 #include <cppunit/extensions/HelperMacros.h>
45 #include <cppunit/TestAssert.h>
46 
47 #include <rtm/CORBA_SeqUtil.h>
48 #include <rtm/NVUtil.h>
49 #include <rtm/PortAdmin.h>
50 #include <rtm/ConnectorBase.h>
51 #include "SeqUtilTestsSkel.h"
52 
57 namespace CORBA_SeqUtil
58 {
59 
60  class Logger
61  {
62  public:
63  void log(const std::string& msg)
64  {
65  m_log.push_back(msg);
66  }
67 
68  int countLog(const std::string& msg)
69  {
70  int count = 0;
71  for (int i = 0; i < (int) m_log.size(); ++i)
72  {
73  if (m_log[i] == msg) ++count;
74  }
75  return count;
76  }
77 
78  void clearLog(void)
79  {
80  m_log.clear();
81  }
82  private:
83  std::vector<std::string> m_log;
84  };
85 
86  class PortMock
87  : public RTC::PortBase
88  {
89  protected:
90  virtual RTC::ReturnCode_t publishInterfaces(RTC::ConnectorProfile&)
91  {
92  return RTC::RTC_OK;
93  }
94  virtual RTC::ReturnCode_t subscribeInterfaces(const RTC::ConnectorProfile&)
95  {
96  return RTC::RTC_OK;
97  }
98  virtual void unsubscribeInterfaces(const RTC::ConnectorProfile&)
99  {
100  }
101  virtual void activateInterfaces()
102  {
103  }
104  virtual void deactivateInterfaces()
105  {
106  }
107  };
108 
109 
110  using namespace std;
111 
113  : public CppUnit::TestFixture
114  {
115  CPPUNIT_TEST_SUITE(CORBA_SeqUtilTests);
116 
117  CPPUNIT_TEST(test_for_each);
118  CPPUNIT_TEST(test_find);
119  CPPUNIT_TEST(test_push_back);
120  CPPUNIT_TEST(test_push_back_list);
121  CPPUNIT_TEST(test_insert);
122  CPPUNIT_TEST(test_front);
123  CPPUNIT_TEST(test_back);
124  CPPUNIT_TEST(test_erase);
125  CPPUNIT_TEST(test_erase_if);
126  CPPUNIT_TEST(test_clear);
127  CPPUNIT_TEST(test_refToVstring);
128 
129  CPPUNIT_TEST_SUITE_END();
130 
131  private:
132  NameValue nv;
134  CORBA::Short st, rst;
135  CORBA::Long lg, rlg;
136  CORBA::Float ft, rft;
137  CORBA::Double dl, rdl;
138 
139  public:
144  {
145  }
146 
151  {
152  }
153 
157  virtual void setUp()
158  {
159  }
160 
164  virtual void tearDown()
165  {
166  }
167 
172  {
173  functor_for_each(vector<NameValue>& receivedNameValues)
174  : _receivedNameValues(receivedNameValues) {}
175 
176  bool operator()(const NameValue& nv) {
177  _receivedNameValues.push_back(nv);
178  return true;
179  }
180 
181  vector<NameValue>& _receivedNameValues;
182  };
183 
193  {
194  // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // for_each()を呼び出す vector<NameValue> receivedNameValues; CORBA_SeqUtil::for_each(nvlist, functor_for_each(receivedNameValues)); // ファンクタが呼び出された時に内部に記録したNameValueのベクタを取得し、期待値と比較する CPPUNIT_ASSERT_EQUAL(4, (int) receivedNameValues.size()); CPPUNIT_ASSERT_EQUAL((string) nvlist[0].name, (string) receivedNameValues[0].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[1].name, (string) receivedNameValues[1].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[2].name, (string) receivedNameValues[2].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[3].name, (string) receivedNameValues[3].name); CORBA::Short expectedShort, actualShort; nvlist[0].value >>= expectedShort; receivedNameValues[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL(expectedShort, actualShort); CORBA::Long expectedLong, actualLong; nvlist[1].value >>= expectedLong; receivedNameValues[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL(expectedLong, actualLong); CORBA::Float expectedFloat, actualFloat; nvlist[2].value >>= expectedFloat; receivedNameValues[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL(expectedFloat, actualFloat); CORBA::Double expectedDouble, actualDouble; nvlist[3].value >>= expectedDouble; receivedNameValues[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL(expectedDouble, actualDouble); } /*! * @brief test_find()で使用するファンクタ */ struct functor_find { functor_find(const string& name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == string(nv.name); } const string _name; }; /*! * @brief find()メソッドのテスト * * <ul> * <li>ファンクタを用いたNVList内の要素を正しく検索できるか?</li> * </ul> */ void test_find() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 名称"short"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 0, CORBA_SeqUtil::find(nvlist, functor_find("short"))); // 名称"long"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 1, CORBA_SeqUtil::find(nvlist, functor_find("long"))); // 名称"float"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 2, CORBA_SeqUtil::find(nvlist, functor_find("float"))); // 名称"double"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 3, CORBA_SeqUtil::find(nvlist, functor_find("double"))); } /*! * @brief push_back()メソッドのテスト * * <ul> * <li>push_backにより追加した各要素の内容が、それぞれ正しく格納されるか?</li> * </ul> */ void test_push_back() { NVList nvlist; // 1つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::push_back(nvlist, nvShort); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length()); // 2つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; CORBA_SeqUtil::push_back(nvlist, nvLong); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length()); // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
195  NVList nvlist;
196  nvlist.length(4);
197 
198  NameValue nvShort;
199  nvShort.name = "short";
200  nvShort.value <<= (CORBA::Short) 123;
201  nvlist[0] = nvShort;
202 
203  NameValue nvLong;
204  nvLong.name = "long";
205  nvLong.value <<= (CORBA::Long) 123456;
206  nvlist[1] = nvLong;
207 
208  NameValue nvFloat;
209  nvFloat.name = "float";
210  nvFloat.value <<= (CORBA::Float) 987.654;
211  nvlist[2] = nvFloat;
212 
213  NameValue nvDouble;
214  nvDouble.name = "double";
215  nvDouble.value <<= (CORBA::Double) 987654.321987;
216  nvlist[3] = nvDouble;
217 
218  // for_each()を呼び出す
219  vector<NameValue> receivedNameValues;
220  CORBA_SeqUtil::for_each(nvlist, functor_for_each(receivedNameValues));
221 
222  // ファンクタが呼び出された時に内部に記録したNameValueのベクタを取得し、期待値と比較する CPPUNIT_ASSERT_EQUAL(4, (int) receivedNameValues.size()); CPPUNIT_ASSERT_EQUAL((string) nvlist[0].name, (string) receivedNameValues[0].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[1].name, (string) receivedNameValues[1].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[2].name, (string) receivedNameValues[2].name); CPPUNIT_ASSERT_EQUAL((string) nvlist[3].name, (string) receivedNameValues[3].name); CORBA::Short expectedShort, actualShort; nvlist[0].value >>= expectedShort; receivedNameValues[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL(expectedShort, actualShort); CORBA::Long expectedLong, actualLong; nvlist[1].value >>= expectedLong; receivedNameValues[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL(expectedLong, actualLong); CORBA::Float expectedFloat, actualFloat; nvlist[2].value >>= expectedFloat; receivedNameValues[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL(expectedFloat, actualFloat); CORBA::Double expectedDouble, actualDouble; nvlist[3].value >>= expectedDouble; receivedNameValues[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL(expectedDouble, actualDouble); } /*! * @brief test_find()で使用するファンクタ */ struct functor_find { functor_find(const string& name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == string(nv.name); } const string _name; }; /*! * @brief find()メソッドのテスト * * <ul> * <li>ファンクタを用いたNVList内の要素を正しく検索できるか?</li> * </ul> */ void test_find() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 名称"short"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 0, CORBA_SeqUtil::find(nvlist, functor_find("short"))); // 名称"long"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 1, CORBA_SeqUtil::find(nvlist, functor_find("long"))); // 名称"float"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 2, CORBA_SeqUtil::find(nvlist, functor_find("float"))); // 名称"double"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 3, CORBA_SeqUtil::find(nvlist, functor_find("double"))); } /*! * @brief push_back()メソッドのテスト * * <ul> * <li>push_backにより追加した各要素の内容が、それぞれ正しく格納されるか?</li> * </ul> */ void test_push_back() { NVList nvlist; // 1つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::push_back(nvlist, nvShort); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length()); // 2つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; CORBA_SeqUtil::push_back(nvlist, nvLong); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length()); // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
223  CPPUNIT_ASSERT_EQUAL(4, (int) receivedNameValues.size());
224  CPPUNIT_ASSERT_EQUAL((string) nvlist[0].name, (string) receivedNameValues[0].name);
225  CPPUNIT_ASSERT_EQUAL((string) nvlist[1].name, (string) receivedNameValues[1].name);
226  CPPUNIT_ASSERT_EQUAL((string) nvlist[2].name, (string) receivedNameValues[2].name);
227  CPPUNIT_ASSERT_EQUAL((string) nvlist[3].name, (string) receivedNameValues[3].name);
228 
229  CORBA::Short expectedShort, actualShort;
230  nvlist[0].value >>= expectedShort;
231  receivedNameValues[0].value >>= actualShort;
232  CPPUNIT_ASSERT_EQUAL(expectedShort, actualShort);
233 
234  CORBA::Long expectedLong, actualLong;
235  nvlist[1].value >>= expectedLong;
236  receivedNameValues[1].value >>= actualLong;
237  CPPUNIT_ASSERT_EQUAL(expectedLong, actualLong);
238 
239  CORBA::Float expectedFloat, actualFloat;
240  nvlist[2].value >>= expectedFloat;
241  receivedNameValues[2].value >>= actualFloat;
242  CPPUNIT_ASSERT_EQUAL(expectedFloat, actualFloat);
243 
244  CORBA::Double expectedDouble, actualDouble;
245  nvlist[3].value >>= expectedDouble;
246  receivedNameValues[3].value >>= actualDouble;
247  CPPUNIT_ASSERT_EQUAL(expectedDouble, actualDouble);
248  }
249 
254  {
255  functor_find(const string& name)
256  : _name(name) {}
257 
258  bool operator()(const NameValue& nv) {
259  return _name == string(nv.name);
260  }
261 
262  const string _name;
263  };
264 
272  void test_find()
273  {
274  // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 名称"short"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 0, CORBA_SeqUtil::find(nvlist, functor_find("short"))); // 名称"long"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 1, CORBA_SeqUtil::find(nvlist, functor_find("long"))); // 名称"float"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 2, CORBA_SeqUtil::find(nvlist, functor_find("float"))); // 名称"double"を持つNameValueのインデクスを正しく検索できるか? CPPUNIT_ASSERT_EQUAL((CORBA::Long) 3, CORBA_SeqUtil::find(nvlist, functor_find("double"))); } /*! * @brief push_back()メソッドのテスト * * <ul> * <li>push_backにより追加した各要素の内容が、それぞれ正しく格納されるか?</li> * </ul> */ void test_push_back() { NVList nvlist; // 1つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::push_back(nvlist, nvShort); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length()); // 2つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; CORBA_SeqUtil::push_back(nvlist, nvLong); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length()); // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
275  NVList nvlist;
276  nvlist.length(4);
277 
278  NameValue nvShort;
279  nvShort.name = "short";
280  nvShort.value <<= (CORBA::Short) 123;
281  nvlist[0] = nvShort;
282 
283  NameValue nvLong;
284  nvLong.name = "long";
285  nvLong.value <<= (CORBA::Long) 123456;
286  nvlist[1] = nvLong;
287 
288  NameValue nvFloat;
289  nvFloat.name = "float";
290  nvFloat.value <<= (CORBA::Float) 987.654;
291  nvlist[2] = nvFloat;
292 
293  NameValue nvDouble;
294  nvDouble.name = "double";
295  nvDouble.value <<= (CORBA::Double) 987654.321987;
296  nvlist[3] = nvDouble;
297 
298  // 名称"short"を持つNameValueのインデクスを正しく検索できるか?
299  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 0, CORBA_SeqUtil::find(nvlist, functor_find("short")));
300 
301  // 名称"long"を持つNameValueのインデクスを正しく検索できるか?
302  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 1, CORBA_SeqUtil::find(nvlist, functor_find("long")));
303 
304  // 名称"float"を持つNameValueのインデクスを正しく検索できるか?
305  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 2, CORBA_SeqUtil::find(nvlist, functor_find("float")));
306 
307  // 名称"double"を持つNameValueのインデクスを正しく検索できるか?
308  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 3, CORBA_SeqUtil::find(nvlist, functor_find("double")));
309  }
310 
319  {
320  NVList nvlist;
321 
322  // 1つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::push_back(nvlist, nvShort); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length()); // 2つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; CORBA_SeqUtil::push_back(nvlist, nvLong); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length()); // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
323  NameValue nvShort;
324  nvShort.name = "short";
325  nvShort.value <<= (CORBA::Short) 123;
326  CORBA_SeqUtil::push_back(nvlist, nvShort);
327  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length());
328 
329  // 2つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; CORBA_SeqUtil::push_back(nvlist, nvLong); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length()); // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
330  NameValue nvLong;
331  nvLong.name = "long";
332  nvLong.value <<= (CORBA::Long) 123456;
333  CORBA_SeqUtil::push_back(nvlist, nvLong);
334  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length());
335 
336  // 3つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::push_back(nvlist, nvFloat); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length()); // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
337  NameValue nvFloat;
338  nvFloat.name = "float";
339  nvFloat.value <<= (CORBA::Float) 987.654;
340  CORBA_SeqUtil::push_back(nvlist, nvFloat);
341  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length());
342 
343  // 4つめの要素をpush_backして、要素数が正しいかチェックする NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::push_back(nvlist, nvDouble); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length()); // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
344  NameValue nvDouble;
345  nvDouble.name = "double";
346  nvDouble.value <<= (CORBA::Double) 987654.321987;
347  CORBA_SeqUtil::push_back(nvlist, nvDouble);
348  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length());
349 
350  // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief push_back_list()メソッドのテスト * * <ul> * <li>一方のNVListの内容を、他方のNVListの後ろに正しくアペンドできるか?</li> * </ul> */ void test_push_back_list() { // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
351  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name);
352  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name);
353  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name);
354  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name);
355 
356  CORBA::Short actualShort;
357  nvlist[0].value >>= actualShort;
358  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
359 
360  CORBA::Long actualLong;
361  nvlist[1].value >>= actualLong;
362  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
363 
364  CORBA::Float actualFloat;
365  nvlist[2].value >>= actualFloat;
366  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
367 
368  CORBA::Double actualDouble;
369  nvlist[3].value >>= actualDouble;
370  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
371  }
372 
381  {
382  // 1つめのNVListを作成する NVList nvlist1; nvlist1.length(2); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
383  NVList nvlist1;
384  nvlist1.length(2);
385 
386  NameValue nvShort;
387  nvShort.name = "short";
388  nvShort.value <<= (CORBA::Short) 123;
389  nvlist1[0] = nvShort;
390 
391  NameValue nvLong;
392  nvLong.name = "long";
393  nvLong.value <<= (CORBA::Long) 123456;
394  nvlist1[1] = nvLong;
395 
396  // 2つめのNVListを作成する NVList nvlist2; nvlist2.length(2); NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist2[0] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist2[1] = nvDouble; // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
397  NVList nvlist2;
398  nvlist2.length(2);
399 
400  NameValue nvFloat;
401  nvFloat.name = "float";
402  nvFloat.value <<= (CORBA::Float) 987.654;
403  nvlist2[0] = nvFloat;
404 
405  NameValue nvDouble;
406  nvDouble.name = "double";
407  nvDouble.value <<= (CORBA::Double) 987654.321987;
408  nvlist2[1] = nvDouble;
409 
410  // push_back_listして、1つめのNVListに2つめのNVListをアペンドする CORBA_SeqUtil::push_back_list(nvlist1, nvlist2); // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
411  CORBA_SeqUtil::push_back_list(nvlist1, nvlist2);
412 
413  // 正しくアペンドされたことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort; nvlist1[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist1[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist1[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist1[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief insert()メソッドのテスト * * <ul> * <li>先頭位置への挿入を正しく行えるか?</li> * <li>最後尾位置への挿入を正しく行えるか?</li> * <li>中間位置への挿入を正しく行えるか?</li> * </ul> */ void test_insert() { // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
414  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name);
415  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name);
416  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name);
417  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name);
418 
419  CORBA::Short actualShort;
420  nvlist1[0].value >>= actualShort;
421  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
422 
423  CORBA::Long actualLong;
424  nvlist1[1].value >>= actualLong;
425  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
426 
427  CORBA::Float actualFloat;
428  nvlist1[2].value >>= actualFloat;
429  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
430 
431  CORBA::Double actualDouble;
432  nvlist1[3].value >>= actualDouble;
433  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
434  }
435 
445  void test_insert()
446  {
447  // 挿入対象となるNVListを作成する NVList nvlist; nvlist.length(1); NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[0] = nvLong; // (1) 先頭への挿入を行う NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; CORBA_SeqUtil::insert(nvlist, nvShort, 0); // (2) 最後尾への挿入を行う NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; CORBA_SeqUtil::insert(nvlist, nvDouble, 2); // (3) 中間への挿入を行う NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; CORBA_SeqUtil::insert(nvlist, nvFloat, 2); // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
448  NVList nvlist;
449  nvlist.length(1);
450 
451  NameValue nvLong;
452  nvLong.name = "long";
453  nvLong.value <<= (CORBA::Long) 123456;
454  nvlist[0] = nvLong;
455 
456  // (1) 先頭への挿入を行う
457  NameValue nvShort;
458  nvShort.name = "short";
459  nvShort.value <<= (CORBA::Short) 123;
460  CORBA_SeqUtil::insert(nvlist, nvShort, 0);
461 
462  // (2) 最後尾への挿入を行う
463  NameValue nvDouble;
464  nvDouble.name = "double";
465  nvDouble.value <<= (CORBA::Double) 987654.321987;
466  CORBA_SeqUtil::insert(nvlist, nvDouble, 2);
467 
468  // (3) 中間への挿入を行う
469  NameValue nvFloat;
470  nvFloat.name = "float";
471  nvFloat.value <<= (CORBA::Float) 987.654;
472  CORBA_SeqUtil::insert(nvlist, nvFloat, 2);
473 
474  // 挿入結果が正しいことを確認する CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name); CORBA::Short actualShort; nvlist[0].value >>= actualShort; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort); CORBA::Long actualLong; nvlist[1].value >>= actualLong; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong); CORBA::Float actualFloat; nvlist[2].value >>= actualFloat; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat); CORBA::Double actualDouble; nvlist[3].value >>= actualDouble; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble); } /*! * @brief front()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?</li> * </ul> */ void test_front() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか NVList nvlistEmpty; try { CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
475  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name);
476  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name);
477  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name);
478  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name);
479 
480  CORBA::Short actualShort;
481  nvlist[0].value >>= actualShort;
482  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
483 
484  CORBA::Long actualLong;
485  nvlist[1].value >>= actualLong;
486  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
487 
488  CORBA::Float actualFloat;
489  nvlist[2].value >>= actualFloat;
490  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
491 
492  CORBA::Double actualDouble;
493  nvlist[3].value >>= actualDouble;
494  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
495  }
496 
505  void test_front()
506  {
507  // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか
508  NVList nvlistEmpty;
509  try {
510  CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty);
511  CPPUNIT_FAIL("Exception must be thrown.");
512  } catch (...) {
513  // expected
514  }
515 
516  // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?
517  NVList nvlist;
518  nvlist.length(4);
519 
520  NameValue nvShort;
521  nvShort.name = "short";
522  nvShort.value <<= (CORBA::Short) 123;
523  nvlist[0] = nvShort;
524 
525  NameValue nvLong;
526  nvLong.name = "long";
527  nvLong.value <<= (CORBA::Long) 123456;
528  nvlist[1] = nvLong;
529 
530  NameValue nvFloat;
531  nvFloat.name = "float";
532  nvFloat.value <<= (CORBA::Float) 987.654;
533  nvlist[2] = nvFloat;
534 
535  NameValue nvDouble;
536  nvDouble.name = "double";
537  nvDouble.value <<= (CORBA::Double) 987654.321987;
538  nvlist[3] = nvDouble;
539 
540  // 先頭の要素を取得して、期待値と比較してチェックする NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name); CORBA::Short actualValue; nvFront.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue); } /*! * @brief back()メソッドのテスト * * <ul> * <li>取得対象のNVListの要素数が0の場合、例外がスローされるか?</li> * <li>取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?</li> * </ul> */ void test_back() { // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか? NVList nvlistEmpty; try { CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty); CPPUNIT_FAIL("Exception must be thrown."); } catch (...) { // expected } // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか? NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
541  NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist);
542 
543  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name);
544 
545  CORBA::Short actualValue;
546  nvFront.value >>= actualValue;
547  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue);
548  }
549 
558  void test_back()
559  {
560  // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか?
561  NVList nvlistEmpty;
562  try {
563  CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty);
564  CPPUNIT_FAIL("Exception must be thrown.");
565  } catch (...) {
566  // expected
567  }
568 
569  // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?
570  NVList nvlist;
571  nvlist.length(4);
572 
573  NameValue nvShort;
574  nvShort.name = "short";
575  nvShort.value <<= (CORBA::Short) 123;
576  nvlist[0] = nvShort;
577 
578  NameValue nvLong;
579  nvLong.name = "long";
580  nvLong.value <<= (CORBA::Long) 123456;
581  nvlist[1] = nvLong;
582 
583  NameValue nvFloat;
584  nvFloat.name = "float";
585  nvFloat.value <<= (CORBA::Float) 987.654;
586  nvlist[2] = nvFloat;
587 
588  NameValue nvDouble;
589  nvDouble.name = "double";
590  nvDouble.value <<= (CORBA::Double) 987654.321987;
591  nvlist[3] = nvDouble;
592 
593  // 最後の要素を取得して、期待値と比較してチェックする NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name); CORBA::Double actualValue; nvBack.value >>= actualValue; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue); } /*! * @brief erase()メソッドのテスト * * <ul> * <li>先頭の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>中間の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * <li>最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?</li> * </ul> */ void test_erase() { // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
594  NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist);
595 
596  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name);
597 
598  CORBA::Double actualValue;
599  nvBack.value >>= actualValue;
600  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue);
601  }
602 
612  void test_erase()
613  {
614  // テスト用のNVListを作成する NVList nvlist1, nvlist2, nvlist3; nvlist1.length(4); nvlist2.length(4); nvlist3.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; nvlist3[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; nvlist3[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; nvlist3[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; nvlist3[3] = nvDouble; // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist1, 0); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length()); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name); CORBA::Long actualLong1; nvlist1[0].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[1].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[2].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist2, 1); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Float actualFloat2; nvlist2[1].value >>= actualFloat2; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか? CORBA_SeqUtil::erase(nvlist3, 3); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length()); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name); CORBA::Short actualShort3; nvlist3[0].value >>= actualShort3; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3); CORBA::Long actualLong3; nvlist3[1].value >>= actualLong3; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3); CORBA::Float actualFloat3; nvlist3[2].value >>= actualFloat3; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3); } /*! * @brief test_erase_if()で使用するファンクタ */ struct functor_erase_if { functor_erase_if(const char* name) : _name(name) {} bool operator()(const NameValue& nv) { return _name == std::string(nv.name); } string _name; }; /*! * @brief erase_if()メソッドのテスト * * <ul> * <li>条件に合致する要素がない場合、何も削除されずに保たれるか?</li> * <li>条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?</li> * </ul> */ void test_erase_if() { NVList nvlist1, nvlist2; nvlist1.length(4); nvlist2.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist1[0] = nvShort; nvlist2[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist1[1] = nvLong; nvlist2[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist1[2] = nvFloat; nvlist2[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist1[3] = nvDouble; nvlist2[3] = nvDouble; // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか? CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name); CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name); CORBA::Short actualShort1; nvlist1[0].value >>= actualShort1; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1); CORBA::Long actualLong1; nvlist1[1].value >>= actualLong1; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1); CORBA::Float actualFloat1; nvlist1[2].value >>= actualFloat1; CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1); CORBA::Double actualDouble1; nvlist1[3].value >>= actualDouble1; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1); // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか? CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float")); CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name); CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name); CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name); CORBA::Short actualShort2; nvlist2[0].value >>= actualShort2; CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2); CORBA::Long actualLong2; nvlist2[1].value >>= actualLong2; CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2); CORBA::Double actualDouble2; nvlist2[2].value >>= actualDouble2; CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2); } /*! * @brief clear()メソッドのテスト * * <ul> * <li>クリアにより要素数が0になるか?</li> * </ul> */ void test_clear() { // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
615  NVList nvlist1, nvlist2, nvlist3;
616  nvlist1.length(4);
617  nvlist2.length(4);
618  nvlist3.length(4);
619 
620  NameValue nvShort;
621  nvShort.name = "short";
622  nvShort.value <<= (CORBA::Short) 123;
623  nvlist1[0] = nvShort;
624  nvlist2[0] = nvShort;
625  nvlist3[0] = nvShort;
626 
627  NameValue nvLong;
628  nvLong.name = "long";
629  nvLong.value <<= (CORBA::Long) 123456;
630  nvlist1[1] = nvLong;
631  nvlist2[1] = nvLong;
632  nvlist3[1] = nvLong;
633 
634  NameValue nvFloat;
635  nvFloat.name = "float";
636  nvFloat.value <<= (CORBA::Float) 987.654;
637  nvlist1[2] = nvFloat;
638  nvlist2[2] = nvFloat;
639  nvlist3[2] = nvFloat;
640 
641  NameValue nvDouble;
642  nvDouble.name = "double";
643  nvDouble.value <<= (CORBA::Double) 987654.321987;
644  nvlist1[3] = nvDouble;
645  nvlist2[3] = nvDouble;
646  nvlist3[3] = nvDouble;
647 
648  // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか?
649  CORBA_SeqUtil::erase(nvlist1, 0);
650  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length());
651  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name);
652  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name);
653  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name);
654 
655  CORBA::Long actualLong1;
656  nvlist1[0].value >>= actualLong1;
657  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1);
658 
659  CORBA::Float actualFloat1;
660  nvlist1[1].value >>= actualFloat1;
661  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1);
662 
663  CORBA::Double actualDouble1;
664  nvlist1[2].value >>= actualDouble1;
665  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1);
666 
667  // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか?
668  CORBA_SeqUtil::erase(nvlist2, 1);
669  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length());
670  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name);
671  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name);
672  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name);
673 
674  CORBA::Short actualShort2;
675  nvlist2[0].value >>= actualShort2;
676  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2);
677 
678  CORBA::Float actualFloat2;
679  nvlist2[1].value >>= actualFloat2;
680  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2);
681 
682  CORBA::Double actualDouble2;
683  nvlist2[2].value >>= actualDouble2;
684  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2);
685 
686  // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?
687  CORBA_SeqUtil::erase(nvlist3, 3);
688  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length());
689  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name);
690  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name);
691  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name);
692 
693  CORBA::Short actualShort3;
694  nvlist3[0].value >>= actualShort3;
695  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3);
696 
697  CORBA::Long actualLong3;
698  nvlist3[1].value >>= actualLong3;
699  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3);
700 
701  CORBA::Float actualFloat3;
702  nvlist3[2].value >>= actualFloat3;
703  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3);
704  }
705 
710  {
711  functor_erase_if(const char* name)
712  : _name(name) {}
713 
714  bool operator()(const NameValue& nv)
715  {
716  return _name == std::string(nv.name);
717  }
718 
719  string _name;
720  };
721 
731  {
732  NVList nvlist1, nvlist2;
733  nvlist1.length(4);
734  nvlist2.length(4);
735 
736  NameValue nvShort;
737  nvShort.name = "short";
738  nvShort.value <<= (CORBA::Short) 123;
739  nvlist1[0] = nvShort;
740  nvlist2[0] = nvShort;
741 
742  NameValue nvLong;
743  nvLong.name = "long";
744  nvLong.value <<= (CORBA::Long) 123456;
745  nvlist1[1] = nvLong;
746  nvlist2[1] = nvLong;
747 
748  NameValue nvFloat;
749  nvFloat.name = "float";
750  nvFloat.value <<= (CORBA::Float) 987.654;
751  nvlist1[2] = nvFloat;
752  nvlist2[2] = nvFloat;
753 
754  NameValue nvDouble;
755  nvDouble.name = "double";
756  nvDouble.value <<= (CORBA::Double) 987654.321987;
757  nvlist1[3] = nvDouble;
758  nvlist2[3] = nvDouble;
759 
760  // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか?
761  CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name"));
762 
763  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name);
764  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name);
765  CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name);
766  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name);
767 
768  CORBA::Short actualShort1;
769  nvlist1[0].value >>= actualShort1;
770  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1);
771 
772  CORBA::Long actualLong1;
773  nvlist1[1].value >>= actualLong1;
774  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1);
775 
776  CORBA::Float actualFloat1;
777  nvlist1[2].value >>= actualFloat1;
778  CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1);
779 
780  CORBA::Double actualDouble1;
781  nvlist1[3].value >>= actualDouble1;
782  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1);
783 
784  // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?
785  CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float"));
786 
787  CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name);
788  CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name);
789  CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name);
790 
791  CORBA::Short actualShort2;
792  nvlist2[0].value >>= actualShort2;
793  CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2);
794 
795  CORBA::Long actualLong2;
796  nvlist2[1].value >>= actualLong2;
797  CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2);
798 
799  CORBA::Double actualDouble2;
800  nvlist2[2].value >>= actualDouble2;
801  CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2);
802  }
803 
811  void test_clear()
812  {
813  // テスト用のNVListを作成する NVList nvlist; nvlist.length(4); NameValue nvShort; nvShort.name = "short"; nvShort.value <<= (CORBA::Short) 123; nvlist[0] = nvShort; NameValue nvLong; nvLong.name = "long"; nvLong.value <<= (CORBA::Long) 123456; nvlist[1] = nvLong; NameValue nvFloat; nvFloat.name = "float"; nvFloat.value <<= (CORBA::Float) 987.654; nvlist[2] = nvFloat; NameValue nvDouble; nvDouble.name = "double"; nvDouble.value <<= (CORBA::Double) 987654.321987; nvlist[3] = nvDouble; // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
814  NVList nvlist;
815  nvlist.length(4);
816 
817  NameValue nvShort;
818  nvShort.name = "short";
819  nvShort.value <<= (CORBA::Short) 123;
820  nvlist[0] = nvShort;
821 
822  NameValue nvLong;
823  nvLong.name = "long";
824  nvLong.value <<= (CORBA::Long) 123456;
825  nvlist[1] = nvLong;
826 
827  NameValue nvFloat;
828  nvFloat.name = "float";
829  nvFloat.value <<= (CORBA::Float) 987.654;
830  nvlist[2] = nvFloat;
831 
832  NameValue nvDouble;
833  nvDouble.name = "double";
834  nvDouble.value <<= (CORBA::Double) 987654.321987;
835  nvlist[3] = nvDouble;
836 
837  // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する CORBA_SeqUtil::clear(nvlist); CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length()); } /*! * @brief refToVstring()メソッドのテスト * * <ul> * <li>オブジェクトリファレンスの文字列が返されるか?</li> * </ul> */ void test_refToVstring() { int argc(0); char** argv(NULL); CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv); PortableServer::POA_ptr m_pPOA = PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA")); m_pPOA->the_POAManager()->activate(); RTC::PortAdmin portAdmin(m_pORB, m_pPOA); PortMock* port0 = new PortMock(); port0->setName("port0"); CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0)); RTC::PortService_var portRef0 = portAdmin.getPortRef("port0"); RTC::ConnectorProfile cprof; cprof.connector_id = "id"; cprof.name = CORBA::string_dup("Test"); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.interface_type", "corba_cdr")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.dataflow_type", "push")); CORBA_SeqUtil::push_back(cprof.properties, NVUtil::newNV("dataport.subscription_type", "flush")); cprof.ports.length(1); cprof.ports[0] = portRef0; coil::vstring str; str = CORBA_SeqUtil::refToVstring(cprof.ports); int len = str.size(); int pos = str[0].find("IOR", 0); CPPUNIT_ASSERT_EQUAL(1, len); CPPUNIT_ASSERT(pos != string::npos); // delete port0; // PortBase デストラクタで、deactivate_object を実行しています。 } }; }; // namespace CORBA_SeqUtil /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // CORBA_SeqUtil_cpp
838  CORBA_SeqUtil::clear(nvlist);
839  CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length());
840  }
841 
850  {
851  int argc(0);
852  char** argv(NULL);
853  CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv);
854  PortableServer::POA_ptr m_pPOA =
855  PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA"));
856  m_pPOA->the_POAManager()->activate();
857 
858  RTC::PortAdmin portAdmin(m_pORB, m_pPOA);
859  PortMock* port0 = new PortMock();
860  port0->setName("port0");
861  CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
862 
863  RTC::PortService_var portRef0 = portAdmin.getPortRef("port0");
864  RTC::ConnectorProfile cprof;
865  cprof.connector_id = "id";
866  cprof.name = CORBA::string_dup("Test");
867 
868  CORBA_SeqUtil::push_back(cprof.properties,
869  NVUtil::newNV("dataport.interface_type",
870  "corba_cdr"));
871  CORBA_SeqUtil::push_back(cprof.properties,
872  NVUtil::newNV("dataport.dataflow_type",
873  "push"));
874  CORBA_SeqUtil::push_back(cprof.properties,
875  NVUtil::newNV("dataport.subscription_type",
876  "flush"));
877  cprof.ports.length(1);
878  cprof.ports[0] = portRef0;
879  coil::vstring str;
880  str = CORBA_SeqUtil::refToVstring(cprof.ports);
881  int len = str.size();
882  int pos = str[0].find("IOR", 0);
883  CPPUNIT_ASSERT_EQUAL(1, len);
884  CPPUNIT_ASSERT(pos != string::npos);
885 
886 // delete port0;
887  // PortBase デストラクタで、deactivate_object を実行しています。
888  }
889 
890  };
891 }; // namespace CORBA_SeqUtil
892 
893 /*
894  * Register test suite
895  */
897 
898 #ifdef LOCAL_MAIN
899 int main(int argc, char* argv[])
900 {
901 
902  FORMAT format = TEXT_OUT;
903  int target = 0;
904  std::string xsl;
905  std::string ns;
906  std::string fname;
907  std::ofstream ofs;
908 
909  int i(1);
910  while (i < argc)
911  {
912  std::string arg(argv[i]);
913  std::string next_arg;
914  if (i + 1 < argc) next_arg = argv[i + 1];
915  else next_arg = "";
916 
917  if (arg == "--text") { format = TEXT_OUT; break; }
918  if (arg == "--xml")
919  {
920  if (next_arg == "")
921  {
922  fname = argv[0];
923  fname += ".xml";
924  }
925  else
926  {
927  fname = next_arg;
928  }
929  format = XML_OUT;
930  ofs.open(fname.c_str());
931  }
932  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
933  if ( arg == "--cerr" ) { target = 1; break; }
934  if ( arg == "--xsl" )
935  {
936  if (next_arg == "") xsl = "default.xsl";
937  else xsl = next_arg;
938  }
939  if ( arg == "--namespace" )
940  {
941  if (next_arg == "")
942  {
943  std::cerr << "no namespace specified" << std::endl;
944  exit(1);
945  }
946  else
947  {
948  xsl = next_arg;
949  }
950  }
951  ++i;
952  }
953  CppUnit::TextUi::TestRunner runner;
954  if ( ns.empty() )
955  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
956  else
957  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
958  CppUnit::Outputter* outputter = 0;
959  std::ostream* stream = target ? &std::cerr : &std::cout;
960  switch ( format )
961  {
962  case TEXT_OUT :
963  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
964  break;
965  case XML_OUT :
966  std::cout << "XML_OUT" << std::endl;
967  outputter = new CppUnit::XmlOutputter(&runner.result(),
968  ofs, "shift_jis");
969  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
970  break;
971  case COMPILER_OUT :
972  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
973  break;
974  }
975  runner.setOutputter(outputter);
976  runner.run();
977  return 0; // runner.run() ? 0 : 1;
978 }
979 
980 #endif // MAIN
981 #endif // CORBA_SeqUtil_cpp
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
void test_push_back()
push_back()メソッドのテスト
virtual RTC::ReturnCode_t subscribeInterfaces(const RTC::ConnectorProfile &)
int main(int argc, char **argv)
void erase(CorbaSequence &seq, CORBA::ULong index)
Erase the element of the specified index.
void clear(CorbaSequence &seq)
Erase all the elements of the CORBA sequence.
test_find()で使用するファンクタ
ReturnCode_t
Definition: doil.h:53
std::vector< std::pair< std::string, std::string > > NVList
Definition: IRTC.h:67
virtual void deactivateInterfaces()
Deactivate all Port interfaces.
void test_for_each()
for_each()メソッドのテスト
void test_erase_if()
erase_if()メソッドのテスト
PortService_ptr getPortRef(const char *port_name) const
Get the reference to Port object.
Definition: PortAdmin.cpp:161
void test_insert()
insert()メソッドのテスト
void test_push_back_list()
push_back_list()メソッドのテスト
PortAdmin class.
Definition: PortAdmin.h:52
std::vector< std::string > vstring
Definition: stringutil.h:37
void log(const std::string &msg)
Connector base class.
virtual RTC::ReturnCode_t publishInterfaces(RTC::ConnectorProfile &)
virtual void setUp()
Test initialization.
Port base class.
Definition: PortBase.h:134
void insert(CorbaSequence &seq, SequenceElement &elem, CORBA::ULong index)
Insert the element to the CORBA sequence.
CORBA::Long find(const CorbaSequence &seq, Functor f)
Return the index of CORBA sequence element that functor matches.
virtual void tearDown()
Test finalization.
NameValue and NVList utility functions.
void test_clear()
clear()メソッドのテスト
CORBA sequence utility template functions.
void erase_if(CorbaSequence &seq, Functor f)
Remove an element of a sequence according to a predicate.
void test_back()
back()メソッドのテスト
virtual void activateInterfaces()
Activate all Port interfaces.
CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests)
functor_for_each(vector< NameValue > &receivedNameValues)
void test_find()
find()メソッドのテスト
int countLog(const std::string &msg)
std::vector< std::string > m_log
bool addPort(PortBase &port)
Regsiter the Port.
Definition: PortAdmin.cpp:191
void push_back_list(CorbaSequence &seq1, const CorbaSequence &seq2)
Merge the elements of the CORBA sequence.
void test_refToVstring()
refToVstring()メソッドのテスト
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
test_erase_if()で使用するファンクタ
void test_front()
front()メソッドのテスト
void test_erase()
erase()メソッドのテスト
test_for_each()で使用するファンクタ
coil::vstring refToVstring(const CorbaRefSequence &objlist)
CORBA IOR helper functions.
virtual void unsubscribeInterfaces(const RTC::ConnectorProfile &)
void setName(const char *name)
Set the name of this Port.
Definition: PortBase.cpp:488
Functor for_each(CorbaSequence &seq, Functor f)
Apply the functor to all CORBA sequence elements.
Definition: CORBA_SeqUtil.h:98
RTC&#39;s Port administration class.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58