Added more test cases

git-svn-id: https://zxing.googlecode.com/svn/trunk@15 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2007-11-07 06:52:45 +00:00
parent 0aeb2f672c
commit 9b296c7e1e
6 changed files with 226 additions and 20 deletions

View file

@ -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]);
}
}

View file

@ -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]);
}
}

View file

@ -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));
}
}
}

View file

@ -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());
}
}

View file

@ -18,10 +18,9 @@ package com.google.zxing.qrcode.decoder;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.textui.TestRunner;
/** /**
* @author Sean Owen * @author srowen@google.com (Sean Owen)
*/ */
public final class DataMaskTestCase extends TestCase { public final class DataMaskTestCase extends TestCase {
@ -115,8 +114,4 @@ public final class DataMaskTestCase extends TestCase {
boolean isMasked(int i, int j); boolean isMasked(int i, int j);
} }
public static void main(String[] args) {
TestRunner.run(new DataMaskTestCase());
}
} }

View file

@ -17,10 +17,9 @@
package com.google.zxing.qrcode.decoder; package com.google.zxing.qrcode.decoder;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.textui.TestRunner;
/** /**
* @author Sean Owen * @author srowen@google.com (Sean Owen)
*/ */
public final class FormatInformationTestCase extends TestCase { public final class FormatInformationTestCase extends TestCase {
@ -33,24 +32,17 @@ public final class FormatInformationTestCase extends TestCase {
public void testDecode() { public void testDecode() {
// Normal case // Normal case
FormatInformation expected = FormatInformation expected = FormatInformation.decodeFormatInformation(0x2BED ^ 0x5412);
FormatInformation.decodeFormatInformation(0x2BED ^ 0x5412);
assertEquals((byte) 0x07, expected.getDataMask()); assertEquals((byte) 0x07, expected.getDataMask());
assertEquals(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel()); assertEquals(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel());
// where the code forgot the mask! // where the code forgot the mask!
assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BED)); assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BED));
// 1,2,3,4 bits difference // 1,2,3,4 bits difference
assertEquals(expected, FormatInformation.decodeFormatInformation( assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEF ^ 0x5412));
0x2BEF ^ 0x5412)); assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEE ^ 0x5412));
assertEquals(expected, FormatInformation.decodeFormatInformation( assertEquals(expected, FormatInformation.decodeFormatInformation(0x2BEA ^ 0x5412));
0x2BEE ^ 0x5412));
assertEquals(expected, FormatInformation.decodeFormatInformation(
0x2BEA ^ 0x5412));
assertNull(FormatInformation.decodeFormatInformation(0x2BE2 ^ 0x5412)); assertNull(FormatInformation.decodeFormatInformation(0x2BE2 ^ 0x5412));
} }
public static void main(String[] args) {
TestRunner.run(new DataMaskTestCase());
}
} }