Add tests for BC_OnedCode128Writer encoding functions.

Make encoding functions return pattern indices instead of pattern
pointers for easier testing. Also remove some dead code.

Change-Id: Ib80d84b2e6828bbc8920b931d77bbcd82427f01a
Reviewed-on: https://pdfium-review.googlesource.com/4474
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 3e5eafb..ba36cfb 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1892,6 +1892,7 @@
     sources += [
       "core/fxcrt/xml/cfx_saxreader_unittest.cpp",
       "core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp",
+      "fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp",
       "fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp",
       "xfa/fde/cfde_txtedtbuf_unittest.cpp",
       "xfa/fde/css/cfde_cssdeclaration_unittest.cpp",
diff --git a/fxbarcode/oned/BC_OnedCode128Writer.cpp b/fxbarcode/oned/BC_OnedCode128Writer.cpp
index 9cdebfd..818b4bc 100644
--- a/fxbarcode/oned/BC_OnedCode128Writer.cpp
+++ b/fxbarcode/oned/BC_OnedCode128Writer.cpp
@@ -29,7 +29,9 @@
 
 namespace {
 
-const int8_t CODE_PATTERNS[107][7] = {
+constexpr size_t kPatternSize = 7;
+
+const int8_t CODE_PATTERNS[107][kPatternSize] = {
     {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
     {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
     {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
@@ -73,16 +75,12 @@
 
 }  // namespace
 
-CBC_OnedCode128Writer::CBC_OnedCode128Writer() {
-  m_codeFormat = BC_CODE128_B;
-}
 CBC_OnedCode128Writer::CBC_OnedCode128Writer(BC_TYPE type) {
   m_codeFormat = type;
 }
+
 CBC_OnedCode128Writer::~CBC_OnedCode128Writer() {}
-BC_TYPE CBC_OnedCode128Writer::GetType() {
-  return m_codeFormat;
-}
+
 bool CBC_OnedCode128Writer::CheckContentValidity(
     const CFX_WideStringC& contents) {
   if (m_codeFormat != BC_CODE128_B && m_codeFormat != BC_CODE128_C)
@@ -146,24 +144,12 @@
                                           hints);
 }
 
-bool CBC_OnedCode128Writer::IsDigits(const CFX_ByteString& contents,
-                                     int32_t start,
-                                     int32_t length) {
-  int32_t end = start + length;
-  for (int32_t i = start; i < end; i++) {
-    if (contents[i] < '0' || contents[i] > '9') {
-      return false;
-    }
-  }
-  return true;
-}
-
 uint8_t* CBC_OnedCode128Writer::EncodeImpl(const CFX_ByteString& contents,
                                            int32_t& outLength) {
   if (contents.GetLength() < 1 || contents.GetLength() > 80)
     return nullptr;
 
-  std::vector<const int8_t*> patterns;
+  std::vector<int32_t> patterns;
   int32_t checkSum = 0;
   if (m_codeFormat == BC_CODE128_B) {
     checkSum = Encode128B(contents, &patterns);
@@ -173,42 +159,41 @@
     return nullptr;
   }
   checkSum %= 103;
-  patterns.push_back(CODE_PATTERNS[checkSum]);
-  patterns.push_back(CODE_PATTERNS[CODE_STOP]);
+  patterns.push_back(checkSum);
+  patterns.push_back(CODE_STOP);
   m_iContentLen = contents.GetLength() + 3;
   int32_t codeWidth = 0;
-  for (size_t k = 0; k < patterns.size(); k++) {
-    const int8_t* pattern = patterns[k];
-    for (size_t j = 0; j < 7; j++) {
-      codeWidth += pattern[j];
-    }
+  for (const auto& patternIndex : patterns) {
+    const int8_t* pattern = CODE_PATTERNS[patternIndex];
+    for (size_t i = 0; i < kPatternSize; ++i)
+      codeWidth += pattern[i];
   }
   outLength = codeWidth;
   std::unique_ptr<uint8_t, FxFreeDeleter> result(FX_Alloc(uint8_t, outLength));
   int32_t pos = 0;
-  for (size_t j = 0; j < patterns.size(); j++) {
-    const int8_t* pattern = patterns[j];
+  for (size_t i = 0; i < patterns.size(); ++i) {
+    const int8_t* pattern = CODE_PATTERNS[patterns[i]];
     int32_t e = BCExceptionNO;
-    pos += AppendPattern(result.get(), pos, pattern, 7, 1, e);
+    pos += AppendPattern(result.get(), pos, pattern, kPatternSize, 1, e);
     if (e != BCExceptionNO)
       return nullptr;
   }
   return result.release();
 }
 
-int32_t CBC_OnedCode128Writer::Encode128B(
-    const CFX_ByteString& contents,
-    std::vector<const int8_t*>* patterns) {
+// static
+int32_t CBC_OnedCode128Writer::Encode128B(const CFX_ByteString& contents,
+                                          std::vector<int32_t>* patterns) {
   int32_t checkSum = 0;
   int32_t checkWeight = 1;
   int32_t position = 0;
-  patterns->push_back(CODE_PATTERNS[CODE_START_B]);
+  patterns->push_back(CODE_START_B);
   checkSum += CODE_START_B * checkWeight;
   while (position < contents.GetLength()) {
     int32_t patternIndex = 0;
     patternIndex = contents[position] - ' ';
     position += 1;
-    patterns->push_back(CODE_PATTERNS[patternIndex]);
+    patterns->push_back(patternIndex);
     checkSum += patternIndex * checkWeight;
     if (position != 0) {
       checkWeight++;
@@ -217,13 +202,13 @@
   return checkSum;
 }
 
-int32_t CBC_OnedCode128Writer::Encode128C(
-    const CFX_ByteString& contents,
-    std::vector<const int8_t*>* patterns) {
+// static
+int32_t CBC_OnedCode128Writer::Encode128C(const CFX_ByteString& contents,
+                                          std::vector<int32_t>* patterns) {
   int32_t checkSum = 0;
   int32_t checkWeight = 1;
   int32_t position = 0;
-  patterns->push_back(CODE_PATTERNS[CODE_START_C]);
+  patterns->push_back(CODE_START_C);
   checkSum += CODE_START_C * checkWeight;
   while (position < contents.GetLength()) {
     int32_t patternIndex = 0;
@@ -240,7 +225,7 @@
         position += 2;
       }
     }
-    patterns->push_back(CODE_PATTERNS[patternIndex]);
+    patterns->push_back(patternIndex);
     checkSum += patternIndex * checkWeight;
     if (position != 0) {
       checkWeight++;
diff --git a/fxbarcode/oned/BC_OnedCode128Writer.h b/fxbarcode/oned/BC_OnedCode128Writer.h
index c4c0bf0..f1b346d 100644
--- a/fxbarcode/oned/BC_OnedCode128Writer.h
+++ b/fxbarcode/oned/BC_OnedCode128Writer.h
@@ -15,10 +15,15 @@
 
 class CBC_OnedCode128Writer : public CBC_OneDimWriter {
  public:
-  CBC_OnedCode128Writer();
   explicit CBC_OnedCode128Writer(BC_TYPE type);
   ~CBC_OnedCode128Writer() override;
 
+  // Exposed for testing.
+  static int32_t Encode128B(const CFX_ByteString& contents,
+                            std::vector<int32_t>* patterns);
+  static int32_t Encode128C(const CFX_ByteString& contents,
+                            std::vector<int32_t>* patterns);
+
   // CBC_OneDimWriter
   uint8_t* EncodeWithHint(const CFX_ByteString& contents,
                           BCFORMAT format,
@@ -32,15 +37,9 @@
 
   bool SetTextLocation(BC_TEXT_LOC location);
 
-  BC_TYPE GetType();
+  BC_TYPE GetType() const { return m_codeFormat; }
 
  private:
-  bool IsDigits(const CFX_ByteString& contents, int32_t start, int32_t length);
-  int32_t Encode128B(const CFX_ByteString& contents,
-                     std::vector<const int8_t*>* patterns);
-  int32_t Encode128C(const CFX_ByteString& contents,
-                     std::vector<const int8_t*>* patterns);
-
   BC_TYPE m_codeFormat;
 };
 
diff --git a/fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp b/fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp
new file mode 100644
index 0000000..add4696
--- /dev/null
+++ b/fxbarcode/oned/BC_OnedCode128Writer_unittest.cpp
@@ -0,0 +1,84 @@
+// 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_OnedCode128Writer.h"
+#include "core/fxcrt/fx_basic.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+struct TestCase {
+  const char* input;
+  int32_t checksum;
+  int32_t patterns[7];
+  size_t num_patterns;
+};
+
+TEST(OnedCode128WriterTest, Encode128B) {
+  char buf[100];
+  TestCase kTestCases[] = {
+      {"", 104, {104}, 1},
+      {"a", 169, {104, 65}, 2},
+      {"1", 121, {104, 17}, 2},
+      {"a1", 203, {104, 65, 17}, 3},
+      {"ab", 301, {104, 65, 66}, 3},
+      {"12", 157, {104, 17, 18}, 3},
+      {"abc", 502, {104, 65, 66, 67}, 4},
+      {"123", 214, {104, 17, 18, 19}, 4},
+      {"abc123", 774, {104, 65, 66, 67, 17, 18, 19}, 7},
+      {"ABC123", 582, {104, 33, 34, 35, 17, 18, 19}, 7},
+      {"321ABC", 722, {104, 19, 18, 17, 33, 34, 35}, 7},
+      {"XYZ", 448, {104, 56, 57, 58}, 4},
+  };
+  for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
+    FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
+    SCOPED_TRACE(buf);
+    const TestCase& test_case = kTestCases[i];
+    std::vector<int32_t> patterns;
+    int32_t checksum =
+        CBC_OnedCode128Writer::Encode128B(test_case.input, &patterns);
+    EXPECT_EQ(test_case.checksum, checksum);
+    ASSERT_EQ(test_case.num_patterns, patterns.size());
+    for (size_t j = 0; j < patterns.size(); ++j) {
+      FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
+      SCOPED_TRACE(buf);
+      EXPECT_EQ(test_case.patterns[j], patterns[j]);
+    }
+  }
+}
+
+TEST(OnedCode128WriterTest, Encode128C) {
+  char buf[100];
+  TestCase kTestCases[] = {
+      {"", 105, {105}, 1},
+      {"a", 202, {105, 97}, 2},
+      {"1", 106, {105, 1}, 2},
+      {"a1", 204, {105, 97, 1}, 3},
+      {"ab", 398, {105, 97, 98}, 3},
+      {"12", 117, {105, 12}, 2},
+      {"abc", 695, {105, 97, 98, 99}, 4},
+      {"123", 123, {105, 12, 3}, 3},
+      {"abc123", 758, {105, 97, 98, 99, 12, 3}, 6},
+      {"ABC123", 566, {105, 65, 66, 67, 12, 3}, 6},
+      {"321ABC", 933, {105, 32, 1, 65, 66, 67}, 6},
+      {"XYZ", 641, {105, 88, 89, 90}, 4},
+  };
+  for (size_t i = 0; i < FX_ArraySize(kTestCases); ++i) {
+    FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
+    SCOPED_TRACE(buf);
+    const TestCase& test_case = kTestCases[i];
+    std::vector<int32_t> patterns;
+    int32_t checksum =
+        CBC_OnedCode128Writer::Encode128C(test_case.input, &patterns);
+    EXPECT_EQ(test_case.checksum, checksum);
+    ASSERT_EQ(test_case.num_patterns, patterns.size());
+    for (size_t j = 0; j < patterns.size(); ++j) {
+      FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
+      SCOPED_TRACE(buf);
+      EXPECT_EQ(test_case.patterns[j], patterns[j]);
+    }
+  }
+}
+
+}  // namespace