ParserIntItem's scan function now supports ITEM_FIXED parsing
This commit is contained in:
@@ -31,16 +31,32 @@ namespace Opm {
|
||||
DeckIntItemPtr deckItem(new DeckIntItem());
|
||||
|
||||
if (size()->sizeType() == ITEM_FIXED) {
|
||||
std::string token = rawRecord->pop_front();
|
||||
std::vector<int> intsFromCurrentToken;
|
||||
fillIntVector(token, intsFromCurrentToken);
|
||||
deckItem->push_back(intsFromCurrentToken);
|
||||
}
|
||||
std::vector<int> intsPreparedForDeckItem;
|
||||
|
||||
do {
|
||||
std::string token = rawRecord->pop_front();
|
||||
fillIntVector(token, intsPreparedForDeckItem);
|
||||
} while (intsPreparedForDeckItem.size() < size()->sizeValue() && rawRecord->getItems().size() > 0U);
|
||||
|
||||
if (intsPreparedForDeckItem.size() != size()->sizeValue()) {
|
||||
std::string preparedInts = boost::lexical_cast<std::string>(intsPreparedForDeckItem.size());
|
||||
std::string parserSizeValue = boost::lexical_cast<std::string>(size()->sizeValue());
|
||||
throw std::invalid_argument("The number of parsed ints (" + preparedInts + ") did not correspond to the fixed size of the ParserItem (" + parserSizeValue + ")");
|
||||
}
|
||||
deckItem->push_back(intsPreparedForDeckItem);
|
||||
} else {
|
||||
throw std::invalid_argument("Unsupported size type, only support ITEM_FIXED");
|
||||
}
|
||||
return deckItem;
|
||||
}
|
||||
|
||||
void ParserIntItem::fillIntVector(std::string token, std::vector<int>& intsFromCurrentToken) {
|
||||
intsFromCurrentToken.push_back(boost::lexical_cast<int>(token));
|
||||
|
||||
void ParserIntItem::fillIntVector(std::string token, std::vector<int>& dataVector) {
|
||||
try {
|
||||
dataVector.push_back(boost::lexical_cast<int>(token));
|
||||
}
|
||||
catch (std::bad_cast& exception) {
|
||||
throw std::invalid_argument("std::bad_cast exception thrown, unable to cast string token (" + token +") to int. Check data.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -62,21 +62,41 @@ BOOST_AUTO_TEST_CASE(Size_ReturnsCorrectSize) {
|
||||
BOOST_CHECK_EQUAL(UNSPECIFIED, item2.size()->sizeType());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestScanInt) {
|
||||
BOOST_AUTO_TEST_CASE(Scan_SingleItemFixed_CorrectIntSetInDeckItem) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(1));
|
||||
ParserIntItem itemInt("ITEM2", itemSize);
|
||||
|
||||
RawRecordPtr rawRecord(new RawRecord("100 /"));
|
||||
RawRecordPtr rawRecord(new RawRecord("100 44.3 'Heisann' /"));
|
||||
DeckIntItemConstPtr deckIntItem = itemInt.scan(rawRecord);
|
||||
BOOST_CHECK_EQUAL(100, deckIntItem->getInt(0));
|
||||
|
||||
// BOOST_REQUIRE(!itemInt.scanItem("200X", value));
|
||||
// BOOST_REQUIRE_EQUAL(value, 100);
|
||||
//
|
||||
// BOOST_REQUIRE(!itemInt.scanItem("", value));
|
||||
// BOOST_REQUIRE_EQUAL(value, 100);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Scan_SeveralIntsFixed_CorrectIntsSetInDeckItem) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(3));
|
||||
ParserIntItem itemInt("ITEM2", itemSize);
|
||||
|
||||
RawRecordPtr rawRecord(new RawRecord("100 443 338932 222.33 'Heisann' /"));
|
||||
DeckIntItemConstPtr deckIntItem = itemInt.scan(rawRecord);
|
||||
BOOST_CHECK_EQUAL(100, deckIntItem->getInt(0));
|
||||
BOOST_CHECK_EQUAL(443, deckIntItem->getInt(1));
|
||||
BOOST_CHECK_EQUAL(338932, deckIntItem->getInt(2));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Scan_RawRecordInconsistencies_ExceptionThrown) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(3));
|
||||
ParserIntItem itemInt("ITEM2", itemSize);
|
||||
|
||||
// Too few elements
|
||||
RawRecordPtr rawRecord(new RawRecord("100 443 /"));
|
||||
BOOST_CHECK_THROW(itemInt.scan(rawRecord), std::invalid_argument);
|
||||
|
||||
// Wrong type
|
||||
RawRecordPtr rawRecord2(new RawRecord("100 443 333.2 /"));
|
||||
BOOST_CHECK_THROW(itemInt.scan(rawRecord2), std::invalid_argument);
|
||||
}
|
||||
|
||||
// Husk kombocase, dvs en record med alt morro i 333 * 2*23 2* 'HEI' 4*'NEIDA' /
|
||||
|
||||
//BOOST_AUTO_TEST_CASE(TestScanDouble) {
|
||||
// ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
// ParserDoubleItem itemDouble("ITEM2", itemSize);
|
||||
|
||||
@@ -51,7 +51,9 @@ namespace Opm {
|
||||
}
|
||||
|
||||
std::string RawRecord::pop_front() {
|
||||
return m_recordItems[0];
|
||||
std::string front = m_recordItems.front();
|
||||
m_recordItems.pop_front();
|
||||
return front;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user