From 8b32658e546d2550feafc00cd8444aa75bb65bdd Mon Sep 17 00:00:00 2001 From: Kristian Flikka Date: Mon, 8 Apr 2013 14:32:17 +0200 Subject: [PATCH] Refactoring, more consts, etc. Comments from Joakim --- opm/parser/eclipse/RawDeck/RawDeck.cpp | 60 +++++----- opm/parser/eclipse/RawDeck/RawDeck.hpp | 13 ++- opm/parser/eclipse/RawDeck/RawKeyword.cpp | 8 +- opm/parser/eclipse/RawDeck/RawKeyword.hpp | 10 +- opm/parser/eclipse/RawDeck/RawRecord.cpp | 2 +- opm/parser/eclipse/RawDeck/RawRecord.hpp | 5 +- tests/CMakeLists.txt | 21 +++- tests/ParserKWTests.cpp | 33 +++--- tests/ParserRecordSizeTests.cpp | 15 +-- tests/ParserTests.cpp | 136 +++++++++++----------- tests/ParserTestsInternalData.cpp | 60 +++++----- tests/RawDataStructuresTests.cpp | 98 ---------------- tests/RawDeckTests.cpp | 21 +++- tests/RawKeywordTests.cpp | 64 ++++++++++ tests/RawParserKWsTests.cpp | 21 ++-- tests/RawRecordTests.cpp | 53 +++++++++ 16 files changed, 330 insertions(+), 290 deletions(-) delete mode 100644 tests/RawDataStructuresTests.cpp create mode 100644 tests/RawKeywordTests.cpp create mode 100644 tests/RawRecordTests.cpp diff --git a/opm/parser/eclipse/RawDeck/RawDeck.cpp b/opm/parser/eclipse/RawDeck/RawDeck.cpp index ff42c9982..c24abd536 100644 --- a/opm/parser/eclipse/RawDeck/RawDeck.cpp +++ b/opm/parser/eclipse/RawDeck/RawDeck.cpp @@ -18,6 +18,7 @@ */ #include #include +#include #include "RawDeck.hpp" #include "RawConsts.hpp" @@ -32,24 +33,25 @@ namespace Opm { * O(n), not using map or hash because the keywords are not unique, * and the order matters. Returns first matching keyword. */ - RawKeywordPtr RawDeck::getKeyword(const std::string& keyword) { - for (std::list::iterator it = m_keywords.begin(); it != m_keywords.end(); it++) { + RawKeywordConstPtr RawDeck::getKeyword(const std::string& keyword) const { + for (std::list::const_iterator it = m_keywords.begin(); it != m_keywords.end(); it++) { if ((*it)->getKeyword() == keyword) { return (*it); } } - return RawKeywordPtr(new RawKeyword()); + throw std::invalid_argument("Keyword not found, keyword: " + keyword); + } + + bool RawDeck::hasKeyword(const std::string& keyword) const { + for (std::list::const_iterator it = m_keywords.begin(); it != m_keywords.end(); it++) { + if ((*it)->getKeyword() == keyword) { + return true; + } + } + return false; } - /* - * Read data into deck, from specified path. - * Throws invalid_argument exception if path not valid. - */ void RawDeck::readDataIntoDeck(const std::string& path) { - readDataIntoDeck(path, m_keywords); - } - - void RawDeck::readDataIntoDeck(const std::string& path, std::list& keywordList) { boost::filesystem::path dataFolderPath = verifyValidInputPath(path); { std::ifstream inputstream; @@ -61,29 +63,26 @@ namespace Opm { while (std::getline(inputstream, line)) { std::string keywordString; if (currentRawKeyword == NULL) { - popAndProcessInclude(keywordList, dataFolderPath); - if (RawKeyword::tryParseKeyword(line, keywordString)) { currentRawKeyword = RawKeywordPtr(new RawKeyword(keywordString)); if (isKeywordFinished(currentRawKeyword)) { - keywordList.push_back(currentRawKeyword); + addKeyword(currentRawKeyword, dataFolderPath); currentRawKeyword.reset(); } } } else if (currentRawKeyword != NULL && RawKeyword::lineContainsData(line)) { currentRawKeyword->addRawRecordString(line); if (isKeywordFinished(currentRawKeyword)) { - keywordList.push_back(currentRawKeyword); + addKeyword(currentRawKeyword, dataFolderPath); currentRawKeyword.reset(); } } else if (currentRawKeyword != NULL && RawKeyword::lineTerminatesKeyword(line)) { if (!currentRawKeyword->isPartialRecordStringEmpty()) { Logger::error("Reached keyword terminator slash, but there is non-terminated data on current keyword. " "Adding terminator, but records should be terminated by slash in data file"); - currentRawKeyword->addRawRecordString("/"); - + currentRawKeyword->addRawRecordString(std::string(1,Opm::RawConsts::slash)); } - keywordList.push_back(currentRawKeyword); + addKeyword(currentRawKeyword, dataFolderPath); currentRawKeyword.reset(); } } @@ -91,16 +90,15 @@ namespace Opm { } } - void RawDeck::popAndProcessInclude(std::list& keywordList, boost::filesystem::path dataFolderPath) { - if (!keywordList.empty() && keywordList.back()->getKeyword() == Opm::RawConsts::include) { - RawKeywordPtr includeKeyword = keywordList.back(); - keywordList.pop_back(); // Don't need the include keyword in the deck. - - std::string includeFileString = includeKeyword->getRecords().front()->getRecords().front(); + void RawDeck::addKeyword(RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) { + if (keyword->getKeyword() == Opm::RawConsts::include) { + std::string includeFileString = keyword->getRecords().front()->getItems().front(); boost::filesystem::path pathToIncludedFile(dataFolderPath); pathToIncludedFile /= includeFileString; - readDataIntoDeck(pathToIncludedFile.string(), m_keywords); + readDataIntoDeck(pathToIncludedFile.string()); + } else { + m_keywords.push_back(keyword); } } @@ -114,11 +112,11 @@ namespace Opm { return pathToInputFile.parent_path(); } - unsigned int RawDeck::getNumberOfKeywords() { + unsigned int RawDeck::getNumberOfKeywords() const { return m_keywords.size(); } - bool RawDeck::isKeywordFinished(RawKeywordPtr rawKeyword) { + bool RawDeck::isKeywordFinished(RawKeywordConstPtr rawKeyword) { if (m_rawParserKWs->keywordExists(rawKeyword->getKeyword())) { return rawKeyword->getNumberOfRecords() == m_rawParserKWs->getFixedNumberOfRecords(rawKeyword->getKeyword()); } @@ -126,12 +124,12 @@ namespace Opm { } std::ostream& operator<<(std::ostream& os, const RawDeck& deck) { - for (std::list::const_iterator keyword = deck.m_keywords.begin(); keyword != deck.m_keywords.end(); keyword++) { + for (std::list::const_iterator keyword = deck.m_keywords.begin(); keyword != deck.m_keywords.end(); keyword++) { os << (*keyword)->getKeyword() << " -- Keyword\n"; - std::list records = (*keyword)->getRecords(); - for (std::list::const_iterator record = records.begin(); record != records.end(); record++) { - std::vector recordItems = (*record)->getRecords(); + std::list records = (*keyword)->getRecords(); + for (std::list::const_iterator record = records.begin(); record != records.end(); record++) { + std::vector recordItems = (*record)->getItems(); for (std::vector::const_iterator recordItem = recordItems.begin(); recordItem != recordItems.end(); recordItem++) { os << (*recordItem) << " "; diff --git a/opm/parser/eclipse/RawDeck/RawDeck.hpp b/opm/parser/eclipse/RawDeck/RawDeck.hpp index dff134ab8..17794c1cf 100644 --- a/opm/parser/eclipse/RawDeck/RawDeck.hpp +++ b/opm/parser/eclipse/RawDeck/RawDeck.hpp @@ -33,18 +33,19 @@ namespace Opm { class RawDeck { public: RawDeck(RawParserKWsConstPtr rawParserKWs); + RawKeywordConstPtr getKeyword(const std::string& keyword) const; + bool hasKeyword(const std::string& keyword) const; void readDataIntoDeck(const std::string& path); - RawKeywordPtr getKeyword(const std::string& keyword); - unsigned int getNumberOfKeywords(); + unsigned int getNumberOfKeywords() const; friend std::ostream& operator<<(std::ostream& os, const RawDeck& deck); virtual ~RawDeck(); private: - std::list m_keywords; + std::list m_keywords; RawParserKWsConstPtr m_rawParserKWs; - void readDataIntoDeck(const std::string& path, std::list& keywordList); - void popAndProcessInclude(std::list& keywordList, boost::filesystem::path baseDataFolder); - bool isKeywordFinished(RawKeywordPtr rawKeyword); + void readDataIntoDeck(const std::string& path, std::list& keywordList); + void addKeyword(RawKeywordConstPtr keyword, const boost::filesystem::path& baseDataFolder); + bool isKeywordFinished(RawKeywordConstPtr rawKeyword); static boost::filesystem::path verifyValidInputPath(const std::string& inputPath); }; diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index 39a69f40e..d571e2523 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -104,19 +104,19 @@ namespace Opm { } } - bool RawKeyword::isPartialRecordStringEmpty() { + bool RawKeyword::isPartialRecordStringEmpty() const { return m_partialRecordString.size() == 0; } - unsigned int RawKeyword::getNumberOfRecords() { + unsigned int RawKeyword::getNumberOfRecords() const { return m_records.size(); } - const std::list& RawKeyword::getRecords() const { + const std::list& RawKeyword::getRecords() const { return m_records; } - std::string RawKeyword::getKeyword() const { + const std::string& RawKeyword::getKeyword() const { return m_keyword; } diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.hpp b/opm/parser/eclipse/RawDeck/RawKeyword.hpp index fcd5c31bf..0cdc614f3 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.hpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.hpp @@ -38,17 +38,17 @@ namespace Opm { static bool lineContainsData(const std::string& line); static bool lineTerminatesKeyword(const std::string& line); - std::string getKeyword() const; - const std::list& getRecords() const; - unsigned int getNumberOfRecords(); + const std::string& getKeyword() const; + const std::list& getRecords() const; + unsigned int getNumberOfRecords() const; void setKeyword(const std::string& keyword); void addRawRecordString(const std::string& partialRecordString); - bool isPartialRecordStringEmpty(); + bool isPartialRecordStringEmpty() const; virtual ~RawKeyword(); private: std::string m_keyword; - std::list m_records; + std::list m_records; std::string m_partialRecordString; static bool isValidKeyword(const std::string& keywordCandidate); }; diff --git a/opm/parser/eclipse/RawDeck/RawRecord.cpp b/opm/parser/eclipse/RawDeck/RawRecord.cpp index 8e05e0508..474690386 100644 --- a/opm/parser/eclipse/RawDeck/RawRecord.cpp +++ b/opm/parser/eclipse/RawDeck/RawRecord.cpp @@ -50,7 +50,7 @@ namespace Opm { splitSingleRecordString(); } - const std::vector& RawRecord::getRecords() const { + const std::vector& RawRecord::getItems() const { return m_recordItems; } diff --git a/opm/parser/eclipse/RawDeck/RawRecord.hpp b/opm/parser/eclipse/RawDeck/RawRecord.hpp index a3dd0915b..a268cc5ac 100644 --- a/opm/parser/eclipse/RawDeck/RawRecord.hpp +++ b/opm/parser/eclipse/RawDeck/RawRecord.hpp @@ -31,12 +31,13 @@ namespace Opm { RawRecord(); RawRecord(const std::string& singleRecordString); const std::string& getRecordString() const; - const std::vector& getRecords() const; + const std::vector& getItems() const; static bool isTerminatedRecordString(const std::string& candidateRecordString); virtual ~RawRecord(); private: std::string m_sanitizedRecordString; std::vector m_recordItems; + void setRecordString(const std::string& singleRecordString); void splitSingleRecordString(); void processSeparatorCharacter(std::string& currentToken, const char& currentChar, char& tokenStarter); @@ -46,6 +47,8 @@ namespace Opm { static unsigned int findTerminatingSlash(const std::string& singleRecordString); }; typedef boost::shared_ptr RawRecordPtr; + typedef boost::shared_ptr RawRecordConstPtr; + } #endif /* RECORD_HPP */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5f3ee9d10..e513efe32 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,8 @@ add_executable(runParserTests ParserTests.cpp) add_executable(runParserTestsInternalData ParserTestsInternalData.cpp) -add_executable(runRawDataTests RawDataStructuresTests.cpp) +add_executable(runRawRecordTests RawRecordTests.cpp) +add_executable(runRawKeywordTests RawKeywordTests.cpp) add_executable(runRawDeckTests RawDeckTests.cpp) add_executable(runRawParserKWsTests RawParserKWsTests.cpp) @@ -11,19 +12,29 @@ add_executable(runParserRecordSizeTests ParserRecordSizeTests.cpp) target_link_libraries(runParserTests Parser Logger ${Boost_LIBRARIES}) target_link_libraries(runParserTestsInternalData Parser Logger ${Boost_LIBRARIES}) -target_link_libraries(runRawDataTests Parser Logger ${Boost_LIBRARIES}) +target_link_libraries(runRawRecordTests Parser Logger ${Boost_LIBRARIES}) +target_link_libraries(runRawKeywordTests Parser Logger ${Boost_LIBRARIES}) target_link_libraries(runRawDeckTests Parser Logger ${Boost_LIBRARIES}) target_link_libraries(runRawParserKWsTests Parser Logger ${Boost_LIBRARIES}) + target_link_libraries(runParserKWTests Parser Logger ${Boost_LIBRARIES}) target_link_libraries(runParserRecordSizeTests Parser Logger ${Boost_LIBRARIES}) - add_test(NAME runParserTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserTests ) add_test(NAME runParserTestsInternalData WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserTestsInternalData) set_tests_properties(runParserTestsInternalData PROPERTIES LABELS Statoil) -add_test(NAME runRawDataTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawDataTests ) +add_test(NAME runRawRecordTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawRecordTests ) +set_tests_properties(runRawRecordTests PROPERTIES LABELS Raw) + +add_test(NAME runRawKeywordTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawKeywordTests ) +set_tests_properties(runRawKeywordTests PROPERTIES LABELS Raw) + add_test(NAME runRawDeckTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawDeckTests ) -add_test(NAME runRawParserKWsTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawDeckTests ) +set_tests_properties(runRawDeckTests PROPERTIES LABELS Raw) + +add_test(NAME runRawParserKWsTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runRawParserKWsTests ) +set_tests_properties(runRawParserKWsTests PROPERTIES LABELS Raw) + add_test(NAME runParserKWTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserKWTests ) add_test(NAME runParserRecordSizeTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserRecordSizeTests ) diff --git a/tests/ParserKWTests.cpp b/tests/ParserKWTests.cpp index af57a4180..bf7950ed4 100644 --- a/tests/ParserKWTests.cpp +++ b/tests/ParserKWTests.cpp @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with OPM. If not, see . -*/ + */ #define BOOST_TEST_MODULE ParserTests @@ -26,35 +26,30 @@ using namespace Opm; - BOOST_AUTO_TEST_CASE(Initialize) { - BOOST_REQUIRE_NO_THROW( ParserKW parserKW ); - - ParserKW parserKW; - BOOST_REQUIRE_EQUAL( parserKW.getName() , ""); -} + BOOST_REQUIRE_NO_THROW(ParserKW parserKW); + ParserKW parserKW; + BOOST_CHECK_EQUAL(parserKW.getName(), ""); +} BOOST_AUTO_TEST_CASE(NamedInit) { std::string keyword("KEYWORD"); - - ParserRecordSizeConstPtr recordSize( new ParserRecordSize( 100 )); - ParserKW parserKW( keyword , recordSize); - BOOST_REQUIRE_EQUAL( parserKW.getName( ) , keyword ); + + ParserRecordSizeConstPtr recordSize(new ParserRecordSize(100)); + ParserKW parserKW(keyword, recordSize); + BOOST_CHECK_EQUAL(parserKW.getName(), keyword); } - - BOOST_AUTO_TEST_CASE(NameTooLong) { std::string keyword("KEYWORDTOOLONG"); - ParserRecordSizeConstPtr recordSize( new ParserRecordSize( 100 )); - BOOST_REQUIRE_THROW( ParserKW parserKW( keyword , recordSize ) , std::invalid_argument ); + ParserRecordSizeConstPtr recordSize(new ParserRecordSize(100)); + BOOST_CHECK_THROW(ParserKW parserKW(keyword, recordSize), std::invalid_argument); } - BOOST_AUTO_TEST_CASE(MixedCase) { std::string keyword("KeyWord"); - - ParserRecordSizeConstPtr recordSize( new ParserRecordSize( 100 )); - BOOST_REQUIRE_THROW( ParserKW parserKW( keyword , recordSize ) , std::invalid_argument ); + + ParserRecordSizeConstPtr recordSize(new ParserRecordSize(100)); + BOOST_CHECK_THROW(ParserKW parserKW(keyword, recordSize), std::invalid_argument); } diff --git a/tests/ParserRecordSizeTests.cpp b/tests/ParserRecordSizeTests.cpp index e65a5b55a..d36cd211c 100644 --- a/tests/ParserRecordSizeTests.cpp +++ b/tests/ParserRecordSizeTests.cpp @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with OPM. If not, see . -*/ + */ #define BOOST_TEST_MODULE ParserTests @@ -26,21 +26,18 @@ using namespace Opm; - BOOST_AUTO_TEST_CASE(Initialize) { - BOOST_REQUIRE_NO_THROW( ParserRecordSize recordSize ); + BOOST_REQUIRE_NO_THROW(ParserRecordSize recordSize); } - BOOST_AUTO_TEST_CASE(DynamicSize) { ParserRecordSize recordSize; - BOOST_REQUIRE_THROW( recordSize.recordSize() , std::logic_error ); + BOOST_CHECK_THROW(recordSize.recordSize(), std::logic_error); } - BOOST_AUTO_TEST_CASE(FixedSize) { - BOOST_REQUIRE_NO_THROW( ParserRecordSize recordSize(100) ); + BOOST_REQUIRE_NO_THROW(ParserRecordSize recordSize(100)); ParserRecordSize recordSize(100); - - BOOST_REQUIRE_EQUAL( recordSize.recordSize() , (size_t) 100 ); + + BOOST_CHECK_EQUAL(recordSize.recordSize(), (size_t) 100); } diff --git a/tests/ParserTests.cpp b/tests/ParserTests.cpp index e129665e3..c4dde576c 100644 --- a/tests/ParserTests.cpp +++ b/tests/ParserTests.cpp @@ -34,113 +34,113 @@ using namespace Opm; BOOST_AUTO_TEST_CASE(RawDeckPrintToOStream) { - boost::filesystem::path singleKeywordFile("testdata/small.data"); + boost::filesystem::path singleKeywordFile("testdata/small.data"); - ParserPtr parser(new Parser()); + ParserPtr parser(new Parser()); - RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); - std::cout << *rawDeck << "\n"; + RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); + std::cout << *rawDeck << "\n"; } BOOST_AUTO_TEST_CASE(Initializing) { - BOOST_REQUIRE_NO_THROW(Parser parser); + BOOST_CHECK_NO_THROW(Parser parser); } BOOST_AUTO_TEST_CASE(ParseWithInvalidInputFileThrows) { - ParserPtr parser(new Parser()); - BOOST_REQUIRE_THROW(parser->parse("nonexistingfile.asdf"), std::invalid_argument); + ParserPtr parser(new Parser()); + BOOST_CHECK_THROW(parser->parse("nonexistingfile.asdf"), std::invalid_argument); } BOOST_AUTO_TEST_CASE(ParseWithValidFileSetOnParseCallNoThrow) { - boost::filesystem::path singleKeywordFile("testdata/small.data"); - ParserPtr parser(new Parser()); + boost::filesystem::path singleKeywordFile("testdata/small.data"); + ParserPtr parser(new Parser()); - BOOST_REQUIRE_NO_THROW(parser->parse(singleKeywordFile.string())); + BOOST_CHECK_NO_THROW(parser->parse(singleKeywordFile.string())); } BOOST_AUTO_TEST_CASE(ParseWithInValidFileSetOnParseCallThrows) { - boost::filesystem::path singleKeywordFile("testdata/nosuchfile.data"); - ParserPtr parser(new Parser()); - BOOST_REQUIRE_THROW(parser->parse(singleKeywordFile.string()), std::invalid_argument); + boost::filesystem::path singleKeywordFile("testdata/nosuchfile.data"); + ParserPtr parser(new Parser()); + BOOST_CHECK_THROW(parser->parse(singleKeywordFile.string()), std::invalid_argument); } BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) { - boost::filesystem::path singleKeywordFile("testdata/mini.data"); + boost::filesystem::path singleKeywordFile("testdata/mini.data"); - ParserPtr parser(new Parser()); + ParserPtr parser(new Parser()); - RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); - BOOST_REQUIRE_EQUAL((unsigned) 1, rawDeck->getNumberOfKeywords()); - RawKeywordPtr rawKeyword = rawDeck->getKeyword("ENDSCALE"); - const std::list& records = rawKeyword->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 1, records.size()); - RawRecordPtr record = records.back(); + RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); + BOOST_CHECK_EQUAL((unsigned) 1, rawDeck->getNumberOfKeywords()); + RawKeywordConstPtr rawKeyword = rawDeck->getKeyword("ENDSCALE"); + const std::list& records = rawKeyword->getRecords(); + BOOST_CHECK_EQUAL((unsigned) 1, records.size()); + RawRecordConstPtr record = records.back(); - const std::string& recordString = record->getRecordString(); - BOOST_REQUIRE_EQUAL("'NODIR' 'REVERS' 1 20", recordString); + const std::string& recordString = record->getRecordString(); + BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString); - const std::vector& recordElements = record->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 4, recordElements.size()); + const std::vector& recordElements = record->getItems(); + BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size()); - BOOST_REQUIRE_EQUAL("NODIR", recordElements[0]); - BOOST_REQUIRE_EQUAL("REVERS", recordElements[1]); - BOOST_REQUIRE_EQUAL("1", recordElements[2]); - BOOST_REQUIRE_EQUAL("20", recordElements[3]); + BOOST_CHECK_EQUAL("NODIR", recordElements[0]); + BOOST_CHECK_EQUAL("REVERS", recordElements[1]); + BOOST_CHECK_EQUAL("1", recordElements[2]); + BOOST_CHECK_EQUAL("20", recordElements[3]); } BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) { - boost::filesystem::path singleKeywordFile("testdata/small.data"); + boost::filesystem::path singleKeywordFile("testdata/small.data"); - ParserPtr parser(new Parser()); + ParserPtr parser(new Parser()); - RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); - BOOST_REQUIRE_EQUAL((unsigned) 7, rawDeck->getNumberOfKeywords()); + RawDeckPtr rawDeck = parser->parse(singleKeywordFile.string()); + BOOST_CHECK_EQUAL((unsigned) 7, rawDeck->getNumberOfKeywords()); - RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL"); - std::list records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL("OIL", matchingKeyword->getKeyword()); - BOOST_REQUIRE_EQUAL((unsigned) 0, records.size()); + RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL"); + std::list records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeyword()); + BOOST_CHECK_EQUAL((unsigned) 0, records.size()); - // The two next come in via the include of the include path/readthis.sch file - matchingKeyword = rawDeck->getKeyword("GRUPTREE"); - BOOST_REQUIRE_EQUAL("GRUPTREE", matchingKeyword->getKeyword()); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 2, records.size()); - - matchingKeyword = rawDeck->getKeyword("WHISTCTL"); - BOOST_REQUIRE_EQUAL("WHISTCTL", matchingKeyword->getKeyword()); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 1, records.size()); - - matchingKeyword = rawDeck->getKeyword("METRIC"); - BOOST_REQUIRE_EQUAL("METRIC", matchingKeyword->getKeyword()); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 0, records.size()); + // The two next come in via the include of the include path/readthis.sch file + matchingKeyword = rawDeck->getKeyword("GRUPTREE"); + BOOST_CHECK_EQUAL("GRUPTREE", matchingKeyword->getKeyword()); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL((unsigned) 2, records.size()); - matchingKeyword = rawDeck->getKeyword("GRIDUNIT"); - BOOST_REQUIRE_EQUAL("GRIDUNIT", matchingKeyword->getKeyword()); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 1, records.size()); + matchingKeyword = rawDeck->getKeyword("WHISTCTL"); + BOOST_CHECK_EQUAL("WHISTCTL", matchingKeyword->getKeyword()); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL((unsigned) 1, records.size()); - matchingKeyword = rawDeck->getKeyword("RADFIN4"); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL("RADFIN4", matchingKeyword->getKeyword()); - BOOST_REQUIRE_EQUAL((unsigned) 1, records.size()); + matchingKeyword = rawDeck->getKeyword("METRIC"); + BOOST_CHECK_EQUAL("METRIC", matchingKeyword->getKeyword()); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL((unsigned) 0, records.size()); - matchingKeyword = rawDeck->getKeyword("ABCDAD"); - BOOST_REQUIRE_EQUAL("ABCDAD", matchingKeyword->getKeyword()); - records = matchingKeyword->getRecords(); + matchingKeyword = rawDeck->getKeyword("GRIDUNIT"); + BOOST_CHECK_EQUAL("GRIDUNIT", matchingKeyword->getKeyword()); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL((unsigned) 1, records.size()); - BOOST_REQUIRE_EQUAL((unsigned) 2, records.size()); + matchingKeyword = rawDeck->getKeyword("RADFIN4"); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL("RADFIN4", matchingKeyword->getKeyword()); + BOOST_CHECK_EQUAL((unsigned) 1, records.size()); + + matchingKeyword = rawDeck->getKeyword("ABCDAD"); + BOOST_CHECK_EQUAL("ABCDAD", matchingKeyword->getKeyword()); + records = matchingKeyword->getRecords(); + + BOOST_CHECK_EQUAL((unsigned) 2, records.size()); } BOOST_AUTO_TEST_CASE(ParserAddKW) { Parser parser; { - ParserRecordSizePtr recordSize(new ParserRecordSize(9)); - ParserKWPtr equilKW(new ParserKW("EQUIL" , recordSize) ); - - parser.addKW( equilKW ); + ParserRecordSizePtr recordSize(new ParserRecordSize(9)); + ParserKWPtr equilKW(new ParserKW("EQUIL", recordSize)); + + parser.addKW(equilKW); } } diff --git a/tests/ParserTestsInternalData.cpp b/tests/ParserTestsInternalData.cpp index 2246e06a0..4711ea805 100644 --- a/tests/ParserTestsInternalData.cpp +++ b/tests/ParserTestsInternalData.cpp @@ -34,44 +34,46 @@ using namespace Opm; //NOTE: needs statoil dataset + BOOST_AUTO_TEST_CASE(ParseFileWithManyKeywords) { - boost::filesystem::path multipleKeywordFile("testdata/statoil/gurbat_trimmed.DATA"); - std::cout << "BOOST path running ParseFullTestFile\n"; + boost::filesystem::path multipleKeywordFile("testdata/statoil/gurbat_trimmed.DATA"); + std::cout << "BOOST path running ParseFullTestFile\n"; - ParserPtr parser(new Parser()); + ParserPtr parser(new Parser()); - RawDeckPtr rawDeck = parser->parse(multipleKeywordFile.string()); - - //This check is not necessarily correct, - //as it depends on that all the fixed recordNum keywords are specified - BOOST_REQUIRE_EQUAL((unsigned) 275, rawDeck->getNumberOfKeywords()); + RawDeckPtr rawDeck = parser->parse(multipleKeywordFile.string()); + + //This check is not necessarily correct, + //as it depends on that all the fixed recordNum keywords are specified + BOOST_CHECK_EQUAL((unsigned) 275, rawDeck->getNumberOfKeywords()); } //NOTE: needs statoil dataset + BOOST_AUTO_TEST_CASE(ParseFullTestFile) { - boost::filesystem::path multipleKeywordFile("testdata/statoil/ECLIPSE.DATA"); + boost::filesystem::path multipleKeywordFile("testdata/statoil/ECLIPSE.DATA"); - ParserPtr parser(new Parser()); + ParserPtr parser(new Parser()); - RawDeckPtr rawDeck = parser->parse(multipleKeywordFile.string()); - // Note, cannot check the number of keywords, since the number of - // records are not defined (yet) for all these keywords. - // But we can check a copule of keywords, and that they have the correct - // number of records - - RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL"); - std::list records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL("OIL", matchingKeyword->getKeyword()); - - BOOST_REQUIRE_EQUAL((unsigned) 0, records.size()); + RawDeckPtr rawDeck = parser->parse(multipleKeywordFile.string()); + // Note, cannot check the number of keywords, since the number of + // records are not defined (yet) for all these keywords. + // But we can check a copule of keywords, and that they have the correct + // number of records - matchingKeyword = rawDeck->getKeyword("VFPPDIMS"); - records = matchingKeyword->getRecords(); - BOOST_REQUIRE_EQUAL("VFPPDIMS", matchingKeyword->getKeyword()); - BOOST_REQUIRE_EQUAL((unsigned) 1, records.size()); + RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL"); + std::list records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeyword()); - const std::string& recordString = records.front()->getRecordString(); - BOOST_REQUIRE_EQUAL("20 20 15 15 15 50", recordString); - std::vector recordItems = records.front()->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 6, recordItems.size()); + BOOST_CHECK_EQUAL((unsigned) 0, records.size()); + + matchingKeyword = rawDeck->getKeyword("VFPPDIMS"); + records = matchingKeyword->getRecords(); + BOOST_CHECK_EQUAL("VFPPDIMS", matchingKeyword->getKeyword()); + BOOST_CHECK_EQUAL((unsigned) 1, records.size()); + + const std::string& recordString = records.front()->getRecordString(); + BOOST_CHECK_EQUAL("20 20 15 15 15 50", recordString); + std::vector recordItems = records.front()->getItems(); + BOOST_CHECK_EQUAL((unsigned) 6, recordItems.size()); } diff --git a/tests/RawDataStructuresTests.cpp b/tests/RawDataStructuresTests.cpp deleted file mode 100644 index a3771bbd0..000000000 --- a/tests/RawDataStructuresTests.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - Copyright 2013 Statoil ASA. - - This file is part of the Open Porous Media project (OPM). - - OPM is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OPM 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with OPM. If not, see . - */ - -#define BOOST_TEST_MODULE ParserTests -#include -#include -#include -#include - -BOOST_AUTO_TEST_CASE(RawKeywordEmptyConstructorEmptyKeyword) { - Opm::RawKeyword keyword; - BOOST_CHECK(keyword.getKeyword() == ""); -} - -BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) { - Opm::RawKeyword keyword("KEYYWORD"); - BOOST_CHECK(keyword.getKeyword() == "KEYYWORD"); -} - -BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) { - BOOST_CHECK_THROW(Opm::RawKeyword keyword("KEYYYWORD"), std::invalid_argument); -} - -BOOST_AUTO_TEST_CASE(RawKeywordSetTooLongKeywordThrows) { - Opm::RawKeyword keyword; - BOOST_CHECK_THROW(keyword.setKeyword("TESTTOOLONG"), std::invalid_argument); -} - -BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) { - Opm::RawKeyword keyword; - BOOST_CHECK_THROW(keyword.setKeyword(" TELONG"), std::invalid_argument); -} - -BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) { - Opm::RawKeyword keyword; - BOOST_CHECK_THROW(keyword.setKeyword("\tTELONG"), std::invalid_argument); -} - -BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) { - Opm::RawKeyword keyword; - keyword.setKeyword("GOODONE"); - BOOST_CHECK(keyword.getKeyword() == "GOODONE"); -} - -BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) { - Opm::RawKeyword keyword; - keyword.setKeyword("GOODONEE "); - BOOST_CHECK(keyword.getKeyword() == "GOODONEE"); -} - - -BOOST_AUTO_TEST_CASE(RawRecordGetRecordStringReturnsTrimmedString) { - Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /")); - const std::string& recordString = record->getRecordString(); - BOOST_REQUIRE_EQUAL("'NODIR ' 'REVERS' 1 20", recordString); -} - - - -BOOST_AUTO_TEST_CASE(RawRecordGetRecordsCorrectElementsReturned) { - Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /")); - - const std::vector& recordElements = record->getRecords(); - BOOST_REQUIRE_EQUAL((unsigned) 4, recordElements.size()); - - BOOST_REQUIRE_EQUAL("NODIR ", recordElements[0]); - BOOST_REQUIRE_EQUAL("REVERS", recordElements[1]); - BOOST_REQUIRE_EQUAL("1", recordElements[2]); - BOOST_REQUIRE_EQUAL("20", recordElements[3]); -} - -BOOST_AUTO_TEST_CASE(RawRecordIsCompleteRecordCompleteRecordReturnsTrue) { - bool isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS' 1 20 /"); - BOOST_REQUIRE_EQUAL(true, isComplete); -} - -BOOST_AUTO_TEST_CASE(RawRecordIsCompleteRecordInCompleteRecordReturnsFalse) { - bool isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS' 1 20 "); - BOOST_REQUIRE_EQUAL(false, isComplete); - isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS 1 20 /"); - BOOST_REQUIRE_EQUAL(false, isComplete); -} diff --git a/tests/RawDeckTests.cpp b/tests/RawDeckTests.cpp index 462e90bcf..dc8e1bb46 100644 --- a/tests/RawDeckTests.cpp +++ b/tests/RawDeckTests.cpp @@ -23,9 +23,24 @@ #include #include #include +#include +#include -BOOST_AUTO_TEST_CASE(ReadData_MissingFixedKeywords_WrongNumberOfKeywordsFound) { - Opm::RawParserKWsConstPtr fixedKeywords(new Opm::RawParserKWs()); - Opm::RawDeck rawDeck(fixedKeywords); +BOOST_AUTO_TEST_CASE(GetNumberOfKeywords_EmptyDeck_RetunsZero) { + Opm::RawParserKWsConstPtr fixedKeywords(new Opm::RawParserKWs()); + Opm::RawDeckPtr rawDeck(new Opm::RawDeck(fixedKeywords)); + BOOST_CHECK_EQUAL((unsigned) 0, rawDeck->getNumberOfKeywords()); +} + +BOOST_AUTO_TEST_CASE(HasKeyword_NotExisting_RetunsFalse) { + Opm::RawParserKWsConstPtr fixedKeywords(new Opm::RawParserKWs()); + Opm::RawDeckPtr rawDeck(new Opm::RawDeck(fixedKeywords)); + BOOST_CHECK_EQUAL(false, rawDeck->hasKeyword("TEST")); +} + +BOOST_AUTO_TEST_CASE(GetKeyword_EmptyDeck_ThrowsExeption) { + Opm::RawParserKWsConstPtr fixedKeywords(new Opm::RawParserKWs()); + Opm::RawDeckPtr rawDeck(new Opm::RawDeck(fixedKeywords)); + BOOST_CHECK_THROW(rawDeck->getKeyword("TEST"), std::invalid_argument); } diff --git a/tests/RawKeywordTests.cpp b/tests/RawKeywordTests.cpp new file mode 100644 index 000000000..5542e52b2 --- /dev/null +++ b/tests/RawKeywordTests.cpp @@ -0,0 +1,64 @@ +/* + Copyright 2013 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + */ + +#define BOOST_TEST_MODULE RawKeywordTests +#include +#include +#include + +BOOST_AUTO_TEST_CASE(RawKeywordEmptyConstructorEmptyKeyword) { + Opm::RawKeyword keyword; + BOOST_CHECK(keyword.getKeyword() == ""); +} + +BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) { + Opm::RawKeyword keyword("KEYYWORD"); + BOOST_CHECK(keyword.getKeyword() == "KEYYWORD"); +} + +BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) { + BOOST_CHECK_THROW(Opm::RawKeyword keyword("KEYYYWORD"), std::invalid_argument); +} + +BOOST_AUTO_TEST_CASE(RawKeywordSetTooLongKeywordThrows) { + Opm::RawKeyword keyword; + BOOST_CHECK_THROW(keyword.setKeyword("TESTTOOLONG"), std::invalid_argument); +} + +BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) { + Opm::RawKeyword keyword; + BOOST_CHECK_THROW(keyword.setKeyword(" TELONG"), std::invalid_argument); +} + +BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) { + Opm::RawKeyword keyword; + BOOST_CHECK_THROW(keyword.setKeyword("\tTELONG"), std::invalid_argument); +} + +BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) { + Opm::RawKeyword keyword; + keyword.setKeyword("GOODONE"); + BOOST_CHECK(keyword.getKeyword() == "GOODONE"); +} + +BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) { + Opm::RawKeyword keyword; + keyword.setKeyword("GOODONEE "); + BOOST_CHECK(keyword.getKeyword() == "GOODONEE"); +} diff --git a/tests/RawParserKWsTests.cpp b/tests/RawParserKWsTests.cpp index 8652d7f0c..527d75bdc 100644 --- a/tests/RawParserKWsTests.cpp +++ b/tests/RawParserKWsTests.cpp @@ -25,28 +25,27 @@ #include using namespace Opm; - BOOST_AUTO_TEST_CASE(KeywordExists_KeywordNotPresent_ReturnsFalse) { - RawParserKWsConstPtr parserKWs(new RawParserKWs()); - BOOST_REQUIRE_EQUAL(false, parserKWs->keywordExists("FLASKE")); + RawParserKWsConstPtr parserKWs(new RawParserKWs()); + BOOST_CHECK_EQUAL(false, parserKWs->keywordExists("FLASKE")); } BOOST_AUTO_TEST_CASE(KeywordExists_KeywordPresent_ReturnsTrue) { - RawParserKWsConstPtr parserKWs(new RawParserKWs()); - BOOST_REQUIRE_EQUAL(true, parserKWs->keywordExists("TITLE")); + RawParserKWsConstPtr parserKWs(new RawParserKWs()); + BOOST_CHECK_EQUAL(true, parserKWs->keywordExists("TITLE")); } BOOST_AUTO_TEST_CASE(GetFixedNumberOfRecords_KeywordNotPresent_ThrowsException) { - RawParserKWsConstPtr parserKWs(new RawParserKWs()); - BOOST_REQUIRE_THROW(parserKWs->getFixedNumberOfRecords("FLASKE"), std::invalid_argument); + RawParserKWsConstPtr parserKWs(new RawParserKWs()); + BOOST_CHECK_THROW(parserKWs->getFixedNumberOfRecords("FLASKE"), std::invalid_argument); } BOOST_AUTO_TEST_CASE(GetFixedNumberOfRecords_OneRecord_ReturnsOne) { - RawParserKWsConstPtr parserKWs(new RawParserKWs()); - BOOST_REQUIRE_EQUAL((unsigned)1,parserKWs->getFixedNumberOfRecords("GRIDUNIT")); + RawParserKWsConstPtr parserKWs(new RawParserKWs()); + BOOST_CHECK_EQUAL((unsigned) 1, parserKWs->getFixedNumberOfRecords("GRIDUNIT")); } BOOST_AUTO_TEST_CASE(GetFixedNumberOfRecords_ZeroRecords_ReturnsZero) { - RawParserKWsConstPtr parserKWs(new RawParserKWs()); - BOOST_REQUIRE_EQUAL((unsigned)1,parserKWs->getFixedNumberOfRecords("METRIC")); + RawParserKWsConstPtr parserKWs(new RawParserKWs()); + BOOST_CHECK_EQUAL((unsigned) 0, parserKWs->getFixedNumberOfRecords("METRIC")); } diff --git a/tests/RawRecordTests.cpp b/tests/RawRecordTests.cpp new file mode 100644 index 000000000..18213479a --- /dev/null +++ b/tests/RawRecordTests.cpp @@ -0,0 +1,53 @@ +/* + Copyright 2013 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + */ + +#define BOOST_TEST_MODULE ParserTests +#include +#include +#include + +BOOST_AUTO_TEST_CASE(RawRecordGetRecordStringReturnsTrimmedString) { + Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /")); + const std::string& recordString = record->getRecordString(); + BOOST_CHECK_EQUAL("'NODIR ' 'REVERS' 1 20", recordString); +} + +BOOST_AUTO_TEST_CASE(RawRecordGetRecordsCorrectElementsReturned) { + Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /")); + + const std::vector& recordElements = record->getItems(); + BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size()); + + BOOST_CHECK_EQUAL("NODIR ", recordElements[0]); + BOOST_CHECK_EQUAL("REVERS", recordElements[1]); + BOOST_CHECK_EQUAL("1", recordElements[2]); + BOOST_CHECK_EQUAL("20", recordElements[3]); +} + +BOOST_AUTO_TEST_CASE(RawRecordIsCompleteRecordCompleteRecordReturnsTrue) { + bool isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS' 1 20 /"); + BOOST_CHECK_EQUAL(true, isComplete); +} + +BOOST_AUTO_TEST_CASE(RawRecordIsCompleteRecordInCompleteRecordReturnsFalse) { + bool isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS' 1 20 "); + BOOST_CHECK_EQUAL(false, isComplete); + isComplete = Opm::RawRecord::isTerminatedRecordString("'NODIR ' 'REVERS 1 20 /"); + BOOST_CHECK_EQUAL(false, isComplete); +}