00001
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef NVUtil_cpp
00050 #define NVUtil_cpp
00051
00052 #include <cppunit/ui/text/TestRunner.h>
00053 #include <cppunit/TextOutputter.h>
00054 #include <cppunit/extensions/TestFactoryRegistry.h>
00055 #include <cppunit/extensions/HelperMacros.h>
00056 #include <cppunit/TestAssert.h>
00057
00058 #include <rtm/NVUtil.h>
00059
00060 namespace NVUtil
00061 {
00062 using namespace NVUtil;
00063 using namespace std;
00064
00065 int g_argc;
00066 vector<string> g_argv;
00067
00072 class NVUtilTests
00073 : public CppUnit::TestFixture
00074 {
00075 CPPUNIT_TEST_SUITE(NVUtilTests);
00076 CPPUNIT_TEST(test_newNV_Short);
00077 CPPUNIT_TEST(test_newNV_Long);
00078 CPPUNIT_TEST(test_newNV_Float);
00079 CPPUNIT_TEST(test_newNV_Double);
00080 CPPUNIT_TEST(test_newNV_Str);
00081 CPPUNIT_TEST(test_newNVChar);
00082 CPPUNIT_TEST(test_newNVBool);
00083 CPPUNIT_TEST(test_newNVOctet);
00084 CPPUNIT_TEST(test_copy);
00085 CPPUNIT_TEST(test_toProperties);
00086 CPPUNIT_TEST(test_copyToProperties);
00087 CPPUNIT_TEST(test_find);
00088 CPPUNIT_TEST(test_find_index);
00089 CPPUNIT_TEST(test_isString);
00090 CPPUNIT_TEST(test_isStringValue);
00091 CPPUNIT_TEST(test_toString);
00092 CPPUNIT_TEST(test_appendStringValue);
00093 CPPUNIT_TEST(test_append);
00094 CPPUNIT_TEST(test_dump);
00095 CPPUNIT_TEST(test_toStringNV);
00096 CPPUNIT_TEST_SUITE_END();
00097
00098 private:
00099
00100 public:
00101 NVUtilTests()
00102 {
00103 CORBA::ORB_var orb;
00104 char* argv[g_argc];
00105 for (int i = 0; i < g_argc; i++) {
00106 argv[i] = (char *)g_argv[i].c_str();
00107 }
00108
00109 orb = CORBA::ORB_init(g_argc, argv);
00110 }
00111
00112 ~NVUtilTests()
00113 {
00114 }
00115
00116 virtual void setUp()
00117 {
00118 }
00119
00120 virtual void tearDown()
00121 {
00122 }
00123
00129 void test_newNV_Short()
00130 {
00131
00132 CORBA::Short value = 1;
00133 string name = "short";
00134 SDOPackage::NameValue nv = newNV(name.c_str(), value);
00135
00136 string nvName(nv.name);
00137 CPPUNIT_ASSERT_EQUAL(name, nvName);
00138
00139 CORBA::Short nvValue;
00140 nv.value >>= nvValue;
00141 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00142 }
00143
00149 void test_newNV_Long()
00150 {
00151
00152 CORBA::Long value = 999999999;
00153 string name = "long";
00154 SDOPackage::NameValue nv = newNV(name.c_str(), value);
00155
00156 string nvName(nv.name);
00157 CPPUNIT_ASSERT_EQUAL(name, nvName);
00158
00159 CORBA::Long nvValue;
00160 nv.value >>= nvValue;
00161 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00162 }
00163
00169 void test_newNV_Float()
00170 {
00171
00172 CORBA::Float value = 99999.9;
00173 string name = "float";
00174 SDOPackage::NameValue nv = newNV(name.c_str(), value);
00175
00176 string nvName(nv.name);
00177 CPPUNIT_ASSERT_EQUAL(name, nvName);
00178
00179 CORBA::Float nvValue;
00180 nv.value >>= nvValue;
00181 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00182 }
00183
00189 void test_newNV_Double()
00190 {
00191
00192 CORBA::Double value = 9999999.999;
00193 string name = "double";
00194 SDOPackage::NameValue nv = newNV(name.c_str(), value);
00195
00196 string nvName(nv.name);
00197 CPPUNIT_ASSERT_EQUAL(name, nvName);
00198
00199 CORBA::Double nvValue;
00200 nv.value >>= nvValue;
00201 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00202 }
00203
00209 void test_newNV_Str()
00210 {
00211
00212 string name = "string";
00213 CORBA::String_var value = CORBA::string_dup("Hello, world!");
00214 SDOPackage::NameValue nv = newNV(name.c_str(), value);
00215
00216
00217 string nvName(nv.name);
00218 CPPUNIT_ASSERT_EQUAL(name, nvName);
00219
00220
00221
00222 const char* nvValue;
00223 nv.value >>= nvValue;
00224 CPPUNIT_ASSERT_EQUAL(string("Hello, world!"), string(nvValue));
00225 }
00226
00232 void test_newNVChar()
00233 {
00234
00235 string name = "char";
00236 CORBA::Char value = 'A';
00237 SDOPackage::NameValue nv = newNVChar(name.c_str(), value);
00238
00239
00240 string nvName(nv.name);
00241 CPPUNIT_ASSERT_EQUAL(name, nvName);
00242
00243
00244 CORBA::Char nvValue;
00245 nv.value >>= CORBA::Any::to_char(nvValue);
00246 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00247 }
00248
00254 void test_newNVBool()
00255 {
00256 string name = "bool";
00257 CORBA::Boolean value = false;
00258 SDOPackage::NameValue nv = newNVBool(name.c_str(), value);
00259
00260 string nvName(nv.name);
00261 CPPUNIT_ASSERT_EQUAL(name, nvName);
00262
00263 CORBA::Boolean nvValue;
00264 nv.value >>= CORBA::Any::to_boolean(nvValue);
00265 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00266 }
00267
00273 void test_newNVOctet()
00274 {
00275 string name = "octet";
00276 CORBA::Octet value = 030;
00277 SDOPackage::NameValue nv = newNVOctet(name.c_str(), value);
00278
00279 string nvName(nv.name);
00280 CPPUNIT_ASSERT_EQUAL(name, nvName);
00281
00282 CORBA::Octet nvValue;
00283 nv.value >>= CORBA::Any::to_octet(nvValue);
00284 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00285 }
00286
00292 void test_copy()
00293 {
00294
00295
00296 string name = "port-type";
00297 string value = "port-type-value";
00298 map<string, string> mProp;
00299 mProp[name] = value;
00300 coil::Properties prop(mProp);
00301
00302
00303 SDOPackage::NVList nvlist;
00304 copyFromProperties(nvlist, prop);
00305
00306
00307 string nvName(nvlist[0].name);
00308 CPPUNIT_ASSERT_EQUAL(name, nvName);
00309
00310 const char* getval;
00311 nvlist[0].value >>= getval;
00312 string nvValue(getval);
00313 CPPUNIT_ASSERT_EQUAL(value, nvValue);
00314 }
00315
00321 void test_toProperties()
00322 {
00323
00324 SDOPackage::NVList nvlist;
00325 nvlist.length(2);
00326
00327 string name1 = "testname.test1";
00328 string value1 = "testval1";
00329 nvlist[0].name = name1.c_str();
00330 nvlist[0].value <<= value1.c_str();
00331
00332 string name2 = "testname.test2";
00333 string value2 = "testval2";
00334 nvlist[1].name = name2.c_str();
00335 nvlist[1].value <<= value2.c_str();
00336
00337
00338 coil::Properties prop = toProperties(nvlist);
00339
00340
00341 string propValue1 = prop.getProperty(name1);
00342 CPPUNIT_ASSERT_EQUAL(value1, propValue1);
00343
00344 string propValue2 = prop.getProperty(name2);
00345 CPPUNIT_ASSERT_EQUAL(value2, propValue2);
00346 }
00347
00353 void test_copyToProperties()
00354 {
00355
00356 SDOPackage::NVList nvlist;
00357 nvlist.length(2);
00358
00359 string name1 = "testname.test1";
00360 string value1 = "testval1";
00361 nvlist[0].name = name1.c_str();
00362 nvlist[0].value <<= value1.c_str();
00363
00364 string name2 = "testname.test2";
00365 string value2 = "testval2";
00366 nvlist[1].name = name2.c_str();
00367 nvlist[1].value <<= value2.c_str();
00368
00369
00370 coil::Properties prop;
00371 copyToProperties(prop, nvlist);
00372
00373
00374 string propValue1 = prop.getProperty(name1);
00375 CPPUNIT_ASSERT_EQUAL(value1, propValue1);
00376 string propValue2 = prop.getProperty(name2);
00377 CPPUNIT_ASSERT_EQUAL(value2, propValue2);
00378 }
00379
00385 void test_find()
00386 {
00387 SDOPackage::NVList nvlist;
00388 nvlist.length(2);
00389
00390
00391 string name1 = "short";
00392 CORBA::Short value1 = 1;
00393 nvlist[0].name = name1.c_str();
00394 nvlist[0].value <<= value1;
00395
00396
00397 string name2 = "long";
00398 CORBA::Long value2 = 111;
00399 nvlist[1].name = name2.c_str();
00400 nvlist[1].value <<= value2;
00401
00402
00403 CORBA::Short foundValue1;
00404 (find(nvlist, name1.c_str())) >>= foundValue1;
00405 CPPUNIT_ASSERT_EQUAL(value1, foundValue1);
00406
00407
00408 CORBA::Long foundValue2;
00409 (find(nvlist, name2.c_str())) >>= foundValue2;
00410 CPPUNIT_ASSERT_EQUAL(value2, foundValue2);
00411 }
00412
00418 void test_find_index()
00419 {
00420 SDOPackage::NVList nvlist;
00421 nvlist.length(2);
00422
00423
00424 string name1 = "short";
00425 CORBA::Short value1 = 1;
00426 nvlist[0].name = name1.c_str();
00427 nvlist[0].value <<= value1;
00428
00429
00430 string name2 = "long";
00431 CORBA::Long value2 = 111;
00432 nvlist[1].name = name2.c_str();
00433 nvlist[1].value <<= value2;
00434
00435
00436 CORBA::Long ret;
00437 ret = find_index(nvlist, name1.c_str());
00438 CPPUNIT_ASSERT_EQUAL((CORBA::Long)0, ret);
00439
00440
00441 ret = find_index(nvlist, name2.c_str());
00442 CPPUNIT_ASSERT_EQUAL((CORBA::Long)1, ret);
00443 }
00444
00450 void test_isString()
00451 {
00452 SDOPackage::NVList nvlist;
00453 nvlist.length(2);
00454
00455
00456 string name1 = "short";
00457 CORBA::Short value1 = 1;
00458 nvlist[0].name = name1.c_str();
00459 nvlist[0].value <<= value1;
00460
00461
00462 string name2 = "string";
00463 string value2 = "test";
00464 nvlist[1].name = name2.c_str();
00465 nvlist[1].value <<= value2.c_str();
00466
00467
00468 CPPUNIT_ASSERT(!isString(nvlist, name1.c_str()));
00469 CPPUNIT_ASSERT(isString(nvlist, name2.c_str()));
00470 }
00471
00477 void test_isStringValue()
00478 {
00479 SDOPackage::NVList nvlist;
00480 nvlist.length(2);
00481
00482
00483 string name1 = "short";
00484 CORBA::Short value1 = 1;
00485 nvlist[0].name = name1.c_str();
00486 nvlist[0].value <<= value1;
00487
00488
00489 string name2 = "string";
00490 string value2 = "test";
00491 nvlist[1].name = name2.c_str();
00492 nvlist[1].value <<= value2.c_str();
00493
00494
00495 CPPUNIT_ASSERT(!isStringValue(nvlist, name1.c_str(), "1"));
00496 CPPUNIT_ASSERT(isStringValue(nvlist, name2.c_str(), "test"));
00497 }
00498
00504 void test_toString() {
00505
00506
00507 SDOPackage::NVList nvlist;
00508 nvlist.length(2);
00509
00510
00511 string name1 = "short";
00512 CORBA::Short value1 = 1;
00513 nvlist[0].name = name1.c_str();
00514 nvlist[0].value <<= value1;
00515
00516
00517 string name2 = "string";
00518 string value2 = "test";
00519 nvlist[1].name = name2.c_str();
00520 nvlist[1].value <<= value2.c_str();
00521
00522
00523 string empty("");
00524 CPPUNIT_ASSERT_EQUAL(empty, toString(nvlist, name1.c_str()));
00525 CPPUNIT_ASSERT_EQUAL(value2, toString(nvlist, name2.c_str()));
00526 }
00527
00533 void test_appendStringValue()
00534 {
00535
00536 SDOPackage::NVList nvlist;
00537 nvlist.length(3);
00538
00539
00540 string name1 = "language";
00541 string value1 = "japanese";
00542 nvlist[0].name = name1.c_str();
00543 nvlist[0].value <<= value1.c_str();
00544
00545 string name2 = "fruit";
00546 string value2 = "apple";
00547 nvlist[1].name = name2.c_str();
00548 nvlist[1].value <<= value2.c_str();
00549
00550 string name3 = "drink";
00551 string value3 = "coffee, coke";
00552 nvlist[2].name = name3.c_str();
00553 nvlist[2].value <<= value3.c_str();
00554
00555
00556 string name4 = "os";
00557 string value4 = "unix";
00558 CPPUNIT_ASSERT(appendStringValue(nvlist, name4.c_str(), value4.c_str()));
00559 CPPUNIT_ASSERT_EQUAL(value4, toString(nvlist, name4.c_str()));
00560
00561
00562 string name5 = name1;
00563 string value5 = "english";
00564 CPPUNIT_ASSERT(appendStringValue(nvlist, name5.c_str(), value5.c_str()));
00565 string expectedValueLanguage = "japanese,english";
00566 CPPUNIT_ASSERT_EQUAL(expectedValueLanguage, toString(nvlist, name5.c_str()));
00567
00568
00569 string name6 = name2;
00570 string value6 = value2;
00571 CPPUNIT_ASSERT(appendStringValue(nvlist, name6.c_str(), value6.c_str()));
00572 CPPUNIT_ASSERT_EQUAL(value2, toString(nvlist, name6.c_str()));
00573
00574
00575 string name7 = name3;
00576 string value7 = "coke, beer";
00577 CPPUNIT_ASSERT(appendStringValue(nvlist, name7.c_str(), value7.c_str()));
00578 string expectedValueDrink = "coffee, coke,coke, beer";
00579 CPPUNIT_ASSERT_EQUAL(expectedValueDrink, toString(nvlist, name7.c_str()));
00580 }
00581
00587 void test_append()
00588 {
00589
00590 SDOPackage::NVList nvlistA;
00591 nvlistA.length(2);
00592
00593 string nameA1 = "nameA1";
00594 string valueA1 = "valueA1";
00595 nvlistA[0].name = nameA1.c_str();
00596 nvlistA[0].value <<= valueA1.c_str();
00597
00598 string nameA2 = "nameA2";
00599 string valueA2 = "valueA2";
00600 nvlistA[1].name = nameA2.c_str();
00601 nvlistA[1].value <<= valueA2.c_str();
00602
00603
00604 SDOPackage::NVList nvlistB;
00605 nvlistB.length(2);
00606
00607 string nameB1 = "nameB1";
00608 string valueB1 = "valueB1";
00609 nvlistB[0].name = nameB1.c_str();
00610 nvlistB[0].value <<= valueB1.c_str();
00611
00612 string nameB2 = "nameB2";
00613 string valueB2 = "valueB2";
00614 nvlistB[1].name = nameB2.c_str();
00615 nvlistB[1].value <<= valueB2.c_str();
00616
00617
00618 append(nvlistA, nvlistB);
00619
00620
00621 CPPUNIT_ASSERT_EQUAL(valueA1, toString(nvlistA, nameA1.c_str()));
00622 CPPUNIT_ASSERT_EQUAL(valueA2, toString(nvlistA, nameA2.c_str()));
00623 CPPUNIT_ASSERT_EQUAL(valueB1, toString(nvlistA, nameB1.c_str()));
00624 CPPUNIT_ASSERT_EQUAL(valueB2, toString(nvlistA, nameB2.c_str()));
00625 }
00626 void test_dump()
00627 {
00628 SDOPackage::NVList nvlistC;
00629 nvlistC.length(3);
00630
00631 string nameC1 = "nameC1";
00632 string valueC1 = "valueC1";
00633 nvlistC[0].name = nameC1.c_str();
00634 nvlistC[0].value <<= valueC1.c_str();
00635
00636 string nameC2 = "nameC2";
00637 string valueC2 = "valueC2";
00638 nvlistC[1].name = nameC2.c_str();
00639 nvlistC[1].value <<= valueC2.c_str();
00640
00641 string nameC3 = "name_hoge";
00642 string valueC3 = "value_hoge";
00643 nvlistC[2].name = nameC3.c_str();
00644 nvlistC[2].value <<= valueC3.c_str();
00645
00646
00647 dump(nvlistC);
00648 }
00649
00650
00651 void test_toStringNV()
00652 {
00653 SDOPackage::NVList nvlistD;
00654 nvlistD.length(2);
00655
00656 string nameD1 = "nameD1";
00657 string valueD1 = "valueD1";
00658 nvlistD[0].name = nameD1.c_str();
00659 nvlistD[0].value <<= valueD1.c_str();
00660
00661 string nameD2 = "nameD2";
00662 string valueD2 = "valueD2";
00663 nvlistD[1].name = nameD2.c_str();
00664 nvlistD[1].value <<= valueD2.c_str();
00665
00666 string strD ="\n\nnameD1: valueD1\nnameD2: valueD2";
00667
00668
00669 SDOPackage::NVList nvlistE;
00670 nvlistE.length(2);
00671
00672
00673 string nameE1 = "short";
00674 CORBA::Short valueE1 = 1;
00675 nvlistE[0].name = nameE1.c_str();
00676 nvlistE[0].value <<= valueE1;
00677
00678
00679 string nameE2 = "string";
00680 string valueE2 = "test";
00681 nvlistE[1].name = nameE2.c_str();
00682 nvlistE[1].value <<= valueE2.c_str();
00683
00684
00685
00686 std::string empty = "short: not a string value\nstring: test\n";
00687 std::string str_nvlistE = toString(nvlistE);
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 CPPUNIT_ASSERT(toString(nvlistE) == empty);
00699
00700
00701 }
00702 };
00703 };
00704
00705
00706
00707
00708 CPPUNIT_TEST_SUITE_REGISTRATION(NVUtil::NVUtilTests);
00709
00710 #ifdef LOCAL_MAIN
00711 int main(int argc, char* argv[])
00712 {
00713
00714 FORMAT format = TEXT_OUT;
00715 int target = 0;
00716 std::string xsl;
00717 std::string ns;
00718 std::string fname;
00719 std::ofstream ofs;
00720
00721 int i(1);
00722 while (i < argc)
00723 {
00724 std::string arg(argv[i]);
00725 std::string next_arg;
00726 if (i + 1 < argc) next_arg = argv[i + 1];
00727 else next_arg = "";
00728
00729 if (arg == "--text") { format = TEXT_OUT; break; }
00730 if (arg == "--xml")
00731 {
00732 if (next_arg == "")
00733 {
00734 fname = argv[0];
00735 fname += ".xml";
00736 }
00737 else
00738 {
00739 fname = next_arg;
00740 }
00741 format = XML_OUT;
00742 ofs.open(fname.c_str());
00743 }
00744 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
00745 if ( arg == "--cerr" ) { target = 1; break; }
00746 if ( arg == "--xsl" )
00747 {
00748 if (next_arg == "") xsl = "default.xsl";
00749 else xsl = next_arg;
00750 }
00751 if ( arg == "--namespace" )
00752 {
00753 if (next_arg == "")
00754 {
00755 std::cerr << "no namespace specified" << std::endl;
00756 exit(1);
00757 }
00758 else
00759 {
00760 xsl = next_arg;
00761 }
00762 }
00763 ++i;
00764 }
00765 CppUnit::TextUi::TestRunner runner;
00766 if ( ns.empty() )
00767 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00768 else
00769 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00770 CppUnit::Outputter* outputter = 0;
00771 std::ostream* stream = target ? &std::cerr : &std::cout;
00772 switch ( format )
00773 {
00774 case TEXT_OUT :
00775 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00776 break;
00777 case XML_OUT :
00778 std::cout << "XML_OUT" << std::endl;
00779 outputter = new CppUnit::XmlOutputter(&runner.result(),
00780 ofs, "shift_jis");
00781 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00782 break;
00783 case COMPILER_OUT :
00784 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00785 break;
00786 }
00787 runner.setOutputter(outputter);
00788 runner.run();
00789 return 0;
00790 }
00791 #endif // MAIN
00792 #endif // NVUtil_cpp