ReedSolomonCodecTest.php 3.0 KB

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