2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.qrcode.encoder;
19 import com.google.zxing.common.ByteArray;
20 import junit.framework.TestCase;
22 import java.util.Arrays;
25 * @author satorux@google.com (Satoru Takabayashi) - creator
26 * @author mysen@google.com (Chris Mysen) - ported from C++
28 public final class EncoderTestCase extends TestCase {
30 public void testGetAlphanumericCode() {
31 // The first ten code points are numbers.
32 for (int i = 0; i < 10; ++i) {
33 assertEquals(i, Encoder.GetAlphanumericCode('0' + i));
36 // The next 26 code points are capital alphabet letters.
37 for (int i = 10; i < 36; ++i) {
38 assertEquals(i, Encoder.GetAlphanumericCode('A' + i - 10));
41 // Others are symbol letters
42 assertEquals(36, Encoder.GetAlphanumericCode(' '));
43 assertEquals(37, Encoder.GetAlphanumericCode('$'));
44 assertEquals(38, Encoder.GetAlphanumericCode('%'));
45 assertEquals(39, Encoder.GetAlphanumericCode('*'));
46 assertEquals(40, Encoder.GetAlphanumericCode('+'));
47 assertEquals(41, Encoder.GetAlphanumericCode('-'));
48 assertEquals(42, Encoder.GetAlphanumericCode('.'));
49 assertEquals(43, Encoder.GetAlphanumericCode('/'));
50 assertEquals(44, Encoder.GetAlphanumericCode(':'));
52 // Should return -1 for other letters;
53 assertEquals(-1, Encoder.GetAlphanumericCode('a'));
54 assertEquals(-1, Encoder.GetAlphanumericCode('#'));
55 assertEquals(-1, Encoder.GetAlphanumericCode('\0'));
58 public void testChooseMode() {
60 assertEquals(QRCode.MODE_NUMERIC, Encoder.ChooseMode(new ByteArray("0")));
61 assertEquals(QRCode.MODE_NUMERIC, Encoder.ChooseMode(new ByteArray("0123456789")));
63 assertEquals(QRCode.MODE_ALPHANUMERIC, Encoder.ChooseMode(new ByteArray("A")));
64 assertEquals(QRCode.MODE_ALPHANUMERIC,
65 Encoder.ChooseMode(new ByteArray("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")));
67 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("a")));
68 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("#")));
69 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("")));
70 // Kanji mode. We used to use MODE_KANJI for these, but we stopped
71 // doing that as we cannot distinguish Shift_JIS from other encodings
72 // from data bytes alone. See also comments in qrcode_encoder.h.
74 // AIUE in Hiragana in Shift_JIS
75 byte[] dat1 = {0x8,0xa,0x8,0xa,0x8,0xa,0x8,(byte)0xa6};
76 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat1)));
78 // Nihon in Kanji in Shift_JIS.
79 byte[] dat2 = {0x9,0xf,0x9,0x7b};
80 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat2)));
82 // Sou-Utsu-Byou in Kanji in Shift_JIS.
83 byte[] dat3 = {0xe,0x4,0x9,0x5,0x9,0x61};
84 assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat3)));
87 public void testEncode() {
88 QRCode qr_code = new QRCode();
89 assertTrue(Encoder.Encode(new ByteArray("ABCDEF"), QRCode.EC_LEVEL_H, qr_code));
90 // The following is a valid QR Code that can be read by cell phones.
93 " mode: ALPHANUMERIC\n" +
96 " matrix_width: 21\n" +
97 " mask_pattern: 0\n" +
98 " num_total_bytes_: 26\n" +
99 " num_data_bytes: 9\n" +
100 " num_ec_bytes: 17\n" +
101 " num_rs_blocks: 1\n" +
103 " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
104 " 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 1\n" +
105 " 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n" +
106 " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1\n" +
107 " 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1\n" +
108 " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1\n" +
109 " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
110 " 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n" +
111 " 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1\n" +
112 " 1 0 1 1 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0\n" +
113 " 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0\n" +
114 " 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0\n" +
115 " 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0\n" +
116 " 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0\n" +
117 " 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1\n" +
118 " 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 1\n" +
119 " 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1\n" +
120 " 1 0 1 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0\n" +
121 " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1\n" +
122 " 1 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1\n" +
123 " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1\n" +
125 assertEquals(expected, qr_code.toString());
128 public void testAppendModeInfo() {
129 BitVector bits = new BitVector();
130 assertTrue(Encoder.AppendModeInfo(QRCode.MODE_NUMERIC, bits));
131 assertEquals("0001", bits.toString());
134 public void testAppendLengthInfo() {
136 BitVector bits = new BitVector();
137 assertTrue(Encoder.AppendLengthInfo(1, // 1 letter (1/1).
141 assertEquals("0000000001", bits.toString()); // 10 bits.
144 BitVector bits = new BitVector();
145 assertTrue(Encoder.AppendLengthInfo(2, // 2 letters (2/1).
147 QRCode.MODE_ALPHANUMERIC,
149 assertEquals("00000000010", bits.toString()); // 11 bits.
152 BitVector bits = new BitVector();
153 assertTrue(Encoder.AppendLengthInfo(255, // 255 letter (255/1).
155 QRCode.MODE_8BIT_BYTE,
157 assertEquals("0000000011111111", bits.toString()); // 16 bits.
160 BitVector bits = new BitVector();
161 assertTrue(Encoder.AppendLengthInfo(1024, // 512 letters (1024/2).
165 assertEquals("001000000000", bits.toString()); // 12 bits.
169 public void testAppendBytes() {
171 // Should use AppendNumericBytes.
172 // 1 = 01 = 0001 in 4 bits.
173 BitVector bits = new BitVector();
174 assertTrue(Encoder.AppendBytes(new ByteArray("1"), QRCode.MODE_NUMERIC, bits));
175 assertEquals("0001" , bits.toString());
176 // 'A' cannot be encoded in MODE_NUMERIC.
177 assertFalse(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_NUMERIC, bits));
180 // Should use AppendAlphanumericBytes.
181 // A = 10 = 0xa = 001010 in 6 bits
182 BitVector bits = new BitVector();
183 assertTrue(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_ALPHANUMERIC, bits));
184 assertEquals("001010" , bits.toString());
185 // Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
186 assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_ALPHANUMERIC, bits));
189 // Should use Append8BitBytes.
191 BitVector bits = new BitVector();
192 assertTrue(Encoder.AppendBytes(new ByteArray("abc"), QRCode.MODE_8BIT_BYTE, bits));
193 assertEquals("01100001" + "01100010" + "01100011", bits.toString());
194 // Anything can be encoded in QRCode.MODE_8BIT_BYTE.
195 byte[] bytes = {0x00};
196 assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_8BIT_BYTE, bits));
199 // Should use AppendKanjiBytes.
201 BitVector bits = new BitVector();
202 byte[] bytes = {(byte)0x93,0x5f};
203 assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_KANJI, bits));
204 assertEquals("0110110011111", bits.toString());
205 // ASCII characters can not be encoded in QRCode.MODE_KANJI.
206 assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_KANJI, bits));
210 public void testInit() {
211 // TODO: should be implemented.
214 public void testTerminateBits() {
216 BitVector v = new BitVector();
217 assertTrue(Encoder.TerminateBits(0, v));
218 assertEquals("", v.toString());
221 BitVector v = new BitVector();
222 assertTrue(Encoder.TerminateBits(1, v));
223 assertEquals("00000000", v.toString());
226 BitVector v = new BitVector();
227 v.AppendBits(0, 3); // Append 000
228 assertTrue(Encoder.TerminateBits(1, v));
229 assertEquals("00000000", v.toString());
232 BitVector v = new BitVector();
233 v.AppendBits(0, 5); // Append 00000
234 assertTrue(Encoder.TerminateBits(1, v));
235 assertEquals("00000000", v.toString());
238 BitVector v = new BitVector();
239 v.AppendBits(0, 8); // Append 00000000
240 assertTrue(Encoder.TerminateBits(1, v));
241 assertEquals("00000000", v.toString());
244 BitVector v = new BitVector();
245 assertTrue(Encoder.TerminateBits(2, v));
246 assertEquals("0000000011101100", v.toString());
249 BitVector v = new BitVector();
250 v.AppendBits(0, 1); // Append 0
251 assertTrue(Encoder.TerminateBits(3, v));
252 assertEquals("000000001110110000010001", v.toString());
256 public void testGetNumDataBytesAndNumECBytesForBlockID() {
257 int[] num_data_bytes = new int[1];
258 int[] num_ec_bytes = new int[1];
260 Encoder.GetNumDataBytesAndNumECBytesForBlockID(26, 9, 1, 0, num_data_bytes, num_ec_bytes);
261 assertEquals(9, num_data_bytes[0]);
262 assertEquals(17, num_ec_bytes[0]);
264 // Version 3-H. 2 blocks.
265 Encoder.GetNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 0, num_data_bytes, num_ec_bytes);
266 assertEquals(13, num_data_bytes[0]);
267 assertEquals(22, num_ec_bytes[0]);
268 Encoder.GetNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 1, num_data_bytes, num_ec_bytes);
269 assertEquals(13, num_data_bytes[0]);
270 assertEquals(22, num_ec_bytes[0]);
272 // Version 7-H. (4 + 1) blocks.
273 Encoder.GetNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 0, num_data_bytes, num_ec_bytes);
274 assertEquals(13, num_data_bytes[0]);
275 assertEquals(26, num_ec_bytes[0]);
276 Encoder.GetNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 4, num_data_bytes, num_ec_bytes);
277 assertEquals(14, num_data_bytes[0]);
278 assertEquals(26, num_ec_bytes[0]);
280 // Version 40-H. (20 + 61) blocks.
281 Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 0, num_data_bytes, num_ec_bytes);
282 assertEquals(15, num_data_bytes[0]);
283 assertEquals(30, num_ec_bytes[0]);
284 Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 20, num_data_bytes, num_ec_bytes);
285 assertEquals(16, num_data_bytes[0]);
286 assertEquals(30, num_ec_bytes[0]);
287 Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 80, num_data_bytes, num_ec_bytes);
288 assertEquals(16, num_data_bytes[0]);
289 assertEquals(30, num_ec_bytes[0]);
292 public void testInterleaveWithECBytes() {
294 final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
295 BitVector in = new BitVector();
296 for (byte data_byte: data_bytes) {
297 in.AppendBits(data_byte, 8);
299 BitVector out = new BitVector();
300 assertTrue(Encoder.InterleaveWithECBytes(in, 26, 9, 1, out));
301 final byte[] expected = {
303 32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236,
304 // Error correction bytes.
305 42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
306 (byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
308 assertTrue(Arrays.equals(expected, out.getArray()));
310 // Numbers are from http://www.swetake.com/qr/qr8.html
312 final byte[] data_bytes = {
313 67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182,
314 (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135,
315 (byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
316 (byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119,
317 (byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236,
320 BitVector in = new BitVector();
321 for (byte data_byte: data_bytes) {
322 in.AppendBits(data_byte, 8);
324 BitVector out = new BitVector();
325 assertTrue(Encoder.InterleaveWithECBytes(in, 134, 62, 4, out));
326 final byte[] expected = {
328 67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
329 118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166,
330 (byte)160, 118, 103, (byte)182, (byte)236, (byte)134, 119, (byte)198, 17, (byte)150,
331 (byte)135, (byte)214, (byte)236, (byte)166, (byte)151, (byte)230, 17, (byte)182,
332 (byte)166, (byte)247, (byte)236, (byte)198, 22, 7, 17, (byte)214, 38, 23, (byte)236, 39,
334 // Error correction bytes.
335 (byte)175, (byte)155, (byte)245, (byte)236, 80, (byte)146, 56, 74, (byte)155, (byte)165,
336 (byte)133, (byte)142, 64, (byte)183, (byte)132, 13, (byte)178, 54, (byte)132, 108, 45,
337 113, 53, 50, (byte)214, 98, (byte)193, (byte)152, (byte)233, (byte)147, 50, 71, 65,
338 (byte)190, 82, 51, (byte)209, (byte)199, (byte)171, 54, 12, 112, 57, 113, (byte)155, 117,
339 (byte)211, (byte)164, 117, 30, (byte)158, (byte)225, 31, (byte)190, (byte)242, 38,
340 (byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
341 (byte)187, 49, (byte)156, (byte)214,
343 assertTrue(Arrays.equals(expected, out.getArray()));
347 public void testAppendNumericBytes() {
349 // 1 = 01 = 0001 in 4 bits.
350 BitVector bits = new BitVector();
351 assertTrue(Encoder.AppendNumericBytes(new ByteArray("1"), bits));
352 assertEquals("0001" , bits.toString());
355 // 12 = 0xc = 0001100 in 7 bits.
356 BitVector bits = new BitVector();
357 assertTrue(Encoder.AppendNumericBytes(new ByteArray("12"), bits));
358 assertEquals("0001100" , bits.toString());
361 // 123 = 0x7b = 0001111011 in 10 bits.
362 BitVector bits = new BitVector();
363 assertTrue(Encoder.AppendNumericBytes(new ByteArray("123"), bits));
364 assertEquals("0001111011" , bits.toString());
367 // 1234 = "123" + "4" = 0001111011 + 0100
368 BitVector bits = new BitVector();
369 assertTrue(Encoder.AppendNumericBytes(new ByteArray("1234"), bits));
370 assertEquals("0001111011" + "0100" , bits.toString());
374 BitVector bits = new BitVector();
375 assertTrue(Encoder.AppendNumericBytes(new ByteArray(""), bits));
376 assertEquals("" , bits.toString());
380 BitVector bits = new BitVector();
381 assertFalse(Encoder.AppendNumericBytes(new ByteArray("abc"), bits));
385 public void testAppendAlphanumericBytes() {
387 // A = 10 = 0xa = 001010 in 6 bits
388 BitVector bits = new BitVector();
389 assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("A"), bits));
390 assertEquals("001010" , bits.toString());
393 // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
394 BitVector bits = new BitVector();
395 assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("AB"), bits));
396 assertEquals("00111001101", bits.toString());
399 // ABC = "AB" + "C" = 00111001101 + 001100
400 BitVector bits = new BitVector();
401 assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("ABC"), bits));
402 assertEquals("00111001101" + "001100" , bits.toString());
406 BitVector bits = new BitVector();
407 assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray(""), bits));
408 assertEquals("" , bits.toString());
412 BitVector bits = new BitVector();
413 assertFalse(Encoder.AppendAlphanumericBytes(new ByteArray("abc"), bits));
417 public void testAppend8BitBytes() {
420 BitVector bits = new BitVector();
421 assertTrue(Encoder.Append8BitBytes(new ByteArray("abc"), bits));
422 assertEquals("01100001" + "01100010" + "01100011", bits.toString());
426 BitVector bits = new BitVector();
427 assertTrue(Encoder.Append8BitBytes(new ByteArray(""), bits));
428 assertEquals("", bits.toString());
432 // Numbers are from page 21 of JISX0510:2004
433 public void testAppendKanjiBytes() {
435 BitVector bits = new BitVector();
436 byte[] dat1 = {(byte)0x93,0x5f};
437 assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat1), bits));
438 assertEquals("0110110011111", bits.toString());
439 byte[] dat2 = {(byte)0xe4,(byte)0xaa};
440 assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat2), bits));
441 assertEquals("0110110011111" + "1101010101010", bits.toString());
445 // JAVAPORT: Uncomment and fix up with new Reed Solomon objects
446 // static boolean ComparePoly(final int[] expected, final int size, final GF_Poly poly) {
447 // if (size != poly.degree() + 1) {
450 // for (int i = 0; i < size; ++i) {
451 // // "expected" is ordered in a reverse order. We reverse the coeff
452 // // index for comparison.
453 // final int coeff = GaloisField.GetField(8).Log(
454 // poly.coeff(size - i - 1));
455 // if (expected[i] != coeff) {
456 // Debug.LOG_ERROR("values don't match at " + i + ": " +
457 // expected[i] + " vs. " + coeff);
464 // // Numbers are from Appendix A of JISX0510 2004 (p.59).
465 // public void testGetECPoly() {
467 // final GF_Poly poly = Encoder.GetECPoly(7);
468 // final int[] expected = {0, 87, 229, 146, 149, 238, 102, 21};
469 // assertTrue(ComparePoly(expected, expected.length, poly));
472 // final GF_Poly poly = Encoder.GetECPoly(17);
473 // final int[] expected = {
474 // 0, 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150,
477 // assertTrue(ComparePoly(expected, expected.length, poly));
480 // final GF_Poly poly = Encoder.GetECPoly(34);
481 // final int[] expected = {
482 // 0, 111, 77, 146, 94, 26, 21, 108, 19,
483 // 105, 94, 113, 193, 86, 140, 163, 125,
485 // 158, 229, 239, 218, 103, 56, 70, 114,
486 // 61, 183, 129, 167, 13, 98, 62, 129, 51
488 // assertTrue(ComparePoly(expected, expected.length, poly));
491 // final GF_Poly poly = Encoder.GetECPoly(68);
492 // final int[] expected = {
493 // 0, 247, 159, 223, 33, 224, 93, 77, 70,
494 // 90, 160, 32, 254, 43, 150, 84, 101,
496 // 205, 133, 52, 60, 202, 165, 220, 203,
497 // 151, 93, 84, 15, 84, 253, 173, 160,
498 // 89, 227, 52, 199, 97, 95, 231, 52,
499 // 177, 41, 125, 137, 241, 166, 225, 118,
501 // 32, 82, 215, 175, 198, 43, 238, 235,
502 // 27, 101, 184, 127, 3, 5, 8, 163, 238
504 // assertTrue(ComparePoly(expected, expected.length, poly));
508 // Numbers are from http://www.swetake.com/qr/qr3.html and
509 // http://www.swetake.com/qr/qr9.html
510 public void testGenerateECBytes() {
512 final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
513 ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 17);
514 final int[] expected = {
515 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61
517 assertEquals(expected.length, ec_bytes.size());
518 for (int x = 0; x < expected.length; x++) {
519 assertEquals(expected[x], ec_bytes.at(x));
523 final byte[] data_bytes = {67, 70, 22, 38, 54, 70, 86, 102, 118,
524 (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
525 ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 18);
526 final int[] expected = {
527 175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187
529 assertEquals(expected.length, ec_bytes.size());
530 for (int x = 0; x < expected.length; x++) {
531 assertEquals(expected[x], ec_bytes.at(x));
535 // High-order zero cofficient case.
536 final byte[] data_bytes = {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17};
537 ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 17);
538 final int[] expected = {
539 0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213
541 assertEquals(expected.length, ec_bytes.size());
542 for (int x = 0; x < expected.length; x++) {
543 assertEquals(expected[x], ec_bytes.at(x));
548 public void testIsValidKanji() {
549 assertTrue(Encoder.IsValidKanji(0x82, 0xa0)); // Hiragana "A".
550 assertTrue(Encoder.IsValidKanji(0x93, 0xfa)); // Nichi in Kanji.
551 assertTrue(Encoder.IsValidKanji(0x8a, 0xbf)); // Kan in Kanji.
552 assertTrue(Encoder.IsValidKanji(0xe7, 0x4e)); // Sou in Kanji.
553 assertTrue(Encoder.IsValidKanji(0xea, 0xa2)); // Haruka in Kanji.
555 assertFalse(Encoder.IsValidKanji('0', '1'));
556 assertFalse(Encoder.IsValidKanji(0x82, 0x7f));
557 assertFalse(Encoder.IsValidKanji(0xa0, 0xa0));
560 public void testIsValidKanjiSequence() {
562 byte[] dat1 = {0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x49};
563 assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat1)));
564 // 012345 in multi-byte letters.
565 byte[] dat2 = {0x8,0x4,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x54};
566 assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat2)));
567 // Yoroshiku in Kanji.
568 byte[] dat3 = {0x9,0xe,0x9,0x4,0x8,0x8,0x8,(byte)0xea};
569 assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat3)));
570 assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("0123")));
571 assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("ABC")));