deckkeyword: new constructors takes (ParserKeyword, std::vector).

DeckKeyword: test for vector<int> data constructor.

DeckKeyword takes std::vector<int> and std::vector<double>.

...

...

...

...
This commit is contained in:
Steinar Foss
2019-10-10 14:40:32 +02:00
parent e6e8b070d7
commit 17f246c1a7
3 changed files with 101 additions and 0 deletions

View File

@@ -43,6 +43,9 @@ namespace Opm {
DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<std::vector<DeckValue>>& record_list);
DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<int>& data);
DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<double>& data);
const std::string& name() const;
void setFixedSize();
void setLocation(const std::pair<const std::string&, std::size_t>& location);

View File

@@ -111,6 +111,61 @@ namespace Opm {
}
DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<int>& data) :
DeckKeyword(parserKeyword)
{
if (!parserKeyword.isDataKeyword())
throw std::invalid_argument("Deckkeyword '" + name() + "' is not a data keyword.");
const ParserRecord& parser_record = parserKeyword.getRecord(0);
const ParserItem& parser_item = parser_record.get(0);
setDataKeyword();
DeckItem item;
if (parser_item.dataType() == type_tag::fdouble) {
item = DeckItem(parser_item.name(), double());
for (double val : data)
item.push_back(val);
}
else if (parser_item.dataType() == type_tag::integer) {
item = DeckItem(parser_item.name(), int());
for (int val : data)
item.push_back(val);
}
else
throw std::invalid_argument("Input to DeckKeyword '" + name() + "': cannot be std::vector<int>.");
DeckRecord deck_record;
deck_record.addItem( std::move(item) );
addRecord( std::move(deck_record) );
}
DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<double>& data) :
DeckKeyword(parserKeyword)
{
if (!parserKeyword.isDataKeyword())
throw std::invalid_argument("Deckkeyword '" + name() + "' is not a data keyword.");
const ParserRecord& parser_record = parserKeyword.getRecord(0);
const ParserItem& parser_item = parser_record.get(0);
setDataKeyword();
if (parser_item.dataType() != type_tag::fdouble)
throw std::invalid_argument("Input to DeckKeyword '" + name() + "': cannot be std::vector<double>.");
DeckItem item(parser_item.name(), double());
for (double val : data)
item.push_back(val);
DeckRecord deck_record;
deck_record.addItem( std::move(item) );
addRecord( std::move(deck_record) );
}
void DeckKeyword::setFixedSize() {
m_slashTerminated = false;
}

View File

@@ -122,3 +122,46 @@ BOOST_AUTO_TEST_CASE(DeckKeywordConstructor) {
}
BOOST_AUTO_TEST_CASE(DeckKeywordVectorInt) {
Parser parser;
const ParserKeyword& hbnum = parser.getKeyword("HBNUM");
const ParserKeyword& box = parser.getKeyword("BOX");
std::vector<int> data = {0, 1, 2, 3, 4, 5, 6, 7, 8};
BOOST_CHECK_THROW( DeckKeyword(box, data), std::invalid_argument );
DeckKeyword hbnum_kw(hbnum, data);
BOOST_CHECK(hbnum_kw.isDataKeyword());
BOOST_CHECK_EQUAL(hbnum_kw.getDataSize(), 9);
BOOST_CHECK( hbnum_kw.getIntData() == data );
std::vector<double> data_double = {1.1, 2.2};
BOOST_CHECK_THROW(DeckKeyword(hbnum, data_double), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(DeckKeywordVectorDouble) {
Parser parser;
const ParserKeyword& poro = parser.getKeyword("PORO");
const ParserKeyword& box = parser.getKeyword("BOX");
std::vector<double> data = {1.1, 2.2, 3.3};
BOOST_CHECK_THROW(DeckKeyword(box, data), std::invalid_argument);
DeckKeyword poro_kw(poro, data);
BOOST_CHECK(poro_kw.isDataKeyword());
BOOST_CHECK_EQUAL(poro_kw.getDataSize(), 3);
BOOST_CHECK( poro_kw.getRawDoubleData() == data );
std::vector<int> data_int = {1, 2};
poro_kw = DeckKeyword(poro, data_int);
std::vector<double> raw_int = {1.0, 2.0};
BOOST_CHECK( poro_kw.getRawDoubleData() == raw_int );
}