housekeeping for tests for qrcode and aztec

This commit is contained in:
Henry Schimke
2022-09-30 15:57:01 -05:00
parent 438ae9f588
commit 5436b8d9f3
14 changed files with 1 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import com.google.zxing.common.BitMatrix;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class DataMaskTestCase extends Assert {
@Test
public void testMask0() {
testMaskAcrossDimensions(0, (i, j) -> (i + j) % 2 == 0);
}
@Test
public void testMask1() {
testMaskAcrossDimensions(1, (i, j) -> i % 2 == 0);
}
@Test
public void testMask2() {
testMaskAcrossDimensions(2, (i, j) -> j % 3 == 0);
}
@Test
public void testMask3() {
testMaskAcrossDimensions(3, (i, j) -> (i + j) % 3 == 0);
}
@Test
public void testMask4() {
testMaskAcrossDimensions(4, (i, j) -> (i / 2 + j / 3) % 2 == 0);
}
@Test
public void testMask5() {
testMaskAcrossDimensions(5, (i, j) -> (i * j) % 2 + (i * j) % 3 == 0);
}
@Test
public void testMask6() {
testMaskAcrossDimensions(6, (i, j) -> ((i * j) % 2 + (i * j) % 3) % 2 == 0);
}
@Test
public void testMask7() {
testMaskAcrossDimensions(7, (i, j) -> ((i + j) % 2 + (i * j) % 3) % 2 == 0);
}
private static void testMaskAcrossDimensions(int reference, MaskCondition condition) {
DataMask mask = DataMask.values()[reference];
for (int version = 1; version <= 40; version++) {
int dimension = 17 + 4 * version;
testMask(mask, dimension, condition);
}
}
private static void testMask(DataMask mask, int dimension, MaskCondition condition) {
BitMatrix bits = new BitMatrix(dimension);
mask.unmaskBitMatrix(bits, dimension);
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
assertEquals(
"(" + i + ',' + j + ')',
condition.isMasked(i, j),
bits.get(j, i));
}
}
}
@FunctionalInterface
private interface MaskCondition {
boolean isMasked(int i, int j);
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import com.google.zxing.common.BitSourceBuilder;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests {@link DecodedBitStreamParser}.
*
* @author Sean Owen
*/
public final class DecodedBitStreamParserTestCase extends Assert {
@Test
public void testSimpleByteMode() throws Exception {
BitSourceBuilder builder = new BitSourceBuilder();
builder.write(0x04, 4); // Byte mode
builder.write(0x03, 8); // 3 bytes
builder.write(0xF1, 8);
builder.write(0xF2, 8);
builder.write(0xF3, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1), null, null).getText();
assertEquals("\u00f1\u00f2\u00f3", result);
}
@Test
public void testSimpleSJIS() throws Exception {
BitSourceBuilder builder = new BitSourceBuilder();
builder.write(0x04, 4); // Byte mode
builder.write(0x04, 8); // 4 bytes
builder.write(0xA1, 8);
builder.write(0xA2, 8);
builder.write(0xA3, 8);
builder.write(0xD0, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1), null, null).getText();
assertEquals("\uff61\uff62\uff63\uff90", result);
}
@Test
public void testECI() throws Exception {
BitSourceBuilder builder = new BitSourceBuilder();
builder.write(0x07, 4); // ECI mode
builder.write(0x02, 8); // ECI 2 = CP437 encoding
builder.write(0x04, 4); // Byte mode
builder.write(0x03, 8); // 3 bytes
builder.write(0xA1, 8);
builder.write(0xA2, 8);
builder.write(0xA3, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1), null, null).getText();
assertEquals("\u00ed\u00f3\u00fa", result);
}
@Test
public void testHanzi() throws Exception {
BitSourceBuilder builder = new BitSourceBuilder();
builder.write(0x0D, 4); // Hanzi mode
builder.write(0x01, 4); // Subset 1 = GB2312 encoding
builder.write(0x01, 8); // 1 characters
builder.write(0x03C1, 13);
String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1), null, null).getText();
assertEquals("\u963f", result);
}
@Test
public void testHanziLevel1() throws Exception {
BitSourceBuilder builder = new BitSourceBuilder();
builder.write(0x0D, 4); // Hanzi mode
builder.write(0x01, 4); // Subset 1 = GB2312 encoding
builder.write(0x01, 8); // 1 characters
// A5A2 (U+30A2) => A5A2 - A1A1 = 401, 4*60 + 01 = 0181
builder.write(0x0181, 13);
String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1), null, null).getText();
assertEquals("\u30a2", result);
}
// TODO definitely need more tests here
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class ErrorCorrectionLevelTestCase extends Assert {
@Test
public void testForBits() {
assertSame(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0));
assertSame(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1));
assertSame(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2));
assertSame(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3));
}
@Test(expected = IllegalArgumentException.class)
public void testBadECLevel() {
ErrorCorrectionLevel.forBits(4);
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class FormatInformationTestCase extends Assert {
private static final int MASKED_TEST_FORMAT_INFO = 0x2BED;
private static final int UNMASKED_TEST_FORMAT_INFO = MASKED_TEST_FORMAT_INFO ^ 0x5412;
@Test
public void testBitsDiffering() {
assertEquals(0, FormatInformation.numBitsDiffering(1, 1));
assertEquals(1, FormatInformation.numBitsDiffering(0, 2));
assertEquals(2, FormatInformation.numBitsDiffering(1, 2));
assertEquals(32, FormatInformation.numBitsDiffering(-1, 0));
}
@Test
public void testDecode() {
// Normal case
FormatInformation expected =
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
assertNotNull(expected);
assertEquals((byte) 0x07, expected.getDataMask());
assertSame(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel());
// where the code forgot the mask!
assertEquals(expected,
FormatInformation.decodeFormatInformation(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO));
}
@Test
public void testDecodeWithBitDifference() {
FormatInformation expected =
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
// 1,2,3,4 bits difference
assertEquals(expected, FormatInformation.decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO ^ 0x01));
assertEquals(expected, FormatInformation.decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x03));
assertEquals(expected, FormatInformation.decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO ^ 0x07));
assertNull(FormatInformation.decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO ^ 0x0F));
}
@Test
public void testDecodeWithMisread() {
FormatInformation expected =
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
assertEquals(expected, FormatInformation.decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x0F));
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class ModeTestCase extends Assert {
@Test
public void testForBits() {
assertSame(Mode.TERMINATOR, Mode.forBits(0x00));
assertSame(Mode.NUMERIC, Mode.forBits(0x01));
assertSame(Mode.ALPHANUMERIC, Mode.forBits(0x02));
assertSame(Mode.BYTE, Mode.forBits(0x04));
assertSame(Mode.KANJI, Mode.forBits(0x08));
}
@Test(expected = IllegalArgumentException.class)
public void testBadMode() {
Mode.forBits(0x10);
}
@Test
public void testCharacterCount() {
// Spot check a few values
assertEquals(10, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(5)));
assertEquals(12, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(26)));
assertEquals(14, Mode.NUMERIC.getCharacterCountBits(Version.getVersionForNumber(40)));
assertEquals(9, Mode.ALPHANUMERIC.getCharacterCountBits(Version.getVersionForNumber(6)));
assertEquals(8, Mode.BYTE.getCharacterCountBits(Version.getVersionForNumber(7)));
assertEquals(8, Mode.KANJI.getCharacterCountBits(Version.getVersionForNumber(8)));
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Sean Owen
*/
public final class VersionTestCase extends Assert {
@Test(expected = IllegalArgumentException.class)
public void testBadVersion() {
Version.getVersionForNumber(0);
}
@Test
public void testVersionForNumber() {
for (int i = 1; i <= 40; i++) {
checkVersion(Version.getVersionForNumber(i), i, 4 * i + 17);
}
}
private static void checkVersion(Version version, int number, int dimension) {
assertNotNull(version);
assertEquals(number, version.getVersionNumber());
assertNotNull(version.getAlignmentPatternCenters());
if (number > 1) {
assertTrue(version.getAlignmentPatternCenters().length > 0);
}
assertEquals(dimension, version.getDimensionForVersion());
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.H));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.L));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.M));
assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.Q));
assertNotNull(version.buildFunctionPattern());
}
@Test
public void testGetProvisionalVersionForDimension() throws Exception {
for (int i = 1; i <= 40; i++) {
assertEquals(i, Version.getProvisionalVersionForDimension(4 * i + 17).getVersionNumber());
}
}
@Test
public void testDecodeVersionInformation() {
// Spot check
doTestVersion(7, 0x07C94);
doTestVersion(12, 0x0C762);
doTestVersion(17, 0x1145D);
doTestVersion(22, 0x168C9);
doTestVersion(27, 0x1B08E);
doTestVersion(32, 0x209D5);
}
private static void doTestVersion(int expectedVersion, int mask) {
Version version = Version.decodeVersionInformation(mask);
assertNotNull(version);
assertEquals(expectedVersion, version.getVersionNumber());
}
}