BitArrayTest.php
8 months ago
BitMatrixTest.php
8 months ago
BitUtilsTest.php
8 months ago
ErrorCorrectionLevelTest.php
8 months ago
FormatInformationTest.php
8 months ago
ModeTest.php
8 months ago
ReedSolomonCodecTest.php
8 months ago
VersionTest.php
8 months ago
ReedSolomonCodecTest.php
97 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCodeTest\Common; |
| 5 | |
| 6 | use BaconQrCode\Common\ReedSolomonCodec; |
| 7 | use PHPUnit\Framework\TestCase; |
| 8 | use SplFixedArray; |
| 9 | |
| 10 | class ReedSolomonTest extends TestCase |
| 11 | { |
| 12 | public function tabs() : array |
| 13 | { |
| 14 | return [ |
| 15 | [2, 0x7, 1, 1, 1], |
| 16 | [3, 0xb, 1, 1, 2], |
| 17 | [4, 0x13, 1, 1, 4], |
| 18 | [5, 0x25, 1, 1, 6], |
| 19 | [6, 0x43, 1, 1, 8], |
| 20 | [7, 0x89, 1, 1, 10], |
| 21 | [8, 0x11d, 1, 1, 32], |
| 22 | ]; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @dataProvider tabs |
| 27 | */ |
| 28 | public function testCodec(int $symbolSize, int $generatorPoly, int $firstRoot, int $primitive, int $numRoots) : void |
| 29 | { |
| 30 | mt_srand(0xdeadbeef, MT_RAND_PHP); |
| 31 | |
| 32 | $blockSize = (1 << $symbolSize) - 1; |
| 33 | $dataSize = $blockSize - $numRoots; |
| 34 | $codec = new ReedSolomonCodec($symbolSize, $generatorPoly, $firstRoot, $primitive, $numRoots, 0); |
| 35 | |
| 36 | for ($errors = 0; $errors <= $numRoots / 2; ++$errors) { |
| 37 | // Load block with random data and encode |
| 38 | $block = SplFixedArray::fromArray(array_fill(0, $blockSize, 0), false); |
| 39 | |
| 40 | for ($i = 0; $i < $dataSize; ++$i) { |
| 41 | $block[$i] = mt_rand(0, $blockSize); |
| 42 | } |
| 43 | |
| 44 | // Make temporary copy |
| 45 | $tBlock = clone $block; |
| 46 | $parity = SplFixedArray::fromArray(array_fill(0, $numRoots, 0), false); |
| 47 | $errorLocations = SplFixedArray::fromArray(array_fill(0, $blockSize, 0), false); |
| 48 | $erasures = []; |
| 49 | |
| 50 | // Create parity |
| 51 | $codec->encode($block, $parity); |
| 52 | |
| 53 | // Copy parity into test blocks |
| 54 | for ($i = 0; $i < $numRoots; ++$i) { |
| 55 | $block[$i + $dataSize] = $parity[$i]; |
| 56 | $tBlock[$i + $dataSize] = $parity[$i]; |
| 57 | } |
| 58 | |
| 59 | // Seed with errors |
| 60 | for ($i = 0; $i < $errors; ++$i) { |
| 61 | $errorValue = mt_rand(1, $blockSize); |
| 62 | |
| 63 | do { |
| 64 | $errorLocation = mt_rand(0, $blockSize); |
| 65 | } while (0 !== $errorLocations[$errorLocation]); |
| 66 | |
| 67 | $errorLocations[$errorLocation] = 1; |
| 68 | |
| 69 | if (mt_rand(0, 1)) { |
| 70 | $erasures[] = $errorLocation; |
| 71 | } |
| 72 | |
| 73 | $tBlock[$errorLocation] ^= $errorValue; |
| 74 | } |
| 75 | |
| 76 | $erasures = SplFixedArray::fromArray($erasures, false); |
| 77 | |
| 78 | // Decode the errored block |
| 79 | $foundErrors = $codec->decode($tBlock, $erasures); |
| 80 | |
| 81 | if ($errors > 0 && null === $foundErrors) { |
| 82 | $this->assertSame($block, $tBlock, 'Decoder failed to correct errors'); |
| 83 | } |
| 84 | |
| 85 | $this->assertSame($errors, $foundErrors, 'Found errors do not equal expected errors'); |
| 86 | |
| 87 | for ($i = 0; $i < $foundErrors; ++$i) { |
| 88 | if (0 === $errorLocations[$erasures[$i]]) { |
| 89 | $this->fail(sprintf('Decoder indicates error in location %d without error', $erasures[$i])); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | $this->assertEquals($block, $tBlock, 'Decoder did not correct errors'); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 |