FormatInformationTest.php 2.9 KB

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