// PathStop_t.cpp #include "PathStop.h" #include #include #include #include "objstream/ObjData.hpp" #include "objstream/ObjTable.hpp" #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" #include "trffit/FullFitterTest.h" #include "Filter.h" #include "PathStopPtr.h" #include "CheckerTest.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::ostringstream; using std::istringstream; using namespace trf; //********************************************************************** ObjPtr create_FilterTest(const ObjData& data); class FilterTest : public Filter { public: // static methods // Return the type name. static TypeName get_type_name() { return "FilterTest"; } // Return the creator. static ObjCreator get_creator() { return create_FilterTest ; } // Return the type. static Type get_static_type() { return get_creator(); } private: int _index; void ostr(ostream& stream) const { stream << begin_object; stream << "Test filter " << _index; stream << end_object; }; public: FilterTest(int index) : _index(index) { }; // Return the type. Type get_type() const { return get_static_type(); } // Return the index. int get_index() const { return _index; } // Write the object data. ObjData write_data() const { ObjData data("FilterTest"); data.add_int("index",get_index()); return data; } FlagArray process(MTrackArray& trms) const { return FlagArray(trms.size()); } }; // Creator. ObjPtr create_FilterTest(const ObjData& data) { assert( data.get_object_type() == "FilterTest" ); return ObjPtr( new FilterTest( data.get_int("index") ) ); } // Register creator. ObjTable::Status stat_create = ObjTable::register_type(); //********************************************************************** int main( ) { string component = "PathStop"; string ok_prefix = component + " (I): "; string error_prefix = component + " test (E): "; cout << ok_prefix << "-------- Testing component " + component + ". --------" << 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 << trf_format; cout << ok_prefix << "Create path stop." << endl; ObjTable::register_type(); FullFitterPtr pfit(new FullFitterTest(1) ); PathStop stop(pfit); cout << stop << endl; assert( stop.get_filters().size() == 0 ); //******************************************************************** cout << ok_prefix << "Add filters." << endl; FilterPtr pfilt1( new FilterTest(1) ); FilterPtr pfilt2( new FilterTest(2) ); FilterPtr pfilt3( new FilterTest(3) ); stop.add_filter( pfilt1 ); cout << stop << endl; assert( stop.get_filters().size() == 1 ); stop.add_filter( pfilt2 ); stop.add_filter( pfilt3 ); cout << stop << endl; assert( stop.get_filters().size() == 3 ); //******************************************************************** cout << ok_prefix << "Add checkers." << endl; CheckerPtr pchk1( new CheckerTest ); CheckerPtr pchk2( new CheckerTest ); CheckerPtr pchk3( new CheckerTest ); stop.add_checker(pchk1); cout << stop << endl; assert( stop.get_checkers().size() == 1 ); stop.add_checker(pchk2); stop.add_checker(pchk3); cout << stop << endl; assert( stop.get_checkers().size() == 3 ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; { Ptr pstp( new PathStop(pfit) ); pstp->add_filter(pfilt1); pstp->add_filter(pfilt2); pstp->add_checker(pchk1); pstp->add_checker(pchk2); pstp->add_checker(pchk3); ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("fit1",pfit); objstream.write_object("filt1",pfilt1); objstream.write_object("filt2",pfilt2); objstream.write_object("check1",pchk1); objstream.write_object("check2",pchk2); objstream.write_object("check3",pchk3); objstream.write_object("stop1",pstp); cout << mystream.str() << endl; string::size_type pos = 0; assert( ObjTable::has_object_name("stop1") ); assert( (pos=mystream.str().find("stop1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("PathStop ",pos)) != string::npos ); #ifdef ObjData_supports_lists assert( (pos=mystream.str().find("fitter",pos)) != string::npos ); assert( (pos=mystream.str().find("fit1",pos)) != string::npos ); assert( (pos=mystream.str().find("filters",pos)) != string::npos ); assert( (pos=mystream.str().find("filt1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("filt2 ",pos)) != string::npos ); assert( (pos=mystream.str().find("checkers",pos)) != string::npos ); assert( (pos=mystream.str().find("check1",pos)) != string::npos ); assert( (pos=mystream.str().find("check2",pos)) != string::npos ); assert( (pos=mystream.str().find("check3",pos)) != string::npos ); #endif } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { string istring = "[ stop2 PathStop"; istring += "\n fitter=@fit1"; #ifdef ObjData_supports_lists istring += "\n filters=@( filt1 filt2 )"; istring += "\n checkers=@( check2 check3 )"; #endif istring += "\n]"; istringstream isstrm(istring); { StdObjStream obstr(isstrm); string name = obstr.read_object(); assert( name == "stop2" ); const PathStop* pstp; ObjTable::get_object("stop2",pstp); assert( pstp != 0 ); assert( pstp->get_type() == PathStop::get_static_type() ); assert( pstp->get_fitter() == pfit ); #ifdef ObjData_supports_lists assert( pstp->get_filters().size() == 2 ); assert( pstp->get_checkers().size() == 2 ); #endif } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }