diff --git a/opm/parser/eclipse/Parser/ParserIntItem.cpp b/opm/parser/eclipse/Parser/ParserIntItem.cpp index 74cc65ec2..d11b8e589 100644 --- a/opm/parser/eclipse/Parser/ParserIntItem.cpp +++ b/opm/parser/eclipse/Parser/ParserIntItem.cpp @@ -31,16 +31,32 @@ namespace Opm { DeckIntItemPtr deckItem(new DeckIntItem()); if (size()->sizeType() == ITEM_FIXED) { - std::string token = rawRecord->pop_front(); - std::vector intsFromCurrentToken; - fillIntVector(token, intsFromCurrentToken); - deckItem->push_back(intsFromCurrentToken); - } + std::vector 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(intsPreparedForDeckItem.size()); + std::string parserSizeValue = boost::lexical_cast(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& intsFromCurrentToken) { - intsFromCurrentToken.push_back(boost::lexical_cast(token)); + + void ParserIntItem::fillIntVector(std::string token, std::vector& dataVector) { + try { + dataVector.push_back(boost::lexical_cast(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."); + } } + } diff --git a/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp b/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp index cac59be96..b64773cbf 100644 --- a/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp @@ -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); diff --git a/opm/parser/eclipse/RawDeck/RawRecord.cpp b/opm/parser/eclipse/RawDeck/RawRecord.cpp index 43d988638..936039131 100644 --- a/opm/parser/eclipse/RawDeck/RawRecord.cpp +++ b/opm/parser/eclipse/RawDeck/RawRecord.cpp @@ -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; }