FormatInformationTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * BaconQrCode
  4. *
  5. * @link http://github.com/Bacon/BaconQrCode For the canonical source repository
  6. * @copyright 2013 Ben 'DASPRiD' Scholzen
  7. * @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
  8. */
  9. namespace BaconQrCode\Common;
  10. use PHPUnit_Framework_TestCase as TestCase;
  11. class FormatInformationTest extends TestCase
  12. {
  13. protected $maskedTestFormatInfo = 0x2bed;
  14. protected $unmaskedTestFormatInfo;
  15. public function setUp()
  16. {
  17. $this->unmaskedTestFormatInfo = $this->maskedTestFormatInfo ^ 0x5412;
  18. }
  19. public function testBitsDiffering()
  20. {
  21. $this->assertEquals(0, FormatInformation::numBitsDiffering(1, 1));
  22. $this->assertEquals(1, FormatInformation::numBitsDiffering(0, 2));
  23. $this->assertEquals(2, FormatInformation::numBitsDiffering(1, 2));
  24. $this->assertEquals(32, FormatInformation::numBitsDiffering(-1, 0));
  25. }
  26. public function testDecode()
  27. {
  28. $expected = FormatInformation::decodeFormatInformation(
  29. $this->maskedTestFormatInfo,
  30. $this->maskedTestFormatInfo
  31. );
  32. $this->assertNotNull($expected);
  33. $this->assertEquals(7, $expected->getDataMask());
  34. $this->assertEquals(ErrorCorrectionLevel::Q, $expected->getErrorCorrectionLevel()->get());
  35. $this->assertEquals(
  36. $expected,
  37. FormatInformation::decodeFormatInformation(
  38. $this->unmaskedTestFormatInfo,
  39. $this->maskedTestFormatInfo
  40. )
  41. );
  42. }
  43. public function testDecodeWithBitDifference()
  44. {
  45. $expected = FormatInformation::decodeFormatInformation(
  46. $this->maskedTestFormatInfo,
  47. $this->maskedTestFormatInfo
  48. );
  49. $this->assertEquals(
  50. $expected,
  51. FormatInformation::decodeFormatInformation(
  52. $this->maskedTestFormatInfo ^ 0x1,
  53. $this->maskedTestFormatInfo ^ 0x1
  54. )
  55. );
  56. $this->assertEquals(
  57. $expected,
  58. FormatInformation::decodeFormatInformation(
  59. $this->maskedTestFormatInfo ^ 0x3,
  60. $this->maskedTestFormatInfo ^ 0x3
  61. )
  62. );
  63. $this->assertEquals(
  64. $expected,
  65. FormatInformation::decodeFormatInformation(
  66. $this->maskedTestFormatInfo ^ 0x7,
  67. $this->maskedTestFormatInfo ^ 0x7
  68. )
  69. );
  70. $this->assertNull(
  71. FormatInformation::decodeFormatInformation(
  72. $this->maskedTestFormatInfo ^ 0xf,
  73. $this->maskedTestFormatInfo ^ 0xf
  74. )
  75. );
  76. }
  77. public function testDecodeWithMisRead()
  78. {
  79. $expected = FormatInformation::decodeFormatInformation(
  80. $this->maskedTestFormatInfo,
  81. $this->maskedTestFormatInfo
  82. );
  83. $this->assertEquals(
  84. $expected,
  85. FormatInformation::decodeFormatInformation(
  86. $this->maskedTestFormatInfo ^ 0x3,
  87. $this->maskedTestFormatInfo ^ 0xf
  88. )
  89. );
  90. }
  91. }