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_OnedCode39Writer.h"
6
7 #include <cstring>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 // 3 wide and 6 narrow modules per char. 1 space between chars.
14 constexpr int MODULES_PER_CHAR = 3 * 3 + 6 + 1;
15
16 // '*' is added as the first and last char.
17 const int DELIMITER_CHARS = 2;
18
19 // Last char may serve as checksum.
20 const int CHECKSUM_CHARS = 1;
21
TEST(OnedCode39WriterTest,SetWideNarrowRatio)22 TEST(OnedCode39WriterTest, SetWideNarrowRatio) {
23 // Code 39 barcodes encode strings of any size into modules in a
24 // unidimensional disposition.
25 // Each module is either: a narrow bar, a narrow space, a wide
26 // bar, or a wide space. Accepted wide-to-narrow ratios are between 2 and 3.
27 // This writer in particular only takes integer ratios, so it's either 2 or 3.
28 CBC_OnedCode39Writer writer;
29 EXPECT_FALSE(writer.SetWideNarrowRatio(0));
30 EXPECT_FALSE(writer.SetWideNarrowRatio(1));
31 EXPECT_TRUE(writer.SetWideNarrowRatio(2));
32 EXPECT_TRUE(writer.SetWideNarrowRatio(3));
33 EXPECT_FALSE(writer.SetWideNarrowRatio(4));
34 EXPECT_FALSE(writer.SetWideNarrowRatio(100));
35
36 writer.SetWideNarrowRatio(3);
37
38 int32_t width;
39 int32_t height;
40 uint8_t* encoded;
41 const char* expected;
42
43 encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
44 expected =
45 "# # ### ### # " // * Start
46 "# ### ### # # " // P
47 "# # ### # ### " // D
48 "# ### ### # # " // F
49 "# ### # ### # " // I
50 "### # # # ### " // U
51 "### ### # # # " // M
52 "# # ### ### #"; // * End
53 for (size_t i = 0; i < strlen(expected); i++)
54 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
55 FX_Free(encoded);
56
57 writer.SetWideNarrowRatio(2);
58
59 encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
60 expected =
61 "# # ## ## # " // * Start
62 "# ## ## # # " // P
63 "# # ## # ## " // D
64 "# ## ## # # " // F
65 "# ## # ## # " // I
66 "## # # # ## " // U
67 "## ## # # # " // M
68 "# # ## ## #"; // * End
69 for (size_t i = 0; i < strlen(expected); i++)
70 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
71 FX_Free(encoded);
72 }
73
TEST(OnedCode39WriterTest,Encode)74 TEST(OnedCode39WriterTest, Encode) {
75 CBC_OnedCode39Writer writer;
76 int32_t width;
77 int32_t height;
78 uint8_t* encoded;
79 const char* expected;
80
81 encoded = writer.Encode("", BCFORMAT_CODE_39, width, height);
82 EXPECT_EQ(1, height);
83 EXPECT_EQ((0 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
84 expected =
85 "# # ### ### # " // * Start
86 "# # ### ### #"; // * End
87 for (size_t i = 0; i < strlen(expected); i++)
88 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
89 FX_Free(encoded);
90
91 encoded = writer.Encode("123", BCFORMAT_CODE_39, width, height);
92 EXPECT_EQ(1, height);
93 EXPECT_EQ((3 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
94 expected =
95 "# # ### ### # " // * Start
96 "### # # # ### " // 1
97 "# ### # # ### " // 2
98 "### ### # # # " // 3
99 "# # ### ### #"; // * End
100 for (size_t i = 0; i < strlen(expected); i++)
101 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
102 FX_Free(encoded);
103
104 encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
105 EXPECT_EQ(1, height);
106 EXPECT_EQ((6 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
107 expected =
108 "# # ### ### # " // * Start
109 "# ### ### # # " // P
110 "# # ### # ### " // D
111 "# ### ### # # " // F
112 "# ### # ### # " // I
113 "### # # # ### " // U
114 "### ### # # # " // M
115 "# # ### ### #"; // * End
116 for (size_t i = 0; i < strlen(expected); i++)
117 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
118 FX_Free(encoded);
119
120 encoded = writer.Encode("A -$%./+Z", BCFORMAT_CODE_39, width, height);
121 EXPECT_EQ(1, height);
122 EXPECT_EQ((9 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
123 expected =
124 "# # ### ### # " // * Start
125 "### # # # ### " // A
126 "# ### # ### # " // Space
127 "# # # ### ### " // -
128 "# # # # # " // $
129 "# # # # # " // %
130 "### # # ### # " // .
131 "# # # # # " // /
132 "# # # # # " // +
133 "# ### ### # # " // Z
134 "# # ### ### #"; // * End
135 for (size_t i = 0; i < strlen(expected); i++)
136 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
137 FX_Free(encoded);
138 }
139
TEST(OnedCode39WriterTest,Checksum)140 TEST(OnedCode39WriterTest, Checksum) {
141 CBC_OnedCode39Writer writer;
142 int32_t width;
143 int32_t height;
144 uint8_t* encoded;
145 const char* expected;
146
147 writer.SetCalcChecksum(true);
148
149 encoded = writer.Encode("123", BCFORMAT_CODE_39, width, height);
150 EXPECT_EQ(1, height);
151 EXPECT_EQ((3 + CHECKSUM_CHARS + DELIMITER_CHARS) * MODULES_PER_CHAR - 1,
152 width);
153 expected =
154 "# # ### ### # " // * Start
155 "### # # # ### " // 1 (1)
156 "# ### # # ### " // 2 (2)
157 "### ### # # # " // 3 (3)
158 "# ### ### # # " // 6 (6 = (1 + 2 + 3) % 43)
159 "# # ### ### #"; // * End
160 for (size_t i = 0; i < strlen(expected); i++)
161 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
162 FX_Free(encoded);
163
164 encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
165 EXPECT_EQ(1, height);
166 EXPECT_EQ((6 + CHECKSUM_CHARS + DELIMITER_CHARS) * MODULES_PER_CHAR - 1,
167 width);
168 expected =
169 "# # ### ### # " // * Start
170 "# ### ### # # " // P (25)
171 "# # ### # ### " // D (13)
172 "# ### ### # # " // F (15)
173 "# ### # ### # " // I (18)
174 "### # # # ### " // U (30)
175 "### ### # # # " // M (22)
176 "### # # ### # " // . (37 = (25 + 13 + 15 + 18 + 30 + 22) % 43)
177 "# # ### ### #"; // * End
178 for (size_t i = 0; i < strlen(expected); i++)
179 EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
180 FX_Free(encoded);
181 }
182
183 } // namespace
184