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
FormatInformationTest.php
95 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCodeTest\Common; |
| 5 | |
| 6 | use BaconQrCode\Common\ErrorCorrectionLevel; |
| 7 | use BaconQrCode\Common\FormatInformation; |
| 8 | use PHPUnit\Framework\TestCase; |
| 9 | |
| 10 | class FormatInformationTest extends TestCase |
| 11 | { |
| 12 | private const MASKED_TEST_FORMAT_INFO = 0x2bed; |
| 13 | private const UNMAKSED_TEST_FORMAT_INFO = self::MASKED_TEST_FORMAT_INFO ^ 0x5412; |
| 14 | |
| 15 | public function testBitsDiffering() : void |
| 16 | { |
| 17 | $this->assertSame(0, FormatInformation::numBitsDiffering(1, 1)); |
| 18 | $this->assertSame(1, FormatInformation::numBitsDiffering(0, 2)); |
| 19 | $this->assertSame(2, FormatInformation::numBitsDiffering(1, 2)); |
| 20 | $this->assertEquals(32, FormatInformation::numBitsDiffering(-1, 0)); |
| 21 | } |
| 22 | |
| 23 | public function testDecode() : void |
| 24 | { |
| 25 | $expected = FormatInformation::decodeFormatInformation( |
| 26 | self::MASKED_TEST_FORMAT_INFO, |
| 27 | self::MASKED_TEST_FORMAT_INFO |
| 28 | ); |
| 29 | |
| 30 | $this->assertNotNull($expected); |
| 31 | $this->assertSame(7, $expected->getDataMask()); |
| 32 | $this->assertSame(ErrorCorrectionLevel::Q(), $expected->getErrorCorrectionLevel()); |
| 33 | |
| 34 | $this->assertEquals( |
| 35 | $expected, |
| 36 | FormatInformation::decodeFormatInformation( |
| 37 | self::UNMAKSED_TEST_FORMAT_INFO, |
| 38 | self::MASKED_TEST_FORMAT_INFO |
| 39 | ) |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public function testDecodeWithBitDifference() : void |
| 44 | { |
| 45 | $expected = FormatInformation::decodeFormatInformation( |
| 46 | self::MASKED_TEST_FORMAT_INFO, |
| 47 | self::MASKED_TEST_FORMAT_INFO |
| 48 | ); |
| 49 | |
| 50 | $this->assertEquals( |
| 51 | $expected, |
| 52 | FormatInformation::decodeFormatInformation( |
| 53 | self::MASKED_TEST_FORMAT_INFO ^ 0x1, |
| 54 | self::MASKED_TEST_FORMAT_INFO ^ 0x1 |
| 55 | ) |
| 56 | ); |
| 57 | $this->assertEquals( |
| 58 | $expected, |
| 59 | FormatInformation::decodeFormatInformation( |
| 60 | self::MASKED_TEST_FORMAT_INFO ^ 0x3, |
| 61 | self::MASKED_TEST_FORMAT_INFO ^ 0x3 |
| 62 | ) |
| 63 | ); |
| 64 | $this->assertEquals( |
| 65 | $expected, |
| 66 | FormatInformation::decodeFormatInformation( |
| 67 | self::MASKED_TEST_FORMAT_INFO ^ 0x7, |
| 68 | self::MASKED_TEST_FORMAT_INFO ^ 0x7 |
| 69 | ) |
| 70 | ); |
| 71 | $this->assertNull( |
| 72 | FormatInformation::decodeFormatInformation( |
| 73 | self::MASKED_TEST_FORMAT_INFO ^ 0xf, |
| 74 | self::MASKED_TEST_FORMAT_INFO ^ 0xf |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | public function testDecodeWithMisRead() : void |
| 80 | { |
| 81 | $expected = FormatInformation::decodeFormatInformation( |
| 82 | self::MASKED_TEST_FORMAT_INFO, |
| 83 | self::MASKED_TEST_FORMAT_INFO |
| 84 | ); |
| 85 | |
| 86 | $this->assertEquals( |
| 87 | $expected, |
| 88 | FormatInformation::decodeFormatInformation( |
| 89 | self::MASKED_TEST_FORMAT_INFO ^ 0x3, |
| 90 | self::MASKED_TEST_FORMAT_INFO ^ 0xf |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 |