Forgot tests for PDF417 EC

git-svn-id: https://zxing.googlecode.com/svn/trunk@2278 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-04-30 18:43:40 +00:00
parent 60288ac9e4
commit 9a168f5994
2 changed files with 143 additions and 0 deletions

View file

@ -0,0 +1,57 @@
/*
* Copyright 2012 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.pdf417.decoder.ec;
import org.junit.Assert;
import java.security.SecureRandom;
import java.util.BitSet;
import java.util.Random;
/**
* @author Sean Owen
*/
abstract class AbstractErrorCorrectionTestCase extends Assert {
static void corrupt(int[] received, int howMany, Random random) {
BitSet corrupted = new BitSet(received.length);
for (int j = 0; j < howMany; j++) {
int location = random.nextInt(received.length);
if (corrupted.get(location)) {
j--;
} else {
corrupted.set(location);
received[location] = random.nextInt(929);
}
}
}
static Random getRandom() {
return new SecureRandom(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF});
}
static void assertArraysEqual(int[] expected,
int expectedOffset,
int[] actual,
int actualOffset,
int length) {
for (int i = 0; i < length; i++) {
assertEquals(expected[expectedOffset + i], actual[actualOffset + i]);
}
}
}

View file

@ -0,0 +1,86 @@
/*
* Copyright 2012 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.pdf417.decoder.ec;
import com.google.zxing.ChecksumException;
import org.junit.Test;
import java.util.Random;
/**
* @author Sean Owen
*/
public final class ErrorCorrectionTestCase extends AbstractErrorCorrectionTestCase {
/** See ISO 15438, Annex Q */
private static final int[] PDF417_TEST =
{ 5, 453, 178, 121, 239 };
private static final int[] PDF417_TEST_WITH_EC =
{ 5, 453, 178, 121, 239, 452, 327, 657, 619 };
private static final int ECC_BYTES = PDF417_TEST_WITH_EC.length - PDF417_TEST.length;
private static final int CORRECTABLE = ECC_BYTES / 2;
private final ErrorCorrection ec = new ErrorCorrection();
@Test
public void testNoError() throws ChecksumException {
int[] received = PDF417_TEST_WITH_EC.clone();
// no errors
checkDecode(received);
}
@Test
public void testOneError() throws ChecksumException {
Random random = getRandom();
for (int i = 0; i < PDF417_TEST_WITH_EC.length; i++) {
int[] received = PDF417_TEST_WITH_EC.clone();
received[i] = random.nextInt(256);
checkDecode(received);
}
}
@Test
public void testMaxErrors() throws ChecksumException {
Random random = getRandom();
for (int test : PDF417_TEST) { // # iterations is kind of arbitrary
int[] received = PDF417_TEST_WITH_EC.clone();
corrupt(received, CORRECTABLE, random);
checkDecode(received);
}
}
@Test
public void testTooManyErrors() {
int[] received = PDF417_TEST_WITH_EC.clone();
Random random = getRandom();
corrupt(received, CORRECTABLE + 1, random);
try {
checkDecode(received);
fail("Should not have decoded");
} catch (ChecksumException ce) {
// good
}
}
private void checkDecode(int[] received) throws ChecksumException {
ec.decode(received, ECC_BYTES);
for (int i = 0; i < PDF417_TEST.length; i++) {
assertEquals(received[i], PDF417_TEST[i]);
}
}
}