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