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 testMask0()29 public void testMask0() { 30 testMaskAcrossDimensions(0, (i, j) -> (i + j) % 2 == 0); 31 } 32 33 @Test testMask1()34 public void testMask1() { 35 testMaskAcrossDimensions(1, (i, j) -> i % 2 == 0); 36 } 37 38 @Test testMask2()39 public void testMask2() { 40 testMaskAcrossDimensions(2, (i, j) -> j % 3 == 0); 41 } 42 43 @Test testMask3()44 public void testMask3() { 45 testMaskAcrossDimensions(3, (i, j) -> (i + j) % 3 == 0); 46 } 47 48 @Test testMask4()49 public void testMask4() { 50 testMaskAcrossDimensions(4, (i, j) -> (i / 2 + j / 3) % 2 == 0); 51 } 52 53 @Test testMask5()54 public void testMask5() { 55 testMaskAcrossDimensions(5, (i, j) -> (i * j) % 2 + (i * j) % 3 == 0); 56 } 57 58 @Test testMask6()59 public void testMask6() { 60 testMaskAcrossDimensions(6, (i, j) -> ((i * j) % 2 + (i * j) % 3) % 2 == 0); 61 } 62 63 @Test testMask7()64 public void testMask7() { 65 testMaskAcrossDimensions(7, (i, j) -> ((i + j) % 2 + (i * j) % 3) % 2 == 0); 66 } 67 testMaskAcrossDimensions(int reference, MaskCondition condition)68 private static void testMaskAcrossDimensions(int reference, MaskCondition condition) { 69 DataMask mask = DataMask.values()[reference]; 70 for (int version = 1; version <= 40; version++) { 71 int dimension = 17 + 4 * version; 72 testMask(mask, dimension, condition); 73 } 74 } 75 testMask(DataMask mask, int dimension, MaskCondition condition)76 private static void testMask(DataMask mask, int dimension, MaskCondition condition) { 77 BitMatrix bits = new BitMatrix(dimension); 78 mask.unmaskBitMatrix(bits, dimension); 79 for (int i = 0; i < dimension; i++) { 80 for (int j = 0; j < dimension; j++) { 81 assertEquals( 82 "(" + i + ',' + j + ')', 83 condition.isMasked(i, j), 84 bits.get(j, i)); 85 } 86 } 87 } 88 89 @FunctionalInterface 90 private interface MaskCondition { isMasked(int i, int j)91 boolean isMasked(int i, int j); 92 } 93 94 } 95