Simple scan implementation in ParserIntItem added. DeckIntItem now support push_back
This commit is contained in:
parent
2d6e6842d6
commit
2088c3e96d
@ -27,5 +27,12 @@ namespace Opm {
|
||||
} else
|
||||
throw std::out_of_range("Out of range, index must be lower than " + boost::lexical_cast<std::string>(m_data.size()));
|
||||
}
|
||||
|
||||
void DeckIntItem::push_back(std::vector<int> data) {
|
||||
for (size_t i=0; i<data.size(); i++) {
|
||||
m_data.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@ namespace Opm {
|
||||
class DeckIntItem : public DeckItem {
|
||||
public:
|
||||
int getInt(unsigned int index) const;
|
||||
void push_back(std::vector<int> data);
|
||||
private:
|
||||
std::vector<int> m_data;
|
||||
};
|
||||
|
Binary file not shown.
@ -35,3 +35,12 @@ BOOST_AUTO_TEST_CASE(GetIntAtIndex_NoData_ExceptionThrown) {
|
||||
BOOST_CHECK_THROW(deckIntItem.getInt(0), std::out_of_range);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(PushBack_VectorPushed_ElementsCorrect) {
|
||||
DeckIntItem deckIntItem;
|
||||
std::vector<int> pushThese;
|
||||
pushThese.push_back(13);
|
||||
pushThese.push_back(33);
|
||||
deckIntItem.push_back(pushThese);
|
||||
BOOST_CHECK_EQUAL(13, deckIntItem.getInt(0));
|
||||
BOOST_CHECK_EQUAL(33, deckIntItem.getInt(1));
|
||||
}
|
||||
|
@ -17,12 +17,30 @@
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
|
||||
|
||||
|
||||
namespace Opm {
|
||||
DeckIntItemConstPtr ParserIntItem::scan(std::vector<std::string>& stringTokens) {
|
||||
DeckIntItemConstPtr deckItem(new DeckIntItem());
|
||||
|
||||
/// Scans the rawRecords data according to the ParserItems definition.
|
||||
/// returns a DeckIntItem object.
|
||||
/// NOTE: data are popped from the rawRecords deque!
|
||||
|
||||
DeckIntItemPtr ParserIntItem::scan(RawRecordPtr rawRecord) {
|
||||
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);
|
||||
}
|
||||
|
||||
return deckItem;
|
||||
}
|
||||
|
||||
|
||||
void ParserIntItem::fillIntVector(std::string token, std::vector<int>& intsFromCurrentToken) {
|
||||
intsFromCurrentToken.push_back(boost::lexical_cast<int>(token));
|
||||
}
|
||||
}
|
||||
|
@ -23,16 +23,16 @@
|
||||
|
||||
#include <opm/parser/eclipse/Parser/ParserItem.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckIntItem.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
|
||||
namespace Opm {
|
||||
|
||||
class ParserIntItem : public ParserItem {
|
||||
public:
|
||||
ParserIntItem(const std::string& itemName, ParserItemSizeConstPtr itemSize) : ParserItem(itemName, itemSize) {};
|
||||
DeckIntItemConstPtr scan(std::vector<std::string>& stringTokens);
|
||||
DeckIntItemPtr scan(RawRecordPtr rawRecord);
|
||||
|
||||
private:
|
||||
|
||||
void fillIntVector(std::string token, std::vector<int>& intsFromCurrentToken);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -25,4 +25,12 @@ namespace Opm {
|
||||
m_name.assign(itemName);
|
||||
m_itemSize = itemSize;
|
||||
}
|
||||
|
||||
std::string ParserItem::name() {
|
||||
return m_name;
|
||||
}
|
||||
|
||||
ParserItemSizeConstPtr ParserItem::size() {
|
||||
return m_itemSize;
|
||||
}
|
||||
}
|
||||
|
@ -30,13 +30,18 @@
|
||||
namespace Opm {
|
||||
|
||||
class ParserItem {
|
||||
public:
|
||||
ParserItem(const std::string& itemName, ParserItemSizeConstPtr itemSize);
|
||||
std::string name();
|
||||
ParserItemSizeConstPtr size();
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
ParserItemSizeConstPtr m_itemSize;
|
||||
};
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
ParserItem(const std::string& itemName, ParserItemSizeConstPtr itemSize);
|
||||
#endif
|
||||
|
||||
// bool scanItem(const std::string& itemString, T& value) {
|
||||
// std::istringstream inputStream(itemString);
|
||||
@ -54,7 +59,7 @@ namespace Opm {
|
||||
// } else
|
||||
// return false;
|
||||
// }
|
||||
};
|
||||
|
||||
|
||||
// int scanItems(const std::string& itemString, size_t items, std::vector<T>& values) {
|
||||
// std::istringstream inputStream(itemString);
|
||||
@ -75,7 +80,4 @@ namespace Opm {
|
||||
// }
|
||||
|
||||
|
||||
// };
|
||||
}
|
||||
|
||||
#endif
|
||||
// };
|
||||
|
@ -28,6 +28,8 @@
|
||||
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
|
||||
#include <opm/parser/eclipse/Parser/ParserItemSize.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/Deck/DeckIntItem.hpp>
|
||||
|
||||
using namespace Opm;
|
||||
@ -35,28 +37,45 @@ using namespace Opm;
|
||||
BOOST_AUTO_TEST_CASE(Initialize) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
|
||||
BOOST_REQUIRE_NO_THROW(ParserIntItem item1("ITEM1", itemSize));
|
||||
BOOST_REQUIRE_NO_THROW(ParserStringItem item1("ITEM1", itemSize));
|
||||
BOOST_REQUIRE_NO_THROW(ParserBoolItem item1("ITEM1", itemSize));
|
||||
BOOST_REQUIRE_NO_THROW(ParserDoubleItem item1("ITEM1", itemSize));
|
||||
BOOST_CHECK_NO_THROW(ParserIntItem item1("ITEM1", itemSize));
|
||||
BOOST_CHECK_NO_THROW(ParserStringItem item1("ITEM1", itemSize));
|
||||
BOOST_CHECK_NO_THROW(ParserBoolItem item1("ITEM1", itemSize));
|
||||
BOOST_CHECK_NO_THROW(ParserDoubleItem item1("ITEM1", itemSize));
|
||||
}
|
||||
|
||||
//BOOST_AUTO_TEST_CASE(TestScanInt) {
|
||||
// ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
// ParserIntItem itemInt("ITEM2", itemSize);
|
||||
//
|
||||
// std::vector<std::string> singleItem;
|
||||
// singleItem.push_back("100");
|
||||
// DeckIntItemConstPtr deckIntItem = itemInt.scan(singleItem);
|
||||
//
|
||||
// BOOST_REQUIRE_EQUAL(100, deckIntItem->getInt(0));
|
||||
BOOST_AUTO_TEST_CASE(Name_ReturnsCorrectName) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
ParserIntItem item1("ITEM1", itemSize);
|
||||
BOOST_CHECK_EQUAL("ITEM1", item1.name());
|
||||
ParserIntItem item2("", itemSize);
|
||||
BOOST_CHECK_EQUAL("", item2.name());
|
||||
}
|
||||
|
||||
// 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(Size_ReturnsCorrectSize) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
ParserIntItem item1("ITEM1", itemSize);
|
||||
BOOST_CHECK_EQUAL(itemSize, item1.size());
|
||||
BOOST_CHECK_EQUAL(10U, item1.size()->sizeValue());
|
||||
ParserItemSizeConstPtr itemSize2(new ParserItemSize(UNSPECIFIED));
|
||||
ParserIntItem item2("ITEM2", itemSize2);
|
||||
BOOST_CHECK_EQUAL(itemSize2, item2.size());
|
||||
BOOST_CHECK_EQUAL(UNSPECIFIED, item2.size()->sizeType());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestScanInt) {
|
||||
ParserItemSizeConstPtr itemSize(new ParserItemSize(1));
|
||||
ParserIntItem itemInt("ITEM2", itemSize);
|
||||
|
||||
RawRecordPtr rawRecord(new RawRecord("100 /"));
|
||||
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(TestScanDouble) {
|
||||
// ParserItemSizeConstPtr itemSize(new ParserItemSize(10));
|
||||
|
@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
|
||||
const std::string& recordString = record->getRecordString();
|
||||
BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
|
||||
|
||||
const std::vector<std::string>& recordElements = record->getItems();
|
||||
const std::deque<std::string>& recordElements = record->getItems();
|
||||
BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size());
|
||||
|
||||
BOOST_CHECK_EQUAL("NODIR", recordElements[0]);
|
||||
|
@ -72,6 +72,6 @@ BOOST_AUTO_TEST_CASE(ParseFullTestFile) {
|
||||
|
||||
const std::string& recordString = records.front()->getRecordString();
|
||||
BOOST_CHECK_EQUAL("20 20 15 15 15 50", recordString);
|
||||
std::vector<std::string> recordItems = records.front()->getItems();
|
||||
std::deque<std::string> recordItems = records.front()->getItems();
|
||||
BOOST_CHECK_EQUAL((unsigned) 6, recordItems.size());
|
||||
}
|
||||
|
@ -138,10 +138,10 @@ namespace Opm {
|
||||
|
||||
std::list<RawRecordConstPtr> records = (*keyword)->getRecords();
|
||||
for (std::list<RawRecordConstPtr>::const_iterator record = records.begin(); record != records.end(); record++) {
|
||||
std::vector<std::string> recordItems = (*record)->getItems();
|
||||
std::deque<std::string> recordItems = (*record)->getItems();
|
||||
|
||||
for (std::vector<std::string>::const_iterator recordItem = recordItems.begin(); recordItem != recordItems.end(); recordItem++) {
|
||||
os << (*recordItem) << " ";
|
||||
for (size_t i=0; i<recordItems.size(); i++) {
|
||||
os << recordItems[i] << " ";
|
||||
}
|
||||
os << " / -- Data\n";
|
||||
}
|
||||
|
@ -50,7 +50,12 @@ namespace Opm {
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::string>& RawRecord::getItems() const {
|
||||
std::string RawRecord::pop_front() {
|
||||
return m_recordItems[0];
|
||||
}
|
||||
|
||||
|
||||
const std::deque<std::string>& RawRecord::getItems() const {
|
||||
return m_recordItems;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@
|
||||
#define RECORD_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
|
||||
|
||||
namespace Opm {
|
||||
@ -34,13 +34,17 @@ namespace Opm {
|
||||
public:
|
||||
RawRecord();
|
||||
RawRecord(const std::string& singleRecordString);
|
||||
|
||||
std::string pop_front();
|
||||
|
||||
const std::string& getRecordString() const;
|
||||
const std::vector<std::string>& getItems() const;
|
||||
const std::deque<std::string>& getItems() const;
|
||||
|
||||
static bool isTerminatedRecordString(const std::string& candidateRecordString);
|
||||
virtual ~RawRecord();
|
||||
private:
|
||||
std::string m_sanitizedRecordString;
|
||||
std::vector<std::string> m_recordItems;
|
||||
std::deque<std::string> m_recordItems;
|
||||
|
||||
void setRecordString(const std::string& singleRecordString);
|
||||
void splitSingleRecordString();
|
||||
|
@ -31,7 +31,7 @@ BOOST_AUTO_TEST_CASE(RawRecordGetRecordStringReturnsTrimmedString) {
|
||||
BOOST_AUTO_TEST_CASE(RawRecordGetRecordsCorrectElementsReturned) {
|
||||
Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /"));
|
||||
|
||||
const std::vector<std::string>& recordElements = record->getItems();
|
||||
const std::deque<std::string>& recordElements = record->getItems();
|
||||
BOOST_CHECK_EQUAL((unsigned) 4, recordElements.size());
|
||||
|
||||
BOOST_CHECK_EQUAL("NODIR ", recordElements[0]);
|
||||
|
Loading…
Reference in New Issue
Block a user