// ObjTable_t.cpp #include "objstream/ObjTable.hpp" #include "objstream/ObjTableTest.hpp" #include #include #include #include #include #include using std::cout; using std::cerr; using std::endl; using std::list; using std::set; using std::string; using std::vector; typedef Ptr PPage; //********************************************************************** int main( ) { string component = "ObjTable"; 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 << ok_prefix << "Check registration." << endl; assert( regstat_Page == ObjTable::OK ); //******************************************************************** cout << ok_prefix << "Check duplicate registration." << endl; #ifdef DEFECT_NO_MEMBER_TEMPLATES ObjTable::Status reg2 = ObjTable::register_type(Page(1,2,true,"x")); #else ObjTable::Status reg2 = ObjTable::register_type(); #endif assert( reg2 == ObjTable::OK ); //******************************************************************** assert( ObjTable::get_object_count() == 0 ); //******************************************************************** cout << ok_prefix << "Insert existing object." << endl; PPage pp1( new Page(1,11.1,false,"p1") ); #ifdef ObjData_supports_lists set refs; refs.insert(1); vector lens; lens.push_back(11.1); lens.push_back(12.2); lens.push_back(13.3); lens.push_back(14.4); lens.push_back(15.5); list isrefs; isrefs.push_back(false); isrefs.push_back(false); isrefs.push_back(true); isrefs.push_back(false); vector paras; paras.push_back("start"); paras.push_back("m i d d l e"); paras.push_back("END"); Figure fig1; Figure fig2; FigureList figs; figs.insert(&fig1); figs.insert(&fig2); TableList tabs; tabs.push_back( TablePtr(new Table) ); tabs.push_back( TablePtr(new Table) ); tabs.push_back( TablePtr(new Table) ); #endif PPage pp2( new Page(2,22.2,false,"p 2",pp1.pointer(),pp1 #ifdef ObjData_supports_lists , refs,lens,isrefs,paras,figs,tabs #endif ) ); { cout << pp1 << " " << *pp1 << endl; ObjTable::add_object("p1",pp1); assert( ObjTable::get_object_count() == 1 ); cout << pp2 << " " << *pp2 << endl; assert( pp1 != pp2 ); assert( ! ( *pp1 == *pp2 ) ); ObjTable::get_object("p1",pp2); cout << pp2 << " " << *pp2 << endl; assert( pp1 == pp2 ); assert( *pp1 == *pp2 ); assert( ObjTable::get_object_name(pp1) == "p1" ); } assert( ObjTable::get_object_count() == 1 ); //******************************************************************** cout << ok_prefix << "Create object from a data object" << endl; { double dval = 33.3; // We define 33.3 from a string to avaoid roundoff problems. ObjData data("Page"); data.add_int("number",3); data.add_double("length",dval); data.add_bool("start",false); data.add_string("text","This is the text on page 3."); data.add_bare_pointer("prev",pp2.pointer()); data.add_share_pointer("first",pp1); #ifdef ObjData_supports_lists data.add_int_list("refs",refs); data.add_double_list("lens",lens); data.add_bool_list("isrefs",isrefs); data.add_string_list("paras",paras); data.add_bare_ptr_list("figs",figs); data.add_share_ptr_list("tabs",tabs); #endif cout << data << endl; ObjTable::new_object("t3", data); PPage pp3; assert( ! pp3 ); ObjTable::get_object("t3", pp3); assert( pp3 != 0 ); cout << *pp3 << endl; assert( *pp3 == Page(3,dval,false,"This is the text on page 3.", pp2.pointer(), pp1 //NT prob #ifdef ObjData_supports_lists , refs, lens, isrefs, paras, figs, tabs //NT prob #endif ) ); assert( ObjTable::get_object_name(pp3) == "t3" ); } assert( ObjTable::get_object_count() == 2 ); //******************************************************************** cout << ok_prefix << "Check type-creator access methods." << endl; { ObjCreator cr = ObjTable::get_creator("Page"); assert( cr != 0 ); ObjTable::TypeName name = ObjTable::get_type_name(cr); assert( name == "Page" ); } //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }