43ae4f2c44482642dc0eda10b18cc1662a26000b
[zxing.git] / core / test / src / com / google / zxing / qrcode / decoder / DataMaskTestCase.java
1 /*
2  * Copyright 2007 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.decoder;
18
19 import com.google.zxing.common.BitMatrix;
20 import junit.framework.TestCase;
21
22 /**
23  * @author Sean Owen
24  */
25 public final class DataMaskTestCase extends TestCase {
26
27   public void testMask0() {
28     testMaskAcrossDimensions(0, new MaskCondition() {
29       public boolean isMasked(int i, int j) {
30         return (i + j) % 2 == 0;
31       }
32     });
33   }
34
35   public void testMask1() {
36     testMaskAcrossDimensions(1, new MaskCondition() {
37       public boolean isMasked(int i, int j) {
38         return i % 2 == 0;
39       }
40     });
41   }
42
43   public void testMask2() {
44     testMaskAcrossDimensions(2, new MaskCondition() {
45       public boolean isMasked(int i, int j) {
46         return j % 3 == 0;
47       }
48     });
49   }
50
51   public void testMask3() {
52     testMaskAcrossDimensions(3, new MaskCondition() {
53       public boolean isMasked(int i, int j) {
54         return (i + j) % 3 == 0;
55       }
56     });
57   }
58
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;
63       }
64     });
65   }
66
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;
71       }
72     });
73   }
74
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;
79       }
80     });
81   }
82
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;
87       }
88     });
89   }
90
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);
97     }
98   }
99
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++) {
105         assertEquals(
106             "(" + i + ',' + j + ')',
107             condition.isMasked(i, j),
108             bits.get(j, i));
109       }
110     }
111   }
112
113   private interface MaskCondition {
114     boolean isMasked(int i, int j);
115   }
116
117 }