2 * Copyright 2007 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.decoder;
19 import com.google.zxing.common.BitMatrix;
20 import junit.framework.TestCase;
25 public final class DataMaskTestCase extends TestCase {
27 public void testMask0() {
28 testMaskAcrossDimensions(0, new MaskCondition() {
29 public boolean isMasked(int i, int j) {
30 return (i + j) % 2 == 0;
35 public void testMask1() {
36 testMaskAcrossDimensions(1, new MaskCondition() {
37 public boolean isMasked(int i, int j) {
43 public void testMask2() {
44 testMaskAcrossDimensions(2, new MaskCondition() {
45 public boolean isMasked(int i, int j) {
51 public void testMask3() {
52 testMaskAcrossDimensions(3, new MaskCondition() {
53 public boolean isMasked(int i, int j) {
54 return (i + j) % 3 == 0;
59 public void testMask4() {
60 testMaskAcrossDimensions(4, new MaskCondition() {
61 public boolean isMasked(int i, int j) {
62 return (i / 2 + j / 3) % 2 == 0;
67 public void testMask5() {
68 testMaskAcrossDimensions(5, new MaskCondition() {
69 public boolean isMasked(int i, int j) {
70 return (i * j) % 2 + (i * j) % 3 == 0;
75 public void testMask6() {
76 testMaskAcrossDimensions(6, new MaskCondition() {
77 public boolean isMasked(int i, int j) {
78 return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
83 public void testMask7() {
84 testMaskAcrossDimensions(7, new MaskCondition() {
85 public boolean isMasked(int i, int j) {
86 return ((i + j) % 2 + (i * j) % 3) % 2 == 0;
91 private void testMaskAcrossDimensions(int reference,
92 MaskCondition condition) {
93 DataMask mask = DataMask.forReference(reference);
94 for (int version = 1; version <= 40; version++) {
95 int dimension = 17 + 4 * version;
96 testMask(mask, dimension, condition);
100 private void testMask(DataMask mask, int dimension, MaskCondition condition) {
101 BitMatrix bits = new BitMatrix(dimension);
102 mask.unmaskBitMatrix(bits, dimension);
103 for (int i = 0; i < dimension; i++) {
104 for (int j = 0; j < dimension; j++) {
106 "(" + i + ',' + j + ')',
107 condition.isMasked(i, j),
113 private interface MaskCondition {
114 boolean isMasked(int i, int j);