Skip to content

Commit ca4bdfe

Browse files
committed
Update data providers and capacity test adjustments
1 parent 8ffb080 commit ca4bdfe

9 files changed

Lines changed: 248 additions & 67 deletions

File tree

tests/CollectionTest.php

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,62 @@ abstract class CollectionTest extends PHPUnit_Framework_TestCase
99
/**
1010
* Sample sizes.
1111
*/
12-
const MANY = 1 << 6 + 1;
13-
const SOME = 1 << 4 + 1;
12+
const MANY = 65;
13+
const SOME = 17;
1414

1515
/**
1616
* Generic mixed value sample array.
1717
*/
1818
public function sample()
1919
{
2020
return array_merge(
21-
[[], null],
22-
['#', '1', 1, 1.0, true],
23-
['', '0', 0, 0.0, false],
24-
['a', 'A', new \stdClass()],
21+
[[]], // empty
22+
[['a']], // 1 value
23+
[['a', 'b']], // 2 values
24+
[['a', 'b', 'c']], // 3 values
25+
['#', '1', 1, 1.0, true], // == true
26+
['', '0', 0, 0.0, false, null], // == false
27+
['a', 'A', new \stdClass()], // string cases, stdClass
2528
range(2, self::SOME)
2629
);
2730
}
2831

32+
/**
33+
* @return a basic data provider providing two equal values for each test.
34+
*/
35+
public function basicDataProvider()
36+
{
37+
$values = [
38+
[],
39+
['a'],
40+
['a', 'b'],
41+
['a', 'b', 'c'],
42+
$this->sample(),
43+
];
44+
45+
return array_map(function($a) { return [$a, $a]; }, $values);
46+
}
47+
48+
/**
49+
* @return a data provider for Sequence and Set to test out of range indexes.
50+
*/
51+
public function outOfRangeDataProvider()
52+
{
53+
return [
54+
[[ ], -1],
55+
[[ ], 1],
56+
[[1], 2],
57+
[[1], -1],
58+
];
59+
}
60+
61+
public function badIndexDataProvider()
62+
{
63+
return [
64+
[[], 'a'],
65+
];
66+
}
67+
2968
public function assertInstanceToString($instance)
3069
{
3170
$this->assertEquals("object(" . get_class($instance) . ')', "$instance");
@@ -36,27 +75,12 @@ public function assertToArray(array $expected, $instance)
3675
$actual = $instance->toArray();
3776

3877
// We have to make separate assertions here because PHPUnit considers an
39-
// array to be equal of the keys match the values even if the order is
78+
// array to be equal if the keys match the values even if the order is
4079
// not the same, ie. [a => 1, b => 2] equals [b => 2, a => 1].
4180
$this->assertEquals(array_values($expected), array_values($actual), "!!! ARRAY VALUE MISMATCH");
4281
$this->assertEquals(array_keys ($expected), array_keys ($actual), "!!! ARRAY KEY MISMATCH");
4382
}
4483

45-
public function basicDataProvider()
46-
{
47-
$sample = $this->sample();
48-
49-
$values = [
50-
[],
51-
['a'],
52-
['a', 'b'],
53-
['a', 'b', 'c'],
54-
$sample,
55-
];
56-
57-
return array_map(function($a) { return [$a, $a]; }, $values);
58-
}
59-
6084
public function expectAccessByReferenceHasNoEffect()
6185
{
6286
$this->setExpectedException(\PHPUnit_Framework_Error_Notice::class);
@@ -132,23 +156,6 @@ public function expectInternalIllegalOffset()
132156
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class);
133157
}
134158

135-
public function outOfRangeDataProvider()
136-
{
137-
return [
138-
[[ ], -1],
139-
[[ ], 1],
140-
[[1], 2],
141-
[[1], -1],
142-
];
143-
}
144-
145-
public function badIndexDataProvider()
146-
{
147-
return [
148-
[[], 'a'],
149-
];
150-
}
151-
152159
public function assertInstanceDump(array $expected, $instance)
153160
{
154161
ob_start();

tests/Deque/capacity.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,37 @@ public function testCapacity()
2020

2121
public function testAutoTruncate()
2222
{
23-
$min = \Ds\Deque::MIN_CAPACITY;
24-
$num = $min * 16;
23+
// size => capacity
24+
$boundaries = [
25+
64 => 128, // Initial capacity for 64 elements would be 128.
26+
33 => 128,
27+
32 => 64,
28+
31 => 64,
29+
17 => 64,
30+
16 => 32,
31+
15 => 32,
32+
9 => 32,
33+
8 => 16,
34+
7 => 16,
35+
5 => 16,
36+
4 => 8,
37+
3 => 8,
38+
0 => 8,
39+
];
40+
41+
$instance = $this->getInstance(range(1, array_keys($boundaries)[0]));
2542

26-
$instance = $this->getInstance(range(1, $num - 1));
27-
$expected = $num / 2;
43+
for (;;) {
44+
if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) {
45+
$this->assertEquals($expected, $instance->capacity());
46+
}
47+
48+
if ($instance->isEmpty()) {
49+
break;
50+
}
2851

29-
for ($i = 0; $i <= 3 * $num / 4; $i++) {
3052
$instance->pop();
3153
}
32-
33-
$this->assertEquals($expected, $instance->capacity());
3454
}
3555

3656
public function testClearResetsCapacity()

tests/DequeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function testReallocatingWhenHeadNotAtZero()
6565
$instance->shift();
6666
$instance->shift();
6767

68+
// [_, _, c, d, _, _, _, _]
69+
70+
// Push many to force a reallocation, where the head is not at 0.
6871
foreach (range(0, self::MANY) as $value) {
6972
$instance->push($value);
7073
}

tests/Map/capacity.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,37 @@ public function testCapacity()
2626

2727
public function testAutoTruncate()
2828
{
29-
$instance = $this->getInstance(range(1, self::MANY));
30-
$expected = $instance->capacity() / 2;
29+
// size => capacity
30+
$boundaries = [
31+
64 => 64, // Initial capacity for 64 elements would be 64 (full).
32+
33 => 64,
33+
32 => 64,
34+
31 => 64,
35+
17 => 64,
36+
16 => 64,
37+
15 => 32,
38+
9 => 32,
39+
8 => 32,
40+
7 => 16,
41+
5 => 16,
42+
4 => 16,
43+
3 => 8,
44+
0 => 8,
45+
];
3146

32-
for ($i = 0; $i <= 3 * self::MANY / 4; $i++) {
33-
$instance->remove($i);
34-
}
47+
$instance = $this->getInstance(range(1, array_keys($boundaries)[0]));
48+
49+
for(;;) {
50+
if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) {
51+
$this->assertEquals($expected, $instance->capacity());
52+
}
3553

36-
$this->assertEquals($expected, $instance->capacity());
54+
if ($instance->isEmpty()) {
55+
break;
56+
}
57+
58+
$instance->remove($instance->count() - 1);
59+
}
3760
}
3861

3962
public function testClearResetsCapacity()

tests/Map/remove.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ public function testRemoveHalfFromMidway()
6464
{
6565
$instance = $this->getInstance();
6666

67-
for ($i = 0; $i < self::MANY; $i++) {
67+
$size = self::MANY + (self::MANY & 1); // Force even
68+
$half = intdiv($size, 2);
69+
70+
for ($i = 1; $i <= $size; $i++) {
6871
$instance->put($i, $i);
6972
}
7073

71-
for ($i = self::MANY / 2; $i < self::MANY; $i++) {
74+
for ($i = $half + 1; $i <= $size; $i++) {
7275
$instance->remove($i);
7376
}
7477

75-
$this->assertCount(self::MANY / 2, $instance);
78+
$this->assertCount($half, $instance);
7679
}
7780

7881
public function testRandomRemove()

tests/Sequence/slice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testLargeSliceHalf()
5353
{
5454
$n = self::MANY;
5555
$x = 0;
56-
$y = $n / 2;
56+
$y = intdiv($n, 2);
5757

5858
$instance = $this->getInstance(range(0, $n));
5959

@@ -64,8 +64,8 @@ public function testLargeSliceHalf()
6464
public function testLargeSliceOffset()
6565
{
6666
$n = self::MANY;
67-
$x = $n / 4;
68-
$y = $n / 4 + $n / 2;
67+
$x = intdiv($n, 4);
68+
$y = intdiv($n, 4) + intdiv($n, 2);
6969

7070
$instance = $this->getInstance(range(0, $n));
7171

tests/Set/get.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,108 @@ public function testGetLastAfterRemove()
118118
$instance->remove('b');
119119
$this->assertEquals('a', $instance->get(0));
120120
}
121+
122+
public function getAfterRemoveProvider()
123+
{
124+
// values, value to remove, get index, expected result
125+
return [
126+
[['a', 'b', 'c'], 'a', 0, 'b'],
127+
[['a', 'b', 'c'], 'a', 1, 'c'],
128+
129+
[['a', 'b', 'c'], 'b', 0, 'a'],
130+
[['a', 'b', 'c'], 'b', 1, 'c'],
131+
132+
[['a', 'b', 'c'], 'c', 0, 'a'],
133+
[['a', 'b', 'c'], 'c', 1, 'b'],
134+
];
135+
}
136+
137+
/**
138+
* @dataProvider getAfterRemoveProvider
139+
*/
140+
public function testGetAfterRemove(array $values, $remove, $index, $expected)
141+
{
142+
$instance = $this->getInstance($values);
143+
$instance->remove($remove);
144+
145+
$this->assertEquals($expected, $instance->get($index));
146+
}
147+
148+
public function testGetAfterRemoveAtTheStart()
149+
{
150+
$instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']);
151+
152+
$instance->remove('a');
153+
$this->assertEquals('b', $instance->get(0));
154+
$this->assertEquals('c', $instance->get(1));
155+
$this->assertEquals('d', $instance->get(2));
156+
$this->assertEquals('e', $instance->get(3));
157+
158+
$instance->remove('b');
159+
$this->assertEquals('c', $instance->get(0));
160+
$this->assertEquals('d', $instance->get(1));
161+
$this->assertEquals('e', $instance->get(2));
162+
163+
$instance->remove('c');
164+
$this->assertEquals('d', $instance->get(0));
165+
$this->assertEquals('e', $instance->get(1));
166+
167+
$instance->remove('d');
168+
$this->assertEquals('e', $instance->get(0));
169+
170+
$instance->remove('e');
171+
$this->assertTrue($instance->isEmpty());
172+
}
173+
174+
public function testGetAfterRemoveAtTheEnd()
175+
{
176+
$instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']);
177+
178+
$instance->remove('e');
179+
$this->assertEquals('a', $instance->get(0));
180+
$this->assertEquals('b', $instance->get(1));
181+
$this->assertEquals('c', $instance->get(2));
182+
$this->assertEquals('d', $instance->get(3));
183+
184+
$instance->remove('d');
185+
$this->assertEquals('a', $instance->get(0));
186+
$this->assertEquals('b', $instance->get(1));
187+
$this->assertEquals('c', $instance->get(2));
188+
189+
$instance->remove('c');
190+
$this->assertEquals('a', $instance->get(0));
191+
$this->assertEquals('b', $instance->get(1));
192+
193+
$instance->remove('b');
194+
$this->assertEquals('a', $instance->get(0));
195+
196+
$instance->remove('a');
197+
$this->assertTrue($instance->isEmpty());
198+
}
199+
200+
public function testGetAfterMultipleRemoveAtEitherEnd()
201+
{
202+
$instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']);
203+
204+
$instance->remove('a');
205+
$this->assertEquals('b', $instance->get(0));
206+
$this->assertEquals('c', $instance->get(1));
207+
$this->assertEquals('d', $instance->get(2));
208+
$this->assertEquals('e', $instance->get(3));
209+
210+
$instance->remove('e');
211+
$this->assertEquals('b', $instance->get(0));
212+
$this->assertEquals('c', $instance->get(1));
213+
$this->assertEquals('d', $instance->get(2));
214+
215+
$instance->remove('b');
216+
$this->assertEquals('c', $instance->get(0));
217+
$this->assertEquals('d', $instance->get(1));
218+
219+
$instance->remove('d');
220+
$this->assertEquals('c', $instance->get(0));
221+
222+
$instance->remove('c');
223+
$this->assertTrue($instance->isEmpty());
224+
}
121225
}

tests/Set/slice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testLargeSliceHalf()
7676
{
7777
$n = self::MANY;
7878
$x = 0;
79-
$y = $n / 2;
79+
$y = intdiv($n, 2);
8080

8181
$instance = $this->getInstance(range(0, $n));
8282

@@ -87,8 +87,8 @@ public function testLargeSliceHalf()
8787
public function testLargeSliceOffset()
8888
{
8989
$n = self::MANY;
90-
$x = $n / 4;
91-
$y = $n / 4 + $n / 2;
90+
$x = intdiv($n, 4);
91+
$y = intdiv($n, 4) + intdiv($n, 2);
9292

9393
$instance = $this->getInstance(range(0, $n));
9494

0 commit comments

Comments
 (0)