From 338dee705864bef582001c641617256a17885d3c Mon Sep 17 00:00:00 2001 From: srowen Date: Wed, 7 Nov 2007 06:52:45 +0000 Subject: [PATCH] Added more test cases git-svn-id: http://zxing.googlecode.com/svn/trunk@15 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../google/zxing/common/BitArrayTestCase.java | 66 +++++++++++++++++++ .../zxing/common/BitMatrixTestCase.java | 62 +++++++++++++++++ .../zxing/common/CollectionsTestCase.java | 45 +++++++++++++ .../qrcode/decoder/BitSourceTestCase.java | 46 +++++++++++++ .../qrcode/decoder/DataMaskTestCase.java | 7 +- .../decoder/FormatInformationTestCase.java | 20 ++---- 6 files changed, 226 insertions(+), 20 deletions(-) create mode 100644 core/test/src/com/google/zxing/common/BitArrayTestCase.java create mode 100644 core/test/src/com/google/zxing/common/BitMatrixTestCase.java create mode 100644 core/test/src/com/google/zxing/common/CollectionsTestCase.java create mode 100644 core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java diff --git a/core/test/src/com/google/zxing/common/BitArrayTestCase.java b/core/test/src/com/google/zxing/common/BitArrayTestCase.java new file mode 100644 index 00000000..6f203a68 --- /dev/null +++ b/core/test/src/com/google/zxing/common/BitArrayTestCase.java @@ -0,0 +1,66 @@ +/* + * 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.common; + +import junit.framework.TestCase; + +/** + * @author srowen@google.com (Sean Owen) + */ +public final class BitArrayTestCase extends TestCase { + + public void testGetSet() { + BitArray array = new BitArray(33); + for (int i = 0; i < 33; i++) { + assertFalse(array.get(i)); + array.set(i); + assertTrue(array.get(i)); + } + } + + public void testSetBulk() { + BitArray array = new BitArray(64); + array.setBulk(32, 0xFFFF0000); + for (int i = 0; i < 48; i++) { + assertFalse(array.get(i)); + } + for (int i = 48; i < 64; i++) { + assertTrue(array.get(i)); + } + } + + public void testClear() { + BitArray array = new BitArray(32); + for (int i = 0; i < 32; i++) { + array.set(i); + } + array.clear(); + for (int i = 0; i < 32; i++) { + assertFalse(array.get(i)); + } + } + + public void testGetArray() { + BitArray array = new BitArray(64); + array.set(0); + array.set(63); + int[] ints = array.getBitArray(); + assertEquals(1, ints[0]); + assertEquals(Integer.MIN_VALUE, ints[1]); + } + +} \ No newline at end of file diff --git a/core/test/src/com/google/zxing/common/BitMatrixTestCase.java b/core/test/src/com/google/zxing/common/BitMatrixTestCase.java new file mode 100644 index 00000000..541ba06b --- /dev/null +++ b/core/test/src/com/google/zxing/common/BitMatrixTestCase.java @@ -0,0 +1,62 @@ +/* + * 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.common; + +import junit.framework.TestCase; + +/** + * @author srowen@google.com (Sean Owen) + */ +public final class BitMatrixTestCase extends TestCase { + + public void testGetSet() { + BitMatrix matrix = new BitMatrix(33); + assertEquals(33, matrix.getDimension()); + for (int i = 0; i < 33; i++) { + for (int j = 0; j < 33; j++) { + if (i * j % 3 == 0) { + matrix.set(i, j); + } + } + } + for (int i = 0; i < 33; i++) { + for (int j = 0; j < 33; j++) { + assertEquals(i * j % 3 == 0, matrix.get(i, j)); + } + } + } + + public void testSetRegion() { + BitMatrix matrix = new BitMatrix(5); + matrix.setRegion(1, 1, 3, 3); + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + assertEquals(i >= 1 && i <= 3 && j >= 1 && j <= 3, matrix.get(i, j)); + } + } + } + + public void testGetBits() { + BitMatrix matrix = new BitMatrix(6); + matrix.set(0, 0); + matrix.set(5, 5); + int[] bits = matrix.getBits(); + assertEquals(1, bits[0]); + assertEquals(8, bits[1]); + } + +} \ No newline at end of file diff --git a/core/test/src/com/google/zxing/common/CollectionsTestCase.java b/core/test/src/com/google/zxing/common/CollectionsTestCase.java new file mode 100644 index 00000000..55b89f96 --- /dev/null +++ b/core/test/src/com/google/zxing/common/CollectionsTestCase.java @@ -0,0 +1,45 @@ +/* + * 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.common; + +import junit.framework.TestCase; + +import java.util.Random; +import java.util.Vector; + +/** + * @author srowen@google.com (Sean Owen) + */ +public final class CollectionsTestCase extends TestCase { + + public void testSort() { + Random r = new Random(0xDEADBEEFL); + Vector v = new Vector(); + for (int i = 0; i < 100; i++) { + v.addElement(new Integer(r.nextInt(1000))); + } + Collections.insertionSort(v, new Comparator() { + public int compare(Object o1, Object o2) { + return (Integer) o1 - (Integer) o2; + } + }); + for (int i = 1; i < 100; i++) { + assertTrue("Element " + i, (Integer) v.elementAt(i-1) <= (Integer) v.elementAt(i)); + } + } + +} \ No newline at end of file diff --git a/core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java new file mode 100644 index 00000000..6990fe3e --- /dev/null +++ b/core/test/src/com/google/zxing/qrcode/decoder/BitSourceTestCase.java @@ -0,0 +1,46 @@ +/* + * 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/DataMaskTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/DataMaskTestCase.java index 6d8011a0..99821684 100644 --- a/core/test/src/com/google/zxing/qrcode/decoder/DataMaskTestCase.java +++ b/core/test/src/com/google/zxing/qrcode/decoder/DataMaskTestCase.java @@ -18,10 +18,9 @@ package com.google.zxing.qrcode.decoder; import com.google.zxing.common.BitMatrix; import junit.framework.TestCase; -import junit.textui.TestRunner; /** - * @author Sean Owen + * @author srowen@google.com (Sean Owen) */ public final class DataMaskTestCase extends TestCase { @@ -115,8 +114,4 @@ public final class DataMaskTestCase extends TestCase { boolean isMasked(int i, int j); } - public static void main(String[] args) { - TestRunner.run(new DataMaskTestCase()); - } - } diff --git a/core/test/src/com/google/zxing/qrcode/decoder/FormatInformationTestCase.java b/core/test/src/com/google/zxing/qrcode/decoder/FormatInformationTestCase.java index 9edb310e..cd810cbc 100644 --- a/core/test/src/com/google/zxing/qrcode/decoder/FormatInformationTestCase.java +++ b/core/test/src/com/google/zxing/qrcode/decoder/FormatInformationTestCase.java @@ -17,10 +17,9 @@ package com.google.zxing.qrcode.decoder; import junit.framework.TestCase; -import junit.textui.TestRunner; /** - * @author Sean Owen + * @author srowen@google.com (Sean Owen) */ public final class FormatInformationTestCase extends TestCase { @@ -33,24 +32,17 @@ public final class FormatInformationTestCase extends TestCase { public void testDecode() { // Normal case - FormatInformation expected = - FormatInformation.decodeFormatInformation(0x2BED ^ 0x5412); + FormatInformation expected = FormatInformation.decodeFormatInformation(0x2BED ^ 0x5412); assertEquals((byte) 0x07, expected.getDataMask()); assertEquals(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel()); // where the code forgot the mask! assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BED)); + // 1,2,3,4 bits difference - assertEquals(expected, FormatInformation.decodeFormatInformation( - 0x2BEF ^ 0x5412)); - assertEquals(expected, FormatInformation.decodeFormatInformation( - 0x2BEE ^ 0x5412)); - assertEquals(expected, FormatInformation.decodeFormatInformation( - 0x2BEA ^ 0x5412)); + assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEF ^ 0x5412)); + assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEE ^ 0x5412)); + assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEA ^ 0x5412)); assertNull(FormatInformation.decodeFormatInformation(0x2BE2 ^ 0x5412)); } - public static void main(String[] args) { - TestRunner.run(new DataMaskTestCase()); - } - } \ No newline at end of file -- 2.20.1