QRDecodedBitStreamParserTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2017 Huy Cuong Nguyen
  3. * Copyright 2008 ZXing authors
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "BitArray.h"
  7. #include "ByteArray.h"
  8. #include "DecoderResult.h"
  9. #include "qrcode/QRErrorCorrectionLevel.h"
  10. #include "qrcode/QRVersion.h"
  11. #include "gtest/gtest.h"
  12. namespace ZXing {
  13. namespace QRCode {
  14. DecoderResult DecodeBitStream(ByteArray&& bytes, const Version& version, ErrorCorrectionLevel ecLevel);
  15. }
  16. }
  17. using namespace ZXing;
  18. using namespace ZXing::QRCode;
  19. TEST(QRDecodedBitStreamParserTest, SimpleByteMode)
  20. {
  21. BitArray ba;
  22. ba.appendBits(0x04, 4); // Byte mode
  23. ba.appendBits(0x03, 8); // 3 bytes
  24. ba.appendBits(0xF1, 8);
  25. ba.appendBits(0xF2, 8);
  26. ba.appendBits(0xF3, 8);
  27. auto result = DecodeBitStream(ba.toBytes(), *Version::Model2(1), ErrorCorrectionLevel::Medium).text();
  28. EXPECT_EQ(L"\xF1\xF2\xF3", result);
  29. }
  30. TEST(QRDecodedBitStreamParserTest, SimpleSJIS)
  31. {
  32. BitArray ba;
  33. ba.appendBits(0x04, 4); // Byte mode
  34. ba.appendBits(0x04, 8); // 4 bytes
  35. ba.appendBits(0xA1, 8);
  36. ba.appendBits(0xA2, 8);
  37. ba.appendBits(0xA3, 8);
  38. ba.appendBits(0xD0, 8);
  39. auto result = DecodeBitStream(ba.toBytes(), *Version::Model2(1), ErrorCorrectionLevel::Medium).text();
  40. EXPECT_EQ(L"\uff61\uff62\uff63\uff90", result);
  41. }
  42. TEST(QRDecodedBitStreamParserTest, ECI)
  43. {
  44. BitArray ba;
  45. ba.appendBits(0x07, 4); // ECI mode
  46. ba.appendBits(0x02, 8); // ECI 2 = CP437 encoding
  47. ba.appendBits(0x04, 4); // Byte mode
  48. ba.appendBits(0x03, 8); // 3 bytes
  49. ba.appendBits(0xA1, 8);
  50. ba.appendBits(0xA2, 8);
  51. ba.appendBits(0xA3, 8);
  52. auto result = DecodeBitStream(ba.toBytes(), *Version::Model2(1), ErrorCorrectionLevel::Medium).text();
  53. EXPECT_EQ(L"\xED\xF3\xFA", result);
  54. }
  55. TEST(QRDecodedBitStreamParserTest, Hanzi)
  56. {
  57. BitArray ba;
  58. ba.appendBits(0x0D, 4); // Hanzi mode
  59. ba.appendBits(0x01, 4); // Subset 1 = GB2312 encoding
  60. ba.appendBits(0x01, 8); // 1 characters
  61. ba.appendBits(0x03C1, 13);
  62. auto result = DecodeBitStream(ba.toBytes(), *Version::Model2(1), ErrorCorrectionLevel::Medium).text();
  63. EXPECT_EQ(L"\u963f", result);
  64. }
  65. TEST(QRDecodedBitStreamParserTest, HanziLevel1)
  66. {
  67. BitArray ba;
  68. ba.appendBits(0x0D, 4); // Hanzi mode
  69. ba.appendBits(0x01, 4); // Subset 1 = GB2312 encoding
  70. ba.appendBits(0x01, 8); // 1 characters
  71. // A5A2 (U+30A2) => A5A2 - A1A1 = 401, 4*60 + 01 = 0181
  72. ba.appendBits(0x0181, 13);
  73. auto result = DecodeBitStream(ba.toBytes(), *Version::Model2(1), ErrorCorrectionLevel::Medium).text();
  74. EXPECT_EQ(L"\u30a2", result);
  75. }
  76. TEST(QRDecodedBitStreamParserTest, SymbologyIdentifier)
  77. {
  78. const Version& version = *Version::Model2(1);
  79. const ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel::Medium;
  80. DecoderResult result;
  81. // Plain "ANUM(1) A"
  82. result = DecodeBitStream({0x20, 0x09, 0x40}, version, ecLevel);
  83. EXPECT_EQ(result.symbologyIdentifier(), "]Q1");
  84. EXPECT_EQ(result.text(), L"A");
  85. // GS1 "FNC1(1st) NUM(4) 2001"
  86. result = DecodeBitStream({0x51, 0x01, 0x0C, 0x81, 0x00}, version, ecLevel);
  87. EXPECT_EQ(result.symbologyIdentifier(), "]Q3");
  88. EXPECT_EQ(result.text(), L"2001"); // "(20)01"
  89. // GS1 "NUM(4) 2001 FNC1(1st) 301" - FNC1(1st) can occur anywhere (this actually violates the specification)
  90. result = DecodeBitStream({0x10, 0x10, 0xC8, 0x15, 0x10, 0x0D, 0x2D, 0x00}, version, ecLevel);
  91. EXPECT_EQ(result.symbologyIdentifier(), "]Q3");
  92. EXPECT_EQ(result.text(), L"2001301"); // "(20)01(30)1"
  93. // AIM "FNC1(2nd) 99 (0x63) ANUM(1) A"
  94. result = DecodeBitStream({0x96, 0x32, 0x00, 0x94, 0x00}, version, ecLevel);
  95. EXPECT_EQ(result.symbologyIdentifier(), "]Q5");
  96. EXPECT_EQ(result.text(), L"99A");
  97. // AIM "BYTE(1) A FNC1(2nd) 99 (0x63) BYTE(1) B" - FNC1(2nd) can occur anywhere
  98. // Disabled this test, since this violates the specification and the code does support it anymore
  99. // result = DecodeBitStream({0x40, 0x14, 0x19, 0x63, 0x40, 0x14, 0x20, 0x00}, version, ecLevel, "");
  100. // EXPECT_EQ(result.symbologyIdentifier(), "]Q5");
  101. // EXPECT_EQ(result.text(), L"99AB"); // Application Indicator prefixed to data
  102. // AIM "FNC1(2nd) A (100 + 61 = 0xA5) ANUM(1) B"
  103. result = DecodeBitStream({0x9A, 0x52, 0x00, 0x96, 0x00}, version, ecLevel);
  104. EXPECT_EQ(result.symbologyIdentifier(), "]Q5");
  105. EXPECT_EQ(result.text(), L"AB");
  106. // AIM "FNC1(2nd) a (100 + 97 = 0xC5) ANUM(1) B"
  107. result = DecodeBitStream({0x9C, 0x52, 0x00, 0x96, 0x00}, version, ecLevel);
  108. EXPECT_EQ(result.symbologyIdentifier(), "]Q5");
  109. EXPECT_EQ(result.text(), L"aB");
  110. // Bad AIM Application Indicator "FNC1(2nd) @ (0xA4) ANUM(1) B"
  111. result = DecodeBitStream({0x9A, 0x42, 0x00, 0x96, 0x00}, version, ecLevel);
  112. EXPECT_FALSE(result.isValid());
  113. }
  114. TEST(QRDecodedBitStreamParserTest, GS1PercentGS)
  115. {
  116. const Version& version = *Version::Model2(1);
  117. const ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel::Quality;
  118. DecoderResult result;
  119. // GS1 "FNC1(1st) A(11) 9112%%%2012 (9112%<FNC1>2012)"
  120. result = DecodeBitStream({0x52, 0x05, 0x99, 0x60, 0x5F, 0xB5, 0x35, 0x80, 0x01, 0x08, 0x00, 0xEC, 0x11}, version, ecLevel);
  121. EXPECT_EQ(result.content().text(TextMode::Plain), "9112%\x1D" "2012");
  122. EXPECT_EQ(result.content().text(TextMode::HRI), "(91)12%(20)12");
  123. }