""" A Rational class, closely followed the example by John W. Shipman of New Mexico Tech http://infohost.nmt.edu/tcc/help/lang/python/examples/rational/rational.pdf Xiannong Meng CSCI 204 Fall 2017 """ #================================================================ # rationaltest: A test driver for the rational.py module. #---------------- from rational import * def main(): """ Test the Rational class """ general_tests() test_comparison() test_negatives() def general_tests(): """Test basic functionality """ print(" -- Test the Rational class -- ") third = Rational(1,3) print("Should be 1/3:", third) fifth = Rational(1,5) print("Should be 1/5:", fifth) print("Should be 8/15:", third + fifth) print("Should be 1/15:", third * fifth) print("Should be 2/15:", third - fifth) print("Should be 3/5:", fifth // third) def test_comparison(): """ Test comarisons """ print(" -- Test comparisons -- ") one = Rational(1,3) two = Rational(3,9) three = Rational(1,2) print('Three Rationals one, two, three : ' \ + str(one) + ' : ' + str(two) + ' : ' + str(three)) print('One should equal to two (True) ', one == two) print('One should not equal to three (True) ', one != three) print('Three should be greater than one (True) ', three > one) print('One is greater than two (False) ', one > two) print('Three should be greather than or equal to one (True) ', three >= one) print('One is greater than or equal to two (True) ', one >= two) print('Three is less than one (False) ', three < one) print('One is less than two (False) ', one < two) print('Three is less than or equal to one (False) ', three <= one) print('One is less than or equal to two (True) ', one <= two) def test_negatives(): """ Test negative Rationals """ print(" -- Test negatives -- ") one = Rational(-1,3) two = Rational(-3,9) three = Rational(1,-2) print('Three Rationals one, two, three : ' \ + str(one) + ' : ' + str(two) + ' : ' + str(three)) print('One should equal to two (True) ', one == two) print('One should not equal to three (True) ', one != three) print('Three should be greater than one (False) ', three > one) print('One is greater than two (False) ', one > two) print('Three should be greather than or equal to one (False) ', three >= one) print('One is greater than or equal to two (True) ', one >= two) print('Three is less than one (True) ', three < one) print('One is less than two (False) ', one < two) print('Three is less than or equal to one (True) ', three <= one) print('One is less than or equal to two (True) ', one <= two) main()