ODCodaBarWriterTest.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2017 Huy Cuong Nguyen
  3. * Copyright 2011 ZXing authors
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "oned/ODCodabarWriter.h"
  7. #include "BitMatrixIO.h"
  8. #include "ReaderOptions.h"
  9. #include "Barcode.h"
  10. #include "oned/ODCodabarReader.h"
  11. #include "gtest/gtest.h"
  12. #include <stdexcept>
  13. using namespace ZXing;
  14. using namespace ZXing::OneD;
  15. namespace {
  16. std::string Encode(const std::string& input)
  17. {
  18. auto result = ToString(CodabarWriter().encode(input, 0, 0), '1', '0', false);
  19. return result.substr(0, result.size() - 1); // remove the \n at the end
  20. }
  21. }
  22. TEST(ODCodaBarWriterTest, Encode)
  23. {
  24. EXPECT_EQ(Encode("B515-3/B"),
  25. "00000"
  26. "1001001011" "0110101001" "0101011001" "0110101001" "0101001101"
  27. "0110010101" "01101101011" "01001001011"
  28. "00000");
  29. }
  30. TEST(ODCodaBarWriterTest, Encode2)
  31. {
  32. EXPECT_EQ(Encode("T123T"),
  33. "00000"
  34. "1011001001" "0101011001" "0101001011" "0110010101" "01011001001"
  35. "00000");
  36. }
  37. TEST(ODCodaBarWriterTest, AltStartEnd)
  38. {
  39. EXPECT_EQ(Encode("T123456789-$T"), Encode("A123456789-$A"));
  40. }
  41. TEST(ODCodaBarWriterTest, FullCircle)
  42. {
  43. std::string text = "A0123456789-$:/.+A";
  44. auto matrix = CodabarWriter().encode(text, 0, 0);
  45. auto opts = ReaderOptions();
  46. auto res = OneD::DecodeSingleRow(CodabarReader(opts), matrix.row(0));
  47. EXPECT_EQ(text, res.text());
  48. }
  49. TEST(ODCodaBarWriterTest, InvalidChars)
  50. {
  51. EXPECT_THROW({Encode("AxA");}, std::invalid_argument );
  52. EXPECT_THROW({Encode("a0a");}, std::invalid_argument );
  53. }