git-svn-id: http://zxing.googlecode.com/svn/trunk@6 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / core / test / src / com / google / zxing / qrcode / decoder / DataMaskTestCase.java
1 /*
2  * Copyright 2007 Google Inc.
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 import junit.textui.TestRunner;
22
23 /**
24  * @author Sean Owen
25  */
26 public final class DataMaskTestCase extends TestCase {
27
28   public void testMask0() {
29     testMaskAcrossDimensions(0, new MaskCondition() {
30       public boolean isMasked(int i, int j) {
31         return (i + j) % 2 == 0;
32       }
33     });
34   }
35
36   public void testMask1() {
37     testMaskAcrossDimensions(1, new MaskCondition() {
38       public boolean isMasked(int i, int j) {
39         return i % 2 == 0;
40       }
41     });
42   }
43
44   public void testMask2() {
45     testMaskAcrossDimensions(2, new MaskCondition() {
46       public boolean isMasked(int i, int j) {
47         return j % 3 == 0;
48       }
49     });
50   }
51
52   public void testMask3() {
53     testMaskAcrossDimensions(3, new MaskCondition() {
54       public boolean isMasked(int i, int j) {
55         return (i + j) % 3 == 0;
56       }
57     });
58   }
59
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;
64       }
65     });
66   }
67
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;
72       }
73     });
74   }
75
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;
80       }
81     });
82   }
83
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;
88       }
89     });
90   }
91
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);
98     }
99   }
100
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++) {
106         assertEquals(
107             "(" + i + ',' + j + ')', 
108             condition.isMasked(i, j),
109             bits.get(i, j));
110       }
111     }
112   }
113
114   private static interface MaskCondition {
115     boolean isMasked(int i, int j);
116   }
117
118   public static void main(String[] args) {
119     TestRunner.run(new DataMaskTestCase());
120   }
121
122 }