Added test for a case with fully, partial and empty record body, defaults are added

This commit is contained in:
Kristian Flikka
2013-08-26 15:17:52 +02:00
parent 0e5593dc9e
commit 480d14934b
8 changed files with 130 additions and 87 deletions

View File

@@ -178,10 +178,38 @@ BOOST_AUTO_TEST_CASE(parse_unknownkeywordWithstrictparsing_exceptionthrown) {
/*********************Testing truncated (default) records ***************************/
// Datafile contains 3 RADFIN4 keywords. One fully specified, one with 2 out of 11 items, and one with no items.
BOOST_AUTO_TEST_CASE(parse_truncatedrecords_deckFilledWithDefaults) {
ParserPtr parser(new Parser());
parser->loadKeywordsFromDirectory(KEYWORD_DIRECTORY);
DeckPtr deck = parser->parse("testdata/integration_tests/truncated_records.data");
BOOST_CHECK_EQUAL(4U, deck->size());
DeckKeywordConstPtr radfin4_0_full= deck->getKeyword("RADFIN4", 0);
DeckKeywordConstPtr radfin4_1_partial= deck->getKeyword("RADFIN4", 1);
DeckKeywordConstPtr radfin4_2_nodata= deck->getKeyword("RADFIN4", 2);
// Specified in datafile
BOOST_CHECK_EQUAL("NAME", radfin4_0_full->getRecord(0)->getItem(0)->getString(0));
BOOST_CHECK_EQUAL("NAME", radfin4_1_partial->getRecord(0)->getItem(0)->getString(0));
// Default string
BOOST_CHECK_EQUAL(ParserItem::defaultString(), radfin4_2_nodata->getRecord(0)->getItem(0)->getString(0));
// Specified in datafile
BOOST_CHECK_EQUAL(213, radfin4_0_full->getRecord(0)->getItem(1)->getInt(0));
BOOST_CHECK_EQUAL(213, radfin4_1_partial->getRecord(0)->getItem(1)->getInt(0));
// Default int
BOOST_CHECK_EQUAL(ParserItem::defaultInt(), radfin4_2_nodata->getRecord(0)->getItem(1)->getInt(0));
ParserKeywordConstPtr parserKeyword = parser->getKeyword("RADFIN4");
ParserRecordConstPtr parserRecord = parserKeyword->getRecord();
ParserItemConstPtr nwmaxItem = parserRecord->get("NWMAX");
ParserIntItemConstPtr intItem = boost::static_pointer_cast<const ParserIntItem>(nwmaxItem);
BOOST_CHECK_EQUAL(18, radfin4_0_full->getRecord(0)->getItem(10)->getInt(0));
BOOST_CHECK_EQUAL(intItem->getDefault(), radfin4_1_partial->getRecord(0)->getItem(10)->getInt(0));
BOOST_CHECK_EQUAL(intItem->getDefault(), radfin4_2_nodata->getRecord(0)->getItem(10)->getInt(0));
}

View File

@@ -35,11 +35,11 @@ namespace Opm {
initializeFromJsonFile(jsonFile);
}
DeckPtr Parser::parse(const std::string &dataFile) {
DeckPtr Parser::parse(const std::string &dataFile) const {
return parse(dataFile, true);
}
DeckPtr Parser::parse(const std::string &dataFile, bool strictParsing) {
DeckPtr Parser::parse(const std::string &dataFile, bool strictParsing) const {
DeckPtr deck(new Deck());
parseFile(deck, dataFile, strictParsing);
return deck;
@@ -49,9 +49,24 @@ namespace Opm {
size_t Parser::size() const {
return m_parserKeywords.size();
}
void Parser::addKeyword(ParserKeywordConstPtr parserKeyword) {
m_parserKeywords.insert(std::make_pair(parserKeyword->getName(), parserKeyword));
}
bool Parser::hasKeyword(const std::string& keyword) const {
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
}
ParserKeywordConstPtr Parser::getKeyword(const std::string& keyword) const {
if (hasKeyword(keyword)) {
return m_parserKeywords.at(keyword);
}
}
void Parser::parseFile(DeckPtr deck, const std::string &file, bool parseStrict) {
void Parser::parseFile(DeckPtr deck, const std::string &file, bool parseStrict) const {
std::ifstream inputstream;
inputstream.open(file.c_str());
@@ -69,7 +84,7 @@ namespace Opm {
parseFile(deck, pathToIncludedFile.string(), parseStrict);
} else {
if (hasKeyword(rawKeyword->getKeywordName())) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords[rawKeyword->getKeywordName()];
ParserKeywordConstPtr parserKeyword = m_parserKeywords.at(rawKeyword->getKeywordName());
DeckKeywordConstPtr deckKeyword = parserKeyword->parse(rawKeyword);
deck->addKeyword(deckKeyword);
} else {
@@ -85,10 +100,6 @@ namespace Opm {
throw std::invalid_argument("Failed to open file: " + file);
}
void Parser::addKeyword(ParserKeywordConstPtr parserKeyword) {
m_parserKeywords.insert(std::make_pair(parserKeyword->getName(), parserKeyword));
}
void Parser::initializeFromJsonFile(const boost::filesystem::path& jsonFile) {
Json::JsonObject jsonConfig(jsonFile);
if (jsonConfig.has_item("keywords")) {
@@ -111,11 +122,8 @@ namespace Opm {
}
bool Parser::hasKeyword(const std::string& keyword) const {
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
}
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& keywordString, bool strictParsing) {
RawKeywordPtr Parser::createRawKeyword(const DeckConstPtr deck, const std::string& keywordString, bool strictParsing) const {
if (hasKeyword(keywordString)) {
ParserKeywordConstPtr parserKeyword = m_parserKeywords.find(keywordString)->second;
if (parserKeyword->getSizeType() == UNDEFINED)
@@ -146,7 +154,7 @@ namespace Opm {
}
}
bool Parser::tryParseKeyword(const DeckConstPtr deck, std::ifstream& inputstream, RawKeywordPtr& rawKeyword, bool strictParsing) {
bool Parser::tryParseKeyword(const DeckConstPtr deck, std::ifstream& inputstream, RawKeywordPtr& rawKeyword, bool strictParsing) const {
std::string line;
while (std::getline(inputstream, line)) {

View File

@@ -43,13 +43,14 @@ namespace Opm {
Parser(const boost::filesystem::path& jsonFile);
/// The starting point of the parsing process. The supplied file is parsed, and the resulting Deck is returned.
DeckPtr parse(const std::string &dataFile);
DeckPtr parse(const std::string &dataFile, bool strictParsing);
DeckPtr parse(const std::string &dataFile) const;
DeckPtr parse(const std::string &dataFile, bool strictParsing) const;
/// Method to add ParserKeyword instances, these holding type and size information about the keywords and their data.
void addKeyword(ParserKeywordConstPtr parserKeyword);
bool hasKeyword(const std::string& keyword) const;
ParserKeywordConstPtr getKeyword(const std::string& keyword) const;
void initializeFromJsonFile( const boost::filesystem::path& jsonFile );
void loadKeywords(const Json::JsonObject& jsonKeywords);
bool loadKeywordFromFile(const boost::filesystem::path& configFile);
@@ -58,11 +59,11 @@ namespace Opm {
size_t size() const;
private:
std::map<std::string, ParserKeywordConstPtr> m_parserKeywords;
bool tryParseKeyword(const DeckConstPtr deck , std::ifstream& inputstream , RawKeywordPtr& rawKeyword, bool strictParsing);
void parseFile(DeckPtr deck , const std::string &file, bool strictParsing) ;
bool tryParseKeyword(const DeckConstPtr deck , std::ifstream& inputstream , RawKeywordPtr& rawKeyword, bool strictParsing) const;
void parseFile(DeckPtr deck , const std::string &file, bool strictParsing) const;
boost::filesystem::path verifyValidInputPath(const std::string& inputPath) const;
void populateDefaultKeywords();
RawKeywordPtr createRawKeyword(const DeckConstPtr deck , const std::string& keywordString, bool strictParsing);
RawKeywordPtr createRawKeyword(const DeckConstPtr deck , const std::string& keywordString, bool strictParsing) const;
};

View File

@@ -18,49 +18,49 @@
*/
template<class T> void fillVectorFromStringToken(std::string token, std::deque<T>& dataVector, T defaultValue, bool& defaultActive) const {
std::istringstream inputStream(token);
size_t starPos = token.find('*');
T value;
bool hasStar = (starPos != std::string::npos);
std::istringstream inputStream(token);
size_t starPos = token.find('*');
T value;
bool hasStar = (starPos != std::string::npos);
defaultActive = false;
defaultActive = false;
if (hasStar) {
bool singleDefault = (starPos == 0);
if (hasStar) {
bool singleDefault = (starPos == 0);
if (singleDefault) {
defaultActive = true;
inputStream.get();
if (token.size() > 1)
throw std::invalid_argument("Token : " + token + " is invalid.");
dataVector.push_back(defaultValue);
if (singleDefault) {
defaultActive = true;
inputStream.get();
if (token.size() > 1)
throw std::invalid_argument("Token : " + token + " is invalid.");
dataVector.push_back(defaultValue);
} else {
size_t multiplier;
int starChar;
inputStream >> multiplier;
starChar = inputStream.get();
if (starChar != '*')
throw std::invalid_argument("Error ...");
defaultActive = (inputStream.peek() == std::char_traits<char>::eof());
if (defaultActive)
value = defaultValue;
else
inputStream >> value;
for (size_t i = 0; i < multiplier; i++)
dataVector.push_back(value);
}
} else {
size_t multiplier;
int starChar;
inputStream >> multiplier;
starChar = inputStream.get();
if (starChar != '*')
throw std::invalid_argument("Error ...");
defaultActive = (inputStream.peek() == std::char_traits<char>::eof());
if (defaultActive)
value = defaultValue;
else
inputStream >> value;
for (size_t i = 0; i < multiplier; i++)
dataVector.push_back(value);
}
} else {
inputStream >> value;
dataVector.push_back(value);
}
inputStream.get();
if (!inputStream.eof())
throw std::invalid_argument("Spurious data at the end of: <" + token + ">");
inputStream.get();
if (!inputStream.eof())
throw std::invalid_argument("Spurious data at the end of: <" + token + ">");
}
template<class T> std::deque<T> readFromRawRecord(RawRecordPtr rawRecord, bool scanAll, T defaultValue, bool& defaultActive) const {
@@ -75,7 +75,7 @@ template<class T> std::deque<T> readFromRawRecord(RawRecordPtr rawRecord, bool s
if (rawRecord->size() == 0 || !scanAll)
cont = false;
} while (cont);
}
return data;
@@ -84,13 +84,13 @@ template<class T> std::deque<T> readFromRawRecord(RawRecordPtr rawRecord, bool s
template <class T> void pushBackToRecord(RawRecordPtr rawRecord, std::deque<T>& data, bool defaultActive) const {
for (size_t i = 0; i < data.size(); i++) {
if (defaultActive)
rawRecord->push_front("*");
else {
T value = data[i];
std::string stringValue = boost::lexical_cast<std::string>(value);
if (defaultActive)
rawRecord->push_front("*");
else {
T value = data[i];
std::string stringValue = boost::lexical_cast<std::string>(value);
rawRecord->push_front(stringValue);
rawRecord->push_front(stringValue);
}
}
}
}

View File

@@ -161,7 +161,7 @@ namespace Opm {
throw std::invalid_argument("The items: object must be an array");
}
ParserRecordPtr ParserKeyword::getRecord() {
ParserRecordPtr ParserKeyword::getRecord() const {
return m_record;
}

View File

@@ -41,7 +41,7 @@ namespace Opm {
static bool validName(const std::string& name);
ParserRecordPtr getRecord();
ParserRecordPtr getRecord() const;
const std::string& getName() const;
size_t getFixedSize() const;
bool hasFixedSize() const;

View File

@@ -21,51 +21,47 @@
#include <opm/parser/eclipse/Parser/ParserItem.hpp>
namespace Opm {
ParserRecord::ParserRecord() {
}
size_t ParserRecord::size() const {
return m_items.size();
}
void ParserRecord::addItem( ParserItemConstPtr item ) {
if (m_itemMap.find( item->name() ) == m_itemMap.end()) {
m_items.push_back( item );
m_itemMap[item->name()] = item;
void ParserRecord::addItem(ParserItemConstPtr item) {
if (m_itemMap.find(item->name()) == m_itemMap.end()) {
m_items.push_back(item);
m_itemMap[item->name()] = item;
} else
throw std::invalid_argument("Itemname: " + item->name() + " already exists.");
}
ParserItemConstPtr ParserRecord::get(size_t index) const{
ParserItemConstPtr ParserRecord::get(size_t index) const {
if (index < m_items.size())
return m_items[ index ];
else
throw std::out_of_range("Out of range");
}
ParserItemConstPtr ParserRecord::get(const std::string& itemName) const {
if (m_itemMap.find( itemName ) == m_itemMap.end())
if (m_itemMap.find(itemName) == m_itemMap.end())
throw std::invalid_argument("Itemname: " + itemName + " does not exist.");
else
{
else {
std::map<std::string, ParserItemConstPtr>::const_iterator theItem = m_itemMap.find(itemName);
return (*theItem).second;
}
}
DeckRecordConstPtr ParserRecord::parse(RawRecordPtr rawRecord) const {
DeckRecordPtr deckRecord(new DeckRecord());
for(size_t i=0; i<size(); i++) {
ParserItemConstPtr parserItem = get(i);
DeckItemConstPtr deckItem = parserItem->scan(rawRecord);
deckRecord->addItem(deckItem);
for (size_t i = 0; i < size(); i++) {
ParserItemConstPtr parserItem = get(i);
DeckItemConstPtr deckItem = parserItem->scan(rawRecord);
deckRecord->addItem(deckItem);
}
return deckRecord;
}
}

View File

@@ -51,7 +51,6 @@ BOOST_AUTO_TEST_CASE(addKeyword_keyword_doesntfail) {
}
}
/************************ JSON config related tests **********************'*/
BOOST_AUTO_TEST_CASE(hasKeyword_hasKeyword_returnstrue) {
ParserPtr parser(new Parser());
@@ -60,6 +59,17 @@ BOOST_AUTO_TEST_CASE(hasKeyword_hasKeyword_returnstrue) {
}
BOOST_AUTO_TEST_CASE(Keyword_getKeyword_returnskeyword) {
ParserPtr parser(new Parser());
ParserKeywordConstPtr parserKeyword(new ParserKeyword("FJAS"));
parser->addKeyword(parserKeyword);
BOOST_CHECK_EQUAL(parserKeyword, parser->getKeyword("FJAS"));
}
/************************ JSON config related tests **********************'*/
BOOST_AUTO_TEST_CASE(addKeywordJSON_hasKeyword_returnstrue) {
ParserPtr parser(new Parser());
Json::JsonObject jsonConfig("{\"name\": \"BPR\", \"size\" : 100 , \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}");