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