1 // Copyright 2014 PDFium Authors. All rights reserved.
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 "testing/gtest/include/gtest/gtest.h"
6 #include "core/include/fxcrt/fx_basic.h"
7 #include "testing/fx_string_testhelpers.h"
8 #include "BC_PDF417HighLevelEncoder.h"
9 
TEST(PDF417HighLevelEncoder,EncodeHighLevel)10 TEST(PDF417HighLevelEncoder, EncodeHighLevel) {
11   // TODO(tsepez): implement test cases.
12 }
13 
TEST(PDF417HighLevelEncoder,EncodeText)14 TEST(PDF417HighLevelEncoder, EncodeText) {
15   // TODO(tsepez): implement test cases.
16 }
17 
TEST(PDF417HighLevelEncoder,EncodeBinary)18 TEST(PDF417HighLevelEncoder, EncodeBinary) {
19   struct EncodeBinaryCase {
20     const char* input;
21     int offset;
22     int count;
23     int startmode;
24     const wchar_t* expected;
25     int expected_length;
26   } encode_binary_cases[] = {
27       // Empty string encodes as empty string.
28       {"", 0, 0, CBC_PDF417HighLevelEncoder::TEXT_COMPACTION, L"", 0},
29 
30       // Fewer than 6 characters encodes as prefix without compaction.
31       {"xxxxx", 0, 5, CBC_PDF417HighLevelEncoder::TEXT_COMPACTION,
32        L"\x0385xxxxx", 6},
33 
34       // 6 charcters triggerst text encoding compaction.
35       {"xxxxxx", 0, 6, CBC_PDF417HighLevelEncoder::TEXT_COMPACTION,
36        L"\u039c\u00c9\u031f\u012a\u00d2\u02d0", 6},
37 
38       // Same result if initially in numeric compaction mode.
39       {"xxxxxx", 0, 6, CBC_PDF417HighLevelEncoder::NUMERIC_COMPACTION,
40        L"\u039c\u00c9\u031f\u012a\u00d2\u02d0", 6},
41   };
42 
43   CBC_PDF417HighLevelEncoder::Initialize();
44   for (size_t i = 0; i < FX_ArraySize(encode_binary_cases); ++i) {
45     EncodeBinaryCase* ptr = &encode_binary_cases[i];
46     CFX_ByteArray input_array;
47     size_t input_length = strlen(ptr->input);
48     input_array.SetSize(input_length);
49     for (size_t j = 0; j < input_length; ++j) {
50       input_array.SetAt(j, ptr->input[j]);
51     }
52     CFX_WideString expected(ptr->expected, ptr->expected_length);
53     CFX_WideString result;
54     CBC_PDF417HighLevelEncoder::encodeBinary(
55         &input_array, ptr->offset, ptr->count, ptr->startmode, result);
56     EXPECT_EQ(expected, result) << " for case number " << i;
57   }
58   CBC_PDF417HighLevelEncoder::Finalize();
59 }
60 
TEST(PDF417HighLevelEncoder,EncodeNumeric)61 TEST(PDF417HighLevelEncoder, EncodeNumeric) {
62   struct EncodeNumericCase {
63     const wchar_t* input;
64     int offset;
65     int count;
66     const wchar_t* expected;
67     int expected_length;
68   } encode_numeric_cases[] = {
69       // Empty string encodes as empty string.
70       {L"", 0, 0, L"", 0},
71 
72       // Single 0 should encode as 10 base-900 == a.
73       {L"0", 0, 1, L"\x000a", 1},
74 
75       // 800 should encode as 1800 base-900 == 2,0.
76       {L"800", 0, 3, L"\x0002\x0000", 2},
77 
78       // Test longer strings and sub-strings.
79       {L"123456", 0, 6, L"\x0001\x015c\x0100", 3},
80       {L"123456", 0, 5, L"\x007c\x02e9", 2},
81       {L"123456", 1, 5, L"\x0089\x009c", 2},
82       {L"123456", 2, 2, L"\x0086", 1},
83 
84       // Up to 44 characters encodes as 15 base-900 words.
85       {L"00000000000000000000000000000000000000000000",
86        0,
87        44,
88        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
89        L"\x0090\x020b\x019b\x0064",
90        15},
91 
92       // 45 characters should encode as same 15 words followed by one additional
93       // word.
94       {L"000000000000000000000000000000000000000000000",
95        0,
96        45,
97        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
98        L"\x0090\x020b\x019b\x0064\x000a",
99        16},
100 
101       // 44 characters followed by 800 should encode as 15 words followed by
102       // 1800 base-900 == 2,0.
103       {L"00000000000000000000000000000000000000000000800",
104        0,
105        47,
106        L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
107        L"\x0090\x020b\x019b\x0064\x0002\x0000",
108        17},
109 
110       // Even longer input.
111       {L"10000000000000000000000000000000000000000000000000",
112        0,
113        50,
114        L"\x01e0\x02f0\x036d\x02ad\x029c\x01ea\x0011\x000b\x02d6\x023c\x0108"
115        L"\x02bb\x0023\x02d2\x00c8\x0001\x00d3\x0064",
116        18},
117   };
118 
119   CBC_PDF417HighLevelEncoder::Initialize();
120   for (size_t i = 0; i < FX_ArraySize(encode_numeric_cases); ++i) {
121     EncodeNumericCase* ptr = &encode_numeric_cases[i];
122     CFX_WideString input(ptr->input);
123     CFX_WideString expected(ptr->expected, ptr->expected_length);
124     CFX_WideString result;
125     CBC_PDF417HighLevelEncoder::encodeNumeric(input, ptr->offset, ptr->count,
126                                               result);
127     EXPECT_EQ(expected, result) << " for case number " << i;
128   }
129   CBC_PDF417HighLevelEncoder::Finalize();
130 }
131 
TEST(PDF417HighLevelEncoder,ConsecutiveDigitCount)132 TEST(PDF417HighLevelEncoder, ConsecutiveDigitCount) {
133   struct ConsecutiveDigitCase {
134     const wchar_t* input;
135     int offset;
136     int expected_count;
137   } consecutive_digit_cases[] = {
138       // Empty string contains 0 consecuitve digits.
139       {L"", 0, 0},
140 
141       // Single non-digit character contains 0 consecutive digits.
142       {L"X", 0, 0},
143 
144       // Leading non-digit followed by digits contains 0 consecutive.
145       {L"X123", 0, 0},
146 
147       // Single digit contains 1 consecutive digit.
148       {L"1", 0, 1},
149 
150       // Single digit followe by non-digit contains 1 consecutive digit.
151       {L"1Z", 0, 1},
152 
153       // Test longer strings.
154       {L"123FOO45678", 0, 3},
155 
156       // Test subtring starting in digits field.
157       {L"123FOO45678", 3, 0},
158 
159       // Test subtring starting in non-digits field.
160       {L"123FOO45678", 3, 0},
161 
162       // Test substring starting in digits field following non-digit field.
163       {L"123FOO45678", 6, 5},
164   };
165 
166   CBC_PDF417HighLevelEncoder::Initialize();
167   for (size_t i = 0; i < FX_ArraySize(consecutive_digit_cases); ++i) {
168     ConsecutiveDigitCase* ptr = &consecutive_digit_cases[i];
169     CFX_WideString input(ptr->input);
170     int actual_count =
171         CBC_PDF417HighLevelEncoder::determineConsecutiveDigitCount(input,
172                                                                    ptr->offset);
173     EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i;
174   }
175   CBC_PDF417HighLevelEncoder::Finalize();
176 }
177 
TEST(PDF417HighLevelEncoder,ConsecutiveTextCount)178 TEST(PDF417HighLevelEncoder, ConsecutiveTextCount) {
179   struct ConsecutiveTextCase {
180     const wchar_t* input;
181     int offset;
182     int expected_count;
183   } consecutive_text_cases[] = {
184       // Empty string contains 0 consecutive text characters.
185       {L"", 0, 0},
186 
187       // Single text character is 1 consecutive text characters.
188       {L"X", 0, 1},
189 
190       // Trailing numbers count as text characters.
191       {L"X123", 0, 4},
192 
193       // Leading numbers count as text characters.
194       {L"123X", 0, 4},
195 
196       // Embedded lo-value binary characters terminate text runs.
197       {L"ABC\x0001XXXX", 0, 3},
198 
199       // Embedded hi-value binary characters terminate text runs.
200       {L"ABC\x0100XXXX", 0, 3},
201 
202       // Text run still found after indexing past lo-value character.
203       {L"ABC\x0001XXXX", 4, 4},
204 
205       // Text run still found after indexing past hi-value character.
206       {L"ABC\x0100XXXX", 4, 4},
207 
208       // Leading hi-value character results in 0 consecutive characters.
209       {L"\x0100XXX", 0, 0},
210 
211       // Up to 12 numbers count as text.
212       {L"123456789012", 0, 12},
213 
214       // 13 or more numbers are compresssed using numeric compression, not text.
215       {L"1234567890123", 0, 0},
216 
217       // Leading Text character doesn't affect the 12 character case.
218       {L"X123456789012", 0, 13},
219 
220       // Leading Text character doesn't affect the 13 character case.
221       {L"X1234567890123", 0, 1},
222 
223       // Jumping between numbers and letters works properly.
224       {L"XXX121XXX12345678901234", 0, 9},
225   };
226 
227   CBC_PDF417HighLevelEncoder::Initialize();
228   for (size_t i = 0; i < FX_ArraySize(consecutive_text_cases); ++i) {
229     ConsecutiveTextCase* ptr = &consecutive_text_cases[i];
230     CFX_WideString input(ptr->input);
231     int actual_count =
232         CBC_PDF417HighLevelEncoder::determineConsecutiveTextCount(input,
233                                                                   ptr->offset);
234     EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i;
235   }
236   CBC_PDF417HighLevelEncoder::Finalize();
237 }
238 
TEST(PDF417HighLevelEncoder,ConsecutiveBinaryCount)239 TEST(PDF417HighLevelEncoder, ConsecutiveBinaryCount) {}
240