2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.encoder;
19 import com.google.zxing.common.ByteMatrix;
20 import com.google.zxing.qrcode.encoder.MatrixUtil;
21 import junit.framework.TestCase;
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"
31 * @author satorux@google.com (Satoru Takabayashi) - creator
32 * @author mysen@google.com (Chris Mysen) - ported from C++
34 public final class MatrixUtilTestCase extends TestCase {
35 public void testtoString() {
36 ByteMatrix array = new ByteMatrix(3, 3);
46 String expected = " 0 1 0\n" + " 1 0 1\n" + " \n";
47 assertEquals(expected, array.toString());
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));
59 public void testEmbedBasicPatterns() {
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" +
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());
90 // Version 2. Position adjustment pattern should apppear at right
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" +
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());
125 public void testEmbedTypeInfo() {
126 // Type info bits = 100000011001110.
136 " 1 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0\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());
158 public void testEmbedVersionInfo() {
159 // Version info bits = 000111 110010 010100
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());
190 public void testEmbedDataBits() {
191 // Cells other than basic patterns should be filled with zero.
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());
222 public void testBuildMatrix() {
223 // From http://www.swetake.com/qr/qr7.html
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);
253 ByteMatrix matrix = new ByteMatrix(21, 21);
254 assertTrue(MatrixUtil.BuildMatrix(bits,
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));
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));
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));
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());
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,
304 assertEquals("100000011001110", bits.toString());