Refactoring, more consts, etc. Comments from Joakim

This commit is contained in:
Kristian Flikka 2013-04-08 14:32:17 +02:00
parent 477551c8d7
commit 8b32658e54
16 changed files with 330 additions and 290 deletions

View File

@ -18,6 +18,7 @@
*/
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <stdexcept>
#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<RawKeywordPtr>::iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
RawKeywordConstPtr RawDeck::getKeyword(const std::string& keyword) const {
for (std::list<RawKeywordConstPtr>::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<RawKeywordConstPtr>::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<RawKeywordPtr>& 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<RawKeywordPtr>& 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<RawKeywordPtr>::const_iterator keyword = deck.m_keywords.begin(); keyword != deck.m_keywords.end(); keyword++) {
for (std::list<RawKeywordConstPtr>::const_iterator keyword = deck.m_keywords.begin(); keyword != deck.m_keywords.end(); keyword++) {
os << (*keyword)->getKeyword() << " -- Keyword\n";
std::list<RawRecordPtr> records = (*keyword)->getRecords();
for (std::list<RawRecordPtr>::const_iterator record = records.begin(); record != records.end(); record++) {
std::vector<std::string> recordItems = (*record)->getRecords();
std::list<RawRecordConstPtr> records = (*keyword)->getRecords();
for (std::list<RawRecordConstPtr>::const_iterator record = records.begin(); record != records.end(); record++) {
std::vector<std::string> recordItems = (*record)->getItems();
for (std::vector<std::string>::const_iterator recordItem = recordItems.begin(); recordItem != recordItems.end(); recordItem++) {
os << (*recordItem) << " ";

View File

@ -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<RawKeywordPtr> m_keywords;
std::list<RawKeywordConstPtr> m_keywords;
RawParserKWsConstPtr m_rawParserKWs;
void readDataIntoDeck(const std::string& path, std::list<RawKeywordPtr>& keywordList);
void popAndProcessInclude(std::list<RawKeywordPtr>& keywordList, boost::filesystem::path baseDataFolder);
bool isKeywordFinished(RawKeywordPtr rawKeyword);
void readDataIntoDeck(const std::string& path, std::list<RawKeywordConstPtr>& keywordList);
void addKeyword(RawKeywordConstPtr keyword, const boost::filesystem::path& baseDataFolder);
bool isKeywordFinished(RawKeywordConstPtr rawKeyword);
static boost::filesystem::path verifyValidInputPath(const std::string& inputPath);
};

View File

@ -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<RawRecordPtr>& RawKeyword::getRecords() const {
const std::list<RawRecordConstPtr>& RawKeyword::getRecords() const {
return m_records;
}
std::string RawKeyword::getKeyword() const {
const std::string& RawKeyword::getKeyword() const {
return m_keyword;
}

View File

@ -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<RawRecordPtr>& getRecords() const;
unsigned int getNumberOfRecords();
const std::string& getKeyword() const;
const std::list<RawRecordConstPtr>& 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<RawRecordPtr> m_records;
std::list<RawRecordConstPtr> m_records;
std::string m_partialRecordString;
static bool isValidKeyword(const std::string& keywordCandidate);
};

View File

@ -50,7 +50,7 @@ namespace Opm {
splitSingleRecordString();
}
const std::vector<std::string>& RawRecord::getRecords() const {
const std::vector<std::string>& RawRecord::getItems() const {
return m_recordItems;
}

View File

@ -31,12 +31,13 @@ namespace Opm {
RawRecord();
RawRecord(const std::string& singleRecordString);
const std::string& getRecordString() const;
const std::vector<std::string>& getRecords() const;
const std::vector<std::string>& getItems() const;
static bool isTerminatedRecordString(const std::string& candidateRecordString);
virtual ~RawRecord();
private:
std::string m_sanitizedRecordString;
std::vector<std::string> 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<RawRecord> RawRecordPtr;
typedef boost::shared_ptr<const RawRecord> RawRecordConstPtr;
}
#endif /* RECORD_HPP */

View File

@ -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 )

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
*/
#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);
}

View File

@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
*/
#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);
}

View File

@ -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<RawRecordPtr>& 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<RawRecordConstPtr>& 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<std::string>& recordElements = record->getRecords();
BOOST_REQUIRE_EQUAL((unsigned) 4, recordElements.size());
const std::vector<std::string>& 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<RawRecordPtr> records = matchingKeyword->getRecords();
BOOST_REQUIRE_EQUAL("OIL", matchingKeyword->getKeyword());
BOOST_REQUIRE_EQUAL((unsigned) 0, records.size());
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword("OIL");
std::list<RawRecordConstPtr> 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);
}
}

View File

@ -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<RawRecordPtr> 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<RawRecordConstPtr> 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<std::string> 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<std::string> recordItems = records.front()->getItems();
BOOST_CHECK_EQUAL((unsigned) 6, recordItems.size());
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE ParserTests
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
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<std::string>& 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);
}

View File

@ -23,9 +23,24 @@
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawDeck.hpp>
#include <opm/parser/eclipse/RawDeck/RawParserKWs.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
#include <boost/test/test_tools.hpp>
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);
}

64
tests/RawKeywordTests.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE RawKeywordTests
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
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");
}

View File

@ -25,28 +25,27 @@
#include <opm/parser/eclipse/RawDeck/RawParserKWs.hpp>
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"));
}

53
tests/RawRecordTests.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE ParserTests
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
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<std::string>& 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);
}