// testExceptions.cc Daniel Roche // This will test the exception throwing of the Rational_C class. #include using std::cout; using std::endl; #include "rational.h" // Test what happens when we try to set a denominator to zero in the // constructor. The numerator will be set to the given value. void constructorTest( int num ) { bool passed = false; // try to do it. try { Rational_C f(num, 0); passed = false; // This line should not be reached. } catch( DenomIsZero x ) { passed = true; // We want to catch this exception } if( passed ) cout << "\tPassed" << endl; else cout << "\tFailed" << endl; } // Test what happens when we try to set the denominator of an existing Rational // to zero. The num and denom will be the values used to construct the rational // initially. The denom should be nonzero. void setDenomTest( int num, int denom ) { bool passed = false; bool constructedYet = false; try { Rational_C f(num, denom); constructedYet = true; f.setDenom(0); passed = false; // shouldn't reach this statement } catch( DenomIsZero x ) { passed = constructedYet; // Only pass if the number was successfully constructed first. } if( passed ) cout << "\tPassed" << endl; else cout << "\tFailed" << endl; } int main(void) { cout << "Constructor exception tests:" << endl; constructorTest(5); constructorTest(-2243); constructorTest(0); cout << "setDeonom exception tests:" << endl; setDenomTest(5,4); setDenomTest(-2,9); setDenomTest(-8,-4); return 0; }