BC_PDF417HighLevelEncoder_unittest.cpp revision 33357cad1fd1321a2b38d2963e2585f27ce980a2
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 "core/fxcrt/fx_basic.h"
6#include "testing/fx_string_testhelpers.h"
7#include "testing/gtest/include/gtest/gtest.h"
8#include "xfa/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h"
9
10TEST(PDF417HighLevelEncoder, EncodeHighLevel) {
11  // TODO(tsepez): implement test cases.
12}
13
14TEST(PDF417HighLevelEncoder, EncodeText) {
15  // TODO(tsepez): implement test cases.
16}
17
18TEST(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_ArrayTemplate<uint8_t> 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
61TEST(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", 0, 44,
86       L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
87       L"\x0090\x020b\x019b\x0064",
88       15},
89
90      // 45 characters should encode as same 15 words followed by one additional
91      // word.
92      {L"000000000000000000000000000000000000000000000", 0, 45,
93       L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
94       L"\x0090\x020b\x019b\x0064\x000a",
95       16},
96
97      // 44 characters followed by 800 should encode as 15 words followed by
98      // 1800 base-900 == 2,0.
99      {L"00000000000000000000000000000000000000000000800", 0, 47,
100       L"\x01b5\x006f\x02cc\x0084\x01bc\x0076\x00b3\x005c\x01f0\x034f\x01e6"
101       L"\x0090\x020b\x019b\x0064\x0002\x0000",
102       17},
103
104      // Even longer input.
105      {L"10000000000000000000000000000000000000000000000000", 0, 50,
106       L"\x01e0\x02f0\x036d\x02ad\x029c\x01ea\x0011\x000b\x02d6\x023c\x0108"
107       L"\x02bb\x0023\x02d2\x00c8\x0001\x00d3\x0064",
108       18},
109  };
110
111  CBC_PDF417HighLevelEncoder::Initialize();
112  for (size_t i = 0; i < FX_ArraySize(encode_numeric_cases); ++i) {
113    EncodeNumericCase* ptr = &encode_numeric_cases[i];
114    CFX_WideString input(ptr->input);
115    CFX_WideString expected(ptr->expected, ptr->expected_length);
116    CFX_WideString result;
117    CBC_PDF417HighLevelEncoder::encodeNumeric(input, ptr->offset, ptr->count,
118                                              result);
119    EXPECT_EQ(expected, result) << " for case number " << i;
120  }
121  CBC_PDF417HighLevelEncoder::Finalize();
122}
123
124TEST(PDF417HighLevelEncoder, ConsecutiveDigitCount) {
125  struct ConsecutiveDigitCase {
126    const wchar_t* input;
127    int offset;
128    int expected_count;
129  } consecutive_digit_cases[] = {
130      // Empty string contains 0 consecuitve digits.
131      {L"", 0, 0},
132
133      // Single non-digit character contains 0 consecutive digits.
134      {L"X", 0, 0},
135
136      // Leading non-digit followed by digits contains 0 consecutive.
137      {L"X123", 0, 0},
138
139      // Single digit contains 1 consecutive digit.
140      {L"1", 0, 1},
141
142      // Single digit followe by non-digit contains 1 consecutive digit.
143      {L"1Z", 0, 1},
144
145      // Test longer strings.
146      {L"123FOO45678", 0, 3},
147
148      // Test subtring starting in digits field.
149      {L"123FOO45678", 3, 0},
150
151      // Test subtring starting in non-digits field.
152      {L"123FOO45678", 3, 0},
153
154      // Test substring starting in digits field following non-digit field.
155      {L"123FOO45678", 6, 5},
156  };
157
158  CBC_PDF417HighLevelEncoder::Initialize();
159  for (size_t i = 0; i < FX_ArraySize(consecutive_digit_cases); ++i) {
160    ConsecutiveDigitCase* ptr = &consecutive_digit_cases[i];
161    CFX_WideString input(ptr->input);
162    int actual_count =
163        CBC_PDF417HighLevelEncoder::determineConsecutiveDigitCount(input,
164                                                                   ptr->offset);
165    EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i;
166  }
167  CBC_PDF417HighLevelEncoder::Finalize();
168}
169
170TEST(PDF417HighLevelEncoder, ConsecutiveTextCount) {
171  struct ConsecutiveTextCase {
172    const wchar_t* input;
173    int offset;
174    int expected_count;
175  } consecutive_text_cases[] = {
176      // Empty string contains 0 consecutive text characters.
177      {L"", 0, 0},
178
179      // Single text character is 1 consecutive text characters.
180      {L"X", 0, 1},
181
182      // Trailing numbers count as text characters.
183      {L"X123", 0, 4},
184
185      // Leading numbers count as text characters.
186      {L"123X", 0, 4},
187
188      // Embedded lo-value binary characters terminate text runs.
189      {L"ABC\x0001XXXX", 0, 3},
190
191      // Embedded hi-value binary characters terminate text runs.
192      {L"ABC\x0100XXXX", 0, 3},
193
194      // Text run still found after indexing past lo-value character.
195      {L"ABC\x0001XXXX", 4, 4},
196
197      // Text run still found after indexing past hi-value character.
198      {L"ABC\x0100XXXX", 4, 4},
199
200      // Leading hi-value character results in 0 consecutive characters.
201      {L"\x0100XXX", 0, 0},
202
203      // Up to 12 numbers count as text.
204      {L"123456789012", 0, 12},
205
206      // 13 or more numbers are compresssed using numeric compression, not text.
207      {L"1234567890123", 0, 0},
208
209      // Leading Text character doesn't affect the 12 character case.
210      {L"X123456789012", 0, 13},
211
212      // Leading Text character doesn't affect the 13 character case.
213      {L"X1234567890123", 0, 1},
214
215      // Jumping between numbers and letters works properly.
216      {L"XXX121XXX12345678901234", 0, 9},
217  };
218
219  CBC_PDF417HighLevelEncoder::Initialize();
220  for (size_t i = 0; i < FX_ArraySize(consecutive_text_cases); ++i) {
221    ConsecutiveTextCase* ptr = &consecutive_text_cases[i];
222    CFX_WideString input(ptr->input);
223    int actual_count =
224        CBC_PDF417HighLevelEncoder::determineConsecutiveTextCount(input,
225                                                                  ptr->offset);
226    EXPECT_EQ(ptr->expected_count, actual_count) << " for case number " << i;
227  }
228  CBC_PDF417HighLevelEncoder::Finalize();
229}
230
231TEST(PDF417HighLevelEncoder, ConsecutiveBinaryCount) {}
232