Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
8 | class TrieTreeTest extends TestCase { |
||
9 | private $tree; |
||
10 | |||
11 | public function setUp() { |
||
12 | $this->tree = new TrieTree(); |
||
13 | } |
||
14 | |||
15 | public function testAdd() { |
||
16 | $this->tree->add('hello'); |
||
17 | $this->tree->add('bye'); |
||
18 | $this->assertEquals(8, $this->tree->size()); |
||
19 | $this->tree->add('hello'); |
||
20 | $this->assertEquals(8, $this->tree->size()); |
||
21 | $this->tree->add('hell'); |
||
22 | $this->assertEquals(8, $this->tree->size()); |
||
23 | } |
||
24 | |||
25 | public function testWordCount() { |
||
26 | $this->tree->add('hello'); |
||
27 | $this->assertEquals(1, $this->tree->wordCount()); |
||
28 | $this->tree->add('bye'); |
||
29 | $this->assertEquals(2, $this->tree->wordCount()); |
||
30 | $this->tree->add('hello'); |
||
31 | $this->assertEquals(2, $this->tree->wordCount()); |
||
32 | $this->tree->add('hell'); |
||
33 | $this->assertEquals(3, $this->tree->wordCount()); |
||
34 | } |
||
35 | |||
36 | public function testContains() { |
||
37 | $this->tree->add('hello'); |
||
38 | $this->assertTrue($this->tree->contains('hello')); |
||
39 | $this->tree->add('bye'); |
||
40 | $this->assertTrue($this->tree->contains('bye')); |
||
41 | $this->assertFalse($this->tree->contains('what')); |
||
42 | } |
||
43 | |||
44 | public function testStartsWith() { |
||
45 | $this->assertFalse($this->tree->startsWith('hello')); |
||
46 | $this->tree->add('hello'); |
||
47 | $this->tree->add('bye'); |
||
48 | $this->assertTrue($this->tree->startsWith('b')); |
||
49 | $this->assertTrue($this->tree->startsWith('hel')); |
||
50 | $this->assertFalse($this->tree->startsWith('hellooo')); |
||
51 | } |
||
52 | |||
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 | } |