| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- /**
- * Class MaskPatternTest
- *
- * @created 21.11.2021
- * @author ZXing Authors
- * @author smiley <smiley@chillerlan.net>
- * @copyright 2021 smiley
- * @license Apache-2.0
- *
- * @codingStandardsIgnoreFile Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast
- */
- declare(strict_types=1);
- namespace chillerlan\QRCodeTest\Common;
- use chillerlan\QRCode\QRCodeException;
- use chillerlan\QRCode\Common\MaskPattern;
- use PHPUnit\Framework\Attributes\{DataProvider, Test};
- use PHPUnit\Framework\TestCase;
- use Closure;
- /**
- * @see https://github.com/zxing/zxing/blob/f4f3c2971dc794346d8b6e14752200008cb90716/core/src/test/java/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java
- */
- final class MaskPatternTest extends TestCase{
- // See mask patterns on the page 43 of JISX0510:2004.
- public static function maskPatternProvider():array{
- return [
- 'PATTERN_000' => [MaskPattern::PATTERN_000, [
- [1, 0, 1, 0, 1, 0],
- [0, 1, 0, 1, 0, 1],
- [1, 0, 1, 0, 1, 0],
- [0, 1, 0, 1, 0, 1],
- [1, 0, 1, 0, 1, 0],
- [0, 1, 0, 1, 0, 1],
- ]],
- 'PATTERN_001' => [MaskPattern::PATTERN_001, [
- [1, 1, 1, 1, 1, 1],
- [0, 0, 0, 0, 0, 0],
- [1, 1, 1, 1, 1, 1],
- [0, 0, 0, 0, 0, 0],
- [1, 1, 1, 1, 1, 1],
- [0, 0, 0, 0, 0, 0],
- ]],
- 'PATTERN_010' => [MaskPattern::PATTERN_010, [
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 1, 0, 0],
- ]],
- 'PATTERN_011' => [MaskPattern::PATTERN_011, [
- [1, 0, 0, 1, 0, 0],
- [0, 0, 1, 0, 0, 1],
- [0, 1, 0, 0, 1, 0],
- [1, 0, 0, 1, 0, 0],
- [0, 0, 1, 0, 0, 1],
- [0, 1, 0, 0, 1, 0],
- ]],
- 'PATTERN_100' => [MaskPattern::PATTERN_100, [
- [1, 1, 1, 0, 0, 0],
- [1, 1, 1, 0, 0, 0],
- [0, 0, 0, 1, 1, 1],
- [0, 0, 0, 1, 1, 1],
- [1, 1, 1, 0, 0, 0],
- [1, 1, 1, 0, 0, 0],
- ]],
- 'PATTERN_101' => [MaskPattern::PATTERN_101, [
- [1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 0, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 1, 0, 1, 0],
- [1, 0, 0, 1, 0, 0],
- [1, 0, 0, 0, 0, 0],
- ]],
- 'PATTERN_110' => [MaskPattern::PATTERN_110, [
- [1, 1, 1, 1, 1, 1],
- [1, 1, 1, 0, 0, 0],
- [1, 1, 0, 1, 1, 0],
- [1, 0, 1, 0, 1, 0],
- [1, 0, 1, 1, 0, 1],
- [1, 0, 0, 0, 1, 1],
- ]],
- 'PATTERN_111' => [MaskPattern::PATTERN_111, [
- [1, 0, 1, 0, 1, 0],
- [0, 0, 0, 1, 1, 1],
- [1, 0, 0, 0, 1, 1],
- [0, 1, 0, 1, 0, 1],
- [1, 1, 1, 0, 0, 0],
- [0, 1, 1, 1, 0, 0],
- ]],
- ];
- }
- /**
- * Tests if the mask function generates the correct pattern
- *
- * @param int[][] $expected
- */
- #[Test]
- #[DataProvider('maskPatternProvider')]
- public function mask(int $pattern, array $expected):void{
- $maskPattern = new MaskPattern($pattern);
- $this::assertTrue($this->assertMask($maskPattern->getMask(), $expected));
- }
- /**
- * @param int[][] $expected
- */
- private function assertMask(Closure $mask, array $expected):bool{
- for($x = 0; $x < 6; $x++){
- for($y = 0; $y < 6; $y++){
- if($mask($x, $y) !== ($expected[$y][$x] === 1)){
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Tests if an exception is thrown on an incorrect mask pattern
- */
- #[Test]
- public function invalidMaskPatternException():void{
- $this->expectException(QRCodeException::class);
- $this->expectExceptionMessage('invalid mask pattern');
- new MaskPattern(42);
- }
- #[Test]
- public function penaltyRule1():void{
- // horizontal
- $this::assertSame(0, MaskPattern::testRule1([[false, false, false, false]], 1, 4));
- $this::assertSame(3, MaskPattern::testRule1([[false, false, false, false, false, true]], 1, 6));
- $this::assertSame(4, MaskPattern::testRule1([[false, false, false, false, false, false]], 1, 6));
- // vertical
- $this::assertSame(0, MaskPattern::testRule1([[false], [false], [false], [false]], 4, 1));
- $this::assertSame(3, MaskPattern::testRule1([[false], [false], [false], [false], [false], [true]], 6, 1));
- $this::assertSame(4, MaskPattern::testRule1([[false], [false], [false], [false], [false], [false]], 6, 1));
- }
- #[Test]
- public function penaltyRule2():void{
- $this::assertSame(0, MaskPattern::testRule2([[false]], 1, 1));
- $this::assertSame(0, MaskPattern::testRule2([[false, false], [false, true]], 2, 2));
- $this::assertSame(3, MaskPattern::testRule2([[false, false], [false, false]], 2, 2));
- $this::assertSame(12, MaskPattern::testRule2([[false, false, false], [false, false, false], [false, false, false]], 3, 3)); // phpcs:ignore
- }
- #[Test]
- public function penaltyRule3():void{
- // horizontal
- $this::assertSame(40, MaskPattern::testRule3([[false, false, false, false, true, false, true, true, true, false, true]], 1, 11)); // phpcs:ignore
- $this::assertSame(40, MaskPattern::testRule3([[true, false, true, true, true, false, true, false, false, false, false]], 1, 11)); // phpcs:ignore
- $this::assertSame(0, MaskPattern::testRule3([[true, false, true, true, true, false, true]], 1, 7));
- // vertical
- $this::assertSame(40, MaskPattern::testRule3([[false], [false], [false], [false], [true], [false], [true], [true], [true], [false], [true]], 11, 1)); // phpcs:ignore
- $this::assertSame(40, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true], [false], [false], [false], [false]], 11, 1)); // phpcs:ignore
- $this::assertSame(0, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true]], 7, 1));
- }
- #[Test]
- public function penaltyRule4():void{
- $this::assertSame(100, MaskPattern::testRule4([[false]], 1, 1));
- $this::assertSame(0, MaskPattern::testRule4([[false, true]], 1, 2));
- $this::assertSame(30, MaskPattern::testRule4([[false, true, true, true, true, false]], 1, 6));
- }
- }
|