testDataStructure.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 testDataStructure.cpp
00024  * @Description Tests the DataStructure framework implementation
00025  * @Status unknown
00026  * @Version $Id: testDataStructure.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa + Nelson Ramos
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 
00039 #include <Boolean.hpp>
00040 
00041 #include <Exception.hpp>
00042 
00043 
00044 
00045 using namespace mermaid::support::data;
00046 using mermaid::support::errorhandling::Exception;
00047 using std::string;
00048 using std::cout;
00049 using std::endl;
00050 
00051 
00052 
00053 int main (int argc, char * const argv[])
00054 {
00055   ACE_Trace::stop_tracing();
00056   
00057   int numErrors = 0;
00058   
00059   try {
00060     std::cerr << "DataStructure framework test" << endl;
00061     
00062     //test creation of integer value
00063     {
00064       std::cerr << "Testing creation of Integer" << std::endl;
00065       int testInt = 42;
00066       shared_ptr<Integer> i1 = DataFactory::buildInteger (testInt);
00067       int retrievedInt = i1->getValue();
00068       if (retrievedInt != testInt) {
00069         numErrors++;
00070         std::cerr << "ERROR: unexpected value read from Integer, expected " << testInt << ", received " << retrievedInt << std::endl;
00071       }
00072     }
00073     
00074     //test creation of double value
00075     {
00076       std::cerr << "Testing creation of Double" << std::endl;
00077       double testDouble = 3.14;
00078       shared_ptr<Double> d1 = DataFactory::buildDouble (testDouble);
00079       double retrievedDouble = d1->getValue();
00080       if (retrievedDouble != testDouble) {
00081         numErrors++;
00082         std::cerr << "ERROR: unexpected value read from Double, expected " << testDouble << ", received " << retrievedDouble << std::endl;
00083       }
00084     }
00085     
00086     //test creation of string value
00087     {
00088       std::cerr << "Testing creation of String" << std::endl;
00089       std::string  testString ("Hello MeRMaID");
00090       shared_ptr<String> s1 = DataFactory::buildString (testString);
00091       std::string retrievedString = s1->getValue();
00092       if (retrievedString != testString) {
00093         numErrors++;
00094         std::cerr << "ERROR: unexpected value read from String, expected " << testString << ", received " << retrievedString << std::endl;
00095       }
00096     }
00097     
00098     //test creation of static IntegerArray
00099     {
00100       std::cerr << "Testing creation of static IntegerArray" << std::endl;
00101       int arrayCreationSize = 2048;
00102       shared_ptr<IntegerArray> array = DataFactory::buildStaticIntegerArray (arrayCreationSize);
00103       if (array->isDynamic() == true) {
00104         numErrors++;
00105         std::cerr << "ERROR: unexpected response to isDynamic expected FALSE, received TRUE" << std::endl;
00106       }
00107       int arrayActualSize = array->getSize();
00108       if (arrayCreationSize != arrayActualSize) {
00109         numErrors++;
00110         std::cerr << "ERROR: unexpected static IntegerArray size, expected " << arrayCreationSize << ", received " << arrayActualSize << std::endl;
00111       }
00112       //check if all values are 0
00113       for (unsigned int i = 0; i < array->getSize(); i++) {
00114         int retrievedInt = ( (*array)) [i]->getValue();
00115         if (retrievedInt != 0) {
00116           numErrors++;
00117           std::cerr << "ERROR: unexpected value read from IntegerArray, expected 0, received " << retrievedInt << std::endl;
00118         }
00119       }
00120       
00121       //assign all values their order number
00122       for (unsigned int i = 0; i < array->getSize(); i++) {
00123         (* (*array) [i]) = i;
00124       }
00125       
00126       //verify that all values have been assigned
00127       for (unsigned int i = 0; i < array->getSize(); i++) {
00128         int retrievedInt = ( (*array)) [i]->getValue();
00129         if (retrievedInt != (signed) i) {
00130           numErrors++;
00131           std::cerr << "ERROR: unexpected value read from IntegerArray, expected " << i << ", received " << retrievedInt << std::endl;
00132         }
00133       }
00134     } // test static IntegerArray
00135     
00136     //test creation of dynamic IntegerArray
00137     {
00138       std::cerr << "Testing creation of dynamic IntegerArray" << std::endl;
00139       unsigned int arrayTargetSize = 2048;
00140       shared_ptr<IntegerArray> array = DataFactory::buildDynamicIntegerArray();
00141       if (array->isDynamic() == false) {
00142         numErrors++;
00143         std::cerr << "ERROR: unexpected response to isDynamic expected TRUE, received FALSE" << std::endl;
00144       }
00145       
00146       if (array->getSize() != 0) {
00147         numErrors++;
00148         std::cerr << "ERROR: unexpected dynamic IntegerArray size, expected 0, received " << array->getSize() << std::endl;
00149       }
00150       
00151       //fill with ordered values
00152       for (unsigned int i = 0; i < arrayTargetSize; i++) {
00153         array->pushBack (i);
00154       }
00155       
00156       if (array->getSize() != arrayTargetSize) {
00157         numErrors++;
00158         std::cerr << "ERROR: unexpected dynamic IntegerArray size, expected " << arrayTargetSize << ", received " << array->getSize() << std::endl;
00159       }
00160       
00161       //verify that all values have been assigned
00162       for (unsigned int i = 0; i < array->getSize(); i++) {
00163         int retrievedInt = ( (*array)) [i]->getValue();
00164         if (retrievedInt != (signed) i) {
00165           numErrors++;
00166           std::cerr << "ERROR: unexpected value read from IntegerArray, expected " << i << ", received " << retrievedInt << std::endl;
00167         }
00168       }
00169     } // test dynamic IntegerArray
00170     
00171     //test creation of static DoubleArray
00172     {
00173       std::cerr << "Testing creation of static DoubleArray" << std::endl;
00174       int arrayCreationSize = 2048;
00175       shared_ptr<DoubleArray> array = DataFactory::buildStaticDoubleArray (arrayCreationSize);
00176       if (array->isDynamic() == true) {
00177         numErrors++;
00178         std::cerr << "ERROR: unexpected response to isDynamic expected FALSE, received TRUE" << std::endl;
00179       }
00180       int arrayActualSize = array->getSize();
00181       if (arrayCreationSize != arrayActualSize) {
00182         numErrors++;
00183         std::cerr << "ERROR: unexpected static DoubleArray size, expected " << arrayCreationSize << ", received " << arrayActualSize << std::endl;
00184       }
00185       //check if all values are 0
00186       for (unsigned int i = 0; i < array->getSize(); i++) {
00187         double retrievedDouble = ( (*array)) [i]->getValue();
00188         if (retrievedDouble != 0) {
00189           numErrors++;
00190           std::cerr << "ERROR: unexpected value read from DoubleArray, expected 0, received " << retrievedDouble << std::endl;
00191         }
00192       }
00193       
00194       //assign all values their order number
00195       for (unsigned int i = 0; i < array->getSize(); i++) {
00196         (* (*array) [i]) = (double) i;
00197       }
00198       
00199       //verify that all values have been assigned
00200       for (unsigned int i = 0; i < array->getSize(); i++) {
00201         double retrievedDouble = ( (*array)) [i]->getValue();
00202         if (retrievedDouble != (signed) i) {
00203           numErrors++;
00204           std::cerr << "ERROR: unexpected value read from DoubleArray, expected " << i << ", received " << retrievedDouble << std::endl;
00205         }
00206       }
00207     } // test static DoubleArray
00208     
00209     //test creation of dynamic DoubleArray
00210     {
00211       std::cerr << "Testing creation of dynamic DoubleArray" << std::endl;
00212       unsigned int arrayTargetSize = 2048;
00213       shared_ptr<DoubleArray> array = DataFactory::buildDynamicDoubleArray();
00214       if (array->isDynamic() == false) {
00215         numErrors++;
00216         std::cerr << "ERROR: unexpected response to isDynamic expected TRUE, received FALSE" << std::endl;
00217       }
00218       if (array->getSize() != 0) {
00219         numErrors++;
00220         std::cerr << "ERROR: unexpected dynamic DoubleArray size, expected 0, received " << array->getSize() << std::endl;
00221       }
00222       
00223       //fill with ordered values
00224       for (unsigned int i = 0; i < arrayTargetSize; i++) {
00225         array->pushBack ( (double) i);
00226       }
00227       
00228       if (array->getSize() != arrayTargetSize) {
00229         numErrors++;
00230         std::cerr << "ERROR: unexpected dynamic DoubleArray size, expected " << arrayTargetSize << ", received " << array->getSize() << std::endl;
00231       }
00232       
00233       //verify that all values have been assigned
00234       for (unsigned int i = 0; i < array->getSize(); i++) {
00235         double retrievedDouble = ( (*array)) [i]->getValue();
00236         if (retrievedDouble != (double) i) {
00237           numErrors++;
00238           std::cerr << "ERROR: unexpected value read from DoubleArray, expected " << ( (double) i) << ", received " << retrievedDouble << std::endl;
00239         }
00240       }
00241     } // test dynamic DoubleArray
00242     
00243     //test creation of static StringArray
00244     {
00245       std::cerr << "Testing creation of static StringArray" << std::endl;
00246       int arrayCreationSize = 2048;
00247       shared_ptr<StringArray> array = DataFactory::buildStaticStringArray (arrayCreationSize);
00248       if (array->isDynamic() == true) {
00249         numErrors++;
00250         std::cerr << "ERROR: unexpected response to isDynamic expected FALSE, received TRUE" << std::endl;
00251       }
00252       int arrayActualSize = array->getSize();
00253       if (arrayCreationSize != arrayActualSize) {
00254         numErrors++;
00255         std::cerr << "ERROR: unexpected static StringArray size, expected " << arrayCreationSize << ", received " << arrayActualSize << std::endl;
00256       }
00257       //check if all values are 0
00258       for (unsigned int i = 0; i < array->getSize(); i++) {
00259         string retrievedString = ( (*array)) [i]->getValue();
00260         if (retrievedString != "") {
00261           numErrors++;
00262           std::cerr << "ERROR: unexpected value read from StringArray, expected \"\", received \"" << retrievedString << "\"" << std::endl;
00263         }
00264       }
00265       
00266       //assign all values their order number
00267       for (unsigned int i = 0; i < array->getSize(); i++) {
00268         std::stringstream ss;
00269         ss << i;
00270         (* (*array) [i]) = ss.str();
00271       }
00272       
00273       //verify that all values have been assigned
00274       for (unsigned int i = 0; i < array->getSize(); i++) {
00275         std::string retrievedString = ( (*array)) [i]->getValue();
00276         std::stringstream ss;
00277         ss << i;
00278         if (retrievedString != ss.str()) {
00279           numErrors++;
00280           std::cerr << "ERROR: unexpected value read from StringArray, expected \"" << ss.str() << "\", received \"" << retrievedString << "\"" << std::endl;
00281         }
00282       }
00283     } // test static StringArray
00284     
00285     //test creation of dynamic StringArray
00286     {
00287       std::cerr << "Testing creation of dynamic StringArray" << std::endl;
00288       unsigned int arrayTargetSize = 2048;
00289       shared_ptr<StringArray> array = DataFactory::buildDynamicStringArray();
00290       if (array->isDynamic() == false) {
00291         numErrors++;
00292         std::cerr << "ERROR: unexpected response to isDynamic expected TRUE, received FALSE" << std::endl;
00293       }
00294       if (array->getSize() != 0) {
00295         numErrors++;
00296         std::cerr << "ERROR: unexpected dynamic StringArray size, expected 0, received " << array->getSize() << std::endl;
00297       }
00298       
00299       //fill with ordered values
00300       for (unsigned int i = 0; i < arrayTargetSize; i++) {
00301         std::stringstream ss;
00302         ss << i;
00303         array->pushBack (ss.str());
00304       }
00305       
00306       if (array->getSize() != arrayTargetSize) {
00307         numErrors++;
00308         std::cerr << "ERROR: unexpected dynamic StringArray size, expected " << arrayTargetSize << ", received " << array->getSize() << std::endl;
00309       }
00310       
00311       //verify that all values have been assigned
00312       for (unsigned int i = 0; i < array->getSize(); i++) {
00313         std::string retrievedString = ( (*array)) [i]->getValue();
00314         std::stringstream ss;
00315         ss << i;
00316         if (retrievedString != ss.str()) {
00317           numErrors++;
00318           std::cerr << "ERROR: unexpected value read from IntegerArray, expected " << ss.str() << ", received " << retrievedString << std::endl;
00319         }
00320       }
00321     } // test dynamic StringArray
00322     
00323     cout << "Ok." << endl;
00324     
00325   }
00326   catch (Exception &exception) {
00327     cout << "ERROR:" << endl;
00328     cout << "\t" << exception.what() << endl;
00329     numErrors++;
00330   }
00331   catch (...) {
00332     cout << "How the hell did I end up here?!" << endl;
00333     numErrors++;
00334   }
00335   
00336   if (numErrors > 0) {
00337     return (1);
00338   }
00339   else {
00340     return (0);
00341   }
00342 };
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3