diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index b4670cc1e..960096072 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -803,6 +803,9 @@ static std::string intsuffix(const std::string & first, const std::string & seco std::string MathLib::add(const std::string & first, const std::string & second) { +#ifdef TEST_MATHLIB_VALUE + return (value(first) + value(second)).str(); +#else if (MathLib::isInt(first) && MathLib::isInt(second)) { return toString(toLongNumber(first) + toLongNumber(second)) + intsuffix(first, second); } @@ -817,10 +820,14 @@ std::string MathLib::add(const std::string & first, const std::string & second) d1 *= 10.0; return toString(d1 + d2); +#endif } std::string MathLib::subtract(const std::string &first, const std::string &second) { +#ifdef TEST_MATHLIB_VALUE + return (value(first) - value(second)).str(); +#else if (MathLib::isInt(first) && MathLib::isInt(second)) { return toString(toLongNumber(first) - toLongNumber(second)) + intsuffix(first, second); } @@ -838,20 +845,31 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon d1 *= 10.0; return toString(d1 - d2); +#endif } std::string MathLib::incdec(const std::string & var, const std::string & op) { +#ifdef TEST_MATHLIB_VALUE + if (op == "++") + return value(var).add(1).str(); + else if (op == "--") + return value(var).add(-1).str(); +#else if (op == "++") return MathLib::add(var, "1"); else if (op == "--") return MathLib::subtract(var, "1"); +#endif throw InternalError(0, std::string("Unexpected operation '") + op + "' in MathLib::incdec(). Please report this to Cppcheck developers."); } std::string MathLib::divide(const std::string &first, const std::string &second) { +#ifdef TEST_MATHLIB_VALUE + return (value(first) / value(second)).str(); +#else if (MathLib::isInt(first) && MathLib::isInt(second)) { const bigint a = toLongNumber(first); const bigint b = toLongNumber(second); @@ -863,23 +881,29 @@ std::string MathLib::divide(const std::string &first, const std::string &second) } else if (isNullValue(second)) { if (isNullValue(first)) return "nan.0"; - const int sign_first = (isPositive(first)) ? 1 : -1; - const int sign_second = (isPositive(second)) ? 1 : -1; - return (sign_first*sign_second == 1) ? "inf.0" : "-inf.0"; + return isPositive(first) ? "inf.0" : "-inf.0"; } return toString(toDoubleNumber(first) / toDoubleNumber(second)); +#endif } std::string MathLib::multiply(const std::string &first, const std::string &second) { +#ifdef TEST_MATHLIB_VALUE + return (value(first) * value(second)).str(); +#else if (MathLib::isInt(first) && MathLib::isInt(second)) { return toString(toLongNumber(first) * toLongNumber(second)) + intsuffix(first, second); } return toString(toDoubleNumber(first) * toDoubleNumber(second)); +#endif } std::string MathLib::mod(const std::string &first, const std::string &second) { +#ifdef TEST_MATHLIB_VALUE + return (value(first) % value(second)).str(); +#else if (MathLib::isInt(first) && MathLib::isInt(second)) { const bigint b = toLongNumber(second); if (b == 0) @@ -887,6 +911,7 @@ std::string MathLib::mod(const std::string &first, const std::string &second) return toString(toLongNumber(first) % b) + intsuffix(first, second); } return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second))); +#endif } std::string MathLib::calculate(const std::string &first, const std::string &second, char action) diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 7625a0de4..9a4a01aeb 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -148,14 +148,19 @@ private: void calculate1() const { ASSERT_EQUALS("0" , MathLib::calculate("2" , "1" , '%')); - ASSERT_EQUALS("0.0" , MathLib::calculate("2.0" , "1.0" , '%')); ASSERT_EQUALS("2" , MathLib::calculate("12" , "5" , '%')); ASSERT_EQUALS("1" , MathLib::calculate("100" , "3" , '%')); + +#ifndef TEST_MATHLIB_VALUE + // floating point modulo is not defined in C/C++ + ASSERT_EQUALS("0.0" , MathLib::calculate("2.0" , "1.0" , '%')); ASSERT_EQUALS("12.0" , MathLib::calculate("12.0" , "13.0" , '%')); ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%')); ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%')); - ASSERT_THROW(MathLib::calculate("123", "0", '%'), InternalError); // throw MathLib::calculate("123", "0.0", '%'); // don't throw +#endif + + ASSERT_THROW(MathLib::calculate("123", "0", '%'), InternalError); // throw ASSERT_EQUALS("0" , MathLib::calculate("1" , "1" , '^')); ASSERT_EQUALS("3" , MathLib::calculate("2" , "1" , '^')); @@ -815,7 +820,7 @@ private: ASSERT_EQUALS("inf.0", MathLib::divide("3.0", "0.f")); // inf (#5875) ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0")); // -inf (#5142) ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "0.0f")); // -inf (#5142) - ASSERT_EQUALS("inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142) + ASSERT_EQUALS("-inf.0", MathLib::divide("-3.0", "-0.0f")); // inf (#5142) } void isdec(void) const {