ODCode128WriterTest.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Huy Cuong Nguyen
  3. * Copyright 2014 ZXing authors
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "oned/ODCode128Writer.h"
  7. #include "BitMatrixIO.h"
  8. #include "ReaderOptions.h"
  9. #include "Barcode.h"
  10. #include "oned/ODCode128Reader.h"
  11. #include "gtest/gtest.h"
  12. using namespace ZXing;
  13. using namespace ZXing::OneD;
  14. static const std::string FNC1 = "11110101110";
  15. static const std::string FNC2 = "11110101000";
  16. static const std::string FNC3 = "10111100010";
  17. static const std::string FNC4A = "11101011110";
  18. static const std::string FNC4B = "10111101110";
  19. static const std::string START_CODE_A = "11010000100";
  20. static const std::string START_CODE_B = "11010010000";
  21. static const std::string START_CODE_C = "11010011100";
  22. static const std::string SWITCH_CODE_A = "11101011110";
  23. static const std::string SWITCH_CODE_B = "10111101110";
  24. static const std::string QUIET_SPACE = "00000";
  25. static const std::string STOP = "1100011101011";
  26. static const std::string LF = "10000110010";
  27. static std::string LineMatrixToString(const BitMatrix& matrix)
  28. {
  29. auto result = ToString(matrix, '1', '0', false);
  30. return result.substr(0, result.size() - 1);
  31. }
  32. static ZXing::Barcode Decode(const BitMatrix &matrix)
  33. {
  34. ReaderOptions opts;
  35. return DecodeSingleRow(Code128Reader(opts), matrix.row(0));
  36. }
  37. TEST(ODCode128Writer, EncodeWithFunc1)
  38. {
  39. auto toEncode = L"\u00f1123";
  40. // "12" "3" check digit 92
  41. auto expected = QUIET_SPACE + START_CODE_C + FNC1 + "10110011100" + SWITCH_CODE_B + "11001011100" + "10101111000" + STOP + QUIET_SPACE;
  42. auto actual = LineMatrixToString(Code128Writer().encode(toEncode, 0, 0));
  43. EXPECT_EQ(actual, expected);
  44. }
  45. TEST(ODCode128Writer, EncodeWithFunc2)
  46. {
  47. auto toEncode = L"\u00f2123";
  48. // "1" "2" "3" check digit 56
  49. auto expected = QUIET_SPACE + START_CODE_B + FNC2 + "10011100110" + "11001110010" + "11001011100" + "11100010110" + STOP + QUIET_SPACE;
  50. auto actual = LineMatrixToString(Code128Writer().encode(toEncode, 0, 0));
  51. EXPECT_EQ(actual, expected);
  52. }
  53. TEST(ODCode128Writer, EncodeWithFunc3)
  54. {
  55. auto toEncode = L"\u00f3123";
  56. // "1" "2" "3" check digit 51
  57. auto expected = QUIET_SPACE + START_CODE_B + FNC3 + "10011100110" + "11001110010" + "11001011100" + "11101000110" + STOP + QUIET_SPACE;
  58. auto actual = LineMatrixToString(Code128Writer().encode(toEncode, 0, 0));
  59. EXPECT_EQ(actual, expected);
  60. }
  61. TEST(ODCode128Writer, EncodeWithFunc4)
  62. {
  63. auto toEncode = L"\u00f4123";
  64. // "1" "2" "3" check digit 59
  65. auto expected = QUIET_SPACE + START_CODE_B + FNC4B + "10011100110" + "11001110010" + "11001011100" + "11100011010" + STOP + QUIET_SPACE;
  66. auto actual = LineMatrixToString(Code128Writer().encode(toEncode, 0, 0));
  67. EXPECT_EQ(actual, expected);
  68. }
  69. TEST(ODCode128Writer, EncodeWithFncsAndNumberInCodesetA)
  70. {
  71. auto toEncode = L"\n\u00f1\u00f41\n";
  72. auto expected = QUIET_SPACE + START_CODE_A + LF + FNC1 + FNC4A + "10011100110" + LF + "10101111000" + STOP + QUIET_SPACE;
  73. auto actual = LineMatrixToString(Code128Writer().encode(toEncode, 0, 0));
  74. EXPECT_EQ(actual, expected);
  75. }
  76. TEST(ODCode128Writer, RoundtripGS1)
  77. {
  78. auto toEncode = L"\u00f110958\u00f117160526";
  79. auto decResult = Decode(Code128Writer().encode(toEncode, 0, 0));
  80. EXPECT_EQ(decResult.text(TextMode::HRI), "(10)958(17)160526");
  81. EXPECT_EQ(decResult.symbologyIdentifier(), "]C1");
  82. }
  83. TEST(ODCode128Writer, RoundtripFNC1)
  84. {
  85. auto toEncode = L"1\u00f10958\u00f117160526";
  86. auto encResult = Code128Writer().encode(toEncode, 0, 0);
  87. auto decResult = Decode(encResult);
  88. EXPECT_EQ(decResult.bytes().asString(), "1\u001D0958\u001D17160526");
  89. EXPECT_EQ(decResult.symbologyIdentifier(), "]C0");
  90. }
  91. TEST(ODCode128Writer, EncodeSwitchCodesetFromAToB)
  92. {
  93. // start with A switch to B and back to A
  94. auto toEncode = std::string("\0ABab\u0010", 6);
  95. // "\0" "A" "B" Switch to B "a" "b" Switch to A "\u0010" check digit
  96. auto expected = QUIET_SPACE + START_CODE_A + "10100001100" + "10100011000" + "10001011000" + SWITCH_CODE_B + "10010110000" + "10010000110" + SWITCH_CODE_A + "10100111100" + "11001110100" + STOP + QUIET_SPACE;
  97. auto encoded = Code128Writer().encode(toEncode, 0, 0);
  98. auto actual = LineMatrixToString(encoded);
  99. EXPECT_EQ(actual, expected);
  100. auto actualRoundTrip = Decode(encoded).text(TextMode::Plain);
  101. EXPECT_EQ(actualRoundTrip, toEncode);
  102. }
  103. TEST(ODCode128Writer, EncodeSwitchCodesetFromBToA)
  104. {
  105. // start with B switch to A and back to B
  106. auto toEncode = std::string("ab\0ab", 5);
  107. // "a" "b" Switch to A "\0 "Switch to B" "a" "b" check digit
  108. auto expected = QUIET_SPACE + START_CODE_B + "10010110000" + "10010000110" + SWITCH_CODE_A + "10100001100" + SWITCH_CODE_B + "10010110000" + "10010000110" + "11010001110" + STOP + QUIET_SPACE;
  109. auto encoded = Code128Writer().encode(toEncode, 0, 0);
  110. auto actual = LineMatrixToString(encoded);
  111. EXPECT_EQ(actual, expected);
  112. auto actualRoundTrip = Decode(encoded).text(TextMode::Plain);
  113. EXPECT_EQ(actualRoundTrip, toEncode);
  114. }