QRFormatInformationTest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2017 Huy Cuong Nguyen
  3. * Copyright 2007 ZXing authors
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "qrcode/QRFormatInformation.h"
  7. #include "gtest/gtest.h"
  8. using namespace ZXing;
  9. using namespace ZXing::QRCode;
  10. static const int MASKED_TEST_FORMAT_INFO = 0x2BED;
  11. static const int MASKED_TEST_FORMAT_INFO2 = ((0x2BED << 1) & 0b1111111000000000) | 0b100000000 | (0x2BED & 0b11111111); // insert the 'Dark Module'
  12. static const int UNMASKED_TEST_FORMAT_INFO = MASKED_TEST_FORMAT_INFO ^ 0x5412;
  13. static const int MICRO_MASKED_TEST_FORMAT_INFO = 0x3BBA;
  14. static const int RMQR_MASKED_TEST_FORMAT_INFO = 0x20137;
  15. static const int RMQR_MASKED_TEST_FORMAT_INFO_SUB = 0x1F1FE;
  16. static void DoFormatInformationTest(const int formatInfo, const uint8_t expectedMask, const ErrorCorrectionLevel& expectedECL)
  17. {
  18. FormatInformation parsedFormat = FormatInformation::DecodeMQR(formatInfo);
  19. EXPECT_TRUE(parsedFormat.isValid());
  20. EXPECT_EQ(expectedMask, parsedFormat.dataMask);
  21. EXPECT_EQ(expectedECL, parsedFormat.ecLevel);
  22. }
  23. // Helper for rMQR to unset `numBits` number of bits
  24. static uint32_t RMQRUnsetBits(uint32_t formatInfoBits, int numBits)
  25. {
  26. for (int i = 0; i < 18 && numBits; i++) {
  27. if (formatInfoBits & (1 << i)) {
  28. formatInfoBits ^= 1 << i;
  29. numBits--;
  30. }
  31. }
  32. return formatInfoBits;
  33. }
  34. TEST(QRFormatInformationTest, Decode)
  35. {
  36. // Normal case
  37. FormatInformation expected = FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO2);
  38. EXPECT_TRUE(expected.isValid());
  39. EXPECT_EQ(0x07, expected.dataMask);
  40. EXPECT_EQ(ErrorCorrectionLevel::Quality, expected.ecLevel);
  41. // where the code forgot the mask!
  42. EXPECT_EQ(expected, FormatInformation::DecodeQR(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO2));
  43. }
  44. TEST(QRFormatInformationTest, DecodeWithBitDifference)
  45. {
  46. FormatInformation expected = FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO2);
  47. // 1,2,3,4 bits difference
  48. EXPECT_EQ(expected, FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO2 ^ 0x01));
  49. EXPECT_EQ(expected, FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO2 ^ 0x03));
  50. EXPECT_EQ(expected, FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO2 ^ 0x07));
  51. auto unexpected = FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO2 ^ 0x0F);
  52. EXPECT_FALSE(expected == unexpected);
  53. EXPECT_FALSE(unexpected.isValid() && unexpected.type() == Type::Model2);
  54. }
  55. TEST(QRFormatInformationTest, DecodeWithMisread)
  56. {
  57. FormatInformation expected = FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO2);
  58. EXPECT_EQ(expected, FormatInformation::DecodeQR(MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO2 ^ 0x0F));
  59. }
  60. TEST(QRFormatInformationTest, DecodeMicro)
  61. {
  62. // Normal cases.
  63. DoFormatInformationTest(0x4445, 0x0, ErrorCorrectionLevel::Low);
  64. DoFormatInformationTest(0x4172, 0x1, ErrorCorrectionLevel::Low);
  65. DoFormatInformationTest(0x5fc0, 0x2, ErrorCorrectionLevel::Low);
  66. DoFormatInformationTest(0x5af7, 0x3, ErrorCorrectionLevel::Low);
  67. DoFormatInformationTest(0x6793, 0x0, ErrorCorrectionLevel::Medium);
  68. DoFormatInformationTest(0x62a4, 0x1, ErrorCorrectionLevel::Medium);
  69. DoFormatInformationTest(0x3e8d, 0x2, ErrorCorrectionLevel::Quality);
  70. DoFormatInformationTest(MICRO_MASKED_TEST_FORMAT_INFO, 0x3, ErrorCorrectionLevel::Quality);
  71. // where the code forgot the mask!
  72. // static const int MICRO_UNMASKED_TEST_FORMAT_INFO = MICRO_MASKED_TEST_FORMAT_INFO ^ 0x4445;
  73. // DoFormatInformationTest(MICRO_UNMASKED_TEST_FORMAT_INFO, 0x3, ErrorCorrectionLevel::Quality);
  74. }
  75. // This doesn't work as expected because the implementation of the decode tries with
  76. // and without the mask (0x4445). This effectively adds a tolerance of 5 bits to the Hamming
  77. // distance calculation.
  78. TEST(QRFormatInformationTest, DecodeMicroWithBitDifference)
  79. {
  80. FormatInformation expected = FormatInformation::DecodeMQR(MICRO_MASKED_TEST_FORMAT_INFO);
  81. // 1,2,3 bits difference
  82. EXPECT_EQ(expected, FormatInformation::DecodeMQR(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x01));
  83. EXPECT_EQ(expected, FormatInformation::DecodeMQR(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x03));
  84. EXPECT_EQ(expected, FormatInformation::DecodeMQR(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x07));
  85. // Bigger bit differences can return valid FormatInformation objects but the data mask and error
  86. // correction levels do not match.
  87. // EXPECT_TRUE(FormatInformation::DecodeFormatInformation(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x3f).isValid());
  88. // EXPECT_NE(expected.dataMask(), FormatInformation::DecodeFormatInformation(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x3f).dataMask());
  89. // EXPECT_NE(expected.errorCorrectionLevel(),
  90. // FormatInformation::DecodeFormatInformation(MICRO_MASKED_TEST_FORMAT_INFO ^ 0x3f).errorCorrectionLevel());
  91. }
  92. TEST(QRFormatInformationTest, DecodeRMQR)
  93. {
  94. // Normal case
  95. FormatInformation expected = FormatInformation::DecodeRMQR(RMQR_MASKED_TEST_FORMAT_INFO, RMQR_MASKED_TEST_FORMAT_INFO_SUB);
  96. EXPECT_TRUE(expected.isValid());
  97. EXPECT_EQ(4, expected.dataMask);
  98. EXPECT_EQ(ErrorCorrectionLevel::High, expected.ecLevel);
  99. EXPECT_EQ(FORMAT_INFO_MASK_RMQR, expected.mask);
  100. // Not catered for: where the code forgot the mask!
  101. }
  102. TEST(QRFormatInformationTest, DecodeRMQRWithBitDifference)
  103. {
  104. FormatInformation expected = FormatInformation::DecodeRMQR(RMQR_MASKED_TEST_FORMAT_INFO, RMQR_MASKED_TEST_FORMAT_INFO_SUB);
  105. EXPECT_EQ(expected.ecLevel, ErrorCorrectionLevel::High);
  106. // 1,2,3,4,5 bits difference
  107. EXPECT_EQ(expected, FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 1), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 1)));
  108. EXPECT_EQ(expected, FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 2), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 2)));
  109. EXPECT_EQ(expected, FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 3), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 3)));
  110. EXPECT_EQ(expected, FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 4), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 4)));
  111. auto unexpected = FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 5), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 5));
  112. EXPECT_FALSE(expected == unexpected);
  113. EXPECT_FALSE(unexpected.isValid());
  114. EXPECT_TRUE(unexpected.type() == Type::rMQR); // Note `mask` (used to determine type) set regardless
  115. }
  116. TEST(QRFormatInformationTest, DecodeRMQRWithMisread)
  117. {
  118. FormatInformation expected = FormatInformation::DecodeRMQR(RMQR_MASKED_TEST_FORMAT_INFO, RMQR_MASKED_TEST_FORMAT_INFO_SUB);
  119. {
  120. auto actual = FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 2), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 4));
  121. EXPECT_EQ(expected, actual);
  122. EXPECT_EQ(actual.mask, FORMAT_INFO_MASK_RMQR);
  123. }
  124. {
  125. auto actual = FormatInformation::DecodeRMQR(RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO, 5), RMQRUnsetBits(RMQR_MASKED_TEST_FORMAT_INFO_SUB, 4));
  126. EXPECT_EQ(expected, actual);
  127. EXPECT_EQ(actual.mask, FORMAT_INFO_MASK_RMQR_SUB);
  128. }
  129. }