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