forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrieTest.php
302 lines (265 loc) · 10.7 KB
/
TrieTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request #162 and #172
* https://github.com/TheAlgorithms/PHP/pull/162
* https://github.com/TheAlgorithms/PHP/pull/172
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures;
require_once __DIR__ . '/../../DataStructures/Trie/Trie.php';
require_once __DIR__ . '/../../DataStructures/Trie/TrieNode.php';
use DataStructures\Trie\Trie;
use DataStructures\Trie\TrieNode;
use PHPUnit\Framework\TestCase;
class TrieTest extends TestCase
{
private Trie $trie;
protected function setUp(): void
{
$this->trie = new Trie();
}
/**
* Test insertion and search functionality of the Trie.
*/
public function testInsertAndSearch()
{
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');
$this->assertTrue($this->trie->search('the'), 'Expected "the" to be found in the Trie.');
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found in the Trie.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found in the Trie.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found in the Trie.');
$this->assertFalse(
$this->trie->search('the universe'),
'Expected "the universe" not to be found in the Trie.'
);
}
/**
* Test insertion and search functionality with mixed case words.
*/
public function testInsertAndSearchMixedCase()
{
$this->trie->insert('Apple');
$this->trie->insert('aPPle');
$this->assertTrue($this->trie->search('apple'), 'Expected "apple" to be found in the Trie.');
$this->assertTrue($this->trie->search('APPLE'), 'Expected "APPLE" to be found in the Trie.');
}
/**
* Test insertion and search functionality with special characters.
*/
public function testInsertAndSearchWithSpecialCharacters()
{
$this->trie->insert('hello123');
$this->trie->insert('user@domain.com');
$this->assertTrue($this->trie->search('hello123'), 'Expected "hello123" to be found in the Trie.');
$this->assertTrue(
$this->trie->search('UseR@domain.CoM'),
'Expected "user@domain.com" to be found in the Trie.'
);
$this->assertTrue(
$this->trie->search('HELLO123'),
'Expected "HELLO123" not to be found in the Trie (case-sensitive).'
);
}
/**
* Test insertion and search functionality with long strings.
*/
public function testInsertAndSearchLongStrings()
{
$longString = str_repeat('a', 1000);
$this->trie->insert($longString);
$this->assertTrue($this->trie->search($longString), 'Expected the long string to be found in the Trie.');
}
/**
* Test the startsWith functionality of the Trie.
*/
public function testStartsWith()
{
$this->trie->insert('hello');
$this->assertEquals(['hello'], $this->trie->startsWith('he'), 'Expected words starting with "he" to be found.');
$this->assertEquals(
['hello'],
$this->trie->startsWith('hello'),
'Expected words starting with "hello" to be found.'
);
$this->assertEquals(
[],
$this->trie->startsWith('world'),
'Expected no words starting with "world" to be found.'
);
}
/**
* Test startsWith functionality with mixed case prefixes.
*/
public function testStartsWithMixedCase()
{
$this->trie->insert('PrefixMatch');
$this->trie->insert('PreFixTesting');
$this->assertEquals(
['prefixmatch', 'prefixtesting'],
$this->trie->startsWith('prefix'),
'Expected words starting with "prefix" to be found in the Trie (case-insensitive).'
);
$this->assertEquals(
['prefixmatch', 'prefixtesting'],
$this->trie->startsWith('PREFIX'),
'Expected words starting with "PREFIX" to be found in the Trie (case-insensitive).'
);
}
/**
* Test deletion of existing words from the Trie.
*/
public function testDelete()
{
$this->trie->insert('the');
$this->trie->insert('universe');
$this->trie->insert('is');
$this->trie->insert('vast');
$this->trie->insert('big');
$this->trie->insert('rather');
// Test deleting an existing word
$this->trie->delete('the');
$this->assertFalse($this->trie->search('the'), 'Expected "the" not to be found after deletion.');
// Test that other words are still present
$this->assertTrue($this->trie->search('universe'), 'Expected "universe" to be found.');
$this->assertTrue($this->trie->search('is'), 'Expected "is" to be found.');
$this->assertTrue($this->trie->search('vast'), 'Expected "vast" to be found.');
$this->assertTrue($this->trie->search('big'), 'Expected "big" to be found.');
$this->assertTrue($this->trie->search('rather'), 'Expected "rather" to be found.');
}
/**
* Test deletion of mixed case words from the Trie.
*/
public function testDeleteMixedCase()
{
$this->trie->insert('MixedCase');
$this->assertTrue($this->trie->search('mixedcase'), 'Expected "mixedcase" to be found before deletion.');
$this->trie->delete('MIXEDCASE');
$this->assertFalse(
$this->trie->search('MixedCase'),
'Expected "MixedCase" not to be found after deletion (case-insensitive).'
);
}
/**
* Test deletion of words with special characters.
*/
public function testDeleteWithSpecialCharacters()
{
$this->trie->insert('spec!@l#chars');
$this->assertTrue(
$this->trie->search('spec!@l#chars'),
'Expected "spec!@l#chars" to be found before deletion.'
);
$this->trie->delete('SPEC!@L#CHARS');
$this->assertFalse(
$this->trie->search('spec!@l#chars'),
'Expected "spec!@l#chars" not to be found after deletion.'
);
}
/**
* Test deletion of a non-existent word from the Trie.
*/
public function testDeleteNonExistentWord()
{
$this->trie->delete('nonexistent');
$this->assertFalse($this->trie->search('nonexistent'), 'Expected "nonexistent" to not be found.');
}
/**
* Test traversal of the Trie and retrieval of words.
*/
public function testTraverseTrieNode()
{
$this->trie->insert('hello');
$this->trie->insert('helium');
$this->trie->insert('helicopter');
$words = $this->trie->getWords();
$this->assertContains('hello', $words, 'Expected "hello" to be found in the Trie.');
$this->assertContains('helium', $words, 'Expected "helium" to be found in the Trie.');
$this->assertContains('helicopter', $words, 'Expected "helicopter" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}
/**
* Test behavior of an empty Trie.
*/
public function testEmptyTrie()
{
$this->assertEquals([], $this->trie->getWords(), 'Expected an empty Trie to return an empty array.');
}
/**
* Test retrieval of words from the Trie.
*/
public function testGetWords()
{
$this->trie->insert('apple');
$this->trie->insert('app');
$this->trie->insert('applet');
$words = $this->trie->getWords();
$this->assertContains('apple', $words, 'Expected "apple" to be found in the Trie.');
$this->assertContains('app', $words, 'Expected "app" to be found in the Trie.');
$this->assertContains('applet', $words, 'Expected "applet" to be found in the Trie.');
$this->assertCount(3, $words, 'Expected 3 words in the Trie.');
}
/**
* Test insertion of an empty string into the Trie.
*/
public function testInsertEmptyString()
{
$this->trie->insert('');
$this->assertTrue($this->trie->search(''), 'Expected empty string to be found in the Trie.');
}
/**
* Test deletion of an empty string from the Trie.
*/
public function testDeleteEmptyString()
{
$this->trie->insert('');
$this->trie->delete('');
$this->assertFalse($this->trie->search(''), 'Expected empty string not to be found after deletion.');
}
/**
* Test the startsWith functionality with a common prefix.
*/
public function testStartsWithWithCommonPrefix()
{
$this->trie->insert('trie');
$this->trie->insert('tried');
$this->trie->insert('trier');
$words = $this->trie->startsWith('tri');
$this->assertContains('trie', $words, 'Expected "trie" to be found with prefix "tri".');
$this->assertContains('tried', $words, 'Expected "tried" to be found with prefix "tri".');
$this->assertContains('trier', $words, 'Expected "trier" to be found with prefix "tri".');
$this->assertCount(3, $words, 'Expected 3 words with prefix "tri".');
}
/**
* Test retrieval of the root node of the Trie.
*/
public function testGetRoot()
{
$root = $this->trie->getRoot();
$this->assertInstanceOf(TrieNode::class, $root, 'Expected root to be an instance of TrieNode.');
$this->assertFalse($root->isEndOfWord, 'Expected the root node not to be the end of a word.');
$this->assertCount(0, $root->children, 'Expected the root node to have no children initially.');
}
/**
* Test retrieval of the root node after populating the Trie with words.
*/
public function testGetRootAfterPopulation()
{
$this->trie->insert('TheAlgorithms');
$this->trie->insert('PHP');
$this->trie->insert('DSA');
$root = $this->trie->getRoot();
$this->assertInstanceOf(TrieNode::class, $root, 'Expected root to be an instance of TrieNode.');
// Assert that the root node is not marked as the end of a word
$this->assertFalse($root->isEndOfWord, 'Expected the root node not to be the end of a word.');
// Assert that the root node has children corresponding to the inserted words
$this->assertCount(3, $root->children, 'Expected the root node to have 3 children after inserting words.');
$this->assertTrue($root->hasChild('t'), 'Expected root to have a child for "t".');
$this->assertTrue($root->hasChild('p'), 'Expected root to have a child for "p".');
$this->assertTrue($root->hasChild('D'), 'Expected root to have a child for "D".');
}
}