Added class StarToken() to treat tokens with a multiplier/default
This commit is contained in:
parent
d1f0a9f5f7
commit
aae260c47d
122
opm/parser/eclipse/RawDeck/StarToken.hpp
Normal file
122
opm/parser/eclipse/RawDeck/StarToken.hpp
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
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 STAR_TOKEN_HPP
|
||||
#define STAR_TOKEN_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#define STAR '*'
|
||||
#define C_EOS '\0'
|
||||
|
||||
namespace Opm {
|
||||
|
||||
|
||||
template <class T>
|
||||
T readValueToken(const std::string& valueToken) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <>
|
||||
int readValueToken(const std::string& valueToken) {
|
||||
char * error_ptr;
|
||||
int value = strtol( valueToken.c_str() , &error_ptr , 10);
|
||||
if (error_ptr[0] != C_EOS)
|
||||
throw std::invalid_argument("Parsing integer value from: " + valueToken + " failed");
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
double readValueToken(const std::string& valueToken) {
|
||||
char * error_ptr;
|
||||
double value = strtod( valueToken.c_str() , &error_ptr);
|
||||
if (error_ptr[0] != C_EOS)
|
||||
throw std::invalid_argument("Parsing double value from: " + valueToken + " failed");
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
std::string readValueToken(const std::string& valueToken) {
|
||||
return valueToken;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <class T>
|
||||
class StarToken {
|
||||
|
||||
public:
|
||||
StarToken(const std::string& token) {
|
||||
size_t star_pos = token.find( STAR );
|
||||
|
||||
if (star_pos != std::string::npos) {
|
||||
|
||||
if (token[0] == STAR)
|
||||
m_multiplier = 1;
|
||||
else {
|
||||
char * error_ptr;
|
||||
m_multiplier = strtol( token.c_str() , &error_ptr , 10);
|
||||
|
||||
if (m_multiplier <= 0 || error_ptr[0] != STAR)
|
||||
throw std::invalid_argument("Parsing multiplier from " + token + " failed");
|
||||
}
|
||||
{
|
||||
const std::string& value_token = token.substr( star_pos + 1);
|
||||
if (value_token.size()) {
|
||||
m_value = readValueToken<T>( value_token );
|
||||
m_hasValue = true;
|
||||
} else {
|
||||
m_hasValue = false;
|
||||
}
|
||||
}
|
||||
} else
|
||||
throw std::invalid_argument("The input token: \'" + token + "\' does not contain a \'*\'.");
|
||||
}
|
||||
|
||||
size_t multiplier() const {
|
||||
return m_multiplier;
|
||||
}
|
||||
|
||||
|
||||
T value() const {
|
||||
if (!m_hasValue)
|
||||
throw std::invalid_argument("The input token did not specify a value ");
|
||||
return m_value;
|
||||
}
|
||||
|
||||
|
||||
bool hasValue() const {
|
||||
return m_hasValue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
ssize_t m_multiplier;
|
||||
T m_value;
|
||||
bool m_hasValue;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
108
opm/parser/eclipse/RawDeck/tests/StarTokenTests.cpp
Normal file
108
opm/parser/eclipse/RawDeck/tests/StarTokenTests.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
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 <stdexcept>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <opm/parser/eclipse/RawDeck/StarToken.hpp>
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(NoStarThrows) {
|
||||
BOOST_REQUIRE_THROW(Opm::StarToken<int> st("Hei...") , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(InvalidMultiplierThrow) {
|
||||
BOOST_REQUIRE_THROW( Opm::StarToken<int> st("X*") , std::invalid_argument);
|
||||
BOOST_REQUIRE_THROW( Opm::StarToken<int> st("1.25*") , std::invalid_argument);
|
||||
BOOST_REQUIRE_THROW( Opm::StarToken<int> st("-3*") , std::invalid_argument);
|
||||
BOOST_REQUIRE_THROW( Opm::StarToken<int> st("0*") , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(MultiplierCOrrect) {
|
||||
Opm::StarToken<int> st1("*");
|
||||
Opm::StarToken<int> st2("5*");
|
||||
Opm::StarToken<int> st3("54*");
|
||||
BOOST_REQUIRE_EQUAL(1U , st1.multiplier());
|
||||
BOOST_REQUIRE_EQUAL(5U , st2.multiplier());
|
||||
BOOST_REQUIRE_EQUAL(54U , st3.multiplier());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(NoValueGetValueThrow) {
|
||||
Opm::StarToken<int> st1("*");
|
||||
Opm::StarToken<int> st2("5*");
|
||||
BOOST_CHECK_THROW( st1.value() , std::invalid_argument );
|
||||
BOOST_CHECK_THROW( st2.value() , std::invalid_argument );
|
||||
BOOST_CHECK_EQUAL( false , st1.hasValue());
|
||||
BOOST_CHECK_EQUAL( false , st2.hasValue());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(IntMalformedValueThrows) {
|
||||
BOOST_CHECK_THROW( Opm::StarToken<int> st1("1*10X") , std::invalid_argument);
|
||||
BOOST_CHECK_THROW( Opm::StarToken<int> st1("1*X") , std::invalid_argument);
|
||||
BOOST_CHECK_THROW( Opm::StarToken<int> st1("1*10.25") , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DoubleMalformedValueThrows) {
|
||||
BOOST_CHECK_THROW( Opm::StarToken<double> st1("1*10X") , std::invalid_argument);
|
||||
BOOST_CHECK_THROW( Opm::StarToken<double> st1("1*X") , std::invalid_argument);
|
||||
BOOST_CHECK_THROW( Opm::StarToken<double> st1("1*10.25F") , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(CorrectIntValue) {
|
||||
Opm::StarToken<int> st1("1*10");
|
||||
Opm::StarToken<int> st2("5*20");
|
||||
BOOST_CHECK_EQUAL( true , st1.hasValue());
|
||||
BOOST_CHECK_EQUAL( true , st2.hasValue());
|
||||
|
||||
BOOST_CHECK_EQUAL( 10 , st1.value());
|
||||
BOOST_CHECK_EQUAL( 20 , st2.value());
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(CorrectDoubleValue) {
|
||||
Opm::StarToken<double> st1("1*10.09");
|
||||
Opm::StarToken<double> st2("5*20.13");
|
||||
BOOST_CHECK_EQUAL( true , st1.hasValue());
|
||||
BOOST_CHECK_EQUAL( true , st2.hasValue());
|
||||
|
||||
BOOST_CHECK_EQUAL( 10.09 , st1.value());
|
||||
BOOST_CHECK_EQUAL( 20.13 , st2.value());
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(CorrectStringValue) {
|
||||
Opm::StarToken<std::string> st1("1*10.09");
|
||||
Opm::StarToken<std::string> st2("5*20.13");
|
||||
BOOST_CHECK_EQUAL( true , st1.hasValue());
|
||||
BOOST_CHECK_EQUAL( true , st2.hasValue());
|
||||
|
||||
BOOST_CHECK_EQUAL( "10.09" , st1.value());
|
||||
BOOST_CHECK_EQUAL( "20.13" , st2.value());
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user