added further testcases to testmathlib::convert(); now the conversion of +-[hexnumber|octalnumber] works correctly

This commit is contained in:
Martin Ettl 2009-12-27 15:18:18 +01:00
parent f06adb18cd
commit c3886a5803
2 changed files with 38 additions and 5 deletions

View File

@ -32,11 +32,15 @@
long MathLib::toLongNumber(const std::string &str)
{
if (strncmp(str.c_str(), "0x", 2) == 0)
if (strncmp(str.c_str(), "0x" , 2) == 0
||strncmp(str.c_str(), "+0x", 3) == 0
||strncmp(str.c_str(), "-0x", 3) == 0)
{
return std::strtoul(str.c_str(), '\0', 16);
}
if (strncmp(str.c_str(), "0", 1) == 0)
if (strncmp(str.c_str(), "0" , 1) == 0
|| strncmp(str.c_str(), "+0", 2) == 0
|| strncmp(str.c_str(), "-0", 2) == 0)
{
return std::strtoul(str.c_str(), '\0', 8);
}

View File

@ -81,9 +81,38 @@ private:
void convert()
{
ASSERT_EQUALS(10, MathLib::toLongNumber("0xa"));
ASSERT_EQUALS(8, MathLib::toLongNumber("010"));
ASSERT_EQUALS(10, MathLib::toLongNumber("10"));
// ------------------
// tolong conversion:
// ------------------
// from hex
ASSERT_EQUALS(10 , MathLib::toLongNumber("0xa"));
ASSERT_EQUALS(10995 , MathLib::toLongNumber("0x2AF3"));
ASSERT_EQUALS(-10 , MathLib::toLongNumber("-0xa"));
ASSERT_EQUALS(-10995, MathLib::toLongNumber("-0x2AF3"));
ASSERT_EQUALS(10 , MathLib::toLongNumber("+0xa"));
ASSERT_EQUALS(10995 , MathLib::toLongNumber("+0x2AF3"));
// from octal
ASSERT_EQUALS(8 , MathLib::toLongNumber("010"));
ASSERT_EQUALS(8 , MathLib::toLongNumber("+010"));
ASSERT_EQUALS(-8 , MathLib::toLongNumber("-010"));
ASSERT_EQUALS(125 , MathLib::toLongNumber("0175"));
ASSERT_EQUALS(125 , MathLib::toLongNumber("+0175"));
ASSERT_EQUALS(-125 , MathLib::toLongNumber("-0175"));
// from base 10
ASSERT_EQUALS(10 , MathLib::toLongNumber("10"));
ASSERT_EQUALS(10 , MathLib::toLongNumber("10."));
ASSERT_EQUALS(10 , MathLib::toLongNumber("10.0"));
ASSERT_EQUALS(100 , MathLib::toLongNumber("10E+1"));
ASSERT_EQUALS(1 , MathLib::toLongNumber("10E-1"));
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10E+1"));
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10E-1"));
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10.E+1"));
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10.E-1"));
ASSERT_EQUALS(100 , MathLib::toLongNumber("+10.0E+1"));
ASSERT_EQUALS(-1 , MathLib::toLongNumber("-10.0E-1"));
}
void isint()