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