Completed
Push — master ( d060d7...4b3449 )
by Siro Díaz
02:14
created

AVLTreeTest::testDeleteMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 14

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace DataStructures\Tests\Trees;
4
5
use DataStructures\Trees\AVLTree;
6
use PHPUnit\Framework\TestCase;
7
8 View Code Duplication
class AVLTreeTest extends TestCase {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The call to AVLTree::putOrUpdate() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
114
        $this->assertEquals(2, $this->tree->size());
115
        $this->assertEquals("three", $this->tree->delete(3)->data);
0 ignored issues
show
Bug introduced by
Accessing data on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
116
        $this->assertEquals(1, $this->tree->size());
117
        $this->assertEquals(2, $this->tree->delete(2)->key);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
128
        $this->assertEquals(2, $this->tree->size());
129
        $this->assertEquals(2, $this->tree->deleteMin()->key);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
134
        $this->assertEquals(1, $this->tree->size());
135
        $this->assertEquals(100, $this->tree->deleteMin()->key);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
144
        $this->assertEquals(2, $this->tree->size());
145
        $this->assertEquals(2, $this->tree->deleteMax()->key);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
150
        $this->assertEquals(1, $this->tree->size());
151
        $this->assertEquals(1, $this->tree->deleteMax()->key);
0 ignored issues
show
Bug introduced by
Accessing key on the interface DataStructures\Trees\Int...ces\BinaryNodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
            // do something with the node
161
            // echo $node->key . PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
            // do something with the node
172
            // echo $node->key . PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
            // do something with the node
183
            // echo $node->key . PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
}