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

More tests added. #11

Merged
merged 3 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
55 changes: 44 additions & 11 deletions src/Structs/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ class Slice
*/
public static function toSlice($s): Slice
{
/** @var mixed $s */
if ($s instanceof Slice) {
return $s;
}

if (!self::isSliceString($s)) {
throw new ValueError("Invalid slice: \"{$s}\".");
$str = \is_scalar($s) ? "{$s}" : gettype($s);
throw new ValueError("Invalid slice: \"{$str}\".");
}

/** @var string $s */
$slice = self::parseSliceString($s);

return new Slice(...$slice);
Expand Down Expand Up @@ -80,6 +83,33 @@ public static function isSliceString($s): bool
return !(\count($slice) < 1 || \count($slice) > 3);
}

/**
* @param mixed $s
*
* @return bool
*/
public static function isSliceArray($s): bool
{
if (!\is_array($s)) {
return false;
}

if (\count($s) > 3) {
return false;
}

foreach ($s as $key => $item) {
if (\is_string($key)) {
return false;
}
if ($item !== null && (!\is_numeric($item) || \is_float($item + 0))) {
return false;
}
}

return true;
}

/**
* @param int|null $start
* @param int|null $end
Expand All @@ -93,11 +123,11 @@ public function __construct(?int $start = null, ?int $end = null, ?int $step = n
}

/**
* @param int $containerLength
* @param int $containerSize
*
* @return NormalizedSlice
*/
public function normalize(int $containerLength): NormalizedSlice
public function normalize(int $containerSize): NormalizedSlice
{
// TODO: Need refactor
$step = $this->step ?? 1;
Expand All @@ -108,25 +138,25 @@ public function normalize(int $containerLength): NormalizedSlice

$defaultEnd = ($step < 0 && $this->end === null) ? -1 : null;

$start = $this->start ?? ($step > 0 ? 0 : $containerLength - 1);
$end = $this->end ?? ($step > 0 ? $containerLength : -1);
$start = $this->start ?? ($step > 0 ? 0 : $containerSize - 1);
$end = $this->end ?? ($step > 0 ? $containerSize : -1);

$start = intval(round($start));
$end = intval(round($end));
$step = intval(round($step));

$start = Util::normalizeIndex($start, $containerLength, false);
$end = Util::normalizeIndex($end, $containerLength, false);
$start = Util::normalizeIndex($start, $containerSize, false);
$end = Util::normalizeIndex($end, $containerSize, false);

if ($step > 0 && $start >= $containerLength) {
$start = $end = $containerLength - 1;
if ($step > 0 && $start >= $containerSize) {
$start = $end = $containerSize - 1;
} elseif ($step < 0 && $start < 0) {
$start = $end = 0;
$defaultEnd = 0;
}

$start = $this->squeezeInBounds($start, 0, $containerLength - 1);
$end = $this->squeezeInBounds($end, $step > 0 ? 0 : -1, $containerLength);
$start = $this->squeezeInBounds($start, 0, $containerSize - 1);
$end = $this->squeezeInBounds($end, $step > 0 ? 0 : -1, $containerSize);

if (($step > 0 && $end < $start) || ($step < 0 && $end > $start)) {
$end = $start;
Expand All @@ -150,6 +180,9 @@ public function toString(): string
*/
private static function parseSliceString(string $s): array
{
if ($s === '') {
return [];
}
return array_map(fn($x) => trim($x) === '' ? null : \intval(trim($x)), \explode(':', $s));
}

Expand Down
150 changes: 150 additions & 0 deletions tests/unit/ArrayView/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Smoren\ArrayView\Exceptions\SizeError;
use Smoren\ArrayView\Exceptions\ValueError;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Views\ArrayView;

class ErrorsTest extends \Codeception\Test\Unit
Expand Down Expand Up @@ -127,6 +128,64 @@ public function testWriteByMaskSizeError(array $source, array $boolMask)
$view[new MaskSelector($boolMask)] = $boolMask;
}

/**
* @dataProvider dataProviderForInvalidSlice
*/
public function testInvalidSliceRead(array $source, string $slice)
{
$view = ArrayView::toView($source);

$this->expectException(IndexError::class);
$this->expectExceptionMessage("Step cannot be 0.");

$_ = $view[new SliceSelector($slice)];
}

/**
* @dataProvider dataProviderForInvalidSlice
*/
public function testInvalidSliceWrite(array $source, string $slice)
{
$view = ArrayView::toView($source);

$this->expectException(IndexError::class);
$this->expectExceptionMessage("Step cannot be 0.");

$view[new SliceSelector($slice)] = [1, 2, 3];
}

/**
* @dataProvider dataProviderForApplyWithSizeError
*/
public function testApplyWithSizeError(array $source, callable $viewGetter, callable $mapper, array $toApplyWith)
{
$view = ArrayView::toView($source);

$sourceSize = \count($source);
$argSize = \count($toApplyWith);

$this->expectException(SizeError::class);
$this->expectExceptionMessage("Length of values array not equal to view length ({$argSize} != {$sourceSize}).");

$view->applyWith($toApplyWith, $mapper);
}

/**
* @dataProvider dataProviderForWriteSizeError
*/
public function testWriteSizeError(array $source, callable $viewGetter, array $toWrite)
{
$view = ArrayView::toView($source);

$sourceSize = \count($source);
$argSize = \count($toWrite);

$this->expectException(SizeError::class);
$this->expectExceptionMessage("Length of values array not equal to view length ({$argSize} != {$sourceSize}).");

$view[':'] = $toWrite;
}

/**
* @dataProvider dataProviderForNonSequentialError
*/
Expand Down Expand Up @@ -177,6 +236,97 @@ public function dataProviderForBadSizeMask(): array
];
}

public function dataProviderForInvalidSlice(): array
{
return [
[[], '::0'],
[[], '0:0:0'],
[[], '0:1:0'],
[[], '0::0'],
[[], ':-1:0'],
[[], '1:-1:0'],
[[1], '::0'],
[[1], '0:0:0'],
[[1], '0:1:0'],
[[1], '0::0'],
[[1], ':-1:0'],
[[1], '1:-1:0'],
[[1, 2, 3], '::0'],
[[1, 2, 3], '0:0:0'],
[[1, 2, 3], '0:1:0'],
[[1, 2, 3], '0::0'],
[[1, 2, 3], ':-1:0'],
[[1, 2, 3], '1:-1:0'],
];
}

public function dataProviderForApplyWithSizeError(): array
{
return [
[
[],
fn (array &$source) => ArrayView::toView($source),
fn (int $item) => $item,
[1],
],
[
[1],
fn (array &$source) => ArrayView::toView($source),
fn (int $item) => $item,
[],
],
[
[1],
fn (array &$source) => ArrayView::toView($source),
fn (int $item) => $item,
[1, 2],
],
[
[1, 2, 3],
fn (array &$source) => ArrayView::toView($source),
fn (int $item) => $item,
[1, 2],
],
[
[1, 2, 3],
fn (array &$source) => ArrayView::toView($source),
fn (int $item) => $item,
[1, 2, 3, 4, 5],
],
];
}

public function dataProviderForWriteSizeError(): array
{
return [
[
[],
fn (array &$source) => ArrayView::toView($source),
[1],
],
[
[1],
fn (array &$source) => ArrayView::toView($source),
[],
],
[
[1],
fn (array &$source) => ArrayView::toView($source),
[1, 2],
],
[
[1, 2, 3],
fn (array &$source) => ArrayView::toView($source),
[1, 2],
],
[
[1, 2, 3],
fn (array &$source) => ArrayView::toView($source),
[1, 2, 3, 4, 5],
],
];
}

public function dataProviderForNonSequentialError(): array
{
return [
Expand Down
Loading
Loading