|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Smoren\ArrayView\Tests\Unit\Utils; |
| 6 | + |
| 7 | +use Smoren\ArrayView\Exceptions\IndexError; |
| 8 | +use Smoren\ArrayView\Util; |
| 9 | + |
| 10 | +class NormalizeIndexTest extends \Codeception\Test\Unit |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @dataProvider dataProviderForNormalizeIndexSuccess |
| 14 | + */ |
| 15 | + public function testNormalizeIndexSuccess(array $source, int $index, int $expected) |
| 16 | + { |
| 17 | + $normalizedIndex = Util::normalizeIndex($index, \count($source)); |
| 18 | + $this->assertSame($expected, $source[$normalizedIndex]); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @dataProvider dataProviderForNormalizeIndexNotThrow |
| 23 | + */ |
| 24 | + public function testNormalizeIndexNotThrow(array $source, int $index, int $expected) |
| 25 | + { |
| 26 | + $normalizedIndex = Util::normalizeIndex($index, \count($source), false); |
| 27 | + $this->assertSame($expected, $normalizedIndex); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider dataProviderForNormalizeIndexError |
| 32 | + */ |
| 33 | + public function testNormalizeIndexError(array $source, int $index) |
| 34 | + { |
| 35 | + $this->expectException(IndexError::class); |
| 36 | + $this->expectExceptionMessage("Index {$index} is out of range."); |
| 37 | + Util::normalizeIndex($index, \count($source)); |
| 38 | + } |
| 39 | + |
| 40 | + public function dataProviderForNormalizeIndexSuccess(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + [[1], 0, 1], |
| 44 | + [[1], -1, 1], |
| 45 | + [[1, 2], 0, 1], |
| 46 | + [[1, 2], -1, 2], |
| 47 | + [[1, 2], 1, 2], |
| 48 | + [[1, 2], -2, 1], |
| 49 | + [[1, 2, 3], 0, 1], |
| 50 | + [[1, 2, 3], -1, 3], |
| 51 | + [[1, 2, 3], 1, 2], |
| 52 | + [[1, 2, 3], -2, 2], |
| 53 | + [[1, 2, 3], 2, 3], |
| 54 | + [[1, 2, 3], -3, 1], |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + public function dataProviderForNormalizeIndexNotThrow(): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + [[1], 0, 0], |
| 62 | + [[1], -1, 0], |
| 63 | + [[1, 2], 0, 0], |
| 64 | + [[1, 2], -1, 1], |
| 65 | + [[1, 2], 1, 1], |
| 66 | + [[1, 2], -2, 0], |
| 67 | + [[1, 2, 3], 0, 0], |
| 68 | + [[1, 2, 3], -1, 2], |
| 69 | + [[1, 2, 3], 1, 1], |
| 70 | + [[1, 2, 3], -2, 1], |
| 71 | + [[1, 2, 3], 2, 2], |
| 72 | + [[1, 2, 3], -3, 0], |
| 73 | + |
| 74 | + [[], 0, 0], |
| 75 | + [[], -1, -1], |
| 76 | + [[], 2, 2], |
| 77 | + [[], -2, -2], |
| 78 | + [[1], 1, 1], |
| 79 | + [[1], -2, -1], |
| 80 | + [[1, 2], 2, 2], |
| 81 | + [[1, 2], -3, -1], |
| 82 | + [[1, 2, 3], 3, 3], |
| 83 | + [[1, 2, 3], -4, -1], |
| 84 | + [[1, 2, 3], 4, 4], |
| 85 | + [[1, 2, 3], -5, -2], |
| 86 | + [[1, 2, 3], 100, 100], |
| 87 | + [[1, 2, 3], -101, -98], |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + public function dataProviderForNormalizeIndexError(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + [[], 0], |
| 95 | + [[], -1], |
| 96 | + [[], 2], |
| 97 | + [[], -2], |
| 98 | + [[1], 1], |
| 99 | + [[1], -2], |
| 100 | + [[1, 2], 2], |
| 101 | + [[1, 2], -3], |
| 102 | + [[1, 2, 3], 3], |
| 103 | + [[1, 2, 3], -4], |
| 104 | + [[1, 2, 3], 4], |
| 105 | + [[1, 2, 3], -5], |
| 106 | + [[1, 2, 3], 100], |
| 107 | + [[1, 2, 3], -101], |
| 108 | + ]; |
| 109 | + } |
| 110 | +} |
0 commit comments