Skip to content

Commit

Permalink
Util::normalizeIndex() tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 12, 2024
1 parent 371745b commit b49a921
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/unit/Structs/SliceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testIsSliceFalse($input)
public function testSliceError($input)
{
$this->expectException(ValueError::class);
$strInput = \is_scalar($input) ? "{$input}" : gettype($input);
$strInput = \is_scalar($input) ? "{$input}" : \gettype($input);
$this->expectExceptionMessage("Invalid slice: \"{$strInput}\"");

Slice::toSlice($input);
Expand Down
110 changes: 110 additions & 0 deletions tests/unit/Utils/NormalizeIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Tests\Unit\Utils;

use Smoren\ArrayView\Exceptions\IndexError;
use Smoren\ArrayView\Util;

class NormalizeIndexTest extends \Codeception\Test\Unit
{
/**
* @dataProvider dataProviderForNormalizeIndexSuccess
*/
public function testNormalizeIndexSuccess(array $source, int $index, int $expected)
{
$normalizedIndex = Util::normalizeIndex($index, \count($source));
$this->assertSame($expected, $source[$normalizedIndex]);
}

/**
* @dataProvider dataProviderForNormalizeIndexNotThrow
*/
public function testNormalizeIndexNotThrow(array $source, int $index, int $expected)
{
$normalizedIndex = Util::normalizeIndex($index, \count($source), false);
$this->assertSame($expected, $normalizedIndex);
}

/**
* @dataProvider dataProviderForNormalizeIndexError
*/
public function testNormalizeIndexError(array $source, int $index)
{
$this->expectException(IndexError::class);
$this->expectExceptionMessage("Index {$index} is out of range.");
Util::normalizeIndex($index, \count($source));
}

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

public function dataProviderForNormalizeIndexNotThrow(): array
{
return [
[[1], 0, 0],
[[1], -1, 0],
[[1, 2], 0, 0],
[[1, 2], -1, 1],
[[1, 2], 1, 1],
[[1, 2], -2, 0],
[[1, 2, 3], 0, 0],
[[1, 2, 3], -1, 2],
[[1, 2, 3], 1, 1],
[[1, 2, 3], -2, 1],
[[1, 2, 3], 2, 2],
[[1, 2, 3], -3, 0],

[[], 0, 0],
[[], -1, -1],
[[], 2, 2],
[[], -2, -2],
[[1], 1, 1],
[[1], -2, -1],
[[1, 2], 2, 2],
[[1, 2], -3, -1],
[[1, 2, 3], 3, 3],
[[1, 2, 3], -4, -1],
[[1, 2, 3], 4, 4],
[[1, 2, 3], -5, -2],
[[1, 2, 3], 100, 100],
[[1, 2, 3], -101, -98],
];
}

public function dataProviderForNormalizeIndexError(): array
{
return [
[[], 0],
[[], -1],
[[], 2],
[[], -2],
[[1], 1],
[[1], -2],
[[1, 2], 2],
[[1, 2], -3],
[[1, 2, 3], 3],
[[1, 2, 3], -4],
[[1, 2, 3], 4],
[[1, 2, 3], -5],
[[1, 2, 3], 100],
[[1, 2, 3], -101],
];
}
}

0 comments on commit b49a921

Please sign in to comment.