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