1// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "fxbarcode/oned/BC_OnedCodaBarWriter.h"
6#include "testing/gtest/include/gtest/gtest.h"
7
8namespace {
9
10// 3 wide and 4 narrow modules per delimiter. One space between them.
11const int kModulesForDelimiters = (3 * 2 + 4 * 1) * 2 + 1;
12
13// 2 wide and 5 narrow modules per number, '_' or '$'. 1 space between chars.
14const int kModulesPerNumber = 2 * 2 + 5 * 1 + 1;
15
16// 3 wide and 4 narrow modules per number, '_' or '$'. 1 space between chars.
17const int kModulesPerPunctuation = 3 * 2 + 4 * 1 + 1;
18
19TEST(OnedCodaBarWriterTest, Encode) {
20  CBC_OnedCodaBarWriter writer;
21  int32_t width;
22  int32_t height;
23
24  uint8_t* encoded = writer.Encode("", BCFORMAT_CODABAR, width, height);
25  EXPECT_EQ(1, height);
26  EXPECT_EQ(kModulesForDelimiters, width);
27  const char* expected =
28      "# ##  #  # "  // A Start
29      "#  #  # ##";  // B End
30  for (size_t i = 0; i < strlen(expected); i++) {
31    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
32  }
33  FX_Free(encoded);
34
35  encoded = writer.Encode("123", BCFORMAT_CODABAR, width, height);
36  EXPECT_EQ(1, height);
37  EXPECT_EQ(kModulesForDelimiters + 3 * kModulesPerNumber, width);
38  expected =
39      "# ##  #  # "  // A Start
40      "# # ##  # "   // 1
41      "# #  # ## "   // 2
42      "##  # # # "   // 3
43      "#  #  # ##";  // B End
44  for (size_t i = 0; i < strlen(expected); i++) {
45    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
46  }
47  FX_Free(encoded);
48
49  encoded = writer.Encode("-$./:+", BCFORMAT_CODABAR, width, height);
50  EXPECT_EQ(1, height);
51  EXPECT_EQ(kModulesForDelimiters + 2 * kModulesPerNumber +
52                4 * kModulesPerPunctuation,
53            width);
54  expected =
55      "# ##  #  # "  // A Start
56      "# #  ## # "   // -
57      "# ##  # # "   // $
58      "## ## ## # "  // .
59      "## ## # ## "  // /
60      "## # ## ## "  // :
61      "# ## ## ## "  // +
62      "#  #  # ##";  // B End
63  for (size_t i = 0; i < strlen(expected); i++) {
64    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
65  }
66  FX_Free(encoded);
67
68  encoded = writer.Encode("456.987987987/001", BCFORMAT_CODABAR, width, height);
69  EXPECT_EQ(1, height);
70  EXPECT_EQ(kModulesForDelimiters + 15 * kModulesPerNumber +
71                2 * kModulesPerPunctuation,
72            width);
73  expected =
74      "# ##  #  # "  // A Start
75      "# ## #  # "   // 4
76      "## # #  # "   // 5
77      "#  # # ## "   // 6
78      "## ## ## # "  // .
79      "## #  # # "   // 9
80      "#  ## # # "   // 8
81      "#  # ## # "   // 7
82      "## #  # # "   // 9
83      "#  ## # # "   // 8
84      "#  # ## # "   // 7
85      "## #  # # "   // 9
86      "#  ## # # "   // 8
87      "#  # ## # "   // 7
88      "## ## # ## "  // /
89      "# # #  ## "   // 0
90      "# # #  ## "   // 0
91      "# # ##  # "   // 1
92      "#  #  # ##";  // B End
93  for (size_t i = 0; i < strlen(expected); i++) {
94    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
95  }
96  FX_Free(encoded);
97}
98
99TEST(OnedCodaBarWriterTest, SetDelimiters) {
100  CBC_OnedCodaBarWriter writer;
101  int32_t width;
102  int32_t height;
103
104  EXPECT_TRUE(writer.SetStartChar('A'));
105  EXPECT_TRUE(writer.SetStartChar('B'));
106  EXPECT_TRUE(writer.SetStartChar('C'));
107  EXPECT_TRUE(writer.SetStartChar('D'));
108  EXPECT_TRUE(writer.SetStartChar('E'));
109  EXPECT_TRUE(writer.SetStartChar('N'));
110  EXPECT_TRUE(writer.SetStartChar('T'));
111  EXPECT_TRUE(writer.SetStartChar('*'));
112  EXPECT_FALSE(writer.SetStartChar('V'));
113  EXPECT_FALSE(writer.SetStartChar('0'));
114  EXPECT_FALSE(writer.SetStartChar('\0'));
115  EXPECT_FALSE(writer.SetStartChar('@'));
116
117  EXPECT_TRUE(writer.SetEndChar('A'));
118  EXPECT_TRUE(writer.SetEndChar('B'));
119  EXPECT_TRUE(writer.SetEndChar('C'));
120  EXPECT_TRUE(writer.SetEndChar('D'));
121  EXPECT_TRUE(writer.SetEndChar('E'));
122  EXPECT_TRUE(writer.SetEndChar('N'));
123  EXPECT_TRUE(writer.SetEndChar('T'));
124  EXPECT_TRUE(writer.SetEndChar('*'));
125  EXPECT_FALSE(writer.SetEndChar('V'));
126  EXPECT_FALSE(writer.SetEndChar('0'));
127  EXPECT_FALSE(writer.SetEndChar('\0'));
128  EXPECT_FALSE(writer.SetEndChar('@'));
129
130  writer.SetStartChar('N');
131  writer.SetEndChar('*');
132
133  uint8_t* encoded = writer.Encode("987", BCFORMAT_CODABAR, width, height);
134  EXPECT_EQ(1, height);
135  EXPECT_EQ(kModulesForDelimiters + 3 * kModulesPerNumber, width);
136  const char* expected =
137      "#  #  # ## "  // N (same as B) Start
138      "## #  # # "   // 9
139      "#  ## # # "   // 8
140      "#  # ## # "   // 7
141      "# #  #  ##";  // * (same as C) End
142  for (size_t i = 0; i < strlen(expected); i++) {
143    EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
144  }
145  FX_Free(encoded);
146}
147
148}  // namespace
149