#include #include "distance.h" using namespace std; const int INCHES_2_FOOT = 12; const int FEET_2_YARD = 3; const int YARDS_2_MILE = 5280; // print an error void Error(string title, int a, int b, string other); // two tests bool CheckTotal(const DistanceT & d, int distance); void ValueTest(DistanceT d , int distance); // utiltity functions void Units(int distance, int & inch, int & foot, int & yard, int & mile); int Inches(int inch, int foot, int yards, int miles); void AddTest(void) ; void ConstructorTest(void); int main() { ConstructorTest(); AddTest(); return 0; } void ConstructorTest() { int inch, foot, yard, mile; int d1=0, d2=0, d3 = 0; // check a bunch of constructors for(mile = 0; mile < 2; mile++) { d1 = mile *YARDS_2_MILE * FEET_2_YARD * INCHES_2_FOOT; for(yard = 0; yard < 10; yard ++) { d2 = yard * FEET_2_YARD * INCHES_2_FOOT; for(foot = 0; foot < 10; foot ++) { d3 = foot * INCHES_2_FOOT; for (inch = 0; inch < 50; inch ++) { DistanceT a(inch); if (!CheckTotal(a, inch)) { cout << "Distance Check Failed: in " << endl; } DistanceT b(foot, inch); if (!CheckTotal(b, d3+inch)) { cout << "Distance Check Failed: ft/in " << endl; } DistanceT c(yard, foot, inch); if (!CheckTotal(c, d2+d3+inch)) { cout << "Distance Check Failed: yd/ft/in " << endl; } DistanceT d(mile, yard, foot, inch); if (!CheckTotal(d, d1+d2+d3+inch)) { cout << "Distance Check Failed: mi/yd/ft/in " << endl; } DistanceT e( d1+d2+d3+inch); if (!CheckTotal(d, d1+d2+d3+inch)) { cout << "Distance Check Failed: big " << endl; } ValueTest(e, d1+d2+d3+inch); if (!CheckTotal(e, d1+d2+d3+inch)) { cout << "Value Check Failed" << endl; } DistanceT f; f = e; if (!CheckTotal(f, d1+d2+d3+inch)) { cout << " = test Failed" << endl; } } } } } // nasty for nsted loop; return; } void AddTest() { DistanceT a, b, c, d; DistanceT in(1), ft(1,0), yd(1,0,0); int distance; int i; distance = 0; for(i = 0; i < 10; i++) { a = a+in; distance ++; if(!CheckTotal(a, distance)) { cout << "Inch add failed " << endl; } } distance = 0; for (i = 0; i < 10;i++) { b = b + in + ft; distance += 1+INCHES_2_FOOT; if(!CheckTotal(b, distance)) { cout << "Inch add failed " << endl; } } distance = 0; for (i = 0; i < 10;i++) { c = c + in + ft + yd; distance += 1+INCHES_2_FOOT+FEET_2_YARD*INCHES_2_FOOT; if(!CheckTotal(c, distance)) { cout << "Inch add failed " << endl; } } return; } void ValueTest(DistanceT d , int distance) { DistanceT different(1); if( not CheckTotal(d, distance)) { cout << "Total check problem in Value Test" << endl; } d = different; return; } int Inches(int inch, int foot, int yards, int miles) { return inch+(foot+(yards+(miles)*YARDS_2_MILE)*FEET_2_YARD)*INCHES_2_FOOT; } void Units(int distance, int & inch, int & foot, int & yard, int & mile) { inch = 0; foot = 0; yard = 0; mile = 0; inch = distance % INCHES_2_FOOT; distance /= INCHES_2_FOOT; if (distance > 0) { foot = distance % FEET_2_YARD; distance /= FEET_2_YARD; if (distance > 0) { yard = distance % YARDS_2_MILE; mile = distance / YARDS_2_MILE; } } return; } void Error(string title, int a, int b, string other) { cout << "Error in " << title << endl; cout << "\tIs : " << a << endl; cout << "\tShould Be: " << b << endl; cout << "\tValue : " << other << endl; exit(0); return; } bool CheckTotal(const DistanceT & d, int distance) { int inch=0, foot=0, yard=0, mile=0; Units(distance, inch, foot, yard, mile); if (inch != d.Inch() ) { Error("inch value", d.Inch(), inch, d.DistanceString()); return false; } if (foot != d.Foot() ) { Error("foot value", d.Foot(), foot, d.DistanceString()); return false; } if (yard != d.Yard() ) { Error("yard value", d.Yard(), yard, d.DistanceString()); return false; } if (mile != d.Mile() ) { Error("mile value", d.Mile(), mile, d.DistanceString()); return false; } return true; }