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 AVLTreeTest extends TestCase { |
||
| 9 | private $tree; |
||
| 10 | |||
| 11 | public function setUp() { |
||
| 12 | $this->tree = new AVLTree(); |
||
| 13 | } |
||
| 14 | |||
| 15 | public function testPut() { |
||
| 16 | $this->tree->put(24, "Siro"); |
||
| 17 | $this->tree->put(19, "Clara"); |
||
| 18 | $this->tree->put(51, "Elisa"); |
||
| 19 | $this->assertEquals(3, $this->tree->size()); |
||
| 20 | $this->assertEquals("Siro", $this->tree->get(24)); |
||
| 21 | $this->assertEquals("Clara", $this->tree->get(19)); |
||
| 22 | $this->assertEquals("Elisa", $this->tree->get(51)); |
||
| 23 | $this->assertNull($this->tree->get(100)); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testPutWithUpdate() { |
||
| 27 | $this->tree->put(24, "Siro"); |
||
| 28 | $this->tree->put(19, "Clara"); |
||
| 29 | $this->tree->put(51, "Elisa"); |
||
| 30 | $this->assertEquals(3, $this->tree->size()); |
||
| 31 | $this->tree->put(51, "Pepe", true); |
||
| 32 | $this->tree->put(24, "John", true); |
||
| 33 | $this->assertEquals("John", $this->tree->get(24)); |
||
| 34 | $this->assertEquals("Clara", $this->tree->get(19)); |
||
| 35 | $this->assertEquals("Pepe", $this->tree->get(51)); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function testPutOrUpdate() { |
||
| 39 | $this->tree->put(24, "Siro"); |
||
| 40 | $this->tree->put(19, "Clara"); |
||
| 41 | $this->tree->put(51, "Elisa"); |
||
| 42 | $this->assertEquals(3, $this->tree->size()); |
||
| 43 | $this->tree->putOrUpdate(24, "John", true); |
||
| 44 | $this->assertEquals(3, $this->tree->size()); |
||
| 45 | $this->tree->putOrUpdate(34, "Ana"); |
||
| 46 | $this->assertEquals(4, $this->tree->size()); |
||
| 47 | $this->assertEquals("John", $this->tree->get(24)); |
||
| 48 | $this->assertEquals("Clara", $this->tree->get(19)); |
||
| 49 | $this->assertEquals("Elisa", $this->tree->get(51)); |
||
| 50 | $this->assertEquals("Ana", $this->tree->get(34)); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testEmpty() { |
||
| 54 | $this->assertTrue($this->tree->empty()); |
||
| 55 | $this->tree->put(24, "Siro"); |
||
| 56 | $this->tree->put(19, "Clara"); |
||
| 57 | $this->tree->put(51, "Elisa"); |
||
| 58 | $this->assertFalse($this->tree->empty()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testSize() { |
||
| 62 | $this->assertEquals(0, $this->tree->size()); |
||
| 63 | $this->tree->put(24, "Siro"); |
||
| 64 | $this->assertEquals(1, $this->tree->size()); |
||
| 65 | $this->tree->put(19, "Clara"); |
||
| 66 | $this->assertEquals(2, $this->tree->size()); |
||
| 67 | $this->tree->put(51, "Elisa"); |
||
| 68 | $this->assertEquals(3, $this->tree->size()); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function testExists() { |
||
| 72 | $this->assertFalse($this->tree->exists("greet")); |
||
| 73 | $this->tree->put("greet", "saludo"); |
||
| 74 | $this->tree->put("tree", "árbol"); |
||
| 75 | $this->tree->put("water", "agua"); |
||
| 76 | $this->assertTrue($this->tree->exists("greet")); |
||
| 77 | $this->assertTrue($this->tree->exists("tree")); |
||
| 78 | $this->assertTrue($this->tree->exists("water")); |
||
| 79 | $this->assertFalse($this->tree->exists("sun")); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function testSearch() { |
||
| 83 | $this->tree->put("greet", "saludo"); |
||
| 84 | $this->tree->put("tree", "árbol"); |
||
| 85 | $this->tree->put("water", "agua"); |
||
| 86 | $this->assertEquals("greet", $this->tree->search("greet")->key); |
||
| 87 | $this->assertEquals("árbol", $this->tree->search("tree")->data); |
||
| 88 | $this->assertEquals("water", $this->tree->search("water")->key); |
||
| 89 | $this->assertNull($this->tree->search(100)); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testMin() { |
||
| 93 | $this->tree->put(2, "two"); |
||
| 94 | $this->tree->put(3, "three"); |
||
| 95 | $this->assertEquals(2, $this->tree->min()->key); |
||
| 96 | $this->tree->put(1, "one"); |
||
| 97 | $this->assertEquals(1, $this->tree->min()->key); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testMax() { |
||
| 101 | $this->tree->put(1, "one"); |
||
| 102 | $this->assertEquals(1, $this->tree->max()->key); |
||
| 103 | $this->tree->put(2, "two"); |
||
| 104 | $this->assertEquals(2, $this->tree->max()->key); |
||
| 105 | $this->tree->put(3, "three"); |
||
| 106 | $this->assertEquals(3, $this->tree->max()->key); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testDelete() { |
||
| 110 | $this->tree->put(1, "one"); |
||
| 111 | $this->tree->put(2, "two"); |
||
| 112 | $this->tree->put(3, "three"); |
||
| 113 | $this->assertEquals(1, $this->tree->delete(1)->key); |
||
| 114 | $this->assertEquals(2, $this->tree->size()); |
||
| 115 | $this->assertEquals("three", $this->tree->delete(3)->data); |
||
| 116 | $this->assertEquals(1, $this->tree->size()); |
||
| 117 | $this->assertEquals(2, $this->tree->delete(2)->key); |
||
| 118 | $this->assertEquals(0, $this->tree->size()); |
||
| 119 | $this->assertNull($this->tree->delete(2)); |
||
| 120 | $this->assertTrue($this->tree->empty()); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function testDeleteMin() { |
||
| 124 | $this->tree->put(1, "one"); |
||
| 125 | $this->tree->put(2, "two"); |
||
| 126 | $this->tree->put(3, "three"); |
||
| 127 | $this->assertEquals(1, $this->tree->deleteMin()->key); |
||
| 128 | $this->assertEquals(2, $this->tree->size()); |
||
| 129 | $this->assertEquals(2, $this->tree->deleteMin()->key); |
||
| 130 | $this->assertEquals(1, $this->tree->size()); |
||
| 131 | $this->tree->put(100, "hundred"); |
||
| 132 | $this->assertEquals(2, $this->tree->size()); |
||
| 133 | $this->assertEquals(3, $this->tree->deleteMin()->key); |
||
| 134 | $this->assertEquals(1, $this->tree->size()); |
||
| 135 | $this->assertEquals(100, $this->tree->deleteMin()->key); |
||
| 136 | $this->assertEquals(0, $this->tree->size()); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testDeleteMax() { |
||
| 140 | $this->tree->put(1, "one"); |
||
| 141 | $this->tree->put(2, "two"); |
||
| 142 | $this->tree->put(3, "three"); |
||
| 143 | $this->assertEquals(3, $this->tree->deleteMax()->key); |
||
| 144 | $this->assertEquals(2, $this->tree->size()); |
||
| 145 | $this->assertEquals(2, $this->tree->deleteMax()->key); |
||
| 146 | $this->assertEquals(1, $this->tree->size()); |
||
| 147 | $this->tree->put(100, "hundred"); |
||
| 148 | $this->assertEquals(2, $this->tree->size()); |
||
| 149 | $this->assertEquals(100, $this->tree->deleteMax()->key); |
||
| 150 | $this->assertEquals(1, $this->tree->size()); |
||
| 151 | $this->assertEquals(1, $this->tree->deleteMax()->key); |
||
| 152 | $this->assertEquals(0, $this->tree->size()); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testPreorder() { |
||
| 156 | $this->tree->put(2, "two"); |
||
| 157 | $this->tree->put(1, "one"); |
||
| 158 | $this->tree->put(3, "three"); |
||
| 159 | $this->tree->preorder(function ($node) { |
||
| 160 | // do something with the node |
||
| 161 | // echo $node->key . PHP_EOL; |
||
| 162 | }); |
||
| 163 | $this->assertTrue(true); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testInorder() { |
||
| 167 | $this->tree->put(2, "two"); |
||
| 168 | $this->tree->put(1, "one"); |
||
| 169 | $this->tree->put(3, "three"); |
||
| 170 | $this->tree->inorder(function ($node) { |
||
| 171 | // do something with the node |
||
| 172 | // echo $node->key . PHP_EOL; |
||
| 173 | }); |
||
| 174 | $this->assertTrue(true); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testPostorder() { |
||
| 178 | $this->tree->put(2, "two"); |
||
| 179 | $this->tree->put(1, "one"); |
||
| 180 | $this->tree->put(3, "three"); |
||
| 181 | $this->tree->postorder(function ($node) { |
||
| 182 | // do something with the node |
||
| 183 | // echo $node->key . PHP_EOL; |
||
| 184 | }); |
||
| 185 | $this->assertTrue(true); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function testIsLeaf() { |
||
| 189 | $this->tree->put(2, "two"); |
||
| 190 | $this->tree->put(1, "one"); |
||
| 191 | $this->tree->put(3, "three"); |
||
| 192 | |||
| 193 | $this->assertTrue($this->tree->isLeaf($this->tree->search(3))); |
||
| 194 | $this->assertFalse($this->tree->isLeaf($this->tree->search(2))); |
||
| 195 | $this->assertTrue($this->tree->isLeaf($this->tree->search(1))); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function testIsRoot() { |
||
| 199 | $this->tree->put(2, "two"); |
||
| 200 | $this->tree->put(1, "one"); |
||
| 201 | $this->tree->put(3, "three"); |
||
| 202 | |||
| 203 | $this->assertTrue($this->tree->isRoot($this->tree->search(2))); |
||
| 204 | $this->assertFalse($this->tree->isRoot($this->tree->search(3))); |
||
| 205 | $this->assertFalse($this->tree->isRoot($this->tree->search(1))); |
||
| 206 | } |
||
| 207 | } |