ODEAN13WriterTest.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright 2017 Huy Cuong Nguyen
  3. * Copyright 2009 ZXing authors
  4. */
  5. // SPDX-License-Identifier: Apache-2.0
  6. #include "oned/ODEAN13Writer.h"
  7. #include "BitMatrixIO.h"
  8. #include "gtest/gtest.h"
  9. #include <stdexcept>
  10. using namespace ZXing;
  11. using namespace ZXing::OneD;
  12. namespace {
  13. std::string Encode(const std::string& input)
  14. {
  15. auto result = ToString(EAN13Writer().encode(input, 0, 0), '1', '0', false);
  16. return result.substr(0, result.size() - 1); // remove the \n at the end
  17. }
  18. }
  19. TEST(ODEAN13WriterTest, Encode1)
  20. {
  21. std::string toEncode = "5901234123457";
  22. std::string expected = "00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
  23. EXPECT_EQ(Encode(toEncode), expected);
  24. }
  25. TEST(ODEAN13WriterTest, AddChecksumAndEncode)
  26. {
  27. std::string toEncode = "590123412345";
  28. std::string expected = "00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
  29. EXPECT_EQ(Encode(toEncode), expected);
  30. }
  31. TEST(ODEAN13WriterTest, EncodeIllegalCharacters)
  32. {
  33. EXPECT_THROW({ Encode("5901234123abc"); }, std::invalid_argument);
  34. }