From: Fernyqc <fe...@us...> - 2005-12-12 18:24:54
|
Update of /cvsroot/robotflow/RobotFlow/ut/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19653/ut/src Added Files: Makefile.am UtLoad.cc UtSave.cc Log Message: Files of the unit test blocks --- NEW FILE: Makefile.am --- ## Process this file with automake to produce Makefile.in. -*-Makefile-*- # $Id: Makefile.am,v 1.1 2005/12/12 18:24:45 fernyqc Exp $ # Disable automatic dependency tracking if using other tools than gcc and gmake #AUTOMAKE_OPTIONS = no-dependencies lib_LTLIBRARIES = libRFut.la # Sources for compilation in the library libRFut_la_SOURCES = UtLoad.cc \ UtSave.cc libRFut_la_LDFLAGS = -release $(LT_RELEASE) INCLUDES = -I../include $(OVERFLOW_INCLUDE) install-data-local: echo "Installing libRFut in $(libdir)" mkdir -p $(libdir) (perl $(OVERFLOW_BIN)/info2def.pl $(libRFut_la_SOURCES) > $(prefix)/RFut.def) (rm -f $(libdir)/RFut.tlb; cd $(libdir); ln -s libRFut.so RFut.tlb) --- NEW FILE: UtLoad.cc --- /* Copyright (C) 2005 Patrick Frenette (pat...@us...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "BufferedNode.h" #include <Vector.h> #include <exception> #include <string> #include <fstream> #include <iostream> using namespace std; using namespace FD; namespace RobotFlow { class UtLoad; DECLARE_NODE(UtLoad) /*Node * * @name UtLoad * @category UnitTest * @description Save input(s) to a file * * @output_name EOF * @output_type bool * @output_description Is the file empty * * @parameter_name HEADERFILE * @parameter_type string * @parameter_value * @parameter_description The filename of the header definition * * @parameter_name TESTFILE * @parameter_type string * @parameter_value * @parameter_description The filename containing the data for the test * END*/ class UtFileException: public std::exception { virtual const char* what() const throw() { return "There are problems with the file (empty or non-existent)"; } }; class UtUnsupportedDataType: public std::exception { virtual const char* what() const throw() { return "Unsupported data type"; } }; class UtOutputMismatch: public std::exception { virtual const char* what() const throw() { return "EOF output as not the index 0. Don't delete it when adding a new block"; } }; class UtLoad : public BufferedNode { private: // Output int m_eofID; // File descriptor ifstream m_file; // Type header std::vector<std::string> m_header; public: UtLoad(std::string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { // --------------------- // Data type header file // --------------------- RCPtr<String> strHeader = parameters.get("HEADERFILE"); ifstream fileHeader; fileHeader.open(strHeader->val().c_str()); if( !fileHeader.is_open() ) { throw UtFileException(); } else { std::string dataType; while(fileHeader.good()) { fileHeader >> dataType; if(fileHeader.good()) { m_header.push_back(dataType); } } } fileHeader.close(); // --------------------- // Testfile managment // --------------------- RCPtr<String> strFile = parameters.get("TESTFILE"); m_file.open(strFile->val().c_str()); if( !m_file.is_open() ) { throw UtFileException(); } } int translateOutput (string outputName) { // Prevent the registration of the same output for (unsigned int i=0; i<outputNames.size(); i++) { if (outputNames[i] == outputName) { return i; } } // Get an output ID and save the one for EOF int numOutput = addOutput(outputName); if(outputName == "EOF") { m_eofID = numOutput; // Throw exception if the EOF is not the outputID 0 if(m_eofID != 0) { throw UtOutputMismatch(); } } //std::cout << "translateOutput -> " << outputName << ", " << numOutput << std::endl; return numOutput; } void calculate(int output_id, int count, Buffer &out) { // Deal with the output if(output_id == m_eofID) { // Return the EOF state (*outputs[output_id].buffer)[count] = ObjectRef(Bool::alloc(!m_file.good())); //std::cout << "calculate -> " << output_id << ", EOF -> " << !m_file.good() << std::endl; } else { // Default output value -> unsuported data type ObjectRef output = nilObject; // Always be careful for the file error (EOF for example) if(m_file.good()) { // Substract 1 to the output_id to consider the output EOF int outID = output_id - 1; // Read the data according to the data type if(m_header[outID] == "String") { std::string data; m_file >> data; if(m_file.good()) { output = ObjectRef(new String(data.c_str())); //std::cout << "utLoad -> " << output_id << ", data -> " << data << std::endl; } } else if(m_header[outID] == "Int") { int data; m_file >> data; if(m_file.good()) { output = ObjectRef(Int::alloc(data)); //std::cout << "utLoad -> " << output_id << ", data -> " << data << std::endl; } } else if(m_header[outID] == "Float") { float data; m_file >> data; if(m_file.good()) { output = ObjectRef(Float::alloc(data)); //std::cout << "utLoad -> " << output_id << ", data -> " << data << std::endl; } } else if(m_header[outID] == "Bool") { bool data; m_file >> data; if(m_file.good()) { output = ObjectRef(Bool::alloc(data)); //std::cout << "utLoad -> " << output_id << ", data -> " << data << std::endl; } } else if(m_header[outID] == "Vector<int>") { // Read the numbers of elements int nb = 0; m_file >> nb; if(m_file.good()) { // Create the vector Vector<int> *data = Vector<int>::alloc(nb); std::cout << "utLoad -> " << output_id << ", data -> " << data->size() << " "; // Read and save the value for(unsigned int i=0; i<nb; ++i) { if(m_file.good()) { m_file >> (*data)[i]; std::cout << (*data)[i] << " "; } } std::cout << std::endl; // Output the vector if(m_file.good()) { // Ouput the vector output = ObjectRef(data); } } } else { // Unsupported data type throw UtUnsupportedDataType(); } } // Output (*outputs[output_id].buffer)[count] = output; //if(output->isNil()) //{ // std::cout << "utLoad -> " << output_id << ", data -> nilObject" << std::endl; //} } } }; }//namespace RobotFlow --- NEW FILE: UtSave.cc --- /* Copyright (C) 2005 Patrick Frenette (pat...@us...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "BufferedNode.h" #include <Vector.h> #include <exception> #include <string> #include <fstream> #include <iostream> using namespace std; using namespace FD; namespace RobotFlow { class UtSave; DECLARE_NODE(UtSave) /*Node * * @name UtSave * @category UnitTest * @description Save input(s) to a file * * @output_name NILOUTPUT * @output_type any * @output_description * * @parameter_name FILENAME * @parameter_type string * @parameter_value * @parameter_description The filename to save the inputs * END*/ class UtCannotOpenFile: public std::exception { virtual const char* what() const throw() { return "Cannot open the file. Look at the parameter value."; } }; class UtSave : public BufferedNode { private: //outputs int m_outputID; // File descriptor ofstream m_file; public: UtSave(std::string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { m_outputID = addOutput("NILOUTPUT"); // Get the filename RCPtr<String> strFile = parameters.get("FILENAME"); // Open the stream m_file.open(strFile->val().c_str()); if( !m_file.is_open() ) { std::cout << "UtSave -> Cannot open the file. Look at the parameter value." << std::endl; throw UtCannotOpenFile(); } } int translateInput (std::string inputName) { for (unsigned int i=0; i< inputs.size(); i++) { if (inputs[i].name == inputName) { return i; } } return addInput(inputName); } void calculate(int output_id, int count, Buffer &out) { // Always output a nil object out[count] = nilObject; // For each input bool isInput = false; for (unsigned int i=0; i<inputs.size(); ++i) { // Write the input to the file ObjectRef in = getInput(i, count); if(m_file.good()) { if(!in->isNil()) { isInput = true; // Get the className std::string className(in->className()); // Print out the supported datatype if(className == "String") { RCPtr<String> params = in; m_file << params->val(); //std::cout << "input -> " << i << ", string -> " << params->val() << std::endl; } else if(className == "Int") { int inInt = dereference_cast<int>(in); m_file << inInt; //std::cout << "input -> " << i << ", int -> " << inInt << std::endl; } else if(className == "Float") { float inFloat = dereference_cast<float>(in); m_file << inFloat; //std::cout << "input -> " << i << ", float -> " << inFloat << std::endl; } else if(className == "Bool") { bool inBool = dereference_cast<bool>(in); m_file << inBool; //std::cout << "input -> " << i << ", bool -> " << inBool << std::endl; } else if(className == "Vector<int>") { Vector<int> &inVector = object_cast<Vector<int> >(in); m_file << inVector.size() << " "; //std::cout << "input -> " << i << ", vector<int> -> "; for(unsigned int i=0; i<inVector.size(); ++i) { if(m_file.good()) { m_file << inVector[i]; //std::cout << inVector[i]; if(m_file.good() && i != inVector.size()-1) { m_file << " "; //std::cout << " "; } } } //std::cout << std::endl; } else { // Unsupported type m_file << "Unsupported"; //std::cout << "input -> " << i << ", unsupported" << std::endl; } } else { m_file << "NilObject"; //std::cout << "input -> " << i << ", nilObject" << std::endl; } // Add a space except at the end of the line if((m_file.good()) && (i < inputs.size() - 1)) { m_file << " "; } } } // Newline if(m_file.good() && isInput) { m_file << std::endl; } } }; }//namespace RobotFlow |