Skip to content

Commit 7be01da

Browse files
committed
fixed ticket 3693. Added modulus operator to Mathlib + unittests. Added a test to checkother to ensure the testcase of ticket 3693 does not trigger an error message.
1 parent 8e3f170 commit 7be01da

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

lib/mathlib.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
225225
return toString<double>(toDoubleNumber(first) * toDoubleNumber(second));
226226
}
227227

228+
std::string MathLib::mod(const std::string &first, const std::string &second)
229+
{
230+
if (MathLib::isInt(first) && MathLib::isInt(second)) {
231+
return toString<MathLib::bigint>(toLongNumber(first) % toLongNumber(second));
232+
}
233+
return toString<double>(fmod(toDoubleNumber(first),toDoubleNumber(second)));
234+
}
235+
228236
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
229237
{
230238
switch (action) {
@@ -240,6 +248,9 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
240248
case '/':
241249
return MathLib::divide(first, second);
242250

251+
case '%':
252+
return MathLib::mod(first, second);
253+
243254
default:
244255
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
245256
}

lib/mathlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class MathLib {
5454
static std::string subtract(const std::string & first, const std::string & second);
5555
static std::string multiply(const std::string & first, const std::string & second);
5656
static std::string divide(const std::string & first, const std::string & second);
57+
static std::string mod(const std::string & first, const std::string & second);
5758
static std::string calculate(const std::string & first, const std::string & second, char action);
5859

5960
static std::string sin(const std::string & tok);

test/testmathlib.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestMathLib : public TestFixture {
3030

3131
void run() {
3232
TEST_CASE(calculate);
33+
TEST_CASE(calculate1);
3334
TEST_CASE(convert);
3435
TEST_CASE(isint);
3536
TEST_CASE(isnegative);
@@ -123,6 +124,16 @@ class TestMathLib : public TestFixture {
123124
ASSERT_THROW(MathLib::calculate("1","2",'j'),InternalError);
124125
}
125126

127+
void calculate1() { // mod
128+
ASSERT_EQUALS("0" , MathLib::calculate("2" , "1" , '%'));
129+
ASSERT_EQUALS("0" , MathLib::calculate("2.0" , "1.0" , '%'));
130+
ASSERT_EQUALS("2" , MathLib::calculate("12" , "5" , '%'));
131+
ASSERT_EQUALS("1" , MathLib::calculate("100" , "3" , '%'));
132+
ASSERT_EQUALS("12" , MathLib::calculate("12.0" , "13.0" , '%'));
133+
ASSERT_EQUALS("1.3" , MathLib::calculate("5.3" , "2.0" , '%'));
134+
ASSERT_EQUALS("1.7" , MathLib::calculate("18.5" , "4.2" , '%'));
135+
}
136+
126137
void convert() {
127138
// ------------------
128139
// tolong conversion:

test/testother.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class TestOther : public TestFixture {
108108
TEST_CASE(testMisusedScopeObjectDoesNotPickNestedClass);
109109
TEST_CASE(trac2071);
110110
TEST_CASE(trac2084);
111+
TEST_CASE(trac3693);
111112

112113
TEST_CASE(assignmentInAssert);
113114

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

2391+
void trac3693() {
2392+
check("struct A{\n"
2393+
" enum {\n"
2394+
" b = 300\n"
2395+
" };\n"
2396+
"};\n"
2397+
"const int DFLT_TIMEOUT = A::b % 1000000 ;\n");
2398+
ASSERT_EQUALS("", errout.str());
2399+
}
2400+
23902401
void testMisusedScopeObjectDoesNotPickFunction1() {
23912402
check("int main ( )\n"
23922403
"{\n"

0 commit comments

Comments
 (0)