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