|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DataStructures; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../DataStructures/Trie/Trie.php'; |
| 6 | +require_once __DIR__ . '/../../DataStructures/Trie/TrieNode.php'; |
| 7 | + |
| 8 | +use DataStructures\Trie\Trie; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class TrieTest extends TestCase |
| 12 | +{ |
| 13 | + private Trie $trie; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + $this->trie = new Trie(); |
| 18 | + } |
| 19 | + |
| 20 | + public function testInsertAndSearch() |
| 21 | + { |
| 22 | + $this->trie->insert('the'); |
| 23 | + $this->trie->insert('universe'); |
| 24 | + $this->trie->insert('is'); |
| 25 | + $this->trie->insert('vast'); |
| 26 | + |
| 27 | + $this->assertTrue($this->trie->search('the'), 'Expected "the" to be found in the Trie.'); |
| 28 | + $this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found in the Trie.'); |
| 29 | + $this->assertTrue($this->trie->search('is'), 'Expected "is" to be found in the Trie.'); |
| 30 | + $this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found in the Trie.'); |
| 31 | + $this->assertFalse( |
| 32 | + $this->trie->search('the universe'), |
| 33 | + 'Expected "the universe" not to be found in the Trie.' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function testStartsWith() |
| 38 | + { |
| 39 | + $this->trie->insert('hello'); |
| 40 | + $this->assertEquals(['hello'], $this->trie->startsWith('he'), 'Expected words starting with "he" to be found.'); |
| 41 | + $this->assertEquals( |
| 42 | + ['hello'], |
| 43 | + $this->trie->startsWith('hello'), |
| 44 | + 'Expected words starting with "hello" to be found.' |
| 45 | + ); |
| 46 | + $this->assertEquals( |
| 47 | + [], |
| 48 | + $this->trie->startsWith('world'), |
| 49 | + 'Expected no words starting with "world" to be found.' |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testDelete() |
| 54 | + { |
| 55 | + // Insert words into the Trie |
| 56 | + $this->trie->insert('the'); |
| 57 | + $this->trie->insert('universe'); |
| 58 | + $this->trie->insert('is'); |
| 59 | + $this->trie->insert('vast'); |
| 60 | + $this->trie->insert('big'); |
| 61 | + $this->trie->insert('rather'); |
| 62 | + |
| 63 | + // Test deleting an existing word |
| 64 | + $this->trie->delete('the'); |
| 65 | + $this->assertFalse($this->trie->search('the'), 'Expected "the" not to be found after deletion.'); |
| 66 | + |
| 67 | + // Test that other words are still present |
| 68 | + $this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found.'); |
| 69 | + $this->assertTrue($this->trie->search('is'), 'Expected "is" to be found.'); |
| 70 | + $this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found.'); |
| 71 | + $this->assertTrue($this->trie->search('big'), 'Expected "big" to be found.'); |
| 72 | + $this->assertTrue($this->trie->search('rather'), 'Expected "rather" to be found.'); |
| 73 | + } |
| 74 | + |
| 75 | + public function testDeleteNonExistentWord() |
| 76 | + { |
| 77 | + $this->trie->delete('nonexistent'); |
| 78 | + $this->assertFalse($this->trie->search('nonexistent'), 'Expected "nonexistent" to not be found.'); |
| 79 | + } |
| 80 | + |
| 81 | + public function testTraverseTrieNode() |
| 82 | + { |
| 83 | + $this->trie->insert('hello'); |
| 84 | + $this->trie->insert('helium'); |
| 85 | + $this->trie->insert('helicopter'); |
| 86 | + |
| 87 | + $words = $this->trie->getWords(); |
| 88 | + $this->assertContains('hello', $words, 'Expected "hello" to be found in the Trie.'); |
| 89 | + $this->assertContains('helium', $words, 'Expected "helium" to be found in the Trie.'); |
| 90 | + $this->assertContains('helicopter', $words, 'Expected "helicopter" to be found in the Trie.'); |
| 91 | + $this->assertCount(3, $words, 'Expected 3 words in the Trie.'); |
| 92 | + } |
| 93 | + |
| 94 | + public function testEmptyTrie() |
| 95 | + { |
| 96 | + $this->assertEquals([], $this->trie->getWords(), 'Expected an empty Trie to return an empty array.'); |
| 97 | + } |
| 98 | + |
| 99 | + public function testGetWords() |
| 100 | + { |
| 101 | + $this->trie->insert('apple'); |
| 102 | + $this->trie->insert('app'); |
| 103 | + $this->trie->insert('applet'); |
| 104 | + |
| 105 | + $words = $this->trie->getWords(); |
| 106 | + $this->assertContains('apple', $words, 'Expected "apple" to be found in the Trie.'); |
| 107 | + $this->assertContains('app', $words, 'Expected "app" to be found in the Trie.'); |
| 108 | + $this->assertContains('applet', $words, 'Expected "applet" to be found in the Trie.'); |
| 109 | + $this->assertCount(3, $words, 'Expected 3 words in the Trie.'); |
| 110 | + } |
| 111 | + |
| 112 | + public function testInsertEmptyString() |
| 113 | + { |
| 114 | + $this->trie->insert(''); |
| 115 | + $this->assertTrue($this->trie->search(''), 'Expected empty string to be found in the Trie.'); |
| 116 | + } |
| 117 | + |
| 118 | + public function testDeleteEmptyString() |
| 119 | + { |
| 120 | + $this->trie->insert(''); |
| 121 | + $this->trie->delete(''); |
| 122 | + $this->assertFalse($this->trie->search(''), 'Expected empty string not to be found after deletion.'); |
| 123 | + } |
| 124 | + |
| 125 | + public function testStartsWithWithCommonPrefix() |
| 126 | + { |
| 127 | + $this->trie->insert('trie'); |
| 128 | + $this->trie->insert('tried'); |
| 129 | + $this->trie->insert('trier'); |
| 130 | + |
| 131 | + $words = $this->trie->startsWith('tri'); |
| 132 | + $this->assertContains('trie', $words, 'Expected "trie" to be found with prefix "tri".'); |
| 133 | + $this->assertContains('tried', $words, 'Expected "tried" to be found with prefix "tri".'); |
| 134 | + $this->assertContains('trier', $words, 'Expected "trier" to be found with prefix "tri".'); |
| 135 | + $this->assertCount(3, $words, 'Expected 3 words with prefix "tri".'); |
| 136 | + } |
| 137 | +} |
0 commit comments