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->withPrefix('he'); |
58
|
|
|
$this->assertEquals(3, $this->tree->wordCount()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testDelete() { |
|
|
|
|
62
|
|
|
$this->tree->add('hellou'); |
63
|
|
|
$this->tree->add('hell'); |
64
|
|
|
$this->tree->add('yellow'); |
65
|
|
|
$this->tree->delete('hellou'); |
66
|
|
|
$this->assertEquals(2, $this->tree->wordCount()); |
67
|
|
|
$this->assertFalse($this->tree->contains('hellou')); |
68
|
|
|
$this->assertTrue($this->tree->contains('hell')); |
69
|
|
|
$this->assertTrue($this->tree->contains('yellow')); |
70
|
|
|
$this->tree->delete('yellow'); |
71
|
|
|
$this->assertEquals(4, $this->tree->size()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function testClear() { |
|
|
|
|
75
|
|
|
$this->tree->add('hellou'); |
76
|
|
|
$this->tree->add('hell'); |
77
|
|
|
$this->tree->add('yellow'); |
78
|
|
|
$this->assertEquals(3, $this->tree->wordCount()); |
79
|
|
|
$this->tree->clear(); |
80
|
|
|
var_dump($this->tree); |
|
|
|
|
81
|
|
|
$this->assertFalse($this->tree->contains('hellou')); |
82
|
|
|
$this->assertFalse($this->tree->contains('hell')); |
83
|
|
|
$this->assertFalse($this->tree->contains('yellow')); |
84
|
|
|
$this->assertEquals(0, $this->tree->size()); |
85
|
|
|
} |
86
|
|
|
} |
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.