98e582df61e4a162928559d4cfd0c34a2417b611
[zxing.git] / core / test / src / com / google / zxing / common / reedsolomon / ReedSolomonDecoderDataMatrixTestCase.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  * @author sanfordsquires
24  */
25 public final class ReedSolomonDecoderDataMatrixTestCase extends AbstractReedSolomonTestCase {
26
27   private static final int[] DM_CODE_TEST = { 142, 164, 186 };
28   private static final int[] DM_CODE_TEST_WITH_EC = { 142, 164, 186, 114, 25, 5, 88, 102 };
29   private static final int DM_CODE_ECC_BYTES = DM_CODE_TEST_WITH_EC.length - DM_CODE_TEST.length;
30   private static final int DM_CODE_CORRECTABLE = DM_CODE_ECC_BYTES / 2;
31
32   private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
33
34   public void testNoError() throws ReedSolomonException {
35     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
36     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
37     // no errors
38     checkQRRSDecode(received);
39   }
40
41   public void testOneError() throws ReedSolomonException {
42     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
43     Random random = new Random(0xDEADBEEFL);
44     for (int i = 0; i < received.length; i++) {
45       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
46       received[i] = random.nextInt(256);
47       checkQRRSDecode(received);
48     }
49   }
50
51   public void testMaxErrors() throws ReedSolomonException {
52     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
53     Random random = new Random(0xDEADBEEFL);
54     for (int i = 0; i < DM_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
55       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
56       corrupt(received, DM_CODE_CORRECTABLE, random);
57       checkQRRSDecode(received);
58     }
59   }
60
61   public void testTooManyErrors() {
62     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
63     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
64     Random random = new Random(0xDEADBEEFL);
65     corrupt(received, DM_CODE_CORRECTABLE + 1, random);
66     try {
67       checkQRRSDecode(received);
68       fail("Should not have decoded");
69     } catch (ReedSolomonException rse) {
70       // good
71     }
72   }
73
74   private void checkQRRSDecode(int[] received) throws ReedSolomonException {
75     dmRSDecoder.decode(received, DM_CODE_ECC_BYTES);
76     for (int i = 0; i < DM_CODE_TEST.length; i++) {
77       assertEquals(received[i], DM_CODE_TEST[i]);
78     }
79   }
80
81 }