Fixed testIsValidKanjiSequence(). Looks like the data arrays got copied over wrong...
[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 com.google.zxing.common.ByteArray;
20 import junit.framework.TestCase;
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, 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, bits));
185     }
186     {
187       // Should use Append8BitBytes.
188       // 0x61, 0x62, 0x63
189       BitVector bits = new BitVector();
190       assertTrue(Encoder.AppendBytes(new ByteArray("abc"), QRCode.MODE_8BIT_BYTE, bits));
191       assertEquals("01100001" + "01100010" + "01100011", bits.toString());
192       // Anything can be encoded in QRCode.MODE_8BIT_BYTE.
193       byte[] bytes = {0x00};
194       assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_8BIT_BYTE, bits));
195     }
196     {
197       // Should use AppendKanjiBytes.
198       // 0x93, 0x5f
199       BitVector bits = new BitVector();
200       byte[] bytes = {(byte)0x93,0x5f};
201       assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_KANJI, bits));
202       assertEquals("0110110011111", bits.toString());
203       // ASCII characters can not be encoded in QRCode.MODE_KANJI.
204       assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_KANJI, bits));
205     }
206   }
207
208   public void testInit() {
209     // TODO: should be implemented.
210   }
211
212   public void testTerminateBits() {
213     {
214       BitVector v = new BitVector();
215       assertTrue(Encoder.TerminateBits(0, v));
216       assertEquals("", v.toString());
217     }
218     {
219       BitVector v = new BitVector();
220       assertTrue(Encoder.TerminateBits(1, v));
221       assertEquals("00000000", v.toString());
222     }
223     {
224       BitVector v = new BitVector();
225       v.AppendBits(0, 3);  // Append 000
226       assertTrue(Encoder.TerminateBits(1, v));
227       assertEquals("00000000", v.toString());
228     }
229     {
230       BitVector v = new BitVector();
231       v.AppendBits(0, 5);  // Append 00000
232       assertTrue(Encoder.TerminateBits(1, v));
233       assertEquals("00000000", v.toString());
234     }
235     {
236       BitVector v = new BitVector();
237       v.AppendBits(0, 8);  // Append 00000000
238       assertTrue(Encoder.TerminateBits(1, v));
239       assertEquals("00000000", v.toString());
240     }
241     {
242       BitVector v = new BitVector();
243       assertTrue(Encoder.TerminateBits(2, v));
244       assertEquals("0000000011101100", v.toString());
245     }
246     {
247       BitVector v = new BitVector();
248       v.AppendBits(0, 1);  // Append 0
249       assertTrue(Encoder.TerminateBits(3, v));
250       assertEquals("000000001110110000010001", v.toString());
251     }
252   }
253
254   public void testGetNumDataBytesAndNumECBytesForBlockID() {
255     int[] num_data_bytes = new int[1];
256     int[] num_ec_bytes = new int[1];
257     // Version 1-H.
258     Encoder.GetNumDataBytesAndNumECBytesForBlockID(26, 9, 1, 0, num_data_bytes, num_ec_bytes);
259     assertEquals(9, num_data_bytes[0]);
260     assertEquals(17, num_ec_bytes[0]);
261
262     // Version 3-H.  2 blocks.
263     Encoder.GetNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 0, num_data_bytes, num_ec_bytes);
264     assertEquals(13, num_data_bytes[0]);
265     assertEquals(22, num_ec_bytes[0]);
266     Encoder.GetNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 1, num_data_bytes, num_ec_bytes);
267     assertEquals(13, num_data_bytes[0]);
268     assertEquals(22, num_ec_bytes[0]);
269
270     // Version 7-H. (4 + 1) blocks.
271     Encoder.GetNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 0, num_data_bytes, num_ec_bytes);
272     assertEquals(13, num_data_bytes[0]);
273     assertEquals(26, num_ec_bytes[0]);
274     Encoder.GetNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 4, num_data_bytes, num_ec_bytes);
275     assertEquals(14, num_data_bytes[0]);
276     assertEquals(26, num_ec_bytes[0]);
277
278     // Version 40-H. (20 + 61) blocks.
279     Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 0, num_data_bytes, num_ec_bytes);
280     assertEquals(15, num_data_bytes[0]);
281     assertEquals(30, num_ec_bytes[0]);
282     Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 20, num_data_bytes, num_ec_bytes);
283     assertEquals(16, num_data_bytes[0]);
284     assertEquals(30, num_ec_bytes[0]);
285     Encoder.GetNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 80, num_data_bytes, num_ec_bytes);
286     assertEquals(16, num_data_bytes[0]);
287     assertEquals(30, num_ec_bytes[0]);
288   }
289
290   public void testInterleaveWithECBytes() {
291     {
292       final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
293       BitVector in = new BitVector();
294       for (byte data_byte: data_bytes) {
295         in.AppendBits(data_byte, 8);
296       }
297       BitVector out = new BitVector();
298       assertTrue(Encoder.InterleaveWithECBytes(in, 26, 9, 1, out));
299       final byte[] expected = {
300           // Data bytes.
301           32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236,
302           // Error correction bytes.
303           42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
304           (byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
305       };
306       assertEquals(expected.length, out.num_bytes());
307       final byte[] out_array = out.getArray();
308       // Can't use Arrays.equals(), because out_array may be longer than out.num_bytes()
309       for (int x = 0; x < expected.length; x++) {
310         assertEquals(expected[x], out_array[x]);
311       }
312     }
313     // Numbers are from http://www.swetake.com/qr/qr8.html
314     {
315       final byte[] data_bytes = {
316           67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182,
317           (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135,
318           (byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
319           (byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119,
320           (byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236,
321           17
322       };
323       BitVector in = new BitVector();
324       for (byte data_byte: data_bytes) {
325         in.AppendBits(data_byte, 8);
326       }
327       BitVector out = new BitVector();
328       assertTrue(Encoder.InterleaveWithECBytes(in, 134, 62, 4, out));
329       final byte[] expected = {
330           // Data bytes.
331           67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
332           118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166,
333           (byte)160, 118, 103, (byte)182, (byte)236, (byte)134, 119, (byte)198, 17, (byte)150,
334           (byte)135, (byte)214, (byte)236, (byte)166, (byte)151, (byte)230, 17, (byte)182,
335           (byte)166, (byte)247, (byte)236, (byte)198, 22, 7, 17, (byte)214, 38, 23, (byte)236, 39,
336           17,
337           // Error correction bytes.
338           (byte)175, (byte)155, (byte)245, (byte)236, 80, (byte)146, 56, 74, (byte)155, (byte)165,
339           (byte)133, (byte)142, 64, (byte)183, (byte)132, 13, (byte)178, 54, (byte)132, 108, 45,
340           113, 53, 50, (byte)214, 98, (byte)193, (byte)152, (byte)233, (byte)147, 50, 71, 65,
341           (byte)190, 82, 51, (byte)209, (byte)199, (byte)171, 54, 12, 112, 57, 113, (byte)155, 117,
342           (byte)211, (byte)164, 117, 30, (byte)158, (byte)225, 31, (byte)190, (byte)242, 38,
343           (byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
344           (byte)187, 49, (byte)156, (byte)214,
345       };
346       assertEquals(expected.length, out.num_bytes());
347       final byte[] out_array = out.getArray();
348       for (int x = 0; x < expected.length; x++) {
349         assertEquals(expected[x], out_array[x]);
350       }
351     }
352   }
353
354   public void testAppendNumericBytes() {
355     {
356       // 1 = 01 = 0001 in 4 bits.
357       BitVector bits = new BitVector();
358       assertTrue(Encoder.AppendNumericBytes(new ByteArray("1"), bits));
359       assertEquals("0001" , bits.toString());
360     }
361     {
362       // 12 = 0xc = 0001100 in 7 bits.
363       BitVector bits = new BitVector();
364       assertTrue(Encoder.AppendNumericBytes(new ByteArray("12"), bits));
365       assertEquals("0001100" , bits.toString());
366     }
367     {
368       // 123 = 0x7b = 0001111011 in 10 bits.
369       BitVector bits = new BitVector();
370       assertTrue(Encoder.AppendNumericBytes(new ByteArray("123"), bits));
371       assertEquals("0001111011" , bits.toString());
372     }
373     {
374       // 1234 = "123" + "4" = 0001111011 + 0100
375       BitVector bits = new BitVector();
376       assertTrue(Encoder.AppendNumericBytes(new ByteArray("1234"), bits));
377       assertEquals("0001111011" + "0100" , bits.toString());
378     }
379     {
380       // Empty.
381       BitVector bits = new BitVector();
382       assertTrue(Encoder.AppendNumericBytes(new ByteArray(""), bits));
383       assertEquals("" , bits.toString());
384     }
385     {
386       // Invalid data.
387       BitVector bits = new BitVector();
388       assertFalse(Encoder.AppendNumericBytes(new ByteArray("abc"), bits));
389     }
390   }
391
392   public void testAppendAlphanumericBytes() {
393     {
394       // A = 10 = 0xa = 001010 in 6 bits
395       BitVector bits = new BitVector();
396       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("A"), bits));
397       assertEquals("001010" , bits.toString());
398     }
399     {
400       // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
401       BitVector bits = new BitVector();
402       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("AB"), bits));
403       assertEquals("00111001101", bits.toString());
404     }
405     {
406       // ABC = "AB" + "C" = 00111001101 + 001100
407       BitVector bits = new BitVector();
408       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("ABC"), bits));
409       assertEquals("00111001101" + "001100" , bits.toString());
410     }
411     {
412       // Empty.
413       BitVector bits = new BitVector();
414       assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray(""), bits));
415       assertEquals("" , bits.toString());
416     }
417     {
418       // Invalid data.
419       BitVector bits = new BitVector();
420       assertFalse(Encoder.AppendAlphanumericBytes(new ByteArray("abc"), bits));
421     }
422   }
423
424   public void testAppend8BitBytes() {
425     {
426       // 0x61, 0x62, 0x63
427       BitVector bits = new BitVector();
428       assertTrue(Encoder.Append8BitBytes(new ByteArray("abc"), bits));
429       assertEquals("01100001" + "01100010" + "01100011", bits.toString());
430     }
431     {
432       // Empty.
433       BitVector bits = new BitVector();
434       assertTrue(Encoder.Append8BitBytes(new ByteArray(""), bits));
435       assertEquals("", bits.toString());
436     }
437   }
438
439   // Numbers are from page 21 of JISX0510:2004
440   public void testAppendKanjiBytes() {
441     {
442       BitVector bits = new BitVector();
443       byte[] dat1 = {(byte)0x93,0x5f};
444       assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat1), bits));
445       assertEquals("0110110011111", bits.toString());
446       byte[] dat2 = {(byte)0xe4,(byte)0xaa};
447       assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat2), bits));
448       assertEquals("0110110011111" + "1101010101010", bits.toString());
449     }
450   }
451
452   // JAVAPORT: Uncomment and fix up with new Reed Solomon objects
453 //  static boolean ComparePoly(final int[] expected, final int size, final GF_Poly poly) {
454 //    if (size != poly.degree() + 1) {
455 //      return false;
456 //    }
457 //    for (int i = 0; i < size; ++i) {
458 //      // "expected" is ordered in a reverse order.  We reverse the coeff
459 //      // index for comparison.
460 //      final int coeff = GaloisField.GetField(8).Log(
461 //          poly.coeff(size - i - 1));
462 //      if (expected[i] != coeff) {
463 //        Debug.LOG_ERROR("values don't match at " + i + ": " +
464 //            expected[i] + " vs. " + coeff);
465 //        return false;
466 //      }
467 //    }
468 //    return true;
469 //  }
470 //
471 //  // Numbers are from Appendix A of JISX0510 2004 (p.59).
472 //  public void testGetECPoly() {
473 //    {
474 //      final GF_Poly poly = Encoder.GetECPoly(7);
475 //      final int[] expected = {0, 87, 229, 146, 149, 238, 102, 21};
476 //      assertTrue(ComparePoly(expected, expected.length, poly));
477 //    }
478 //    {
479 //      final GF_Poly poly = Encoder.GetECPoly(17);
480 //      final int[] expected = {
481 //          0, 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150,
482 //          39, 243, 163, 136
483 //      };
484 //      assertTrue(ComparePoly(expected, expected.length, poly));
485 //    }
486 //    {
487 //      final GF_Poly poly = Encoder.GetECPoly(34);
488 //      final int[] expected = {
489 //          0, 111, 77, 146, 94, 26, 21, 108, 19,
490 //          105, 94, 113, 193, 86, 140, 163, 125,
491 //          58,
492 //          158, 229, 239, 218, 103, 56, 70, 114,
493 //          61, 183, 129, 167, 13, 98, 62, 129, 51
494 //      };
495 //      assertTrue(ComparePoly(expected, expected.length, poly));
496 //    }
497 //    {
498 //      final GF_Poly poly = Encoder.GetECPoly(68);
499 //      final int[] expected = {
500 //          0, 247, 159, 223, 33, 224, 93, 77, 70,
501 //          90, 160, 32, 254, 43, 150, 84, 101,
502 //          190,
503 //          205, 133, 52, 60, 202, 165, 220, 203,
504 //          151, 93, 84, 15, 84, 253, 173, 160,
505 //          89, 227, 52, 199, 97, 95, 231, 52,
506 //          177, 41, 125, 137, 241, 166, 225, 118,
507 //          2, 54,
508 //          32, 82, 215, 175, 198, 43, 238, 235,
509 //          27, 101, 184, 127, 3, 5, 8, 163, 238
510 //      };
511 //      assertTrue(ComparePoly(expected, expected.length, poly));
512 //    }
513 //  }
514
515   // Numbers are from http://www.swetake.com/qr/qr3.html and
516   // http://www.swetake.com/qr/qr9.html
517   public void testGenerateECBytes() {
518     {
519       final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
520       ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 17);
521       final int[] expected = {
522           42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61
523       };
524       assertEquals(expected.length, ec_bytes.size());
525       for (int x = 0; x < expected.length; x++) {
526         assertEquals(expected[x], ec_bytes.at(x));
527       }
528     }
529     {
530       final byte[] data_bytes = {67, 70, 22, 38, 54, 70, 86, 102, 118,
531           (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
532       ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 18);
533       final int[] expected = {
534           175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187
535       };
536       assertEquals(expected.length, ec_bytes.size());
537       for (int x = 0; x < expected.length; x++) {
538         assertEquals(expected[x], ec_bytes.at(x));
539       }
540     }
541     {
542       // High-order zero cofficient case.
543       final byte[] data_bytes = {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17};
544       ByteArray ec_bytes = Encoder.GenerateECBytes(new ByteArray(data_bytes), 17);
545       final int[] expected = {
546           0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213
547       };
548       assertEquals(expected.length, ec_bytes.size());
549       for (int x = 0; x < expected.length; x++) {
550         assertEquals(expected[x], ec_bytes.at(x));
551       }
552     }
553   }
554
555   public void testIsValidKanji() {
556     assertTrue(Encoder.IsValidKanji(0x82, 0xa0));  // Hiragana "A".
557     assertTrue(Encoder.IsValidKanji(0x93, 0xfa));  // Nichi in Kanji.
558     assertTrue(Encoder.IsValidKanji(0x8a, 0xbf));  // Kan in Kanji.
559     assertTrue(Encoder.IsValidKanji(0xe7, 0x4e));  // Sou in Kanji.
560     assertTrue(Encoder.IsValidKanji(0xea, 0xa2));  // Haruka in Kanji.
561
562     assertFalse(Encoder.IsValidKanji('0', '1'));
563     assertFalse(Encoder.IsValidKanji(0x82, 0x7f));
564     assertFalse(Encoder.IsValidKanji(0xa0, 0xa0));
565   }
566
567   public void testIsValidKanjiSequence() {
568     // AIUEO in Katakana
569     byte[] dat1 = {
570         (byte)0x83, 0x41, (byte)0x83, 0x43, (byte)0x83, 0x45, (byte)0x83, 0x47, (byte)0x83, 0x49
571     };
572     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat1)));
573     // 012345 in multi-byte letters.
574     byte[] dat2 = {
575         (byte)0x82, 0x4f, (byte)0x82, 0x50, (byte)0x82, 0x51, (byte)0x82, 0x52, (byte)0x82, 0x53,
576         (byte)0x82, 0x54
577     };
578     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat2)));
579     // Yoroshiku in Kanji.
580     byte[] dat3 = {
581         (byte)0x96, (byte)0xe9, (byte)0x98, 0x49, (byte)0x8e, (byte)0x80, (byte)0x8b, (byte)0xea
582     };
583     assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat3)));
584     assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("0123")));
585     assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("ABC")));
586   }
587 }