summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrique Nakashima <hnakashima@chromium.org>2017-09-06 11:11:49 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-09-06 15:44:58 +0000
commit746552254ad113ef88b40aca4dbc0e57cefbac85 (patch)
tree75c606a9f305fe2206af0143138e84dc7272d2c5
parent81f9eeef041f2974274751d7508598049ae32db2 (diff)
downloadpdfium-746552254ad113ef88b40aca4dbc0e57cefbac85.tar.xz
Add unit tests for UPC-A barcode writer.
Bug: pdfium:882 Change-Id: I609adfa652285fe1702f742a2774ffa566471d5c Reviewed-on: https://pdfium-review.googlesource.com/13270 Reviewed-by: Ryan Harrison <rharrison@chromium.org> Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
-rw-r--r--BUILD.gn1
-rw-r--r--fxbarcode/oned/BC_OnedUPCAWriter_unittest.cpp96
2 files changed, 97 insertions, 0 deletions
diff --git a/BUILD.gn b/BUILD.gn
index 6b4ff62bfd..452f32610b 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1957,6 +1957,7 @@ test("pdfium_unittests") {
"fxbarcode/oned/BC_OnedCode39Writer_unittest.cpp",
"fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp",
"fxbarcode/oned/BC_OnedEAN8Writer_unittest.cpp",
+ "fxbarcode/oned/BC_OnedUPCAWriter_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_OnedUPCAWriter_unittest.cpp b/fxbarcode/oned/BC_OnedUPCAWriter_unittest.cpp
new file mode 100644
index 0000000000..3d9f6c5e61
--- /dev/null
+++ b/fxbarcode/oned/BC_OnedUPCAWriter_unittest.cpp
@@ -0,0 +1,96 @@
+// 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_OnedUPCAWriter.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+TEST(OnedUPCAWriterTest, Encode) {
+ CBC_OnedUPCAWriter writer;
+ int32_t width;
+ int32_t height;
+
+ // TODO(hnakashima): CBC_OnedUPCAWriter is unique in that it needs to be
+ // Init()'d. Get rid of this call.
+ writer.Init();
+
+ // UPCA barcodes encode 12-digit numbers into 95 modules in a unidimensional
+ // disposition.
+ uint8_t* encoded = writer.Encode("", BCFORMAT_UPC_A, width, height);
+ EXPECT_EQ(nullptr, encoded);
+ FX_Free(encoded);
+
+ encoded = writer.Encode("123", BCFORMAT_UPC_A, width, height);
+ EXPECT_EQ(nullptr, encoded);
+ FX_Free(encoded);
+
+ encoded = writer.Encode("12345678901", BCFORMAT_UPC_A, width, height);
+ EXPECT_EQ(nullptr, encoded);
+ FX_Free(encoded);
+
+ encoded = writer.Encode("1234567890123", BCFORMAT_UPC_A, width, height);
+ EXPECT_EQ(nullptr, encoded);
+ FX_Free(encoded);
+
+ encoded = writer.Encode("123456789012", BCFORMAT_UPC_A, width, height);
+ const char* expected =
+ "# #" // Start
+ " ## #" // 1 L
+ " # ##" // 2 L
+ " #### #" // 3 L
+ " # ##" // 4 L
+ " ## #" // 5 L
+ " # ####" // 6 L
+ " # # " // Middle
+ "# # " // 7 R
+ "# # " // 8 R
+ "### # " // 9 R
+ "### # " // 0 R
+ "## ## " // 1 R
+ "## ## " // 2 R
+ "# #"; // End
+ EXPECT_NE(nullptr, encoded);
+ EXPECT_EQ(1, height);
+ EXPECT_EQ(static_cast<int32_t>(strlen(expected)), width);
+ for (size_t i = 0; i < strlen(expected); i++) {
+ EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+ }
+ FX_Free(encoded);
+
+ encoded = writer.Encode("777666555440", BCFORMAT_UPC_A, width, height);
+ expected =
+ "# #" // Start
+ " ### ##" // 7 L
+ " ### ##" // 7 L
+ " ### ##" // 7 L
+ " # ####" // 6 L
+ " # ####" // 6 L
+ " # ####" // 6 L
+ " # # " // Middle
+ "# ### " // 5 R
+ "# ### " // 5 R
+ "# ### " // 5 R
+ "# ### " // 4 R
+ "# ### " // 4 R
+ "### # " // 0 R
+ "# #"; // End
+ EXPECT_NE(nullptr, encoded);
+ EXPECT_EQ(1, height);
+ EXPECT_EQ(static_cast<int32_t>(strlen(expected)), width);
+ for (size_t i = 0; i < strlen(expected); i++) {
+ EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+ }
+ FX_Free(encoded);
+}
+
+TEST(OnedUPCAWriterTest, Checksum) {
+ CBC_OnedUPCAWriter writer;
+ EXPECT_EQ(0, writer.CalcChecksum(""));
+ EXPECT_EQ(6, writer.CalcChecksum("123"));
+ EXPECT_EQ(2, writer.CalcChecksum("12345678901"));
+ EXPECT_EQ(0, writer.CalcChecksum("77766655544"));
+}
+
+} // namespace