Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArraySliceView read tests added. #14

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Selectors/SliceSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class SliceSelector extends Slice implements ArraySelectorInterface
{
/**
* @param Slice|string $slice
* @param Slice|string|array<int> $slice
*/
public function __construct($slice)
{
Expand Down
8 changes: 6 additions & 2 deletions src/Structs/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Slice
public ?int $step;

/**
* @param string|Slice $s
* @param string|Slice|array<int> $s
*
* @return Slice
*/
Expand All @@ -38,6 +38,10 @@ public static function toSlice($s): Slice
return $s;
}

if (\is_array($s) && self::isSliceArray($s)) {
return new Slice(...$s);
}

if (!self::isSliceString($s)) {
$str = \is_scalar($s) ? "{$s}" : gettype($s);
throw new ValueError("Invalid slice: \"{$str}\".");
Expand All @@ -56,7 +60,7 @@ public static function toSlice($s): Slice
*/
public static function isSlice($s): bool
{
return ($s instanceof Slice) || static::isSliceString($s);
return ($s instanceof Slice) || static::isSliceString($s) || static::isSliceArray($s);
}

/**
Expand Down
162 changes: 162 additions & 0 deletions tests/unit/ArraySliceView/ReadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace Smoren\ArrayView\Tests\Unit\ArraySliceView;

use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Tests\Unit\Fixtures\PythonDataProviders;
use Smoren\ArrayView\Views\ArraySliceView;
use Smoren\ArrayView\Views\ArrayView;

class ReadTest extends \Codeception\Test\Unit
{
use PythonDataProviders;

/**
* @dataProvider dataProviderForRead
* @dataProvider dataProviderForSliceInBoundsPythonAutoGenerated
* @dataProvider dataProviderForSliceOutOfBoundsPythonAutoGenerated
*/
public function testReadByMethod(array $source, string $config, array $expected)
{
$view = ArrayView::toView($source);
$subview = $view->subview($config);

$this->assertInstanceOf(ArraySliceView::class, $subview);

$this->assertSame($expected, [...$subview]);
$this->assertSame(\count($expected), \count([...$subview]));

for ($i = 0; $i < \count($subview); ++$i) {
$this->assertSame($expected[$i], $subview[$i]);
}

for ($i = 0; $i < \count($view); ++$i) {
$this->assertSame($source[$i], $view[$i]);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($expected, $subview->toArray());

$this->assertSame($source, [...$view]);
$this->assertSame($expected, [...$subview]);
}

/**
* @dataProvider dataProviderForRead
* @dataProvider dataProviderForSliceInBoundsPythonAutoGenerated
* @dataProvider dataProviderForSliceOutOfBoundsPythonAutoGenerated
*/
public function testReadByIndex(array $source, string $config, array $expected)
{
$view = ArrayView::toView($source);
$slicedArray = $view[$config];

$this->assertSame($expected, $slicedArray);
$this->assertSame(\count($expected), \count($slicedArray));

for ($i = 0; $i < \count($slicedArray); ++$i) {
$this->assertSame($expected[$i], $slicedArray[$i]);
}

for ($i = 0; $i < \count($view); ++$i) {
$this->assertSame($source[$i], $view[$i]);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($source, [...$view]);
$this->assertSame($expected, $slicedArray);
}

/**
* @dataProvider dataProviderForRead
* @dataProvider dataProviderForSliceArraySubviewRead
* @dataProvider dataProviderForSliceInBoundsPythonAutoGenerated
* @dataProvider dataProviderForSliceOutOfBoundsPythonAutoGenerated
*/
public function testReadByMethodAndIndex(array $source, $config, array $expected)
{
$view = ArrayView::toView($source);
$slicedArray = $view->subview(new SliceSelector($config))[':'];

$this->assertSame($expected, $slicedArray);
$this->assertSame(\count($expected), \count($slicedArray));

for ($i = 0; $i < \count($slicedArray); ++$i) {
$this->assertSame($expected[$i], $slicedArray[$i]);
}

for ($i = 0; $i < \count($view); ++$i) {
$this->assertSame($source[$i], $view[$i]);
}

$this->assertSame($source, $view->toArray());
$this->assertSame($source, [...$view]);
$this->assertSame($expected, $slicedArray);
}

public function dataProviderForRead(): array
{
return [
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6', [2, 3, 4, 5, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6:1', [2, 3, 4, 5, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:6:2', [2, 4, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8', [3, 4, 5, 6, 7, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8:1', [3, 4, 5, 6, 7, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '2:8:2', [3, 5, 7]],

[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-1::-1', [9, 8, 7, 6, 5, 4, 3, 2, 1]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-1:0:-1', [9, 8, 7, 6, 5, 4, 3, 2]],

[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:9:1', [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:9:2', [1, 3, 5, 7, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1:9:2', [2, 4, 6, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:10:1', [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '0:10:2', [1, 3, 5, 7, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-9:9:1', [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-9:9:2', [1, 3, 5, 7, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-10:10:1', [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-10:10:2', [1, 3, 5, 7, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-5:10:1', [5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '-5:100:2', [5, 7, 9]],

[[], '0:', []],
[[], '0:0', []],
[[], '0:0:1', []],
[[], '1:-1', []],
[[], '-1:-1', []],
[[], '-2:-1', []],
[[], '-2:-1:2', []],
[[], '-1:0:-1', []],
[[1], '0:', [1]],
[[1], '0:1', [1]],
[[1], '0:1:1', [1]],
[[1], '0:1:2', [1]],
[[1], '0:-1', []],
[[1], '0:-1:1', []],
[[1], '0:-1:2', []],
[[1], '0:10:100', [1]],
[[1], '1:10:100', []],
[[1], '0:', [1]],
[[1, 2, 3], '0:0:1', []],
[[1], '1:', []],
[[1, 2], '1:0', []],
[[1, 2], '1::-1', [2, 1]],
[[1, 2], '0:1', [1]],
[[1, 2], '1:1', []],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '1::2', [2, 4, 6, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], '::2', [1, 3, 5, 7, 9]],
];
}

public function dataProviderForSliceArraySubviewRead(): array
{
return [
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6], [2, 3, 4, 5, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6,1], [2, 3, 4, 5, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1,6,2], [2, 4, 6]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8], [3, 4, 5, 6, 7, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8,1], [3, 4, 5, 6, 7, 8]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2,8,2], [3, 5, 7]],
];
}
}
Loading
Loading