At last update to JUnit 4.x
[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 org.junit.Assert;
21 import org.junit.Test;
22
23 /**
24  * @author Sean Owen
25  */
26 public final class DataMaskTestCase extends Assert {
27
28   @Test
29   public void testMask0() {
30     testMaskAcrossDimensions(0, new MaskCondition() {
31       public boolean isMasked(int i, int j) {
32         return (i + j) % 2 == 0;
33       }
34     });
35   }
36
37   @Test
38   public void testMask1() {
39     testMaskAcrossDimensions(1, new MaskCondition() {
40       public boolean isMasked(int i, int j) {
41         return i % 2 == 0;
42       }
43     });
44   }
45
46   @Test
47   public void testMask2() {
48     testMaskAcrossDimensions(2, new MaskCondition() {
49       public boolean isMasked(int i, int j) {
50         return j % 3 == 0;
51       }
52     });
53   }
54
55   @Test
56   public void testMask3() {
57     testMaskAcrossDimensions(3, new MaskCondition() {
58       public boolean isMasked(int i, int j) {
59         return (i + j) % 3 == 0;
60       }
61     });
62   }
63
64   @Test
65   public void testMask4() {
66     testMaskAcrossDimensions(4, new MaskCondition() {
67       public boolean isMasked(int i, int j) {
68         return (i / 2 + j / 3) % 2 == 0;
69       }
70     });
71   }
72
73   @Test
74   public void testMask5() {
75     testMaskAcrossDimensions(5, new MaskCondition() {
76       public boolean isMasked(int i, int j) {
77         return (i * j) % 2 + (i * j) % 3 == 0;
78       }
79     });
80   }
81
82   @Test
83   public void testMask6() {
84     testMaskAcrossDimensions(6, 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   @Test
92   public void testMask7() {
93     testMaskAcrossDimensions(7, new MaskCondition() {
94       public boolean isMasked(int i, int j) {
95         return ((i + j) % 2 + (i * j) % 3) % 2 == 0;
96       }
97     });
98   }
99
100   private static void testMaskAcrossDimensions(int reference, MaskCondition condition) {
101     DataMask mask = DataMask.forReference(reference);
102     for (int version = 1; version <= 40; version++) {
103       int dimension = 17 + 4 * version;
104       testMask(mask, dimension, condition);
105     }
106   }
107
108   private static void testMask(DataMask mask, int dimension, MaskCondition condition) {
109     BitMatrix bits = new BitMatrix(dimension);
110     mask.unmaskBitMatrix(bits, dimension);
111     for (int i = 0; i < dimension; i++) {
112       for (int j = 0; j < dimension; j++) {
113         assertEquals(
114             "(" + i + ',' + j + ')',
115             condition.isMasked(i, j),
116             bits.get(j, i));
117       }
118     }
119   }
120
121   private interface MaskCondition {
122     boolean isMasked(int i, int j);
123   }
124
125 }