2 * Copyright 2007 Google Inc.
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;
21 import junit.textui.TestRunner;
26 public final class DataMaskTestCase extends TestCase {
28 public void testMask0() {
29 testMaskAcrossDimensions(0, new MaskCondition() {
30 public boolean isMasked(int i, int j) {
31 return (i + j) % 2 == 0;
36 public void testMask1() {
37 testMaskAcrossDimensions(1, new MaskCondition() {
38 public boolean isMasked(int i, int j) {
44 public void testMask2() {
45 testMaskAcrossDimensions(2, new MaskCondition() {
46 public boolean isMasked(int i, int j) {
52 public void testMask3() {
53 testMaskAcrossDimensions(3, new MaskCondition() {
54 public boolean isMasked(int i, int j) {
55 return (i + j) % 3 == 0;
60 public void testMask4() {
61 testMaskAcrossDimensions(4, new MaskCondition() {
62 public boolean isMasked(int i, int j) {
63 return (i/2 + j/3) % 2 == 0;
68 public void testMask5() {
69 testMaskAcrossDimensions(5, new MaskCondition() {
70 public boolean isMasked(int i, int j) {
71 return (i*j) % 2 + (i*j) % 3 == 0;
76 public void testMask6() {
77 testMaskAcrossDimensions(6, new MaskCondition() {
78 public boolean isMasked(int i, int j) {
79 return ((i*j) % 2 + (i*j) % 3) % 2 == 0;
84 public void testMask7() {
85 testMaskAcrossDimensions(7, new MaskCondition() {
86 public boolean isMasked(int i, int j) {
87 return ((i+j) % 2 + (i*j) % 3) % 2 == 0;
92 private void testMaskAcrossDimensions(int reference,
93 MaskCondition condition) {
94 DataMask mask = DataMask.forReference(reference);
95 for (int version = 1; version <= 40; version++) {
96 int dimension = 17 + 4*version;
97 testMask(mask, dimension, condition);
101 private void testMask(DataMask mask, int dimension, MaskCondition condition) {
102 BitMatrix bits = new BitMatrix(dimension);
103 mask.unmaskBitMatrix(bits.getBits(), dimension);
104 for (int i = 0; i < dimension; i++) {
105 for (int j = 0; j < dimension; j++) {
107 "(" + i + ',' + j + ')',
108 condition.isMasked(i, j),
114 private static interface MaskCondition {
115 boolean isMasked(int i, int j);
118 public static void main(String[] args) {
119 TestRunner.run(new DataMaskTestCase());