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

DoublyLinkedListTest::testGetLast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace DataStructures\Tests\Lists;
4
5
use DataStructures\Lists\DoublyLinkedList;
6
use PHPUnit\Framework\TestCase;
7
8
class DoublyLinkedListTest extends TestCase {
9
    private $list;
10
11
    public function setUp() {
12
        $this->list = new DoublyLinkedList();
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 View Code Duplication
    public function testShift() {
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...
96
        $this->assertNull($this->list->shift());
97
        $this->list->push(20);
98
        $this->list->push(true);
99
        $this->assertEquals(20, $this->list->shift());
100
        $this->assertEquals(1, $this->list->size());
101
        $this->assertEquals(true, $this->list->shift());
102
        $this->assertEquals(true, $this->list->empty());
103
        $this->assertNull($this->list->shift());
104
    }
105
    
106 View Code Duplication
    public function testPop() {
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...
107
        $this->list->push(20);
108
        $this->list->push(true);
109
        $this->list->push(15);
110
        $this->list->push(3.14);
111
        $this->list->push("string");
112
        
113
        $this->assertEquals($this->list->pop(), "string");
114
        $this->assertEquals(3.14, $this->list->pop());
115
        $this->list->insert(1, ['hello']);
116
        $this->assertEquals(15, $this->list->pop());
117
        $this->assertTrue($this->list->pop());
118
        $this->assertSame($this->list->pop(), ['hello']);
119
        $this->assertSame($this->list->pop(), 20);
120
        $this->assertTrue($this->list->empty());
121
    }
122
    
123
    public function testPopException() {
124
        $this->expectException(\OutOfBoundsException::class);
125
        $this->list->pop();
126
    }
127
    
128 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...
129
        $this->list->unshift(999);
130
        $this->assertEquals(1, $this->list->size());
131
        $this->assertEquals(999, $this->list->get(0));
132
        $this->assertEquals($this->list->getLast(), 999);
133
        $this->list->unshift(888);
134
        $this->assertEquals(2, $this->list->size());
135
        $this->assertEquals(888, $this->list->get(0));
136
        $this->assertEquals(999, $this->list->get(1));
137
        $this->assertEquals($this->list->getLast(), 999);
138
        $this->list->unshift(777);
139
        $this->assertEquals(3, $this->list->size());
140
        $this->assertEquals(777, $this->list->get(0));
141
        $this->assertEquals(888, $this->list->get(1));
142
        $this->assertEquals(999, $this->list->get(2));
143
        $this->assertEquals($this->list->getLast(), 999);
144
    }
145
    
146 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...
147
        $this->list->push(20);
148
        $this->list->push(true);
149
        $this->list->push(15);
150
        $this->list->push(3.14);
151
        $this->list->push("string");
152
        $this->assertEquals($this->list->get(0), 20);
153
        $this->assertTrue($this->list->get(1));
154
        $this->assertEquals($this->list->get(2), 15);
155
        $this->assertEquals($this->list->get(3), 3.14);
156
        $this->assertEquals($this->list->get(4), "string");
157
    }
158
    
159
    public function testGetWithException() {
160
        $this->expectException(\OutOfBoundsException::class);
161
        $this->list->get(5);
162
    }
163
164
    public function testGetOutOfBound() {
165
        $this->expectException(\OutOfBoundsException::class);
166
        $this->list->get(-1);
167
    }
168
    
169 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...
170
        foreach($this->list->getAll() as $node) {
171
            $this->assertNull($node);
172
        }
173
        
174
        $this->list->push(20);
175
        $this->list->push(true);
176
        $this->list->push(15);
177
        $this->list->push(3.14);
178
        $this->list->push("string");
179
        $data = [];
180
        foreach($this->list->getAll() as $node) {
181
            $data[] = $node;
182
        }
183
        $this->assertCount(5, $data);
184
        $this->assertSame([20, true, 15, 3.14, "string"], $data);
185
    }
186
    
187 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...
188
        $this->list->push(20);
189
        $this->list->push(true);
190
        $this->list->push(15);
191
        $this->list->push(3.14);
192
        $this->list->push("string");
193
        $this->list->pop();
194
        $nodes = $this->list->toArray();
195
        $this->assertSame([20, true, 15, 3.14], $nodes);
196
    }
197
    
198 View Code Duplication
    public function testClear() {
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...
199
        $this->list->push(20);
200
        $this->list->push(true);
201
        $this->list->push(15);
202
        $this->list->push(3.14);
203
        $this->list->push("string");
204
        $this->list->clear();
205
        $this->assertEmpty($this->list->toArray());
206
        $this->assertEquals($this->list->size(), 0);
207
    }
208
209
    public function testIterator() {
210
        $this->list->push(20);
211
        $this->list->push(true);
212
        $this->list->push(15);
213
        $this->list->push(3.14);
214
        $this->list->push("string");
215
216
        $this->list->shift();
217
        $expectedResult = [];
218
        $result = [];
219
        foreach($this->list->getAll() as $node) {
220
            $expectedResult[] = $node;
221
        }
222
        foreach($this->list as $index => $val) {
223
            $result[] = $val;
224
        }
225
226
        $this->assertSame($expectedResult, $result);
227
    }
228
}