3fb675e5f990f8648ccf44f85827585f79b20a83
[zxing.git] / core / test / src / com / google / zxing / common / reedsolomon / ReedSolomonDecoderQRCodeTestCase.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.common.reedsolomon;
18
19 import junit.framework.TestCase;
20
21 import java.util.BitSet;
22 import java.util.Random;
23
24 /**
25  * @author srowen@google.com (Sean Owen)
26  */
27 public final class ReedSolomonDecoderQRCodeTestCase extends TestCase {
28
29   /** See ISO 18004, Appendix I, from which this example is taken. */
30   private static final int[] QR_CODE_TEST =
31       { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
32         0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5 };
33   private static final int[] QR_CODE_TEST_WITH_EC =
34       { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
35         0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5, 0x24,
36         0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55 };
37   private static final int QR_CODE_CORRECTABLE = (QR_CODE_TEST_WITH_EC.length - QR_CODE_TEST.length) / 2;
38
39   private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
40
41   public void testNoError() throws ReedSolomonException {
42     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
43     System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
44     // no errors
45     checkQRRSDecode(received);
46   }
47
48   public void testOneError() throws ReedSolomonException {
49     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
50     Random random = new Random(0xDEADBEEFL);
51     for (int i = 0; i < received.length; i++) {
52       System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
53       received[i] = random.nextInt(256);
54       checkQRRSDecode(received);
55     }
56   }
57
58   public void testMaxErrors() throws ReedSolomonException {
59     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
60     Random random = new Random(0xDEADBEEFL);
61     for (int i = 0; i < QR_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
62       System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
63       corrupt(received, QR_CODE_CORRECTABLE, random);
64       checkQRRSDecode(received);
65     }
66   }
67
68   public void testTooManyErrors() {
69     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
70     System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
71     Random random = new Random(0xDEADBEEFL);
72     corrupt(received, QR_CODE_CORRECTABLE + 1, random);
73     try {
74       checkQRRSDecode(received);
75       fail("Should not have decoded");
76     } catch (ReedSolomonException rse) {
77       // good
78     }
79   }
80
81   private void checkQRRSDecode(int[] received) throws ReedSolomonException {
82     qrRSDecoder.decode(received, 2*QR_CODE_CORRECTABLE, false);
83     for (int i = 0; i < QR_CODE_TEST.length; i++) {
84       assertEquals(received[i], QR_CODE_TEST[i]);
85     }
86   }
87
88   private static void corrupt(int[] received, int howMany, Random random) {
89     BitSet corrupted = new BitSet(received.length);
90     for (int j = 0; j < howMany; j++) {
91       int location = random.nextInt(received.length);
92       if (corrupted.get(location)) {
93         j--;
94       } else {
95         corrupted.set(location);
96         int newByte = random.nextInt(256);
97         received[location] = newByte;
98       }
99     }
100   }
101 }