xref: /aosp_15_r20/external/pdfium/fxbarcode/pdf417/BC_PDF417HighLevelEncoder_unittest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 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/pdf417/BC_PDF417HighLevelEncoder.h"
6 
7 #include <vector>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
TEST(PDF417HighLevelEncoderTest,EncodeHighLevel)11 TEST(PDF417HighLevelEncoderTest, EncodeHighLevel) {
12   static constexpr struct EncodeHighLevelCase {
13     const wchar_t* input;
14     const wchar_t* expected;
15     int expected_length;
16   } kEncodeHighLevelCases[] = {
17       // Empty string encodes as empty string.
18       {L"", L"", 0},
19 
20       // Binary mode with digit.
21       {L"\x000b\x000b\x0030", L"\x0385\x000b\x000b\x0030", 4},
22 
23       // Text mode.
24       {L"xxxxxxXx", L"\x0384\x0341\x02c9\x02c9\x02cd\x02c9", 6},
25 
26       // Text mode with punctuation.
27       {L"xxxxxx!x", L"\x0384\x0341\x02c9\x02c9\x02cf\x0143", 6},
28 
29       // Text mode with mixed submode.
30       {L"xxxxxx0x", L"\x0384\x0341\x02c9\x02c9\x02ce\x001b\x02cf", 7},
31 
32       // Text mode with mixed submode, and space in alpha submode.
33       {L"xxxxxx0X ", L"\x0384\x0341\x02c9\x02c9\x02ce\x001c\x02cc", 7},
34 
35       // Text mode to binary mode.
36       {L"xxxxxx\x0b", L"\x0384\x0341\x02c9\x02c9\x02cf\x0391\x0385\x000b", 8},
37 
38       // 13 consecutive digits triggers numeric encoding.
39       {L"0000000000000", L"\x0386\x000f\x00d9\x017b\x000b\x0064", 6},
40   };
41 
42   for (size_t i = 0; i < std::size(kEncodeHighLevelCases); ++i) {
43     const EncodeHighLevelCase& testcase = kEncodeHighLevelCases[i];
44     WideStringView input(testcase.input);
45     WideString expected(testcase.expected, testcase.expected_length);
46     absl::optional<WideString> result =
47         CBC_PDF417HighLevelEncoder::EncodeHighLevel(input);
48     ASSERT_TRUE(result.has_value());
49     EXPECT_EQ(expected, result.value()) << " for case number " << i;
50   }
51 }
52 
TEST(PDF417HighLevelEncoderTest,EncodeText)53 TEST(PDF417HighLevelEncoderTest, EncodeText) {
54   // TODO(tsepez): implement test cases.
55 }
56 
TEST(PDF417HighLevelEncoderTest,EncodeBinary)57 TEST(PDF417HighLevelEncoderTest, EncodeBinary) {
58   static constexpr struct EncodeBinaryCase {
59     const char* input;
60     int offset;
61     int count;
62     CBC_PDF417HighLevelEncoder::EncodingMode startmode;
63     const wchar_t* expected;
64     int expected_length;
65   } kEncodeBinaryCases[] = {
66       // Empty string encodes as empty string.
67       {"", 0, 0, CBC_PDF417HighLevelEncoder::EncodingMode::kText, L"", 0},
68 
69       // Single digit encodes with a shift-to byte.
70       {"x", 0, 1, CBC_PDF417HighLevelEncoder::EncodingMode::kText,
71        L"\x0391\x0385x", 3},
72 
73       // Fewer than 6 characters encodes as prefix without compaction.
74       {"xxxxx", 0, 5, CBC_PDF417HighLevelEncoder::EncodingMode::kText,
75        L"\x0385xxxxx", 6},
76 
77       // 6 charcters triggers text encoding compaction.
78       {"xxxxxx", 0, 6, CBC_PDF417HighLevelEncoder::EncodingMode::kText,
79        L"\u039c\u00c9\u031f\u012a\u00d2\u02d0", 6},
80 
81       // Same result if initially in numeric compaction mode.
82       {"xxxxxx", 0, 6, CBC_PDF417HighLevelEncoder::EncodingMode::kNumeric,
83        L"\u039c\u00c9\u031f\u012a\u00d2\u02d0", 6},
84   };
85 
86   for (size_t i = 0; i < std::size(kEncodeBinaryCases); ++i) {
87     const EncodeBinaryCase& testcase = kEncodeBinaryCases[i];
88     std::vector<uint8_t> input_array;
89     size_t input_length = strlen(testcase.input);
90     input_array.resize(input_length);
91     for (size_t j = 0; j < input_length; ++j) {
92       input_array[j] = testcase.input[j];
93     }
94     WideString expected(testcase.expected, testcase.expected_length);
95     WideString result;
96     CBC_PDF417HighLevelEncoder::EncodeBinary(input_array, testcase.offset,
97                                              testcase.count, testcase.startmode,
98                                              &result);
99     EXPECT_EQ(expected, result) << " for case number " << i;
100   }
101 }
102 
TEST(PDF417HighLevelEncoderTest,EncodeNumeric)103 TEST(PDF417HighLevelEncoderTest, EncodeNumeric) {
104   static constexpr struct EncodeNumericCase {
105     const wchar_t* input;
106     int offset;
107     int count;
108     const wchar_t* expected;
109     int expected_length;
110   } kEncodeNumericCases[] = {
111       // Empty string encodes as empty string.
112       {L"", 0, 0, L"", 0},
113 
114       // Single 0 should encode as 10 base-900 == a.
115       {L"0", 0, 1, L"\x000a", 1},
116 
117       // 800 should encode as 1800 base-900 == 2,0.
118       {L"800", 0, 3, L"\x0002\x0000", 2},
119 
120       // Test longer strings and sub-strings.
121       {L"123456", 0, 6, L"\x0001\x015c\x0100", 3},
122       {L"123456", 0, 5, L"\x007c\x02e9", 2},
123       {L"123456", 1, 5, L"\x0089\x009c", 2},
124       {L"123456", 2, 2, L"\x0086", 1},
125 
126       // Up to 44 characters encodes as 15 base-900 words.
127       {L"00000000000000000000000000000000000000000000", 0, 44,
128        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
129        L"\x0090\x020b\x019b\x0064",
130        15},
131 
132       // 45 characters should encode as same 15 words followed by one additional
133       // word.
134       {L"000000000000000000000000000000000000000000000", 0, 45,
135        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
136        L"\x0090\x020b\x019b\x0064\x000a",
137        16},
138 
139       // 44 characters followed by 800 should encode as 15 words followed by
140       // 1800 base-900 == 2,0.
141       {L"00000000000000000000000000000000000000000000800", 0, 47,
142        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
143        L"\x0090\x020b\x019b\x0064\x0002\x0000",
144        17},
145 
146       // Even longer input.
147       {L"10000000000000000000000000000000000000000000000000", 0, 50,
148        L"\x01e0\x02f0\x036d\x02ad\x029c\x01ea\x0011\x000b\x02d6\x023c\x0108"
149        L"\x02bb\x0023\x02d2\x00c8\x0001\x00d3\x0064",
150        18},
151   };
152 
153   for (size_t i = 0; i < std::size(kEncodeNumericCases); ++i) {
154     const EncodeNumericCase& testcase = kEncodeNumericCases[i];
155     WideString input(testcase.input);
156     WideString expected(testcase.expected, testcase.expected_length);
157     WideString result;
158     CBC_PDF417HighLevelEncoder::EncodeNumeric(input, testcase.offset,
159                                               testcase.count, &result);
160     EXPECT_EQ(expected, result) << " for case number " << i;
161   }
162 }
163 
TEST(PDF417HighLevelEncoderTest,ConsecutiveDigitCount)164 TEST(PDF417HighLevelEncoderTest, ConsecutiveDigitCount) {
165   static constexpr struct ConsecutiveDigitCase {
166     const wchar_t* input;
167     int offset;
168     int expected_count;
169   } kConsecutiveDigitCases[] = {
170       // Empty string contains 0 consecutive digits.
171       {L"", 0, 0},
172 
173       // Single non-digit character contains 0 consecutive digits.
174       {L"X", 0, 0},
175 
176       // Leading non-digit followed by digits contains 0 consecutive.
177       {L"X123", 0, 0},
178 
179       // Single digit contains 1 consecutive digit.
180       {L"1", 0, 1},
181 
182       // Single digit followe by non-digit contains 1 consecutive digit.
183       {L"1Z", 0, 1},
184 
185       // Test longer strings.
186       {L"123FOO45678", 0, 3},
187 
188       // Test subtring starting in digits field.
189       {L"123FOO45678", 3, 0},
190 
191       // Test subtring starting in non-digits field.
192       {L"123FOO45678", 3, 0},
193 
194       // Test substring starting in digits field following non-digit field.
195       {L"123FOO45678", 6, 5},
196   };
197 
198   for (size_t i = 0; i < std::size(kConsecutiveDigitCases); ++i) {
199     const ConsecutiveDigitCase& testcase = kConsecutiveDigitCases[i];
200     WideString input(testcase.input);
201     int actual_count =
202         CBC_PDF417HighLevelEncoder::DetermineConsecutiveDigitCount(
203             input, testcase.offset);
204     EXPECT_EQ(testcase.expected_count, actual_count)
205         << " for case number " << i;
206   }
207 }
208 
TEST(PDF417HighLevelEncoderTest,ConsecutiveTextCount)209 TEST(PDF417HighLevelEncoderTest, ConsecutiveTextCount) {
210   static constexpr struct ConsecutiveTextCase {
211     const wchar_t* input;
212     int offset;
213     int expected_count;
214   } kConsecutiveTextCases[] = {
215       // Empty string contains 0 consecutive text characters.
216       {L"", 0, 0},
217 
218       // Single text character is 1 consecutive text characters.
219       {L"X", 0, 1},
220 
221       // Trailing numbers count as text characters.
222       {L"X123", 0, 4},
223 
224       // Leading numbers count as text characters.
225       {L"123X", 0, 4},
226 
227       // Embedded lo-value binary characters terminate text runs.
228       {L"ABC\x0001XXXX", 0, 3},
229 
230       // Embedded hi-value binary characters terminate text runs.
231       {L"ABC\x0100XXXX", 0, 3},
232 
233       // Text run still found after indexing past lo-value character.
234       {L"ABC\x0001XXXX", 4, 4},
235 
236       // Text run still found after indexing past hi-value character.
237       {L"ABC\x0100XXXX", 4, 4},
238 
239       // Leading hi-value character results in 0 consecutive characters.
240       {L"\x0100XXX", 0, 0},
241 
242       // Up to 12 numbers count as text.
243       {L"123456789012", 0, 12},
244 
245       // 13 or more numbers are compresssed using numeric compression, not text.
246       {L"1234567890123", 0, 0},
247 
248       // Leading Text character doesn't affect the 12 character case.
249       {L"X123456789012", 0, 13},
250 
251       // Leading Text character doesn't affect the 13 character case.
252       {L"X1234567890123", 0, 1},
253 
254       // Jumping between numbers and letters works properly.
255       {L"XXX121XXX12345678901234", 0, 9},
256   };
257 
258   for (size_t i = 0; i < std::size(kConsecutiveTextCases); ++i) {
259     const ConsecutiveTextCase& testcase = kConsecutiveTextCases[i];
260     WideString input(testcase.input);
261     int actual_count =
262         CBC_PDF417HighLevelEncoder::DetermineConsecutiveTextCount(
263             input, testcase.offset);
264     EXPECT_EQ(testcase.expected_count, actual_count)
265         << " for case number " << i;
266   }
267 }
268 
TEST(PDF417HighLevelEncoderTest,ConsecutiveBinaryCount)269 TEST(PDF417HighLevelEncoderTest, ConsecutiveBinaryCount) {
270   // TODO(tsepez): implement test cases.
271 }
272