Example #1
0
 /**
  * Tests that the method isParentOf() works as expected.
  */
 public function testIsParentOf()
 {
     $nodeA = new Node('A');
     // Is not a child
     $nodeB = new Node('B');
     // Is the child of A
     $nodeC = new Node('C');
     // Is the child of B
     $nodeD = new Node('D');
     // Is the child of C
     $nodeE = new Node('E');
     // Is the child of D
     $nodeF = new Node('F');
     // Is the child of B
     $nodeG = new Node('G');
     // Is the child of A
     $nodeA->addChild($nodeB)->addChild($nodeC)->addChild($nodeD);
     $nodeA->addChild($nodeG);
     $nodeD->addChild($nodeE);
     $nodeB->addChild($nodeF);
     $this->assertTrue($nodeA->isParentOf($nodeB));
     $this->assertTrue($nodeA->isParentOf($nodeG));
     $this->assertTrue($nodeB->isParentOf($nodeC));
     $this->assertTrue($nodeC->isParentOf($nodeD));
     $this->assertTrue($nodeD->isParentOf($nodeE));
     $this->assertTrue($nodeB->isParentOf($nodeF));
     $this->assertFalse($nodeB->isParentOf($nodeA));
     $this->assertFalse($nodeG->isParentOf($nodeA));
     $this->assertFalse($nodeC->isParentOf($nodeB));
     $this->assertFalse($nodeD->isParentOf($nodeC));
     $this->assertFalse($nodeE->isParentOf($nodeD));
     $this->assertFalse($nodeF->isParentOf($nodeB));
 }
Example #2
0
// ---------------------------------------------------------------------------------------------------------------------
// Testing ancestry
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $root->getData() . "> an ascendant of <" . $L->getData() . "> ? " . ($root->isAscendantOf($L) ? 'yes' : 'no') . "\n";
print "Is <" . $root->getData() . "> an ascendant of <" . $F->getData() . "> ? " . ($root->isAscendantOf($F) ? 'yes' : 'no') . "\n";
print "Is <" . $L->getData() . "> an ascendant of <" . $K->getData() . "> ? " . ($L->isAscendantOf($K) ? 'yes' : 'no') . "\n";
// ---------------------------------------------------------------------------------------------------------------------
// Testing  descendance
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $root->getData() . "> a descendant of <" . $L->getData() . "> ? " . ($root->isDescendantOf($L) ? 'yes' : 'no') . "\n";
print "Is <" . $root->getData() . "> a descendant of <" . $F->getData() . "> ? " . ($root->isDescendantOf($F) ? 'yes' : 'no') . "\n";
print "Is <" . $L->getData() . "> a descendant of <" . $K->getData() . "> ? " . ($L->isDescendantOf($K) ? 'yes' : 'no') . "\n";
// ---------------------------------------------------------------------------------------------------------------------
// Is a node the parent of another node ?
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $root->getData() . "> the parent of <" . $A->getData() . "> ? " . ($root->isParentOf($A) ? 'yes' : 'no') . "\n";
print "Is <" . $root->getData() . "> the parent of <" . $B->getData() . "> ? " . ($root->isParentOf($B) ? 'yes' : 'no') . "\n";
print "Is <" . $root->getData() . "> the parent of <" . $J->getData() . "> ? " . ($root->isParentOf($J) ? 'yes' : 'no') . "\n";
// ---------------------------------------------------------------------------------------------------------------------
// Is a node a child of another node ?
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $A->getData() . "> a child of <" . $root->getData() . "> ? " . ($A->isChildOf($root) ? 'yes' : 'no') . "\n";
print "Is <" . $C->getData() . "> a child of <" . $root->getData() . "> ? " . ($C->isChildOf($root) ? 'yes' : 'no') . "\n";
print "Is <" . $D->getData() . "> a child of <" . $C->getData() . "> ? " . ($A->isChildOf($C) ? 'yes' : 'no') . "\n";
print "Is <" . $L->getData() . "> a child of <" . $C->getData() . "> ? " . ($L->isChildOf($C) ? 'yes' : 'no') . "\n";
// ---------------------------------------------------------------------------------------------------------------------
// Test if sibling.
// ---------------------------------------------------------------------------------------------------------------------
print "Is <" . $A->getData() . "> a sibling of <" . $B->getData() . "> ? " . ($A->isSiblingWith($B) ? 'yes' : 'no') . "\n";
print "Is <" . $EE->getData() . "> a sibling of <" . $EEE->getData() . "> ? " . ($EE->isSiblingWith($EEE) ? 'yes' : 'no') . "\n";
// ---------------------------------------------------------------------------------------------------------------------