#include #include #include #include #include #include "exact_number.h" using namespace std; string error_msg(string rand, ExactNumber& n, double ans) { ostringstream msg; msg << " " << rand << " failed with " << static_cast(n) << "... expecting " << ans; return msg.str(); } string error_msg(string rand, ExactNumber& n1, ExactNumber& n2, bool ans) { ostringstream msg; msg << " " << rand << " failed with " << ans << "... expecting " << !ans << " => " << n1 << " : " << n2 << endl; return msg.str(); } int main() { cout << "Testing ExactNumber..." << endl << endl; cout << "Manual check:"; cout << " operator<< should print an ExactNumber as \"(a/b)\"" << endl; ExactNumber e1; // default constructor cout << " manual check for (0/1) : " << e1 << endl; ExactNumber e2(3); // memberwise-copy constructor cout << " manual check for (3/1) : " << e2 << endl; ExactNumber e3(5, 2); // memberwise-copy constructor cout << " manual check for (5/2) : " << e3 << endl; ExactNumber e4(e3); // copy constructor cout << " manual check for (5/2) : " << e4 << endl; ExactNumber e5(10, 5); // simplification cout << " manual check for (2/1) : " << e5 << endl; cout << endl; cout << "Automatic check" << endl; cout.precision(numeric_limits::digits10); unsigned int num_tests = 0; unsigned int num_failed = 0; ExactNumber n1(2, 4), n2(5, 3), n3(5, 6), n4; // = num_tests++; n4 = n1; try { if (static_cast(n4) != 1.0/2.0) throw error_msg("operator=", n4, 1.0/2.0); } catch(string m) { cout << m << endl; num_failed++; } // +=, -=, *=, /= num_tests++; n4 += n2; try { if (static_cast(n4) != 13.0/6.0) throw error_msg("opeator=", n4, 13.0/6.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 -= n2; try { if (static_cast(n4) != 1.0/2.0) throw error_msg("operator-=", n4, 1.0/2.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 *= n2; try { if (static_cast(n4) != 5.0/6.0) throw error_msg("operator*=", n4, 5.0/6.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 /= n2; try { if (static_cast(n4) != 1.0/2.0) throw error_msg("operator/=", n4, 1.0/2.0); } catch(string m) { cout << m << endl; num_failed++; } // +, -, *, / num_tests++; n4 = n1 + n2; try { if (static_cast(n4) != 13.0/6.0) throw error_msg("operator+", n4, 13.0/6.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = n1 - n2; try { if (static_cast(n4) != -7.0/6.0) throw error_msg("operator-", n4, -7.0/6.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = n1 * n2; try { if (static_cast(n4) != 5.0/6.0) throw error_msg("operator*", n4, 5.0/6.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = n1 / n2; try { if (static_cast(n4) != 3.0/10.0) throw error_msg("operator/", n4, 3.0/10.0); } catch(string m) { cout << m << endl; num_failed++; } // ==, != num_tests++; n4 = n1 * n2; bool result = n4 == n3; try { if (result != true) throw error_msg("operator==", n4, n3, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n1 == n2; try { if(result != false) throw error_msg("operator==", n4, n1, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n1 != n2; try { if (result != true) throw error_msg("operator!=", n1, n2, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n4 != n3; try { if(result != false) throw error_msg("operator!=", n1, n2, result); } catch(string m) { cout << m << endl; num_failed++; } // <, >, <=, >= num_tests++; result = n1 < n2; try { if (result != true) throw error_msg("operator<", n1, n2, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n2 > n3; try { if (result != true) throw error_msg("operator>", n2, n3, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n1 <= n2; try { if (result != true) throw error_msg("operator<=", n1, n2, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n2 <= n2; try { if (result != true) throw error_msg("operator<=", n2, n2, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n2 >= n3; try { if (result != true) throw error_msg("operator>=", n2, n3, result); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; result = n2 >= n2; try { if (result != true) throw error_msg("operator>=", n2, n2, result); } catch(string m) { cout << m << endl; num_failed++; } // combination num_tests++; n4 = (n1 - n2) * n3 + n1; try { if (static_cast(n4) != ExactNumber(-17, 36)) throw error_msg("(n1 - n2) * n3 + n1", n4, -17.0/36.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = n1 * n1 / n1 / n1; try { if (static_cast(n4) != ExactNumber(1, 1)) throw error_msg("n1 * n1 / n1 / n1", n4, 1.0/1.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = n1 * n3 / n2 + n3 / n1 * n2; try { if (static_cast(n4) != ExactNumber(109, 36)) throw error_msg("n1 * n3 / n2 + n3 / n1 * n2", n4, 109.0/36.0); } catch(string m) { cout << m << endl; num_failed++; } num_tests++; n4 = (n1 + n2) / (n3 * (n1 - n2) / (n3 + n1 * n2)); try { if(static_cast(n4) != ExactNumber(-26, 7)) throw error_msg("(n1 + n2) / (n3 * (n1 - n2) / (n3 + n1 * n2))", n4, -26.0/7.0); } catch(string m) { cout << m << endl; num_failed++; } // operator* involving 0 ExactNumber n5(11, 3), n6(0, 5); num_tests++; n4 = n5 * n6; try { if (static_cast(n4) != ExactNumber(0, 1)) throw error_msg("operator* involving 0", n4, 0.0/1.0); } catch(string m) { cout << m << endl; num_failed++; } // operator/ involving 0 num_tests++; n4 = n5 / n6; try { if(static_cast(n4) != ExactNumber(1, 0)) throw error_msg("operator/ involving 0", n4, 0.0/1.0); } catch(string m) { cout << m << endl; num_failed++; } /* Do not modify the code below */ if (num_failed == 0) cout << "passed all " << num_tests << " default test cases" << endl; else cout << "failed " << num_failed << " default test cases out of " << num_tests << " default test cases" << endl; cout << endl; /********************************************************/ /* */ /* add your test cases here */ /* */ /********************************************************/ num_tests = 0; // Negative number test num_tests++; ExactNumber n9(1000000, 5000000); try { if (static_cast(n9) != 0.2) throw error_msg("large number simplification", n9, 1.0/5.0); } catch (string m) { cout << m << endl; num_failed++; } // Complex Arithmetic Test num_tests++; ExactNumber n10(1, 2), n11(2, 3), n12(3, 4); ExactNumber n13 = n10 + n11 * n12; try { if (static_cast(n13) != 1.0) throw error_msg("complex arithmetic test", n13, 1.0); } catch (string m) { cout << m << endl; num_failed++; } // Zero Comparison Test num_tests++; ExactNumber n14(0, 1); result = n14 == ExactNumber(0, 1); try { if (!result) throw "Zero comparision test failed"; } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n15(7, -3); ExactNumber n16(-7, 3); try { if (static_cast(n15) != -7.0 / 3.0 || static_cast(n16) != -7.0 / 3.0) throw error_msg("Negative and Positive Denominator Combination", n15, -7.0 / 3.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n17(1000000000, 2000000000); try { if (static_cast(n17) != 0.5) throw error_msg("large rational numbers", n17, 1.0 / 2.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n18(1, 2), n19(1, 3); ExactNumber n20 = n18 + n19; try { if (static_cast(n20) != 5.0 / 6.0) throw error_msg("basic addition", n20, 5.0 / 6.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n21(5, 6), n22(1, 2); ExactNumber n23 = n21 - n22; try { if (static_cast(n23) != 1.0 / 3.0) throw error_msg("basic subtraction", n23, 1.0 / 3.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n24(2, 3), n25(3, 4); ExactNumber n26 = n24 * n25; try { if (static_cast(n26) != 1.0 / 2.0) throw error_msg("basic multiplication", n26, 1.0 / 2.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n27(7, 8), n28(2, 3); ExactNumber n29 = n27 / n28; try { if (static_cast(n29) != 21.0 / 16.0) throw error_msg("basic division", n29, 21.0 / 16.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n30(-5, 6), n31(3, 4); ExactNumber n32 = n30 + n31; try { if (static_cast(n32) != -1.0 / 12.0) throw error_msg("negative number addition", n32, -1.0 / 12.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n33(-3, 5), n34(5, 6); ExactNumber n35 = n33 * n34; try { if (static_cast(n35) != -15.0 / 30.0) throw error_msg("negative number multiplication", n35, -15.0 / 30.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n36(1000000, 2000000), n37(1000000, 3000000); ExactNumber n38 = n36 * n37; try { if (static_cast(n38) != 1.0 / 6.0) throw error_msg("large number multiplication", n38, 1.0 / 6.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n39(1, 2), n40(2, 4); result = n39 == n40; try { if (!result) throw "test failed"; } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n41(3, 4), n42(2, 3); result = n41 != n42; try { if (!result) throw "foo"; } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n43(0, 1000000); try { if (static_cast(n43) != 0.0) throw error_msg("zero numerator", n43, 0.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n44(1, 1000000000); try { if (static_cast(n44) != 1.0 / 1000000000) throw error_msg("large denominator", n44, 1.0 / 1000000000); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n45(2, 4); try { if (static_cast(n45) != 0.5) throw error_msg("simplification test", n45, 0.5); } catch (string m) { cout << m << endl; num_failed++; } ExactNumber n46(1, 0); try { double x = static_cast(n46); throw "Works."; num_failed++; } catch (...) { num_tests++; } num_tests++; ExactNumber n47(5, 7); ExactNumber n48 = ExactNumber(1,1) / n47; try { if (static_cast(n48) != 7.0 / 5.0) throw error_msg("inversion test", n48, 7.0 / 5.0); } catch (string m) { cout << m << endl; num_failed++; } num_tests++; ExactNumber n49(7, 3); ExactNumber n50(22, 7); ExactNumber n51 = n49 + n50; try { if (static_cast(n51) != 115.0/21.0) throw error_msg("mixed fraction addition", n51, 115.0/21.0); } catch (string m) { cout << m << endl; num_failed++; } if (num_failed == 0) cout << "passed all " << num_tests << " custom test cases" << endl; else cout << "failed " << num_failed << " custom test cases out of " << num_tests << " custom test cases" << endl; cout << endl; return 0; }