|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use KaririCode\DataStructure\Tree\TreeNode; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +final class TreeNodeTest extends TestCase |
| 9 | +{ |
| 10 | + public function testCreateNode(): void |
| 11 | + { |
| 12 | + $node = new TreeNode(1, 'valor1'); |
| 13 | + |
| 14 | + $this->assertSame(1, $node->key); |
| 15 | + $this->assertSame('valor1', $node->value); |
| 16 | + $this->assertNull($node->left); |
| 17 | + $this->assertNull($node->right); |
| 18 | + $this->assertNull($node->parent); |
| 19 | + } |
| 20 | + |
| 21 | + public function testSetLeftChild(): void |
| 22 | + { |
| 23 | + $parent = new TreeNode(1, 'parent'); |
| 24 | + $child = new TreeNode(2, 'child'); |
| 25 | + |
| 26 | + $parent->setLeft($child); |
| 27 | + |
| 28 | + $this->assertSame($child, $parent->left); |
| 29 | + $this->assertSame($parent, $child->parent); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSetRightChild(): void |
| 33 | + { |
| 34 | + $parent = new TreeNode(1, 'parent'); |
| 35 | + $child = new TreeNode(2, 'child'); |
| 36 | + |
| 37 | + $parent->setRight($child); |
| 38 | + |
| 39 | + $this->assertSame($child, $parent->right); |
| 40 | + $this->assertSame($parent, $child->parent); |
| 41 | + } |
| 42 | + |
| 43 | + public function testRemoveFromParent(): void |
| 44 | + { |
| 45 | + $parent = new TreeNode(1, 'parent'); |
| 46 | + $child = new TreeNode(2, 'child'); |
| 47 | + |
| 48 | + $parent->setLeft($child); |
| 49 | + $child->removeFromParent(); |
| 50 | + |
| 51 | + $this->assertNull($parent->left); |
| 52 | + $this->assertNull($child->parent); |
| 53 | + } |
| 54 | + |
| 55 | + public function testReplaceWith(): void |
| 56 | + { |
| 57 | + $parent = new TreeNode(1, 'parent'); |
| 58 | + $child = new TreeNode(2, 'child'); |
| 59 | + $replacement = new TreeNode(3, 'replacement'); |
| 60 | + |
| 61 | + $parent->setLeft($child); |
| 62 | + $child->replaceWith($replacement); |
| 63 | + |
| 64 | + $this->assertSame($replacement, $parent->left); |
| 65 | + $this->assertSame($parent, $replacement->parent); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSetRightAndLeftTogether(): void |
| 69 | + { |
| 70 | + $parent = new TreeNode(1, 'parent'); |
| 71 | + $leftChild = new TreeNode(2, 'left'); |
| 72 | + $rightChild = new TreeNode(3, 'right'); |
| 73 | + |
| 74 | + $parent->setLeft($leftChild); |
| 75 | + $parent->setRight($rightChild); |
| 76 | + |
| 77 | + $this->assertSame($leftChild, $parent->left); |
| 78 | + $this->assertSame($rightChild, $parent->right); |
| 79 | + $this->assertSame($parent, $leftChild->parent); |
| 80 | + $this->assertSame($parent, $rightChild->parent); |
| 81 | + } |
| 82 | + |
| 83 | + public function testReplaceWithThrowsExceptionWhenNodeIsRoot(): void |
| 84 | + { |
| 85 | + $this->expectException(RuntimeException::class); |
| 86 | + $this->expectExceptionMessage('Cannot replace root node'); |
| 87 | + |
| 88 | + $root = new TreeNode(1, 'root'); |
| 89 | + $replacement = new TreeNode(2, 'replacement'); |
| 90 | + |
| 91 | + $root->replaceWith($replacement); |
| 92 | + } |
| 93 | + |
| 94 | + public function testRemoveFromParentRightChild(): void |
| 95 | + { |
| 96 | + $parent = new TreeNode(1, 'parent'); |
| 97 | + $child = new TreeNode(2, 'child'); |
| 98 | + |
| 99 | + $parent->setRight($child); |
| 100 | + $child->removeFromParent(); |
| 101 | + |
| 102 | + $this->assertNull($parent->right); |
| 103 | + $this->assertNull($child->parent); |
| 104 | + } |
| 105 | + |
| 106 | + public function testReplaceWithRightChild(): void |
| 107 | + { |
| 108 | + $parent = new TreeNode(1, 'parent'); |
| 109 | + $child = new TreeNode(2, 'child'); |
| 110 | + $replacement = new TreeNode(3, 'replacement'); |
| 111 | + |
| 112 | + $parent->setRight($child); |
| 113 | + $child->replaceWith($replacement); |
| 114 | + |
| 115 | + $this->assertSame($replacement, $parent->right); |
| 116 | + $this->assertSame($parent, $replacement->parent); |
| 117 | + } |
| 118 | +} |
0 commit comments