// SpacePoint_t.cpp // Test the spacepoint class. #include "SpacePoint.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 = "SpacePoint"; 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; double z = 3.69; //********************************************************************** cout << ok_prefix << "Testing constructors." << endl; // Create a space point CartesianPoint cart(x,y,z); cout << cart << endl; // Create in cylindrical coordinates. CylindricalPoint cyl( cart.rxy(), cart.phi(), cart.z() ); cout << cyl << endl; // Create in spherical coordinates. SphericalPoint sph( cyl.rxyz(), cyl.phi(), cyl.theta() ); cout << sph << endl; if ( ! myequal(sph.x(),x) || ! myequal(sph.y(),y) || ! myequal(sph.z(),z) ) { cout << error_prefix << "Mismatch." << endl; return 1; } //********************************************************************** cout << ok_prefix << "Testing assignment." << endl; SpacePoint spt; cout << spt << endl; spt = sph; cout << spt << endl; if ( ! myequal(spt.x(),sph.x()) || ! myequal(spt.y(),sph.y()) || ! myequal(spt.z(),sph.z()) ) { 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() ) ) ); assert( myequal( spt.cos_theta(), cos( spt.theta() ) ) ); assert( myequal( spt.sin_theta(), sin( spt.theta() ) ) ); //********************************************************************** cout << ok_prefix << "Test equality." << endl; CartesianPoint spt2(spt.x(),spt.y(),spt.z()); assert( spt == spt2 ); assert( ! (spt != spt2) ); CartesianPoint spt3(spt.x()+0.1,spt.y(),spt.z()); assert( ! (spt == spt3) ); assert( spt != spt3 ); CartesianPoint spt4(spt.x(),spt.y()+0.2,spt.z()); assert( spt != spt4 ); CartesianPoint spt5(spt.x(),spt.y(),spt.z()+0.3); assert( spt != spt5 ); //********************************************************************** cout << ok_prefix << "Test distance function." << endl; assert( distance(cart,cart) == 0.0 ); double dx = 1.1; double dy = 2.2; double dz = 3.3; double dist0 = sqrt(dx*dx+dy*dy+dz*dz); CartesianPoint cart2(x+dx,y+dy,z+dz); 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; }