Added basic ParserRecordItem class

This commit is contained in:
Joakim Hove 2013-05-03 17:06:53 +02:00
parent 558a705b52
commit be7e6e0cad
5 changed files with 402 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include <opm/parser/eclipse/Parser/ParserEnums.hpp>
#include <opm/parser/eclipse/Parser/ItemSize.hpp>
namespace Opm {
ItemSize::ItemSize() {
m_sizeType = UNSPECIFIED;
m_sizeValue = 0;
}
ItemSize::ItemSize(int sizeValue) {
m_sizeType = ITEM_FIXED;
m_sizeValue = sizeValue;
}
ItemSize::ItemSize(ItemSizeEnum sizeType) {
m_sizeType = sizeType;
m_sizeValue = 0;
}
ItemSize::ItemSize(ItemSizeEnum sizeType, size_t sizeValue) {
m_sizeType = sizeType;
m_sizeValue = sizeValue;
}
ItemSizeEnum ItemSize::sizeType() const{
return m_sizeType;
}
size_t ItemSize::sizeValue() const{
if (m_sizeType == ITEM_FIXED)
return m_sizeValue;
else
throw std::invalid_argument("Can not ask for actual size when type != ITEM_FIXED");
}
}

View File

@ -0,0 +1,48 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef RECORD_ITEM_SIZE_H
#define RECORD_ITEM_SIZE_H
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Parser/ParserEnums.hpp>
namespace Opm {
class ItemSize {
private:
ItemSizeEnum m_sizeType;
size_t m_sizeValue;
public:
ItemSize();
ItemSize(int sizeValue);
ItemSize(ItemSizeEnum sizeType);
ItemSize(ItemSizeEnum sizeType, size_t sizeValue);
ItemSizeEnum sizeType() const;
size_t sizeValue() const;
};
typedef boost::shared_ptr<ItemSize> ItemSizePtr;
typedef boost::shared_ptr<const ItemSize> ItemSizeConstPtr;
}
#endif

View File

@ -0,0 +1,87 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARSER_RECORD_ITEM_H
#define PARSER_RECORD_ITEM_H
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Parser/ItemSize.hpp>
namespace Opm {
template<class T>
class ParserRecordItem {
private:
std::string m_name;
ItemSizeConstPtr m_itemSize;
public:
ParserRecordItem<T>(const std::string& itemName , ItemSizeConstPtr itemSize) {
m_name.assign( itemName );
m_itemSize = itemSize;
}
bool scanItem(const std::string& itemString , T& value) {
std::istringstream inputStream( itemString );
T newValue;
inputStream >> newValue;
if (inputStream.fail())
return false;
else {
char c;
inputStream >> c;
if (inputStream.eof() || c == ' ') {
value = newValue;
return true;
}
else
return false;
}
};
int scanItems(const std::string& itemString , size_t items , std::vector<T>& values) {
std::istringstream inputStream( itemString );
unsigned int itemsRead = 0;
while (inputStream.good() && itemsRead < items) {
T value;
inputStream >> value;
values.push_back( value );
itemsRead++;
}
return itemsRead;
}
int scanItems(const std::string& itemString , std::vector<T>& values) {
return scanItems( itemString , m_itemSize->sizeValue() , values);
}
};
}
#endif

View File

@ -0,0 +1,71 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE ParserTests
#include <boost/test/unit_test.hpp>
#include "opm/parser/eclipse/Parser/ItemSize.hpp"
#include "opm/parser/eclipse/Parser/ParserEnums.hpp"
using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize) {
BOOST_REQUIRE_NO_THROW( ItemSize itemSize );
}
BOOST_AUTO_TEST_CASE(Default) {
ItemSize itemSize;
BOOST_REQUIRE_EQUAL( itemSize.sizeType() , UNSPECIFIED );
BOOST_REQUIRE_THROW( itemSize.sizeValue() , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(Fixed) {
ItemSize itemSize(100);
BOOST_REQUIRE_EQUAL( itemSize.sizeType() , ITEM_FIXED );
BOOST_REQUIRE_EQUAL( itemSize.sizeValue() , 100U );
}
BOOST_AUTO_TEST_CASE(Fixed2) {
ItemSize itemSize(ITEM_FIXED , 100U);
BOOST_REQUIRE_EQUAL( itemSize.sizeType() , ITEM_FIXED );
BOOST_REQUIRE_EQUAL( itemSize.sizeValue() , 100U );
}
BOOST_AUTO_TEST_CASE(Box) {
ItemSize itemSize(ITEM_BOX);
BOOST_REQUIRE_EQUAL( itemSize.sizeType() , ITEM_BOX );
BOOST_REQUIRE_THROW( itemSize.sizeValue() , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(Boost) {
ItemSizeConstPtr itemSize( new ItemSize( ITEM_BOX ));
BOOST_REQUIRE_EQUAL( itemSize->sizeType() , ITEM_BOX );
BOOST_REQUIRE_THROW( itemSize->sizeValue() , std::invalid_argument );
}

View File

@ -0,0 +1,132 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE ParserTests
#include <boost/test/unit_test.hpp>
#include "opm/parser/eclipse/Parser/ParserRecordItem.hpp"
#include <opm/parser/eclipse/Parser/ItemSize.hpp>
using namespace Opm;
BOOST_AUTO_TEST_CASE(Initialize) {
ItemSizeConstPtr itemSize( new ItemSize(10));
BOOST_REQUIRE_NO_THROW( ParserRecordItem<int> item1("ITEM1" , itemSize));
BOOST_REQUIRE_NO_THROW( ParserRecordItem<std::string> item1("ITEM1" , itemSize));
BOOST_REQUIRE_NO_THROW( ParserRecordItem<bool> item1("ITEM1" , itemSize));
BOOST_REQUIRE_NO_THROW( ParserRecordItem<double> item1("ITEM1" , itemSize));
}
BOOST_AUTO_TEST_CASE(TestScanInt) {
ItemSizeConstPtr itemSize( new ItemSize(10));
ParserRecordItem<int> itemInt("ITEM2" , itemSize);
int value = 78;
BOOST_REQUIRE( itemInt.scanItem("100" , value));
BOOST_REQUIRE_EQUAL( value , 100 );
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) {
ItemSizeConstPtr itemSize( new ItemSize(10));
ParserRecordItem<double> itemDouble("ITEM2" , itemSize);
double value = 78;
BOOST_REQUIRE( itemDouble.scanItem("100.25" , value));
BOOST_REQUIRE_EQUAL( value , 100.25 );
BOOST_REQUIRE( !itemDouble.scanItem("200X" , value));
BOOST_REQUIRE_EQUAL( value , 100.25 );
BOOST_REQUIRE( !itemDouble.scanItem("" , value));
BOOST_REQUIRE_EQUAL( value , 100.25 );
}
BOOST_AUTO_TEST_CASE(TestScanString) {
ItemSizeConstPtr itemSize( new ItemSize(10));
ParserRecordItem<std::string> itemString("ITEM2" , itemSize);
std::string value = "Hei";
BOOST_REQUIRE( itemString.scanItem("100.25" , value));
BOOST_REQUIRE_EQUAL( value , "100.25" );
BOOST_REQUIRE( !itemString.scanItem("" , value));
BOOST_REQUIRE_EQUAL( value , "100.25" );
}
BOOST_AUTO_TEST_CASE(TestScanBool) {
ItemSizeConstPtr itemSize( new ItemSize(10));
ParserRecordItem<bool> itemString("ITEM2" , itemSize);
bool value = true;
BOOST_REQUIRE( itemString.scanItem("1" , value));
BOOST_REQUIRE_EQUAL( value , true );
BOOST_REQUIRE( itemString.scanItem("0" , value));
BOOST_REQUIRE_EQUAL( value , false );
BOOST_REQUIRE( !itemString.scanItem("" , value));
BOOST_REQUIRE_EQUAL( value , false );
BOOST_REQUIRE( !itemString.scanItem("10" , value));
BOOST_REQUIRE_EQUAL( value , false );
}
/*****************************************************************/
BOOST_AUTO_TEST_CASE(TestScanItemsFail) {
ItemSizeConstPtr itemSize( new ItemSize( ITEM_BOX ) );
ParserRecordItem<int> itemInt("ITEM2" , itemSize);
std::vector<int> values;
BOOST_REQUIRE_THROW( itemInt.scanItems("100 100 100" , values) , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(TestScanItemsInt) {
ItemSizeConstPtr itemSize( new ItemSize( 4 ) );
ParserRecordItem<int> itemInt("ITEM2" , itemSize);
std::vector<int> values;
BOOST_REQUIRE_EQUAL( itemInt.scanItems("1 2 3 4" , values) , 4 );
BOOST_REQUIRE_EQUAL( values[0] , 1);
BOOST_REQUIRE_EQUAL( values[1] , 2);
BOOST_REQUIRE_EQUAL( values[2] , 3);
BOOST_REQUIRE_EQUAL( values[3] , 4);
}