diff options
author | Henrique Nakashima <hnakashima@chromium.org> | 2017-09-01 16:40:48 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-09-01 20:47:34 +0000 |
commit | 3c5944c93445bb40c2131ea6782e8cc8b673bffb (patch) | |
tree | f6f9db309598f7abaaa4d544782ce993b7c576e6 | |
parent | ab3c111433229fc79275c4b413b24bec6f5463ce (diff) | |
download | pdfium-chromium/3204.tar.xz |
Add unit tests for EAN-8 barcode writer.chromium/3204
Bug: pdfium:882
Change-Id: I900d3c1b0b74523fa9e4497da65c68eb307ea6dc
Reviewed-on: https://pdfium-review.googlesource.com/12950
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | fxbarcode/oned/BC_OnedEAN8Writer.cpp | 6 | ||||
-rw-r--r-- | fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp | 88 |
3 files changed, 92 insertions, 3 deletions
@@ -1952,6 +1952,7 @@ test("pdfium_unittests") { "core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp", "fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp", "fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp", + "fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp", "fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp", "xfa/fde/cfde_texteditengine_unittest.cpp", "xfa/fgas/crt/cfgas_formatstring_unittest.cpp", diff --git a/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/fxbarcode/oned/BC_OnedEAN8Writer.cpp index 3fcb1e8350..c3becc1a43 100644 --- a/fxbarcode/oned/BC_OnedEAN8Writer.cpp +++ b/fxbarcode/oned/BC_OnedEAN8Writer.cpp @@ -86,16 +86,16 @@ CFX_WideString CBC_OnedEAN8Writer::FilterContents( int32_t CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString& contents) { int32_t odd = 0; int32_t even = 0; + FX_STRSIZE parity = 1; for (FX_STRSIZE i = contents.GetLength(); i > 0; i--) { if (i % 2) { odd += FXSYS_DecimalCharToInt(contents[i - 1]); } else { even += FXSYS_DecimalCharToInt(contents[i - 1]); } + parity++; } - int32_t checksum = (odd * 3 + even) % 10; - checksum = (10 - checksum) % 10; - return checksum; + return (10 - (odd * 3 + even) % 10) % 10; } uint8_t* CBC_OnedEAN8Writer::EncodeWithHint(const CFX_ByteString& contents, diff --git a/fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp b/fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp new file mode 100644 index 0000000000..16a1df6c28 --- /dev/null +++ b/fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp @@ -0,0 +1,88 @@ +// 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_OnedEAN8Writer.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +TEST(OnedEAN8WriterTest, Encode) { + CBC_OnedEAN8Writer writer; + int32_t width; + int32_t height; + uint8_t* encoded; + const char* expected; + + // EAN-8 barcodes encode 8-digit numbers into 67 modules in a unidimensional + // disposition. + encoded = writer.Encode("", BCFORMAT_EAN_8, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("123", BCFORMAT_EAN_8, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("1234567", BCFORMAT_EAN_8, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("123456789", BCFORMAT_EAN_8, width, height); + EXPECT_EQ(nullptr, encoded); + FX_Free(encoded); + + encoded = writer.Encode("12345670", BCFORMAT_EAN_8, width, height); + EXPECT_NE(nullptr, encoded); + EXPECT_EQ(1, height); + EXPECT_EQ(67, width); + + expected = + "# #" // Start + " ## #" // 1 L + " # ##" // 2 L + " #### #" // 3 L + " # ##" // 4 L + " # # " // Middle + "# ### " // 5 R + "# # " // 6 R + "# # " // 7 R + "### # " // 0 R + "# #"; // End + for (int i = 0; i < 67; i++) { + EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; + } + FX_Free(encoded); + + encoded = writer.Encode("99441104", BCFORMAT_EAN_8, width, height); + EXPECT_NE(nullptr, encoded); + EXPECT_EQ(1, height); + EXPECT_EQ(67, width); + + expected = + "# #" // Start + " # ##" // 9 L + " # ##" // 9 L + " # ##" // 4 L + " # ##" // 4 L + " # # " // Middle + "## ## " // 1 R + "## ## " // 1 R + "### # " // 0 R + "# ### " // 4 R + "# #"; // End + for (int i = 0; i < 67; i++) { + EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i; + } + FX_Free(encoded); +} + +TEST(OnedEAN8WriterTest, Checksum) { + CBC_OnedEAN8Writer writer; + EXPECT_EQ(0, writer.CalcChecksum("")); + EXPECT_EQ(6, writer.CalcChecksum("123")); + EXPECT_EQ(0, writer.CalcChecksum("1234567")); + EXPECT_EQ(4, writer.CalcChecksum("9944110")); +} + +} // namespace |