testDataSerialization.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename testDataSerialization.cpp
00024  * @Description Tests the conversion of Data objects into ValueVectors
00025  * @Status unknown
00026  * @Version $Id: testDataSerialization.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include <ace/Trace.h>
00033 
00034 #include <iostream>
00035 #include <sstream>
00036 #include <typeinfo>
00037 #include "DataFactory.hpp"
00038 #include "DataValueVector.hpp"
00039 
00040 
00041 #include <Exception.hpp>
00042 #include <FilePathSearch.hpp>
00043 
00044 #include <yarp/os/all.h>
00045 
00046 using namespace mermaid::support::data;
00047 using mermaid::support::errorhandling::Exception;
00048 using mermaid::support::system::FilePathSearch;
00049 using std::string;
00050 using std::cout;
00051 using std::endl;
00052 
00053 using yarp::os::Value;
00054 
00055 
00056 int main (int argc, char * const argv[])
00057 {
00058   ACE_Trace::stop_tracing();
00059   
00060   int numErrors = 0;
00061   
00062   if (argc != 2) {
00063     cout << "Usage: testDataSerialization [filename]" << endl;
00064     return (1);
00065   }
00066   
00067   try {
00068     std::cerr << "DataStructure framework test" << endl;
00069     
00070     FilePathSearch::addSearchPath ("./");
00071     FilePathSearch::addSearchPath ("/");
00072     
00073     shared_ptr<XmlDocument>  doc = shared_ptr<XmlDocument> (new XmlDocument());
00074     cout << "Reading from file: " << argv[1] << endl;
00075     doc->parseFile (string (argv[1]));
00076     DataFactory::loadDataDescription (doc);
00077     
00078     
00079     //test integer
00080     {
00081       cout << "Testing single integer" << endl;
00082       shared_ptr<DataBox> i = DataFactory::buildDataBox ("lonely-int");
00083       Bottle b;
00084       DataFactory::writeDataBoxToBottle (i, b);
00085       
00086       if (b.size() != 1) {
00087         throw Exception ("testDataSerialization : Bottle for single integer does not have size 1");
00088       }
00089       
00090       Value v = b.get (0);
00091       if (v.isInt() == false) {
00092         throw Exception ("testDataSerialization : Value for single integer does not have type INTEGER");
00093       }
00094     }
00095     
00096     // test integer array static
00097     {
00098       cout << "Testing static integer array" << endl;
00099       shared_ptr<DataBox> i = DataFactory::buildDataBox ("int-array-static");
00100       Bottle b;
00101       DataFactory::writeDataBoxToBottle (i, b);
00102       
00103       int arraySize = 3;
00104       
00105       if (b.size() != (arraySize)) {
00106         throw Exception ("testDataSerialization : Bottle for static integer array does not have expected size");
00107       }
00108       
00109       for (int j = 0; j < arraySize; j++) {
00110         Value v = b.get (j);
00111         if (v.isInt() == false) {
00112           throw Exception ("testDataSerialization : Value for single integer does not have type INTEGER");
00113         }
00114       }
00115     }
00116     
00117     // test integer dynamic array
00118     {
00119       cout << "Testing dynamic integer array" << endl;
00120       shared_ptr<DataBox> i = DataFactory::buildDataBox ("int-array-dynamic");
00121       // filling dynamic array with some data
00122       int arraySize = 13;
00123       shared_ptr<IntegerArray> array = i->getIntegerArray ("intarray");
00124       for (int j = 0; j < arraySize; j++) {
00125         array->pushBack (j);
00126       }
00127       
00128       Bottle b;
00129       DataFactory::writeDataBoxToBottle (i, b);
00130       
00131       if (b.size() != (arraySize + 1)) {
00132         throw Exception ("testDataSerialization : Bottle for dynamic integer array does not have expected size");
00133       }
00134       
00135       Value v = b.get (0);
00136       if (v.isInt() == false) {
00137         throw Exception ("testDataSerialization : Value for dynamic integer array size does not have type INTEGER");
00138       }
00139       
00140       for (int j = 1; j < (arraySize + 1); j++) {
00141         Value v = b.get (j);
00142         if (v.isInt() == false) {
00143           throw Exception ("testDataSerialization : Value for single integer does not have type INTEGER");
00144         }
00145       }
00146     }
00147     
00148     // test double
00149     {
00150       cout << "Testing single double" << endl;
00151       shared_ptr<DataBox> d = DataFactory::buildDataBox ("lonely-double");
00152       Bottle b;
00153       DataFactory::writeDataBoxToBottle (d, b);
00154       
00155       if (b.size() != 1) {
00156         throw Exception ("testDataSerialization : Bottle for single double does not have size 1");
00157       }
00158       
00159       Value v = b.get (0);
00160       if (v.isDouble() == false) {
00161         throw Exception ("testDataSerialization : Value for single double does not have type DOUBLE");
00162       }
00163     }
00164     
00165     //test double static array
00166     {
00167       cout << "Testing static double array" << endl;
00168       shared_ptr<DataBox> d = DataFactory::buildDataBox ("double-array-static");
00169       Bottle b;
00170       DataFactory::writeDataBoxToBottle (d, b);
00171       
00172       int arraySize = 3;
00173       
00174       if (b.size() != (arraySize)) {
00175         throw Exception ("testDataSerialization : Bottle for static double array does not have expected size");
00176       }
00177       
00178       for (int j = 0; j < arraySize; j++) {
00179         Value v = b.get (j);
00180         if (v.isDouble() == false) {
00181           throw Exception ("testDataSerialization : Value for single double does not have type DOUBLE");
00182         }
00183       }
00184     }
00185     
00186     //test double dynamic array
00187     {
00188       cout << "Testing dynamic double array" << endl;
00189       shared_ptr<DataBox> d = DataFactory::buildDataBox ("double-array-dynamic");
00190       // filling dynamic array with some data
00191       int arraySize = 13;
00192       shared_ptr<DoubleArray> array = d->getDoubleArray ("doublearray");
00193       for (int j = 0; j < arraySize; j++) {
00194         array->pushBack ( (double) j);
00195       }
00196       
00197       Bottle b;
00198       DataFactory::writeDataBoxToBottle (d, b);
00199       
00200       if (b.size() != (arraySize + 1)) {
00201         throw Exception ("testDataSerialization : Bottle for dynamic integer array does not have expected size");
00202       }
00203       
00204       Value v = b.get (0);
00205       if (v.isInt() == false) {
00206         throw Exception ("testDataSerialization : Value for dynamic double array size does not have type INTEGER");
00207       }
00208       
00209       for (int j = 1; j < (arraySize + 1); j++) {
00210         Value v = b.get (j);
00211         if (v.isDouble() == false) {
00212           throw Exception ("testDataSerialization : Value for single double does not have type DOUBLE");
00213         }
00214       }
00215     }
00216     
00217     
00218     //test string
00219     {
00220       cout << "Testing single string" << endl;
00221       shared_ptr<DataBox> s = DataFactory::buildDataBox ("lonely-string");
00222       Bottle b;
00223       DataFactory::writeDataBoxToBottle (s, b);
00224       
00225       if (b.size() != 1) {
00226         throw Exception ("testDataSerialization : Bottle for single string does not have size 1");
00227       }
00228       
00229       Value v = b.get (0);
00230       if (v.isString() == false) {
00231         throw Exception ("testDataSerialization : Value for single double does not have type STRING");
00232       }
00233     }
00234     
00235     //test string static array
00236     {
00237       cout << "Testing static string array" << endl;
00238       shared_ptr<DataBox> s = DataFactory::buildDataBox ("string-array-static");
00239       Bottle b;
00240       DataFactory::writeDataBoxToBottle (s, b);
00241       
00242       int arraySize = 3;
00243       
00244       if (b.size() != (arraySize)) {
00245         throw Exception ("testDataSerialization : Bottle for static string array does not have expected size");
00246       }
00247       
00248       for (int j = 0; j < arraySize; j++) {
00249         Value v = b.get (j);
00250         if (v.isString() == false) {
00251           throw Exception ("testDataSerialization : Value for single string does not have type STRING");
00252         }
00253       }
00254     }
00255     
00256     //test string dynamic array
00257     {
00258       cout << "Testing dynamic string array" << endl;
00259       shared_ptr<DataBox> s = DataFactory::buildDataBox ("string-array-dynamic");
00260       // filling dynamic array with some data
00261       int arraySize = 13;
00262       shared_ptr<StringArray> array = s->getStringArray ("stringarray");
00263       for (int j = 0; j < arraySize; j++) {
00264         std::stringstream ss;
00265         ss << j;
00266         array->pushBack (ss.str());
00267       }
00268       
00269       Bottle b;
00270       DataFactory::writeDataBoxToBottle (s, b);
00271       
00272       if (b.size() != (arraySize + 1)) {
00273         throw Exception ("testDataSerialization : Bottle for dynamic string array does not have expected size");
00274       }
00275       
00276       Value v = b.get (0);
00277       if (v.isInt() == false) {
00278         throw Exception ("testDataSerialization : Value for dynamic string array size does not have type INTEGER");
00279       }
00280       
00281       for (int j = 1; j < (arraySize + 1); j++) {
00282         Value v = b.get (j);
00283         if (v.isString() == false) {
00284           throw Exception ("testDataSerialization : Value for single string does not have type STRING");
00285         }
00286       }
00287     }
00288     
00289     //test simple data box
00290     {
00291       cout << "Testing simple DataBox" << endl;
00292       shared_ptr<DataBox> d = DataFactory::buildDataBox ("simple-data");
00293       Bottle b;
00294       DataFactory::writeDataBoxToBottle (d, b);
00295       
00296       if (b.size() != 3) {
00297         throw Exception ("testDataSerialization : Bottle for simple-data does not have size 3");
00298       }
00299       
00300       {
00301         Value v0 = b.get (0);
00302         if (v0.isInt() == false) {
00303           throw Exception ("testDataSerialization : Value for simple-data at position #0 does not have type INTEGER");
00304         }
00305       }
00306       
00307       {
00308         Value v1 = b.get (1);
00309         if (v1.isDouble() == false) {
00310           throw Exception ("testDataSerialization : Value for simple-data at position #1 does not have type DOUBLE");
00311         }
00312       }
00313       
00314       {
00315         Value v2 = b.get (2);
00316         if (v2.isString() == false) {
00317           throw Exception ("testDataSerialization : Value for simple-data at position #2 does not have type STRING");
00318         }
00319       }
00320     }
00321     
00322     //test complex data box
00323     {
00324       cout << "Testing complex DataBox" << endl;
00325       shared_ptr<DataBox> d = DataFactory::buildDataBox ("complex-data");
00326       Bottle b;
00327       DataFactory::writeDataBoxToBottle (d, b);
00328       
00329       if (b.size() != 6) {
00330         throw Exception ("testDataSerialization : Bottle for complex-data does not have size 6");
00331       }
00332       
00333       {
00334         Value v0 = b.get (0);
00335         if (v0.isInt() == false) {
00336           throw Exception ("testDataSerialization : Value for complex-data at position #0 does not have type INTEGER");
00337         }
00338       }
00339       
00340       {
00341         Value v1 = b.get (1);
00342         if (v1.isDouble() == false) {
00343           throw Exception ("testDataSerialization : Value for complex-data at position #1 does not have type DOUBLE");
00344         }
00345       }
00346       
00347       {
00348         Value v2 = b.get (2);
00349         if (v2.isString() == false) {
00350           throw Exception ("testDataSerialization : Value for complex-data at position #2 does not have type STRING");
00351         }
00352       }
00353       
00354       {
00355         Value v3 = b.get (3);
00356         if (v3.isInt() == false) {
00357           throw Exception ("testDataSerialization : Value for complex-data at position #3 does not have type INTEGER");
00358         }
00359       }
00360       
00361       {
00362         Value v4 = b.get (4);
00363         if (v4.isDouble() == false) {
00364           throw Exception ("testDataSerialization : Value for complex-data at position #4 does not have type DOUBLE");
00365         }
00366       }
00367       
00368       {
00369         Value v5 = b.get (5);
00370         if (v5.isString() == false) {
00371           throw Exception ("testDataSerialization : Value for complex-data at position #5 does not have type STRING");
00372         }
00373       }
00374     }
00375     
00376     //test complex data static array
00377     {
00378       cout << "Testing complex DataBox static array" << endl;
00379       shared_ptr<DataBox> d = DataFactory::buildDataBox ("complex-data-array-static");
00380       Bottle b;
00381       DataFactory::writeDataBoxToBottle (d, b);
00382       
00383       if (b.size() != 15) {
00384         throw Exception ("testDataSerialization : Bottle for complex-data-array-static does not have size 15");
00385       }
00386       
00387       {
00388         Value v0 = b.get (0);
00389         if (v0.isInt() == false) {
00390           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type INTEGER");
00391         }
00392       }
00393       
00394       {
00395         Value v1 = b.get (1);
00396         if (v1.isDouble() == false) {
00397           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type DOUBLE");
00398         }
00399       }
00400       
00401       {
00402         Value v2 = b.get (2);
00403         if (v2.isString() == false) {
00404           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type STRING");
00405         }
00406       }
00407       
00408       {
00409         Value v3 = b.get (3);
00410         if (v3.isInt() == false) {
00411           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type INTEGER");
00412         }
00413       }
00414       
00415       {
00416         Value v4 = b.get (4);
00417         if (v4.isDouble() == false) {
00418           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type DOUBLE");
00419         }
00420       }
00421       
00422       {
00423         Value v5 = b.get (5);
00424         if (v5.isString() == false) {
00425           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type STRING");
00426         }
00427       }
00428       
00429       {
00430         Value v6 = b.get (6);
00431         if (v6.isInt() == false) {
00432           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type INTEGER");
00433         }
00434       }
00435       
00436       {
00437         Value v7 = b.get (7);
00438         if (v7.isDouble() == false) {
00439           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type DOUBLE");
00440         }
00441       }
00442       
00443       {
00444         Value v8 = b.get (8);
00445         if (v8.isString() == false) {
00446           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type STRING");
00447         }
00448       }
00449       
00450       {
00451         Value v9 = b.get (9);
00452         if (v9.isInt() == false) {
00453           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type INTEGER");
00454         }
00455       }
00456       
00457       {
00458         Value v10 = b.get (10);
00459         if (v10.isDouble() == false) {
00460           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type DOUBLE");
00461         }
00462       }
00463       
00464       {
00465         Value v11 = b.get (11);
00466         if (v11.isString() == false) {
00467           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type STRING");
00468         }
00469       }
00470       
00471       {
00472         Value v12 = b.get (12);
00473         if (v12.isInt() == false) {
00474           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type INTEGER");
00475         }
00476       }
00477       
00478       {
00479         Value v13 = b.get (13);
00480         if (v13.isDouble() == false) {
00481           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type DOUBLE");
00482         }
00483       }
00484       
00485       {
00486         Value v14 = b.get (14);
00487         if (v14.isString() == false) {
00488           throw Exception ("testDataSerialization : Value for complex-data-array-static at position #0 does not have type STRING");
00489         }
00490       }
00491     }
00492     
00493     //test complex data dynamic array
00494     {
00495       cout << "Testing complex DataBox dynamic array" << endl;
00496       shared_ptr<DataBox> d = DataFactory::buildDataBox ("complex-data-array-dynamic");
00497       Bottle b;
00498       int arraySize = 10;
00499       shared_ptr<DataBoxArray> array = d->getDataBoxArray ("x");
00500       for (int j = 0; j < arraySize; j++) {
00501         std::stringstream ss;
00502         shared_ptr<DataBox> data = DataFactory::buildDataBox ("simple-data");
00503         array->pushBack (data);
00504       }
00505       
00506       DataFactory::writeDataBoxToBottle (d, b);
00507       
00508       if (b.size() != 31) {
00509         throw Exception ("testDataSerialization : Bottle for complex-data-array-dynamic does not have size 31");
00510       }
00511     }
00512     
00513     
00514   }
00515   catch (Exception &exception) {
00516     cout << "ERROR:" << endl;
00517     cout << "\t" << exception.what() << endl;
00518     numErrors++;
00519   }
00520   catch (...) {
00521     cout << "How the hell did I end up here?!" << endl;
00522     numErrors++;
00523   }
00524   
00525   if (numErrors > 0) {
00526     return (1);
00527   }
00528   else {
00529     return (0);
00530   }
00531 }; // main()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3