xref: /aosp_15_r20/external/pdfium/fxbarcode/oned/BC_OnedCode128Writer_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_OnedCode128Writer.h"
6 
7 #include <iterator>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace {
12 
13 struct TestCase {
14   const char* input;
15   int32_t checksum;
16   int32_t patterns[7];
17   size_t num_patterns;
18 };
19 
TEST(OnedCode128WriterTest,Encode128B)20 TEST(OnedCode128WriterTest, Encode128B) {
21   char buf[100];
22   static const TestCase kTestCases[] = {
23       {"", 104, {104}, 1},
24       {"a", 169, {104, 65}, 2},
25       {"1", 121, {104, 17}, 2},
26       {"a1", 203, {104, 65, 17}, 3},
27       {"ab", 301, {104, 65, 66}, 3},
28       {"12", 157, {104, 17, 18}, 3},
29       {"abc", 502, {104, 65, 66, 67}, 4},
30       {"123", 214, {104, 17, 18, 19}, 4},
31       {"abc123", 774, {104, 65, 66, 67, 17, 18, 19}, 7},
32       {"ABC123", 582, {104, 33, 34, 35, 17, 18, 19}, 7},
33       {"321ABC", 722, {104, 19, 18, 17, 33, 34, 35}, 7},
34       {"XYZ", 448, {104, 56, 57, 58}, 4},
35   };
36   for (size_t i = 0; i < std::size(kTestCases); ++i) {
37     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
38     SCOPED_TRACE(buf);
39     const TestCase& test_case = kTestCases[i];
40     std::vector<int32_t> patterns;
41     int32_t checksum =
42         CBC_OnedCode128Writer::Encode128B(test_case.input, &patterns);
43     EXPECT_EQ(test_case.checksum, checksum);
44     ASSERT_EQ(test_case.num_patterns, patterns.size());
45     for (size_t j = 0; j < patterns.size(); ++j) {
46       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
47       SCOPED_TRACE(buf);
48       EXPECT_EQ(test_case.patterns[j], patterns[j]);
49     }
50   }
51 }
52 
TEST(OnedCode128WriterTest,Encode128C)53 TEST(OnedCode128WriterTest, Encode128C) {
54   char buf[100];
55   static const TestCase kTestCases[] = {
56       {"", 105, {105}, 1},
57       {"a", 202, {105, 97}, 2},
58       {"1", 106, {105, 1}, 2},
59       {"a1", 204, {105, 97, 1}, 3},
60       {"ab", 398, {105, 97, 98}, 3},
61       {"12", 117, {105, 12}, 2},
62       {"abc", 695, {105, 97, 98, 99}, 4},
63       {"123", 123, {105, 12, 3}, 3},
64       {"abc123", 758, {105, 97, 98, 99, 12, 3}, 6},
65       {"ABC123", 566, {105, 65, 66, 67, 12, 3}, 6},
66       {"321ABC", 933, {105, 32, 1, 65, 66, 67}, 6},
67       {"XYZ", 641, {105, 88, 89, 90}, 4},
68   };
69   for (size_t i = 0; i < std::size(kTestCases); ++i) {
70     FXSYS_snprintf(buf, sizeof(buf) - 1, "Test case %zu", i);
71     SCOPED_TRACE(buf);
72     const TestCase& test_case = kTestCases[i];
73     std::vector<int32_t> patterns;
74     int32_t checksum =
75         CBC_OnedCode128Writer::Encode128C(test_case.input, &patterns);
76     EXPECT_EQ(test_case.checksum, checksum);
77     ASSERT_EQ(test_case.num_patterns, patterns.size());
78     for (size_t j = 0; j < patterns.size(); ++j) {
79       FXSYS_snprintf(buf, sizeof(buf) - 1, "Comparison %zu", j);
80       SCOPED_TRACE(buf);
81       EXPECT_EQ(test_case.patterns[j], patterns[j]);
82     }
83   }
84 }
85 
TEST(OnedCode128WriterTest,CheckContentValidity)86 TEST(OnedCode128WriterTest, CheckContentValidity) {
87   {
88     CBC_OnedCode128Writer writer(BC_TYPE::kCode128B);
89     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
90     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
91     EXPECT_FALSE(writer.CheckContentValidity(L""));
92     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
93     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
94     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
95     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
96   }
97   {
98     CBC_OnedCode128Writer writer(BC_TYPE::kCode128C);
99     EXPECT_TRUE(writer.CheckContentValidity(L"foo"));
100     EXPECT_TRUE(writer.CheckContentValidity(L"xyz"));
101     EXPECT_FALSE(writer.CheckContentValidity(L""));
102     EXPECT_FALSE(writer.CheckContentValidity(L"\""));
103     EXPECT_FALSE(writer.CheckContentValidity(L"f\x10oo"));
104     EXPECT_FALSE(writer.CheckContentValidity(L"bar\x7F"));
105     EXPECT_FALSE(writer.CheckContentValidity(L"qux\x88"));
106   }
107 }
108 
TEST(OnedCode128WriterTest,FilterContents)109 TEST(OnedCode128WriterTest, FilterContents) {
110   {
111     CBC_OnedCode128Writer writer(BC_TYPE::kCode128B);
112     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
113     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10").c_str());
114     EXPECT_STREQ(L"fool", writer.FilterContents(L"foo\x10l").c_str());
115     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F").c_str());
116     EXPECT_STREQ(L"foo", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
117     EXPECT_STREQ(L"bar", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
118   }
119   {
120     CBC_OnedCode128Writer writer(BC_TYPE::kCode128C);
121     EXPECT_STREQ(L"", writer.FilterContents(L"").c_str());
122     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10").c_str());
123     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10l").c_str());
124     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F").c_str());
125     EXPECT_STREQ(L"f", writer.FilterContents(L"foo\x10\x7F\x88").c_str());
126     EXPECT_STREQ(L"ba", writer.FilterContents(L"bar\x10\x7F\x88").c_str());
127   }
128 }
129 
130 }  // namespace
131