Overdue unit tests for some QR code classes

git-svn-id: https://zxing.googlecode.com/svn/trunk@260 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-03-10 20:38:00 +00:00
parent 7a6f72bad4
commit 600e93c174
7 changed files with 175 additions and 51 deletions

View file

@ -16,6 +16,8 @@
package com.google.zxing.qrcode.decoder;
import com.google.zxing.ReaderException;
/**
* <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
* defined by the QR code standard.</p>
@ -59,7 +61,10 @@ final class ErrorCorrectionLevel {
* @param bits int containing the two bits encoding a QR Code's error correction level
* @return {@link ErrorCorrectionLevel} representing the encoded error correction level
*/
static ErrorCorrectionLevel forBits(int bits) {
static ErrorCorrectionLevel forBits(int bits) throws ReaderException {
if (bits < 0 || bits >= FOR_BITS.length) {
throw new ReaderException("Illegal error correction level bits" + bits);
}
return FOR_BITS[bits];
}

View file

@ -16,6 +16,8 @@
package com.google.zxing.qrcode.decoder;
import com.google.zxing.ReaderException;
/**
* <p>Encapsulates a QR Code's format information, including the data mask used and
* error correction level.</p>
@ -75,7 +77,7 @@ final class FormatInformation {
private final ErrorCorrectionLevel errorCorrectionLevel;
private final byte dataMask;
private FormatInformation(int formatInfo) {
private FormatInformation(int formatInfo) throws ReaderException {
// Bits 3,4
errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
// Bottom 3 bits
@ -99,7 +101,7 @@ final class FormatInformation {
* @param rawFormatInfo
* @return
*/
static FormatInformation decodeFormatInformation(int rawFormatInfo) {
static FormatInformation decodeFormatInformation(int rawFormatInfo) throws ReaderException {
FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
if (formatInfo != null) {
return formatInfo;
@ -110,7 +112,7 @@ final class FormatInformation {
return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
}
private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) {
private static FormatInformation doDecodeFormatInformation(int rawFormatInfo) throws ReaderException {
// Unmask:
int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
// Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing

View file

@ -1,46 +0,0 @@
/*
* Copyright 2007 Google Inc.
*
* 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 junit.framework.TestCase;
/**
* @author srowen@google.com (Sean Owen)
*/
public final class BitSourceTestCase extends TestCase {
public void testSource() {
byte[] bytes = new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5};
BitSource source = new BitSource(bytes);
assertEquals(40, source.available());
assertEquals(0, source.readBits(1));
assertEquals(39, source.available());
assertEquals(0, source.readBits(6));
assertEquals(33, source.available());
assertEquals(1, source.readBits(1));
assertEquals(32, source.available());
assertEquals(2, source.readBits(8));
assertEquals(24, source.available());
assertEquals(12, source.readBits(10));
assertEquals(14, source.available());
assertEquals(16, source.readBits(8));
assertEquals(6, source.available());
assertEquals(5, source.readBits(6));
assertEquals(0, source.available());
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright 2008 Google Inc.
*
* 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.ReaderException;
import junit.framework.TestCase;
/**
* @author srowen@google.com (Sean Owen)
*/
public final class ErrorCorrectionLevelTestCase extends TestCase {
public void testForBits() throws ReaderException {
assertEquals(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0));
assertEquals(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1));
assertEquals(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2));
assertEquals(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3));
try {
ErrorCorrectionLevel.forBits(4);
fail("Should have thrown an exception");
} catch (ReaderException re) {
// good
}
}
}

View file

@ -16,6 +16,7 @@
package com.google.zxing.qrcode.decoder;
import com.google.zxing.ReaderException;
import junit.framework.TestCase;
/**
@ -30,7 +31,7 @@ public final class FormatInformationTestCase extends TestCase {
assertEquals(32, FormatInformation.numBitsDiffering(-1, 0));
}
public void testDecode() {
public void testDecode() throws ReaderException {
// Normal case
FormatInformation expected = FormatInformation.decodeFormatInformation(0x2BED ^ 0x5412);
assertEquals((byte) 0x07, expected.getDataMask());

View file

@ -0,0 +1,51 @@
/*
* Copyright 2008 Google Inc.
*
* 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.ReaderException;
import junit.framework.TestCase;
/**
* @author srowen@google.com (Sean Owen)
*/
public final class ModeTestCase extends TestCase {
public void testForBits() throws ReaderException {
assertEquals(Mode.TERMINATOR, Mode.forBits(0x00));
assertEquals(Mode.NUMERIC, Mode.forBits(0x01));
assertEquals(Mode.ALPHANUMERIC, Mode.forBits(0x02));
assertEquals(Mode.BYTE, Mode.forBits(0x04));
assertEquals(Mode.KANJI, Mode.forBits(0x08));
try {
Mode.forBits(0x10);
fail("Should have thrown an exception");
} catch (ReaderException re) {
// good
}
}
public void testCharacterCount() throws ReaderException {
// 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,70 @@
/*
* Copyright 2008 Google Inc.
*
* 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.ReaderException;
import junit.framework.TestCase;
/**
* @author srowen@google.com (Sean Owen)
*/
public final class VersionTestCase extends TestCase {
public void testVersionForNumber() throws ReaderException {
try {
Version.getVersionForNumber(0);
fail("Should have thrown an exception");
} catch (ReaderException re) {
// good
}
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());
}
public void testGetProvisionalVersionForDimension() throws ReaderException {
for (int i = 1; i <= 40; i++) {
assertEquals(i, Version.getProvisionalVersionForDimension(4*i + 17).getVersionNumber());
}
}
public void testDecodeVersionInformation() throws ReaderException {
// Spot check
assertEquals(7, Version.decodeVersionInformation(0x07C94).getVersionNumber());
assertEquals(12, Version.decodeVersionInformation(0x0C762).getVersionNumber());
assertEquals(17, Version.decodeVersionInformation(0x1145D).getVersionNumber());
assertEquals(22, Version.decodeVersionInformation(0x168C9).getVersionNumber());
assertEquals(27, Version.decodeVersionInformation(0x1B08E).getVersionNumber());
assertEquals(32, Version.decodeVersionInformation(0x209D5).getVersionNumber());
}
}