diff options
author | Henrique Nakashima <hnakashima@chromium.org> | 2017-09-01 16:01:44 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-09-01 20:29:34 +0000 |
commit | ab3c111433229fc79275c4b413b24bec6f5463ce (patch) | |
tree | ceee33196d76ebded37cd5c9e7ebfd35303215c2 /fxbarcode | |
parent | a2188df09255b49ad41a89ae9b5de640d0b03126 (diff) | |
download | pdfium-ab3c111433229fc79275c4b413b24bec6f5463ce.tar.xz |
Fix EAN-13 checksum and add unit tests.
Parity in EAN-13 is considered counting digits from right to left,
starting at 1.
Bug: pdfium:882
Change-Id: I3e586499091b8400daf93657eb9878f29d9e6922
Reviewed-on: https://pdfium-review.googlesource.com/12910
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'fxbarcode')
-rw-r--r-- | fxbarcode/oned/BC_OnedEAN13Writer.cpp | 15 | ||||
-rw-r--r-- | fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp | 98 |
2 files changed, 105 insertions, 8 deletions
diff --git a/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/fxbarcode/oned/BC_OnedEAN13Writer.cpp index dddb9cc24d..e7642de56d 100644 --- a/fxbarcode/oned/BC_OnedEAN13Writer.cpp +++ b/fxbarcode/oned/BC_OnedEAN13Writer.cpp @@ -79,17 +79,16 @@ CFX_WideString CBC_OnedEAN13Writer::FilterContents( int32_t CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString& contents) { int32_t odd = 0; int32_t even = 0; - FX_STRSIZE j = 1; - for (FX_STRSIZE i = 0; i < contents.GetLength(); i++) { - if (j % 2) { - odd += FXSYS_DecimalCharToInt(contents[i]); + FX_STRSIZE parity = 1; + for (FX_STRSIZE i = contents.GetLength(); i > 0; i--) { + if (parity % 2) { + odd += FXSYS_DecimalCharToInt(contents[i - 1]); } else { - even += FXSYS_DecimalCharToInt(contents[i]); + even += FXSYS_DecimalCharToInt(contents[i - 1]); } - j++; + parity++; } - int32_t checksum = 10 - (odd * 3 + even) % 10; - return checksum; + return (10 - (odd * 3 + even) % 10) % 10; } uint8_t* CBC_OnedEAN13Writer::EncodeWithHint(const CFX_ByteString& contents, diff --git a/fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp b/fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp new file mode 100644 index 0000000000..932e1f42c8 --- /dev/null +++ b/fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp @@ -0,0 +1,98 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "fxbarcode/oned/BC_OnedEAN13Writer.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +TEST(OnedEAN13WriterTest, Encode) { + CBC_OnedEAN13Writer writer; + int32_t width; + int32_t height; + uint8_t* encoded; + const char* expected; + + // EAN-13 barcodes encode 13-digit numbers into 95 modules in a unidimensional + // disposition. + encoded = writer.Encode("", BCFORMAT_EAN_13, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("123", BCFORMAT_EAN_13, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("123456789012", BCFORMAT_EAN_13, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("12345678901234", BCFORMAT_EAN_13, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("1234567890128", BCFORMAT_EAN_13, width, height); + EXPECT_NE(nullptr, encoded); + EXPECT_EQ(1, height); + EXPECT_EQ(95, width); + + expected = + "# #" // Start + // 1 implicit by LLGLGG in next 6 digits + " # ##" // 2 L + " #### #" // 3 L + " ### #" // 4 G + " ## #" // 5 L + " # #" // 6 G + " # #" // 7 G + " # # " // Middle + "# # " // 8 R + "### # " // 9 R + "### # " // 0 R + "## ## " // 1 R + "## ## " // 2 R + "# # " // 8 R + "# #"; // End + for (int i = 0; i < 95; i++) { + EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; + } + FX_Free(encoded); + + encoded = writer.Encode("7776665554440", BCFORMAT_EAN_13, width, height); + EXPECT_NE(nullptr, encoded); + EXPECT_EQ(1, height); + EXPECT_EQ(95, width); + + expected = + "# #" // Start + // 7 implicit by LGLGLG in next 6 digits + " ### ##" // 7 L + " # #" // 7 G + " # ####" // 6 L + " # #" // 6 G + " # ####" // 6 L + " ### #" // 5 G + " # # " // Middle + "# ### " // 5 R + "# ### " // 5 R + "# ### " // 4 R + "# ### " // 4 R + "# ### " // 4 R + "### # " // 0 R + "# #"; // End + for (int i = 0; i < 95; i++) { + EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; + } + FX_Free(encoded); +} + +TEST(OnedEAN13WriterTest, Checksum) { + CBC_OnedEAN13Writer writer; + EXPECT_EQ(0, writer.CalcChecksum("")); + EXPECT_EQ(6, writer.CalcChecksum("123")); + EXPECT_EQ(8, writer.CalcChecksum("123456789012")); + EXPECT_EQ(0, writer.CalcChecksum("777666555444")); +} + +} // namespace |