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