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

ArrayListTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DataStructures\Tests\Lists;
4
5
use DataStructures\Lists\ArrayList;
6
use PHPUnit\Framework\TestCase;
7
8
class ArrayListTest extends TestCase {
9
    private $list;
10
11
    public function setUp() {
12
        $this->list = new ArrayList();
13
    }
14
15 View Code Duplication
    public function testSize() {
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...
16
        $this->assertEquals($this->list->size(), 0);
17
        $this->list->push(true);
18
        $this->list->push(2);
19
        $this->assertEquals($this->list->size(), 2);
20
    }
21
22
    public function testEmpty() {
23
        $this->assertTrue($this->list->empty());
24
    }
25
26 View Code Duplication
    public function testPush() {
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...
27
        $this->list->push(20);
28
        $this->assertEquals(1, $this->list->size());
29
        $this->assertEquals(20, $this->list->get(0));
30
        $this->list->push(true);
31
        
32
        $this->assertEquals(2, $this->list->size());
33
        
34
        $this->assertTrue($this->list->get(1));
35
        
36
        $this->list->push(30);
37
        $this->assertEquals(20, $this->list->get(0));
38
        $this->assertEquals(30, $this->list->get(1));
39
        $this->assertEquals(true, $this->list->get(2));
40
    }
41
42 View Code Duplication
    public function testGetLast() {
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...
43
        $this->assertNull($this->list->getLast());
44
        $this->list->push(true);
45
        $this->list->push(50);
46
        $this->list->push("string");
47
        $this->assertEquals("string", $this->list->getLast());
48
    }
49
50 View Code Duplication
    public function testInsert() {
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...
51
        $this->list->insert(0, 100);
52
        $this->assertEquals(100, $this->list->get(0));
53
        $this->assertEquals(1, $this->list->size());
54
        
55
        $this->list->insert(0, 200);
56
        $this->assertEquals(2, $this->list->size());
57
        $this->assertEquals(200, $this->list->get(0));
58
        $this->assertEquals(100, $this->list->get(1));
59
        $this->assertEquals(100, $this->list->getLast());
60
        
61
        $this->list->insert(1, 300);
62
        $this->assertEquals(3, $this->list->size());
63
        $this->assertEquals(200, $this->list->get(0));
64
        $this->assertEquals(300, $this->list->get(1));
65
        $this->assertEquals(100, $this->list->get(2));
66
        $this->list->insert(2, 1000);
67
        $this->assertEquals(1000, $this->list->get(2));
68
        $this->assertEquals(100, $this->list->get(3));
69
        $this->list->insert(6, true);
70
        $this->assertTrue($this->list->get(4));
71
        $this->assertEquals(5, $this->list->size());
72
    }
73
    
74
    public function testDeleteException() {
75
        $this->expectException(\OutOfBoundsException::class);
76
        $this->list->delete(10);
77
    }
78
    
79 View Code Duplication
    public function testDelete() {
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...
80
        $this->list->push(20);
81
        $this->list->push(true);
82
        $this->list->push(15);
83
        $this->list->push(3.14);
84
        $this->list->push("string");
85
        
86
        $this->assertEquals($this->list->delete(4), "string");
87
        $this->assertEquals(3.14, $this->list->delete(3));
88
        
89
        $this->assertEquals(3, $this->list->size());
90
        $this->assertEquals(true, $this->list->delete(1));
91
        $this->assertEquals(false, $this->list->empty());
92
        $this->assertEquals(20, $this->list->delete(0));
93
    }
94
    
95
    public function testShift() {
96
        $this->list->push(20);
97
        $this->list->push(true);
98
        $this->assertEquals(20, $this->list->shift());
99
        $this->assertEquals(1, $this->list->size());
100
        $this->assertEquals(true, $this->list->shift());
101
        $this->assertEquals(true, $this->list->empty());
102
    }
103
    
104
    public function testPop() {
105
        $this->list->push(20);
106
        $this->list->push(true);
107
        $this->list->push(15);
108
        $this->list->push(3.14);
109
        $this->list->push("string");
110
        
111
        $this->assertEquals($this->list->pop(), "string");
112
        $this->assertEquals(3.14, $this->list->pop());
113
        $this->list->insert(1, ['hello']);
114
        $this->assertEquals(15, $this->list->pop());
115
        $this->assertTrue($this->list->pop());
116
        $this->assertSame($this->list->pop(), 'hello');
117
        $this->assertSame($this->list->pop(), 20);
118
        $this->assertTrue($this->list->empty());
119
    }
120
    
121
    public function testPopException() {
122
        $this->expectException(\OutOfBoundsException::class);
123
        $this->list->pop();
124
    }
125
    
126 View Code Duplication
    public function testUnshift() {
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...
127
        $this->list->unshift(999);
128
        $this->assertEquals(1, $this->list->size());
129
        $this->assertEquals(999, $this->list->get(0));
130
        $this->assertEquals($this->list->getLast(), 999);
131
        $this->list->unshift(888);
132
        $this->assertEquals(2, $this->list->size());
133
        $this->assertEquals(888, $this->list->get(0));
134
        $this->assertEquals(999, $this->list->get(1));
135
        $this->assertEquals($this->list->getLast(), 999);
136
        $this->list->unshift(777);
137
        $this->assertEquals(3, $this->list->size());
138
        $this->assertEquals(777, $this->list->get(0));
139
        $this->assertEquals(888, $this->list->get(1));
140
        $this->assertEquals(999, $this->list->get(2));
141
        $this->assertEquals($this->list->getLast(), 999);
142
    }
143
    
144 View Code Duplication
    public function testGet() {
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...
145
        $this->list->push(20);
146
        $this->list->push(true);
147
        $this->list->push(15);
148
        $this->list->push(3.14);
149
        $this->list->push("string");
150
        $this->assertEquals($this->list->get(0), 20);
151
        $this->assertTrue($this->list->get(1));
152
        $this->assertEquals($this->list->get(2), 15);
153
        $this->assertEquals($this->list->get(3), 3.14);
154
        $this->assertEquals($this->list->get(4), "string");
155
    }
156
    
157
    public function testGetWithException() {
158
        $this->expectException(\OutOfBoundsException::class);
159
        $this->list->get(5);
160
    }
161
162
    public function testGetOutOfBound() {
163
        $this->expectException(\OutOfBoundsException::class);
164
        $this->list->get(-1);
165
    }
166
    
167 View Code Duplication
    public function testGetAll() {
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...
168
        foreach($this->list->getAll() as $node) {
169
            $this->assertNull($node);
170
        }
171
        
172
        $this->list->push(20);
173
        $this->list->push(true);
174
        $this->list->push(15);
175
        $this->list->push(3.14);
176
        $this->list->push("string");
177
        $data = [];
178
        foreach($this->list->getAll() as $node) {
179
            $data[] = $node;
180
        }
181
        $this->assertCount(5, $data);
182
        $this->assertSame([20, true, 15, 3.14, "string"], $data);
183
    }
184
    
185 View Code Duplication
    public function testToArray() {
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...
186
        $this->list->push(20);
187
        $this->list->push(true);
188
        $this->list->push(15);
189
        $this->list->push(3.14);
190
        $this->list->push("string");
191
        $this->list->pop();
192
        $nodes = $this->list->toArray();
193
        $this->assertSame([20, true, 15, 3.14], $nodes);
194
    }
195
    
196
    public function testClear() {
197
        $this->list->push(20);
198
        $this->list->push(true);
199
        $this->list->push(15);
200
        $this->list->push(3.14);
201
        $this->list->push("string");
202
        $this->list->clear();
203
        $this->assertEmpty($this->list->toArray());
204
        $this->assertEquals($this->list->size(), 0);
205
    }
206
207
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
208
    public function testIterator() {
209
        $this->list->push(20);
210
        $this->list->push(true);
211
        $this->list->push(15);
212
        $this->list->push(3.14);
213
        $this->list->push("string");
214
215
        $this->list->shift();
216
        $this->assertEquals(4, count($this->list));
217
        $expectedResult = [];
218
        $result = [];
219
        foreach($this->list->getAll() as $node) {
220
            $expectedResult[] = $node;
221
        }
222
        
223
        foreach($this->list as $index => $val) {
224
            echo $index ." ----- ". $val . PHP_EOL;
225
            $result[] = $val;
226
        }
227
228
        $this->assertSame($expectedResult, $result);
229
    }
230
    */
231
}