Small style stuff
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / MatrixUtilTestCase.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.WriterException;
20 import com.google.zxing.common.BitArray;
21 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
22 import org.junit.Assert;
23 import org.junit.Test;
24
25 /**
26  * @author satorux@google.com (Satoru Takabayashi) - creator
27  * @author mysen@google.com (Chris Mysen) - ported from C++
28  */
29 public final class MatrixUtilTestCase extends Assert {
30   public void testtoString() {
31     ByteMatrix array = new ByteMatrix(3, 3);
32     array.set(0, 0, 0);
33     array.set(1, 0, 1);
34     array.set(2, 0, 0);
35     array.set(0, 1, 1);
36     array.set(1, 1, 0);
37     array.set(2, 1, 1);
38     array.set(0, 2, -1);
39     array.set(1, 2, -1);
40     array.set(2, 2, -1);
41     String expected = " 0 1 0\n" + " 1 0 1\n" + "      \n";
42     assertEquals(expected, array.toString());
43   }
44
45   @Test
46   public void testClearMatrix() {
47     ByteMatrix matrix = new ByteMatrix(2, 2);
48     MatrixUtil.clearMatrix(matrix);
49     assertEquals(-1, matrix.get(0, 0));
50     assertEquals(-1, matrix.get(1, 0));
51     assertEquals(-1, matrix.get(0, 1));
52     assertEquals(-1, matrix.get(1, 1));
53   }
54
55   @Test
56   public void testEmbedBasicPatterns() throws WriterException {
57     {
58       // Version 1.
59       String expected =
60         " 1 1 1 1 1 1 1 0           0 1 1 1 1 1 1 1\n" +
61         " 1 0 0 0 0 0 1 0           0 1 0 0 0 0 0 1\n" +
62         " 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n" +
63         " 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n" +
64         " 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n" +
65         " 1 0 0 0 0 0 1 0           0 1 0 0 0 0 0 1\n" +
66         " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
67         " 0 0 0 0 0 0 0 0           0 0 0 0 0 0 0 0\n" +
68         "             1                            \n" +
69         "             0                            \n" +
70         "             1                            \n" +
71         "             0                            \n" +
72         "             1                            \n" +
73         " 0 0 0 0 0 0 0 0 1                        \n" +
74         " 1 1 1 1 1 1 1 0                          \n" +
75         " 1 0 0 0 0 0 1 0                          \n" +
76         " 1 0 1 1 1 0 1 0                          \n" +
77         " 1 0 1 1 1 0 1 0                          \n" +
78         " 1 0 1 1 1 0 1 0                          \n" +
79         " 1 0 0 0 0 0 1 0                          \n" +
80         " 1 1 1 1 1 1 1 0                          \n";
81       ByteMatrix matrix = new ByteMatrix(21, 21);
82       MatrixUtil.clearMatrix(matrix);
83       MatrixUtil.embedBasicPatterns(1, matrix);
84       assertEquals(expected, matrix.toString());
85     }
86     {
87       // Version 2.  Position adjustment pattern should apppear at right
88       // bottom corner.
89       String expected =
90         " 1 1 1 1 1 1 1 0                   0 1 1 1 1 1 1 1\n" +
91         " 1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\n" +
92         " 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n" +
93         " 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n" +
94         " 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n" +
95         " 1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\n" +
96         " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
97         " 0 0 0 0 0 0 0 0                   0 0 0 0 0 0 0 0\n" +
98         "             1                                    \n" +
99         "             0                                    \n" +
100         "             1                                    \n" +
101         "             0                                    \n" +
102         "             1                                    \n" +
103         "             0                                    \n" +
104         "             1                                    \n" +
105         "             0                                    \n" +
106         "             1                   1 1 1 1 1        \n" +
107         " 0 0 0 0 0 0 0 0 1               1 0 0 0 1        \n" +
108         " 1 1 1 1 1 1 1 0                 1 0 1 0 1        \n" +
109         " 1 0 0 0 0 0 1 0                 1 0 0 0 1        \n" +
110         " 1 0 1 1 1 0 1 0                 1 1 1 1 1        \n" +
111         " 1 0 1 1 1 0 1 0                                  \n" +
112         " 1 0 1 1 1 0 1 0                                  \n" +
113         " 1 0 0 0 0 0 1 0                                  \n" +
114         " 1 1 1 1 1 1 1 0                                  \n";
115       ByteMatrix matrix = new ByteMatrix(25, 25);
116       MatrixUtil.clearMatrix(matrix);
117       MatrixUtil.embedBasicPatterns(2, matrix);
118       assertEquals(expected, matrix.toString());
119     }
120   }
121
122   @Test
123   public void testEmbedTypeInfo() throws WriterException {
124     // Type info bits = 100000011001110.
125     String expected =
126       "                 0                        \n" +
127       "                 1                        \n" +
128       "                 1                        \n" +
129       "                 1                        \n" +
130       "                 0                        \n" +
131       "                 0                        \n" +
132       "                                          \n" +
133       "                 1                        \n" +
134       " 1 0 0 0 0 0   0 1         1 1 0 0 1 1 1 0\n" +
135       "                                          \n" +
136       "                                          \n" +
137       "                                          \n" +
138       "                                          \n" +
139       "                                          \n" +
140       "                 0                        \n" +
141       "                 0                        \n" +
142       "                 0                        \n" +
143       "                 0                        \n" +
144       "                 0                        \n" +
145       "                 0                        \n" +
146       "                 1                        \n";
147     ByteMatrix matrix = new ByteMatrix(21, 21);
148     MatrixUtil.clearMatrix(matrix);
149     MatrixUtil.embedTypeInfo(ErrorCorrectionLevel.M, 5, matrix);
150     assertEquals(expected, matrix.toString());
151   }
152
153   @Test
154   public void testEmbedVersionInfo() throws WriterException {
155     // Version info bits = 000111 110010 010100
156     String expected =
157       "                     0 0 1                \n" +
158       "                     0 1 0                \n" +
159       "                     0 1 0                \n" +
160       "                     0 1 1                \n" +
161       "                     1 1 1                \n" +
162       "                     0 0 0                \n" +
163       "                                          \n" +
164       "                                          \n" +
165       "                                          \n" +
166       "                                          \n" +
167       " 0 0 0 0 1 0                              \n" +
168       " 0 1 1 1 1 0                              \n" +
169       " 1 0 0 1 1 0                              \n" +
170       "                                          \n" +
171       "                                          \n" +
172       "                                          \n" +
173       "                                          \n" +
174       "                                          \n" +
175       "                                          \n" +
176       "                                          \n" +
177       "                                          \n";
178     // Actually, version 7 QR Code has 45x45 matrix but we use 21x21 here
179     // since 45x45 matrix is too big to depict.
180     ByteMatrix matrix = new ByteMatrix(21, 21);
181     MatrixUtil.clearMatrix(matrix);
182     MatrixUtil.maybeEmbedVersionInfo(7, matrix);
183     assertEquals(expected, matrix.toString());
184   }
185
186   @Test
187   public void testEmbedDataBits() throws WriterException {
188     // Cells other than basic patterns should be filled with zero.
189     String expected =
190       " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" +
191       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
192       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
193       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
194       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
195       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
196       " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
197       " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
198       " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
199       " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
200       " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
201       " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
202       " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
203       " 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n" +
204       " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
205       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
206       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
207       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
208       " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
209       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
210       " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
211     BitArray bits = new BitArray();
212     ByteMatrix matrix = new ByteMatrix(21, 21);
213     MatrixUtil.clearMatrix(matrix);
214     MatrixUtil.embedBasicPatterns(1, matrix);
215     MatrixUtil.embedDataBits(bits, -1, matrix);
216     assertEquals(expected, matrix.toString());
217   }
218
219   @Test
220   public void testBuildMatrix() throws WriterException {
221     // From http://www.swetake.com/qr/qr7.html
222     String expected =
223       " 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" +
224       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
225       " 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n" +
226       " 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\n" +
227       " 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\n" +
228       " 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\n" +
229       " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
230       " 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\n" +
231       " 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\n" +
232       " 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\n" +
233       " 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\n" +
234       " 1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\n" +
235       " 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\n" +
236       " 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\n" +
237       " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n" +
238       " 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\n" +
239       " 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\n" +
240       " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\n" +
241       " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" +
242       " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" +
243       " 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n";
244     char[] bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236,
245         42, 159, 74, 221, 244, 169, 239, 150, 138,
246         70, 237, 85, 224, 96, 74, 219 , 61};
247     BitArray bits = new BitArray();
248     for (char c: bytes) {
249       bits.appendBits(c, 8);
250     }
251     ByteMatrix matrix = new ByteMatrix(21, 21);
252     MatrixUtil.buildMatrix(bits,
253                            ErrorCorrectionLevel.H,
254                            1,  // Version 1
255                            3,  // Mask pattern 3
256                            matrix);
257   }
258
259   @Test
260   public void testFindMSBSet() {
261     assertEquals(0, MatrixUtil.findMSBSet(0));
262     assertEquals(1, MatrixUtil.findMSBSet(1));
263     assertEquals(8, MatrixUtil.findMSBSet(0x80));
264     assertEquals(32, MatrixUtil.findMSBSet(0x80000000));
265   }
266
267   @Test
268   public void testCalculateBCHCode() {
269     // Encoding of type information.
270     // From Appendix C in JISX0510:2004 (p 65)
271     assertEquals(0xdc, MatrixUtil.calculateBCHCode(5, 0x537));
272     // From http://www.swetake.com/qr/qr6.html
273     assertEquals(0x1c2, MatrixUtil.calculateBCHCode(0x13, 0x537));
274     // From http://www.swetake.com/qr/qr11.html
275     assertEquals(0x214, MatrixUtil.calculateBCHCode(0x1b, 0x537));
276
277     // Encofing of version information.
278     // From Appendix D in JISX0510:2004 (p 68)
279     assertEquals(0xc94, MatrixUtil.calculateBCHCode(7, 0x1f25));
280     assertEquals(0x5bc, MatrixUtil.calculateBCHCode(8, 0x1f25));
281     assertEquals(0xa99, MatrixUtil.calculateBCHCode(9, 0x1f25));
282     assertEquals(0x4d3, MatrixUtil.calculateBCHCode(10, 0x1f25));
283     assertEquals(0x9a6, MatrixUtil.calculateBCHCode(20, 0x1f25));
284     assertEquals(0xd75, MatrixUtil.calculateBCHCode(30, 0x1f25));
285     assertEquals(0xc69, MatrixUtil.calculateBCHCode(40, 0x1f25));
286   }
287
288   // We don't test a lot of cases in this function since we've already
289   // tested them in TEST(calculateBCHCode).
290   @Test
291   public void testMakeVersionInfoBits() throws WriterException {
292     // From Appendix D in JISX0510:2004 (p 68)
293     BitArray bits = new BitArray();
294     MatrixUtil.makeVersionInfoBits(7, bits);
295     assertEquals(" ...XXXXX ..X..X.X ..", bits.toString());
296   }
297
298   // We don't test a lot of cases in this function since we've already
299   // tested them in TEST(calculateBCHCode).
300   @Test
301   public void testMakeTypeInfoInfoBits() throws WriterException {
302     // From Appendix C in JISX0510:2004 (p 65)
303     BitArray bits = new BitArray();
304     MatrixUtil.makeTypeInfoBits(ErrorCorrectionLevel.M, 5, bits);
305     assertEquals(" X......X X..XXX.", bits.toString());
306   }
307 }