// TwoSpacePoint_t.cpp // Test the spacepoint class. #include "TwoSpacePoint.h" #include #include #include #include #include #ifndef DEFECT_NO_STDLIB_NAMESPACES using std::cout; using std::cerr; using std::endl; using std::string; using std::list; #ifndef DEFECT_CMATH_NOT_STD using std::cos; using std::sin; using std::sqrt; using std::fabs; #endif #endif #include "ptr/Ptr.h" #include "ptr/AutoPolicy.h" typedef list< Ptr > POINTS; bool myequal(double x1, double x2) { return fabs(x2-x1) < 1.e-12; } //********************************************************************** int main() { string name = "TwoSpacePoint"; string ok_prefix = name + " test (I): "; string error_prefix = name + " test (E): "; cout << ok_prefix << "------- Testing component " + name + ". -------" << endl; double x = 1.23; double y = 2.46; //********************************************************************** cout << ok_prefix << "Testing constructors." << endl; // Create a space point CartesianTwoPoint cart(x,y); cout << cart << endl; // Create in cylindrical coordinates. CylindricalTwoPoint cyl( cart.rxy(), cart.phi() ); cout << cyl << endl; if ( ! myequal(cyl.x(),x) || ! myequal(cyl.y(),y) ) { cout << error_prefix << "Mismatch." << endl; return 1; } //********************************************************************** cout << ok_prefix << "Testing assignment." << endl; TwoSpacePoint spt; cout << spt << endl; spt = cyl; cout << spt << endl; if ( ! myequal(spt.x(),cyl.x()) || ! myequal(spt.y(),cyl.y()) ) { cout << error_prefix << "Mismatch." << endl; return 1; } //********************************************************************** cout << ok_prefix << "Test cos and sin returns." << endl; assert( myequal( spt.cos_phi(), cos( spt.phi() ) ) ); assert( myequal( spt.sin_phi(), sin( spt.phi() ) ) ); //********************************************************************** cout << ok_prefix << "Test equality." << endl; CartesianTwoPoint spt2(spt.x(),spt.y()); assert( spt == spt2 ); assert( ! (spt != spt2) ); CartesianTwoPoint spt3(spt.x()+0.1,spt.y()); assert( ! (spt == spt3) ); assert( spt != spt3 ); CartesianTwoPoint spt4(spt.x(),spt.y()+0.2); assert( spt != spt4 ); //********************************************************************** cout << ok_prefix << "Test distance function." << endl; assert( distance(cart,cart) == 0.0 ); double dx = 1.1; double dy = 2.2; double dist0 = sqrt(dx*dx+dy*dy); CartesianTwoPoint cart2(x+dx,y+dy); cout << cart2 << endl; double dist = distance(cart,cart2); cout << "Distance = " << dist0 << " " << dist << endl; assert( fabs( dist - dist0 ) < 1.e-12 ); //********************************************************************** cout << ok_prefix << "------------- All tests passed. ------------" << endl; return 0; }