Added enum for size of rawkeywords.

This commit is contained in:
Joakim Hove
2013-12-04 17:21:54 +01:00
parent ed06ebf4d3
commit 9ccc70b58d
6 changed files with 92 additions and 34 deletions

View File

@@ -63,6 +63,7 @@ RawDeck/RawConsts.hpp
RawDeck/RawKeyword.hpp
RawDeck/RawRecord.hpp
RawDeck/StarToken.hpp
RawDeck/RawEnums.hpp
#
Deck/Deck.hpp
Deck/DeckKeyword.hpp

View File

@@ -380,8 +380,8 @@ BOOST_AUTO_TEST_CASE(ParseEmptyRecord) {
ParserIntItemConstPtr item(new ParserIntItem(std::string("ITEM") , ALL));
RawKeywordPtr rawkeyword(new RawKeyword( tabdimsKeyword->getName() , "FILE" , 10U , 1));
BOOST_CHECK_EQUAL( Raw::FIXED , rawkeyword->getSizeType());
rawkeyword->addRawRecordString("/");
tabdimsKeyword->addItem(item);

View File

@@ -0,0 +1,40 @@
/*
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/>.
*/
#ifndef RAW_ENUMS_H
#define RAW_ENUMS_H
#include <string>
#include <stdexcept>
namespace Opm {
namespace Raw {
enum KeywordSizeEnum {
SLASH_TERMINATED = 0,
FIXED = 1,
UNKNOWN = 3,
TABLE_COLLECTION = 4
};
}
}
#endif

View File

@@ -26,18 +26,22 @@
namespace Opm {
RawKeyword::RawKeyword(const std::string& name, const std::string& filename, size_t lineNR) {
commonInit(name,filename,lineNR);
RawKeyword::RawKeyword(const std::string& name, Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR) {
if (sizeType == Raw::SLASH_TERMINATED || sizeType == Raw::UNKNOWN) {
commonInit(name,filename,lineNR);
m_sizeType = sizeType;
} else
throw std::invalid_argument("Error - invalid sizetype on input");
}
RawKeyword::RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize, bool isTableCollection ) {
commonInit(name,filename,lineNR);
if (isTableCollection) {
m_isTableCollection = true;
m_sizeType = Raw::TABLE_COLLECTION;
m_numTables = inputSize;
} else {
m_fixedSizeKeyword = true;
m_sizeType = Raw::FIXED;
m_fixedSize = inputSize;
if (m_fixedSize == 0)
m_isFinished = true;
@@ -52,8 +56,6 @@ namespace Opm {
m_filename = filename;
m_lineNR = lineNR;
m_isFinished = false;
m_fixedSizeKeyword = false;
m_isTableCollection = false;
m_currentNumTables = 0;
}
@@ -75,8 +77,8 @@ namespace Opm {
void RawKeyword::addRawRecordString(const std::string& partialRecordString) {
m_partialRecordString += " " + partialRecordString;
if (!m_fixedSizeKeyword && isTerminator( m_partialRecordString )) {
if (m_isTableCollection) {
if (m_sizeType != Raw::FIXED && isTerminator( m_partialRecordString )) {
if (m_sizeType == Raw::TABLE_COLLECTION) {
m_currentNumTables += 1;
if (m_currentNumTables == m_numTables) {
m_isFinished = true;
@@ -94,7 +96,7 @@ namespace Opm {
m_records.push_back(record);
m_partialRecordString.clear();
if (m_fixedSizeKeyword && (m_records.size() == m_fixedSize))
if (m_sizeType == Raw::FIXED && (m_records.size() == m_fixedSize))
m_isFinished = true;
}
}
@@ -109,9 +111,6 @@ namespace Opm {
}
bool RawKeyword::isTableCollection() const {
return m_isTableCollection;
}
RawRecordPtr RawKeyword::getRecord(size_t index) const {
if (index < m_records.size()) {
@@ -188,5 +187,9 @@ namespace Opm {
return m_lineNR;
}
Raw::KeywordSizeEnum RawKeyword::getSizeType() const {
return m_sizeType;
}
}

View File

@@ -25,7 +25,8 @@
#include <vector>
#include <memory>
#include "RawRecord.hpp"
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
#include <opm/parser/eclipse/RawDeck/RawEnums.hpp>
namespace Opm {
@@ -36,30 +37,31 @@ namespace Opm {
class RawKeyword {
public:
RawKeyword(const std::string& name , const std::string& filename, size_t lineNR);
RawKeyword(const std::string& name , Raw::KeywordSizeEnum sizeType , const std::string& filename, size_t lineNR);
RawKeyword(const std::string& name , const std::string& filename, size_t lineNR , size_t inputSize , bool isTableCollection = false);
const std::string& getKeywordName() const;
void addRawRecordString(const std::string& partialRecordString);
size_t size() const;
Raw::KeywordSizeEnum getSizeType() const;
RawRecordPtr getRecord(size_t index) const;
static bool tryParseKeyword(const std::string& line, std::string& result);
static bool isTerminator(std::string line);
static bool useLine(std::string line);
bool isPartialRecordStringEmpty() const;
bool isFinished() const;
bool isTableCollection() const;
bool unKnownSize() const;
const std::string& getFilename() const;
size_t getLineNR() const;
private:
bool m_isTableCollection;
Raw::KeywordSizeEnum m_sizeType;
bool m_isFinished;
bool m_fixedSizeKeyword;
size_t m_fixedSize;
size_t m_numTables;
size_t m_currentNumTables;

View File

@@ -21,50 +21,58 @@
#include <stdexcept>
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawEnums.hpp>
using namespace Opm;
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorKeywordSet) {
RawKeyword keyword("KEYYWORD", "FILE" , 10U);
RawKeyword keyword("KEYYWORD", Raw::SLASH_TERMINATED , "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "KEYYWORD");
BOOST_CHECK_EQUAL(Raw::SLASH_TERMINATED , keyword.getSizeType());
}
BOOST_AUTO_TEST_CASE(RawKeywordSizeTypeInvalidThrows) {
BOOST_CHECK_THROW( RawKeyword("KEYYWORD", Raw::FIXED , "FILE" , 0U) , std::invalid_argument);
BOOST_CHECK_THROW( RawKeyword("KEYYWORD", Raw::TABLE_COLLECTION , "FILE" , 10U) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordGiveKeywordToConstructorTooLongThrows) {
BOOST_CHECK_THROW(RawKeyword keyword("KEYYYWORD", "FILE" , 10U), std::invalid_argument);
BOOST_CHECK_THROW(RawKeyword keyword("KEYYYWORD", Raw::SLASH_TERMINATED , "FILE" , 10U), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialWhitespaceInKeywordThrows) {
BOOST_CHECK_THROW(RawKeyword(" TELONG", "FILE" , 10U), std::invalid_argument);
BOOST_CHECK_THROW(RawKeyword(" TELONG", Raw::SLASH_TERMINATED, "FILE" , 10U), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(constructor_mixedCaseName_throws) {
BOOST_CHECK_THROW(RawKeyword("Test", "FILE" , 10U), std::invalid_argument);
BOOST_CHECK_THROW(RawKeyword("Test", Raw::SLASH_TERMINATED , "FILE" , 10U), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetKeywordInitialTabInKeywordThrows) {
BOOST_CHECK_THROW( RawKeyword("\tTELONG", "FILE" , 10U), std::invalid_argument);
BOOST_CHECK_THROW( RawKeyword("\tTELONG", Raw::SLASH_TERMINATED , "FILE" , 10U), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(RawKeywordSetCorrectLenghtKeywordNoError) {
RawKeyword keyword("GOODONE", "FILE" , 10U);
RawKeyword keyword("GOODONE", Raw::SLASH_TERMINATED , "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "GOODONE");
}
BOOST_AUTO_TEST_CASE(RawKeywordSet8CharKeywordWithTrailingWhitespaceKeywordTrimmed) {
RawKeyword keyword("GOODONEE ", "FILE" , 10U);
RawKeyword keyword("GOODONEE ", Raw::SLASH_TERMINATED , "FILE" , 10U);
BOOST_CHECK(keyword.getKeywordName() == "GOODONEE");
}
BOOST_AUTO_TEST_CASE(addRecord_singleRecord_recordAdded) {
RawKeyword keyword("TEST", "FILE" , 10U);
RawKeyword keyword("TEST", Raw::SLASH_TERMINATED , "FILE" , 10U);
keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_EQUAL(1U, keyword.size());
}
BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) {
RawKeyword keyword("TEST", "FILE" , 10U);
RawKeyword keyword("TEST", Raw::SLASH_TERMINATED , "FILE" , 10U);
keyword.addRawRecordString("test 1 3 4 /");
BOOST_CHECK_THROW(keyword.getRecord(1), std::range_error);
}
@@ -72,7 +80,7 @@ BOOST_AUTO_TEST_CASE(getRecord_outOfRange_throws) {
BOOST_AUTO_TEST_CASE(isFinished_undef_size) {
RawKeyword keyword("TEST", "FILE" , 10U);
RawKeyword keyword("TEST", Raw::SLASH_TERMINATED , "FILE" , 10U);
BOOST_CHECK( !keyword.isFinished() );
keyword.addRawRecordString("test 1 3 4 /");
@@ -147,22 +155,26 @@ BOOST_AUTO_TEST_CASE(useLine) {
BOOST_AUTO_TEST_CASE(isTableCollection) {
RawKeyword keyword1("TEST" , "FILE" , 10U, 4U , false);
RawKeyword keyword2("TEST2", "FILE" , 10U);
BOOST_CHECK_EQUAL( false , keyword1.isTableCollection());
BOOST_CHECK_EQUAL( false , keyword2.isTableCollection());
RawKeyword keyword2("TEST2", Raw::SLASH_TERMINATED , "FILE" , 10U);
BOOST_CHECK_EQUAL( Raw::FIXED , keyword1.getSizeType());
BOOST_CHECK_EQUAL( Raw::SLASH_TERMINATED , keyword2.getSizeType());
}
BOOST_AUTO_TEST_CASE(CreateTableCollection) {
RawKeyword keyword1("TEST" , "FILE" , 10U, 2, true);
BOOST_CHECK_EQUAL( true , keyword1.isTableCollection());
BOOST_CHECK_EQUAL( Raw::TABLE_COLLECTION , keyword1.getSizeType());
}
BOOST_AUTO_TEST_CASE(CreateWithFileAndLine) {
RawKeyword keyword1("TEST" , "XXX", 100);
RawKeyword keyword1("TEST" , Raw::SLASH_TERMINATED , "XXX", 100);
BOOST_CHECK_EQUAL( "XXX" , keyword1.getFilename());
BOOST_CHECK_EQUAL( 100U , keyword1.getLineNR() );
}
BOOST_AUTO_TEST_CASE(isUnknownSize) {
RawKeyword keyword("TEST2", Raw::UNKNOWN , "FILE" , 10U);
BOOST_CHECK_EQUAL( Raw::UNKNOWN , keyword.getSizeType( ));
}