Пример #1
0
        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) ); 
        }
Пример #2
0
        void testCopy() {

            NodeRef orig = DeviceInterface::create ( "orig" );
            orig->add_child(Terminal::create("child1"));
            orig->add_child(Terminal::create("child2"));
            NodeRef copy(orig);
            
            CPPUNIT_ASSERT ( copy->has_children() );
            CPPUNIT_ASSERT ( std::string("child1") == copy->get_child("child1")->get_name() );
            CPPUNIT_ASSERT ( std::string("child2") == copy->get_child("child2")->get_name() );
            CPPUNIT_ASSERT_THROW ( copy->get_child("child3") , Exception );

            NodeRef orig2 = DeviceInterface::create("orig");
            orig2->add_child(Terminal::create("child3"));
            orig2->add_child(Terminal::create("child4"));
            CPPUNIT_ASSERT_THROW ( orig2->add_child(Terminal::create("child3")), Exception );
            copy = orig2;
            
            CPPUNIT_ASSERT ( copy->has_children() );
            CPPUNIT_ASSERT_THROW ( copy->get_child("child1"), Exception );
            CPPUNIT_ASSERT_EQUAL ( std::string("child3") , copy->get_child("child3")->get_name() );

            int children=0;
            for (DITreeIter i = copy->child_begin(); i != copy->child_end(); ++i ) {
               children += 1; 
            }
            CPPUNIT_ASSERT ( children == 2 );
        };
Пример #3
0
 void testRefs() {
     NodeRef orig = DeviceInterface::create("top");
     orig->add_child(Terminal::create("child1"));
     NodeRef child2 = Register::create("child2");
     CPPUNIT_ASSERT_NO_THROW(orig->get_child ( "child1")->add_child(child2));
     child2->set_attr("test", "hello" );
     CPPUNIT_ASSERT_NO_THROW(orig->get_child ( "child1")->get_child("child2")->get_attr("test"));
     CPPUNIT_ASSERT_EQUAL ( std::string("hello"), (std::string)orig->get_child("child1")->get_child("child2")->get_attr("test"));
 }
Пример #4
0
 void testErase() {
    NodeRef node = Node::create("Test");
    NodeRef child1 = Node::create("child1");
    NodeRef child2 = Node::create("child2");
    node->add_child(child1);
    node->add_child(child2);
    CPPUNIT_ASSERT_NO_THROW( node->del_child("child2") );
    CPPUNIT_ASSERT_EQUAL ( 1, (int) node->num_children() );
    CPPUNIT_ASSERT_THROW ( node->del_child ( "child2" ), Exception ); 
 }
Пример #5
0
        void testNameChange() {
            NodeRef p = Node::create("Parent");
            NodeRef c = Node::create("child");

            p->add_child( c );

            c->set_name ( "child1" );
            
            CPPUNIT_ASSERT_THROW ( p->get_child ( "child" ), Exception );
            CPPUNIT_ASSERT_NO_THROW ( p->get_child ( "child1" ) );


        }
Пример #6
0
 void testOrder() {
     NodeRef orig = DeviceInterface::create("top");
     for (int i=10;i>=0;--i) {
         std::stringstream io;
         std::string name;
         io << '_';
         io << i;
         io >> name;
         NodeRef child = Terminal::create(name);
         orig->add_child(child);
     }
     // nodes are ordered 
     int i=10;
     for (DITreeIter itr=orig->child_begin();itr!=orig->child_end();++itr) {
        NodeRef c =*itr; 
        std::stringstream io;
        std::string name;      
        io << '_';
        io << i--;
        io >> name;
        CPPUNIT_ASSERT_EQUAL ( name, c->get_name() ); 
     }
 }
Пример #7
0
        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") ); 

        }