Skip to content

Commit

Permalink
fixed ticket 3693. Added modulus operator to Mathlib + unittests. Add…
Browse files Browse the repository at this point in the history
…ed a test to checkother to ensure the testcase of ticket 3693 does not trigger an error message.
  • Loading branch information
orbitcowboy committed Apr 12, 2012
1 parent 8e3f170 commit 7be01da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/mathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
return toString<double>(toDoubleNumber(first) * toDoubleNumber(second));
}

std::string MathLib::mod(const std::string &first, const std::string &second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString<MathLib::bigint>(toLongNumber(first) % toLongNumber(second));
}
return toString<double>(fmod(toDoubleNumber(first),toDoubleNumber(second)));
}

std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
{
switch (action) {
Expand All @@ -240,6 +248,9 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
case '/':
return MathLib::divide(first, second);

case '%':
return MathLib::mod(first, second);

default:
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
}
Expand Down
1 change: 1 addition & 0 deletions lib/mathlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MathLib {
static std::string subtract(const std::string & first, const std::string & second);
static std::string multiply(const std::string & first, const std::string & second);
static std::string divide(const std::string & first, const std::string & second);
static std::string mod(const std::string & first, const std::string & second);
static std::string calculate(const std::string & first, const std::string & second, char action);

static std::string sin(const std::string & tok);
Expand Down
11 changes: 11 additions & 0 deletions test/testmathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestMathLib : public TestFixture {

void run() {
TEST_CASE(calculate);
TEST_CASE(calculate1);
TEST_CASE(convert);
TEST_CASE(isint);
TEST_CASE(isnegative);
Expand Down Expand Up @@ -123,6 +124,16 @@ class TestMathLib : public TestFixture {
ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError);
}

void calculate1() { // mod
ASSERT_EQUALS("0" , MathLib::calculate("2" , "1" , '%'));
ASSERT_EQUALS("0" , MathLib::calculate("2.0" , "1.0" , '%'));
ASSERT_EQUALS("2" , MathLib::calculate("12" , "5" , '%'));
ASSERT_EQUALS("1" , MathLib::calculate("100" , "3" , '%'));
ASSERT_EQUALS("12" , 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" , '%'));
}

void convert() {
// ------------------
// tolong conversion:
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class TestOther : public TestFixture {
TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass);
TEST_CASE(trac2071);
TEST_CASE(trac2084);
TEST_CASE(trac3693);

TEST_CASE(assignmentInAssert);

Expand Down Expand Up @@ -2387,6 +2388,16 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[trac1132.cpp:16]: (error) instance of \"Lock\" object destroyed immediately\n", errout.str());
}

void trac3693() {
check("struct A{\n"
" enum {\n"
" b = 300\n"
" };\n"
"};\n"
"const int DFLT_TIMEOUT = A::b % 1000000 ;\n");
ASSERT_EQUALS("", errout.str());
}

void testMisusedScopeObjectDoesNotPickFunction1() {
check("int main ( )\n"
"{\n"
Expand Down

0 comments on commit 7be01da

Please sign in to comment.