Updates from sanfordsquires to fix RS decoding for Datamatrix
[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 java.util.Random;
20
21 /**
22  * @author srowen@google.com (Sean Owen)
23  */
24 public final class ReedSolomonDecoderQRCodeTestCase extends AbstractReedSolomonTestCase {
25
26   /** See ISO 18004, Appendix I, from which this example is taken. */
27   private static final int[] QR_CODE_TEST =
28       { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
29         0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5 };
30   private static final int[] QR_CODE_TEST_WITH_EC =
31       { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC,
32         0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xA5, 0x24,
33         0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55 };
34   private static final int QR_CODE_CORRECTABLE = (QR_CODE_TEST_WITH_EC.length - QR_CODE_TEST.length) / 2;
35
36   private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
37
38   public void testNoError() throws ReedSolomonException {
39     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
40     System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
41     // no errors
42     checkQRRSDecode(received);
43   }
44
45   public void testOneError() throws ReedSolomonException {
46     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
47     Random random = new Random(0xDEADBEEFL);
48     for (int i = 0; i < received.length; i++) {
49       System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
50       received[i] = random.nextInt(256);
51       checkQRRSDecode(received);
52     }
53   }
54
55   public void testMaxErrors() throws ReedSolomonException {
56     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
57     Random random = new Random(0xDEADBEEFL);
58     for (int i = 0; i < QR_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
59       System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
60       corrupt(received, QR_CODE_CORRECTABLE, random);
61       checkQRRSDecode(received);
62     }
63   }
64
65   public void testTooManyErrors() {
66     int[] received = new int[QR_CODE_TEST_WITH_EC.length];
67     System.arraycopy(QR_CODE_TEST_WITH_EC, 0, received, 0, received.length);
68     Random random = new Random(0xDEADBEEFL);
69     corrupt(received, QR_CODE_CORRECTABLE + 1, random);
70     try {
71       checkQRRSDecode(received);
72       fail("Should not have decoded");
73     } catch (ReedSolomonException rse) {
74       // good
75     }
76   }
77
78   private void checkQRRSDecode(int[] received) throws ReedSolomonException {
79     qrRSDecoder.decode(received, 2*QR_CODE_CORRECTABLE);
80     for (int i = 0; i < QR_CODE_TEST.length; i++) {
81       assertEquals(received[i], QR_CODE_TEST[i]);
82     }
83   }
84
85 }