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