added a helper function to mathlib::isNullValue(), it checks wheter a string has a null representation or not and returns a boolian result; extended nullchecks in testmathlib.cpp

This commit is contained in:
Martin Ettl 2010-04-02 22:41:54 +02:00
parent 0f284b477d
commit 622663b8db
3 changed files with 23 additions and 3 deletions

View File

@ -56,8 +56,7 @@ double MathLib::toDoubleNumber(const std::string &str)
return std::strtoul(str.c_str(), '\0', 16); return std::strtoul(str.c_str(), '\0', 16);
} }
// nullcheck // nullcheck
else if (str == "-0" || str == "-0.0" || str == "-0." else if (isNullValue(str))
|| str == "+0" || str == "+0.0" || str == "+0.")
return 0.0; return 0.0;
// otherwise, convert to double // otherwise, convert to double
std::istringstream istr(str.c_str()); std::istringstream istr(str.c_str());
@ -298,6 +297,19 @@ bool MathLib::isGreater(const std::string &first, const std::string &second)
return toDoubleNumber(first) > toDoubleNumber(second); return toDoubleNumber(first) > toDoubleNumber(second);
} }
bool MathLib::isNullValue(const std::string &str)
{
return (str == "-0" || str == "-0.0"
|| str == "-0." || str == "-0E-00"
|| str == "-0E+00" || str == "+0E+00"
|| str == "+0E-00" || str == "+0"
|| str == "+0.0" || str == "+0."
|| str == "0.0" || str == "-0e-00"
|| str == "+0e+00" || str == "-0e+00"
|| str == "+0e-00" || str == "-0e-00"
|| str == "-0E-0" || str == "+0E-00");
}
bool MathLib::isOctalDigit(char c) bool MathLib::isOctalDigit(char c)
{ {
if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7') if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')

View File

@ -51,7 +51,7 @@ public:
static std::string tan(const std::string & tok); static std::string tan(const std::string & tok);
static std::string abs(const std::string & tok); static std::string abs(const std::string & tok);
static bool isGreater(const std::string & first, const std::string & second); static bool isGreater(const std::string & first, const std::string & second);
static bool isNullValue(const std::string &tok);
/** /**
* Return true if given character is 0,1,2,3,4,5,6 or 7. * Return true if given character is 0,1,2,3,4,5,6 or 7.
* @param c The character to check * @param c The character to check

View File

@ -128,7 +128,15 @@ private:
ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("+1.0E+10")); ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("+1.0E+10"));
ASSERT_EQUALS(100.0 , MathLib::toDoubleNumber("1.0E+2")); ASSERT_EQUALS(100.0 , MathLib::toDoubleNumber("1.0E+2"));
ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("1.0E+10")); ASSERT_EQUALS(1e+10 , MathLib::toDoubleNumber("1.0E+10"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E+0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E-0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E+00"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0E-00"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0E+00"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("+0E-00"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0."));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("0.0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("+0")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("+0"));
ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0.")); ASSERT_EQUALS(0.0 , MathLib::toDoubleNumber("-0."));