/** * Tests that the method isAscendantOf() works as expected. */ public function testIsAscendantOf() { $nodeA = new Node('A'); // Is parent of B, C, D, E, F $nodeB = new Node('B'); // Is parent of C, D, E, F $nodeC = new Node('C'); // Is parent of D $nodeD = new Node('D'); // Is parent of E $nodeE = new Node('E'); // Is not a parent $nodeF = new Node('F'); // Is not a parent $nodeA->addChild($nodeB)->addChild($nodeC)->addChild($nodeD); $nodeD->addChild($nodeE); $nodeB->addChild($nodeF); // Is parent of... $this->assertTrue($nodeA->isAscendantOf($nodeB)); $this->assertTrue($nodeA->isAscendantOf($nodeC)); $this->assertTrue($nodeA->isAscendantOf($nodeD)); $this->assertTrue($nodeA->isAscendantOf($nodeE)); $this->assertTrue($nodeA->isAscendantOf($nodeF)); $this->assertTrue($nodeB->isAscendantOf($nodeC)); $this->assertTrue($nodeB->isAscendantOf($nodeD)); $this->assertTrue($nodeB->isAscendantOf($nodeE)); $this->assertTrue($nodeB->isAscendantOf($nodeF)); $this->assertTrue($nodeC->isAscendantOf($nodeD)); $this->assertTrue($nodeD->isAscendantOf($nodeE)); // Is not parent of... $this->assertFalse($nodeA->isAscendantOf($nodeA)); $this->assertFalse($nodeB->isAscendantOf($nodeB)); $this->assertFalse($nodeC->isAscendantOf($nodeC)); $this->assertFalse($nodeD->isAscendantOf($nodeD)); $this->assertFalse($nodeE->isAscendantOf($nodeE)); $this->assertFalse($nodeF->isAscendantOf($nodeF)); $this->assertFalse($nodeE->isAscendantOf($nodeA)); $this->assertFalse($nodeE->isAscendantOf($nodeB)); $this->assertFalse($nodeE->isAscendantOf($nodeC)); $this->assertFalse($nodeE->isAscendantOf($nodeD)); $this->assertFalse($nodeE->isAscendantOf($nodeF)); $this->assertFalse($nodeF->isAscendantOf($nodeA)); $this->assertFalse($nodeF->isAscendantOf($nodeB)); $this->assertFalse($nodeF->isAscendantOf($nodeC)); $this->assertFalse($nodeF->isAscendantOf($nodeD)); $this->assertFalse($nodeF->isAscendantOf($nodeE)); // ... It's OK. We could generate all the possible combinations... but it is not necessary. }
$D = new Node("D"); $E = new Node("E"); $EE = new Node("EE"); $EEE = new Node("EEE"); $F = new Node("F"); $H = new Node("H"); $I = new Node("I"); $J = new Node("J"); $K = new Node("K"); $L = new Node("L"); $tree = new Tree($root); $tree->getRoot()->addChild($A)->end()->addChild($B)->end()->addChild($C)->addChild($D)->addChild($E)->addChild($EE)->end()->addChild($EEE)->end()->end()->addChild($F)->end()->end()->addChild($AA)->addChild($H)->addChild($I)->addChild($J)->addChild($K)->end()->addChild($L)->end()->end()->end()->end()->end()->end(); // --------------------------------------------------------------------------------------------------------------------- // 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"; // ---------------------------------------------------------------------------------------------------------------------