From c3886a580320aaa00dc5b910c637dc75270bb63a Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Sun, 27 Dec 2009 15:18:18 +0100 Subject: [PATCH] added further testcases to testmathlib::convert(); now the conversion of +-[hexnumber|octalnumber] works correctly --- lib/mathlib.cpp | 8 ++++++-- test/testmathlib.cpp | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 248b24df6..fc182fca4 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -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); } diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 3466b4955..6413b122c 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -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()