From f5cd842267c268860392d8106f3057efc699cc09 Mon Sep 17 00:00:00 2001 From: Kenneth Leiter Date: Tue, 25 May 2010 11:41:19 -0400 Subject: [PATCH] ENH: Add test for XdmfGridCollection. --- XdmfArray.cpp | 14 ++++--- XdmfGrid.cpp | 2 +- XdmfGridCollection.cpp | 1 + XdmfReader.cpp | 25 +++++++++-- XdmfReader.hpp | 2 +- tests/Cxx/CMakeLists.txt | 1 + tests/Cxx/TestXdmfArray.cpp | 2 +- tests/Cxx/TestXdmfGridCollection.cpp | 62 ++++++++++++++++++++++++++++ tests/Cxx/TestXdmfReader.cpp | 28 +++++++------ 9 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 tests/Cxx/TestXdmfGridCollection.cpp diff --git a/XdmfArray.cpp b/XdmfArray.cpp index 35efcc0a..79786a53 100644 --- a/XdmfArray.cpp +++ b/XdmfArray.cpp @@ -485,7 +485,7 @@ std::string XdmfArray::getType() const { return mHDF5Controller->getType(); } - return ""; + return "None"; } const void * const XdmfArray::getValuesPointer() const @@ -536,22 +536,26 @@ void XdmfArray::initialize(const std::string & type, const unsigned int precisio { this->initialize(); } - else if(type.compare("Double") == 0 && precision == 8) + else if(type.compare("Float") == 0 && precision == 8) { this->initialize(); } - else if(type.compare("UChar") == 0 && precision == 8) + else if(type.compare("UChar") == 0 && precision == 1) { this->initialize(); } - else if(type.compare("UShort") == 0 && precision == 8) + else if(type.compare("UShort") == 0 && precision == 2) { this->initialize(); } - else if(type.compare("UInt") == 0 && precision == 8) + else if(type.compare("UInt") == 0 && precision == 4) { this->initialize(); } + else if(type.compare("None") == 0) + { + // No-op uninitialized! + } else { assert(false); diff --git a/XdmfGrid.cpp b/XdmfGrid.cpp index a3bd7e1f..5b82e4cd 100644 --- a/XdmfGrid.cpp +++ b/XdmfGrid.cpp @@ -150,7 +150,7 @@ void XdmfGrid::populateItem(const std::map & itemPrope } else { - assert(false); + //assert(false); } } } diff --git a/XdmfGridCollection.cpp b/XdmfGridCollection.cpp index a363f190..daccbd43 100644 --- a/XdmfGridCollection.cpp +++ b/XdmfGridCollection.cpp @@ -65,6 +65,7 @@ void XdmfGridCollection::insert(boost::shared_ptr grid) void XdmfGridCollection::populateItem(const std::map & itemProperties, std::vector > & childItems) { + mCollectionType = XdmfGridCollectionType::New(itemProperties); for(std::vector >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter) { if(boost::shared_ptr grid = boost::shared_dynamic_cast(*iter)) diff --git a/XdmfReader.cpp b/XdmfReader.cpp index 9d2c2186..ea18b334 100644 --- a/XdmfReader.cpp +++ b/XdmfReader.cpp @@ -6,6 +6,7 @@ #include "XdmfDomain.hpp" #include "XdmfGeometry.hpp" #include "XdmfGrid.hpp" +#include "XdmfGridCollection.hpp" #include "XdmfSet.hpp" #include "XdmfTopology.hpp" @@ -49,7 +50,15 @@ public: } else if(itemTag.compare(XdmfGrid::ItemTag) == 0) { - newItem = XdmfGrid::New(); + std::map::const_iterator gridType = itemProperties.find("GridType"); + if(gridType->second.compare("Collection") == 0) + { + newItem = XdmfGridCollection::New(); + } + else + { + newItem = XdmfGrid::New(); + } } else if(itemTag.compare(XdmfSet::ItemTag) == 0) { @@ -76,7 +85,15 @@ public: if(currNode->type == XML_ELEMENT_NODE) { std::map itemProperties; - itemProperties["Content"] = (const char *)currNode->children->content; + std::cout << currNode->name << std::endl; + if(currNode->children != NULL) + { + itemProperties["Content"] = (const char *)currNode->children->content; + } + else + { + itemProperties["Content"] = ""; + } xmlAttrPtr currAttribute = currNode->properties; while(currAttribute != NULL) { @@ -104,7 +121,7 @@ XdmfReader::~XdmfReader() std::cout << "Deleted XdmfReader " << this << std::endl; } -boost::shared_ptr XdmfReader::read(const std::string & filePath) const +boost::shared_ptr XdmfReader::read(const std::string & filePath) const { xmlDocPtr document; xmlNodePtr currNode; @@ -121,7 +138,7 @@ boost::shared_ptr XdmfReader::read(const std::string & filePath) con xmlFreeDoc(document); xmlCleanupParser(); - return boost::shared_dynamic_cast(toReturn[0]); + return toReturn[0]; } diff --git a/XdmfReader.hpp b/XdmfReader.hpp index b58cf448..8eac624c 100644 --- a/XdmfReader.hpp +++ b/XdmfReader.hpp @@ -19,7 +19,7 @@ public: XdmfNewMacro(XdmfReader); - boost::shared_ptr read(const std::string & fileName) const; + boost::shared_ptr read(const std::string & fileName) const; protected: diff --git a/tests/Cxx/CMakeLists.txt b/tests/Cxx/CMakeLists.txt index 91e9a796..511ec683 100644 --- a/tests/Cxx/CMakeLists.txt +++ b/tests/Cxx/CMakeLists.txt @@ -5,6 +5,7 @@ set(XdmfCxxTests TestXdmfDataItem TestXdmfGeometry TestXdmfGrid + TestXdmfGridCollection TestXdmfHDF5Writer TestXdmfReader TestXdmfSet diff --git a/tests/Cxx/TestXdmfArray.cpp b/tests/Cxx/TestXdmfArray.cpp index e405d592..5d9414ef 100644 --- a/tests/Cxx/TestXdmfArray.cpp +++ b/tests/Cxx/TestXdmfArray.cpp @@ -14,7 +14,7 @@ int main(int argc, char* argv[]) */ boost::shared_ptr array = XdmfArray::New(); assert(array->getSize() == 0); - assert(array->getType() == ""); + assert(array->getType() == "None"); assert(array->getPrecision() == 0); assert(array->getValuesString() == ""); assert(array->getValuesPointer() == NULL); diff --git a/tests/Cxx/TestXdmfGridCollection.cpp b/tests/Cxx/TestXdmfGridCollection.cpp new file mode 100644 index 00000000..5157a743 --- /dev/null +++ b/tests/Cxx/TestXdmfGridCollection.cpp @@ -0,0 +1,62 @@ +#include +#include +#include "XdmfGridCollection.hpp" +#include "XdmfReader.hpp" +#include "XdmfWriter.hpp" + +#include "XdmfTestDataGenerator.hpp" + +int main(int argc, char* argv[]) +{ + // Test != and == operators + assert(XdmfGridCollectionType::Spatial() == XdmfGridCollectionType::Spatial()); + assert(XdmfGridCollectionType::Spatial() == XdmfGridCollectionType::Temporal() == false); + assert(XdmfGridCollectionType::Spatial() != XdmfGridCollectionType::Spatial() == false); + assert(XdmfGridCollectionType::Spatial() != XdmfGridCollectionType::Temporal()); + + boost::shared_ptr gridCollection = XdmfGridCollection::New(); + + gridCollection->setName("Collection2"); + assert(gridCollection->getName().compare("Collection2") == 0); + + boost::shared_ptr childGrid1 = XdmfTestDataGenerator::createHexahedron(); + boost::shared_ptr childGrid2 = XdmfTestDataGenerator::createHexahedron(); + + assert(gridCollection->getNumberOfGrids() == 0); + gridCollection->insert(childGrid1); + assert(gridCollection->getNumberOfGrids() == 1); + assert(gridCollection->getGrid(0) == childGrid1); + gridCollection->removeGrid(0); + assert(gridCollection->getNumberOfGrids() == 0); + gridCollection->insert(childGrid1); + gridCollection->insert(childGrid2); + + gridCollection->setGridCollectionType(XdmfGridCollectionType::Temporal()); + + + boost::shared_ptr writer = XdmfWriter::New("TestXdmfGridCollection1.xmf"); + gridCollection->accept(writer); + + boost::shared_ptr reader = XdmfReader::New(); + boost::shared_ptr gridCollection2 = boost::shared_dynamic_cast(reader->read("TestXdmfGridCollection1.xmf")); + + boost::shared_ptr writer2 = XdmfWriter::New("TestXdmfGridCollection2.xmf"); + gridCollection2->accept(writer2); + + // Compare two files for equality + std::ifstream firstFile("TestXdmfGridCollection1.xmf"); + std::ifstream secondFile("TestXdmfGridCollection2.xmf"); + + std::stringstream firstBuffer; + std::stringstream secondBuffer; + + firstBuffer << firstFile.rdbuf(); + secondBuffer << secondFile.rdbuf(); + + std::string firstContents(firstBuffer.str()); + std::string secondContents(secondBuffer.str()); + + assert(firstContents.compare(secondContents) == 0); + + return 0; +} diff --git a/tests/Cxx/TestXdmfReader.cpp b/tests/Cxx/TestXdmfReader.cpp index f55a8a83..a4910a7d 100644 --- a/tests/Cxx/TestXdmfReader.cpp +++ b/tests/Cxx/TestXdmfReader.cpp @@ -1,4 +1,6 @@ #include +#include +#include "XdmfItem.hpp" #include "XdmfReader.hpp" #include "XdmfWriter.hpp" @@ -16,26 +18,26 @@ int main(int argc, char* argv[]) domain->accept(writer); boost::shared_ptr reader = XdmfReader::New(); - boost::shared_ptr readDomain = reader->read("TestXdmfReader1.xmf"); + boost::shared_ptr readDomain = boost::shared_dynamic_cast(reader->read("TestXdmfReader1.xmf")); boost::shared_ptr writer2 = XdmfWriter::New("TestXdmfReader2.xmf"); readDomain->accept(writer2); + // Compare two files for equality std::ifstream firstFile("TestXdmfReader1.xmf"); std::ifstream secondFile("TestXdmfReader2.xmf"); - std::string firstLine; - std::string secondLine; - - while(!firstFile.eof()) - { - std::getline(firstFile, firstLine); - std::getline(secondFile, secondLine); - if(firstLine.compare(secondLine) != 0) - { - assert(false); - } - } + std::stringstream firstBuffer; + std::stringstream secondBuffer; + + firstBuffer << firstFile.rdbuf(); + secondBuffer << secondFile.rdbuf(); + + std::string firstContents(firstBuffer.str()); + std::string secondContents(secondBuffer.str()); + + assert(firstContents.compare(secondContents) == 0); + return 0; } -- GitLab