//This program is designed to test the implementation of the //the class Rational. //File name = test_rational.cxx //Date last changed = write date here #include #include #include "rational.h" void new_line(istream& in_stream); //Discards all the input remaining on the current input line of in_stream. //Also discards the '\n' at the end of the line. //Precondition: in_stream has been connected to a file with a call to open. void wait(); int main() { cout << "This program test your implementation of the class Rational.\n" << "If the output says\n" << "expression1 == expression2\n" << "and expression1 and expression2 DO NOT look EXACTLY the same,\n" <<"then there is a problem with your implementation.\n" << "Even if your pass this test, there still may be problems with\n" << "your implementation. So, we will also look at your code.\n"; wait(); cout << "Testing Constructors:\n" << "There should be no output.\n"; Rational zero(0), one(1), two(2), three(3), one_half(1, 2), one_third(1, 3), minus_one_third(-1, 3), r1, r2; wait(); cout << "Testing get_double:\n" << "1/2 = " << one_half.get_double() << endl << "The answer should be 0.5\n"; wait(); cout << "Testing overloaded output operator:\n" << one_half << " == 1/2\n" << one_third << " == 1/3\n" << zero << " == 0/1\n" << one << " == 1/1\n" << two << " == 2/1\n" << three << " == 3/1\n"; wait(); cout << "Testing to see that your constructor makes reduced forms.\n"; Rational another_half(-2, -4); cout << another_half << " == 1/2\n"; wait(); cout << "Testing +, -, *, and /:\n"; cout << "2 + 2 = " << (two + two) << endl << "The answer should be 4/1.\n"; cout << "1/2 + 1/3 = " << (one_half + one_third) << endl << "The answer should be 5/6.\n"; cout << "1/2 - 1/3 = " << (one_half - one_third) << endl << "The answer should be 1/6.\n"; cout << "2/3 divided by 1/3 = " << ((2*one_third)/one_third) << endl << "The answer should be 2/1.\n"; cout << "-(1/3) = " << (-one_third) << endl << "The answer should be -1/3.\n"; cout << "-(-1/3) = " << (-minus_one_third) << endl << "The answer should be 1/3.\n"; wait(); cout << "Testing overloaded comparison operators < and <=:\n"; if ((one_third < one_half) && (!(one_half < one_third))) cout << "Test1 OK\n"; else cout << "Test1 is NOT OK; problem with <\n"; if ((one_third <= one_half) && (!(one_half <= one_third))) cout << "Test2 OK\n"; else cout << "Test2 is NOT OK; problem with <=\n"; if (one_third <= one_third) cout << "Test3 OK\n"; else cout << "Test3 is NOT OK; problem with <=\n"; wait(); cout << "Testing overloaded comparison operators > and >=:\n"; if ((one_half > one_third) && (!(one_third > one_half))) cout << "Test4 OK\n"; else cout << "Test4 is NOT OK; problem with >\n"; if ((one_half >= one_third) && (!(one_third >= one_half))) cout << "Test5 OK\n"; else cout << "Test5 is NOT OK; problem with >=\n"; if (one_third >= one_third) cout << "Test6 OK\n"; else cout << "Test6 is NOT OK; problem with >=\n"; wait(); cout << "Testing overloaded comparison operator ==:\n"; if ((2*one_half == 3*one_third) && (! (one_half == one_third))) cout << "Test7 OK\n"; else cout << "Test7 is NOT OK; problem with ==\n"; wait(); cout << "Testing overloaded input operator:\n" << "Enter\n" << " 4/-6 -4/-6\n" << "In other words: Enter a space followed\n" << "by 4/-6 followed by another\n" << "space followed by -4/-6\n" << " (note three minus signs)\n" << "and then press the return key.\n"; cin >> r1 >> r2; new_line(cin); cout << r1 << " == -2/3\n"; cout << r2 << " == 2/3\n"; cout << "End of tests.\n"; return 0; } //Uses iostream.h: void new_line(istream& in_stream) { char symbol; do { in_stream.get(symbol); } while (symbol != '\n'); } void wait() { cout << "Press return to continue to the next test.\n"; new_line(cin); }