2020-02-21 02:03:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Collection;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-03-01 21:52:25 +01:00
|
|
|
use Psl\Collection\IVector;
|
2020-02-21 02:03:40 +01:00
|
|
|
use Psl\Exception\InvariantViolationException;
|
2020-03-01 21:52:25 +01:00
|
|
|
use Psl\Str;
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Psl\Collection\AbstractVector
|
|
|
|
* @covers \Psl\Collection\AbstractAccessibleCollection
|
|
|
|
*/
|
|
|
|
abstract class AbstractVectorTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The Vector class used for values, keys .. etc.
|
|
|
|
*
|
|
|
|
* @psalm-var class-string<IVector>
|
|
|
|
*/
|
|
|
|
protected string $vectorClass = IVector::class;
|
|
|
|
|
|
|
|
public function testIsEmpty(): void
|
|
|
|
{
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertTrue($this->create([])->isEmpty());
|
|
|
|
self::assertFalse($this->create(['foo', 'bar'])->isEmpty());
|
|
|
|
self::assertEmpty($this->create([null])->isEmpty());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCount(): void
|
|
|
|
{
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertCount(0, $this->create([]));
|
|
|
|
self::assertCount(2, $this->create(['foo', 'bar']));
|
|
|
|
self::assertSame(5, $this->create([
|
2020-02-21 02:03:40 +01:00
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
'hax' // ??
|
|
|
|
])->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValues(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([1, 2, 3]);
|
|
|
|
|
|
|
|
$values = $vector->values();
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $values);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertCount(3, $values);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(1, $values->at(0));
|
|
|
|
self::assertSame(2, $values->at(1));
|
|
|
|
self::assertSame(3, $values->at(2));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$values = $vector->values();
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $values);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertCount(0, $values);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testKeys(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
]);
|
|
|
|
$keys = $vector->keys();
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $keys);
|
|
|
|
self::assertCount(3, $keys);
|
|
|
|
self::assertSame(0, $keys->at(0));
|
|
|
|
self::assertSame(1, $keys->at(1));
|
|
|
|
self::assertSame(2, $keys->at(2));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$keys = $vector->keys();
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $keys);
|
|
|
|
self::assertCount(0, $keys);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilter(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$filtered = $vector->filter(fn (string $item) => Str\contains($item, 'b'));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $filtered);
|
|
|
|
self::assertNotSame($vector, $filtered);
|
|
|
|
self::assertContains('bar', $filtered);
|
|
|
|
self::assertContains('baz', $filtered);
|
|
|
|
self::assertNotContains('foo', $filtered);
|
|
|
|
self::assertNotContains('qux', $filtered);
|
|
|
|
self::assertCount(2, $filtered);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$filtered = $vector->filter(fn (string $item) => Str\contains($item, 'hello'));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $filtered);
|
|
|
|
self::assertNotContains('bar', $filtered);
|
|
|
|
self::assertNotContains('baz', $filtered);
|
|
|
|
self::assertNotContains('foo', $filtered);
|
|
|
|
self::assertNotContains('qux', $filtered);
|
|
|
|
self::assertCount(0, $filtered);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterWithKey(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$filtered = $vector->filterWithKey(fn (int $k, string $v) => 'foo' === $v || 3 === $k);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $filtered);
|
|
|
|
self::assertNotSame($vector, $filtered);
|
|
|
|
self::assertContains('foo', $filtered);
|
|
|
|
self::assertContains('qux', $filtered);
|
|
|
|
self::assertNotContains('bar', $filtered);
|
|
|
|
self::assertNotContains('baz', $filtered);
|
|
|
|
self::assertCount(2, $filtered);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$filtered = $vector->filterWithKey(fn (int $k, string $v) => 4 === $k);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $filtered);
|
|
|
|
self::assertNotContains('bar', $filtered);
|
|
|
|
self::assertNotContains('baz', $filtered);
|
|
|
|
self::assertNotContains('foo', $filtered);
|
|
|
|
self::assertNotContains('qux', $filtered);
|
|
|
|
self::assertCount(0, $filtered);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMap(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$mapped = $vector->map(fn (string $item) => Str\uppercase($item));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $mapped);
|
|
|
|
self::assertSame([
|
2020-02-21 02:03:40 +01:00
|
|
|
'FOO',
|
|
|
|
'BAR',
|
|
|
|
'BAZ',
|
|
|
|
'QUX',
|
|
|
|
], $mapped->toArray());
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNotSame($vector, $mapped);
|
|
|
|
self::assertCount(4, $mapped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$mapped = $vector->map(fn (string $item) => $item);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $mapped);
|
|
|
|
self::assertNotSame($vector, $mapped);
|
|
|
|
self::assertSame($vector->toArray(), $mapped->toArray());
|
|
|
|
self::assertCount(4, $mapped);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMapWithKey(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
0 => 'foo',
|
|
|
|
1 => 'bar',
|
|
|
|
2 => 'baz',
|
|
|
|
3 => 'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$mapped = $vector->mapWithKey(fn (int $k, string $v) => Str\format('%s ( %d )', $v, $k));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $mapped);
|
|
|
|
self::assertSame([
|
2020-02-21 02:03:40 +01:00
|
|
|
'foo ( 0 )',
|
|
|
|
'bar ( 1 )',
|
|
|
|
'baz ( 2 )',
|
|
|
|
'qux ( 3 )',
|
|
|
|
], $mapped->toArray());
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNotSame($vector, $mapped);
|
|
|
|
self::assertCount(4, $mapped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$mapped = $vector->mapWithKey(fn (int $k, string $v) => $k);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $mapped);
|
|
|
|
self::assertNotSame($vector, $mapped);
|
|
|
|
self::assertSame($vector->keys()->toArray(), $mapped->toArray());
|
|
|
|
self::assertCount(4, $mapped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
$mapped = $vector->mapWithKey(fn (int $k, string $v) => $v);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $mapped);
|
|
|
|
self::assertNotSame($vector, $mapped);
|
|
|
|
self::assertSame($vector->toArray(), $mapped->toArray());
|
|
|
|
self::assertCount(4, $mapped);
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFirst(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->first());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([null]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->first());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('foo', $vector->first());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('bar', $vector->first());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFirstKey(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->firstKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([null]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->firstKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->firstKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->firstKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLast(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->last());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([null]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->last());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('foo', $vector->last());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('qux', $vector->last());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLastKey(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->lastKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([null]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->lastKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->lastKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(1, $vector->lastKey());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinearSearch(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertNull($vector->linearSearch('foo'));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(0, $vector->linearSearch('foo'));
|
|
|
|
self::assertSame(1, $vector->linearSearch('bar'));
|
|
|
|
self::assertNull($vector->linearSearch('baz'));
|
|
|
|
self::assertNull($vector->linearSearch('qux'));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testZip(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$zipped = $vector->zip([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(0, $zipped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$zipped = $vector->zip([1, 2]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(0, $zipped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo', 'bar']);
|
|
|
|
$zipped = $vector->zip([]);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(0, $zipped);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo', 'bar']);
|
|
|
|
$zipped = $vector->zip(['baz', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(2, $zipped);
|
|
|
|
self::assertSame(['foo', 'baz'], $zipped->at(0));
|
|
|
|
self::assertSame(['bar', 'qux'], $zipped->at(1));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['foo', 'bar', 'baz', 'qux']);
|
|
|
|
$zipped = $vector->zip(['hello', 'world']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(2, $zipped);
|
|
|
|
self::assertSame(['foo', 'hello'], $zipped->at(0));
|
|
|
|
self::assertSame(['bar', 'world'], $zipped->at(1));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['hello', 'world']);
|
|
|
|
$zipped = $vector->zip(['foo', 'bar', 'baz', 'qux']);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $zipped);
|
|
|
|
self::assertCount(2, $zipped);
|
|
|
|
self::assertSame(['hello', 'foo'], $zipped->at(0));
|
|
|
|
self::assertSame(['world', 'bar'], $zipped->at(1));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTake(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$rest = $vector->take(2);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
|
|
|
$rest = $vector->take(4);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(2, $rest);
|
|
|
|
self::assertSame($vector->toArray(), $rest->toArray());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
|
|
|
$rest = $vector->take(1);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(1, $rest);
|
|
|
|
self::assertSame('bar', $rest->at(0));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTakeWhile(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->takeWhile(fn ($v) => false);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([]);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->takeWhile(fn ($v) => true);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->takeWhile(fn ($v) => true);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(2, $rest);
|
|
|
|
self::assertSame($vector->toArray(), $rest->toArray());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->takeWhile(fn ($v) => 'bar' === $v);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(1, $rest);
|
|
|
|
self::assertSame('bar', $rest->at(0));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDrop(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
|
|
|
$rest = $vector->drop(2);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
|
|
|
$rest = $vector->drop(4);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
|
|
|
$rest = $vector->drop(1);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(1, $rest);
|
|
|
|
self::assertSame('qux', $rest->at(0));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
|
|
|
$rest = $vector->drop(0);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(2, $rest);
|
|
|
|
self::assertSame($vector->toArray(), $rest->toArray());
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDropWhile(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([]);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->dropWhile(fn ($v) => true);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create([]);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->dropWhile(fn ($v) => false);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->dropWhile(fn ($v) => true);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(0, $rest);
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->dropWhile(fn ($v) => false);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(2, $rest);
|
|
|
|
self::assertSame($vector->toArray(), $rest->toArray());
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector = $this->create(['bar', 'qux']);
|
2020-03-01 21:52:25 +01:00
|
|
|
$rest = $vector->dropWhile(fn ($v) => 'bar' === $v);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $rest);
|
|
|
|
self::assertNotSame($vector, $rest);
|
|
|
|
self::assertCount(1, $rest);
|
|
|
|
self::assertSame('qux', $rest->at(0));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSlice(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'foo',
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'baz',
|
|
|
|
'qux',
|
|
|
|
'qux',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$slice1 = $vector->slice(0, 1);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $slice1);
|
|
|
|
self::assertNotSame($slice1, $vector);
|
|
|
|
self::assertCount(1, $slice1);
|
|
|
|
self::assertSame('foo', $slice1->at(0));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$slice2 = $vector->slice(2, 4);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertInstanceOf($this->vectorClass, $slice1);
|
|
|
|
self::assertNotSame($slice2, $vector);
|
|
|
|
self::assertCount(4, $slice2);
|
|
|
|
self::assertSame([
|
2020-02-21 02:03:40 +01:00
|
|
|
'bar',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
'baz',
|
|
|
|
], $slice2->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAt(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'hello',
|
|
|
|
'world',
|
|
|
|
]);
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('hello', $vector->at(0));
|
|
|
|
self::assertSame('world', $vector->at(1));
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$this->expectException(InvariantViolationException::class);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Key (2) is out-of-bounds.');
|
2020-02-21 02:03:40 +01:00
|
|
|
|
|
|
|
$vector->at(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testContains(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'hello',
|
|
|
|
'world',
|
|
|
|
]);
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertTrue($vector->contains(0));
|
|
|
|
self::assertTrue($vector->contains(1));
|
|
|
|
self::assertFalse($vector->contains(2));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGet(): void
|
|
|
|
{
|
|
|
|
$vector = $this->create([
|
|
|
|
'hello',
|
|
|
|
'world',
|
|
|
|
]);
|
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('hello', $vector->get(0));
|
|
|
|
self::assertSame('world', $vector->get(1));
|
|
|
|
self::assertNull($vector->get(2));
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|
2020-03-01 21:52:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
*
|
|
|
|
* @psalm-param iterable<T> $items
|
|
|
|
*
|
|
|
|
* @psalm-return IVector<T>
|
|
|
|
*/
|
|
|
|
abstract protected function create(iterable $items): IVector;
|
2020-02-21 02:03:40 +01:00
|
|
|
}
|