Add unit tests for Codabar barcode writer.

Bug: pdfium:882
Change-Id: Ieb06c4c060307bffa6e4fe20c7ced6be6518adca
Reviewed-on: https://pdfium-review.googlesource.com/13190
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index f2fe2e1..ec37644 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1954,6 +1954,7 @@
       "core/fxcrt/css/cfx_cssvaluelistparser_unittest.cpp",
       "core/fxcrt/xml/cfx_saxreader_unittest.cpp",
       "core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp",
+      "fxbarcode/oned/BC_OnedCodaBarWriter_unittest.cpp",
       "fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp",
       "fxbarcode/oned/BC_OnedCode39Writer_unittest.cpp",
       "fxbarcode/oned/BC_OnedEAN13Writer_unittest.cpp",
diff --git a/fxbarcode/oned/BC_OnedCodaBarWriter_unittest.cpp b/fxbarcode/oned/BC_OnedCodaBarWriter_unittest.cpp
new file mode 100644
index 0000000..f498449
--- /dev/null
+++ b/fxbarcode/oned/BC_OnedCodaBarWriter_unittest.cpp
@@ -0,0 +1,148 @@
+// 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_OnedCodaBarWriter.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// 3 wide and 4 narrow modules per delimiter. One space between them.
+const int kModulesForDelimiters = (3 * 2 + 4 * 1) * 2 + 1;
+
+// 2 wide and 5 narrow modules per number, '_' or '$'. 1 space between chars.
+const int kModulesPerNumber = 2 * 2 + 5 * 1 + 1;
+
+// 3 wide and 4 narrow modules per number, '_' or '$'. 1 space between chars.
+const int kModulesPerPunctuation = 3 * 2 + 4 * 1 + 1;
+
+TEST(OnedCodaBarWriterTest, Encode) {
+  CBC_OnedCodaBarWriter writer;
+  int32_t width;
+  int32_t height;
+
+  uint8_t* encoded = writer.Encode("", BCFORMAT_CODABAR, width, height);
+  EXPECT_EQ(1, height);
+  EXPECT_EQ(kModulesForDelimiters, width);
+  const char* expected =
+      "# ##  #  # "  // A Start
+      "#  #  # ##";  // B End
+  for (size_t i = 0; i < strlen(expected); i++) {
+    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+  }
+  FX_Free(encoded);
+
+  encoded = writer.Encode("123", BCFORMAT_CODABAR, width, height);
+  EXPECT_EQ(1, height);
+  EXPECT_EQ(kModulesForDelimiters + 3 * kModulesPerNumber, width);
+  expected =
+      "# ##  #  # "  // A Start
+      "# # ##  # "   // 1
+      "# #  # ## "   // 2
+      "##  # # # "   // 3
+      "#  #  # ##";  // B End
+  for (size_t i = 0; i < strlen(expected); i++) {
+    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+  }
+  FX_Free(encoded);
+
+  encoded = writer.Encode("-$./:+", BCFORMAT_CODABAR, width, height);
+  EXPECT_EQ(1, height);
+  EXPECT_EQ(kModulesForDelimiters + 2 * kModulesPerNumber +
+                4 * kModulesPerPunctuation,
+            width);
+  expected =
+      "# ##  #  # "  // A Start
+      "# #  ## # "   // -
+      "# ##  # # "   // $
+      "## ## ## # "  // .
+      "## ## # ## "  // /
+      "## # ## ## "  // :
+      "# ## ## ## "  // +
+      "#  #  # ##";  // B End
+  for (size_t i = 0; i < strlen(expected); i++) {
+    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+  }
+  FX_Free(encoded);
+
+  encoded = writer.Encode("456.987987987/001", BCFORMAT_CODABAR, width, height);
+  EXPECT_EQ(1, height);
+  EXPECT_EQ(kModulesForDelimiters + 15 * kModulesPerNumber +
+                2 * kModulesPerPunctuation,
+            width);
+  expected =
+      "# ##  #  # "  // A Start
+      "# ## #  # "   // 4
+      "## # #  # "   // 5
+      "#  # # ## "   // 6
+      "## ## ## # "  // .
+      "## #  # # "   // 9
+      "#  ## # # "   // 8
+      "#  # ## # "   // 7
+      "## #  # # "   // 9
+      "#  ## # # "   // 8
+      "#  # ## # "   // 7
+      "## #  # # "   // 9
+      "#  ## # # "   // 8
+      "#  # ## # "   // 7
+      "## ## # ## "  // /
+      "# # #  ## "   // 0
+      "# # #  ## "   // 0
+      "# # ##  # "   // 1
+      "#  #  # ##";  // B End
+  for (size_t i = 0; i < strlen(expected); i++) {
+    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+  }
+  FX_Free(encoded);
+}
+
+TEST(OnedCodaBarWriterTest, SetDelimiters) {
+  CBC_OnedCodaBarWriter writer;
+  int32_t width;
+  int32_t height;
+
+  EXPECT_TRUE(writer.SetStartChar('A'));
+  EXPECT_TRUE(writer.SetStartChar('B'));
+  EXPECT_TRUE(writer.SetStartChar('C'));
+  EXPECT_TRUE(writer.SetStartChar('D'));
+  EXPECT_TRUE(writer.SetStartChar('E'));
+  EXPECT_TRUE(writer.SetStartChar('N'));
+  EXPECT_TRUE(writer.SetStartChar('T'));
+  EXPECT_TRUE(writer.SetStartChar('*'));
+  EXPECT_FALSE(writer.SetStartChar('V'));
+  EXPECT_FALSE(writer.SetStartChar('0'));
+  EXPECT_FALSE(writer.SetStartChar('\0'));
+  EXPECT_FALSE(writer.SetStartChar('@'));
+
+  EXPECT_TRUE(writer.SetEndChar('A'));
+  EXPECT_TRUE(writer.SetEndChar('B'));
+  EXPECT_TRUE(writer.SetEndChar('C'));
+  EXPECT_TRUE(writer.SetEndChar('D'));
+  EXPECT_TRUE(writer.SetEndChar('E'));
+  EXPECT_TRUE(writer.SetEndChar('N'));
+  EXPECT_TRUE(writer.SetEndChar('T'));
+  EXPECT_TRUE(writer.SetEndChar('*'));
+  EXPECT_FALSE(writer.SetEndChar('V'));
+  EXPECT_FALSE(writer.SetEndChar('0'));
+  EXPECT_FALSE(writer.SetEndChar('\0'));
+  EXPECT_FALSE(writer.SetEndChar('@'));
+
+  writer.SetStartChar('N');
+  writer.SetEndChar('*');
+
+  uint8_t* encoded = writer.Encode("987", BCFORMAT_CODABAR, width, height);
+  EXPECT_EQ(1, height);
+  EXPECT_EQ(kModulesForDelimiters + 3 * kModulesPerNumber, width);
+  const char* expected =
+      "#  #  # ## "  // N (same as B) Start
+      "## #  # # "   // 9
+      "#  ## # # "   // 8
+      "#  # ## # "   // 7
+      "# #  #  ##";  // * (same as C) End
+  for (size_t i = 0; i < strlen(expected); i++) {
+    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
+  }
+  FX_Free(encoded);
+}
+
+}  // namespace