// PropDispatch_t.cpp #include "PropDispatch.h" #include #include #include #include "objstream/StdObjStream.hpp" #include "SurfTest.h" #include "TrackVector.h" #include "VTrack.h" #include "ETrack.h" #include "PropStat.h" #include "trfutil/trfstream.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using namespace trf; //********************************************************************** int _flag; // Simple test propagator that sets a flag differently // depending on the method called. ObjPtr create(const ObjData& data); class PropTest : public Propagator { private: // Return the type name. static TypeName get_type_name() { return "PropTest"; } // Return the creator. static ObjCreator get_creator() { return create; } // Return the type. static Type get_static_type() { return get_creator(); } void ostr(ostream& stream) const { stream << begin_object << "Test propagator." << end_object; } public: PropTest() { _flag = 0; }; // return the type Type get_type() const { return get_static_type(); }; // Return object data. ObjData write_data() const { return ObjData("PropTest"); } int get_flag() { return _flag; }; Propagator* new_propagator() const { return new PropTest; }; PropStat vec_prop(VTrack& trv, const Surface& srf, TrackDerivative* pder) const { _flag = 1; return PropStat(); }; PropStat err_prop(ETrack& tre, const Surface& srf, TrackDerivative* pder) const { _flag = 2; return PropStat(); }; PropStat vec_dir_prop(VTrack& trv, const Surface& srf, PropDir dir, TrackDerivative* pder) const { _flag = 3; return PropStat(); }; PropStat err_dir_prop(ETrack& tre, const Surface& srf, PropDir dir, TrackDerivative* pder) const { _flag = 4; return PropStat(); }; }; // Creator. ObjPtr create(const ObjData& data) { assert( data.get_object_type() == "PropTest" ); return ObjPtr( new PropTest ); } //********************************************************************** int main( ) { string ok_prefix = "PropDispatch test (I): "; string error_prefix = "PropDispatch test (E): "; cout << ok_prefix << "------- Testing component PropDispatch. -------" << 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; } //******************************************************************** cout << ok_prefix << "Test constructor." << endl; cout << trf_format; ObjTable::register_type(); ObjTable::register_type(); PropTest* ptest_prop0 = new PropTest; PropagatorPtr pprop0( ptest_prop0 ); cout << *ptest_prop0 << endl; PropDispatch tprop; TrfObject::Type stype = SurfTest::get_static_type(); int stat = tprop.add_propagator(stype,stype,pprop0); assert( stat == 0 ); stat = tprop.add_propagator(stype,stype,pprop0); assert( stat != 0 ); cout << tprop << endl; //******************************************************************** cout << ok_prefix << "Test propagation." << endl; SurfTest tsrf1(1); VTrack trv1( SurfacePtr(tsrf1.new_pure_surface()) ); ETrack tre1( SurfacePtr(tsrf1.new_pure_surface()) ); SurfTest tsrf2(2); tprop.vec_prop(trv1,tsrf2); assert( ptest_prop0->get_flag() == 1 ); tprop.err_prop(tre1,tsrf2); assert( ptest_prop0->get_flag() == 2 ); tprop.vec_dir_prop(trv1,tsrf2,Propagator::NEAREST); assert( ptest_prop0->get_flag() == 3 ); tprop.err_dir_prop(tre1,tsrf2,Propagator::NEAREST); assert( ptest_prop0->get_flag() == 4 ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; PropagatorPtr ptest( new PropTest ); { PropDispatch* ppropd( new PropDispatch ); PropagatorPtr pprop(ppropd); ppropd->add_propagator(stype,stype,ptest); cout << *ppropd << endl; ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object( "test1", ptest ); objstream.write_object( "dispatch1", pprop ); cout << mystream.str() << endl; assert( ObjTable::has_object_name("dispatch1") ); string::size_type pos = 0; assert( (pos=mystream.str().find("PropTest ", pos)) != string::npos ); assert( (pos=mystream.str().find("PropDispatch ", pos)) != string::npos ); #ifdef ObjData_supports_lists assert( (pos=mystream.str().find("SurfTest", pos)) != string::npos ); ++pos; assert( (pos=mystream.str().find("SurfTest", pos)) != string::npos ); assert( (pos=mystream.str().find("test1", pos)) != string::npos ); #endif } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ dispatch2 PropDispatch "; #ifdef ObjData_supports_lists istring += "begin_surfs = string( \"SurfTest\" ) "; istring += "end_surfs = string( \"SurfTest\" ) "; istring += "propagators = @( test1 ) "; #endif istring += "]"; cout << istring << endl; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "dispatch2" ); const PropDispatch* pobj; ObjTable::get_object("dispatch2",pobj); assert( pobj != 0 ); cout << *pobj << endl; assert( pobj->get_type() == PropDispatch::get_static_type() ); #ifdef ObjData_supports_lists assert( pobj->get_propagator_count() == 1 ); assert( pobj->get_propagator(stype,stype) == ptest ); #endif } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }