2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common.reedsolomon;
19 import org.junit.Test;
21 import java.util.Random;
26 public final class ReedSolomonDecoderQRCodeTestCase extends AbstractReedSolomonTestCase {
28 /** See ISO 18004, Appendix I, from which this example is taken. */
29 private static final int[] QR_CODE_TEST =
30 { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
31 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11 };
32 private static final int[] QR_CODE_TEST_WITH_EC =
33 { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
34 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5, 0x24,
35 0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55 };
36 private static final int QR_CODE_ECC_BYTES = QR_CODE_TEST_WITH_EC.length - QR_CODE_TEST.length;
37 private static final int QR_CODE_CORRECTABLE = QR_CODE_ECC_BYTES / 2;
39 private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
42 public void testNoError() throws ReedSolomonException {
43 int[] received = new int[QR_CODE_TEST_WITH_EC.length];
44 System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
46 checkQRRSDecode(received);
50 public void testOneError() throws ReedSolomonException {
51 int[] received = new int[QR_CODE_TEST_WITH_EC.length];
52 Random random = getRandom();
53 for (int i = 0; i < received.length; i++) {
54 System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
55 received[i] = random.nextInt(256);
56 checkQRRSDecode(received);
61 public void testMaxErrors() throws ReedSolomonException {
62 int[] received = new int[QR_CODE_TEST_WITH_EC.length];
63 Random random = getRandom();
64 for (int test : QR_CODE_TEST) { // # iterations is kind of arbitrary
65 System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
66 corrupt(received, QR_CODE_CORRECTABLE, random);
67 checkQRRSDecode(received);
72 public void testTooManyErrors() {
73 int[] received = new int[QR_CODE_TEST_WITH_EC.length];
74 System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
75 Random random = getRandom();
76 corrupt(received, QR_CODE_CORRECTABLE + 1, random);
78 checkQRRSDecode(received);
79 fail("Should not have decoded");
80 } catch (ReedSolomonException rse) {
85 private void checkQRRSDecode(int[] received) throws ReedSolomonException {
86 qrRSDecoder.decode(received, QR_CODE_ECC_BYTES);
87 for (int i = 0; i < QR_CODE_TEST.length; i++) {
88 assertEquals(received[i], QR_CODE_TEST[i]);