Overdue unit tests for some QR code classes
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 10 Mar 2008 20:38:00 +0000 (20:38 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 10 Mar 2008 20:38:00 +0000 (20:38 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@260 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/qrcode/decoder/ErrorCorrectionLevel.java
core/src/com/google/zxing/qrcode/decoder/FormatInformation.java
core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java [deleted file]
core/test/src/com/google/zxing/qrcode/decoder/ErrorCorrectionLevelTestCase.java [new file with mode: 0644]
core/test/src/com/google/zxing/qrcode/decoder/FormatInformationTestCase.java
core/test/src/com/google/zxing/qrcode/decoder/ModeTestCase.java [new file with mode: 0644]
core/test/src/com/google/zxing/qrcode/decoder/VersionTestCase.java [new file with mode: 0644]

index 4274e07..0e83d58 100644 (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];
   }
 
index aaa6060..2234746 100644 (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
diff --git a/core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java
deleted file mode 100644 (file)
index b528943..0000000
+++ /dev/null
@@ -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());
-  }
-
-}
\ No newline at end of file
diff --git a/core/test/src/com/google/zxing/qrcode/decoder/ErrorCorrectionLevelTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/ErrorCorrectionLevelTestCase.java
new file mode 100644 (file)
index 0000000..0a2dc1f
--- /dev/null
@@ -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
+    }
+  }
+
+
+}
\ No newline at end of file
index 3829de7..777dba7 100644 (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());
diff --git a/core/test/src/com/google/zxing/qrcode/decoder/ModeTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/ModeTestCase.java
new file mode 100644 (file)
index 0000000..caaf963
--- /dev/null
@@ -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)));
+  }
+
+}
\ No newline at end of file
diff --git a/core/test/src/com/google/zxing/qrcode/decoder/VersionTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/VersionTestCase.java
new file mode 100644 (file)
index 0000000..75ed3dd
--- /dev/null
@@ -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());    
+  }
+
+}
\ No newline at end of file