testDataDescription.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "config.h"
00032
00033
00034 #include "DataBox.hpp"
00035 #include "DataFactory.hpp"
00036 #include <Exception.hpp>
00037
00038 #include <FilePathSearch.hpp>
00039
00040 #include <iostream>
00041 #include <ace/Trace.h>
00042
00043
00044 using namespace mermaid::support::data;
00045 using mermaid::support::errorhandling::Exception;
00046 using mermaid::support::system::FilePathSearch;
00047 using std::string;
00048 using std::cout;
00049 using std::endl;
00050
00051 using yarp::os::Bottle;
00052
00053
00054 int main (int argc, char * const argv[])
00055 {
00056 ACE_Trace::stop_tracing();
00057
00058 bool errorOccured = false;
00059
00060 if (argc != 2) {
00061 cout << "Usage: testDataDescription [filename]" << endl;
00062 return (1);
00063 }
00064
00065 try {
00066 std::cerr << "DataStructure description test" << endl;
00067
00068 FilePathSearch::addSearchPath ("./");
00069 FilePathSearch::addSearchPath ("/");
00070
00071 shared_ptr<XmlDocument> doc = shared_ptr<XmlDocument> (new XmlDocument());
00072 cout << "Reading from file: " << argv[1] << endl;
00073 doc->parseFile (string (argv[1]));
00074 DataFactory::loadDataDescription (doc);
00075
00076 try {
00077 DataFactory::buildDataBox ("xpto");
00078 std::cerr << "ERROR:\tExpected to get an Exception for invalid data structure type name" << std::endl;
00079 errorOccured = true;
00080 }
00081 catch (Exception &exception) {
00082 std::cerr << "OK:\tGot an Exception due to an invalid data structure type name" << std::endl;
00083
00084 }
00085
00086 {
00087 std::cerr << "Building \"simple-data\"" << std::endl;
00088 shared_ptr<DataBox> db1 = DataFactory::buildDataBox ("simple-data");
00089
00090 if (db1) {
00091 cout << "db1 structure:\t" << db1->getBoxStructureName() << endl;
00092
00093 {
00094 shared_ptr<DataValue> dva = db1->getValue ("a");
00095 string dvas = *dva;
00096
00097 cout << "db1: a: " << dvas << endl;
00098 }
00099
00100 {
00101 shared_ptr<DataValue> dvb = db1->getValue ("b");
00102 string dvbs = *dvb;
00103
00104 cout << "db1: b: " << dvbs << endl;
00105 }
00106
00107 {
00108 shared_ptr<DataValue> dvc = db1->getValue ("c");
00109 string dvcs = *dvc;
00110
00111 cout << "db1: c: " << dvcs << endl;
00112 }
00113 }
00114 else {
00115 cout << "ERROR:\tDataFactory did not build a valid Data for structure type name \"simple-data\"" << std::endl;
00116 errorOccured = true;
00117 };
00118 }
00119
00120
00121 {
00122 shared_ptr<DataBox> db = DataFactory::buildDataBox ("lonely-int");
00123
00124 shared_ptr<DataValue> boxvalue = db->getValue ("lonely");
00125 shared_ptr<DataValue> vectorvalue = db->getDataValueVector()->getValue (0);
00126
00127 if (boxvalue.get() != vectorvalue.get()) {
00128 cout << "ERROR:\tPointers are not equal when accessing by name or by the value vector" << std::endl;
00129 errorOccured = true;
00130 }
00131 }
00132
00133 {
00134 shared_ptr<DataBox> db = DataFactory::buildDataBox ("int-array-dynamic");
00135 shared_ptr<IntegerArray> intarray = db->getIntegerArray ("intarray");
00136 std::cerr << (std::string) ( (*intarray)) << std::endl;
00137
00138 intarray->pushBack (42);
00139 std::cerr << (std::string) ( (*intarray)) << std::endl;
00140 }
00141
00142 {
00143 shared_ptr<DataBox> db = DataFactory::buildDataBox ("int-array-static");
00144 shared_ptr<IntegerArray> intarray = db->getIntegerArray ("intarray");
00145 std::cerr << (std::string) ( (*intarray)) << std::endl;
00146
00147
00148
00149 }
00150
00151 {
00152 shared_ptr<DataBox> db = DataFactory::buildDataBox ("double-array-dynamic");
00153 shared_ptr<DoubleArray> doublearray = db->getDoubleArray ("doublearray");
00154 std::cerr << (std::string) ( (*doublearray)) << std::endl;
00155
00156 doublearray->pushBack (3.14);
00157 std::cerr << (std::string) ( (*doublearray)) << std::endl;
00158 }
00159
00160 {
00161 shared_ptr<DataBox> db = DataFactory::buildDataBox ("double-array-static");
00162 shared_ptr<DoubleArray> doublearray = db->getDoubleArray ("doublearray");
00163 std::cerr << (std::string) ( (*doublearray)) << std::endl;
00164
00165
00166
00167 }
00168
00169 {
00170 shared_ptr<DataBox> db = DataFactory::buildDataBox ("string-array-dynamic");
00171 shared_ptr<StringArray> stringarray = db->getStringArray ("stringarray");
00172 std::cerr << (std::string) ( (*stringarray)) << std::endl;
00173
00174 stringarray->pushBack ("Hello MeRMaID");
00175 std::cerr << (std::string) ( (*stringarray)) << std::endl;
00176 }
00177
00178 {
00179 shared_ptr<DataBox> db = DataFactory::buildDataBox ("string-array-static");
00180 shared_ptr<StringArray> stringarray = db->getStringArray ("stringarray");
00181 std::cerr << (std::string) ( (*stringarray)) << std::endl;
00182
00183
00184
00185 }
00186
00187 {
00188 std::cerr << "Building \"complex-data\"" << std::endl;
00189 shared_ptr<DataBox> db1 = DataFactory::buildDataBox ("complex-data");
00190
00191 shared_ptr<DataBox> boxX = db1->getDataBox ("x");
00192 shared_ptr<DataBox> boxY = db1->getDataBox ("y");
00193
00194 boxX->setInteger ("a", 1);
00195 boxY->setInteger ("a", 2);
00196
00197 if (boxX->getInteger ("a") != 1) {
00198 cout << "ERROR: expected boxX.a to be 1, instead it is " << boxX->getInteger ("a") << endl;
00199 errorOccured = true;
00200 }
00201 }
00202
00203 }
00204 catch (Exception &exception) {
00205 cout << "ERROR:";
00206 cout << "\t" << exception.what() << endl;
00207 errorOccured = true;
00208 }
00209 catch (...) {
00210 cout << "How the hell did I end up here?!" << endl;
00211 errorOccured = true;
00212 }
00213
00214 if (errorOccured) {
00215 std::cerr << "ERROR occured" << std::endl;
00216 return (1);
00217 }
00218 else {
00219 std::cerr << "All went well." << std::endl;
00220 return (0);
00221 }
00222 };