// Path_t.cpp #include "Path.h" #include #include #include #include "objstream/StdObjStream.hpp" #include "trfutil/trfstream.h" #include "trflayer/LayerTest.h" #include "trfbase/PropStat.h" #include "trfbase/PropDirectedTest.h" #include "trfbase/PropagatorPtr.h" #include "trflayer/LayerPtr.h" #include "trffit/AddFitter.h" #include "trffit/AddFitterPtr.h" #include "trffit/AddFitterTest.h" #include "Checker.h" #include "Filter.h" #include "PathStop.h" #include "ClusterFilterTest.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; //********************************************************************** int main( ) { string component = "Path"; 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 the head path." << endl; ObjTable::register_type(); ObjTable::register_type(); assert( Path::get_path_count() == 0 ); Path path0; const Path& cpath0 = path0; cout << path0 << endl; assert( cpath0.is_head() ); assert( cpath0.get_children().size() == 0 ); assert( path0.get_mutable_children().size() == 0 ); assert( Path::get_path_count() == 1 ); //******************************************************************** cout << ok_prefix << "Add a child." << endl; LayerTest lay(1.0); PropTest prop; AddFitterTest fit(1); Path& path1 = *path0.add_child(lay,prop,fit); cout << path1 << endl; assert( ! path1.is_head() ); assert( path0.get_children().size() == 1 ); assert( path1.get_children().size() == 0 ); assert( path1.get_checkers().size() == 0 ); assert( Path::get_path_count() == 2 ); //******************************************************************** cout << ok_prefix << "Add more children." << endl; Path& path2 = *path0.add_child(lay,prop,fit,"p2"); Path& path3 = *path0.add_child(lay,prop,fit); cout << path0 << endl; assert( path0.get_children().size() == 3 ); assert( path0.get_mutable_children().size() == 3 ); assert( Path::get_path_count() == 4 ); assert( path2.get_name() == "p2" ); assert( path3.get_name() == "" ); //******************************************************************** cout << ok_prefix << "Add another generation." << endl; Path& path11 = *path1.add_child(lay,prop,fit); Path& path21 = *path2.add_child(lay,prop,fit); Path& path22 = *path2.add_child(lay,prop,fit); Path& path31 = *path3.add_child(lay,prop,fit); Path& path32 = *path3.add_child(lay,prop,fit); Path& path33 = *path3.add_child(lay,prop,fit); cout << path0 << endl; cout << path1 << endl; cout << path2 << endl; cout << path3 << endl; assert( path0.get_children().size() == 3 ); Path::PathList::const_iterator ipth; const Path::PathList& paths = path0.get_children(); assert( paths.size() == 3 ); int icnt = 0; cout << "Checking paths." << endl; for ( ipth=paths.begin(); ipth!=paths.end(); ++ipth ) { cout << **ipth << endl; assert( (*ipth)->get_children().size() == ++icnt ); assert( (*ipth)->get_parent() == &path0 ); } assert( Path::get_path_count() == 10 ); //******************************************************************** cout << ok_prefix << "Add cluster filters." << endl; assert( path0.get_cluster_filters().size() == 0 ); assert( path1.get_cluster_filters().size() == 0 ); ClusterFilterPtr pfil( new ClusterFilterTest ); path1.add_cluster_filter(pfil); path1.add_cluster_filter(pfil); path1.add_cluster_filter(pfil); path1.add_cluster_filter(pfil); cout << path1 << endl; assert( path0.get_cluster_filters().size() == 0 ); assert( path1.get_cluster_filters().size() == 4 ); assert( path2.get_cluster_filters().size() == 0 ); //******************************************************************** cout << ok_prefix << "Add checkers." << endl; assert( path0.get_checkers().size() == 0 ); assert( path1.get_checkers().size() == 0 ); CheckerPtr pchk( new CheckerTest ); path1.add_checker(pchk); path1.add_checker(pchk); path1.add_checker(pchk); path1.add_checker(pchk); cout << path1 << endl; assert( path0.get_checkers().size() == 0 ); assert( path1.get_checkers().size() == 4 ); assert( path2.get_checkers().size() == 0 ); //******************************************************************** cout << ok_prefix << "Check candidate layer." << endl; const CandidateLayer& cnl = path1.get_candidate_layer(); cout << path1 << endl; assert( &cnl.get_propagator() == &prop ); //******************************************************************** cout << ok_prefix << "Check stop." << endl; PathStopPtr pstop( new PathStop ); assert( ! path1.get_stop() ); path1.set_stop(pstop); cout << path1 << endl; assert( path1.get_stop() == pstop ); //******************************************************************** cout << ok_prefix << "Check end." << endl; assert( ! path1.get_end() ); path1.set_end(); assert( path1.get_end() ); path1.set_end(false); assert( ! path1.get_end() ); //******************************************************************** cout << ok_prefix << "Write object data." << endl; LayerPtr plyr( new LayerTest(1.0) ); PropagatorPtr pprop( new PropTest ); AddFitterPtr pfit( new AddFitterTest(1) ); CheckerPtr pchk1( new CheckerTest ); CheckerPtr pchk2( new CheckerTest ); { MutablePathPtr phead( new Path ); MutablePathPtr pchild = phead->add_child(*plyr,*pprop,*pfit); pchild->add_checker(pchk1); pchild->add_checker(pchk2); pchild->set_stop(pstop); pchild->set_end(true); // Write objects. ostringstream mystream; StdObjStream objstream(mystream); objstream.write_object("lyr",plyr); objstream.write_object("prop",pprop); objstream.write_object("fit",pfit); objstream.write_object("chk1",pchk1); objstream.write_object("chk2",pchk2); objstream.write_object("stop",pstop); objstream.write_object("head1",phead); objstream.write_object("child1",pchild); cout << mystream.str() << endl; // Check parent. assert( ObjTable::has_object_name("head1") ); string::size_type pos = 0; assert( (pos=mystream.str().find("head1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("Path ",pos)) != string::npos ); assert( (pos=mystream.str().find("parent",pos)) != string::npos ); assert( (pos=mystream.str().find("0 ",pos)) != string::npos ); #ifdef ObjData_supports_lists assert( (pos=mystream.str().find("checkers",pos)) != string::npos ); #endif assert( (pos=mystream.str().find("stop",pos)) != string::npos ); assert( (pos=mystream.str().find("0 ",pos)) != string::npos ); assert( (pos=mystream.str().find("end",pos)) != string::npos ); assert( (pos=mystream.str().find("false ",pos)) != string::npos ); // Check child. assert( ObjTable::has_object_name("child1") ); assert( (pos=mystream.str().find("child1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("Path ",pos)) != string::npos ); assert( (pos=mystream.str().find("parent",pos)) != string::npos ); assert( (pos=mystream.str().find("head1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("layer",pos)) != string::npos ); assert( (pos=mystream.str().find("lyr ",pos)) != string::npos ); assert( (pos=mystream.str().find("propagator",pos)) != string::npos ); assert( (pos=mystream.str().find("prop ",pos)) != string::npos ); assert( (pos=mystream.str().find("fitter",pos)) != string::npos ); assert( (pos=mystream.str().find("fit ",pos)) != string::npos ); #ifdef ObjData_supports_lists assert( (pos=mystream.str().find("checkers",pos)) != string::npos ); assert( (pos=mystream.str().find("chk1 ",pos)) != string::npos ); assert( (pos=mystream.str().find("chk2 ",pos)) != string::npos ); #endif assert( (pos=mystream.str().find("stop",pos)) != string::npos ); assert( (pos=mystream.str().find("stop ",++pos)) != string::npos ); assert( (pos=mystream.str().find("end",pos)) != string::npos ); assert( (pos=mystream.str().find("true ",pos)) != string::npos ); } //******************************************************************** cout << ok_prefix << "Read object data." << endl; { // Write head. string istring = "[ head2 Path parent=@0"; #ifdef ObjData_supports_lists istring += "\n checkers=@( )"; #endif istring += "\n stop=@0 end=false"; istring += "\n]"; // Write child. istring += "\n[ child2 Path parent=@head2"; istring += "\n layer=*lyr propagator=*prop fitter=*fit"; #ifdef ObjData_supports_lists istring += "\n checkers=@( chk1 chk2 )"; #endif istring += "\n stop=@stop end=true"; istring += "\n]"; istringstream isstrm(istring); StdObjStream obstr(isstrm); // Read and check head. { string name = obstr.read_object(); assert( name == "head2" ); const Path* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == Path::get_static_type() ); assert( pobj->is_head() ); #ifdef ObjData_supports_lists assert( pobj->get_checkers().size() == 0 ); #endif assert( pobj->get_end() == false ); } // Read and check child. { string name = obstr.read_object(); assert( name == "child2" ); const Path* pobj; ObjTable::get_object(name,pobj); assert( pobj != 0 ); assert( pobj->get_type() == Path::get_static_type() ); assert( ! pobj->is_head() ); assert( &pobj->get_candidate_layer().get_layer() == plyr ); assert( &pobj->get_candidate_layer().get_propagator() == pprop ); assert( &pobj->get_candidate_layer().get_fitter() == pfit ); #ifdef ObjData_supports_lists assert( pobj->get_checkers().size() == 2 ); assert( pobj->get_checkers().front() == pchk1 ); assert( pobj->get_checkers().back() == pchk2 ); #endif assert( pobj->get_stop() == pstop ); assert( pobj->get_end() == true ); } } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }