void testDictSet() { NodeRef vals = Node::create("vals"); // 110010 vals->set_attr("sub1", 2 ); vals->set_attr("sub2", "6" ); // valuemap value dev.set ( "Terminal1", "reg2", vals ); CPPUNIT_ASSERT_EQUAL ( 50, (int) dev.get( "Terminal1", "reg2" ) ); // 110011 vals = Node::create("newvals" ); vals->set_attr("sub1", 3 ); dev.set ( "Terminal1", "reg2" , vals ); CPPUNIT_ASSERT_EQUAL ( 51, (int) dev.get( "Terminal1", "reg2" ) ); // 000011 vals->set_attr("sub2", 0); dev.set ( "Terminal1", "reg2", vals ); CPPUNIT_ASSERT_EQUAL ( 3, (int) dev.get("Terminal1", "reg2" ) ); CPPUNIT_ASSERT_NO_THROW ( vals = dev.get_subregs ( "Terminal1", "reg2" ) ); CPPUNIT_ASSERT ( vals->has_attr("sub1" ) ); CPPUNIT_ASSERT ( vals->has_attr("sub2" ) ); CPPUNIT_ASSERT_EQUAL ( 3, (int) vals->get_attr("sub1") ); CPPUNIT_ASSERT_EQUAL ( 0, (int) vals->get_attr("sub2") ); }
void testClone() { NodeRef p = Node::create("Parent"); NodeRef c = Node::create("child"); p->add_child(c); NodeRef copy = p->clone(); copy->get_child("child")->set_name("child1"); CPPUNIT_ASSERT_NO_THROW ( copy->get_child("child1") ); CPPUNIT_ASSERT_NO_THROW ( p->get_child("child") ); CPPUNIT_ASSERT_THROW ( p->get_child("child1"), Exception ); NodeRef attr = Node::create("attr"); p->set_attr("test",attr); p->set_attr("data",3); copy = p->clone(); ((NodeRef)copy->get_attr("test"))->set_name("attr1"); CPPUNIT_ASSERT_EQUAL ( ((NodeRef)p->get_attr("test"))->get_name(), std::string("attr" ) ); CPPUNIT_ASSERT_EQUAL ( ((NodeRef)copy->get_attr("test"))->get_name(), std::string("attr1" ) ); CPPUNIT_ASSERT_NO_THROW ( copy->get_attr("data") ); CPPUNIT_ASSERT_EQUAL ( 3, (int) copy->get_attr("data" ) ); NodeRef di = DeviceInterface::create("di"); NodeRef term = Terminal::create("term1"); di->add_child(term); NodeRef term2 = term->clone(); term2->set_name("term2"); term2->del_attr("addr"); // so we get a new one CPPUNIT_ASSERT_NO_THROW( di->add_child(term2) ); }
void testList() { NodeRef n = Node::create("Some Node"); { std::vector<DataType> my_list; for (int i= 0; i< 10; ++i ) my_list.push_back ( i ); my_list.push_back ( "Ten" ); n->set_attr ( "list_data", my_list ); // my_list popped } std::vector<DataType> new_list = n->get_attr ( "list_data" ); CPPUNIT_ASSERT_EQUAL ( 11, (int32) new_list.size() ); CPPUNIT_ASSERT_EQUAL ( 7, (int32) new_list.at(7) ); CPPUNIT_ASSERT_EQUAL ( std::string ( "Ten" ) , (std::string) new_list.at(10) ); }
void testAttrs() { NodeRef orig = DeviceInterface::create("top"); CPPUNIT_ASSERT_THROW ( orig->get_attr("attrX"), Exception ); orig->set_attr ( "attr1", 1 ); orig->set_attr ( "attr2", std::string("two") ); orig->set_attr ( "attr3", 3 ); CPPUNIT_ASSERT ( orig->get_attr("attr3")==3 ); orig->set_attr ( "attr3", 4 ); // overwrite CPPUNIT_ASSERT ( orig->get_attr("attr3")==4 ); orig->set_attr ( "attr3", 5 ); // overwrite again CPPUNIT_ASSERT ( orig->get_attr("attr3")==5 ); CPPUNIT_ASSERT ( orig->get_attr("attr1")== 1 ); CPPUNIT_ASSERT ( orig->get_attr("attr2") == "two" ); CPPUNIT_ASSERT_THROW ( orig->get_attr ( "attrX" ), Exception ); { NodeRef attr_map = Node::create("generic attrs"); attr_map->set_attr ( "map1", 1 ); attr_map->set_attr ( "map2", 2 ); orig->set_attr ( "map", attr_map ); // attr map out of scope } CPPUNIT_ASSERT_NO_THROW ( orig->get_attr ( "map" )); CPPUNIT_ASSERT_EQUAL ( NODE_DATA, orig->get_attr( "map" ).get_type()); CPPUNIT_ASSERT_EQUAL ( 2, (int32)((NodeRef)orig->get_attr("map"))->get_attr ("map2") ); // four attrs DIAttrIter ai = orig->attrs_begin(); ++ai; ++ai; CPPUNIT_ASSERT ( ai != orig->attrs_end() ); ++ai; ++ai; CPPUNIT_ASSERT ( ai == orig->attrs_end() ); { NodeRef first = Terminal::create("first"); first->set_attr("attr1", 1 ); orig->add_child(first); //first goes out of scope } NodeRef child=orig->get_child("first"); child->set_attr ( "attr2", 2 ); CPPUNIT_ASSERT_THROW ( orig->get_child ("first")->get_attr ( "attrX" ), Exception ); CPPUNIT_ASSERT ( orig->get_child("first")->get_attr("attr2") == 2 ); { NodeRef second = Terminal::create("second"); second->set_attr("attr3",std::string("three")); orig->add_child(second); // second goes out of scope } CPPUNIT_ASSERT_NO_THROW ( orig->get_child("second")->get_attr("attr3") ); NodeRef second=orig->get_child("second"); CPPUNIT_ASSERT ( second->get_attr("attr3") == "three" ); CPPUNIT_ASSERT_THROW ( orig->get_child("childY"), Exception ); // child ref should still be good CPPUNIT_ASSERT_NO_THROW ( child->get_attr("attr1") ); }