├── .gitignore ├── tests ├── Sequence │ ├── _serialize.php │ ├── _echo.php │ ├── clear.php │ ├── _foreach.php │ ├── _var_dump.php │ ├── _list.php │ ├── count.php │ ├── copy.php │ ├── toArray.php │ ├── _jsonEncode.php │ ├── sort.php │ ├── reverse.php │ ├── reversed.php │ ├── _clone.php │ ├── sorted.php │ ├── last.php │ ├── first.php │ ├── find.php │ ├── isEmpty.php │ ├── sum.php │ ├── contains.php │ ├── unshift.php │ ├── pop.php │ ├── merge.php │ ├── join.php │ ├── shift.php │ ├── remove.php │ ├── rotate.php │ ├── _unset.php │ ├── _empty.php │ ├── apply.php │ ├── _isset.php │ ├── insert.php │ ├── slice.php │ ├── map.php │ ├── get.php │ └── reduce.php ├── Pair │ ├── _list.php │ ├── _foreach.php │ ├── _echo.php │ ├── _serialize.php │ ├── __construct.php │ ├── _var_dump.php │ ├── toArray.php │ ├── _jsonEncode.php │ ├── _clone.php │ ├── copy.php │ ├── _unset.php │ ├── __set.php │ ├── _empty.php │ ├── __get.php │ └── _isset.php ├── PriorityQueue │ ├── _list.php │ ├── _echo.php │ ├── __construct.php │ ├── _empty.php │ ├── _isset.php │ ├── _unset.php │ ├── _var_dump.php │ ├── count.php │ ├── _jsonEncode.php │ ├── isEmpty.php │ ├── toArray.php │ ├── _clone.php │ ├── clear.php │ ├── _serialize.php │ ├── _foreach.php │ ├── peek.php │ ├── allocate.php │ ├── copy.php │ ├── pop.php │ ├── capacity.php │ └── push.php ├── Map │ ├── _echo.php │ ├── clear.php │ ├── toArray.php │ ├── _foreach.php │ ├── _list.php │ ├── _serialize.php │ ├── count.php │ ├── copy.php │ ├── _jsonEncode.php │ ├── _unset.php │ ├── reversed.php │ ├── _clone.php │ ├── reverse.php │ ├── keys.php │ ├── values.php │ ├── _var_dump.php │ ├── last.php │ ├── first.php │ ├── isEmpty.php │ ├── sum.php │ ├── allocate.php │ ├── hasKey.php │ ├── __construct.php │ ├── hasValue.php │ ├── _empty.php │ ├── intersect.php │ ├── diff.php │ ├── xor_.php │ ├── ksort.php │ ├── merge.php │ ├── sort.php │ ├── union.php │ ├── skip.php │ ├── ksorted.php │ ├── sorted.php │ ├── pairs.php │ ├── _isset.php │ ├── capacity.php │ ├── apply.php │ ├── get.php │ ├── slice.php │ ├── map.php │ └── filter.php ├── Set │ ├── _echo.php │ ├── _empty.php │ ├── clear.php │ ├── toArray.php │ ├── _foreach.php │ ├── _var_dump.php │ ├── _serialize.php │ ├── _list.php │ ├── _jsonEncode.php │ ├── copy.php │ ├── _unset.php │ ├── _isset.php │ ├── _clone.php │ ├── sort.php │ ├── reverse.php │ ├── count.php │ ├── reversed.php │ ├── sorted.php │ ├── last.php │ ├── first.php │ ├── isEmpty.php │ ├── sum.php │ ├── allocate.php │ ├── join.php │ ├── contains.php │ ├── __construct.php │ ├── merge.php │ ├── capacity.php │ ├── diff.php │ ├── filter.php │ ├── remove.php │ ├── reduce.php │ ├── map.php │ ├── xor_.php │ └── union.php ├── Queue │ ├── _echo.php │ ├── _empty.php │ ├── clear.php │ ├── toArray.php │ ├── _var_dump.php │ ├── _serialize.php │ ├── _jsonEncode.php │ ├── copy.php │ ├── _isset.php │ ├── _unset.php │ ├── count.php │ ├── _list.php │ ├── _clone.php │ ├── _foreach.php │ ├── peek.php │ ├── isEmpty.php │ ├── __construct.php │ ├── pop.php │ └── push.php ├── Stack │ ├── _echo.php │ ├── _empty.php │ ├── _isset.php │ ├── _unset.php │ ├── _list.php │ ├── clear.php │ ├── toArray.php │ ├── _var_dump.php │ ├── _jsonEncode.php │ ├── copy.php │ ├── count.php │ ├── _clone.php │ ├── _serialize.php │ ├── _foreach.php │ ├── peek.php │ ├── isEmpty.php │ ├── __construct.php │ ├── pop.php │ └── push.php ├── HashableObject.php ├── PairTest.php ├── Vector │ ├── allocate.php │ ├── __construct.php │ └── capacity.php ├── Deque │ ├── allocate.php │ ├── __construct.php │ ├── capacity.php │ ├── remove.php │ └── slice.php ├── QueueTest.php ├── PriorityQueueTest.php ├── VectorTest.php ├── StackTest.php └── SetTest.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /tests/Sequence/_serialize.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Set/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Pair/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getPair('a', 1)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Queue/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Stack/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Sequence/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_echo.php: -------------------------------------------------------------------------------- 1 | assertInstanceToString($this->getInstance()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/Pair/_serialize.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $this->assertSerialized(['a', 1], $pair, false); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/PriorityQueue/__construct.php: -------------------------------------------------------------------------------- 1 | assertToArray([], new PriorityQueue()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Queue/_empty.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | empty($set['a']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Stack/_empty.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | empty($set['a']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Stack/_isset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | isset($set['a']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Stack/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | unset($set['a']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Set/_empty.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | $this->expectArrayAccessUnsupportedException(); 10 | empty($set[0]); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Stack/_list.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | $this->expectListNotSupportedException(); 10 | list($a, $b, $c) = $instance; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_empty.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | empty($instance['?']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_isset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | isset($instance['?']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | unset($instance['?']); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Pair/__construct.php: -------------------------------------------------------------------------------- 1 | assertEquals('a', $pair->key); 12 | $this->assertEquals( 1, $pair->value); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/clear.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $instance->clear(); 10 | 11 | $this->assertToArray([], $instance); 12 | $this->assertCount(0, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Queue/clear.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $instance->clear(); 10 | 11 | $this->assertToArray([], $instance); 12 | $this->assertCount(0, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/clear.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $instance->clear(); 10 | 11 | $this->assertToArray([], $instance); 12 | $this->assertCount(0, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Stack/clear.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $instance->clear(); 10 | 11 | $this->assertToArray([], $instance); 12 | $this->assertCount(0, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/toArray.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $instance->put(new \stdClass(), 1); 10 | 11 | $this->expectInternalIllegalOffset(); 12 | $instance->toArray(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Sequence/clear.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $instance->clear(); 10 | 11 | $this->assertToArray([], $instance); 12 | $this->assertCount(0, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/toArray.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertToArray($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertForEach($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/_list.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertToArray($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertForEach($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Stack/toArray.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertToArray($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertInstanceDump($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Queue/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertInstanceDump($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Sequence/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertForEach($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Stack/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertInstanceDump($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/_serialize.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertSerialized($expected, $instance, true); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Pair/_var_dump.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $expected = [ 10 | 'key' => 'a', 11 | 'value' => 1 12 | ]; 13 | 14 | $this->assertInstanceDump($expected, $instance); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Pair/toArray.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $expected = [ 10 | 'key' => 'a', 11 | 'value' => 1 12 | ]; 13 | 14 | $this->assertEquals($expected, $instance->toArray()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Sequence/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertInstanceDump($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/_serialize.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertSerialized($expected, $instance, false); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Queue/_serialize.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertSerialized($expected, $instance, false); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Set/_list.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | list($a, $b, $c) = $instance; 10 | 11 | $this->assertEquals('a', $a); 12 | $this->assertEquals('b', $b); 13 | $this->assertEquals('c', $c); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertInstanceDump($expected, $instance); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/PriorityQueue/count.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | 10 | foreach (range(1, self::MANY) as $i) { 11 | $instance->push($i, rand()); 12 | } 13 | 14 | $this->assertCount(self::MANY, $instance); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Sequence/_list.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | list($a, $b, $c) = $instance; 10 | 11 | $this->assertEquals('a', $a); 12 | $this->assertEquals('b', $b); 13 | $this->assertEquals('c', $c); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/Set/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 12 | $this->assertEquals(json_encode($expected), json_encode($instance)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Pair/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $expected = json_encode([ 10 | 'key' => 'a', 11 | 'value' => 1 12 | ]); 13 | 14 | $this->assertEquals($expected, json_encode($instance)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Queue/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 12 | $this->assertEquals(json_encode($expected), json_encode($instance)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Stack/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 12 | $this->assertEquals(json_encode($expected), json_encode($instance)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 12 | $this->assertEquals(json_encode($expected), json_encode($instance)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/Map/count.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $this->assertCount(count($this->sample()), $instance); 10 | } 11 | 12 | public function testCountEmpty() 13 | { 14 | $instance = $this->getInstance(); 15 | $this->assertCount(0, $instance); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Sequence/count.php: -------------------------------------------------------------------------------- 1 | getInstance($this->sample()); 9 | $this->assertCount(count($this->sample()), $instance); 10 | } 11 | 12 | public function testCountEmpty() 13 | { 14 | $instance = $this->getInstance(); 15 | $this->assertCount(0, $instance); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Pair/_clone.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | 10 | $clone = clone $instance; 11 | 12 | $this->assertEquals(get_class($instance), get_class($clone)); 13 | $this->assertEquals($instance->toArray(), $clone->toArray()); 14 | $this->assertFalse($clone === $instance); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "php-ds/tests", 3 | "license": "MIT", 4 | "authors": [ 5 | { 6 | "name": "Rudi Theunissen", 7 | "email": "rudolf.theunissen@gmail.com" 8 | } 9 | ], 10 | "require": { 11 | "php": "^8.3", 12 | "phpunit/phpunit": "^12.1.4" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Ds\\Tests\\": "tests" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/PriorityQueue/isEmpty.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->assertTrue($instance->isEmpty()); 10 | 11 | $instance->push('a', 1); 12 | $this->assertFalse($instance->isEmpty()); 13 | 14 | $instance->pop(); 15 | $this->assertTrue($instance->isEmpty()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Pair/copy.php: -------------------------------------------------------------------------------- 1 | getPair("a", 1); 9 | $copy = $pair->copy(); 10 | 11 | $copy->key = "x"; 12 | $copy->value = 2; 13 | 14 | $this->assertEquals(["key" => "a", "value" => 1], $pair->toArray()); 15 | $this->assertEquals(["key" => "x", "value" => 2], $copy->toArray()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Map/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Set/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Pair/_unset.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | unset($pair->key); 10 | $this->assertNull($pair->key); 11 | } 12 | 13 | public function testPropertyUnsetValue() 14 | { 15 | $pair = $this->getPair('a', 1); 16 | unset($pair->value); 17 | $this->assertNull($pair->value); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Queue/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Stack/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Sequence/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Sequence/toArray.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider toArrayDataProvider 13 | */ 14 | public function testToArray(array $values, array $expected) 15 | { 16 | $instance = $this->getInstance($values); 17 | $this->assertToArray($expected, $instance); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/PriorityQueue/toArray.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | 13 | // Also check that toArray is not destructive 14 | $this->assertToArray($expected, $instance); 15 | $this->assertToArray($expected, $instance); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/Queue/_isset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | isset($set['a']); 11 | } 12 | 13 | public function testArrayAccessIssetByMethod() 14 | { 15 | $set = $this->getInstance(); 16 | $this->expectArrayAccessUnsupportedException(); 17 | $set->offsetExists('a'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Queue/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | $this->expectArrayAccessUnsupportedException(); 10 | unset($set['a']); 11 | } 12 | 13 | public function testArrayAccessUnsetByMethod() 14 | { 15 | $set = $this->getInstance(); 16 | $this->expectArrayAccessUnsupportedException(); 17 | $set->offsetUnset('a'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Queue/count.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertCount(count($expected), $instance); 13 | } 14 | 15 | public function testCountEmpty() 16 | { 17 | $instance = $this->getInstance(); 18 | $this->assertCount(0, $instance); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Set/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | $this->expectArrayAccessUnsupportedException(); 10 | unset($set[0]); 11 | } 12 | 13 | public function testArrayAccessUnsetByMethod() 14 | { 15 | $set = $this->getInstance(); 16 | $this->expectArrayAccessUnsupportedException(); 17 | $set->offsetUnset('a'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Stack/count.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $this->assertCount(count($expected), $instance); 13 | } 14 | 15 | public function testCountEmpty() 16 | { 17 | $instance = $this->getInstance(); 18 | $this->assertCount(0, $instance); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/Queue/_list.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | $this->expectListNotSupportedException(); 10 | list($a, $b, $c) = $instance; 11 | } 12 | 13 | public function testListByMethod() 14 | { 15 | $instance = $this->getInstance(['a', 'b', 'c']); 16 | $this->expectListNotSupportedException(); 17 | $instance->offsetGet(0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Map/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider jsonEncodeDataProvider 13 | */ 14 | public function testJsonEncode(array $initial, array $expected) 15 | { 16 | $instance = $this->getInstance($initial); 17 | $this->assertEquals(json_encode((object) $expected), json_encode($instance)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Sequence/_jsonEncode.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider jsonEncodeDataProvider 13 | */ 14 | public function testJsonEncode(array $initial, array $expected) 15 | { 16 | $instance = $this->getInstance($initial); 17 | $this->assertEquals(json_encode($expected), json_encode($instance)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Set/_isset.php: -------------------------------------------------------------------------------- 1 | getInstance(['a', 'b', 'c']); 9 | $this->expectArrayAccessUnsupportedException(); 10 | isset($set[0]); 11 | } 12 | 13 | public function testArrayAccessIssetByMethod() 14 | { 15 | $set = $this->getInstance(['a', 'b', 'c']); 16 | $this->expectArrayAccessUnsupportedException(); 17 | $set->offsetExists(0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Set/_clone.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | 13 | $clone = clone $instance; 14 | 15 | $this->assertEquals(get_class($instance), get_class($clone)); 16 | $this->assertEquals($instance->toArray(), $clone->toArray()); 17 | $this->assertFalse($clone === $instance); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Map/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance(['a' => 1]); 10 | unset($instance['a']); 11 | $this->assertToArray([], $instance); 12 | } 13 | 14 | public function testArrayAccessUnsetByReference() 15 | { 16 | $instance = $this->getInstance(['a' => [1]]); 17 | unset($instance['a'][0]); 18 | 19 | $this->assertToArray(['a' => []], $instance); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/Queue/_clone.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | 13 | $clone = clone $instance; 14 | 15 | $this->assertEquals(get_class($instance), get_class($clone)); 16 | $this->assertEquals($instance->toArray(), $clone->toArray()); 17 | $this->assertFalse($clone === $instance); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Stack/_clone.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | 13 | $clone = clone $instance; 14 | 15 | $this->assertEquals(get_class($instance), get_class($clone)); 16 | $this->assertEquals($instance->toArray(), $clone->toArray()); 17 | $this->assertFalse($clone === $instance); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_clone.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | 13 | $clone = clone $instance; 14 | 15 | $this->assertEquals(get_class($instance), get_class($clone)); 16 | $this->assertEquals($instance->toArray(), $clone->toArray()); 17 | $this->assertFalse($clone === $instance); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/Stack/_serialize.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 16 | $this->assertSerialized($expected, $instance, false); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/PriorityQueue/clear.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | 10 | foreach (range(1, self::MANY) as $i) { 11 | $instance->push($i, rand()); 12 | } 13 | 14 | $this->assertCount(self::MANY, $instance); 15 | 16 | $instance->clear(); 17 | $this->assertCount(0, $instance); 18 | $this->assertTrue($instance->isEmpty()); 19 | $this->assertToArray([], $instance); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/HashableObject.php: -------------------------------------------------------------------------------- 1 | value = $value; 17 | $this->hash = func_num_args() === 1 ? $value : $hash; 18 | } 19 | 20 | public function equals($obj): bool { 21 | return $obj->value === $this->value; 22 | } 23 | 24 | public function hash() { 25 | return $this->hash; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_serialize.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], ['b' => 2, 'a' => 1] 11 | ], 12 | ]; 13 | } 14 | 15 | /** 16 | * @dataProvider serializeDataProvider 17 | */ 18 | public function testSerialize(array $values, array $expected) 19 | { 20 | $instance = $this->getInstance($values); 21 | $this->assertSerialized($expected, $instance, true); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Set/sort.php: -------------------------------------------------------------------------------- 1 | getInstance([4, 1, 2, 5, 3]); 9 | 10 | $instance->sort(); 11 | $this->assertToArray([1, 2, 3, 4, 5], $instance); 12 | } 13 | 14 | public function testSortUsingComparator() 15 | { 16 | $instance = $this->getInstance([4, 1, 2, 5, 3]); 17 | 18 | $instance->sort(function($a, $b) { 19 | return $b <=> $a; 20 | }); 21 | 22 | $this->assertToArray([5, 4, 3, 2, 1], $instance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Set/reverse.php: -------------------------------------------------------------------------------- 1 | basicDataProvider() 10 | ); 11 | } 12 | 13 | /** 14 | * @dataProvider reverseDataProvider 15 | */ 16 | public function testReverse(array $values, array $expected) 17 | { 18 | $instance = $this->getInstance($values); 19 | $instance->reverse(); 20 | 21 | $this->assertToArray($expected, $instance); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Sequence/sort.php: -------------------------------------------------------------------------------- 1 | getInstance([4, 1, 2, 5, 3]); 9 | $instance->sort(); 10 | 11 | $this->assertToArray([1, 2, 3, 4, 5], $instance); 12 | } 13 | 14 | public function testSortUsingComparator() 15 | { 16 | $instance = $this->getInstance([4, 1, 2, 5, 3]); 17 | 18 | $instance->sort(function($a, $b) { 19 | return $b <=> $a; 20 | }); 21 | 22 | $this->assertToArray([5, 4, 3, 2, 1], $instance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Set/count.php: -------------------------------------------------------------------------------- 1 | getUniqueAndDuplicateData(); 9 | 10 | $instance = $this->getInstance($unique); 11 | $this->assertCount(count($unique), $instance); 12 | 13 | $instance = $this->getInstance($duplicates); 14 | $this->assertCount(count($unique), $instance); 15 | } 16 | 17 | public function testCountEmpty() 18 | { 19 | $instance = $this->getInstance(); 20 | $this->assertCount(0, $instance); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Sequence/reverse.php: -------------------------------------------------------------------------------- 1 | basicDataProvider() 10 | ); 11 | } 12 | 13 | /** 14 | * @dataProvider reverseDataProvider 15 | */ 16 | public function testReverse(array $values, array $expected) 17 | { 18 | $instance = $this->getInstance($values); 19 | $instance->reverse(); 20 | 21 | $this->assertToArray($expected, $instance); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Map/reversed.php: -------------------------------------------------------------------------------- 1 | basicDataProvider()); 13 | } 14 | 15 | /** 16 | * @dataProvider reversedDataProvider 17 | */ 18 | public function testReversed(array $values, array $expected) 19 | { 20 | $instance = $this->getInstance($values); 21 | $this->assertToArray($expected, $instance->reversed()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Sequence/reversed.php: -------------------------------------------------------------------------------- 1 | basicDataProvider() 10 | ); 11 | } 12 | 13 | /** 14 | * @dataProvider reversedDataProvider 15 | */ 16 | public function testReversed(array $values, array $expected) 17 | { 18 | $instance = $this->getInstance($values); 19 | $this->assertToArray($expected, $instance->reversed()); 20 | $this->assertToArray($values, $instance); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/Map/_clone.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider cloneDataProvider 13 | */ 14 | public function testClone($values, array $expected) 15 | { 16 | $instance = $this->getInstance($values); 17 | 18 | $clone = clone $instance; 19 | 20 | $this->assertEquals(get_class($instance), get_class($clone)); 21 | $this->assertEquals($instance->toArray(), $clone->toArray()); 22 | $this->assertFalse($clone === $instance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Map/reverse.php: -------------------------------------------------------------------------------- 1 | basicDataProvider()); 13 | } 14 | 15 | /** 16 | * @dataProvider reverseDataProvider 17 | */ 18 | public function testReverse(array $values, array $expected) 19 | { 20 | $instance = $this->getInstance($values); 21 | $instance->reverse(); 22 | 23 | $this->assertToArray($expected, $instance); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Sequence/_clone.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider cloneDataProvider 13 | */ 14 | public function testClone($values, array $expected) 15 | { 16 | $instance = $this->getInstance($values); 17 | 18 | $clone = clone $instance; 19 | 20 | $this->assertEquals(get_class($instance), get_class($clone)); 21 | $this->assertEquals($instance->toArray(), $clone->toArray()); 22 | $this->assertFalse($clone === $instance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Pair/__set.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $pair->key = 'b'; 10 | $this->assertEquals('b', $pair->key); 11 | } 12 | 13 | public function testPropertySetValue() 14 | { 15 | $pair = $this->getPair('a', 1); 16 | $pair->value = 2; 17 | $this->assertEquals(2, $pair->value); 18 | } 19 | 20 | public function testSetSelf() 21 | { 22 | $pair = $this->getPair('a', 1); 23 | $pair->value = $pair; 24 | $this->assertEquals($pair, $pair->value); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Set/reversed.php: -------------------------------------------------------------------------------- 1 | basicDataProvider() 10 | ); 11 | } 12 | 13 | /** 14 | * @dataProvider reversedDataProvider 15 | */ 16 | public function testReversed(array $values, array $expected) 17 | { 18 | $instance = $this->getInstance($values); 19 | $reversed = $instance->reversed(); 20 | 21 | $this->assertToArray($expected, $reversed); 22 | $this->assertToArray($values, $instance); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Map/keys.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], ['a', 'b']], 11 | [range(0, self::MANY), range(0, self::MANY)], 12 | ]; 13 | } 14 | 15 | /** 16 | * @dataProvider keysDataProvider 17 | */ 18 | public function testKeys(array $initial, array $expected) 19 | { 20 | $instance = $this->getInstance($initial); 21 | $keys = $instance->keys(); 22 | 23 | $this->assertInstanceOf(\Ds\Set::class, $keys); 24 | $this->assertEquals($expected, $keys->toArray()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Pair/_empty.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $this->assertFalse(empty($pair->key)); 10 | $this->assertTrue (empty($pair->nope)); 11 | 12 | $pair = $this->getPair(false, 1); 13 | $this->assertTrue (empty($pair->key)); 14 | } 15 | 16 | public function testPropertyEmptyValue() 17 | { 18 | $pair = $this->getPair('a', 1); 19 | $this->assertFalse(empty($pair->value)); 20 | $this->assertTrue (empty($pair->nope)); 21 | 22 | $pair = $this->getPair('a', false); 23 | $this->assertTrue (empty($pair->value)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Map/values.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], [1, 2]], 15 | 16 | [range(0, self::MANY), range(0, self::MANY)], 17 | ]; 18 | } 19 | 20 | /** 21 | * @dataProvider valuesDataProvider 22 | */ 23 | public function testValues(array $initial, array $expected) 24 | { 25 | $instance = $this->getInstance($initial); 26 | $values = $instance->values(); 27 | 28 | $this->assertInstanceOf(Vector::class, $values); 29 | $this->assertEquals($expected, $values->toArray()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Set/sorted.php: -------------------------------------------------------------------------------- 1 | getInstance([4, 1, 2, 5, 3]); 9 | 10 | $sorted = $instance->sorted(); 11 | 12 | $this->assertToArray([1, 2, 3, 4, 5], $sorted); 13 | $this->assertToArray([4, 1, 2, 5, 3], $instance); 14 | } 15 | 16 | public function testSortedUsingComparator() 17 | { 18 | $instance = $this->getInstance([4, 1, 2, 5, 3]); 19 | 20 | $sorted = $instance->sorted(function($a, $b) { 21 | return $b <=> $a; 22 | }); 23 | 24 | $this->assertToArray([5, 4, 3, 2, 1], $sorted); 25 | $this->assertToArray([4, 1, 2, 5, 3], $instance); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Sequence/sorted.php: -------------------------------------------------------------------------------- 1 | getInstance([4, 1, 2, 5, 3]); 9 | $sorted = $instance->sorted(); 10 | 11 | $this->assertToArray([1, 2, 3, 4, 5], $sorted); 12 | $this->assertToArray([4, 1, 2, 5, 3], $instance); 13 | } 14 | 15 | public function testSortedUsingComparator() 16 | { 17 | $instance = $this->getInstance([4, 1, 2, 5, 3]); 18 | 19 | $sorted = $instance->sorted(function($a, $b) { 20 | return $b <=> $a; 21 | }); 22 | 23 | $this->assertToArray([5, 4, 3, 2, 1], $sorted); 24 | $this->assertToArray([4, 1, 2, 5, 3], $instance); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Pair/__get.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | 10 | $this->assertEquals('a', $pair->key); 11 | $this->assertEquals( 1, $pair->value); 12 | } 13 | 14 | public function testReflection() 15 | { 16 | $pair = $this->getPair('a', 'b'); 17 | 18 | $key = new \ReflectionProperty($pair, 'key'); 19 | $val = new \ReflectionProperty($pair, 'value'); 20 | 21 | $this->assertEquals('a', $key->getValue($pair)); 22 | $this->assertEquals('b', $val->getValue($pair)); 23 | 24 | $class = new \ReflectionClass(\Ds\Pair::class); 25 | 26 | $this->assertCount(2, $class->getProperties()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Set/last.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $this->assertEquals($expected, $instance->last()); 23 | } 24 | 25 | public function testLastNotAllowedWhenEmpty() 26 | { 27 | $instance = $this->getInstance(); 28 | $this->expectEmptyNotAllowedException(); 29 | $instance->last(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Sequence/last.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $this->assertEquals($expected, $instance->last()); 23 | } 24 | 25 | public function testLastNotAllowedWhenEmpty() 26 | { 27 | $instance = $this->getInstance(); 28 | $this->expectEmptyNotAllowedException(); 29 | $instance->last(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Set/first.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $this->assertEquals($expected, $instance->first()); 23 | } 24 | 25 | public function testFirstNowAllowedWhenEmpty() 26 | { 27 | $instance = $this->getInstance(); 28 | $this->expectEmptyNotAllowedException(); 29 | $instance->first(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Sequence/first.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $this->assertEquals($expected, $instance->first()); 23 | } 24 | 25 | public function testFirstNowAllowedWhenEmpty() 26 | { 27 | $instance = $this->getInstance(); 28 | $this->expectEmptyNotAllowedException(); 29 | $instance->first(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/PairTest.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 32 | $this->assertTrue((bool) $instance); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Map/_var_dump.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 33 | $this->assertInstanceDump($expected, $instance); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Queue/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | 10 | $instance->push('a'); 11 | $instance->push('b'); 12 | $instance->push('c'); 13 | 14 | $data = []; 15 | 16 | foreach ($instance as $value) { 17 | $data[] = $value; 18 | } 19 | 20 | $this->assertEquals(['a', 'b', 'c'], $data); 21 | 22 | // Test that foreach is destructive. 23 | $this->assertTrue($instance->isEmpty()); 24 | $this->assertCount(0, $instance); 25 | $this->assertToArray([], $instance); 26 | 27 | // Just to make sure that an iteration attempt can still be made. 28 | foreach ($instance as $value) {} 29 | foreach ($instance as $value) {} 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Stack/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | 10 | $instance->push('a'); 11 | $instance->push('b'); 12 | $instance->push('c'); 13 | 14 | $data = []; 15 | 16 | foreach ($instance as $value) { 17 | $data[] = $value; 18 | } 19 | 20 | $this->assertEquals(['c', 'b', 'a'], $data); 21 | 22 | // Test that foreach is destructive. 23 | $this->assertTrue($instance->isEmpty()); 24 | $this->assertCount(0, $instance); 25 | $this->assertToArray([], $instance); 26 | 27 | // Just to make sure that an iteration attempt can still be made. 28 | foreach ($instance as $value) {} 29 | foreach ($instance as $value) {} 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Queue/peek.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 21 | 22 | $value = $instance->peek(); 23 | 24 | $this->assertToArray($initial, $instance); 25 | $this->assertEquals($returned, $value); 26 | } 27 | 28 | public function testPeekNotAllowedWhenEmpty() 29 | { 30 | $instance = $this->getInstance(); 31 | $this->expectEmptyNotAllowedException(); 32 | $instance->peek(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Map/last.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $last = $instance->last(); 23 | 24 | $this->assertEquals($expected, [$last->key, $last->value]); 25 | } 26 | 27 | public function testLastNotAllowedWhenEmpty() 28 | { 29 | $instance = $this->getInstance(); 30 | $this->expectEmptyNotAllowedException(); 31 | $instance->last(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/PriorityQueue/_foreach.php: -------------------------------------------------------------------------------- 1 | getInstance(); 9 | 10 | $instance->push('a', 1); 11 | $instance->push('c', 3); 12 | $instance->push('b', 2); 13 | 14 | $data = []; 15 | 16 | foreach ($instance as $value) { 17 | $data[] = $value; 18 | } 19 | 20 | $this->assertEquals(['c', 'b', 'a'], $data); 21 | 22 | // Test that foreach is destructive. 23 | $this->assertTrue($instance->isEmpty()); 24 | $this->assertCount(0, $instance); 25 | $this->assertToArray([], $instance); 26 | 27 | // Just to make sure that an iteration attempt can still be made. 28 | foreach ($instance as $value) {} 29 | foreach ($instance as $value) {} 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Stack/peek.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 21 | 22 | $value = $instance->peek(); 23 | 24 | $this->assertToArray(array_reverse($initial), $instance); 25 | $this->assertEquals($returned, $value); 26 | } 27 | 28 | public function testPeekNotAllowedWhenEmpty() 29 | { 30 | $instance = $this->getInstance(); 31 | $this->expectEmptyNotAllowedException(); 32 | $instance->peek(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Map/first.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | $first = $instance->first(); 23 | 24 | $this->assertEquals($expected, [$first->key, $first->value]); 25 | } 26 | 27 | public function testFirstNowAllowedWhenEmpty() 28 | { 29 | $instance = $this->getInstance(); 30 | $this->expectEmptyNotAllowedException(); 31 | $instance->first(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/PriorityQueue/peek.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], 'b'], 11 | [['a' => 2, 'b' => 1], 'a'], 12 | [['a' => 1, 'b' => 1], 'a'], 13 | ]; 14 | } 15 | 16 | /** 17 | * @dataProvider peekDataProvider 18 | */ 19 | public function testPeek(array $initial, $expected) 20 | { 21 | $instance = $this->getInstance($initial); 22 | $this->assertEquals($expected, $instance->peek()); 23 | $this->assertCount(count($initial), $instance); 24 | } 25 | 26 | public function testPeekNotAllowedWhenEmpty() 27 | { 28 | $instance = $this->getInstance(); 29 | $this->expectEmptyNotAllowedException(); 30 | $instance->peek(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/PriorityQueue/allocate.php: -------------------------------------------------------------------------------- 1 | getInstance(); 29 | 30 | $instance->allocate($initial); 31 | $instance->allocate($allocate); 32 | $this->assertEquals($expected, $instance->capacity()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/PriorityQueue/copy.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 12 | $copy = $instance->copy(); 13 | 14 | $this->assertEquals($instance->toArray(), $copy->toArray()); 15 | $this->assertEquals(count($instance), count($copy)); 16 | } 17 | 18 | public function testCopyDoesNotAffectSubject() 19 | { 20 | $instance = $this->getInstance(); 21 | $instance->push('a', 1); 22 | $instance->push('b', 2); 23 | $instance->push('c', 3); 24 | 25 | $copy = $instance->copy(); 26 | 27 | $instance->pop(); 28 | 29 | $this->assertEquals(2, count($instance)); 30 | $this->assertEquals(3, count($copy)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Sequence/find.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 33 | $this->assertEquals($expected, $instance->find($value)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Queue/isEmpty.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 21 | $this->assertEquals($isEmpty, $instance->isEmpty()); 22 | } 23 | 24 | public function testIsNotEmptyAfterPop() 25 | { 26 | $instance = $this->getInstance(); 27 | $this->assertTrue($instance->isEmpty()); 28 | 29 | $instance->push('a'); 30 | $this->assertFalse($instance->isEmpty()); 31 | 32 | $instance->pop(); 33 | $this->assertTrue($instance->isEmpty()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Stack/isEmpty.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 21 | $this->assertEquals($isEmpty, $instance->isEmpty()); 22 | } 23 | 24 | public function testIsNotEmptyAfterPop() 25 | { 26 | $instance = $this->getInstance(); 27 | $this->assertTrue($instance->isEmpty()); 28 | 29 | $instance->push('a'); 30 | $this->assertFalse($instance->isEmpty()); 31 | 32 | $instance->pop(); 33 | $this->assertTrue($instance->isEmpty()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Set/isEmpty.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 21 | $this->assertEquals($isEmpty, $instance->isEmpty()); 22 | } 23 | 24 | public function testIsNotEmptyAfterRemove() 25 | { 26 | $instance = $this->getInstance(); 27 | $this->assertTrue($instance->isEmpty()); 28 | 29 | $instance->add('a'); 30 | $this->assertFalse($instance->isEmpty()); 31 | 32 | $instance->remove('a'); 33 | $this->assertTrue($instance->isEmpty()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Sequence/isEmpty.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 21 | $this->assertEquals($isEmpty, $instance->isEmpty()); 22 | } 23 | 24 | public function testIsNotEmptyAfterRemove() 25 | { 26 | $instance = $this->getInstance(); 27 | $this->assertTrue($instance->isEmpty()); 28 | 29 | $instance->push('a'); 30 | $this->assertFalse($instance->isEmpty()); 31 | 32 | $instance->remove(0); 33 | $this->assertTrue($instance->isEmpty()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Map/isEmpty.php: -------------------------------------------------------------------------------- 1 | 1], false], 12 | ]; 13 | } 14 | 15 | /** 16 | * @dataProvider isEmptyDataProvider 17 | */ 18 | public function testIsEmpty(array $values, bool $isEmpty) 19 | { 20 | $instance = $this->getInstance($values); 21 | $this->assertEquals($isEmpty, $instance->isEmpty()); 22 | } 23 | 24 | public function testIsNotEmptyAfterRemove() 25 | { 26 | $instance = $this->getInstance(); 27 | $this->assertTrue($instance->isEmpty()); 28 | 29 | $instance->put('a', 1); 30 | $this->assertFalse($instance->isEmpty()); 31 | 32 | $instance->remove('a'); 33 | $this->assertTrue($instance->isEmpty()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Queue/__construct.php: -------------------------------------------------------------------------------- 1 | sample()], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider constructDataProvider 21 | */ 22 | public function testConstruct(array $values) 23 | { 24 | $this->assertToArray($values, new Queue($values)); 25 | } 26 | 27 | /** 28 | * @dataProvider constructDataProvider 29 | */ 30 | public function testConstructUsingIterable(array $values) 31 | { 32 | $this->assertToArray($values, new Queue(new \ArrayIterator($values))); 33 | } 34 | 35 | public function testConstructNoParams() 36 | { 37 | $this->assertToArray([], new Queue()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Stack/__construct.php: -------------------------------------------------------------------------------- 1 | sample()], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider constructDataProvider 21 | */ 22 | public function testConstruct(array $values) 23 | { 24 | $this->assertToArray(array_reverse($values), new Stack($values)); 25 | } 26 | 27 | /** 28 | * @dataProvider constructDataProvider 29 | */ 30 | public function testConstructUsingIterable(array $values) 31 | { 32 | $this->assertToArray(array_reverse($values), new Stack(new \ArrayIterator($values))); 33 | } 34 | 35 | public function testConstructNoParams() 36 | { 37 | $this->assertToArray([], new Stack()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Map/sum.php: -------------------------------------------------------------------------------- 1 | =') ? [true, false, null] : ["a", true, false, null]; 9 | return [ 10 | 11 | // Empty 12 | [[], 0], 13 | 14 | // Basic integer sum 15 | [[1, 2, 3], 6], 16 | 17 | // Basic float sum 18 | [[1.5, 2.5, 5.1], 9.1], 19 | 20 | // Mixed numeric 21 | [[1.5, 3], 4.5], 22 | 23 | // Numeric strings 24 | [["2", "5", "10.5"], 17.5], 25 | 26 | // Non-numbers 27 | [$nonNumbers, 1], 28 | ]; 29 | } 30 | 31 | /** 32 | * @dataProvider sumDataProvider 33 | */ 34 | public function testSum($values, $expected) 35 | { 36 | $instance = $this->getInstance($values); 37 | $this->assertEquals($expected, $instance->sum()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Set/sum.php: -------------------------------------------------------------------------------- 1 | =') ? [true, false, null] : ["a", true, false, null]; 9 | return [ 10 | 11 | // Empty 12 | [[], 0], 13 | 14 | // Basic integer sum 15 | [[1, 2, 3], 6], 16 | 17 | // Basic float sum 18 | [[1.5, 2.5, 5.1], 9.1], 19 | 20 | // Mixed numeric 21 | [[1.5, 3], 4.5], 22 | 23 | // Numeric strings 24 | [["2", "5", "10.5"], 17.5], 25 | 26 | // Non-numbers 27 | [$nonNumbers, 1], 28 | ]; 29 | } 30 | 31 | /** 32 | * @dataProvider sumDataProvider 33 | */ 34 | public function testSum($values, $expected) 35 | { 36 | $instance = $this->getInstance($values); 37 | $this->assertEquals($expected, $instance->sum()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Map/allocate.php: -------------------------------------------------------------------------------- 1 | getInstance(); 31 | 32 | $instance->allocate($initial); 33 | $instance->allocate($allocate); 34 | $this->assertEquals($expected, $instance->capacity()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Set/allocate.php: -------------------------------------------------------------------------------- 1 | getInstance(); 31 | 32 | $instance->allocate($initial); 33 | $instance->allocate($allocate); 34 | $this->assertEquals($expected, $instance->capacity()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Vector/allocate.php: -------------------------------------------------------------------------------- 1 | getInstance(); 31 | 32 | $instance->allocate($initial); 33 | $instance->allocate($allocate); 34 | $this->assertEquals($expected, $instance->capacity()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Deque/allocate.php: -------------------------------------------------------------------------------- 1 | getInstance(); 31 | 32 | $instance->allocate($initial); 33 | $instance->allocate($allocate); 34 | $this->assertEquals($expected, $instance->capacity()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Sequence/sum.php: -------------------------------------------------------------------------------- 1 | =') ? [true, false, null] : ["a", true, false, null]; 9 | return [ 10 | 11 | // Empty 12 | [[], 0], 13 | 14 | // Basic integer sum 15 | [[1, 2, 3], 6], 16 | 17 | // Basic float sum 18 | [[1.5, 2.5, 5.1], 9.1], 19 | 20 | // Mixed numeric 21 | [[1.5, 3], 4.5], 22 | 23 | // Numeric strings 24 | [["2", "5", "10.5"], 17.5], 25 | 26 | // Non-numbers 27 | [$nonNumbers, 1], 28 | ]; 29 | } 30 | 31 | /** 32 | * @dataProvider sumDataProvider 33 | */ 34 | public function testSum($values, $expected) 35 | { 36 | $instance = $this->getInstance($values); 37 | $this->assertEquals($expected, $instance->sum()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Map/hasKey.php: -------------------------------------------------------------------------------- 1 | 1], 'a', true], 12 | [['a' => 1], 'b', false], 13 | ]; 14 | } 15 | 16 | /** 17 | * @dataProvider hasKeyDataProvider 18 | */ 19 | public function testHasKey(array $initial, $key, bool $has) 20 | { 21 | $instance = $this->getInstance($initial); 22 | $this->assertEquals($has, $instance->hasKey($key)); 23 | } 24 | 25 | public function testHasKeyAfterRemoveAndPut() 26 | { 27 | $instance = $this->getInstance(['a' => 1]); 28 | $this->assertTrue($instance->hasKey('a')); 29 | 30 | $instance->remove('a'); 31 | $this->assertFalse($instance->hasKey('a')); 32 | 33 | $instance->put('a', 1); 34 | $this->assertTrue($instance->hasKey('a')); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Map/__construct.php: -------------------------------------------------------------------------------- 1 | 1], 13 | ['a' => 1, 'b' => 2], 14 | ['a' => 1, 'b' => 2, 'c' => 3], 15 | $this->sample(), 16 | ]); 17 | } 18 | 19 | /** 20 | * @dataProvider constructDataProvider 21 | */ 22 | public function testConstruct(array $values, array $expected) 23 | { 24 | $this->assertToArray($expected, new Map($values)); 25 | } 26 | 27 | /** 28 | * @dataProvider constructDataProvider 29 | */ 30 | public function testConstructUsingNonArrayIterable(array $values, array $expected) 31 | { 32 | $this->assertToArray($expected, new Map(new \ArrayIterator($values))); 33 | } 34 | 35 | public function testConstructNoParams() 36 | { 37 | $this->assertToArray([], new Map()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Deque/__construct.php: -------------------------------------------------------------------------------- 1 | sample(), 16 | range(1, self::MANY), 17 | ]); 18 | } 19 | 20 | /** 21 | * @dataProvider constructDataProvider 22 | */ 23 | public function testConstruct($values, array $expected) 24 | { 25 | $this->assertToArray($expected, new Deque($values)); 26 | } 27 | 28 | /** 29 | * @dataProvider constructDataProvider 30 | */ 31 | public function testConstructUsingNonArrayIterable(array $values, array $expected) 32 | { 33 | $this->assertToArray($expected, new Deque(new \ArrayIterator($values))); 34 | } 35 | 36 | public function testConstructNoParams() 37 | { 38 | $this->assertToArray([], new Deque()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Vector/__construct.php: -------------------------------------------------------------------------------- 1 | sample(), 16 | range(1, self::MANY), 17 | ]); 18 | } 19 | 20 | /** 21 | * @dataProvider constructDataProvider 22 | */ 23 | public function testConstruct($values, array $expected) 24 | { 25 | $this->assertToArray($expected, new Vector($values)); 26 | } 27 | 28 | /** 29 | * @dataProvider constructDataProvider 30 | */ 31 | public function testConstructUsingNonArrayIterable(array $values, array $expected) 32 | { 33 | $this->assertToArray($expected, new Vector(new \ArrayIterator($values))); 34 | } 35 | 36 | public function testConstructNoParams() 37 | { 38 | $this->assertToArray([], new Vector()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Pair/_isset.php: -------------------------------------------------------------------------------- 1 | getPair('a', 1); 9 | $this->assertTrue (isset($pair->key)); 10 | $this->assertFalse(isset($pair->nope)); 11 | 12 | $pair = $this->getPair(null, 1); 13 | $this->assertFalse (isset($pair->key)); 14 | } 15 | 16 | public function testPropertyIssetValue() 17 | { 18 | $pair = $this->getPair('a', 1); 19 | $this->assertTrue (isset($pair->value)); 20 | $this->assertFalse(isset($pair->nope)); 21 | 22 | $pair = $this->getPair('a', null); 23 | $this->assertFalse (isset($pair->value)); 24 | } 25 | 26 | public function testPropertyExists() 27 | { 28 | $pair = $this->getPair('a', 1); 29 | 30 | $this->assertTrue(property_exists($pair, 'key')); 31 | $this->assertTrue(property_exists($pair, 'value')); 32 | 33 | $this->assertFalse(property_exists($pair, 'foo')); 34 | $this->assertFalse(property_exists($pair, 'bar')); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Map/hasValue.php: -------------------------------------------------------------------------------- 1 | 1], 1, true], 12 | [['a' => 1], 2, false], 13 | [['a' => null], null, true], 14 | ]; 15 | } 16 | 17 | /** 18 | * @dataProvider hasValueDataProvider 19 | */ 20 | public function testHasValue(array $initial, $value, bool $expected) 21 | { 22 | $instance = $this->getInstance($initial); 23 | $this->assertEquals($expected, $instance->hasValue($value)); 24 | } 25 | 26 | public function testHasValueAfterRemoveAndPut() 27 | { 28 | $instance = $this->getInstance(['a' => 1]); 29 | $this->assertTrue($instance->hasValue(1)); 30 | 31 | $instance->remove('a'); 32 | $this->assertFalse($instance->hasValue(1)); 33 | 34 | $instance->put('a', 1); 35 | $this->assertTrue($instance->hasValue(1)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Set/join.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 29 | $expected = join($glue, $values); 30 | $this->assertEquals($expected, $instance->join($glue)); 31 | } 32 | 33 | /** 34 | * @dataProvider joinDataProvider 35 | */ 36 | public function testJoinWithoutGlue(array $values, $glue) 37 | { 38 | $instance = $this->getInstance($values); 39 | $expected = join($values); 40 | $this->assertEquals($expected, $instance->join()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/Sequence/contains.php: -------------------------------------------------------------------------------- 1 | sample(); 9 | 10 | // initial, values, expected 11 | return [ 12 | [[1, 2, 3], [], true], // Empty set is a subset of any set 13 | 14 | [['a'], ['a'], true], 15 | [[ 1 ], [ 1 ], true], 16 | [[ 1 ], [ ], true], 17 | [['a'], ['b'], false], 18 | [[ ], [ 1 ], false], 19 | [['1'], [ 1 ], false], 20 | [[ 1 ], ['1'], false], 21 | 22 | [[], $sample, false], 23 | [$sample, $sample, true], 24 | 25 | [array_slice($sample, 1), $sample, false], 26 | [$sample, array_slice($sample, 1), true], 27 | ]; 28 | } 29 | 30 | /** 31 | * @dataProvider containsDataProvider 32 | */ 33 | public function testContains($initial, array $values, bool $expected) 34 | { 35 | $instance = $this->getInstance($initial); 36 | $this->assertEquals($expected, $instance->contains(...$values)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Set/contains.php: -------------------------------------------------------------------------------- 1 | getUniqueAndDuplicateData(); 9 | 10 | // initial, values, contains 11 | return [ 12 | 13 | // Test empty set is a subset of any other set. 14 | [[], [], true], 15 | [$sample, [], true], 16 | 17 | // Test that an empty set does not contain any values. 18 | [[], $sample, false], 19 | 20 | // Test that a sample set contains all sample values. 21 | [$sample, $sample, true], 22 | 23 | // Test that a sample set contains all duplicate values. 24 | [$sample, $duplicates, true], 25 | ]; 26 | } 27 | 28 | /** 29 | * @dataProvider containsDataProvider 30 | */ 31 | public function testContains( 32 | array $initial, 33 | array $values, 34 | bool $contains 35 | ) { 36 | $set = $this->getInstance($initial); 37 | $this->assertEquals($contains, $set->contains(...$values)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Set/__construct.php: -------------------------------------------------------------------------------- 1 | getUniqueAndDuplicateData(); 11 | 12 | return [ 13 | [[], []], 14 | [['a'], ['a']], 15 | [['a', 'a'], ['a']], 16 | [['a', 'b'], ['a', 'b']], 17 | [$unique, $unique], 18 | [$duplicated, $unique], 19 | ]; 20 | } 21 | 22 | /** 23 | * @dataProvider constructDataProvider 24 | */ 25 | public function testConstruct(array $values, array $expected) 26 | { 27 | $this->assertToArray($expected, new Set($values)); 28 | } 29 | 30 | /** 31 | * @dataProvider constructDataProvider 32 | */ 33 | public function testConstructUsingIterable(array $values, array $expected) 34 | { 35 | $this->assertToArray($expected, new Set(new \ArrayIterator($values))); 36 | } 37 | 38 | public function testConstructNoParams() 39 | { 40 | $this->assertToArray([], new Set()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/Map/_empty.php: -------------------------------------------------------------------------------- 1 | 0 ], 'a', true ], 12 | [['a' => 1 ], 'a', false], 13 | [['a' => false], 'a', true ], 14 | [['a' => null ], 'a', true ], 15 | [[ ], 'a', true ], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider emptyDataProvider 21 | */ 22 | public function testArrayAccessEmpty(array $initial, $key, bool $empty) 23 | { 24 | $instance = $this->getInstance(); 25 | 26 | foreach ($initial as $key => $value) { 27 | $instance->put($key, $value); 28 | } 29 | 30 | $this->assertEquals($empty, empty($instance[$key])); 31 | } 32 | 33 | /** 34 | * @dataProvider emptyDataProvider 35 | */ 36 | public function testArrayAccessEmptyByReference(array $initial, $key, bool $empty) 37 | { 38 | $instance = $this->getInstance([$initial]); 39 | $this->assertEquals($empty, empty($instance[0][$key])); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /tests/Set/merge.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 24 | 25 | $this->assertToArray($expected, $instance->merge($values)); 26 | $this->assertToArray($initial, $instance); 27 | } 28 | 29 | /** 30 | * @dataProvider mergeDataProvider 31 | */ 32 | public function testMergeWithSelf(array $initial, array $values, array $expected) 33 | { 34 | $instance = $this->getInstance($initial); 35 | 36 | $this->assertToArray($initial, $instance->merge($instance)); 37 | $this->assertToArray($initial, $instance); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Sequence/unshift.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider unshiftDataProvider 13 | */ 14 | public function testUnshiftVariadic(array $initial, array $values) 15 | { 16 | $instance = $this->getInstance($initial); 17 | 18 | $instance->unshift(...$values); 19 | $expected = array_merge($values, $initial); 20 | 21 | $this->assertToArray($expected, $instance); 22 | $this->assertEquals(count($expected), count($instance)); 23 | } 24 | 25 | /** 26 | * @dataProvider unshiftDataProvider 27 | */ 28 | public function testUnshift(array $initial, array $values) 29 | { 30 | $instance = $this->getInstance($initial); 31 | 32 | foreach ($values as $value) { 33 | $instance->unshift($value); 34 | } 35 | 36 | $expected = array_merge(array_reverse($values), $initial); 37 | 38 | $this->assertToArray($expected, $instance); 39 | $this->assertEquals(count($expected), count($instance)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/QueueTest.php: -------------------------------------------------------------------------------- 1 | getInstance(); 41 | $this->expectArrayAccessUnsupportedException(); 42 | $set['a'] = 1; 43 | } 44 | 45 | public function testImplementsArrayAccess() 46 | { 47 | $this->assertInstanceOf(ArrayAccess::class, $this->getInstance()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Sequence/pop.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 21 | 22 | $result = $instance->pop(); 23 | 24 | $this->assertToArray($expected, $instance); 25 | $this->assertEquals($returned, $result); 26 | $this->assertEquals(count($initial) - 1, count($instance)); 27 | 28 | // 29 | $instance = $this->getInstance(); 30 | $this->expectEmptyNotAllowedException(); 31 | $instance->pop(); 32 | } 33 | 34 | public function testPopAll() 35 | { 36 | $instance = $this->getInstance(range(1, self::MANY)); 37 | 38 | while ( ! $instance->isEmpty()) { 39 | $instance->pop(); 40 | } 41 | 42 | $this->assertEquals(count($instance), 0); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/PriorityQueue/pop.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], 'b', ['a']], 11 | [['a' => 2, 'b' => 1], 'a', ['b']], 12 | [['a' => 1, 'b' => 1], 'a', ['b']], 13 | ]; 14 | } 15 | 16 | /** 17 | * @dataProvider popDataProvider 18 | */ 19 | public function testPop(array $initial, $expected, array $result) 20 | { 21 | $instance = $this->getInstance($initial); 22 | 23 | $this->assertEquals($expected, $instance->pop()); 24 | $this->assertToArray($result, $instance); 25 | } 26 | 27 | public function testPopAll() 28 | { 29 | $instance = $this->getInstance(range(1, self::MANY)); 30 | 31 | while ( ! $instance->isEmpty()) { 32 | $instance->pop(); 33 | } 34 | 35 | $this->assertEquals(count($instance), 0); 36 | } 37 | 38 | public function testPopNowAllowedWhenEmpty() 39 | { 40 | $instance = $this->getInstance(); 41 | $this->expectEmptyNotAllowedException(); 42 | $instance->pop(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Sequence/merge.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 24 | 25 | $this->assertToArray($expected, $instance->merge($values)); 26 | $this->assertToArray($initial, $instance); 27 | } 28 | 29 | /** 30 | * @dataProvider mergeDataProvider 31 | */ 32 | public function testMergeWithSelf(array $initial, array $values, array $expected) 33 | { 34 | $instance = $this->getInstance($initial); 35 | 36 | $this->assertToArray(array_merge($initial, $initial), $instance->merge($instance)); 37 | $this->assertToArray($initial, $instance); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Queue/pop.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 21 | 22 | $value = $instance->pop(); 23 | 24 | $this->assertToArray($expected, $instance); 25 | $this->assertEquals($returned, $value); 26 | $this->assertEquals(count($initial) - 1, count($instance)); 27 | } 28 | 29 | public function testPopNotAllowedWhenEmpty() 30 | { 31 | $instance = $this->getInstance(); 32 | $this->expectEmptyNotAllowedException(); 33 | $instance->pop(); 34 | } 35 | 36 | public function testPopAll() 37 | { 38 | $instance = $this->getInstance(range(1, self::MANY)); 39 | 40 | while ( ! $instance->isEmpty()) { 41 | $instance->pop(); 42 | } 43 | 44 | $this->assertEquals(count($instance), 0); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Stack/pop.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 22 | 23 | $result = $instance->pop(); 24 | 25 | $this->assertToArray($expected, $instance); 26 | $this->assertEquals($returned, $result); 27 | $this->assertEquals(count($initial) - 1, count($instance)); 28 | } 29 | 30 | public function testPopAll() 31 | { 32 | $instance = $this->getInstance(range(1, self::MANY)); 33 | 34 | while ( ! $instance->isEmpty()) { 35 | $instance->pop(); 36 | } 37 | 38 | $this->assertEquals(count($instance), 0); 39 | } 40 | 41 | public function testPopNowAllowedWhenEmpty() 42 | { 43 | $instance = $this->getInstance(); 44 | $this->expectEmptyNotAllowedException(); 45 | $instance->pop(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Map/intersect.php: -------------------------------------------------------------------------------- 1 | 1], ['b' => 2], []], 13 | [['a' => 1], ['a' => 2], ['a' => 1]], 14 | [['a' => 1, 'b' => 2], ['a' => 3, 'b' => 4], ['a' => 1, 'b' => 2]], 15 | [['b' => 2, 'a' => 1], ['a' => 3, 'b' => 4], ['b' => 2, 'a' => 1]], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider intersectDataProvider 21 | */ 22 | public function testIntersect(array $a, array $b, array $expected) 23 | { 24 | $a = $this->getInstance($a); 25 | $b = $this->getInstance($b); 26 | 27 | $this->assertEquals($expected, $a->intersect($b)->toArray()); 28 | } 29 | 30 | /** 31 | * @dataProvider intersectDataProvider 32 | */ 33 | public function testIntersectWithSelf(array $a, array $b, array $expected) 34 | { 35 | $map = $this->getInstance($a); 36 | 37 | $this->assertEquals($a, $map->intersect($map)->toArray()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Map/diff.php: -------------------------------------------------------------------------------- 1 | 1], ['a' => 2], []], 13 | [['a' => 1], ['b' => 2], ['a' => 1]], 14 | [['a' => 1, 'b' => 2], ['a' => 3], ['b' => 2]], 15 | [['a' => 1, 'b' => 2], ['b' => 4], ['a' => 1]], 16 | [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['a' => 1, 'b' => 2]], 17 | ]; 18 | } 19 | 20 | /** 21 | * @dataProvider diffDataProvider 22 | */ 23 | public function testDiff(array $a, array $b, array $expected) 24 | { 25 | $a = $this->getInstance($a); 26 | $b = $this->getInstance($b); 27 | 28 | $this->assertEquals($expected, $a->diff($b)->toArray()); 29 | } 30 | 31 | /** 32 | * @dataProvider diffDataProvider 33 | */ 34 | public function testDiffWithSelf(array $a, array $b, array $expected) 35 | { 36 | $map = $this->getInstance($a); 37 | 38 | $this->assertEquals([], $map->diff($map)->toArray()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Map/xor_.php: -------------------------------------------------------------------------------- 1 | 1], ['a' => 2], []], 13 | [['a' => 1], ['b' => 2], ['a' => 1, 'b' => 2]], 14 | [['a' => 1, 'b' => 2], ['a' => 3], ['b' => 2]], 15 | [['a' => 1, 'b' => 2], ['b' => 4], ['a' => 1]], 16 | [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]], 17 | ]; 18 | } 19 | 20 | /** 21 | * @dataProvider xorDataProvider 22 | */ 23 | public function testXor(array $a, array $b, array $expected) 24 | { 25 | $a = $this->getInstance($a); 26 | $b = $this->getInstance($b); 27 | 28 | $this->assertEquals($expected, $a->xor($b)->toArray()); 29 | } 30 | 31 | /** 32 | * @dataProvider xorDataProvider 33 | */ 34 | public function testXorWithSelf(array $a, array $b, array $expected) 35 | { 36 | $map = $this->getInstance($a); 37 | 38 | $this->assertEquals([], $map->xor($map)->toArray()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Sequence/join.php: -------------------------------------------------------------------------------- 1 | getInstance(); 14 | 15 | foreach ($lengths as $length) { 16 | foreach ($glues as $glue) { 17 | $data[] = [range(1, $length), $glue]; // integers 18 | $data[] = [array_fill(0, $length, 'x'), $glue]; // string 19 | $data[] = [array_fill(0, $length, $obj), $glue]; // objects 20 | } 21 | } 22 | 23 | return $data; 24 | } 25 | 26 | /** 27 | * @dataProvider joinDataProvider 28 | */ 29 | public function testJoin(array $values, $glue) 30 | { 31 | $instance = $this->getInstance($values); 32 | $expected = join($glue, $values); 33 | $this->assertEquals($expected, $instance->join($glue)); 34 | } 35 | 36 | /** 37 | * @dataProvider joinDataProvider 38 | */ 39 | public function testJoinWithoutGlue(array $values, $glue) 40 | { 41 | $instance = $this->getInstance($values); 42 | $expected = join($values); 43 | $this->assertEquals($expected, $instance->join()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Map/ksort.php: -------------------------------------------------------------------------------- 1 | 3, 14 | 'c' => 1, 15 | 'b' => 2, 16 | ]], 17 | [[ 18 | 3 => 'd', 19 | 0 => 'a', 20 | 1 => 'b', 21 | 4 => 'e', 22 | 2 => 'c', 23 | ]], 24 | ]; 25 | } 26 | 27 | /** 28 | * @dataProvider sortKeyDataProvider 29 | */ 30 | public function testSortByKey(array $values) 31 | { 32 | $instance = $this->getInstance($values); 33 | 34 | $expected = $values; 35 | ksort($expected); 36 | 37 | $instance->ksort(); 38 | $this->assertToArray($expected, $instance); 39 | } 40 | 41 | /** 42 | * @dataProvider sortKeyDataProvider 43 | */ 44 | public function testSortByKeyUsingComparator(array $values) 45 | { 46 | $instance = $this->getInstance($values); 47 | 48 | $instance->ksort(function($a, $b) { 49 | return $b <=> $a; 50 | }); 51 | 52 | $expected = $values; 53 | krsort($expected); 54 | 55 | $this->assertToArray($expected, $instance); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/Map/merge.php: -------------------------------------------------------------------------------- 1 | 1], ['a' => 1]], 12 | [['a' => 1], ['a' => 2], ['a' => 2]], 13 | [['a' => 1], ['b' => 2], ['a' => 1, 'b' => 2]], 14 | [['b' => 2], ['a' => 1], ['b' => 2, 'a' => 1]], 15 | [['a' => 1, 'b' => 2], ['c' => 3], ['a' => 1, 'b' => 2, 'c' => 3]], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider mergeDataProvider 21 | */ 22 | public function testMerge(array $initial, array $values, array $expected) 23 | { 24 | $instance = $this->getInstance($initial); 25 | 26 | $this->assertToArray($expected, $instance->merge($values)); 27 | $this->assertToArray($initial, $instance); 28 | } 29 | 30 | /** 31 | * @dataProvider mergeDataProvider 32 | */ 33 | public function testMergeWithSelf(array $initial, array $values, array $expected) 34 | { 35 | $instance = $this->getInstance($initial); 36 | 37 | $this->assertToArray($initial, $instance->merge($instance)); 38 | $this->assertToArray($initial, $instance); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/PriorityQueueTest.php: -------------------------------------------------------------------------------- 1 | $priority) { 34 | $queue->push($value, (int) $priority); 35 | } 36 | 37 | return $queue; 38 | } 39 | 40 | public function basicDataProvider() 41 | { 42 | return [ 43 | [[], []], 44 | [['a' => 1], ['a']], 45 | [['a' => 1, 'b' => 2], ['b', 'a']], 46 | [['a' => 2, 'b' => 1], ['a', 'b']], 47 | ]; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Map/sort.php: -------------------------------------------------------------------------------- 1 | 3, 14 | 'c' => 1, 15 | 'b' => 2, 16 | ]], 17 | [[ 18 | 3 => 'd', 19 | 0 => 'a', 20 | 1 => 'b', 21 | 4 => 'e', 22 | 2 => 'c', 23 | ]], 24 | ]; 25 | } 26 | 27 | /** 28 | * @dataProvider sortDataProvider 29 | */ 30 | public function testSort(array $values) 31 | { 32 | $instance = $this->getInstance($values); 33 | 34 | $expected = array_slice($values, 0, count($values), true); 35 | asort($expected); 36 | 37 | $instance->sort(); 38 | $this->assertToArray($expected, $instance); 39 | } 40 | 41 | /** 42 | * @dataProvider sortDataProvider 43 | */ 44 | public function testSortUsingComparator(array $values) 45 | { 46 | $instance = $this->getInstance($values); 47 | 48 | $expected = array_slice($values, 0, count($values), true); 49 | arsort($expected); 50 | 51 | $instance->sort(function($a, $b) { 52 | return $b <=> $a; 53 | }); 54 | 55 | $this->assertToArray($expected, $instance); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/Sequence/shift.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 24 | 25 | $this->assertEquals($expected, $instance->shift()); 26 | $this->assertToArray($result, $instance); 27 | $this->assertEquals(count($initial) - 1, count($instance)); 28 | } 29 | 30 | public function testShiftNotAllowedWhenEmpty() 31 | { 32 | $instance = $this->getInstance(); 33 | $this->expectEmptyNotAllowedException(); 34 | $instance->shift(); 35 | } 36 | 37 | public function testShiftAll() 38 | { 39 | $instance = $this->getInstance(range(1, self::MANY)); 40 | 41 | while ( ! $instance->isEmpty()) { 42 | $instance->shift(); 43 | } 44 | 45 | $this->assertEquals(count($instance), 0); 46 | $this->assertToArray([], $instance); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tests/Map/union.php: -------------------------------------------------------------------------------- 1 | 1], ['a' => 1]], 12 | [['a' => 1], ['a' => 2], ['a' => 2]], 13 | [['a' => 1], ['b' => 2], ['a' => 1, 'b' => 2]], 14 | [['b' => 2], ['a' => 1], ['b' => 2, 'a' => 1]], 15 | [['a' => 1, 'b' => 2], ['c' => 3], ['a' => 1, 'b' => 2, 'c' => 3]], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider unionDataProvider 21 | */ 22 | public function testUnion(array $initial, array $values, array $expected) 23 | { 24 | $instance = $this->getInstance($initial); 25 | $other = $this->getInstance($values); 26 | 27 | $this->assertToArray($expected, $instance->union($other)); 28 | $this->assertToArray($initial, $instance); 29 | } 30 | 31 | /** 32 | * @dataProvider unionDataProvider 33 | */ 34 | public function testUnionWithSelf(array $initial, array $values, array $expected) 35 | { 36 | $instance = $this->getInstance($initial); 37 | 38 | $this->assertToArray($initial, $instance->union($instance)); 39 | $this->assertToArray($initial, $instance); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/Map/skip.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 36 | $pair = $instance->skip($position); 37 | 38 | $this->assertEquals($expected, [$pair->key, $pair->value]); 39 | } 40 | 41 | /** 42 | * @dataProvider skipOutOfRangeDataProvider 43 | */ 44 | public function testSkipIndexOutOfRange(array $values, int $position) 45 | { 46 | $this->expectIndexOutOfRangeException(); 47 | $instance = $this->getInstance($values); 48 | $instance->skip($position); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Map/ksorted.php: -------------------------------------------------------------------------------- 1 | 3, 14 | 'c' => 1, 15 | 'b' => 2, 16 | ]], 17 | [[ 18 | 3 => 'd', 19 | 0 => 'a', 20 | 1 => 'b', 21 | 4 => 'e', 22 | 2 => 'c', 23 | ]], 24 | ]; 25 | } 26 | 27 | /** 28 | * @dataProvider sortedKeyDataProvider 29 | */ 30 | public function testSortedByKey(array $values) 31 | { 32 | $instance = $this->getInstance($values); 33 | 34 | $expected = $values; 35 | ksort($expected); 36 | 37 | $this->assertToArray($expected, $instance->ksorted()); 38 | $this->assertToArray($values, $instance); 39 | } 40 | 41 | /** 42 | * @dataProvider sortKeyDataProvider 43 | */ 44 | public function testSortedByKeyUsingComparator(array $values) 45 | { 46 | $instance = $this->getInstance($values); 47 | 48 | $sorted = $instance->ksorted(function($a, $b) { 49 | return $b <=> $a; 50 | }); 51 | 52 | $expected = $values; 53 | krsort($expected); 54 | 55 | $this->assertToArray($expected, $sorted); 56 | $this->assertToArray($values, $instance); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Map/sorted.php: -------------------------------------------------------------------------------- 1 | 3, 14 | 'c' => 1, 15 | 'b' => 2, 16 | ]], 17 | [[ 18 | 3 => 'd', 19 | 0 => 'a', 20 | 1 => 'b', 21 | 4 => 'e', 22 | 2 => 'c', 23 | ]], 24 | ]; 25 | } 26 | 27 | /** 28 | * @dataProvider sortedDataProvider 29 | */ 30 | public function testSorted(array $values) 31 | { 32 | $instance = $this->getInstance($values); 33 | 34 | $expected = array_slice($values, 0, count($values), true); 35 | asort($expected); 36 | 37 | $this->assertToArray($expected, $instance->sorted()); 38 | $this->assertToArray($values, $instance); 39 | } 40 | 41 | /** 42 | * @dataProvider sortedDataProvider 43 | */ 44 | public function testSortedUsingComparator(array $values) 45 | { 46 | $instance = $this->getInstance($values); 47 | 48 | $sorted = $instance->sorted(function($a, $b) { 49 | return $b <=> $a; 50 | }); 51 | 52 | $expected = array_slice($values, 0, count($values), true); 53 | arsort($expected); 54 | 55 | $this->assertToArray($expected, $sorted); 56 | $this->assertToArray($values, $instance); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Map/pairs.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], [['a', 1], ['b', 2]]], 13 | ]; 14 | } 15 | 16 | /** 17 | * @dataProvider pairsDataProvider 18 | */ 19 | public function testPairs(array $initial, array $expected) 20 | { 21 | $instance = $this->getInstance($initial); 22 | $pairs = $instance->pairs(); 23 | 24 | $this->assertInstanceOf(Vector::class, $pairs); 25 | 26 | $to_array = function ($pair) { 27 | return [$pair->key, $pair->value]; 28 | }; 29 | 30 | $this->assertEquals($expected, array_map($to_array, $pairs->toArray())); 31 | } 32 | 33 | public function testObjectsAreMutableThroughAccess() 34 | { 35 | $key = new \stdClass(); 36 | $key->state = true; 37 | 38 | $instance = $this->getInstance(); 39 | $instance->put($key, 1); 40 | 41 | $instance->pairs()->first()->key->state = false; 42 | 43 | $this->assertFalse($key->state); 44 | $this->assertFalse($instance->pairs()->first()->key->state); 45 | } 46 | 47 | public function testKeysAreNotMutableThroughAccess() 48 | { 49 | $instance = $this->getInstance(['a' => 1, 'b' => 2]); 50 | $instance->pairs()->first()->key = 'c'; 51 | 52 | $this->assertEquals('a', $instance->pairs()->first()->key); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/Map/_isset.php: -------------------------------------------------------------------------------- 1 | 0 ], 'a', true ], 12 | [['a' => 1 ], 'a', true ], 13 | [['a' => false], 'a', true ], 14 | [['a' => null ], 'a', false], 15 | [[ ], 'a', false], 16 | ]; 17 | } 18 | 19 | /** 20 | * @dataProvider issetDataProvider 21 | */ 22 | public function testArrayAccessIsset(array $initial, $key, bool $isset) 23 | { 24 | $instance = $this->getInstance(); 25 | 26 | foreach ($initial as $key => $value) { 27 | $instance->put($key, $value); 28 | } 29 | 30 | $this->assertEquals($isset, isset($instance[$key])); 31 | } 32 | 33 | /** 34 | * @dataProvider issetDataProvider 35 | */ 36 | public function testArrayAccessIssetByMethod(array $initial, $key, bool $isset) 37 | { 38 | $instance = $this->getInstance(); 39 | 40 | foreach ($initial as $key => $value) { 41 | $instance->put($key, $value); 42 | } 43 | 44 | $this->assertEquals($isset, $instance->offsetExists($key)); 45 | } 46 | 47 | /** 48 | * @dataProvider issetDataProvider 49 | */ 50 | public function testArrayAccessIssetByReference(array $initial, $key, bool $isset) 51 | { 52 | $instance = $this->getInstance([$initial]); 53 | $this->assertEquals($isset, isset($instance[0][$key])); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/Sequence/remove.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 28 | $returned = $instance->remove($index); 29 | 30 | $this->assertEquals(count($initial) - 1, count($instance)); 31 | $this->assertToArray($expected, $instance); 32 | $this->assertEquals($return, $returned); 33 | } 34 | 35 | /** 36 | * @dataProvider outOfRangeDataProvider 37 | */ 38 | public function testRemoveIndexOutOfRange($initial, $index) 39 | { 40 | $instance = $this->getInstance($initial); 41 | $this->expectIndexOutOfRangeException(); 42 | $instance->remove($index); 43 | } 44 | 45 | /** 46 | * @dataProvider badIndexDataProvider 47 | */ 48 | public function testRemoveIndexBadIndex($initial, $index) 49 | { 50 | $instance = $this->getInstance($initial); 51 | $this->expectWrongIndexTypeException(); 52 | $instance->remove($index); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tests/VectorTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(ArrayAccess::class, $this->getInstance()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/StackTest.php: -------------------------------------------------------------------------------- 1 | getInstance(); 56 | $this->expectArrayAccessUnsupportedException(); 57 | $set['a'] = 1; 58 | } 59 | 60 | public function testImplementsArrayAccess() 61 | { 62 | $this->assertInstanceOf(ArrayAccess::class, $this->getInstance()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Sequence/rotate.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 50 | $instance->rotate($rotation); 51 | $this->assertToArray($expected, $instance); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/Sequence/_unset.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 12 | unset($instance[$index]); 13 | $this->assertToArray($expected, $instance); 14 | $this->assertEquals(count($expected), count($instance)); 15 | } 16 | 17 | /** 18 | * @dataProvider removeDataProvider 19 | */ 20 | public function testArrayAccessUnsetByMethod($initial, $index, $return, array $expected) 21 | { 22 | $instance = $this->getInstance($initial); 23 | $instance->offsetUnset($index); 24 | $this->assertToArray($expected, $instance); 25 | $this->assertEquals(count($expected), count($instance)); 26 | } 27 | 28 | /** 29 | * @dataProvider badIndexDataProvider 30 | */ 31 | public function testArrayAccessUnsetIndexBadIndex($initial, $index) 32 | { 33 | $instance = $this->getInstance($initial); 34 | unset($instance[$index]); 35 | 36 | $this->assertFalse(isset($instance[$index])); 37 | } 38 | 39 | /** 40 | * @dataProvider outOfRangeDataProvider 41 | */ 42 | public function testArrayAccessUnsetIndexOutOfRange($initial, $index) 43 | { 44 | $instance = $this->getInstance($initial); 45 | unset($instance[$index]); 46 | 47 | $this->assertFalse(isset($instance[$index])); 48 | } 49 | 50 | 51 | public function testArrayAccessUnsetByReference() 52 | { 53 | $instance = $this->getInstance([[1]]); 54 | unset($instance[0][0]); 55 | 56 | $this->assertToArray([[]], $instance); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/Deque/capacity.php: -------------------------------------------------------------------------------- 1 | getInstance(); 11 | $this->assertEquals($min, $instance->capacity()); 12 | 13 | for ($i = 0; $i < $min; $i++) { 14 | $instance->push($i); 15 | } 16 | 17 | // Should not resize when full after push 18 | $this->assertEquals($min, $instance->capacity()); 19 | 20 | // Should resize if full before push 21 | $instance->push('x'); 22 | $this->assertEquals(intval($min * 2), $instance->capacity()); 23 | } 24 | 25 | public function testAutoTruncate() 26 | { 27 | // size => capacity 28 | $boundaries = [ 29 | 64 => 64, 30 | 33 => 64, 31 | 32 => 64, 32 | 31 => 64, 33 | 17 => 64, 34 | 16 => 32, 35 | 15 => 32, 36 | 9 => 32, 37 | 8 => 16, 38 | 7 => 16, 39 | 5 => 16, 40 | 4 => 8, 41 | 3 => 8, 42 | 0 => 8, 43 | ]; 44 | 45 | $instance = $this->getInstance(range(1, array_keys($boundaries)[0])); 46 | 47 | for (;;) { 48 | if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) { 49 | $this->assertEquals($expected, $instance->capacity()); 50 | } 51 | 52 | if ($instance->isEmpty()) { 53 | break; 54 | } 55 | 56 | $instance->pop(); 57 | } 58 | } 59 | 60 | public function testClearResetsCapacity() 61 | { 62 | $min = \Ds\Deque::MIN_CAPACITY; 63 | 64 | $instance = $this->getInstance(range(1, self::MANY)); 65 | $instance->clear(); 66 | $this->assertEquals($min, $instance->capacity()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/Set/capacity.php: -------------------------------------------------------------------------------- 1 | getInstance(); 13 | $this->assertEquals($min, $instance->capacity()); 14 | 15 | for ($i = 0; $i < $min; $i++) { 16 | $instance[] = $i; 17 | } 18 | 19 | // Should not resize when full after add 20 | $this->assertEquals($min, $instance->capacity()); 21 | 22 | // Should resize when full before add 23 | $instance[] = $min; 24 | $this->assertEquals($min * 2, $instance->capacity()); 25 | } 26 | 27 | public function testAutoTruncate() 28 | { 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 => 32, 37 | 15 => 32, 38 | 9 => 32, 39 | 8 => 16, 40 | 7 => 16, 41 | 5 => 16, 42 | 4 => 8, 43 | 3 => 8, 44 | 0 => 8, 45 | ]; 46 | 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 | } 53 | 54 | if ($instance->isEmpty()) { 55 | break; 56 | } 57 | 58 | $instance->remove($instance->count()); 59 | } 60 | } 61 | 62 | public function testClearResetsCapacity() 63 | { 64 | $instance = $this->getInstance(range(1, self::MANY)); 65 | $instance->clear(); 66 | $this->assertEquals(Set::MIN_CAPACITY, $instance->capacity()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/Map/capacity.php: -------------------------------------------------------------------------------- 1 | getInstance(); 13 | $this->assertEquals($min, $instance->capacity()); 14 | 15 | for ($i = 0; $i < $min; $i++) { 16 | $instance[$i] = null; 17 | } 18 | 19 | // Should not resize when full after add 20 | $this->assertEquals($min, $instance->capacity()); 21 | 22 | // Should resize when full before add 23 | $instance[$min] = null; 24 | $this->assertEquals($min * 2, $instance->capacity()); 25 | } 26 | 27 | public function testAutoTruncate() 28 | { 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 => 32, 37 | 15 => 32, 38 | 9 => 32, 39 | 8 => 16, 40 | 7 => 16, 41 | 5 => 16, 42 | 4 => 8, 43 | 3 => 8, 44 | 0 => 8, 45 | ]; 46 | 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 | } 53 | 54 | if ($instance->isEmpty()) { 55 | break; 56 | } 57 | 58 | $instance->remove($instance->count() - 1); 59 | } 60 | } 61 | 62 | public function testClearResetsCapacity() 63 | { 64 | $instance = $this->getInstance(range(1, self::MANY)); 65 | $instance->clear(); 66 | $this->assertEquals(Map::MIN_CAPACITY, $instance->capacity()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/Vector/capacity.php: -------------------------------------------------------------------------------- 1 | getInstance(); 11 | $this->assertEquals($min, $instance->capacity()); 12 | 13 | for ($i = 0; $i < $min; $i++) { 14 | $instance->push($i); 15 | } 16 | 17 | // Should not resize when full after push 18 | $this->assertEquals($min, $instance->capacity()); 19 | 20 | // Should resize if full before push 21 | $instance->push('x'); 22 | $this->assertEquals(intval($min * 1.5), $instance->capacity()); 23 | } 24 | 25 | public function testAutoTruncate() 26 | { 27 | // size => capacity 28 | $boundaries = [ 29 | 64 => 64, // Initial capacity for 64 elements would be 64 (full). 30 | 33 => 64, 31 | 32 => 64, 32 | 31 => 64, 33 | 17 => 64, 34 | 16 => 32, 35 | 15 => 32, 36 | 9 => 32, 37 | 8 => 16, 38 | 7 => 16, 39 | 5 => 16, 40 | 4 => 8, 41 | 3 => 8, 42 | 0 => 8, 43 | ]; 44 | 45 | $instance = $this->getInstance(range(1, array_keys($boundaries)[0])); 46 | 47 | for (;;) { 48 | if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) { 49 | $this->assertEquals($expected, $instance->capacity()); 50 | } 51 | 52 | if ($instance->isEmpty()) { 53 | break; 54 | } 55 | 56 | $instance->pop(); 57 | } 58 | } 59 | 60 | public function testClearResetsCapacity() 61 | { 62 | $min = \Ds\Vector::MIN_CAPACITY; 63 | 64 | $instance = $this->getInstance(range(1, self::MANY)); 65 | $instance->clear(); 66 | $this->assertEquals($min, $instance->capacity()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/PriorityQueue/capacity.php: -------------------------------------------------------------------------------- 1 | getInstance(); 13 | $this->assertEquals($min, $instance->capacity()); 14 | 15 | for ($i = 0; $i < $min; $i++) { 16 | $instance->push($i, 0); 17 | } 18 | 19 | // Should not resize when full after push 20 | $this->assertEquals($min, $instance->capacity()); 21 | 22 | // Should resize if full before push 23 | $instance->push($min, 0); 24 | $this->assertEquals($min * 2, $instance->capacity()); 25 | } 26 | 27 | public function testAutoTruncate() 28 | { 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 => 32, 37 | 15 => 32, 38 | 9 => 32, 39 | 8 => 16, 40 | 7 => 16, 41 | 5 => 16, 42 | 4 => 8, 43 | 3 => 8, 44 | 0 => 8, 45 | ]; 46 | 47 | $instance = $this->getInstance(); 48 | 49 | foreach (range(1, array_keys($boundaries)[0]) as $value) { 50 | $instance->push($value, 1); 51 | } 52 | 53 | for (;;) { 54 | if ( ! is_null(($expected = $boundaries[$instance->count()] ?? null))) { 55 | $this->assertEquals($expected, $instance->capacity()); 56 | } 57 | 58 | if ($instance->isEmpty()) { 59 | break; 60 | } 61 | 62 | $instance->pop(); 63 | } 64 | } 65 | 66 | public function testClearResetsCapacity() 67 | { 68 | $instance = $this->getInstance(range(1, self::MANY)); 69 | $instance->clear(); 70 | $this->assertEquals(PriorityQueue::MIN_CAPACITY, $instance->capacity()); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /tests/Stack/push.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider pushDataProvider 13 | */ 14 | public function testPushVariadic(array $values, array $expected) 15 | { 16 | $instance = $this->getInstance(); 17 | $instance->push(...$values); 18 | 19 | $this->assertToArray($expected, $instance); 20 | $this->assertCount(count($expected), $instance); 21 | } 22 | 23 | /** 24 | * @dataProvider pushDataProvider 25 | */ 26 | public function testPush(array $values, array $expected) 27 | { 28 | $instance = $this->getInstance(); 29 | 30 | foreach ($values as $value) { 31 | $instance->push($value); 32 | } 33 | 34 | $this->assertToArray($expected, $instance); 35 | $this->assertCount(count($expected), $instance); 36 | } 37 | 38 | /** 39 | * @dataProvider pushDataProvider 40 | */ 41 | public function testArrayAccessPush(array $values, array $expected) 42 | { 43 | $instance = $this->getInstance(); 44 | 45 | foreach ($values as $value) { 46 | $instance[] = $value; 47 | } 48 | 49 | $this->assertToArray($expected, $instance); 50 | $this->assertCount(count($expected), $instance); 51 | } 52 | 53 | /** 54 | * @dataProvider pushDataProvider 55 | */ 56 | public function testArrayAccessPushByMethod(array $values, array $expected) 57 | { 58 | $instance = $this->getInstance(); 59 | 60 | foreach ($values as $value) { 61 | $instance->offsetSet(null, $value); 62 | } 63 | 64 | $this->assertToArray($expected, $instance); 65 | $this->assertCount(count($expected), $instance); 66 | } 67 | 68 | public function testPushCircularReference() 69 | { 70 | $instance = $this->getInstance(); 71 | $instance->push($instance); 72 | $this->assertToArray([$instance], $instance); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/Queue/push.php: -------------------------------------------------------------------------------- 1 | basicDataProvider(); 9 | } 10 | 11 | /** 12 | * @dataProvider pushDataProvider 13 | */ 14 | public function testPushVariadic(array $values, array $expected) 15 | { 16 | $instance = $this->getInstance(); 17 | 18 | $instance->push(...$values); 19 | 20 | $this->assertToArray($expected, $instance); 21 | $this->assertCount(count($expected), $instance); 22 | } 23 | 24 | /** 25 | * @dataProvider pushDataProvider 26 | */ 27 | public function testPush(array $values, array $expected) 28 | { 29 | $instance = $this->getInstance(); 30 | 31 | foreach ($values as $value) { 32 | $instance->push($value); 33 | } 34 | 35 | $this->assertToArray($expected, $instance); 36 | $this->assertCount(count($expected), $instance); 37 | } 38 | 39 | /** 40 | * @dataProvider pushDataProvider 41 | */ 42 | public function testArrayAccessPush(array $values, array $expected) 43 | { 44 | $instance = $this->getInstance(); 45 | 46 | foreach ($values as $value) { 47 | $instance[] = $value; 48 | } 49 | 50 | $this->assertToArray($expected, $instance); 51 | $this->assertCount(count($expected), $instance); 52 | } 53 | 54 | /** 55 | * @dataProvider pushDataProvider 56 | */ 57 | public function testArrayAccessPushByMethod(array $values, array $expected) 58 | { 59 | $instance = $this->getInstance(); 60 | 61 | foreach ($values as $value) { 62 | $instance->offsetSet(null, $value); 63 | } 64 | 65 | $this->assertToArray($expected, $instance); 66 | $this->assertCount(count($expected), $instance); 67 | } 68 | 69 | public function testPushCircularReference() 70 | { 71 | $instance = $this->getInstance(); 72 | $instance->push($instance); 73 | $this->assertToArray([$instance], $instance); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/Sequence/_empty.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 45 | $this->assertEquals($empty, empty($instance[$index])); 46 | } 47 | 48 | /** 49 | * @dataProvider badIndexDataProvider 50 | */ 51 | public function testArrayAccessEmptyIndexBadIndex($initial, $index) 52 | { 53 | $instance = $this->getInstance($initial); 54 | $this->assertTrue(empty($instance[$index])); 55 | } 56 | 57 | /** 58 | * @dataProvider outOfRangeDataProvider 59 | */ 60 | public function testArrayAccessEmptyIndexOutOfRange($initial, $index) 61 | { 62 | $instance = $this->getInstance($initial); 63 | $this->assertTrue(empty($instance[$index])); 64 | } 65 | 66 | /** 67 | * @dataProvider emptyDataProvider 68 | */ 69 | public function testArrayAccessEmptyByReference($initial, $index, bool $empty) 70 | { 71 | $instance = $this->getInstance([$initial]); 72 | $this->assertEquals($empty, empty($instance[0][$index])); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/Map/apply.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 22 | $instance->apply($callback); 23 | 24 | $expected = array_map($callback, array_keys($values), $values); 25 | $this->assertToArray($expected, $instance); 26 | } 27 | 28 | public function testApplyCallbackThrowsException() 29 | { 30 | $instance = $this->getInstance([1, 2, 3]); 31 | 32 | try { 33 | $instance->apply(function($value) { 34 | throw new \Exception(); 35 | }); 36 | } catch (\Exception $e) { 37 | $this->assertToArray([1, 2, 3], $instance); 38 | return; 39 | } 40 | 41 | $this->fail('Exception should have been caught'); 42 | } 43 | 44 | public function testApplyCallbackThrowsExceptionLaterOn() 45 | { 46 | $instance = $this->getInstance([1, 2, 3]); 47 | 48 | try { 49 | $instance->apply(function($key, $value) { 50 | if ($value === 3) { 51 | throw new \Exception(); 52 | } else { 53 | return "*"; 54 | } 55 | }); 56 | } catch (\Exception $e) { 57 | $this->assertToArray(["*", "*", 3], $instance); 58 | return; 59 | } 60 | 61 | $this->fail('Exception should have been caught'); 62 | } 63 | 64 | public function testApplyDoesNotLeakWhenCallbackFails() 65 | { 66 | $instance = $this->getInstance([ 67 | "a" => new \stdClass(), 68 | "b" => new \stdClass(), 69 | "c" => new \stdClass(), 70 | ]); 71 | 72 | static::expectException(\Exception::class); 73 | 74 | $instance->apply(function($key, $value) { 75 | if ($key === "c") { 76 | throw new \Exception(); 77 | } 78 | }); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /tests/PriorityQueue/push.php: -------------------------------------------------------------------------------- 1 | 1, 'b' => 2], ['b', 'a']], 11 | [[], ['a' => 1, 'b' => 1], ['a', 'b']], 12 | 13 | [['a' => 1], ['b' => 2], ['b', 'a']], 14 | [['a' => 1], ['b' => 1], ['a', 'b']], 15 | 16 | // Non-integer priorities 17 | [[], ['a' => 1.5, 'b' => '5', 'c' => false], ['b', 'a', 'c']], 18 | ]; 19 | } 20 | 21 | /** 22 | * @dataProvider pushDataProvider 23 | */ 24 | public function testPush(array $initial, array $values, array $expected) 25 | { 26 | $instance = $this->getInstance($initial); 27 | 28 | foreach ($values as $value => $priority) { 29 | $instance->push($value, (int) $priority); 30 | } 31 | 32 | $this->assertToArray($expected, $instance); 33 | } 34 | 35 | public function testPushIdenticalValues() 36 | { 37 | $instance = $this->getInstance(); 38 | 39 | $instance->push('a', 1); 40 | $instance->push('a', 1); 41 | $instance->push('a', 1); 42 | 43 | $this->assertToArray(['a', 'a', 'a'], $instance); 44 | } 45 | 46 | public function testPushManyRandom() 47 | { 48 | $instance = $this->getInstance(); 49 | 50 | $reference = range(1, self::SOME); 51 | shuffle($reference); 52 | 53 | foreach ($reference as $index => $priority) { 54 | $instance->push($index, (int) $priority); 55 | } 56 | 57 | asort($reference); 58 | 59 | $this->assertEmpty(array_diff_key($reference, $instance->toArray())); 60 | } 61 | 62 | public function testInsertionOrder() 63 | { 64 | $instance = $this->getInstance(); 65 | 66 | foreach (range(1, self::MANY) as $i) { 67 | $instance->push($i, 0); 68 | } 69 | 70 | foreach (range(1, self::MANY) as $i) { 71 | $this->assertEquals($i, $instance->pop()); 72 | } 73 | } 74 | 75 | public function testPushCircularReference() 76 | { 77 | $instance = $this->getInstance(); 78 | $instance->push($instance, 1); 79 | $this->assertToArray([$instance], $instance); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/Sequence/apply.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 31 | 32 | $instance->apply($callback); 33 | $expected = array_map($callback, $values); 34 | 35 | $this->assertToArray($expected, $instance); 36 | } 37 | 38 | public function testApplyCallbackThrowsException() 39 | { 40 | $instance = $this->getInstance([1, 2, 3]); 41 | 42 | try { 43 | $instance->apply(function($value) { 44 | throw new \Exception(); 45 | }); 46 | } catch (\Exception $e) { 47 | $this->assertToArray([1, 2, 3], $instance); 48 | return; 49 | } 50 | 51 | $this->fail('Exception should have been caught'); 52 | } 53 | 54 | public function testApplyCallbackThrowsExceptionLaterOn() 55 | { 56 | $instance = $this->getInstance([1, 2, 3]); 57 | 58 | try { 59 | $instance->apply(function($value) { 60 | if ($value === 3) { 61 | throw new \Exception(); 62 | } else { 63 | return $value * 2; 64 | } 65 | }); 66 | } catch (\Exception $e) { 67 | $this->assertToArray([2, 4, 3], $instance); 68 | return; 69 | } 70 | 71 | $this->fail('Exception should have been caught'); 72 | } 73 | 74 | public function testApplyDoesNotCallByReference() 75 | { 76 | $instance = $this->getInstance([1, 2, 3]); 77 | 78 | $instance->apply(function($value) { 79 | $before = $value; 80 | $value = null; 81 | return $before; 82 | }); 83 | 84 | $this->assertToArray([1, 2, 3], $instance); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/Sequence/_isset.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 42 | $this->assertEquals($isset, isset($instance[$index])); 43 | } 44 | 45 | /** 46 | * @dataProvider issetDataProvider 47 | */ 48 | public function testArrayAccessIssetByMethod($initial, $index, bool $isset) 49 | { 50 | $instance = $this->getInstance($initial); 51 | $this->assertEquals($isset, $instance->offsetExists($index)); 52 | } 53 | 54 | /** 55 | * @dataProvider badIndexDataProvider 56 | */ 57 | public function testArrayAccessIssetIndexBadIndex($initial, $index) 58 | { 59 | $instance = $this->getInstance($initial); 60 | $this->assertFalse(isset($instance[$index])); 61 | } 62 | 63 | /** 64 | * @dataProvider outOfRangeDataProvider 65 | */ 66 | public function testArrayAccessIssetIndexOutOfRange($initial, $index) 67 | { 68 | $instance = $this->getInstance($initial); 69 | $this->assertFalse(isset($instance[$index])); 70 | } 71 | 72 | 73 | /** 74 | * @dataProvider issetDataProvider 75 | */ 76 | public function testArrayAccessIssetByReference($initial, $index, bool $isset) 77 | { 78 | $instance = $this->getInstance([$initial]); 79 | $this->assertEquals($isset, isset($instance[0][$index])); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /tests/Deque/remove.php: -------------------------------------------------------------------------------- 1 | t. 9 | */ 10 | public function testRemoveExtended() 11 | { 12 | $instance = $this->getInstance(); 13 | 14 | /* HEAD WRAPPED AROUND, TAIL = 0 */ 15 | // The head of the deque will wrap around if all items are unshifted. 16 | 17 | $instance->unshift('c'); // [_, _, _, _, _, _, _, c] tail = 0, head = 3 18 | $instance->unshift('b'); // [_, _, _, _, _, _, b, c] tail = 0, head = 2 19 | $instance->unshift('a'); // [_, _, _, _, _, a, b, c] tail = 0, head = 1 20 | 21 | // The sequence is now a, b, c 22 | $this->assertEquals('b', $instance->remove(1)); // [..., _, _, a, c] 23 | $this->assertEquals('a', $instance->remove(0)); // [..., _, _, _, c] 24 | $this->assertEquals('c', $instance->remove(0)); // [..., _, _, _, _] 25 | $this->assertTrue($instance->isEmpty()); 26 | 27 | /* HEAD WRAPPED AROUND, TAIL > 0 */ 28 | $instance = $this->getInstance(); 29 | 30 | $instance->unshift('b'); // [_, _, ..., _, b] tail = 0, head = 3 31 | $instance->unshift('a'); // [_, _, ..., a, b] tail = 0, head = 2 32 | $instance->push('c'); // [c, _, ..., a, b] tail = 1, head = 2 33 | 34 | // The sequence is now a, b, c 35 | $this->assertEquals('b', $instance->remove(1)); // [c, _, ..., _, a] 36 | $this->assertEquals('a', $instance->remove(0)); // [c, _, ..., _, _] 37 | $this->assertEquals('c', $instance->remove(0)); // [_, _, ..., _, _] 38 | $this->assertTrue($instance->isEmpty()); 39 | 40 | /* HEAD NOT WRAPPED, TAIL > 0 */ 41 | $instance = $this->getInstance(); 42 | 43 | $instance->push('a'); // [a, _, _, ..., _] tail = 1, head = 0 44 | $instance->push('b'); // [a, b, _, ..., _] tail = 2, head = 0 45 | $instance->push('c'); // [a, b, c, ..., _] tail = 3, head = 0 46 | 47 | // The sequence is now a, b, c 48 | $this->assertEquals('b', $instance->remove(1)); // [a, c, ..., _, _] 49 | $this->assertEquals('a', $instance->remove(0)); // [c, _, ..., _, _] 50 | $this->assertEquals('c', $instance->remove(0)); // [_, _, ..., _, _] 51 | $this->assertTrue($instance->isEmpty()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/Map/get.php: -------------------------------------------------------------------------------- 1 | 1], 'a', 1], 11 | ]; 12 | } 13 | 14 | /** 15 | * @dataProvider getDataProvider 16 | */ 17 | public function testGet(array $initial, $key, $expected) 18 | { 19 | $instance = $this->getInstance($initial); 20 | $this->assertEquals($expected, $instance->get($key)); 21 | } 22 | 23 | public function testGetDefault() 24 | { 25 | $instance = $this->getInstance(); 26 | $this->assertEquals('a', $instance->get('?', 'a')); 27 | } 28 | 29 | public function testGetKeyNotFound() 30 | { 31 | $instance = $this->getInstance(); 32 | $this->expectKeyNotFoundException(); 33 | $instance->get('?'); 34 | } 35 | 36 | public function testArrayAccessGet() 37 | { 38 | $instance = $this->getInstance(['a' => 1]); 39 | $this->assertEquals(1, $instance['a']); 40 | } 41 | 42 | public function testArrayAccessGetByMethod() 43 | { 44 | $instance = $this->getInstance(['a' => 1]); 45 | $this->assertEquals(1, $instance->offsetGet('a')); 46 | } 47 | 48 | public function testArrayAccessGetByReference() 49 | { 50 | $instance = $this->getInstance(['a' => [1]]); 51 | $this->assertEquals(1, $instance['a'][0]); 52 | } 53 | 54 | public function testArrayAccessGetKeyNotFound() 55 | { 56 | $instance = $this->getInstance(['a' => 1]); 57 | $this->expectKeyNotFoundException(); 58 | $instance['b']; 59 | } 60 | 61 | public function testArrayAccessGetNullCoalesce() 62 | { 63 | $instance = $this->getInstance(); 64 | 65 | $obj = new \stdClass; 66 | 67 | $this->assertEquals(null, $instance[false] ?? null); 68 | $this->assertEquals(null, $instance[[]] ?? null); 69 | $this->assertEquals(null, $instance[$obj] ?? null); 70 | $this->assertEquals(null, $instance[0] ?? null); 71 | 72 | $instance->put(false, 1); 73 | $instance->put([], 2); 74 | $instance->put($obj, 3); 75 | $instance->put(0, 4); 76 | 77 | $this->assertEquals(1, $instance[false] ?? null); 78 | $this->assertEquals(2, $instance[[]] ?? null); 79 | $this->assertEquals(3, $instance[$obj] ?? null); 80 | $this->assertEquals(4, $instance[0] ?? null); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Set/diff.php: -------------------------------------------------------------------------------- 1 | getInstance($a); 26 | $b = $this->getInstance($b); 27 | 28 | $this->assertEquals($expected, $a->diff($b)->toArray()); 29 | } 30 | 31 | // /** 32 | // * @dataProvider diffDataProvider 33 | // */ 34 | // public function testDiffOperator(array $a, array $b, array $expected) 35 | // { 36 | // $a = $this->getInstance($a); 37 | // $b = $this->getInstance($b); 38 | 39 | // $this->assertEquals($expected, ($a - $b)->toArray()); 40 | // } 41 | 42 | // /** 43 | // * @dataProvider diffDataProvider 44 | // */ 45 | // public function testDiffOperatorAssign(array $a, array $b, array $expected) 46 | // { 47 | // $a = $this->getInstance($a); 48 | // $b = $this->getInstance($b); 49 | 50 | // $a -= $b; 51 | // $this->assertEquals($expected, $a->toArray()); 52 | // } 53 | 54 | /** 55 | * @dataProvider diffDataProvider 56 | */ 57 | public function testDiffWithSelf(array $a, array $b, array $expected) 58 | { 59 | $a = $this->getInstance($a); 60 | $this->assertEquals([], $a->diff($a)->toArray()); 61 | } 62 | 63 | // /** 64 | // * @dataProvider diffDataProvider 65 | // */ 66 | // public function testDiffOperatorWithSelf(array $a, array $b, array $expected) 67 | // { 68 | // $a = $this->getInstance($a); 69 | // $this->assertEquals([], ($a - $a)->toArray()); 70 | // } 71 | 72 | // /** 73 | // * @dataProvider diffDataProvider 74 | // */ 75 | // public function testDiffOperatorAssignWithSelf(array $a, array $b, array $expected) 76 | // { 77 | // $a = $this->getInstance($a); 78 | 79 | // $a -= $a; 80 | // $this->assertEquals([], $a->toArray()); 81 | // } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Deque/slice.php: -------------------------------------------------------------------------------- 1 | t. 9 | */ 10 | public function testSliceExtended() 11 | { 12 | $instance = $this->getInstance(); 13 | 14 | $instance->unshift('c'); // [_, _, _, _, _, _, _, c] tail = 0, head = 7 15 | $instance->unshift('b'); // [_, _, _, _, _, _, b, c] tail = 0, head = 6 16 | $instance->unshift('a'); // [_, _, _, _, _, a, b, c] tail = 0, head = 5 17 | 18 | $this->assertEquals(['a'], $instance->slice(0, 1)->toArray()); 19 | $this->assertEquals(['b'], $instance->slice(1, 1)->toArray()); 20 | $this->assertEquals(['c'], $instance->slice(2, 1)->toArray()); 21 | 22 | $this->assertEquals(['a', 'b'], $instance->slice(0, 2)->toArray()); 23 | $this->assertEquals(['b', 'c'], $instance->slice(1, 2)->toArray()); 24 | $this->assertEquals(['c' ], $instance->slice(2, 2)->toArray()); 25 | 26 | $this->assertEquals(['a', 'b', 'c'], $instance->slice(0, 3)->toArray()); 27 | $this->assertEquals(['b', 'c' ], $instance->slice(1, 3)->toArray()); 28 | $this->assertEquals(['c' ], $instance->slice(2, 3)->toArray()); 29 | 30 | /* If only some values have wrapped around, slice would have to copy 31 | from both the wrapped and not-wrapped values */ 32 | 33 | $instance = $this->getInstance(); 34 | 35 | $instance->push('b'); // [b, _, _, _, _, _, _, _] tail = 1, head = 0 36 | $instance->push('c'); // [b, c, _, _, _, _, _, _] tail = 2, head = 1 37 | $instance->unshift('a'); // [b, c, _, _, _, _, _, a] tail = 2, head = 7 38 | 39 | $this->assertEquals(['a'], $instance->slice(0, 1)->toArray()); 40 | $this->assertEquals(['b'], $instance->slice(1, 1)->toArray()); 41 | $this->assertEquals(['c'], $instance->slice(2, 1)->toArray()); 42 | 43 | $this->assertEquals(['a', 'b'], $instance->slice(0, 2)->toArray()); 44 | $this->assertEquals(['b', 'c'], $instance->slice(1, 2)->toArray()); 45 | $this->assertEquals(['c' ], $instance->slice(2, 2)->toArray()); 46 | 47 | $this->assertEquals(['a', 'b', 'c'], $instance->slice(0, 3)->toArray()); 48 | $this->assertEquals(['b', 'c' ], $instance->slice(1, 3)->toArray()); 49 | $this->assertEquals(['c' ], $instance->slice(2, 3)->toArray()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/Set/filter.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 23 | 24 | $filtered = $instance->filter($callback); 25 | $expected = array_values(array_filter($values, $callback)); 26 | 27 | $this->assertToArray($values, $instance); 28 | $this->assertEquals($expected, $filtered->toArray()); 29 | } 30 | 31 | /** 32 | * @dataProvider filterDataProvider 33 | */ 34 | public function testFilterWithoutCallback(array $values, callable $callback) 35 | { 36 | $instance = $this->getInstance($values); 37 | 38 | $filtered = $instance->filter(); 39 | $expected = array_values(array_filter($values)); 40 | 41 | $this->assertToArray($values, $instance); 42 | $this->assertEquals($expected, $filtered->toArray()); 43 | } 44 | 45 | public function testFilterCallbackThrowsException() 46 | { 47 | $instance = $this->getInstance([1, 2, 3]); 48 | $filtered = null; 49 | 50 | try { 51 | $filtered = $instance->filter(function($value) { 52 | throw new \Exception(); 53 | }); 54 | } catch (\Exception $e) { 55 | $this->assertToArray([1, 2, 3], $instance); 56 | $this->assertNull($filtered); 57 | return; 58 | } 59 | 60 | $this->fail('Exception should have been caught'); 61 | } 62 | 63 | public function testFilterCallbackThrowsExceptionLaterOn() 64 | { 65 | $instance = $this->getInstance([1, 2, 3]); 66 | $filtered = null; 67 | 68 | try { 69 | $filtered = $instance->filter(function($value) { 70 | if ($value === 3) { 71 | throw new \Exception(); 72 | } 73 | }); 74 | } catch (\Exception $e) { 75 | $this->assertToArray([1, 2, 3], $instance); 76 | $this->assertNull($filtered); 77 | return; 78 | } 79 | 80 | $this->fail('Exception should have been caught'); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Sequence/insert.php: -------------------------------------------------------------------------------- 1 | sample(); 9 | 10 | $h = count($s) / 2; 11 | 12 | // initial, index, values 13 | return [ 14 | [[0], 0, [1]], 15 | [[0], 1, [1]], 16 | 17 | [['a', 'b'], 0, ['c', 'd']], 18 | [['a', 'b'], 1, ['c', 'd']], 19 | [['a', 'b'], 2, ['c', 'd']], 20 | 21 | // Inserting many values at the front of an empty sequence 22 | [[], 0, $s], 23 | 24 | // Inserting many values at the front of a non-empty sequence 25 | [$s, 0, $s], 26 | 27 | // Inserting many values at the front of a non-empty sequence 28 | [$s, $h, $s], 29 | ]; 30 | } 31 | 32 | 33 | /** 34 | * @dataProvider insertDataProvider 35 | */ 36 | public function testInsertVariadic(array $initial, $index, array $values) 37 | { 38 | $expected = $initial; 39 | array_splice($expected, $index, 0, $values); 40 | 41 | $instance = $this->getInstance($initial); 42 | $instance->insert($index, ...$values); 43 | 44 | $this->assertEquals(count($expected), count($instance)); 45 | $this->assertToArray($expected, $instance); 46 | } 47 | 48 | /** 49 | * @dataProvider insertDataProvider 50 | */ 51 | public function testInsert(array $initial, $index, array $values) 52 | { 53 | $expected = $initial; 54 | array_splice($expected, $index, 0, array_reverse($values)); 55 | 56 | $instance = $this->getInstance($initial); 57 | 58 | foreach ($values as $value) { 59 | $instance->insert($index, $value); 60 | } 61 | 62 | $this->assertEquals(count($expected), count($instance)); 63 | $this->assertToArray($expected, $instance); 64 | } 65 | 66 | /** 67 | * @dataProvider outOfRangeDataProvider 68 | */ 69 | public function testInsertIndexOutOfRange($initial, $index) 70 | { 71 | $instance = $this->getInstance($initial); 72 | $this->expectIndexOutOfRangeException(); 73 | $instance->insert($index); 74 | } 75 | 76 | /** 77 | * @dataProvider badIndexDataProvider 78 | */ 79 | public function testInsertIndexBadIndex($initial, $index) 80 | { 81 | $instance = $this->getInstance(); 82 | $this->expectWrongIndexTypeException(); 83 | $instance->insert($index); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tests/Set/remove.php: -------------------------------------------------------------------------------- 1 | getUniqueAndDuplicateData(); 9 | 10 | // initial, values to remove, expected. 11 | return [ 12 | [[], [], []], 13 | [[], ['a'], []], 14 | [['a'], ['a'], []], 15 | [['a'], ['b'], ['a']], 16 | 17 | // // Test removing a few values. 18 | // [[], ['a', 'b'], []], 19 | // [['a', 'b'], ['a', 'b'], []], 20 | // [['a', 'b'], ['a', 'a'], ['b']], 21 | 22 | // // Test that numeric strings are not treated as int. 23 | // [[ 1 ], ['1'], [ 1 ]], 24 | // [['1'], [ 1 ], ['1']], 25 | 26 | // // Test removing many values from empty set does nothing. 27 | // [[], $unique, []], 28 | 29 | // // Test removing identical data removes everything 30 | // [$unique, $unique, []], 31 | 32 | // // Test removing duplicate values only remove duplicate values. 33 | // [$unique, $duplicates, []], 34 | ]; 35 | } 36 | 37 | /** 38 | * @dataProvider removeDataProvider 39 | */ 40 | public function testRemove( 41 | array $initial, 42 | array $values, 43 | array $expected 44 | ) { 45 | $instance = $this->getInstance($initial); 46 | 47 | foreach ($values as $value) { 48 | $instance->remove($value); 49 | } 50 | 51 | $this->assertEquals(count($expected), count($instance)); 52 | 53 | foreach ($values as $value) { 54 | $this->assertFalse($instance->contains($value)); 55 | } 56 | 57 | foreach ($expected as $value) { 58 | $this->assertTrue($instance->contains($value)); 59 | } 60 | } 61 | 62 | /** 63 | * @dataProvider removeDataProvider 64 | */ 65 | public function testRemoveVariadic( 66 | array $initial, 67 | array $values, 68 | array $expected 69 | ) { 70 | $instance = $this->getInstance($initial); 71 | $instance->remove(...$values); 72 | 73 | $this->assertEquals(count($expected), count($instance)); 74 | 75 | foreach ($values as $value) { 76 | $this->assertFalse($instance->contains($value)); 77 | } 78 | 79 | foreach ($expected as $value) { 80 | $this->assertTrue($instance->contains($value)); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /tests/Sequence/slice.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 30 | 31 | $sliced = $instance->slice($index, $length); 32 | $expected = array_slice($values, $index, $length); 33 | 34 | $this->assertToArray($values, $instance); 35 | $this->assertToArray($expected, $sliced); 36 | } 37 | 38 | /** 39 | * @dataProvider sliceDataProvider 40 | */ 41 | public function testSliceWithoutLength(array $values, int $index, int $length) 42 | { 43 | $instance = $this->getInstance($values); 44 | 45 | $sliced = $instance->slice($index); 46 | $expected = array_slice($values, $index); 47 | 48 | $this->assertToArray($values, $instance); 49 | $this->assertToArray($expected, $sliced); 50 | } 51 | 52 | /** 53 | * @dataProvider sliceDataProvider 54 | */ 55 | public function testSliceWithLengthNull(array $values, int $index, int $length) 56 | { 57 | $instance = $this->getInstance($values); 58 | 59 | $sliced = $instance->slice($index, null); 60 | $expected = array_slice($values, $index); 61 | 62 | $this->assertToArray($values, $instance); 63 | $this->assertToArray($expected, $sliced); 64 | } 65 | 66 | public function testLargeSliceHalf() 67 | { 68 | $n = self::MANY; 69 | $x = 0; 70 | $y = intdiv($n, 2); 71 | 72 | $instance = $this->getInstance(range(0, $n)); 73 | 74 | $this->assertToArray(range($x, $y - 1), $instance->slice($x, $y)); 75 | $this->assertToArray(range($y, $n), $instance->slice($y)); 76 | } 77 | 78 | public function testLargeSliceOffset() 79 | { 80 | $n = self::MANY; 81 | $x = intdiv($n, 4); 82 | $y = intdiv($n, 4) + intdiv($n, 2); 83 | 84 | $instance = $this->getInstance(range(0, $n)); 85 | 86 | $this->assertToArray(range($x, $y - 1), $instance->slice($x, $y - $x)); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /tests/SetTest.php: -------------------------------------------------------------------------------- 1 | sample(); 57 | $duplicates = []; 58 | 59 | foreach ($sample as $value) { 60 | $duplicates[] = $value; 61 | $duplicates[] = $value; 62 | } 63 | 64 | $sample[] = new HashableObject(1); 65 | 66 | $duplicates[] = new HashableObject(1); 67 | $duplicates[] = new HashableObject(1); 68 | 69 | return [$sample, $duplicates]; 70 | } 71 | 72 | public function testArrayAccessSet() 73 | { 74 | $set = $this->getInstance(); 75 | $this->expectArrayAccessUnsupportedException(); 76 | $set['a'] = 1; 77 | } 78 | 79 | public function testImplementsArrayAccess() 80 | { 81 | $this->assertInstanceOf(ArrayAccess::class, $this->getInstance()); 82 | } 83 | 84 | /** 85 | * @see https://github.com/php-ds/ext-ds/issues/193 86 | */ 87 | public function testIssue193() 88 | { 89 | $data = [ 90 | [ 91 | 'id' => 1, 92 | 'data' => 'test', 93 | ], 94 | [ 95 | 'id' => 2, 96 | 'data' => 'test', 97 | ] 98 | ]; 99 | foreach ($data as &$item) { 100 | unset($item['id']); 101 | } 102 | $set = new \Ds\Set($data); 103 | static::assertEquals([['data' => 'test']], $set->toArray()); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/Set/reduce.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 28 | 29 | $reduced = $instance->reduce($callback, $initial); 30 | $expected = array_reduce($values, $callback, $initial); 31 | 32 | $this->assertToArray($values, $instance); 33 | $this->assertEquals($expected, $reduced); 34 | } 35 | 36 | /** 37 | * @dataProvider reduceDataProvider 38 | */ 39 | public function testReduceWithoutInitial(array $values, $initial, callable $callback) 40 | { 41 | $instance = $this->getInstance($values); 42 | 43 | $reduced = $instance->reduce($callback); 44 | $expected = array_reduce($values, $callback); 45 | 46 | $this->assertToArray($values, $instance); 47 | $this->assertEquals($expected, $reduced); 48 | } 49 | 50 | public function testReduceCallbackThrowsException() 51 | { 52 | $instance = $this->getInstance([1, 2, 3]); 53 | $result = null; 54 | 55 | try { 56 | $result = $instance->reduce(function($carry, $value) { 57 | throw new \Exception(); 58 | }); 59 | } catch (\Exception $e) { 60 | $this->assertToArray([1, 2, 3], $instance); 61 | $this->assertNull($result); 62 | return; 63 | } 64 | 65 | $this->fail('Exception should have been caught'); 66 | } 67 | 68 | public function testReduceCallbackThrowsExceptionLaterOn() 69 | { 70 | $instance = $this->getInstance([1, 2, 3]); 71 | $result = null; 72 | 73 | try { 74 | $result = $instance->reduce(function($carry, $value) { 75 | if ($value === 3) { 76 | throw new \Exception(); 77 | } 78 | }); 79 | } catch (\Exception $e) { 80 | $this->assertToArray([1, 2, 3], $instance); 81 | $this->assertNull($result); 82 | return; 83 | } 84 | 85 | $this->fail('Exception should have been caught'); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /tests/Sequence/map.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 32 | 33 | $mapped = $instance->map($callback); 34 | $expected = array_map($callback, $values); 35 | 36 | $this->assertToArray($values, $instance); 37 | $this->assertEquals($expected, $mapped->toArray()); 38 | } 39 | 40 | public function testMapCallbackThrowsException() 41 | { 42 | $instance = $this->getInstance([1, 2, 3]); 43 | $mapped = null; 44 | 45 | try { 46 | $mapped = $instance->map(function($value) { 47 | throw new \Exception(); 48 | }); 49 | } catch (\Exception $e) { 50 | $this->assertToArray([1, 2, 3], $instance); 51 | $this->assertNull($mapped); 52 | return; 53 | } 54 | 55 | $this->fail('Exception should have been caught'); 56 | } 57 | 58 | public function testMapCallbackThrowsExceptionLaterOn() 59 | { 60 | $instance = $this->getInstance([1, 2, 3]); 61 | $mapped = null; 62 | 63 | try { 64 | $mapped = $instance->map(function($value) { 65 | if ($value === 3) { 66 | throw new \Exception(); 67 | } 68 | 69 | return $value; 70 | }); 71 | } catch (\Exception $e) { 72 | $this->assertToArray([1, 2, 3], $instance); 73 | $this->assertNull($mapped); 74 | return; 75 | } 76 | 77 | $this->fail('Exception should have been caught'); 78 | } 79 | 80 | public function testMapDoesNotLeakWhenCallbackFails() 81 | { 82 | $instance = $this->getInstance(["a", "b", "c"]); 83 | 84 | static::expectException(\Exception::class); 85 | 86 | $instance->map(function($value) { 87 | if ($value === "c") { 88 | throw new \Exception(); 89 | } 90 | return $value; 91 | }); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/Set/map.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 32 | 33 | $mapped = $instance->map($callback); 34 | $expected = array_unique(array_map($callback, $values)); 35 | 36 | $this->assertToArray($values, $instance); 37 | $this->assertEquals($expected, $mapped->toArray()); 38 | } 39 | 40 | public function testMapCallbackThrowsException() 41 | { 42 | $instance = $this->getInstance([1, 2, 3]); 43 | $mapped = null; 44 | 45 | try { 46 | $mapped = $instance->map(function($value) { 47 | throw new \Exception(); 48 | }); 49 | } catch (\Exception $e) { 50 | $this->assertToArray([1, 2, 3], $instance); 51 | $this->assertNull($mapped); 52 | return; 53 | } 54 | 55 | $this->fail('Exception should have been caught'); 56 | } 57 | 58 | public function testMapCallbackThrowsExceptionLaterOn() 59 | { 60 | $instance = $this->getInstance([1, 2, 3]); 61 | $mapped = null; 62 | 63 | try { 64 | $mapped = $instance->map(function($value) { 65 | if ($value === 3) { 66 | throw new \Exception(); 67 | } 68 | 69 | return $value; 70 | }); 71 | } catch (\Exception $e) { 72 | $this->assertToArray([1, 2, 3], $instance); 73 | $this->assertNull($mapped); 74 | return; 75 | } 76 | 77 | $this->fail('Exception should have been caught'); 78 | } 79 | 80 | public function testMapDoesNotLeakWhenCallbackFails() 81 | { 82 | $instance = $this->getInstance(["a", "b", "c"]); 83 | 84 | static::expectException(\Exception::class); 85 | 86 | $instance->map(function($value) { 87 | if ($value === "c") { 88 | throw new \Exception(); 89 | } 90 | return $value; 91 | }); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/Set/xor_.php: -------------------------------------------------------------------------------- 1 | getInstance($a); 24 | $b = $this->getInstance($b); 25 | 26 | $this->assertEquals($expected, $a->xor($b)->toArray()); 27 | } 28 | 29 | /** 30 | * @dataProvider xorDataProvider 31 | */ 32 | public function testXorWithSelf(array $a, array $b, array $expected) 33 | { 34 | $a = $this->getInstance($a); 35 | $this->assertEquals([], $a->xor($a)->toArray()); 36 | } 37 | 38 | /** 39 | * @see https://github.com/php-ds/extension/issues/53 40 | */ 41 | public function testXorAfterDiff() 42 | { 43 | $a = $this->getInstance(['guest', 'member']); 44 | $b = $this->getInstance(['member', 'nothing']); 45 | 46 | $k = $a->diff($b); // [guest] 47 | $x = $a->xor($k); // [guest, member] ^ [guest] = [member] 48 | 49 | $this->assertToArray(['member'], $x); 50 | } 51 | 52 | // /** 53 | // * @dataProvider xorDataProvider 54 | // */ 55 | // public function testXorOperator(array $a, array $b, array $expected) 56 | // { 57 | // $a = $this->getInstance($a); 58 | // $b = $this->getInstance($b); 59 | 60 | // $this->assertEquals($expected, ($a ^ $b)->toArray()); 61 | // } 62 | 63 | // /** 64 | // * @dataProvider xorDataProvider 65 | // */ 66 | // public function testXorOperatorAssign(array $a, array $b, array $expected) 67 | // { 68 | // $a = $this->getInstance($a); 69 | // $b = $this->getInstance($b); 70 | 71 | // $a ^= $b; 72 | // $this->assertEquals($expected, $a->toArray()); 73 | // } 74 | 75 | // /** 76 | // * @dataProvider xorDataProvider 77 | // */ 78 | // public function testXorOperatorWithSelf(array $a, array $b, array $expected) 79 | // { 80 | // $a = $this->getInstance($a); 81 | // $this->assertEquals([], ($a ^ $a)->toArray()); 82 | // } 83 | 84 | // /** 85 | // * @dataProvider xorDataProvider 86 | // */ 87 | // public function testXorOperatorAssignWithSelf(array $a, array $b, array $expected) 88 | // { 89 | // $a = $this->getInstance($a); 90 | 91 | // $a ^= $a; 92 | // $this->assertEquals([], $a->toArray()); 93 | // } 94 | } 95 | -------------------------------------------------------------------------------- /tests/Map/slice.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 29 | 30 | $sliced = $instance->slice($index, $length); 31 | $expected = array_slice($values, $index, $length, true); 32 | 33 | $this->assertToArray($values, $instance); 34 | $this->assertToArray($expected, $sliced); 35 | } 36 | 37 | /** 38 | * @dataProvider sliceDataProvider 39 | */ 40 | public function testSliceWithoutLength(array $values, int $index, int $length) 41 | { 42 | $instance = $this->getInstance($values); 43 | 44 | $sliced = $instance->slice($index); 45 | $expected = array_slice($values, $index, null, true); 46 | 47 | $this->assertToArray($values, $instance); 48 | $this->assertToArray($expected, $sliced); 49 | } 50 | 51 | /** 52 | * @dataProvider sliceDataProvider 53 | */ 54 | public function testSliceWithLengthNull(array $values, int $index, int $length) 55 | { 56 | $instance = $this->getInstance($values); 57 | 58 | $sliced = $instance->slice($index, null); 59 | $expected = array_slice($values, $index, null, true); 60 | 61 | $this->assertToArray($values, $instance); 62 | $this->assertToArray($expected, $sliced); 63 | } 64 | 65 | public function testSliceAfterRemoveOutsideOfSlice() 66 | { 67 | $instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']); 68 | $instance->remove(3); // d 69 | 70 | $this->assertToArray(['a', 'b', 'c'], $instance->slice(0, 3)); 71 | } 72 | 73 | public function testSliceAfterRemoveAtStartOfSlice() 74 | { 75 | $instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']); 76 | $instance->remove(1); // b 77 | 78 | $this->assertToArray([ 79 | 2 => 'c', 80 | 3 => 'd', 81 | 4 => 'e', 82 | ], $instance->slice(1)); 83 | } 84 | 85 | public function testSliceAfterRemoveWithinSlice() 86 | { 87 | $instance = $this->getInstance(['a', 'b', 'c', 'd', 'e']); 88 | $instance->remove(2); // c 89 | 90 | $this->assertToArray([ 91 | 1 => 'b', 92 | 3 => 'd', 93 | 4 => 'e', 94 | ], $instance->slice(1)); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /tests/Sequence/get.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 30 | 31 | $returned = $instance->get($index); 32 | 33 | $this->assertEquals(count($initial), count($instance)); 34 | $this->assertEquals($return, $returned); 35 | } 36 | 37 | /** 38 | * @dataProvider outOfRangeDataProvider 39 | */ 40 | public function testGetIndexOutOfRange($initial, $index) 41 | { 42 | $instance = $this->getInstance($initial); 43 | $this->expectIndexOutOfRangeException(); 44 | $instance->get($index); 45 | } 46 | 47 | /** 48 | * @dataProvider badIndexDataProvider 49 | */ 50 | public function testGetIndexBadIndex($initial, $index) 51 | { 52 | $instance = $this->getInstance(); 53 | $this->expectWrongIndexTypeException(); 54 | $instance->get($index); 55 | } 56 | 57 | 58 | /** 59 | * @dataProvider getDataProvider 60 | */ 61 | public function testArrayAccessGet(array $initial, $index, $return) 62 | { 63 | $instance = $this->getInstance($initial); 64 | $this->assertEquals($return, $instance[$index]); 65 | } 66 | 67 | 68 | /** 69 | * @dataProvider badIndexDataProvider 70 | */ 71 | public function testArrayAccessGetIndexBadIndex($initial, $index) 72 | { 73 | $instance = $this->getInstance($initial); 74 | $this->expectWrongIndexTypeException(); 75 | $instance[$index]; 76 | } 77 | 78 | /** 79 | * @dataProvider outOfRangeDataProvider 80 | */ 81 | public function testArrayAccessGetIndexOutOfRange($initial, $index) 82 | { 83 | $instance = $this->getInstance($initial); 84 | $this->expectIndexOutOfRangeException(); 85 | $instance[$index]; 86 | } 87 | 88 | 89 | public function testArrayAccessGetByReference() 90 | { 91 | $instance = $this->getInstance([[1]]); 92 | $this->assertEquals(1, $instance[0][0]); 93 | } 94 | 95 | public function testArrayAccessGetNullCoalesce() 96 | { 97 | $instance = $this->getInstance(); 98 | 99 | $this->assertEquals(null, $instance[10] ?? null); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /tests/Map/map.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 26 | 27 | $mapped = $instance->map($callback); 28 | $expected = array_map($callback, array_keys($values), $values); 29 | 30 | $this->assertToArray($values, $instance); 31 | $this->assertEquals($expected, $mapped->toArray()); 32 | } 33 | 34 | public function testMapPreservesKeys() 35 | { 36 | $instance = $this->getInstance(["speed" => 5]); 37 | 38 | $mapped = $instance->map(function ($key, $value) { 39 | return $value * 2; 40 | }); 41 | 42 | $this->assertToArray(["speed" => 10], $mapped); 43 | } 44 | 45 | public function testMapCallbackThrowsException() 46 | { 47 | $instance = $this->getInstance([1, 2, 3]); 48 | $mapped = null; 49 | 50 | try { 51 | $mapped = $instance->map(function($value) { 52 | throw new \Exception(); 53 | }); 54 | } catch (\Exception $e) { 55 | $this->assertToArray([1, 2, 3], $instance); 56 | $this->assertNull($mapped); 57 | return; 58 | } 59 | 60 | $this->fail('Exception should have been caught'); 61 | } 62 | 63 | public function testMapCallbackThrowsExceptionLaterOn() 64 | { 65 | $instance = $this->getInstance([1, 2, 3]); 66 | $mapped = null; 67 | 68 | try { 69 | $mapped = $instance->map(function($key, $value) { 70 | if ($value === 3) { 71 | throw new \Exception(); 72 | } 73 | }); 74 | } catch (\Exception $e) { 75 | $this->assertToArray([1, 2, 3], $instance); 76 | $this->assertNull($mapped); 77 | return; 78 | } 79 | 80 | $this->fail('Exception should have been caught'); 81 | } 82 | 83 | public function testMapDoesNotLeakWhenCallbackFails() 84 | { 85 | $instance = $this->getInstance([ 86 | "a" => new \stdClass(), 87 | "b" => new \stdClass(), 88 | "c" => new \stdClass(), 89 | ]); 90 | 91 | static::expectException(\Exception::class); 92 | 93 | $mapped = $instance->map(function($key, $value) { 94 | if ($key === "c") { 95 | throw new \Exception(); 96 | } 97 | }); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /tests/Map/filter.php: -------------------------------------------------------------------------------- 1 | 2]], 16 | [[1, 2, 3], function ($k, $v) { return $v & 1; }, [0 => 1, 2 => 3]], 17 | 18 | // Test not asking for the value. 19 | [[1, 2, 3], function ($k) { return $k & 1; }, [1 => 2]], 20 | ]; 21 | } 22 | 23 | /** 24 | * @dataProvider filterDataProvider 25 | */ 26 | public function testFilter(array $values, callable $callback, array $expected) 27 | { 28 | $instance = $this->getInstance($values); 29 | 30 | $filtered = $instance->filter($callback); 31 | 32 | $this->assertToArray($values, $instance); 33 | $this->assertEquals($expected, $filtered->toArray()); 34 | } 35 | 36 | public function testFilterCallbackThrowsException() 37 | { 38 | $instance = $this->getInstance([1, 2, 3]); 39 | $filtered = null; 40 | 41 | try { 42 | $filtered = $instance->filter(function($key, $value) { 43 | throw new \Exception(); 44 | }); 45 | } catch (\Exception $e) { 46 | $this->assertToArray([1, 2, 3], $instance); 47 | $this->assertNull($filtered); 48 | return; 49 | } 50 | 51 | $this->fail('Exception should have been caught'); 52 | } 53 | 54 | public function testFilterCallbackThrowsExceptionLaterOn() 55 | { 56 | $instance = $this->getInstance([1, 2, 3]); 57 | $filtered = null; 58 | 59 | try { 60 | $filtered = $instance->filter(function($key, $value) { 61 | if ($value === 3) { 62 | throw new \Exception(); 63 | } 64 | }); 65 | } catch (\Exception $e) { 66 | $this->assertToArray([1, 2, 3], $instance); 67 | $this->assertNull($filtered); 68 | return; 69 | } 70 | 71 | $this->fail('Exception should have been caught'); 72 | } 73 | 74 | public function testFilterDoesNotLeakWhenCallbackFails() 75 | { 76 | $instance = $this->getInstance([ 77 | "a" => new \stdClass(), 78 | "b" => new \stdClass(), 79 | "c" => new \stdClass(), 80 | ]); 81 | 82 | static::expectException(\Exception::class); 83 | 84 | $mapped = $instance->filter(function($key, $value) { 85 | if ($key === "c") { 86 | throw new \Exception(); 87 | } 88 | }); 89 | } 90 | 91 | public function testFilterWithoutCallable() 92 | { 93 | $values = [ 94 | "a" => 1, 95 | "b" => 2, 96 | "c" => true, 97 | "d" => false, 98 | "e" => [], 99 | "f" => 0, 100 | ]; 101 | 102 | $instance = $this->getInstance($values); 103 | $this->assertToArray(array_filter($values), $instance->filter()); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/Sequence/reduce.php: -------------------------------------------------------------------------------- 1 | getInstance($values); 29 | 30 | $reduced = $instance->reduce($callback, $initial); 31 | $expected = array_reduce($values, $callback, $initial); 32 | 33 | $this->assertToArray($values, $instance); 34 | $this->assertEquals($expected, $reduced); 35 | } 36 | 37 | /** 38 | * @dataProvider reduceDataProvider 39 | */ 40 | public function testReduceWithoutInitial(array $values, $initial, callable $callback) 41 | { 42 | $instance = $this->getInstance($values); 43 | 44 | $reduced = $instance->reduce($callback); 45 | $expected = array_reduce($values, $callback); 46 | 47 | $this->assertToArray($values, $instance); 48 | $this->assertEquals($expected, $reduced); 49 | } 50 | 51 | public function testReduceCallbackThrowsException() 52 | { 53 | $instance = $this->getInstance(["a", "b", "c"]); 54 | $result = null; 55 | 56 | try { 57 | $result = $instance->reduce(function($carry, $value) { 58 | throw new \Exception(); 59 | }); 60 | } catch (\Exception $e) { 61 | $this->assertToArray(["a", "b", "c"], $instance); 62 | $this->assertNull($result); 63 | return; 64 | } 65 | 66 | $this->fail('Exception should have been caught'); 67 | } 68 | 69 | public function testReduceCallbackThrowsExceptionLaterOn() 70 | { 71 | $instance = $this->getInstance(["a", "b", "c"]); 72 | $result = null; 73 | 74 | try { 75 | $result = $instance->reduce(function($carry, $value) { 76 | if ($value === "c") { 77 | throw new \Exception(); 78 | } 79 | 80 | return $value; 81 | }); 82 | } catch (\Exception $e) { 83 | $this->assertToArray(["a", "b", "c"], $instance); 84 | $this->assertNull($result); 85 | return; 86 | } 87 | 88 | $this->fail('Exception should have been caught'); 89 | } 90 | 91 | public function testReduceCallbackDoesNotLeakOnFailure() 92 | { 93 | $instance = $this->getInstance(["a", "b", "c"]); 94 | 95 | static::expectException(\Exception::class); 96 | 97 | $instance->reduce(function($carry, $value) { 98 | if ($value === "c") { 99 | throw new \Exception(); 100 | } 101 | return $value; 102 | }); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/Set/union.php: -------------------------------------------------------------------------------- 1 | getInstance($initial); 25 | $b = $this->getInstance($values); 26 | 27 | $this->assertEquals($expected, $a->union($b)->toArray()); 28 | } 29 | 30 | /** 31 | * @dataProvider unionDataProvider 32 | */ 33 | public function testUnionWithSelf(array $initial, array $values, array $expected) 34 | { 35 | $a = $this->getInstance($initial); 36 | $this->assertEquals($initial, $a->union($a)->toArray()); 37 | } 38 | 39 | public function testUnionWhenOperatingOnSetsWithObjectsWithNonZeroHash() 40 | { 41 | $a = new \Ds\Tests\HashableObject("a", rand()); 42 | $b = new \Ds\Tests\HashableObject("b", rand()); 43 | 44 | $setA = $this->getInstance([$a]); 45 | $setB = $this->getInstance([$b]); 46 | 47 | $this->assertToArray([$a, $b], $setA->union($setB)); 48 | } 49 | 50 | public function testUnionWhenOperatingOnSetsWithObjectsWithZeroHash() 51 | { 52 | $a = new \Ds\Tests\HashableObject("a", 0); 53 | $b = new \Ds\Tests\HashableObject("b", 0); 54 | 55 | $setA = $this->getInstance([$a]); 56 | $setB = $this->getInstance([$b]); 57 | 58 | $this->assertToArray([$a, $b], $setA->union($setB)); 59 | } 60 | 61 | // /** 62 | // * @dataProvider unionDataProvider 63 | // */ 64 | // public function testUnionOperator(array $initial, array $values, array $expected) 65 | // { 66 | // $a = $this->getInstance($initial); 67 | // $b = $this->getInstance($values); 68 | 69 | // $this->assertEquals($expected, ($a | $b)->toArray()); 70 | // } 71 | 72 | // /** 73 | // * @dataProvider unionDataProvider 74 | // */ 75 | // public function testUnionOperatorAssign(array $initial, array $values, array $expected) 76 | // { 77 | // $a = $this->getInstance($initial); 78 | // $b = $this->getInstance($values); 79 | 80 | // $a |= $b; 81 | // $this->assertEquals($expected, $a->toArray()); 82 | // } 83 | 84 | // /** 85 | // * @dataProvider unionDataProvider 86 | // */ 87 | // public function testUnionOperatorWithSelf(array $initial, array $values, array $expected) 88 | // { 89 | // $a = $this->getInstance($initial); 90 | // $this->assertEquals($initial, ($a | $a)->toArray()); 91 | // } 92 | 93 | // /** 94 | // * @dataProvider unionDataProvider 95 | // */ 96 | // public function testUnionOperatorAssignWithSelf(array $initial, array $values, array $expected) 97 | // { 98 | // $a = $this->getInstance($initial); 99 | 100 | // $a |= $a; 101 | // $this->assertEquals($initial, $a->toArray()); 102 | // } 103 | } 104 | --------------------------------------------------------------------------------