take feedback into account

This commit is contained in:
Rudy Huyn 2019-04-04 19:53:41 -07:00
commit 38f3e6281d
2 changed files with 75 additions and 56 deletions

View file

@ -183,10 +183,10 @@ namespace CalcEngine
} }
/// <summary> /// <summary>
/// Calculate the remainder after division, the sign of the result will match the sign of the current object. /// Calculate the remainder after division, the sign of a result will match the sign of the current object.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This function has the same behavior than the standard C/C++ operator '%', to calculate the modulus after division instead, use <see cref="Rational::operator%"/> instead. /// This function has the same behavior as the standard C/C++ operator '%', to calculate the modulus after division instead, use <see cref="Rational::operator%"/> instead.
/// </remarks> /// </remarks>
Rational& Rational::operator%=(Rational const& rhs) Rational& Rational::operator%=(Rational const& rhs)
{ {
@ -349,10 +349,10 @@ namespace CalcEngine
} }
/// <summary> /// <summary>
/// Calculate the remainder after division, the sign of the result will match the sign of a. /// Calculate the remainder after division, the sign of a result will match the sign of lhs.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This function has the same behavior than the standard C/C++ operator '%', to calculate the modulus after division instead, use <see cref="Rational::operator%"/> instead. /// This function has the same behavior as the standard C/C++ operator '%', to calculate the modulus after division instead, use <see cref="Rational::operator%"/> instead.
/// </remarks> /// </remarks>
Rational operator%(Rational lhs, Rational const& rhs) Rational operator%(Rational lhs, Rational const& rhs)
{ {

View file

@ -21,59 +21,69 @@ namespace CalculatorEngineTests
} }
TEST_METHOD(ModuloTest) TEST_METHOD(ModuloTest)
{ {
// Verify results but also check that operands are not modified
Rational rat25(25); Rational rat25(25);
Rational ratminus25(-25); Rational ratminus25(-25);
Rational rat4(4); Rational rat4(4);
Rational ratminus4(-4); Rational ratminus4(-4);
Rational res = Mod(rat25, rat4); Rational res = Mod(rat25, rat4);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"1"); VERIFY_ARE_EQUAL(res, 1);
VERIFY_ARE_EQUAL(rat25, 25);
VERIFY_ARE_EQUAL(rat4, 4);
res = Mod(rat25, ratminus4); res = Mod(rat25, ratminus4);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-3"); VERIFY_ARE_EQUAL(res, -3);
VERIFY_ARE_EQUAL(rat25, 25);
VERIFY_ARE_EQUAL(ratminus4, -4);
res = Mod(ratminus25, ratminus4); res = Mod(ratminus25, ratminus4);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-1"); VERIFY_ARE_EQUAL(res, -1);
VERIFY_ARE_EQUAL(ratminus25, -25);
VERIFY_ARE_EQUAL(ratminus4, -4);
res = Mod(ratminus25, rat4); res = Mod(ratminus25, rat4);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"3"); VERIFY_ARE_EQUAL(res, 3);
VERIFY_ARE_EQUAL(ratminus25, -25);
VERIFY_ARE_EQUAL(rat4, 4);
// Check with integers
res = Mod(Rational(426), Rational(56478)); res = Mod(Rational(426), Rational(56478));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"426"); VERIFY_ARE_EQUAL(res, 426);
res = Mod(Rational(56478), Rational(426)); res = Mod(Rational(56478), Rational(426));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"246"); VERIFY_ARE_EQUAL(res, 246);
res = Mod(Rational(-643), Rational(8756)); res = Mod(Rational(-643), Rational(8756));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"8113"); VERIFY_ARE_EQUAL(res, 8113);
res = Mod(Rational(643), Rational(-8756)); res = Mod(Rational(643), Rational(-8756));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-8113"); VERIFY_ARE_EQUAL(res, -8113);
res = Mod(Rational(-643), Rational(-8756)); res = Mod(Rational(-643), Rational(-8756));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); VERIFY_ARE_EQUAL(res, -643);
res = Mod(Rational(1000), Rational(250)); res = Mod(Rational(1000), Rational(250));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
res = Mod(Rational(1000), Rational(-250)); res = Mod(Rational(1000), Rational(-250));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
//Test with Zero
res = Mod(Rational(343654332), Rational(0));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"343654332");
res = Mod(Rational(0), Rational(8756));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0");
res = Mod(Rational(0), Rational(-242));
auto dfd = res.ToString(10, FMT_FLOAT, 128);
VERIFY_ARE_EQUAL(dfd, L"0");
res = Mod(Rational(0), Rational(0));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0");
res = Mod(Rational(Number(1, 0, { 23242 }), Number(1, 0, { 2 })), Rational(Number(1, 0, { 0 }), Number(1, 0, { 23 })));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"11621");
//Test with rational numbers // Test with Zero
res = Mod(Rational(343654332), Rational(0));
VERIFY_ARE_EQUAL(res, 343654332);
res = Mod(Rational(0), Rational(8756));
VERIFY_ARE_EQUAL(res, 0);
res = Mod(Rational(0), Rational(-242));
VERIFY_ARE_EQUAL(res, 0);
res = Mod(Rational(0), Rational(0));
VERIFY_ARE_EQUAL(res, 0);
res = Mod(Rational(Number(1, 0, { 23242 }), Number(1, 0, { 2 })), Rational(Number(1, 0, { 0 }), Number(1, 0, { 23 })));
VERIFY_ARE_EQUAL(res, 11621);
// Test with rational numbers
res = Mod(Rational(Number(1, 0, { 250 }), Number(1, 0, { 100 })), Rational(89)); res = Mod(Rational(Number(1, 0, { 250 }), Number(1, 0, { 100 })), Rational(89));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"2.5");
res = Mod(Rational(Number(1, 0, { 3330 }), Number(1, 0, { 1332 })), Rational(1)); res = Mod(Rational(Number(1, 0, { 3330 }), Number(1, 0, { 1332 })), Rational(1));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.5");
res = Mod(Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(10)); res = Mod(Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(10));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"2.5");
res = Mod(Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(10)); res = Mod(Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(10));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"7.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"7.5");
res = Mod(Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(-10)); res = Mod(Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(-10));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"-2.5");
res = Mod(Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(-10)); res = Mod(Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })), Rational(-10));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-7.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"-7.5");
res = Mod(Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })), Rational(1)); res = Mod(Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })), Rational(1));
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.33333333"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.33333333");
res = Mod(Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })), Rational(-10)); res = Mod(Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })), Rational(-10));
@ -86,41 +96,50 @@ namespace CalculatorEngineTests
TEST_METHOD(RemainderTest) TEST_METHOD(RemainderTest)
{ {
//Verify results but also check that operands are not modified
Rational rat25(25); Rational rat25(25);
Rational ratminus25(-25); Rational ratminus25(-25);
Rational rat4(4); Rational rat4(4);
Rational ratminus4(-4); Rational ratminus4(-4);
Rational res = rat25 % rat4; Rational res = rat25 % rat4;
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"1"); VERIFY_ARE_EQUAL(res, 1);
VERIFY_ARE_EQUAL(rat25, 25);
VERIFY_ARE_EQUAL(rat4, 4);
res = rat25 % ratminus4; res = rat25 % ratminus4;
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"1"); VERIFY_ARE_EQUAL(res, 1);
VERIFY_ARE_EQUAL(rat25, 25);
VERIFY_ARE_EQUAL(ratminus4, -4);
res = ratminus25 % ratminus4; res = ratminus25 % ratminus4;
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-1"); VERIFY_ARE_EQUAL(res, -1);
VERIFY_ARE_EQUAL(ratminus25, -25);
VERIFY_ARE_EQUAL(ratminus4, -4);
res = ratminus25 % rat4; res = ratminus25 % rat4;
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-1"); VERIFY_ARE_EQUAL(res, -1);
VERIFY_ARE_EQUAL(ratminus25, -25);
VERIFY_ARE_EQUAL(rat4, 4);
// Check with integers
res = Rational(426) % Rational(56478); res = Rational(426) % Rational(56478);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"426"); VERIFY_ARE_EQUAL(res, 426);
res = Rational(56478) % Rational(426); res = Rational(56478) % Rational(426);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"246"); VERIFY_ARE_EQUAL(res, 246);
res = Rational(-643) % Rational(8756); res = Rational(-643) % Rational(8756);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); VERIFY_ARE_EQUAL(res, -643);
res = Rational(643) % Rational(-8756); res = Rational(643) % Rational(-8756);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"643"); VERIFY_ARE_EQUAL(res, 643);
res = Rational(-643) % Rational(-8756); res = Rational(-643) % Rational(-8756);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); VERIFY_ARE_EQUAL(res, -643);
res = Rational(-124) % Rational(-124); res = Rational(-124) % Rational(-124);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
res = Rational(24) % Rational(24); res = Rational(24) % Rational(24);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
//Test with Zero // Test with Zero
res = Rational(0) % Rational(3654); res = Rational(0) % Rational(3654);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
res = Rational(0) % Rational(-242); res = Rational(0) % Rational(-242);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); VERIFY_ARE_EQUAL(res, 0);
for (auto number : { 343654332, 0, -23423 }) for (auto number : { 343654332, 0, -23423 })
{ {
try try
@ -160,17 +179,17 @@ namespace CalculatorEngineTests
//Test with rational numbers //Test with rational numbers
res = Rational(Number(1, 0, { 250 }), Number(1, 0, { 100 })) % Rational(89); res = Rational(Number(1, 0, { 250 }), Number(1, 0, { 100 })) % Rational(89);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"2.5");
res = Rational(Number(1, 0, { 3330 }), Number(1, 0, { 1332 })) % Rational(1); res = Rational(Number(1, 0, { 3330 }), Number(1, 0, { 1332 })) % Rational(1);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.5");
res = Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(10); res = Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(10);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"2.5");
res = Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(10); res = Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(10);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"-2.5");
res = Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(-10); res = Rational(Number(-1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(-10);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"-2.5");
res = Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(-10); res = Rational(Number(1, 0, { 12250 }), Number(1, 0, { 100 })) % Rational(-10);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"2.5"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"2.5");
res = Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })) % Rational(1); res = Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })) % Rational(1);
VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.33333333"); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.33333333");
res = Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })) % Rational(-10); res = Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })) % Rational(-10);