Moved ByteArray up to core/common now that it has no dependencies on qrcode/encoder.
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / EncoderTestCase.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 import junit.framework.TestCase;
20 import com.google.zxing.common.ByteArray;
21
22 /**
23  * @author satorux@google.com (Satoru Takabayashi) - creator
24  * @author mysen@google.com (Chris Mysen) - ported from C++
25  */
26 public final class EncoderTestCase extends TestCase {
27
28   public void testGetAlphanumericCode() {
29     // The first ten code points are numbers.
30     for (int i = 0; i < 10; ++i) {
31       assertEquals(i, Encoder.GetAlphanumericCode('0' + i));
32     }
33
34     // The next 26 code points are capital alphabet letters.
35     for (int i = 10; i < 36; ++i) {
36       assertEquals(i, Encoder.GetAlphanumericCode('A' + i - 10));
37     }
38
39     // Others are symbol letters
40     assertEquals(36, Encoder.GetAlphanumericCode(' '));
41     assertEquals(37, Encoder.GetAlphanumericCode('$'));
42     assertEquals(38, Encoder.GetAlphanumericCode('%'));
43     assertEquals(39, Encoder.GetAlphanumericCode('*'));
44     assertEquals(40, Encoder.GetAlphanumericCode('+'));
45     assertEquals(41, Encoder.GetAlphanumericCode('-'));
46     assertEquals(42, Encoder.GetAlphanumericCode('.'));
47     assertEquals(43, Encoder.GetAlphanumericCode('/'));
48     assertEquals(44, Encoder.GetAlphanumericCode(':'));
49
50     // Should return -1 for other letters;
51     assertEquals(-1, Encoder.GetAlphanumericCode('a'));
52     assertEquals(-1, Encoder.GetAlphanumericCode('#'));
53     assertEquals(-1, Encoder.GetAlphanumericCode('\0'));
54   }
55
56   public void testChooseMode() {
57     // Numeric mode.
58     assertEquals(QRCode.MODE_NUMERIC, Encoder.ChooseMode(new ByteArray("0")));
59     assertEquals(QRCode.MODE_NUMERIC, Encoder.ChooseMode(new ByteArray("0123456789")));
60     // Alphanumeric mode.
61     assertEquals(QRCode.MODE_ALPHANUMERIC, Encoder.ChooseMode(new ByteArray("A")));
62     assertEquals(QRCode.MODE_ALPHANUMERIC,
63         Encoder.ChooseMode(new ByteArray("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")));
64     // 8-bit byte mode.
65     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("a")));
66     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("#")));
67     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("")));
68     // Kanji mode.  We used to use MODE_KANJI for these, but we stopped
69     // doing that as we cannot distinguish Shift_JIS from other encodings
70     // from data bytes alone.  See also comments in qrcode_encoder.h.
71
72     // AIUE in Hiragana in Shift_JIS
73     byte[] dat1 = {0x8,0xa,0x8,0xa,0x8,0xa,0x8,(byte)0xa6};
74     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat1)));
75
76     // Nihon in Kanji in Shift_JIS.
77     byte[] dat2 = {0x9,0xf,0x9,0x7b};
78     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat2)));
79
80     // Sou-Utsu-Byou in Kanji in Shift_JIS.
81     byte[] dat3 = {0xe,0x4,0x9,0x5,0x9,0x61};
82     assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray(dat3)));
83   }
84
85   public void testEncode() {
86     QRCode qr_code = new QRCode();
87     assertTrue(Encoder.Encode(new ByteArray("ABCDEF"), QRCode.EC_LEVEL_H, qr_code));
88     // The following is a valid QR Code that can be read by cell phones.
89     String expected =
90       "<<\n" +
91       " mode: ALPHANUMERIC\n" +
92       " ec_level: H\n" +
93       " version: 1\n" +
94       " matrix_width: 21\n" +
95       " mask_pattern: 0\n" +
96       " num_total_bytes_: 26\n" +
97       " num_data_bytes: 9\n" +
98       " num_ec_bytes: 17\n" +
99       " num_rs_blocks: 1\n" +
100       " matrix:\n" +
101       " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
102       " 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 1\n" +
103       " 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n" +
104       " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1\n" +
105       " 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1\n" +
106       " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1\n" +
107       " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
108       " 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n" +
109       " 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1\n" +
110       " 1 0 1 1 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0\n" +
111       " 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0\n" +
112       " 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0\n" +
113       " 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0\n" +
114       " 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0\n" +
115       " 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1\n" +
116       " 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 1\n" +
117       " 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1\n" +
118       " 1 0 1 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0\n" +
119       " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1\n" +
120       " 1 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1\n" +
121       " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1\n" +
122       ">>\n";
123     assertEquals(expected, qr_code.toString());
124   }
125
126   public void testAppendModeInfo() {
127     BitVector bits = new BitVector();
128     assertTrue(Encoder.AppendModeInfo(QRCode.MODE_NUMERIC, bits));
129     assertEquals("0001", bits.toString());
130   }
131
132   public void testAppendLengthInfo() {
133     {
134       BitVector bits = new BitVector();
135       assertTrue(Encoder.AppendLengthInfo(1,  // 1 letter (1/1).
136                                                   1,  // version 1.
137                                                   QRCode.MODE_NUMERIC,
138                                                   bits));
139       assertEquals("0000000001", bits.toString());  // 10 bits.
140     }
141     {
142       BitVector bits = new BitVector();
143       assertTrue(Encoder.AppendLengthInfo(2,  // 2 letters (2/1).
144                                                   10,  // version 10.
145                                                   QRCode.MODE_ALPHANUMERIC,
146                                                   bits));
147       assertEquals("00000000010", bits.toString());  // 11 bits.
148     }
149     {
150       BitVector bits = new BitVector();
151       assertTrue(Encoder.AppendLengthInfo(255,  // 255 letter (255/1).
152                                                   27,  // version 27.
153                                                   QRCode.MODE_8BIT_BYTE,
154                                                   bits));
155       assertEquals("0000000011111111", bits.toString());  // 16 bits.
156     }
157     {
158       BitVector bits = new BitVector();
159       assertTrue(Encoder.AppendLengthInfo(1024,  // 512 letters (1024/2).
160                                                   40,  // version 40.
161                                                   QRCode.MODE_KANJI,
162                                                   bits));
163       assertEquals("001000000000", bits.toString());  // 12 bits.
164     }
165   }
166
167   public void testAppendBytes() {
168     {
169       // Should use AppendNumericBytes.
170       // 1 = 01 = 0001 in 4 bits.
171       BitVector bits = new BitVector();
172       assertTrue(Encoder.AppendBytes(new ByteArray("1"), QRCode.MODE_NUMERIC, bits));
173       assertEquals("0001" , bits.toString());
174       // 'A' cannot be encoded in MODE_NUMERIC.
175       assertFalse(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_NUMERIC, bits));
176     }
177     {
178       // Should use AppendAlphanumericBytes.
179       // A = 10 = 0xa = 001010 in 6 bits
180       BitVector bits = new BitVector();
181       assertTrue(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_ALPHANUMERIC,
182                                              bits));
183       assertEquals("001010" , bits.toString());
184       // Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
185       assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_ALPHANUMERIC,
186                                               bits));
187     }
188     {
189       // Should use Append8BitBytes.
190       // 0x61, 0x62, 0x63
191       BitVector bits = new BitVector();
192       assertTrue(Encoder.AppendBytes(new ByteArray("abc"), QRCode.MODE_8BIT_BYTE,
193                                              bits));
194       assertEquals("01100001" + "01100010" + "01100011", bits.toString());
195       // Anything can be encoded in QRCode.MODE_8BIT_BYTE.
196       byte[] bytes = {0x00};
197       assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_8BIT_BYTE,
198                                              bits));
199     }
200     {
201       // Should use AppendKanjiBytes.
202       // 0x93, 0x5f
203       BitVector bits = new BitVector();
204       byte[] bytes = {(byte)0x93,0x5f};
205       assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_KANJI, bits));
206       assertEquals("0110110011111", bits.toString());
207       // ASCII characters can not be encoded in QRCode.MODE_KANJI.
208       assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_KANJI, bits));
209     }
210   }
211
212   public void testInit() {
213     // TODO: should be implemented.
214   }
215
216   public void testTerminateBits() {
217     {
218       BitVector v = new BitVector();
219       assertTrue(Encoder.TerminateBits(0, v));
220       assertEquals("", v.toString());
221     }
222     {
223       BitVector v = new BitVector();
224       assertTrue(Encoder.TerminateBits(1, v));
225       assertEquals("00000000", v.toString());
226     }
227     {
228       BitVector v = new BitVector();
229       v.AppendBits(0, 3);  // Append 000
230       assertTrue(Encoder.TerminateBits(1, v));
231       assertEquals("00000000", v.toString());
232     }
233     {
234       BitVector v = new BitVector();
235       v.AppendBits(0, 5);  // Append 00000
236       assertTrue(Encoder.TerminateBits(1, v));
237       assertEquals("00000000", v.toString());
238     }
239     {
240       BitVector v = new BitVector();
241       v.AppendBits(0, 8);  // Append 00000000
242       assertTrue(Encoder.TerminateBits(1, v));
243       assertEquals("00000000", v.toString());
244     }
245     {
246       BitVector v = new BitVector();
247       assertTrue(Encoder.TerminateBits(2, v));
248       assertEquals("0000000011101100", v.toString());
249     }
250     {
251       BitVector v = new BitVector();
252       v.AppendBits(0, 1);  // Append 0
253       assertTrue(Encoder.TerminateBits(3, v));
254       assertEquals("000000001110110000010001", v.toString());
255     }
256   }
257
258   public void testGetNumDataBytesAndNumECBytesForBlockID() {
259     int[] num_data_bytes = new int[0];
260     int[] num_ec_bytes = new int[0];
261     // Version 1-H.
262     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
263         26, 9, 1, 0, num_data_bytes, num_ec_bytes);
264     assertEquals(9, num_data_bytes[0]);
265     assertEquals(17, num_ec_bytes[0]);
266
267     // Version 3-H.  2 blocks.
268     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
269         70, 26, 2, 0, num_data_bytes, num_ec_bytes);
270     assertEquals(13, num_data_bytes[0]);
271     assertEquals(22, num_ec_bytes[0]);
272     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
273         70, 26, 2, 1, num_data_bytes, num_ec_bytes);
274     assertEquals(13, num_data_bytes[0]);
275     assertEquals(22, num_ec_bytes[0]);
276
277     // Version 7-H. (4 + 1) blocks.
278     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
279         196, 66, 5, 0, num_data_bytes, num_ec_bytes);
280     assertEquals(13, num_data_bytes[0]);
281     assertEquals(26, num_ec_bytes[0]);
282     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
283         196, 66, 5, 4, num_data_bytes, num_ec_bytes);
284     assertEquals(14, num_data_bytes[0]);
285     assertEquals(26, num_ec_bytes[0]);
286
287     // Version 40-H. (20 + 61) blocks.
288     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
289         3706, 1276, 81, 0, num_data_bytes, num_ec_bytes);
290     assertEquals(15, num_data_bytes[0]);
291     assertEquals(30, num_ec_bytes[0]);
292     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
293         3706, 1276, 81, 20, num_data_bytes, num_ec_bytes);
294     assertEquals(16, num_data_bytes[0]);
295     assertEquals(30, num_ec_bytes[0]);
296     Encoder.GetNumDataBytesAndNumECBytesForBlockID(
297         3706, 1276, 81, 80, num_data_bytes, num_ec_bytes);
298     assertEquals(16, num_data_bytes[0]);
299     assertEquals(30, num_ec_bytes[0]);
300   }
301
302   public void testInterleaveWithECBytes() {
303     {
304       final char[] data_bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236};
305       BitVector in = new BitVector();
306       for (char data_byte: data_bytes) {
307         in.AppendBits(data_byte, 8);
308       }
309       BitVector out = new BitVector();
310       assertTrue(Encoder.InterleaveWithECBytes(in, 26, 9, 1, out));
311       final char[] expected = {
312           // Data bytes.
313           32, 65, 205, 69, 41, 220, 46, 128, 236,
314           // Error correction bytes.
315           42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224,
316           96, 74, 219, 61,
317       };
318       assertEquals(new String(expected), out.toString());
319     }
320     // Numbers are from http://www.swetake.com/qr/qr8.html
321     {
322       final char[] data_bytes = {
323           67, 70, 22, 38, 54, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214,
324           230, 247, 7, 23, 39, 55, 71, 87, 103, 119, 135, 151, 166, 22, 38,
325           54, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 247, 7, 23, 39,
326           55, 71, 87, 103, 119, 135, 151, 160, 236, 17, 236, 17, 236, 17, 236, 17,
327       };
328       BitVector in = new BitVector();
329       for (char data_byte: data_bytes) {
330         in.AppendBits(data_byte, 8);
331       }
332       BitVector out = new BitVector();
333       assertTrue(Encoder.InterleaveWithECBytes(in, 134, 62, 4, out));
334       final char[] expected = {
335           // Data bytes.
336           67, 230, 54, 55, 70, 247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54,
337           39, 118, 119, 70, 55, 134, 135, 86, 71, 150, 151, 102, 87, 166, 160,
338           118, 103, 182, 236, 134, 119, 198, 17, 150, 135, 214, 236, 166, 151,
339           230, 17, 182, 166, 247, 236, 198, 22, 7, 17, 214, 38, 23, 236, 39, 17,
340           // Error correction bytes.
341           175, 155, 245, 236, 80, 146, 56, 74, 155, 165, 133, 142, 64, 183, 132,
342           13, 178, 54, 132, 108, 45, 113, 53, 50, 214, 98, 193, 152, 233, 147, 50,
343           71, 65, 190, 82, 51, 209, 199, 171, 54, 12, 112, 57, 113, 155, 117, 211,
344           164, 117, 30, 158, 225, 31, 190, 242, 38, 140, 61, 179, 154, 214, 138,
345           147, 87, 27, 96, 77, 47, 187, 49, 156, 214,
346       };
347       assertEquals(new String(expected), out.toString());
348     }
349   }
350
351   public void testAppendNumericBytes() {
352     {
353       // 1 = 01 = 0001 in 4 bits.
354       BitVector bits = new BitVector();
355       assertTrue(Encoder.AppendNumericBytes(new ByteArray("1"), bits));
356       assertEquals("0001" , bits.toString());
357     }
358     {
359       // 12 = 0xc = 0001100 in 7 bits.
360       BitVector bits = new BitVector();
361       assertTrue(Encoder.AppendNumericBytes(new ByteArray("12"), bits));
362       assertEquals("0001100" , bits.toString());
363     }
364     {
365       // 123 = 0x7b = 0001111011 in 10 bits.
366       BitVector bits = new BitVector();
367       assertTrue(Encoder.AppendNumericBytes(new ByteArray("123"), bits));
368       assertEquals("0001111011" , bits.toString());
369     }
370     {
371       // 1234 = "123" + "4" = 0001111011 + 0100
372       BitVector bits = new BitVector();
373       assertTrue(Encoder.AppendNumericBytes(new ByteArray("1234"), bits));
374       assertEquals("0001111011" + "0100" , bits.toString());
375     }
376     {
377       // Empty.
378       BitVector bits = new BitVector();
379       assertTrue(Encoder.AppendNumericBytes(new ByteArray(""), bits));
380       assertEquals("" , bits.toString());
381     }
382     {
383       // Invalid data.
384       BitVector bits = new BitVector();
385       assertFalse(Encoder.AppendNumericBytes(new ByteArray("abc"), bits));
386     }
387   }
388
389   public void testAppendAlphanumericBytes() {
390     {
391       // A = 10 = 0xa = 001010 in 6 bits
392       BitVector bits = new BitVector();
393       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("A"), bits));
394       assertEquals("001010" , bits.toString());
395     }
396     {
397       // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
398       BitVector bits = new BitVector();
399       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("AB"), bits));
400       assertEquals("00111001101", bits.toString());
401     }
402     {
403       // ABC = "AB" + "C" = 00111001101 + 001100
404       BitVector bits = new BitVector();
405       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("ABC"), bits));
406       assertEquals("00111001101" + "001100" , bits.toString());
407     }
408     {
409       // Empty.
410       BitVector bits = new BitVector();
411       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray(""), bits));
412       assertEquals("" , bits.toString());
413     }
414     {
415       // Invalid data.
416       BitVector bits = new BitVector();
417       assertFalse(Encoder.AppendAlphanumericBytes(new ByteArray("abc"), bits));
418     }
419   }
420
421   public void testAppend8BitBytes() {
422     {
423       // 0x61, 0x62, 0x63
424       BitVector bits = new BitVector();
425       assertTrue(Encoder.Append8BitBytes(new ByteArray("abc"), bits));
426       assertEquals("01100001" + "01100010" + "01100011", bits.toString());
427     }
428     {
429       // Empty.
430       BitVector bits = new BitVector();
431       assertTrue(Encoder.Append8BitBytes(new ByteArray(""), bits));
432       assertEquals("", bits.toString());
433     }
434   }
435
436   // Numbers are from page 21 of JISX0510:2004
437   public void testAppendKanjiBytes() {
438     {
439       BitVector bits = new BitVector();
440       byte[] dat1 = {(byte)0x93,0x5f};
441       assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat1), bits));
442       assertEquals("0110110011111", bits.toString());
443       byte[] dat2 = {(byte)0xe4,(byte)0xaa};
444       assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat2), bits));
445       assertEquals("0110110011111" + "1101010101010", bits.toString());
446     }
447   }
448
449   // JAVAPORT: Uncomment and fix up with new Reed Solomon objects
450 //  static boolean ComparePoly(final int[] expected, final int size, final GF_Poly poly) {
451 //    if (size != poly.degree() + 1) {
452 //      return false;
453 //    }
454 //    for (int i = 0; i < size; ++i) {
455 //      // "expected" is ordered in a reverse order.  We reverse the coeff
456 //      // index for comparison.
457 //      final int coeff = GaloisField.GetField(8).Log(
458 //          poly.coeff(size - i - 1));
459 //      if (expected[i] != coeff) {
460 //        Debug.LOG_ERROR("values don't match at " + i + ": " +
461 //            expected[i] + " vs. " + coeff);
462 //        return false;
463 //      }
464 //    }
465 //    return true;
466 //  }
467 //
468 //  // Numbers are from Appendix A of JISX0510 2004 (p.59).
469 //  public void testGetECPoly() {
470 //    {
471 //      final GF_Poly poly = Encoder.GetECPoly(7);
472 //      final int[] expected = {0, 87, 229, 146, 149, 238, 102, 21};
473 //      assertTrue(ComparePoly(expected, expected.length, poly));
474 //    }
475 //    {
476 //      final GF_Poly poly = Encoder.GetECPoly(17);
477 //      final int[] expected = {
478 //          0, 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150,
479 //          39, 243, 163, 136
480 //      };
481 //      assertTrue(ComparePoly(expected, expected.length, poly));
482 //    }
483 //    {
484 //      final GF_Poly poly = Encoder.GetECPoly(34);
485 //      final int[] expected = {
486 //          0, 111, 77, 146, 94, 26, 21, 108, 19,
487 //          105, 94, 113, 193, 86, 140, 163, 125,
488 //          58,
489 //          158, 229, 239, 218, 103, 56, 70, 114,
490 //          61, 183, 129, 167, 13, 98, 62, 129, 51
491 //      };
492 //      assertTrue(ComparePoly(expected, expected.length, poly));
493 //    }
494 //    {
495 //      final GF_Poly poly = Encoder.GetECPoly(68);
496 //      final int[] expected = {
497 //          0, 247, 159, 223, 33, 224, 93, 77, 70,
498 //          90, 160, 32, 254, 43, 150, 84, 101,
499 //          190,
500 //          205, 133, 52, 60, 202, 165, 220, 203,
501 //          151, 93, 84, 15, 84, 253, 173, 160,
502 //          89, 227, 52, 199, 97, 95, 231, 52,
503 //          177, 41, 125, 137, 241, 166, 225, 118,
504 //          2, 54,
505 //          32, 82, 215, 175, 198, 43, 238, 235,
506 //          27, 101, 184, 127, 3, 5, 8, 163, 238
507 //      };
508 //      assertTrue(ComparePoly(expected, expected.length, poly));
509 //    }
510 //  }
511
512   // Numbers are from http://www.swetake.com/qr/qr3.html and
513   // http://www.swetake.com/qr/qr9.html
514   public void testGenerateECBytes() {
515     {
516       final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
517       ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 17);
518       final byte[] expected = {
519           42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
520           (byte)237, 85, (byte)224, 96, 74, (byte)219, 61
521       };
522       assertEquals(new ByteArray(expected), ec_bytes);
523     }
524     {
525       final byte[] data_bytes = {67, 70, 22, 38, 54, 70, 86, 102, 118,
526           (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
527       ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 18);
528       final byte[] expected = {
529           (byte)175, 80, (byte)155, 64, (byte)178, 45, (byte)214, (byte)233, 65, (byte)209, 12,
530           (byte)155, 117, 31, (byte)140, (byte)214, 27, (byte)187
531       };
532       assertEquals(new ByteArray(expected), ec_bytes);
533     }
534     {
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 byte[] expected = {
539           0, 3, (byte)130, (byte)179, (byte)194, 0, 55, (byte)211, 110, 79, 98, 72, (byte)170, 96,
540           (byte)211, (byte)137, (byte)213
541       };
542       assertEquals(new ByteArray(expected), ec_bytes);
543     }
544   }
545
546   public void testIsValidKanji() {
547     assertTrue(Encoder.IsValidKanji(0x82, 0xa0));  // Hiragana "A".
548     assertTrue(Encoder.IsValidKanji(0x93, 0xfa));  // Nichi in Kanji.
549     assertTrue(Encoder.IsValidKanji(0x8a, 0xbf));  // Kan in Kanji.
550     assertTrue(Encoder.IsValidKanji(0xe7, 0x4e));  // Sou in Kanji.
551     assertTrue(Encoder.IsValidKanji(0xea, 0xa2));  // Haruka in Kanji.
552
553     assertFalse(Encoder.IsValidKanji('0', '1'));
554     assertFalse(Encoder.IsValidKanji(0x82, 0x7f));
555     assertFalse(Encoder.IsValidKanji(0xa0, 0xa0));
556   }
557
558   public void testIsValidKanjiSequence() {
559     // AIUEO in Katakana
560     byte[] dat1 = {0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x49};
561     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat1)));
562     // 012345 in multi-byte letters.
563     byte[] dat2 = {0x8,0x4,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x54};
564     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat2)));
565     // Yoroshiku in Kanji.
566     byte[] dat3 = {0x9,0xe,0x9,0x4,0x8,0x8,0x8,(byte)0xea};
567     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat3)));
568     assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("0123")));
569     assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("ABC")));
570   }
571 }