MaskPatternTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Class MaskPatternTest
  4. *
  5. * @created 21.11.2021
  6. * @author ZXing Authors
  7. * @author smiley <smiley@chillerlan.net>
  8. * @copyright 2021 smiley
  9. * @license Apache-2.0
  10. *
  11. * @codingStandardsIgnoreFile Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast
  12. */
  13. declare(strict_types=1);
  14. namespace chillerlan\QRCodeTest\Common;
  15. use chillerlan\QRCode\QRCodeException;
  16. use chillerlan\QRCode\Common\MaskPattern;
  17. use PHPUnit\Framework\Attributes\{DataProvider, Test};
  18. use PHPUnit\Framework\TestCase;
  19. use Closure;
  20. /**
  21. * @see https://github.com/zxing/zxing/blob/f4f3c2971dc794346d8b6e14752200008cb90716/core/src/test/java/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java
  22. */
  23. final class MaskPatternTest extends TestCase{
  24. // See mask patterns on the page 43 of JISX0510:2004.
  25. public static function maskPatternProvider():array{
  26. return [
  27. 'PATTERN_000' => [MaskPattern::PATTERN_000, [
  28. [1, 0, 1, 0, 1, 0],
  29. [0, 1, 0, 1, 0, 1],
  30. [1, 0, 1, 0, 1, 0],
  31. [0, 1, 0, 1, 0, 1],
  32. [1, 0, 1, 0, 1, 0],
  33. [0, 1, 0, 1, 0, 1],
  34. ]],
  35. 'PATTERN_001' => [MaskPattern::PATTERN_001, [
  36. [1, 1, 1, 1, 1, 1],
  37. [0, 0, 0, 0, 0, 0],
  38. [1, 1, 1, 1, 1, 1],
  39. [0, 0, 0, 0, 0, 0],
  40. [1, 1, 1, 1, 1, 1],
  41. [0, 0, 0, 0, 0, 0],
  42. ]],
  43. 'PATTERN_010' => [MaskPattern::PATTERN_010, [
  44. [1, 0, 0, 1, 0, 0],
  45. [1, 0, 0, 1, 0, 0],
  46. [1, 0, 0, 1, 0, 0],
  47. [1, 0, 0, 1, 0, 0],
  48. [1, 0, 0, 1, 0, 0],
  49. [1, 0, 0, 1, 0, 0],
  50. ]],
  51. 'PATTERN_011' => [MaskPattern::PATTERN_011, [
  52. [1, 0, 0, 1, 0, 0],
  53. [0, 0, 1, 0, 0, 1],
  54. [0, 1, 0, 0, 1, 0],
  55. [1, 0, 0, 1, 0, 0],
  56. [0, 0, 1, 0, 0, 1],
  57. [0, 1, 0, 0, 1, 0],
  58. ]],
  59. 'PATTERN_100' => [MaskPattern::PATTERN_100, [
  60. [1, 1, 1, 0, 0, 0],
  61. [1, 1, 1, 0, 0, 0],
  62. [0, 0, 0, 1, 1, 1],
  63. [0, 0, 0, 1, 1, 1],
  64. [1, 1, 1, 0, 0, 0],
  65. [1, 1, 1, 0, 0, 0],
  66. ]],
  67. 'PATTERN_101' => [MaskPattern::PATTERN_101, [
  68. [1, 1, 1, 1, 1, 1],
  69. [1, 0, 0, 0, 0, 0],
  70. [1, 0, 0, 1, 0, 0],
  71. [1, 0, 1, 0, 1, 0],
  72. [1, 0, 0, 1, 0, 0],
  73. [1, 0, 0, 0, 0, 0],
  74. ]],
  75. 'PATTERN_110' => [MaskPattern::PATTERN_110, [
  76. [1, 1, 1, 1, 1, 1],
  77. [1, 1, 1, 0, 0, 0],
  78. [1, 1, 0, 1, 1, 0],
  79. [1, 0, 1, 0, 1, 0],
  80. [1, 0, 1, 1, 0, 1],
  81. [1, 0, 0, 0, 1, 1],
  82. ]],
  83. 'PATTERN_111' => [MaskPattern::PATTERN_111, [
  84. [1, 0, 1, 0, 1, 0],
  85. [0, 0, 0, 1, 1, 1],
  86. [1, 0, 0, 0, 1, 1],
  87. [0, 1, 0, 1, 0, 1],
  88. [1, 1, 1, 0, 0, 0],
  89. [0, 1, 1, 1, 0, 0],
  90. ]],
  91. ];
  92. }
  93. /**
  94. * Tests if the mask function generates the correct pattern
  95. *
  96. * @param int[][] $expected
  97. */
  98. #[Test]
  99. #[DataProvider('maskPatternProvider')]
  100. public function mask(int $pattern, array $expected):void{
  101. $maskPattern = new MaskPattern($pattern);
  102. $this::assertTrue($this->assertMask($maskPattern->getMask(), $expected));
  103. }
  104. /**
  105. * @param int[][] $expected
  106. */
  107. private function assertMask(Closure $mask, array $expected):bool{
  108. for($x = 0; $x < 6; $x++){
  109. for($y = 0; $y < 6; $y++){
  110. if($mask($x, $y) !== ($expected[$y][$x] === 1)){
  111. return false;
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. /**
  118. * Tests if an exception is thrown on an incorrect mask pattern
  119. */
  120. #[Test]
  121. public function invalidMaskPatternException():void{
  122. $this->expectException(QRCodeException::class);
  123. $this->expectExceptionMessage('invalid mask pattern');
  124. new MaskPattern(42);
  125. }
  126. #[Test]
  127. public function penaltyRule1():void{
  128. // horizontal
  129. $this::assertSame(0, MaskPattern::testRule1([[false, false, false, false]], 1, 4));
  130. $this::assertSame(3, MaskPattern::testRule1([[false, false, false, false, false, true]], 1, 6));
  131. $this::assertSame(4, MaskPattern::testRule1([[false, false, false, false, false, false]], 1, 6));
  132. // vertical
  133. $this::assertSame(0, MaskPattern::testRule1([[false], [false], [false], [false]], 4, 1));
  134. $this::assertSame(3, MaskPattern::testRule1([[false], [false], [false], [false], [false], [true]], 6, 1));
  135. $this::assertSame(4, MaskPattern::testRule1([[false], [false], [false], [false], [false], [false]], 6, 1));
  136. }
  137. #[Test]
  138. public function penaltyRule2():void{
  139. $this::assertSame(0, MaskPattern::testRule2([[false]], 1, 1));
  140. $this::assertSame(0, MaskPattern::testRule2([[false, false], [false, true]], 2, 2));
  141. $this::assertSame(3, MaskPattern::testRule2([[false, false], [false, false]], 2, 2));
  142. $this::assertSame(12, MaskPattern::testRule2([[false, false, false], [false, false, false], [false, false, false]], 3, 3)); // phpcs:ignore
  143. }
  144. #[Test]
  145. public function penaltyRule3():void{
  146. // horizontal
  147. $this::assertSame(40, MaskPattern::testRule3([[false, false, false, false, true, false, true, true, true, false, true]], 1, 11)); // phpcs:ignore
  148. $this::assertSame(40, MaskPattern::testRule3([[true, false, true, true, true, false, true, false, false, false, false]], 1, 11)); // phpcs:ignore
  149. $this::assertSame(0, MaskPattern::testRule3([[true, false, true, true, true, false, true]], 1, 7));
  150. // vertical
  151. $this::assertSame(40, MaskPattern::testRule3([[false], [false], [false], [false], [true], [false], [true], [true], [true], [false], [true]], 11, 1)); // phpcs:ignore
  152. $this::assertSame(40, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true], [false], [false], [false], [false]], 11, 1)); // phpcs:ignore
  153. $this::assertSame(0, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true]], 7, 1));
  154. }
  155. #[Test]
  156. public function penaltyRule4():void{
  157. $this::assertSame(100, MaskPattern::testRule4([[false]], 1, 1));
  158. $this::assertSame(0, MaskPattern::testRule4([[false, true]], 1, 2));
  159. $this::assertSame(30, MaskPattern::testRule4([[false, true, true, true, true, false]], 1, 6));
  160. }
  161. }