// SurfCylinder_t.cpp // Test SurfCylinder. #include "SurfCylinder.h" #include #include #include #include "objstream/StdObjStream.hpp" #include "spacegeom/SpacePath.h" #include "trfutil/trfstream.h" #include "trfbase/VTrack.h" #include "trfbase/CrossStat.h" #include "trfutil/TRFMath.h" #include "SurfCylinderParameters.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using namespace trf; //********************************************************************** namespace { // comparison of doubles int myequal(double x1, double x2) { double small = 1.e-12; if ( fabs(x1-x2) < small ) return 1; cout << "myequal: difference too large:" << endl; cout << "value 1: " << x1 << endl; cout << "value 2: " << x2 << endl; cout << " diff: " << x1-x2 << endl; cout << "maxdiff: " << small << endl; return 0; } } //********************************************************************** int main( ) { string ok_prefix = "SurfCylinder test (I): "; string error_prefix = "SurfCylinder test (E): "; cout << ok_prefix << "------ Testing component SurfCylinder. ------" << endl; // Make sure assert is enabled. bool assert_flag = false; assert ( ( assert_flag = true, assert_flag ) ); if ( ! assert_flag ) { cerr << "Assert is disabled" << endl; return 1; } //******************************************************************** ObjTable::register_type(); cout << trf_format; cout << ok_prefix << "Check indices." << endl; assert( SurfCylinder::RADIUS == 0 ); assert( SurfCylinder::IPHI == 0 ); assert( SurfCylinder::IZ == 1 ); assert( SurfCylinder::IALF == 2 ); assert( SurfCylinder::ITLM == 3 ); assert( SurfCylinder::IQPT == 4 ); //******************************************************************** cout << ok_prefix << "Test constructor and get_parameter." << endl; double r1 = 12.34; SurfCylinder scy1(r1); cout << scy1 << endl; if ( scy1.get_parameter(SurfCylinder::RADIUS) != r1 ) { cerr << error_prefix << "Incorrect radius." << endl; return 1; } //******************************************************************** cout << ok_prefix << "Test type." << endl; cout << SurfCylinder::get_static_type() << endl; cout << scy1.get_type() << endl; assert( scy1.get_type() != 0 ); assert( scy1.get_type() == SurfCylinder::get_static_type() ); //******************************************************************** cout << ok_prefix << "Test equalityi and ordering." << endl; SurfCylinder scy2(12.34); SurfCylinder scy3(24.68); assert( scy1.bound_equal(scy2) ); assert( scy1.pure_equal(scy2) ); assert( ! scy1.bound_equal(scy3) ); assert( ! scy1.pure_equal(scy3) ); assert( scy2.pure_less_than(scy3) ); assert( ! scy3.pure_less_than(scy2) ); //******************************************************************** cout << ok_prefix << "Test virtual constructor." << endl; SurfCylinder* pscy4 = scy1.new_pure_surface(); if ( scy1 != *pscy4 ) { cerr << error_prefix << "Virtual construction failed." << endl; return 5; } //******************************************************************** cout << ok_prefix << "Test crossing status." << endl; TrackVector tvec; tvec(IPHI) = 1.0; tvec(IZ) = 2.0; tvec(IALF) = 3.0; tvec(ITLM) = 4.0; tvec(IQPT) = 5.0; SurfCylinder scin(6.0); SurfCylinder sout(56.0); VTrack ton( SurfacePtr(scy1.new_pure_surface()), tvec ); VTrack tin( SurfacePtr(scin.new_pure_surface()), tvec ); VTrack tout( SurfacePtr(sout.new_pure_surface()), tvec ); CrossStat xs1 = scy1.pure_status(ton); cout << xs1 << endl; assert( xs1.at() && xs1.on() && !xs1.inside() && !xs1.outside() && !xs1.in_bounds() && ! xs1.out_of_bounds() ); CrossStat xs2 = scy1.pure_status(tin); assert( !xs2.at() && !xs2.on() && xs2.inside() && !xs2.outside() && !xs2.in_bounds() && ! xs2.out_of_bounds() ); CrossStat xs3 = scy1.pure_status(tout); cout << xs3 << endl; assert( !xs3.at() && !xs3.on() && !xs3.inside() && xs3.outside() && !xs3.in_bounds() && ! xs3.out_of_bounds() ); //******************************************************************** cout << ok_prefix << "Test vector difference." << endl; TrackVector tvec2; tvec2(0) = 1.1 + 2.0*TWOPI; tvec2(1) = 2.2; tvec2(2) = 3.3; tvec2(3) = 4.4; tvec2(4) = 5.5; TrackVector diff = scy1.vec_diff(tvec2,tvec); cout << tvec2 << endl; cout << tvec << endl; cout << diff << endl; TrackVector ediff; ediff(0) = 0.1; ediff(1) = 0.2; ediff(2) = 0.3; ediff(3) = 0.4; ediff(4) = 0.5; TrackVector zero = diff - ediff; cout << ediff << endl; cout << zero << endl; if ( zero.amax() > 1.e-10 ) { cerr << error_prefix << "Incorrect difference." << endl; return 9; } //******************************************************************** cout << ok_prefix << "Test space point." << endl; SpacePoint spt = ton.space_point(); cout << spt << endl; assert( spt.rxy() == 12.34 ); assert( spt.phi() == 1.0 ); assert( spt.z() == 2.0 ); //******************************************************************** cout << ok_prefix << "Test space vector." << endl; SpacePath svec = ton.space_vector(); double lam = atan(4.0); cout << svec << endl; assert( svec.rxy() == 12.34 ); assert( svec.phi() == 1.0 ); assert( svec.z() == 2.0 ); assert( myequal( svec.drxy(), cos(lam)*cos(3.0) ) ); assert( myequal( svec.rxy_dphi(), cos(lam)*sin(3.0) ) ); assert( myequal( svec.dz(), sin(lam) ) ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; { ObjPtr pobj( new SurfCylinder(12.3) ); ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("cyl1",pobj); cout << mystream.str() << endl; assert( ObjTable::has_object_name("cyl1") ); string::size_type pos = 0; assert( (pos=mystream.str().find( "cyl1 ",pos)) != string::npos ); assert( (pos=mystream.str().find( "SurfCylinder ",pos)) != string::npos ); assert( (pos=mystream.str().find( "radius",pos)) != string::npos ); assert( (pos=mystream.str().find( "12.3 ",pos)) != string::npos ); } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ cyl2 SurfCylinder radius=24.6 ]"; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "cyl2" ); const SurfCylinder* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == SurfCylinder::get_static_type() ); assert( pobj->get_radius() == 24.6 ); } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }