// testfraction.cpp - tests fraction.cpp // DO NOT CHANGE THIS FILE - you will not turn it in // use to test CS 60 assignment 5, part 2, Fall 2009 // cmc, updated 11/17/09 #include using namespace std; #include "fraction.h" // a macro to simplify printing #define pf(f) #f": " << f << endl // a function to test implicit conversion to double double value(double d) { return d; } // just some simple tests int main() { Fraction const zero; // test default ctor Fraction const one = 1; // test one converting ctor Fraction const third = "1/3"; // test other converting ctor Fraction const two = "2"; // test it another way Fraction const minusOne = -one; // test negation and copy ctor Fraction const three = two + one; // test addition Fraction const half = !two; // test invert Fraction const quarter(-4, -16); // test simplification in ctor Fraction const minus3by4("75/-100"); // test it another way Fraction const eighth = quarter * half; // test multiplication Fraction const twelfth = two * third * eighth; // test multiply/simplify cout << pf(zero) << pf(one) << pf(third) << pf(two) << pf(minusOne) << pf(three) << pf(half) << pf(quarter) << pf(minus3by4) << pf(eighth) << pf(twelfth); cout << "value of -(1/4 + 1/8): " // test conversion to double << static_cast( -(quarter + eighth) ) << endl; cout << "value of eighth: " // test implicit conversion << value(eighth) << endl; try { Fraction f = "junk"; } // test construction with bad string catch(char const *error) { cerr << error << endl; } try { Fraction f(1, 0); } // test construction with zero denominator catch(char const *error) { cerr << error << endl; } try { Fraction f = "1/0"; } // test zero denominator a different way catch(char const *error) { cerr << error << endl; } try { !zero; } // test attempt to invert zero catch(char const *error) { cerr << error << endl; } }