1 // Copyright 2017 The PDFium Authors
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 <string.h>
8
9 #include "core/fxcrt/compiler_specific.h"
10 #include "core/fxcrt/data_vector.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
TEST(OnedCode39WriterTest,SetWideNarrowRatio)15 TEST(OnedCode39WriterTest, SetWideNarrowRatio) {
16 // Code 39 barcodes encode strings of any size into modules in a
17 // unidimensional disposition.
18 // Each module is either: a narrow bar, a narrow space, a wide
19 // bar, or a wide space. Accepted wide-to-narrow ratios are between 2 and 3.
20 // This writer in particular only takes integer ratios, so it's either 2 or 3.
21 CBC_OnedCode39Writer writer;
22 EXPECT_FALSE(writer.SetWideNarrowRatio(0));
23 EXPECT_FALSE(writer.SetWideNarrowRatio(1));
24 EXPECT_TRUE(writer.SetWideNarrowRatio(2));
25 EXPECT_TRUE(writer.SetWideNarrowRatio(3));
26 EXPECT_FALSE(writer.SetWideNarrowRatio(4));
27 EXPECT_FALSE(writer.SetWideNarrowRatio(100));
28
29 writer.SetWideNarrowRatio(3);
30
31 static const char kExpected1[] =
32 "# # ### ### # " // * Start
33 "# ### ### # # " // P
34 "# # ### # ### " // D
35 "# ### ### # # " // F
36 "# ### # ### # " // I
37 "### # # # ### " // U
38 "### ### # # # " // M
39 "# # ### ### #"; // * End
40 DataVector<uint8_t> encoded = writer.Encode("PDFIUM");
41 ASSERT_EQ(strlen(kExpected1), encoded.size());
42 for (size_t i = 0; i < strlen(kExpected1); i++) {
43 UNSAFE_TODO(EXPECT_EQ(kExpected1[i] != ' ', !!encoded[i])) << i;
44 }
45 writer.SetWideNarrowRatio(2);
46
47 static const char kExpected2[] =
48 "# # ## ## # " // * Start
49 "# ## ## # # " // P
50 "# # ## # ## " // D
51 "# ## ## # # " // F
52 "# ## # ## # " // I
53 "## # # # ## " // U
54 "## ## # # # " // M
55 "# # ## ## #"; // * End
56 encoded = writer.Encode("PDFIUM");
57 ASSERT_EQ(strlen(kExpected2), encoded.size());
58 for (size_t i = 0; i < strlen(kExpected2); i++) {
59 UNSAFE_TODO(EXPECT_EQ(kExpected2[i] != ' ', !!encoded[i])) << i;
60 }
61 }
62
TEST(OnedCode39WriterTest,Encode)63 TEST(OnedCode39WriterTest, Encode) {
64 CBC_OnedCode39Writer writer;
65
66 static const char kExpected1[] =
67 "# # ### ### # " // * Start
68 "# # ### ### #"; // * End
69 DataVector<uint8_t> encoded = writer.Encode("");
70 ASSERT_EQ(strlen(kExpected1), encoded.size());
71 for (size_t i = 0; i < strlen(kExpected1); i++) {
72 UNSAFE_TODO(EXPECT_EQ(kExpected1[i] != ' ', !!encoded[i])) << i;
73 }
74
75 static const char kExpected2[] =
76 "# # ### ### # " // * Start
77 "### # # # ### " // 1
78 "# ### # # ### " // 2
79 "### ### # # # " // 3
80 "# # ### ### #"; // * End
81 encoded = writer.Encode("123");
82 ASSERT_EQ(strlen(kExpected2), encoded.size());
83 for (size_t i = 0; i < strlen(kExpected2); i++) {
84 UNSAFE_TODO(EXPECT_EQ(kExpected2[i] != ' ', !!encoded[i])) << i;
85 }
86
87 static const char kExpected3[] =
88 "# # ### ### # " // * Start
89 "# ### ### # # " // P
90 "# # ### # ### " // D
91 "# ### ### # # " // F
92 "# ### # ### # " // I
93 "### # # # ### " // U
94 "### ### # # # " // M
95 "# # ### ### #"; // * End
96 encoded = writer.Encode("PDFIUM");
97 ASSERT_EQ(strlen(kExpected3), encoded.size());
98 for (size_t i = 0; i < strlen(kExpected3); i++) {
99 UNSAFE_TODO(EXPECT_EQ(kExpected3[i] != ' ', !!encoded[i])) << i;
100 }
101
102 static const char kExpected4[] =
103 "# # ### ### # " // * Start
104 "### # # # ### " // A
105 "# ### # ### # " // Space
106 "# # # ### ### " // -
107 "# # # # # " // $
108 "# # # # # " // %
109 "### # # ### # " // .
110 "# # # # # " // /
111 "# # # # # " // +
112 "# ### ### # # " // Z
113 "# # ### ### #"; // * End
114 encoded = writer.Encode("A -$%./+Z");
115 ASSERT_EQ(strlen(kExpected4), encoded.size());
116 for (size_t i = 0; i < strlen(kExpected4); i++) {
117 UNSAFE_TODO(EXPECT_EQ(kExpected4[i] != ' ', !!encoded[i])) << i;
118 }
119 }
120
TEST(OnedCode39WriterTest,Checksum)121 TEST(OnedCode39WriterTest, Checksum) {
122 CBC_OnedCode39Writer writer;
123 writer.SetCalcChecksum(true);
124
125 static const char kExpected1[] =
126 "# # ### ### # " // * Start
127 "### # # # ### " // 1 (1)
128 "# ### # # ### " // 2 (2)
129 "### ### # # # " // 3 (3)
130 "# ### ### # # " // 6 (6 = (1 + 2 + 3) % 43)
131 "# # ### ### #"; // * End
132 DataVector<uint8_t> encoded = writer.Encode("123");
133 ASSERT_EQ(strlen(kExpected1), encoded.size());
134 for (size_t i = 0; i < strlen(kExpected1); i++)
135 UNSAFE_TODO(EXPECT_EQ(kExpected1[i] != ' ', !!encoded[i])) << i;
136
137 static const char kExpected2[] =
138 "# # ### ### # " // * Start
139 "# ### ### # # " // P (25)
140 "# # ### # ### " // D (13)
141 "# ### ### # # " // F (15)
142 "# ### # ### # " // I (18)
143 "### # # # ### " // U (30)
144 "### ### # # # " // M (22)
145 "### # # ### # " // . (37 = (25 + 13 + 15 + 18 + 30 + 22) % 43)
146 "# # ### ### #"; // * End
147 encoded = writer.Encode("PDFIUM");
148 ASSERT_EQ(strlen(kExpected2), encoded.size());
149 for (size_t i = 0; i < strlen(kExpected2); i++) {
150 UNSAFE_TODO(EXPECT_EQ(kExpected2[i] != ' ', !!encoded[i])) << i;
151 }
152 }
153
154 } // namespace
155