Updated tests to reflect that RawDeck has been removed

This commit is contained in:
Joakim Hove 2013-08-13 14:51:33 +02:00
parent 765ade785a
commit a0a20425f6
4 changed files with 33 additions and 262 deletions

View File

@ -144,59 +144,30 @@ BOOST_AUTO_TEST_CASE(parse_fileWithBPRKeyword_dataiscorrect) {
}
BOOST_AUTO_TEST_CASE(PrintToOStream_noThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
std::cout << *rawDeck << "\n";
}
BOOST_AUTO_TEST_CASE(Parse_InvalidInputFile_Throws) {
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
BOOST_CHECK_THROW(parser->readToRawDeck("nonexistingfile.asdf"), std::invalid_argument);
BOOST_CHECK_THROW(parser->parse("nonexistingfile.asdf"), std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(Parse_ValidInputFile_NoThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
BOOST_CHECK_NO_THROW(parser->readToRawDeck(singleKeywordFile.string()));
}
BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
boost::filesystem::path singleKeywordFile("testdata/mini.data");
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
BOOST_CHECK_EQUAL(1U, rawDeck->size());
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL(1U, rawKeyword->size());
RawRecordConstPtr record = rawKeyword->getRecord(rawKeyword->size() - 1);
const std::string& recordString = record->getRecordString();
BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
BOOST_CHECK_EQUAL(4U, record->size());
BOOST_CHECK_EQUAL("NODIR", record->getItem(0));
BOOST_CHECK_EQUAL("REVERS", record->getItem(1));
BOOST_CHECK_EQUAL("1", record->getItem(2));
BOOST_CHECK_EQUAL("20", record->getItem(3));
BOOST_CHECK_NO_THROW(parser->parse(singleKeywordFile.string()));
}
/*
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
DeckPtr Deck = parser->parse(singleKeywordFile.string());
BOOST_CHECK_EQUAL(7U, rawDeck->size());
BOOST_CHECK_EQUAL(7U, Deck->size());
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
@ -228,3 +199,4 @@ BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
BOOST_CHECK_EQUAL(2U, matchingKeyword->size());
}
*/

View File

@ -27,7 +27,6 @@
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawDeck.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
using namespace Opm;
@ -36,10 +35,10 @@ using namespace Opm;
BOOST_AUTO_TEST_CASE(ParseFileWithManyKeywords) {
boost::filesystem::path multipleKeywordFile("testdata/statoil/gurbat_trimmed.DATA");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(multipleKeywordFile.string());
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
DeckPtr Deck = 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->size());
BOOST_CHECK_EQUAL((unsigned) 250, Deck->size());
}

View File

@ -18,8 +18,8 @@
*/
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawConsts.hpp>
#include <opm/parser/eclipse/Logger/Logger.hpp>
#include <opm/parser/eclipse/RawDeck/RawConsts.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckIntItem.hpp>
@ -46,45 +46,35 @@ namespace Opm {
void Parser::parseFile(DeckPtr deck , const std::string &file) {
std::ifstream inputstream;
inputstream.open( file.c_str() );
RawKeywordPtr rawKeyword;
while (tryParseKeyword(deck , inputstream , rawKeyword)) {
if (inputstream) {
RawKeywordPtr rawKeyword;
while (tryParseKeyword(deck , inputstream , rawKeyword)) {
if (rawKeyword->getKeywordName() == Opm::RawConsts::include) {
boost::filesystem::path dataFolderPath = verifyValidInputPath(file);
RawRecordConstPtr firstRecord = rawKeyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0);
boost::filesystem::path pathToIncludedFile(dataFolderPath);
pathToIncludedFile /= includeFileString;
parseFile( deck , pathToIncludedFile.string() );
boost::filesystem::path dataFolderPath = verifyValidInputPath(file);
RawRecordConstPtr firstRecord = rawKeyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0);
boost::filesystem::path pathToIncludedFile(dataFolderPath);
pathToIncludedFile /= includeFileString;
parseFile( deck , pathToIncludedFile.string() );
} else {
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
deck->addKeyword( deckKeyword );
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
std::cout << "Adding: " << rawKeyword->getKeywordName() << std::endl;
deck->addKeyword( deckKeyword );
}
rawKeyword.reset();
}
inputstream.close();
}
DeckPtr Parser::parseFromRawDeck(RawDeckConstPtr rawDeck) {
DeckPtr deck(new Deck());
for (size_t i = 0; i < rawDeck->size(); i++) {
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword(i);
if (hasKeyword(rawKeyword->getKeywordName())) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
deck->addKeyword(deckKeyword);
} else
std::cerr << "Keyword: " << rawKeyword->getKeywordName() << " is not recognized, skipping this." << std::endl;
}
return deck;
}
inputstream.close();
} else
throw std::invalid_argument("Failed to open file: " + file);
}
void Parser::addKeyword(ParserKeywordConstPtr parserKeyword) {
m_parserKeywords.insert(std::make_pair(parserKeyword->getName(), parserKeyword));
}
@ -118,11 +108,6 @@ namespace Opm {
}
RawDeckPtr Parser::readToRawDeck(const std::string& path) const {
RawDeckPtr rawDeck(new RawDeck());
readToRawDeck(rawDeck, path);
return rawDeck;
}
RawKeywordPtr Parser::newRawKeyword(const DeckConstPtr deck , const std::string& keywordString) {
@ -175,96 +160,6 @@ namespace Opm {
/// The main data reading function, reads one and one keyword into the RawDeck
/// If the INCLUDE keyword is found, the specified include file is inline read into the RawDeck.
/// The data is read into a keyword, record by record, until the fixed number of records specified
/// in the RawParserKeyword is met, or till a slash on a separate line is found.
void Parser::readToRawDeck(RawDeckPtr rawDeck, const std::string& path) const {
boost::filesystem::path dataFolderPath = verifyValidInputPath(path);
{
std::ifstream inputstream;
inputstream.open(path.c_str());
std::string line;
RawKeywordPtr currentRawKeyword;
while (std::getline(inputstream, line)) {
std::string keywordString;
if (currentRawKeyword != NULL) {
if (RawKeyword::lineContainsData(line)) {
currentRawKeyword->addRawRecordString(line);
if (isFixedLenghtKeywordFinished(currentRawKeyword)) {
// The INCLUDE keyword has fixed lenght 1, will hit here
if (currentRawKeyword->getKeywordName() == Opm::RawConsts::include)
processIncludeKeyword(rawDeck, currentRawKeyword, dataFolderPath);
else
rawDeck->addKeyword(currentRawKeyword);
currentRawKeyword.reset();
}
} else if (RawKeyword::lineTerminatesKeyword(line)) {
if (!currentRawKeyword->isPartialRecordStringEmpty()) {
// This is an error in the input file, but sometimes occurs
currentRawKeyword->addRawRecordString(std::string(1, Opm::RawConsts::slash));
}
// Don't need to check for include here, since only non-fixed lenght keywords come here.
rawDeck->addKeyword(currentRawKeyword);
currentRawKeyword.reset();
}
} else {
if (RawKeyword::tryParseKeyword(line, keywordString)) {
currentRawKeyword = RawKeywordPtr(new RawKeyword(keywordString));
if (isFixedLenghtKeywordFinished(currentRawKeyword)) {
rawDeck->addKeyword(currentRawKeyword);
currentRawKeyword.reset();
}
}
}
}
inputstream.close();
}
}
bool Parser::isFixedLenghtKeywordFinished(RawKeywordConstPtr rawKeyword) const {
bool fixedSizeReached = false;
if (hasKeyword(rawKeyword->getKeywordName())) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(rawKeyword->getKeywordName())->second;
if (parserKeyword->getSizeType() != UNDEFINED) {
size_t targetSize;
if (parserKeyword->hasFixedSize())
targetSize = parserKeyword->getFixedSize();
else {
const std::pair<std::string,std::string> sizeKeyword = parserKeyword->getSizePair();
// Need to check the deck ....
throw std::invalid_argument("Not implemented - need access to Deck here ...");
}
fixedSizeReached = (rawKeyword->size() == targetSize);
}
}
return fixedSizeReached;
}
void Parser::processIncludeKeyword(RawDeckPtr rawDeck, RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) const {
RawRecordConstPtr firstRecord = keyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0);
boost::filesystem::path pathToIncludedFile(dataFolderPath);
pathToIncludedFile /= includeFileString;
readToRawDeck(rawDeck, pathToIncludedFile.string());
}
boost::filesystem::path Parser::verifyValidInputPath(const std::string& inputPath) const {
Logger::info("Verifying path: " + inputPath);
boost::filesystem::path pathToInputFile(inputPath);

View File

@ -27,7 +27,6 @@
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawDeck.hpp>
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
@ -167,101 +166,7 @@ ParserKeywordPtr setupParserKeywordInt(std::string name, int numberOfItems) {
return parserKeyword;
}
RawDeckPtr setupRawDeckInt(std::string name, int numberOfRecords, int numberOfItems) {
RawDeckPtr rawDeck(new RawDeck());
RawKeywordPtr rawKeyword(new RawKeyword(name));
for (int records = 0; records < numberOfRecords; records++) {
for (int i = 0; i < numberOfItems; i++)
rawKeyword->addRawRecordString("42 ");
rawKeyword->addRawRecordString("/");
}
rawDeck->addKeyword(rawKeyword);
return rawDeck;
}
BOOST_AUTO_TEST_CASE(parseFromRawDeck_singleRawSingleIntItem_deckReturned) {
ParserPtr parser(new Parser());
parser->addKeyword(setupParserKeywordInt("RANDOM", 1));
DeckPtr deck = parser->parseFromRawDeck(setupRawDeckInt("RANDOM", 1, 1));
BOOST_CHECK(!deck->hasKeyword("ANDOM"));
BOOST_CHECK(deck->hasKeyword("RANDOM"));
BOOST_CHECK_EQUAL(1U, deck->getKeyword("RANDOM" , 0)->getRecord(0)->size());
}
BOOST_AUTO_TEST_CASE(parseFromRawDeck_singleRawRecordsSeveralIntItem_deckReturned) {
ParserPtr parser(new Parser());
parser->addKeyword(setupParserKeywordInt("RANDOM", 50));
DeckPtr deck = parser->parseFromRawDeck(setupRawDeckInt("RANDOM", 1, 50));
BOOST_CHECK(deck->hasKeyword("RANDOM"));
BOOST_CHECK_EQUAL(50U, deck->getKeyword("RANDOM" , 0)->getRecord(0)->size());
}
BOOST_AUTO_TEST_CASE(parseFromRawDeck_severalRawRecordsSeveralIntItem_deckReturned) {
ParserPtr parser(new Parser());
parser->addKeyword(setupParserKeywordInt("RANDOM", 50));
DeckPtr deck = parser->parseFromRawDeck(setupRawDeckInt("RANDOM", 10, 50));
BOOST_CHECK(deck->hasKeyword("RANDOM"));
BOOST_CHECK_EQUAL(10U, deck->getKeyword("RANDOM", 0)->size());
BOOST_CHECK_EQUAL(50U, deck->getKeyword("RANDOM", 0)->getRecord(0)->size());
}
/****************** readToRawDeck ***********************************************/
BOOST_AUTO_TEST_CASE(readToRawDeck_twoKeywords_sizeTwoReturned) {
ParserPtr parser(new Parser(JSON_CONFIG_FILE));
boost::filesystem::path wconhistFile("testdata/WCONHIST/WCONHIST1");
RawDeckPtr rawDeck = parser->readToRawDeck( wconhistFile.string() );
BOOST_CHECK_EQUAL( 2U , rawDeck->size() );
}
/***************** Simple String parsing ********************************/
ParserKeywordPtr setupParserKeywordString(std::string name, int numberOfItems) {
ParserKeywordPtr parserKeyword(new ParserKeyword(name));
ParserRecordPtr parserRecord = parserKeyword->getRecord();
for (int i = 0; i < numberOfItems; i++) {
std::string name = "ITEM_" + boost::lexical_cast<std::string>(i);
ParserItemPtr stringItem(new ParserStringItem(name, SINGLE));
parserRecord->addItem(stringItem);
}
return parserKeyword;
}
RawDeckPtr setupRawDeckString(std::string name, int numberOfRecords, int numberOfItems) {
RawDeckPtr rawDeck(new RawDeck());
RawKeywordPtr rawKeyword(new RawKeyword(name));
for (int records = 0; records < numberOfRecords; records++) {
for (int i = 0; i < numberOfItems; i++) {
std::string data = "WELL-" + boost::lexical_cast<std::string>(i);
rawKeyword->addRawRecordString(data);
}
rawKeyword->addRawRecordString("/");
}
rawDeck->addKeyword(rawKeyword);
return rawDeck;
}
BOOST_AUTO_TEST_CASE(parseFromRawDeck_singleRawRecordsSingleStringItem_deckReturned) {
ParserPtr parser(new Parser());
parser->addKeyword(setupParserKeywordString("WWCT", 1));
DeckPtr deck = parser->parseFromRawDeck(setupRawDeckString("WWCT",1, 1));
BOOST_CHECK(deck->hasKeyword("WWCT"));
BOOST_CHECK_EQUAL(1U, deck->getKeyword("WWCT" , 0)->size());
}