CORBA_SeqUtilTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: CORBA_SeqUtilTests.cpp,v $
00023  * Revision 1.2  2008/01/10 11:02:06  arafune
00024  * *** empty log message ***
00025  *
00026  * Revision 1.1  2007/12/20 07:50:18  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.1  2006/11/27 08:26:10  n-ando
00030  * TestSuites are devided into each directory.
00031  *
00032  * Revision 1.1  2006/10/31 13:10:51  kurihara
00033  *
00034  * test program for CORBA_SeqUtil.h
00035  *
00036  */
00037 
00038 #ifndef CORBA_SeqUtil_cpp
00039 #define CORBA_SeqUtil_cpp
00040 
00041 #include <cppunit/ui/text/TestRunner.h>
00042 #include <cppunit/TextOutputter.h>
00043 #include <cppunit/extensions/TestFactoryRegistry.h>
00044 #include <cppunit/extensions/HelperMacros.h>
00045 #include <cppunit/TestAssert.h>
00046 
00047 #include <rtm/CORBA_SeqUtil.h>
00048 #include <rtm/NVUtil.h>
00049 #include <rtm/PortAdmin.h>
00050 #include <rtm/ConnectorBase.h>
00051 #include "SeqUtilTestsSkel.h"
00052 
00057 namespace CORBA_SeqUtil
00058 {
00059 
00060   class Logger
00061   {
00062   public:
00063     void log(const std::string& msg)
00064     {
00065       m_log.push_back(msg);
00066     }
00067 
00068     int countLog(const std::string& msg)
00069     {
00070       int count = 0;
00071       for (int i = 0; i < (int) m_log.size(); ++i)
00072         {
00073           if (m_log[i] == msg) ++count;
00074         }
00075       return count;
00076     }
00077                 
00078     void clearLog(void)
00079     {
00080         m_log.clear();
00081     }
00082   private:
00083     std::vector<std::string> m_log;
00084   };
00085 
00086   class PortMock
00087     : public RTC::PortBase
00088   {
00089   protected:
00090     virtual RTC::ReturnCode_t publishInterfaces(RTC::ConnectorProfile&)
00091     {
00092       return RTC::RTC_OK;
00093     }
00094     virtual RTC::ReturnCode_t subscribeInterfaces(const RTC::ConnectorProfile&)
00095     {
00096       return RTC::RTC_OK;
00097     }
00098     virtual void unsubscribeInterfaces(const RTC::ConnectorProfile&)
00099     {
00100     }
00101     virtual void activateInterfaces()
00102     {
00103     }
00104     virtual void deactivateInterfaces()
00105     {
00106     }
00107   };
00108 
00109 
00110   using namespace std;
00111   
00112   class CORBA_SeqUtilTests
00113     : public CppUnit::TestFixture
00114   {
00115     CPPUNIT_TEST_SUITE(CORBA_SeqUtilTests);
00116                 
00117     CPPUNIT_TEST(test_for_each);
00118     CPPUNIT_TEST(test_find);
00119     CPPUNIT_TEST(test_push_back);
00120     CPPUNIT_TEST(test_push_back_list);
00121     CPPUNIT_TEST(test_insert);
00122     CPPUNIT_TEST(test_front);
00123     CPPUNIT_TEST(test_back);
00124     CPPUNIT_TEST(test_erase);
00125     CPPUNIT_TEST(test_erase_if);
00126     CPPUNIT_TEST(test_clear);
00127     CPPUNIT_TEST(test_refToVstring);
00128 
00129     CPPUNIT_TEST_SUITE_END();
00130                 
00131   private:
00132     NameValue nv;
00133     NVList nvlist;
00134     CORBA::Short  st, rst;
00135     CORBA::Long   lg, rlg;
00136     CORBA::Float  ft, rft;
00137     CORBA::Double dl, rdl;
00138                 
00139   public:
00143     CORBA_SeqUtilTests()
00144     {
00145     }
00146                     
00150     ~CORBA_SeqUtilTests()
00151     {
00152     }
00153                 
00157     virtual void setUp()
00158     {
00159     }
00160                 
00164     virtual void tearDown()
00165     { 
00166     }
00167                 
00171     struct functor_for_each
00172     {
00173       functor_for_each(vector<NameValue>& receivedNameValues)
00174         : _receivedNameValues(receivedNameValues) {}
00175                         
00176       bool operator()(const NameValue& nv) {
00177         _receivedNameValues.push_back(nv);
00178         return true;
00179       }
00180                         
00181       vector<NameValue>& _receivedNameValues;
00182     };
00183                 
00192     void test_for_each()
00193     {
00194       // テスト用のNVListを作成する
00195       NVList nvlist;
00196       nvlist.length(4);
00197                         
00198       NameValue nvShort;
00199       nvShort.name = "short";
00200       nvShort.value <<= (CORBA::Short) 123;
00201       nvlist[0] = nvShort;
00202                         
00203       NameValue nvLong;
00204       nvLong.name = "long";
00205       nvLong.value <<= (CORBA::Long) 123456;
00206       nvlist[1] = nvLong;
00207                         
00208       NameValue nvFloat;
00209       nvFloat.name = "float";
00210       nvFloat.value <<= (CORBA::Float) 987.654;
00211       nvlist[2] = nvFloat;
00212                         
00213       NameValue nvDouble;
00214       nvDouble.name = "double";
00215       nvDouble.value <<= (CORBA::Double) 987654.321987;
00216       nvlist[3] = nvDouble;
00217                         
00218       // for_each()を呼び出す
00219       vector<NameValue> receivedNameValues;
00220       CORBA_SeqUtil::for_each(nvlist, functor_for_each(receivedNameValues));
00221                         
00222       // ファンクタが呼び出された時に内部に記録したNameValueのベクタを取得し、期待値と比較する
00223       CPPUNIT_ASSERT_EQUAL(4, (int) receivedNameValues.size());
00224       CPPUNIT_ASSERT_EQUAL((string) nvlist[0].name, (string) receivedNameValues[0].name);
00225       CPPUNIT_ASSERT_EQUAL((string) nvlist[1].name, (string) receivedNameValues[1].name);
00226       CPPUNIT_ASSERT_EQUAL((string) nvlist[2].name, (string) receivedNameValues[2].name);
00227       CPPUNIT_ASSERT_EQUAL((string) nvlist[3].name, (string) receivedNameValues[3].name);
00228                         
00229       CORBA::Short expectedShort, actualShort;
00230       nvlist[0].value >>= expectedShort;
00231       receivedNameValues[0].value >>= actualShort;
00232       CPPUNIT_ASSERT_EQUAL(expectedShort, actualShort);
00233                         
00234       CORBA::Long expectedLong, actualLong;
00235       nvlist[1].value >>= expectedLong;
00236       receivedNameValues[1].value >>= actualLong;
00237       CPPUNIT_ASSERT_EQUAL(expectedLong, actualLong);
00238 
00239       CORBA::Float expectedFloat, actualFloat;
00240       nvlist[2].value >>= expectedFloat;
00241       receivedNameValues[2].value >>= actualFloat;
00242       CPPUNIT_ASSERT_EQUAL(expectedFloat, actualFloat);
00243 
00244       CORBA::Double expectedDouble, actualDouble;
00245       nvlist[3].value >>= expectedDouble;
00246       receivedNameValues[3].value >>= actualDouble;
00247       CPPUNIT_ASSERT_EQUAL(expectedDouble, actualDouble);
00248     }
00249                 
00253     struct functor_find
00254     {
00255       functor_find(const string& name)
00256         : _name(name) {}
00257                         
00258       bool operator()(const NameValue& nv) {
00259         return _name == string(nv.name);
00260       }
00261                         
00262       const string _name;
00263     };
00264                 
00272     void test_find()
00273     {
00274       // テスト用のNVListを作成する
00275       NVList nvlist;
00276       nvlist.length(4);
00277                         
00278       NameValue nvShort;
00279       nvShort.name = "short";
00280       nvShort.value <<= (CORBA::Short) 123;
00281       nvlist[0] = nvShort;
00282                         
00283       NameValue nvLong;
00284       nvLong.name = "long";
00285       nvLong.value <<= (CORBA::Long) 123456;
00286       nvlist[1] = nvLong;
00287                         
00288       NameValue nvFloat;
00289       nvFloat.name = "float";
00290       nvFloat.value <<= (CORBA::Float) 987.654;
00291       nvlist[2] = nvFloat;
00292                         
00293       NameValue nvDouble;
00294       nvDouble.name = "double";
00295       nvDouble.value <<= (CORBA::Double) 987654.321987;
00296       nvlist[3] = nvDouble;
00297 
00298       // 名称"short"を持つNameValueのインデクスを正しく検索できるか?
00299       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 0, CORBA_SeqUtil::find(nvlist, functor_find("short")));
00300                         
00301       // 名称"long"を持つNameValueのインデクスを正しく検索できるか?
00302       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 1, CORBA_SeqUtil::find(nvlist, functor_find("long")));
00303 
00304       // 名称"float"を持つNameValueのインデクスを正しく検索できるか?
00305       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 2, CORBA_SeqUtil::find(nvlist, functor_find("float")));
00306                         
00307       // 名称"double"を持つNameValueのインデクスを正しく検索できるか?
00308       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 3, CORBA_SeqUtil::find(nvlist, functor_find("double")));
00309     }
00310 
00318     void test_push_back()
00319     {
00320       NVList nvlist;
00321 
00322       // 1つめの要素をpush_backして、要素数が正しいかチェックする
00323       NameValue nvShort;
00324       nvShort.name = "short";
00325       nvShort.value <<= (CORBA::Short) 123;
00326       CORBA_SeqUtil::push_back(nvlist, nvShort);
00327       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 1, nvlist.length());
00328                         
00329       // 2つめの要素をpush_backして、要素数が正しいかチェックする
00330       NameValue nvLong;
00331       nvLong.name = "long";
00332       nvLong.value <<= (CORBA::Long) 123456;
00333       CORBA_SeqUtil::push_back(nvlist, nvLong);
00334       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 2, nvlist.length());
00335                         
00336       // 3つめの要素をpush_backして、要素数が正しいかチェックする
00337       NameValue nvFloat;
00338       nvFloat.name = "float";
00339       nvFloat.value <<= (CORBA::Float) 987.654;
00340       CORBA_SeqUtil::push_back(nvlist, nvFloat);
00341       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist.length());
00342                         
00343       // 4つめの要素をpush_backして、要素数が正しいかチェックする
00344       NameValue nvDouble;
00345       nvDouble.name = "double";
00346       nvDouble.value <<= (CORBA::Double) 987654.321987;
00347       CORBA_SeqUtil::push_back(nvlist, nvDouble);
00348       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 4, nvlist.length());
00349                         
00350       // push_backした各要素の内容が、NVList内に正しく格納されていることを確認する
00351       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name);
00352       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name);
00353       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name);
00354       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name);
00355                         
00356       CORBA::Short actualShort;
00357       nvlist[0].value >>= actualShort;
00358       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
00359                         
00360       CORBA::Long actualLong;
00361       nvlist[1].value >>= actualLong;
00362       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
00363 
00364       CORBA::Float actualFloat;
00365       nvlist[2].value >>= actualFloat;
00366       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
00367 
00368       CORBA::Double actualDouble;
00369       nvlist[3].value >>= actualDouble;
00370       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
00371     }
00372                 
00380     void test_push_back_list()
00381     {
00382       // 1つめのNVListを作成する
00383       NVList nvlist1;
00384       nvlist1.length(2);
00385                         
00386       NameValue nvShort;
00387       nvShort.name = "short";
00388       nvShort.value <<= (CORBA::Short) 123;
00389       nvlist1[0] = nvShort;
00390                         
00391       NameValue nvLong;
00392       nvLong.name = "long";
00393       nvLong.value <<= (CORBA::Long) 123456;
00394       nvlist1[1] = nvLong;
00395                         
00396       // 2つめのNVListを作成する
00397       NVList nvlist2;
00398       nvlist2.length(2);
00399                         
00400       NameValue nvFloat;
00401       nvFloat.name = "float";
00402       nvFloat.value <<= (CORBA::Float) 987.654;
00403       nvlist2[0] = nvFloat;
00404                         
00405       NameValue nvDouble;
00406       nvDouble.name = "double";
00407       nvDouble.value <<= (CORBA::Double) 987654.321987;
00408       nvlist2[1] = nvDouble;
00409                         
00410       // push_back_listして、1つめのNVListに2つめのNVListをアペンドする
00411       CORBA_SeqUtil::push_back_list(nvlist1, nvlist2);
00412                         
00413       // 正しくアペンドされたことを確認する
00414       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name);
00415       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name);
00416       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name);
00417       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name);
00418                         
00419       CORBA::Short actualShort;
00420       nvlist1[0].value >>= actualShort;
00421       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
00422                         
00423       CORBA::Long actualLong;
00424       nvlist1[1].value >>= actualLong;
00425       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
00426 
00427       CORBA::Float actualFloat;
00428       nvlist1[2].value >>= actualFloat;
00429       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
00430 
00431       CORBA::Double actualDouble;
00432       nvlist1[3].value >>= actualDouble;
00433       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
00434     }
00435                 
00445     void test_insert()
00446     {
00447       // 挿入対象となるNVListを作成する
00448       NVList nvlist;
00449       nvlist.length(1);
00450 
00451       NameValue nvLong;
00452       nvLong.name = "long";
00453       nvLong.value <<= (CORBA::Long) 123456;
00454       nvlist[0] = nvLong;
00455                         
00456       // (1) 先頭への挿入を行う
00457       NameValue nvShort;
00458       nvShort.name = "short";
00459       nvShort.value <<= (CORBA::Short) 123;
00460       CORBA_SeqUtil::insert(nvlist, nvShort, 0);
00461                         
00462       // (2) 最後尾への挿入を行う
00463       NameValue nvDouble;
00464       nvDouble.name = "double";
00465       nvDouble.value <<= (CORBA::Double) 987654.321987;
00466       CORBA_SeqUtil::insert(nvlist, nvDouble, 2);
00467                         
00468       // (3) 中間への挿入を行う
00469       NameValue nvFloat;
00470       nvFloat.name = "float";
00471       nvFloat.value <<= (CORBA::Float) 987.654;
00472       CORBA_SeqUtil::insert(nvlist, nvFloat, 2);
00473                         
00474       // 挿入結果が正しいことを確認する
00475       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist[0].name);
00476       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist[1].name);
00477       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist[2].name);
00478       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist[3].name);
00479                         
00480       CORBA::Short actualShort;
00481       nvlist[0].value >>= actualShort;
00482       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort);
00483                         
00484       CORBA::Long actualLong;
00485       nvlist[1].value >>= actualLong;
00486       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong);
00487 
00488       CORBA::Float actualFloat;
00489       nvlist[2].value >>= actualFloat;
00490       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat);
00491 
00492       CORBA::Double actualDouble;
00493       nvlist[3].value >>= actualDouble;
00494       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble);
00495     }
00496                 
00505     void test_front()
00506     {
00507       // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか
00508       NVList nvlistEmpty;
00509       try {
00510         CORBA_SeqUtil::front<NVList, NameValue>(nvlistEmpty);
00511         CPPUNIT_FAIL("Exception must be thrown.");
00512       } catch (...) {
00513         // expected
00514       }
00515                         
00516       // (2) 取得対象のNVListの要素数が1以上の場合、先頭の要素を取得できるか?
00517       NVList nvlist;
00518       nvlist.length(4);
00519                         
00520       NameValue nvShort;
00521       nvShort.name = "short";
00522       nvShort.value <<= (CORBA::Short) 123;
00523       nvlist[0] = nvShort;
00524                         
00525       NameValue nvLong;
00526       nvLong.name = "long";
00527       nvLong.value <<= (CORBA::Long) 123456;
00528       nvlist[1] = nvLong;
00529                         
00530       NameValue nvFloat;
00531       nvFloat.name = "float";
00532       nvFloat.value <<= (CORBA::Float) 987.654;
00533       nvlist[2] = nvFloat;
00534                         
00535       NameValue nvDouble;
00536       nvDouble.name = "double";
00537       nvDouble.value <<= (CORBA::Double) 987654.321987;
00538       nvlist[3] = nvDouble;
00539                         
00540       // 先頭の要素を取得して、期待値と比較してチェックする
00541       NameValue nvFront = CORBA_SeqUtil::front<NVList, NameValue>(nvlist);
00542                         
00543       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvFront.name);
00544                         
00545       CORBA::Short actualValue;
00546       nvFront.value >>= actualValue;
00547       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualValue);
00548     }
00549                 
00558     void test_back()
00559     {
00560       // (1) 取得対象のNVListの要素数が0の場合、例外がスローされるか?
00561       NVList nvlistEmpty;
00562       try {
00563         CORBA_SeqUtil::back<NVList, NameValue>(nvlistEmpty);
00564         CPPUNIT_FAIL("Exception must be thrown.");
00565       } catch (...) {
00566         // expected
00567       }
00568                         
00569       // (2) 取得対象のNVListの要素数が1以上の場合、最後尾の要素を取得できるか?
00570       NVList nvlist;
00571       nvlist.length(4);
00572                         
00573       NameValue nvShort;
00574       nvShort.name = "short";
00575       nvShort.value <<= (CORBA::Short) 123;
00576       nvlist[0] = nvShort;
00577                         
00578       NameValue nvLong;
00579       nvLong.name = "long";
00580       nvLong.value <<= (CORBA::Long) 123456;
00581       nvlist[1] = nvLong;
00582                         
00583       NameValue nvFloat;
00584       nvFloat.name = "float";
00585       nvFloat.value <<= (CORBA::Float) 987.654;
00586       nvlist[2] = nvFloat;
00587                         
00588       NameValue nvDouble;
00589       nvDouble.name = "double";
00590       nvDouble.value <<= (CORBA::Double) 987654.321987;
00591       nvlist[3] = nvDouble;
00592 
00593       // 最後の要素を取得して、期待値と比較してチェックする
00594       NameValue nvBack = CORBA_SeqUtil::back<NVList, NameValue>(nvlist);
00595                         
00596       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvBack.name);
00597                         
00598       CORBA::Double actualValue;
00599       nvBack.value >>= actualValue;
00600       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualValue);
00601     }
00602                 
00612     void test_erase()
00613     {
00614       // テスト用のNVListを作成する
00615       NVList nvlist1, nvlist2, nvlist3;
00616       nvlist1.length(4);
00617       nvlist2.length(4);
00618       nvlist3.length(4);
00619                         
00620       NameValue nvShort;
00621       nvShort.name = "short";
00622       nvShort.value <<= (CORBA::Short) 123;
00623       nvlist1[0] = nvShort;
00624       nvlist2[0] = nvShort;
00625       nvlist3[0] = nvShort;
00626                         
00627       NameValue nvLong;
00628       nvLong.name = "long";
00629       nvLong.value <<= (CORBA::Long) 123456;
00630       nvlist1[1] = nvLong;
00631       nvlist2[1] = nvLong;
00632       nvlist3[1] = nvLong;
00633                         
00634       NameValue nvFloat;
00635       nvFloat.name = "float";
00636       nvFloat.value <<= (CORBA::Float) 987.654;
00637       nvlist1[2] = nvFloat;
00638       nvlist2[2] = nvFloat;
00639       nvlist3[2] = nvFloat;
00640                         
00641       NameValue nvDouble;
00642       nvDouble.name = "double";
00643       nvDouble.value <<= (CORBA::Double) 987654.321987;
00644       nvlist1[3] = nvDouble;
00645       nvlist2[3] = nvDouble;
00646       nvlist3[3] = nvDouble;
00647                         
00648       // (1) 先頭の要素のみを削除した場合、他要素はそのまま保たれるか?
00649       CORBA_SeqUtil::erase(nvlist1, 0);
00650       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist1.length());
00651       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[0].name);
00652       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[1].name);
00653       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[2].name);
00654                         
00655       CORBA::Long actualLong1;
00656       nvlist1[0].value >>= actualLong1;
00657       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1);
00658 
00659       CORBA::Float actualFloat1;
00660       nvlist1[1].value >>= actualFloat1;
00661       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1);
00662 
00663       CORBA::Double actualDouble1;
00664       nvlist1[2].value >>= actualDouble1;
00665       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1);
00666                         
00667       // (2) 中間の要素のみを削除した場合、他要素はそのまま保たれるか?
00668       CORBA_SeqUtil::erase(nvlist2, 1);
00669       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist2.length());
00670       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name);
00671       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist2[1].name);
00672       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name);
00673                         
00674       CORBA::Short actualShort2;
00675       nvlist2[0].value >>= actualShort2;
00676       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2);
00677 
00678       CORBA::Float actualFloat2;
00679       nvlist2[1].value >>= actualFloat2;
00680       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat2);
00681 
00682       CORBA::Double actualDouble2;
00683       nvlist2[2].value >>= actualDouble2;
00684       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2);
00685 
00686       // (3) 最後尾の要素のみを削除した場合、他要素はそのまま保たれるか?
00687       CORBA_SeqUtil::erase(nvlist3, 3);
00688       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 3, nvlist3.length());
00689       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist3[0].name);
00690       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist3[1].name);
00691       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist3[2].name);
00692                         
00693       CORBA::Short actualShort3;
00694       nvlist3[0].value >>= actualShort3;
00695       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort3);
00696 
00697       CORBA::Long actualLong3;
00698       nvlist3[1].value >>= actualLong3;
00699       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong3);
00700 
00701       CORBA::Float actualFloat3;
00702       nvlist3[2].value >>= actualFloat3;
00703       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat3);
00704     }
00705 
00709     struct functor_erase_if
00710     {
00711       functor_erase_if(const char* name)
00712         : _name(name) {}
00713                         
00714       bool operator()(const NameValue& nv)
00715       {
00716         return _name == std::string(nv.name);
00717       }
00718                         
00719       string _name;
00720     };
00721                 
00730     void test_erase_if()
00731     {
00732       NVList nvlist1, nvlist2;
00733       nvlist1.length(4);
00734       nvlist2.length(4);
00735                         
00736       NameValue nvShort;
00737       nvShort.name = "short";
00738       nvShort.value <<= (CORBA::Short) 123;
00739       nvlist1[0] = nvShort;
00740       nvlist2[0] = nvShort;
00741                         
00742       NameValue nvLong;
00743       nvLong.name = "long";
00744       nvLong.value <<= (CORBA::Long) 123456;
00745       nvlist1[1] = nvLong;
00746       nvlist2[1] = nvLong;
00747                         
00748       NameValue nvFloat;
00749       nvFloat.name = "float";
00750       nvFloat.value <<= (CORBA::Float) 987.654;
00751       nvlist1[2] = nvFloat;
00752       nvlist2[2] = nvFloat;
00753                         
00754       NameValue nvDouble;
00755       nvDouble.name = "double";
00756       nvDouble.value <<= (CORBA::Double) 987654.321987;
00757       nvlist1[3] = nvDouble;
00758       nvlist2[3] = nvDouble;
00759                         
00760       // (1) 条件に合致する要素がない場合、何も削除されずに保たれるか?
00761       CORBA_SeqUtil::erase_if(nvlist1, functor_erase_if("no-match-name"));
00762                         
00763       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist1[0].name);
00764       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist1[1].name);
00765       CPPUNIT_ASSERT_EQUAL((string) "float", (string) nvlist1[2].name);
00766       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist1[3].name);
00767                         
00768       CORBA::Short actualShort1;
00769       nvlist1[0].value >>= actualShort1;
00770       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort1);
00771                         
00772       CORBA::Long actualLong1;
00773       nvlist1[1].value >>= actualLong1;
00774       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong1);
00775 
00776       CORBA::Float actualFloat1;
00777       nvlist1[2].value >>= actualFloat1;
00778       CPPUNIT_ASSERT_EQUAL((CORBA::Float) 987.654, actualFloat1);
00779 
00780       CORBA::Double actualDouble1;
00781       nvlist1[3].value >>= actualDouble1;
00782       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble1);
00783                         
00784       // (2) 条件に合致する要素がある場合、その要素が削除され、他要素は保たれるか?
00785       CORBA_SeqUtil::erase_if(nvlist2, functor_erase_if("float"));
00786                         
00787       CPPUNIT_ASSERT_EQUAL((string) "short", (string) nvlist2[0].name);
00788       CPPUNIT_ASSERT_EQUAL((string) "long", (string) nvlist2[1].name);
00789       CPPUNIT_ASSERT_EQUAL((string) "double", (string) nvlist2[2].name);
00790                         
00791       CORBA::Short actualShort2;
00792       nvlist2[0].value >>= actualShort2;
00793       CPPUNIT_ASSERT_EQUAL((CORBA::Short) 123, actualShort2);
00794                         
00795       CORBA::Long actualLong2;
00796       nvlist2[1].value >>= actualLong2;
00797       CPPUNIT_ASSERT_EQUAL((CORBA::Long) 123456, actualLong2);
00798 
00799       CORBA::Double actualDouble2;
00800       nvlist2[2].value >>= actualDouble2;
00801       CPPUNIT_ASSERT_EQUAL((CORBA::Double) 987654.321987, actualDouble2);
00802     }
00803                 
00811     void test_clear()
00812     {
00813       // テスト用のNVListを作成する
00814       NVList nvlist;
00815       nvlist.length(4);
00816                         
00817       NameValue nvShort;
00818       nvShort.name = "short";
00819       nvShort.value <<= (CORBA::Short) 123;
00820       nvlist[0] = nvShort;
00821                         
00822       NameValue nvLong;
00823       nvLong.name = "long";
00824       nvLong.value <<= (CORBA::Long) 123456;
00825       nvlist[1] = nvLong;
00826                         
00827       NameValue nvFloat;
00828       nvFloat.name = "float";
00829       nvFloat.value <<= (CORBA::Float) 987.654;
00830       nvlist[2] = nvFloat;
00831                         
00832       NameValue nvDouble;
00833       nvDouble.name = "double";
00834       nvDouble.value <<= (CORBA::Double) 987654.321987;
00835       nvlist[3] = nvDouble;
00836                         
00837       // clear()を呼出し、クリアされたこと(要素数0になったこと)を確認する
00838       CORBA_SeqUtil::clear(nvlist);
00839       CPPUNIT_ASSERT_EQUAL((CORBA::ULong) 0, nvlist.length());
00840     }
00841     
00849     void test_refToVstring()
00850     {
00851       int argc(0);
00852       char** argv(NULL);
00853       CORBA::ORB_ptr m_pORB = CORBA::ORB_init(argc, argv);
00854       PortableServer::POA_ptr m_pPOA = 
00855           PortableServer::POA::_narrow(m_pORB->resolve_initial_references("RootPOA"));
00856       m_pPOA->the_POAManager()->activate();
00857 
00858       RTC::PortAdmin portAdmin(m_pORB, m_pPOA);
00859       PortMock* port0 = new PortMock();
00860       port0->setName("port0");
00861       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
00862 
00863       RTC::PortService_var portRef0 = portAdmin.getPortRef("port0");
00864       RTC::ConnectorProfile cprof;
00865       cprof.connector_id = "id";
00866       cprof.name = CORBA::string_dup("Test");
00867 
00868       CORBA_SeqUtil::push_back(cprof.properties,
00869                                NVUtil::newNV("dataport.interface_type",
00870                                              "corba_cdr"));
00871       CORBA_SeqUtil::push_back(cprof.properties,
00872                                NVUtil::newNV("dataport.dataflow_type",
00873                                              "push"));
00874       CORBA_SeqUtil::push_back(cprof.properties,
00875                                NVUtil::newNV("dataport.subscription_type",
00876                                              "flush"));
00877       cprof.ports.length(1);
00878       cprof.ports[0] = portRef0;
00879       coil::vstring str;
00880       str = CORBA_SeqUtil::refToVstring(cprof.ports);
00881       int len = str.size();
00882       int pos = str[0].find("IOR", 0);
00883       CPPUNIT_ASSERT_EQUAL(1, len);
00884       CPPUNIT_ASSERT(pos != string::npos);
00885 
00886 //      delete port0;
00887       // PortBase デストラクタで、deactivate_object を実行しています。
00888     }
00889     
00890   };
00891 }; // namespace CORBA_SeqUtil
00892 
00893 /*
00894  * Register test suite
00895  */
00896 CPPUNIT_TEST_SUITE_REGISTRATION(CORBA_SeqUtil::CORBA_SeqUtilTests);
00897 
00898 #ifdef LOCAL_MAIN
00899 int main(int argc, char* argv[])
00900 {
00901 
00902   FORMAT format = TEXT_OUT;
00903   int target = 0;
00904   std::string xsl;
00905   std::string ns;
00906   std::string fname;
00907   std::ofstream ofs;
00908 
00909   int i(1);
00910   while (i < argc)
00911     {
00912       std::string arg(argv[i]);
00913       std::string next_arg;
00914       if (i + 1 < argc) next_arg = argv[i + 1];
00915       else              next_arg = "";
00916 
00917       if (arg == "--text") { format = TEXT_OUT; break; }
00918       if (arg == "--xml")
00919         {
00920           if (next_arg == "")
00921             {
00922               fname = argv[0];
00923               fname += ".xml";
00924             }
00925           else
00926             {
00927               fname = next_arg;
00928             }
00929           format = XML_OUT;
00930           ofs.open(fname.c_str());
00931         }
00932       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00933       if ( arg == "--cerr"      ) { target = 1; break; }
00934       if ( arg == "--xsl"       )
00935         {
00936           if (next_arg == "") xsl = "default.xsl"; 
00937           else                xsl = next_arg;
00938         }
00939       if ( arg == "--namespace" )
00940         {
00941           if (next_arg == "")
00942             {
00943               std::cerr << "no namespace specified" << std::endl;
00944               exit(1); 
00945             }
00946           else
00947             {
00948               xsl = next_arg;
00949             }
00950         }
00951       ++i;
00952     }
00953   CppUnit::TextUi::TestRunner runner;
00954   if ( ns.empty() )
00955     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00956   else
00957     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00958   CppUnit::Outputter* outputter = 0;
00959   std::ostream* stream = target ? &std::cerr : &std::cout;
00960   switch ( format )
00961     {
00962     case TEXT_OUT :
00963       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00964       break;
00965     case XML_OUT :
00966       std::cout << "XML_OUT" << std::endl;
00967       outputter = new CppUnit::XmlOutputter(&runner.result(),
00968                                             ofs, "shift_jis");
00969       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00970       break;
00971     case COMPILER_OUT :
00972       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00973       break;
00974     }
00975   runner.setOutputter(outputter);
00976   runner.run();
00977   return 0; // runner.run() ? 0 : 1;
00978 }
00979 
00980 #endif // MAIN
00981 #endif // CORBA_SeqUtil_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15