From 9a168f59945fa4aaa613e4e924ce6208750c10de Mon Sep 17 00:00:00 2001 From: srowen Date: Mon, 30 Apr 2012 18:43:40 +0000 Subject: [PATCH] Forgot tests for PDF417 EC git-svn-id: https://zxing.googlecode.com/svn/trunk@2278 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../ec/AbstractErrorCorrectionTestCase.java | 57 ++++++++++++ .../decoder/ec/ErrorCorrectionTestCase.java | 86 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 core/test/src/com/google/zxing/pdf417/decoder/ec/AbstractErrorCorrectionTestCase.java create mode 100644 core/test/src/com/google/zxing/pdf417/decoder/ec/ErrorCorrectionTestCase.java diff --git a/core/test/src/com/google/zxing/pdf417/decoder/ec/AbstractErrorCorrectionTestCase.java b/core/test/src/com/google/zxing/pdf417/decoder/ec/AbstractErrorCorrectionTestCase.java new file mode 100644 index 000000000..10a7f94a9 --- /dev/null +++ b/core/test/src/com/google/zxing/pdf417/decoder/ec/AbstractErrorCorrectionTestCase.java @@ -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]); + } + } + +} \ No newline at end of file diff --git a/core/test/src/com/google/zxing/pdf417/decoder/ec/ErrorCorrectionTestCase.java b/core/test/src/com/google/zxing/pdf417/decoder/ec/ErrorCorrectionTestCase.java new file mode 100644 index 000000000..d89951c7f --- /dev/null +++ b/core/test/src/com/google/zxing/pdf417/decoder/ec/ErrorCorrectionTestCase.java @@ -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]); + } + } + +} \ No newline at end of file