|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
use DataStructures\Trees\TrieTree; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class TrieTreeTest extends TestCase { |
|
|
|
|
|
|
8
|
|
|
private $tree; |
|
9
|
|
|
|
|
10
|
|
|
public function setUp() { |
|
11
|
|
|
$this->tree = new TrieTree(); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
public function testAdd() { |
|
|
|
|
|
|
15
|
|
|
$this->tree->add('hello'); |
|
16
|
|
|
$this->tree->add('bye'); |
|
17
|
|
|
$this->assertEquals(8, $this->tree->size()); |
|
18
|
|
|
$this->tree->add('hello'); |
|
19
|
|
|
$this->assertEquals(8, $this->tree->size()); |
|
20
|
|
|
$this->tree->add('hell'); |
|
21
|
|
|
$this->assertEquals(8, $this->tree->size()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public function testWordCount() { |
|
|
|
|
|
|
25
|
|
|
$this->tree->add('hello'); |
|
26
|
|
|
$this->assertEquals(1, $this->tree->wordCount()); |
|
27
|
|
|
$this->tree->add('bye'); |
|
28
|
|
|
$this->assertEquals(2, $this->tree->wordCount()); |
|
29
|
|
|
$this->tree->add('hello'); |
|
30
|
|
|
$this->assertEquals(2, $this->tree->wordCount()); |
|
31
|
|
|
$this->tree->add('hell'); |
|
32
|
|
|
$this->assertEquals(3, $this->tree->wordCount()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testContains() { |
|
36
|
|
|
$this->tree->add('hello'); |
|
37
|
|
|
$this->assertTrue($this->tree->contains('hello')); |
|
38
|
|
|
$this->tree->add('bye'); |
|
39
|
|
|
$this->assertTrue($this->tree->contains('bye')); |
|
40
|
|
|
$this->assertFalse($this->tree->contains('what')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testStartsWith() { |
|
44
|
|
|
$this->assertFalse($this->tree->startsWith('hello')); |
|
45
|
|
|
$this->tree->add('hello'); |
|
46
|
|
|
$this->tree->add('bye'); |
|
47
|
|
|
$this->assertTrue($this->tree->startsWith('b')); |
|
48
|
|
|
$this->assertTrue($this->tree->startsWith('hel')); |
|
49
|
|
|
$this->assertFalse($this->tree->startsWith('hellooo')); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
//TODO |
|
53
|
|
|
public function testWithPrefix() { |
|
54
|
|
|
$this->tree->add('hello'); |
|
55
|
|
|
$this->tree->add('hell'); |
|
56
|
|
|
$this->tree->add('bye'); |
|
57
|
|
|
$this->tree->add('beyond'); |
|
58
|
|
|
$withH = $this->tree->withPrefix('he'); |
|
59
|
|
|
$withB = $this->tree->withPrefix('b'); |
|
60
|
|
|
$withBy = $this->tree->withPrefix('by'); |
|
61
|
|
|
$all = $this->tree->withPrefix(''); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertSame(['hell', 'hello'], $withH); |
|
64
|
|
|
$this->assertSame(['bye', 'beyond'], $withB); |
|
65
|
|
|
$this->assertSame(['bye'], $withBy); |
|
66
|
|
|
$this->assertSame(['hell', 'hello', 'bye', 'beyond'], $all); |
|
67
|
|
|
$this->assertEquals(4, $this->tree->wordCount()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testDelete() { |
|
71
|
|
|
$this->tree->add('hellou'); |
|
72
|
|
|
$this->tree->add('hell'); |
|
73
|
|
|
$this->tree->add('yellow'); |
|
74
|
|
|
$this->tree->delete('hellou'); |
|
75
|
|
|
$this->assertEquals(2, $this->tree->wordCount()); |
|
76
|
|
|
$this->assertFalse($this->tree->contains('hellou')); |
|
77
|
|
|
$this->assertTrue($this->tree->contains('hell')); |
|
78
|
|
|
$this->assertTrue($this->tree->contains('yellow')); |
|
79
|
|
|
$this->tree->delete('yellow'); |
|
80
|
|
|
$this->assertEquals(4, $this->tree->size()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testClear() { |
|
84
|
|
|
$this->tree->add('hellou'); |
|
85
|
|
|
$this->tree->add('hell'); |
|
86
|
|
|
$this->tree->add('yellow'); |
|
87
|
|
|
$this->assertEquals(3, $this->tree->wordCount()); |
|
88
|
|
|
$this->tree->clear(); |
|
89
|
|
|
$this->assertFalse($this->tree->contains('hellou')); |
|
90
|
|
|
$this->assertFalse($this->tree->contains('hell')); |
|
91
|
|
|
$this->assertFalse($this->tree->contains('yellow')); |
|
92
|
|
|
$this->assertEquals(0, $this->tree->size()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testGetWords() { |
|
96
|
|
|
$this->assertSame([], $this->tree->getWords()); |
|
97
|
|
|
$this->tree->add('hello'); |
|
98
|
|
|
$this->tree->add('hell'); |
|
99
|
|
|
$this->tree->add('yellow'); |
|
100
|
|
|
$this->tree->add(''); |
|
101
|
|
|
$this->assertEquals(3, $this->tree->wordCount()); |
|
102
|
|
|
$this->assertSame(['hell', 'hello', 'yellow'], $this->tree->getWords()); |
|
103
|
|
|
$this->tree->delete('hello'); |
|
104
|
|
|
$this->assertSame(['hell', 'yellow'], $this->tree->getWords()); |
|
105
|
|
|
$this->tree->delete('hell'); |
|
106
|
|
|
$this->assertSame(['yellow'], $this->tree->getWords()); |
|
107
|
|
|
$this->tree->delete('yellow'); |
|
108
|
|
|
$this->assertSame([], $this->tree->getWords()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.