Commit 277cd164 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Add program to test generated code

parent 811f2d15
project(Phoenix) project(Phoenix)
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
call_generator(test_base_config_lib baseConfig.ph5) call_generator(test_base_config_lib baseConfig.ph5)
add_executable(test_base_config main.cpp)
target_link_libraries(test_base_config test_base_config_lib ${HDF5_CXX_LIBRARIES})
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include "baseConfig.h"
///Test to write data
/** @param fileName : name of the file to be written
* @return true on success, false otherwise
*/
bool testWriteData(const std::string & fileName){
TableParameters table;
size_t nbRow(10lu), nbCol(5lu);
table.setAllDim(nbCol); //Always set the dimentions before resize call
table.resize(nbRow);
//Let's set some values in the Table
for(size_t i(0lu); i < nbRow; ++i){
table.setX(i, i);
table.setY(i, 2lu*i);
float * ptrTab = table.getTabValue(i);
for(size_t j(0lu); j < nbCol; ++j){
ptrTab[j] = i*nbCol + j;
}
}
//Now let's open a file
H5::H5File file(fileName, H5F_ACC_TRUNC);
//And write our table directly in it
table.write(file);
//We can also explicitly cloase the file, otherwise it will do it automatically
file.close();
return true;
}
///Test to read data
/** @param fileName : name of the file to be read
* @return true on success, false otherwise
*/
bool testReadData(const std::string & fileName){
//First let's open a file in read only mode (better for concurencial access)
H5::H5File file(fileName, H5F_ACC_RDONLY);
//Let's create our table
TableParameters table;
//Add read the file
table.read(file);
//Now we can close the file and use the Table directly
file.close();
//Get the number of rows and number of columns
size_t nbRow = table.getNbRow(), nbCol = table.getNbCol();
bool b(true);
for(size_t i(0lu); i < nbRow; ++i){
b &= table.getX(i) == i;
b &= table.getY(i) == 2lu*i;
float * ptrTab = table.getTabValue(i);
for(size_t j(0lu); j < nbCol; ++j){
b &= ptrTab[j] == i*nbCol + j;
}
}
return b;
}
int main(int argc, char** argv){
std::string fileName("baseConfig.h5");
if(!testWriteData(fileName)){
std::cerr << "Cannot write file '"<<fileName<<"'" << std::endl;
return -1;
}
if(!testReadData(fileName)){
std::cerr << "Cannot read file '"<<fileName<<"'" << std::endl;
return -1;
}
return 0;
}
...@@ -100,6 +100,20 @@ std::string ph5_backendTableHeaderDataType(const PTable & table){ ...@@ -100,6 +100,20 @@ std::string ph5_backendTableHeaderDataType(const PTable & table){
return body; return body;
} }
///Save the header of the given PTable
/** @param table : PTable to be used
* @return corresponding string
*/
std::string ph5_backendTableHeaderUpdateArrayDim(const PTable & table){
std::string body("");
const PVecAttribute & vecAttriute = table.getVecAttribute();
for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
if(!ph5_attributeIsTensor(*it)){continue;}
body += "\t\tvoid readDim" + firstToUpper(it->getName()) + "(const H5::CompType & compType);\n";
}
return body;
}
///Save the header of the given PTable ///Save the header of the given PTable
/** @param table : PTable to be used /** @param table : PTable to be used
* @return corresponding string * @return corresponding string
...@@ -131,8 +145,9 @@ std::string ph5_backendTableHeader(const PTable & table){ ...@@ -131,8 +145,9 @@ std::string ph5_backendTableHeader(const PTable & table){
body += "class " + name + "{\n"; body += "class " + name + "{\n";
body += "\tpublic:\n"; body += "\tpublic:\n";
body += "\t\t" + name + "();\n"; body += "\t\t" + name + "();\n";
body += "\t\tvirtual ~" + name + "();\n"; body += "\t\tvirtual ~" + name + "();\n\n";
body += "\t\tsize_t getNbRow() const;\n";
body += "\t\tvoid resize(size_t nbRow);\n"; body += "\t\tvoid resize(size_t nbRow);\n";
body += "\t\tvoid clear();\n\n"; body += "\t\tvoid clear();\n\n";
...@@ -168,6 +183,8 @@ std::string ph5_backendTableHeader(const PTable & table){ ...@@ -168,6 +183,8 @@ std::string ph5_backendTableHeader(const PTable & table){
body += "\t\tvoid writeDataSet(H5::DataSet & dataset) const;\n"; body += "\t\tvoid writeDataSet(H5::DataSet & dataset) const;\n";
body += "\t\tvoid allocate(size_t nbRow);\n\n"; body += "\t\tvoid allocate(size_t nbRow);\n\n";
body += ph5_backendTableHeaderUpdateArrayDim(table);
body += "\t\t///Number of rows in the table "+name+"\n"; body += "\t\t///Number of rows in the table "+name+"\n";
body += "\t\tsize_t p__nbRow;\n\n"; body += "\t\tsize_t p__nbRow;\n\n";
...@@ -547,10 +564,16 @@ std::string ph5_backendTableSourceReadDataSet(const PTable & table){ ...@@ -547,10 +564,16 @@ std::string ph5_backendTableSourceReadDataSet(const PTable & table){
body += "/**\t@param dataset : dataset to be used\n"; body += "/**\t@param dataset : dataset to be used\n";
body += "*/\n"; body += "*/\n";
body += "void "+name+"::readDataSet(const H5::DataSet & dataset){\n"; body += "void "+name+"::readDataSet(const H5::DataSet & dataset){\n";
body += "\tH5::CompType compType = dataset.getCompType();\n";
const PVecAttribute & vecAttriute = table.getVecAttribute();
for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
if(!ph5_attributeIsTensor(*it)){continue;}
body += "\treadDim" + firstToUpper(it->getName()) + "(compType);\n";
}
body += "\tH5::DataSpace dataSpace = dataset.getSpace();\n"; body += "\tH5::DataSpace dataSpace = dataset.getSpace();\n";
body += "\tsize_t nbEntries(dataSpace.getSimpleExtentNpoints());\n"; body += "\tsize_t nbEntries(dataSpace.getSimpleExtentNpoints());\n";
body += "\tresize(nbEntries);\n"; body += "\tresize(nbEntries);\n";
const PVecAttribute & vecAttriute = table.getVecAttribute();
for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){ for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
body += "\tdataset.read(p_"+it->getName()+", getCompType" + firstToUpper(it->getName()) + "());\n"; body += "\tdataset.read(p_"+it->getName()+", getCompType" + firstToUpper(it->getName()) + "());\n";
} }
...@@ -600,6 +623,41 @@ std::string ph5_backendTableSourceAllocate(const PTable & table){ ...@@ -600,6 +623,41 @@ std::string ph5_backendTableSourceAllocate(const PTable & table){
return body; return body;
} }
///Save the header of the given PTable
/** @param table : PTable to be used
* @return corresponding string
*/
std::string ph5_backendTableSourceUpdateArrayDim(const PTable & table){
std::string body(""), name(table.getName());
const PVecAttribute & vecAttriute = table.getVecAttribute();
for(PVecAttribute::const_iterator it(vecAttriute.begin()); it != vecAttriute.end(); ++it){
if(!ph5_attributeIsTensor(*it)){continue;}
const PVecDim & vecDim = it->getVecDim();
size_t nbDim(vecDim.size());
body += "///Update the dimentions of the tensor "+it->getName()+" (column '"+it->getColName()+"')\n";
body += "/**\t@param compType : main type to be used to update the tensor dimentions";
body += "*/\n";
body += "void "+name+"::readDim" + firstToUpper(it->getName()) + "(const H5::CompType & compType){\n";
body += "\tint indexCol = compType.getMemberIndex(\""+it->getColName()+"\");\n";
body += "\tH5::ArrayType arrayType = compType.getMemberArrayType(indexCol);\n";
body += "\t//Check if the number of dientions matches\n";
body += "\tif(arrayType.getArrayNDims() != "+convertToString(nbDim)+"){\n";
body += "\t\tstd::cerr << \""+name+"::readDim" + firstToUpper(it->getName()) + " wrong number of dimentions : expect "+convertToString(nbDim)+" but \"<< arrayType.getArrayNDims() <<\" provided from the file\" << std::endl;\n";
body += "\t\treturn;\t//We have to throw an exception!!!\n";
body += "\t}\n";
body += "\thsize_t dims["+convertToString(nbDim)+"];\n";
body += "\tarrayType.getArrayDims(dims);\n";
size_t i(0lu);
for(PVecDim::const_iterator itDim(vecDim.begin()); itDim != vecDim.end(); ++itDim){
if(itDim->getIsValue()){continue;}
body += "\tp_"+itDim->getName()+" = dims["+convertToString(i)+"];\n";
++i;
}
body += "}\n\n";
}
return body;
}
///Save the source of the given PTable ///Save the source of the given PTable
/** @param table : PTable to be used /** @param table : PTable to be used
* @return corresponding string * @return corresponding string
...@@ -623,6 +681,13 @@ std::string ph5_backendTableSource(const PTable & table){ ...@@ -623,6 +681,13 @@ std::string ph5_backendTableSource(const PTable & table){
body += "\tclear();\n"; body += "\tclear();\n";
body += "}\n\n"; body += "}\n\n";
body += "///Get the total number of rows in the current Table " + name + "\n";
body += "/**\t@return total number of rows\n";
body += "*/";
body += "size_t " + name + "::getNbRow() const{\n";
body += "\treturn p__nbRow;\n";
body += "}\n\n";
body += ph5_backendTableSourceResize(table); body += ph5_backendTableSourceResize(table);
body += ph5_backendTableSourceClear(table); body += ph5_backendTableSourceClear(table);
...@@ -642,6 +707,8 @@ std::string ph5_backendTableSource(const PTable & table){ ...@@ -642,6 +707,8 @@ std::string ph5_backendTableSource(const PTable & table){
body += ph5_backendTableSourceWriteDataSet(table); body += ph5_backendTableSourceWriteDataSet(table);
body += ph5_backendTableSourceAllocate(table); body += ph5_backendTableSourceAllocate(table);
body += ph5_backendTableSourceUpdateArrayDim(table);
return body; return body;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment