Small style stuff
[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 org.junit.Test;
20
21 import java.util.Random;
22
23 /**
24  * @author Sean Owen
25  */
26 public final class ReedSolomonDecoderQRCodeTestCase extends AbstractReedSolomonTestCase {
27
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;
38
39   private final ReedSolomonDecoder qrRSDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
40
41   @Test
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);
45     // no errors
46     checkQRRSDecode(received);
47   }
48
49   @Test
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);
57     }
58   }
59
60   @Test
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);
68     }
69   }
70
71   @Test
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);
77     try {
78       checkQRRSDecode(received);
79       fail("Should not have decoded");
80     } catch (ReedSolomonException rse) {
81       // good
82     }
83   }
84
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]);
89     }
90   }
91
92 }