testDataDeserialization.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 testDataDeserialization.cpp
00024  * @Description Tests the conversion of Data objects into ValueVectors
00025  * @Status unknown
00026  * @Version $Id: testDataDeserialization.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include <iostream>
00033 #include <sstream>
00034 #include <typeinfo>
00035 #include <ace/Trace.h>
00036 
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: testDataDeserialization [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       
00083       Bottle b;
00084       b.addInt (42);
00085       
00086       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("lonely-int", b);
00087       
00088       int i = db->getInteger ("lonely");
00089       
00090       if (i != 42) {
00091         throw Exception ("testDataDeserialization : retrieved integer does not have the expected value");
00092       }
00093     }
00094     
00095     // test integer array static
00096     {
00097       cout << "Testing static integer array" << endl;
00098       
00099       Bottle b;
00100       b.addInt (0);
00101       b.addInt (1);
00102       b.addInt (2);
00103       
00104       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("int-array-static", b);
00105       
00106       shared_ptr<IntegerArray> ia = db->getIntegerArray ("intarray");
00107       
00108       if (ia->isDynamic() == true) {
00109         throw Exception ("testDataDeserialization : expected static array");
00110       }
00111       
00112       if (ia->getSize() != 3) {
00113         throw Exception ("testDataDeserialization : unexpected array size");
00114       }
00115       
00116       for (int j = 0; j < 3; j++) {
00117         int i = (*ia) [j]->getValue();
00118         if (j != i) {
00119           throw Exception ("testDeserialization : unexpected array element value");
00120         }
00121       }
00122       
00123     }
00124     
00125     // test integer dynamic array
00126     {
00127       cout << "Testing dynamic integer array" << endl;
00128       
00129       Bottle b;
00130       b.addInt (13);
00131       
00132       for (int i = 0; i < 13; i++) {
00133         b.addInt (i);
00134       }
00135       
00136       
00137       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("int-array-dynamic", b);
00138       
00139       shared_ptr<IntegerArray> array = db->getIntegerArray ("intarray");
00140       int arraySize = array->getSize();
00141       
00142       if (array->isDynamic() == false) {
00143         throw Exception ("testDataDeserialization : expected dynamic array");
00144       }
00145       
00146       if (arraySize != 13) {
00147         throw Exception ("testDeserialization : unexpeced array size");
00148       }
00149       
00150       for (int j = 0; j < arraySize; j++) {
00151         int i = (*array) [j]->getValue();
00152         
00153         if (i != j) {
00154           throw Exception ("testDeserialization : unexpected value in array");
00155         }
00156       }
00157       
00158     }
00159     
00160     // test double
00161     {
00162       cout << "Testing single double" << endl;
00163       
00164       Bottle b;
00165       b.addDouble (3.14);
00166       
00167       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("lonely-double", b);
00168       
00169       double d = db->getDouble ("lonely");
00170       
00171       if (d != 3.14) {
00172         throw Exception ("testDeserialization : unexpected double value");
00173       }
00174     }
00175     
00176     //test double static array
00177     {
00178       cout << "Testing static double array" << endl;
00179       
00180       Bottle b;
00181       b.addDouble (0.0);
00182       b.addDouble (1.1);
00183       b.addDouble (2.2);
00184       
00185       shared_ptr<DataBox> d = DataFactory::buildDataBoxFromStructureAndBottle ("double-array-static", b);
00186       
00187       shared_ptr<DoubleArray> da = d->getDoubleArray ("doublearray");
00188       
00189       if (da->isDynamic() == true) {
00190         throw Exception ("testDataDeserialization : expected static array");
00191       }
00192       
00193       if (da->getSize() != 3) {
00194         throw Exception ("testDataDeserialization : unexpected array size");
00195       }
00196       
00197       for (int j = 0; j < 3; j++) {
00198         double i = (*da) [j]->getValue();
00199         if ( (j*1.1) != i) {
00200           throw Exception ("testDeserialization : unexpected array element value");
00201         }
00202       }
00203     }
00204     
00205     //test double dynamic array
00206     {
00207       cout << "Testing dynamic double array" << endl;
00208       
00209       Bottle b;
00210       b.addInt (13);
00211       
00212       for (int i = 0; i < 13; i++) {
00213         b.addDouble (i*1.1);
00214       }
00215       
00216       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("double-array-dynamic", b);
00217       
00218       
00219       shared_ptr<DoubleArray> array = db->getDoubleArray ("doublearray");
00220       int arraySize = array->getSize();
00221       
00222       if (array->isDynamic() == false) {
00223         throw Exception ("testDataDeserialization : expected dynamic array");
00224       }
00225       
00226       if (arraySize != 13) {
00227         throw Exception ("testDeserialization : unexpeced array size");
00228       }
00229       
00230       for (int j = 0; j < arraySize; j++) {
00231         double d = (*array) [j]->getValue();
00232         if (abs (d - (j*1.1)) > 0.00001) {
00233           throw Exception ("testDeserialization : unexpected value in array");
00234         }
00235       }
00236     }
00237     
00238     
00239     //test string
00240     {
00241       cout << "Testing single string" << endl;
00242       
00243       Bottle b;
00244       b.addString ("hello");
00245       
00246       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("lonely-string", b);
00247       
00248       std::string s = db->getString ("lonely");
00249       
00250       if (s.compare ("hello") != 0) {
00251         throw Exception ("testDataDeserialization : retrieved string does not have the expected value");
00252       }
00253       
00254     }
00255     
00256     //test string static array
00257     {
00258       cout << "Testing static string array" << endl;
00259       
00260       Bottle b;
00261       b.addString ("hello");
00262       b.addString ("world");
00263       b.addString ("!");
00264       
00265       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("string-array-static", b);
00266       
00267       shared_ptr<StringArray> sa = db->getStringArray ("stringarray");
00268       
00269       if (sa->isDynamic() == true) {
00270         throw Exception ("testDataDeserialization : expected static array");
00271       }
00272       
00273       if (sa->getSize() != 3) {
00274         throw Exception ("testDataDeserialization : unexpected array size");
00275       }
00276       
00277       {
00278         std::string s = (*sa) [0]->getValue();
00279         if (s.compare ("hello") != 0) {
00280           throw Exception ("testDeserialization : unexpected array element value");
00281         }
00282       }
00283       
00284       {
00285         std::string s = (*sa) [1]->getValue();
00286         if (s.compare ("world") != 0) {
00287           throw Exception ("testDeserialization : unexpected array element value");
00288         }
00289       }
00290       
00291       {
00292         std::string s = (*sa) [2]->getValue();
00293         if (s.compare ("!") != 0) {
00294           throw Exception ("testDeserialization : unexpected array element value");
00295         }
00296       }
00297       
00298     }
00299     
00300     //test string dynamic array
00301     {
00302       cout << "Testing dynamic string array" << endl;
00303       
00304       Bottle b;
00305       b.addInt (4);
00306       b.addString ("The");
00307       b.addString ("big brown fox");
00308       b.addString ("jumped over");
00309       b.addString ("the lazy dog.");
00310       
00311       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("string-array-dynamic", b);
00312       
00313       shared_ptr<StringArray> array = db->getStringArray ("stringarray");
00314       int arraySize = array->getSize();
00315       
00316       if (array->isDynamic() == false) {
00317         throw Exception ("testDataDeserialization : expected dynamic array");
00318       }
00319       
00320       if (arraySize != 4) {
00321         throw Exception ("testDeserialization : unexpeced array size");
00322       }
00323       
00324       
00325       {
00326         std::string s = (*array) [0]->getValue();
00327         if (s.compare ("The") != 0) {
00328           throw Exception ("testDeserialization : unexpected value in array");
00329         }
00330       }
00331       
00332       {
00333         std::string s = (*array) [1]->getValue();
00334         if (s.compare ("big brown fox") != 0) {
00335           throw Exception ("testDeserialization : unexpected value in array");
00336         }
00337       }
00338       
00339       {
00340         std::string s = (*array) [2]->getValue();
00341         if (s.compare ("jumped over") != 0) {
00342           throw Exception ("testDeserialization : unexpected value in array");
00343         }
00344       }
00345       
00346       {
00347         std::string s = (*array) [3]->getValue();
00348         if (s.compare ("the lazy dog.") != 0) {
00349           throw Exception ("testDeserialization : unexpected value in array");
00350         }
00351       }
00352     }
00353     
00354     //test simple data box
00355     {
00356       cout << "Testing simple DataBox" << endl;
00357       
00358       Bottle b;
00359       b.addInt (42);
00360       b.addDouble (3.14);
00361       b.addString ("Hello world!");
00362       
00363       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("simple-data", b);
00364       
00365       int i = db->getInteger ("a");
00366       if (i != 42) {
00367         throw Exception ("testDeserialization : unexpected value");
00368       }
00369       
00370       double d = db->getDouble ("b");
00371       if (abs (d - 3.14) > 0.00001) {
00372         throw Exception ("testDeserialization : unexpected value");
00373       }
00374       
00375       std::string s = db->getString ("c");
00376       if (s.compare ("Hello world!") != 0) {
00377         throw Exception ("testDeserialization : unexpected value");
00378       }
00379     }
00380     
00381     //test complex data box
00382     {
00383       cout << "Testing complex DataBox" << endl;
00384       
00385       Bottle b;
00386       b.addInt (42);
00387       b.addDouble (3.14);
00388       b.addString ("Hello world!");
00389       b.addInt (84);
00390       b.addDouble (6.28);
00391       b.addString ("Hello hello world world!");
00392       
00393       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("complex-data", b);
00394       
00395       shared_ptr<DataBox> x = db->getDataBox ("x");
00396       shared_ptr<DataBox> y = db->getDataBox ("y");
00397       
00398       int xa = x->getInteger ("a");
00399       double xb = x->getDouble ("b");
00400       std::string xc = x->getString ("c");
00401       
00402       if (xa != 42) {
00403         throw Exception ("testDeserialization : unexpected value");
00404       }
00405       
00406       if (abs (xb - 3.14) > 0.00001) {
00407         throw Exception ("testDeserialization : unexpected value");
00408       }
00409       
00410       if (xc.compare ("Hello world!") != 0) {
00411         throw Exception ("testDeserialization : unexpected value");
00412       }
00413       
00414       int ya = y->getInteger ("a");
00415       double yb = y->getDouble ("b");
00416       std::string yc = y->getString ("c");
00417       
00418       if (ya != 84) {
00419         throw Exception ("testDeserialization : unexpected value");
00420       }
00421       
00422       if (abs (yb - 6.28) > 0.00001) {
00423         throw Exception ("testDeserialization : unexpected value");
00424       }
00425       
00426       if (yc.compare ("Hello hello world world!") != 0) {
00427         throw Exception ("testDeserialization : unexpected value");
00428       }
00429     }
00430     
00431     //test complex data static array
00432     {
00433       cout << "Testing complex DataBox static array" << endl;
00434       
00435       Bottle b;
00436       
00437       for (int i = 0; i < 5; i++) {
00438         b.addInt (42*i);
00439         b.addDouble (3.14*i);
00440         b.addString ("Hello world!");
00441       }
00442       
00443       
00444       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("complex-data-array-static", b);
00445       
00446       
00447       shared_ptr<DataBoxArray> x = db->getDataBoxArray ("x");
00448       shared_ptr<DataBoxArray> y = db->getDataBoxArray ("y");
00449       
00450       if (x->isDynamic() != false) {
00451         throw Exception ("testDataDeserialization : expected static array");
00452       }
00453       
00454       unsigned int xSize = x->getSize();
00455       if (xSize != 2) {
00456         throw Exception ("testDataDeserialization : unexpected array size");
00457       }
00458       
00459       
00460       if (y->isDynamic() != false) {
00461         throw Exception ("testDataDeserialization : expected static array");
00462       }
00463       
00464       unsigned int ySize = y->getSize();
00465       if (ySize != 3) {
00466         throw Exception ("testDataDeserialization : unexpected array size");
00467       }
00468       
00469       
00470       for (unsigned int i = 0; i < xSize; i++) {
00471         shared_ptr<DataBox> db = (*x) [i];
00472         
00473         int a = db->getInteger ("a");
00474         double b = db->getDouble ("b");
00475         std::string c = db->getString ("c");
00476         
00477         if (a != (int) (i*42)) {
00478           throw Exception ("testDataDeserialization : unexpected value");
00479         }
00480         
00481         if (abs (b - (i*3.14)) > 0.00001) {
00482           throw Exception ("testDataDeserialization : unexpected value");
00483         }
00484         
00485         if (c.compare ("Hello world!") != 0) {
00486           throw Exception ("testDataDeserialization : unexpected value");
00487         }
00488       }
00489       
00490       for (unsigned int i = 0; i < ySize; i++) {
00491         shared_ptr<DataBox> db = (*y) [i];
00492         
00493         int a = db->getInteger ("a");
00494         double b = db->getDouble ("b");
00495         std::string c = db->getString ("c");
00496         
00497         if (a != (int) ( (i + 2) *42)) {
00498           throw Exception ("testDataDeserialization : unexpected value");
00499         }
00500         
00501         if (abs (b - ( (i + 2) *3.14)) > 0.00001) {
00502           throw Exception ("testDataDeserialization : unexpected value");
00503         }
00504         
00505         if (c.compare ("Hello world!") != 0) {
00506           throw Exception ("testDataDeserialization : unexpected value");
00507         }
00508       }
00509       
00510       
00511       //db->print();
00512       //std::cerr << std::endl;
00513     }
00514     
00515     //test complex data dynamic array
00516     {
00517       cout << "Testing complex DataBox dynamic array" << endl;
00518       
00519       Bottle b;
00520       
00521       int arraySize = 50;
00522       
00523       b.addInt (arraySize);
00524       
00525       for (int i = 0; i < arraySize; i++) {
00526         b.addInt (i*42);
00527         b.addDouble (i*3.14);
00528         b.addString ("Hello world!");
00529       }
00530       
00531       shared_ptr<DataBox> db = DataFactory::buildDataBoxFromStructureAndBottle ("complex-data-array-dynamic", b);
00532       
00533       shared_ptr<DataBoxArray> x = db->getDataBoxArray ("x");
00534       
00535       if (x->isDynamic() != true) {
00536         throw Exception ("testDataDeserialization : expected dynamic array");
00537       }
00538       
00539       unsigned int xSize = x->getSize();
00540       if (xSize != 50) {
00541         throw Exception ("testDataDeserialization : unexpected array size");
00542       }
00543       
00544       for (unsigned int i = 0; i < xSize; i++) {
00545         shared_ptr<DataBox> db = (*x) [i];
00546         
00547         int a = db->getInteger ("a");
00548         double b = db->getDouble ("b");
00549         std::string c = db->getString ("c");
00550         
00551         if (a != (int) (i*42)) {
00552           throw Exception ("testDataDeserialization : unexpected value");
00553         }
00554         
00555         if (abs (b - (i*3.14)) > 0.00001) {
00556           throw Exception ("testDataDeserialization : unexpected value");
00557         }
00558         
00559         if (c.compare ("Hello world!") != 0) {
00560           throw Exception ("testDataDeserialization : unexpected value");
00561         }
00562       }
00563       
00564       //db->print();
00565       //std::cerr << std::endl;
00566     }
00567     
00568     
00569   }
00570   catch (Exception &exception) {
00571     cout << "ERROR:" << endl;
00572     cout << "\t" << exception.what() << endl;
00573     numErrors++;
00574   }
00575   catch (...) {
00576     cout << "How the hell did I end up here?!" << endl;
00577     numErrors++;
00578   }
00579   
00580   if (numErrors > 0) {
00581     return (1);
00582   }
00583   else {
00584     return (0);
00585   }
00586 }; // main()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3