From b49a921044b13593b1c64f30a74ebacf9c5eb7c0 Mon Sep 17 00:00:00 2001 From: Smoren Date: Tue, 12 Mar 2024 16:41:34 +0300 Subject: [PATCH] Util::normalizeIndex() tested. --- tests/unit/Structs/SliceTest.php | 2 +- tests/unit/Utils/NormalizeIndexTest.php | 110 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Utils/NormalizeIndexTest.php diff --git a/tests/unit/Structs/SliceTest.php b/tests/unit/Structs/SliceTest.php index 1bdd297..cfe80dd 100644 --- a/tests/unit/Structs/SliceTest.php +++ b/tests/unit/Structs/SliceTest.php @@ -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); diff --git a/tests/unit/Utils/NormalizeIndexTest.php b/tests/unit/Utils/NormalizeIndexTest.php new file mode 100644 index 0000000..11ef333 --- /dev/null +++ b/tests/unit/Utils/NormalizeIndexTest.php @@ -0,0 +1,110 @@ +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], + ]; + } +}