Completed
Push — master ( c24877...e2c3e9 )
by Siro Díaz
20:04 queued 17:53
created

BinarySearchTreeTest::testIsLeaf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
use DataStructures\Trees\BinarySearchTree;
4
use PHPUnit\Framework\TestCase;
5
6
class BinarySearchTreeTest extends TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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