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