diff --git a/src/CalcManager/CEngine/Rational.cpp b/src/CalcManager/CEngine/Rational.cpp index 9ca5ccb8..ea330ce1 100644 --- a/src/CalcManager/CEngine/Rational.cpp +++ b/src/CalcManager/CEngine/Rational.cpp @@ -183,10 +183,10 @@ namespace CalcEngine } /// - /// 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. /// /// - /// This function has the same behavior than the standard C/C++ operator '%', to calculate the modulus after division instead, use instead. + /// This function has the same behavior as the standard C/C++ operator '%', to calculate the modulus after division instead, use instead. /// Rational& Rational::operator%=(Rational const& rhs) { @@ -349,10 +349,10 @@ namespace CalcEngine } /// - /// 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. /// /// - /// This function has the same behavior than the standard C/C++ operator '%', to calculate the modulus after division instead, use instead. + /// This function has the same behavior as the standard C/C++ operator '%', to calculate the modulus after division instead, use instead. /// Rational operator%(Rational lhs, Rational const& rhs) { diff --git a/src/CalculatorUnitTests/RationalTest.cpp b/src/CalculatorUnitTests/RationalTest.cpp index d7d20231..455571d8 100644 --- a/src/CalculatorUnitTests/RationalTest.cpp +++ b/src/CalculatorUnitTests/RationalTest.cpp @@ -21,59 +21,69 @@ namespace CalculatorEngineTests } TEST_METHOD(ModuloTest) { + // Verify results but also check that operands are not modified Rational rat25(25); Rational ratminus25(-25); Rational rat4(4); Rational ratminus4(-4); 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); - 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); - 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); - 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)); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"426"); + VERIFY_ARE_EQUAL(res, 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)); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"8113"); + VERIFY_ARE_EQUAL(res, 8113); 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)); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); + VERIFY_ARE_EQUAL(res, -643); 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)); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"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"); + VERIFY_ARE_EQUAL(res, 0); - //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)); - 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)); - 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)); - 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)); - 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)); - 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)); - 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)); 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)); @@ -86,41 +96,50 @@ namespace CalculatorEngineTests TEST_METHOD(RemainderTest) { + //Verify results but also check that operands are not modified Rational rat25(25); Rational ratminus25(-25); Rational rat4(4); Rational ratminus4(-4); 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; - 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; - 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; - 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); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"426"); + VERIFY_ARE_EQUAL(res, 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); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); + VERIFY_ARE_EQUAL(res, -643); 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); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"-643"); - + VERIFY_ARE_EQUAL(res, -643); 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); - 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); - VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 128), L"0"); - + VERIFY_ARE_EQUAL(res, 0); 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 }) { try @@ -160,17 +179,17 @@ namespace CalculatorEngineTests //Test with rational numbers 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); - 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); - 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); - 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); - 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); - 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); VERIFY_ARE_EQUAL(res.ToString(10, FMT_FLOAT, 8), L"0.33333333"); res = Rational(Number(1, 0, { 1000 }), Number(1, 0, { 3 })) % Rational(-10);