diff --git a/opm/parser/eclipse/CMakeLists.txt b/opm/parser/eclipse/CMakeLists.txt index cc2532354..47d5154c4 100644 --- a/opm/parser/eclipse/CMakeLists.txt +++ b/opm/parser/eclipse/CMakeLists.txt @@ -11,6 +11,7 @@ RawDeck/RawParserKWs.cpp ) set( deck_source Deck/DeckIntItem.cpp +Deck/DeckDoubleItem.cpp ) set( parser_source diff --git a/opm/parser/eclipse/Deck/DeckDoubleItem.cpp b/opm/parser/eclipse/Deck/DeckDoubleItem.cpp new file mode 100644 index 000000000..69d1a40ac --- /dev/null +++ b/opm/parser/eclipse/Deck/DeckDoubleItem.cpp @@ -0,0 +1,55 @@ +/* + 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 . + */ + +#include + +#include + +namespace Opm { + + double DeckDoubleItem::getDouble(size_t index) const { + if (index < m_data.size()) { + return m_data[index]; + } else + throw std::out_of_range("Out of range, index must be lower than " + boost::lexical_cast(m_data.size())); + } + + + void DeckDoubleItem::push_back(std::vector data , size_t items) { + for (size_t i=0; i data) { + push_back( data , data.size() ); + } + + void DeckDoubleItem::push_back(double data) { + m_data.push_back( data ); + } + + + size_t DeckDoubleItem::size() const { + return m_data.size(); + } + +} + diff --git a/opm/parser/eclipse/Deck/DeckDoubleItem.hpp b/opm/parser/eclipse/Deck/DeckDoubleItem.hpp new file mode 100644 index 000000000..deaea3f00 --- /dev/null +++ b/opm/parser/eclipse/Deck/DeckDoubleItem.hpp @@ -0,0 +1,46 @@ +/* + 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 . + */ + +#ifndef DECKDOUBLEITEM_HPP +#define DECKDOUBLEITEM_HPP + +#include +#include +#include + +namespace Opm { + + class DeckDoubleItem : public DeckItem { + public: + double getDouble(size_t index) const; + + void push_back(std::vector data , size_t items); + void push_back(std::vector data); + void push_back(double value); + + size_t size() const; + private: + std::vector m_data; + }; + + typedef boost::shared_ptr DeckDoubleItemPtr; + typedef boost::shared_ptr DeckDoubleItemConstPtr; +} +#endif /* DECKDOUBLEITEM_HPP */ + diff --git a/opm/parser/eclipse/Deck/DeckIntItem.cpp b/opm/parser/eclipse/Deck/DeckIntItem.cpp index 6490ddee4..b53b10bcd 100644 --- a/opm/parser/eclipse/Deck/DeckIntItem.cpp +++ b/opm/parser/eclipse/Deck/DeckIntItem.cpp @@ -17,11 +17,13 @@ along with OPM. If not, see . */ -#include "DeckIntItem.hpp" #include + +#include + namespace Opm { - int DeckIntItem::getInt(unsigned int index) const { + int DeckIntItem::getInt(size_t index) const { if (index < m_data.size()) { return m_data[index]; } else diff --git a/opm/parser/eclipse/Deck/DeckIntItem.hpp b/opm/parser/eclipse/Deck/DeckIntItem.hpp index b714e8070..c9a403613 100644 --- a/opm/parser/eclipse/Deck/DeckIntItem.hpp +++ b/opm/parser/eclipse/Deck/DeckIntItem.hpp @@ -28,7 +28,7 @@ namespace Opm { class DeckIntItem : public DeckItem { public: - int getInt(unsigned int index) const; + int getInt(size_t index) const; void push_back(std::vector data , size_t items); void push_back(std::vector data); diff --git a/opm/parser/eclipse/Deck/DeckItem.hpp b/opm/parser/eclipse/Deck/DeckItem.hpp index 9cad2a69b..00df01b9a 100644 --- a/opm/parser/eclipse/Deck/DeckItem.hpp +++ b/opm/parser/eclipse/Deck/DeckItem.hpp @@ -18,7 +18,7 @@ */ #ifndef DECKITEM_HPP -#define DECKITEM_HPP +#define DECKITEM_HPP #include #include @@ -26,19 +26,19 @@ class DeckItem { public: - virtual int getInt(unsigned int index) const { + virtual int getInt(size_t index) const { throw std::logic_error("This implementation of DeckItem does not support int"); }; - virtual double getDouble(unsigned int index) const { + virtual double getDouble(size_t index) const { throw std::logic_error("This implementation of DeckItem does not support double"); }; - virtual bool getBool(unsigned int index) const { + virtual bool getBool(size_t index) const { throw std::logic_error("This implementation of DeckItem does not support bool"); } - virtual std::string getString(unsigned int index) const { + virtual std::string getString(size_t index) const { throw std::logic_error("This implementation of DeckItem does not support string"); } @@ -47,5 +47,5 @@ private: }; -#endif /* DECKITEM_HPP */ +#endif /* DECKITEM_HPP */ diff --git a/opm/parser/eclipse/Deck/tests/DeckItemTests.cpp b/opm/parser/eclipse/Deck/tests/DeckItemTests.cpp index 9c794d03a..0334afc03 100644 --- a/opm/parser/eclipse/Deck/tests/DeckItemTests.cpp +++ b/opm/parser/eclipse/Deck/tests/DeckItemTests.cpp @@ -23,6 +23,7 @@ #include #include #include +#include using namespace Opm; @@ -35,6 +36,8 @@ 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 pushThese; @@ -72,5 +75,55 @@ BOOST_AUTO_TEST_CASE(size_correct) { BOOST_CHECK_EQUAL( 3U , deckIntItem.size()); } +/*****************************************************************/ + +BOOST_AUTO_TEST_CASE(InitializeDouble) { + BOOST_REQUIRE_NO_THROW(DeckDoubleItem deckDoubleItem); +} + +BOOST_AUTO_TEST_CASE(GetDoubleAtIndex_NoData_ExceptionThrown) { + const DeckDoubleItem deckDoubleItem; + BOOST_CHECK_THROW(deckDoubleItem.getDouble(0), std::out_of_range); +} + + + +BOOST_AUTO_TEST_CASE(PushBackDouble_VectorPushed_ElementsCorrect) { + DeckDoubleItem deckDoubleItem; + std::vector pushThese; + pushThese.push_back(13); + pushThese.push_back(33); + deckDoubleItem.push_back(pushThese); + BOOST_CHECK_EQUAL(13, deckDoubleItem.getDouble(0)); + BOOST_CHECK_EQUAL(33, deckDoubleItem.getDouble(1)); +} + + +BOOST_AUTO_TEST_CASE(PushBackDouble_subVectorPushed_ElementsCorrect) { + DeckDoubleItem deckDoubleItem; + std::vector pushThese; + pushThese.push_back(13); + pushThese.push_back(33); + pushThese.push_back(47); + deckDoubleItem.push_back(pushThese , 2); + BOOST_CHECK_EQUAL(13 , deckDoubleItem.getDouble(0)); + BOOST_CHECK_EQUAL(33 , deckDoubleItem.getDouble(1)); + BOOST_CHECK_EQUAL( 2U , deckDoubleItem.size()); +} + + + +BOOST_AUTO_TEST_CASE(sizeDouble_correct) { + DeckDoubleItem deckDoubleItem; + + BOOST_CHECK_EQUAL( 0U , deckDoubleItem.size()); + deckDoubleItem.push_back( 100 ); + BOOST_CHECK_EQUAL( 1U , deckDoubleItem.size()); + + deckDoubleItem.push_back( 100 ); + deckDoubleItem.push_back( 100 ); + BOOST_CHECK_EQUAL( 3U , deckDoubleItem.size()); +} + diff --git a/opm/parser/eclipse/Parser/ParserItem.hpp b/opm/parser/eclipse/Parser/ParserItem.hpp index 66a8d2bd6..551869692 100644 --- a/opm/parser/eclipse/Parser/ParserItem.hpp +++ b/opm/parser/eclipse/Parser/ParserItem.hpp @@ -39,7 +39,7 @@ namespace Opm { ParserItemSizeEnum sizeType(); static int defaultInt(); - + protected: template void fillVectorFromStringToken(std::string token , std::vector& dataVector, T defaultValue , bool& defaultActive) {