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