At last update to JUnit 4.x
[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 org.junit.Test;
20
21 import java.util.Random;
22
23 /**
24  * @author Sean Owen
25  * @author sanfordsquires
26  */
27 public final class ReedSolomonDecoderDataMatrixTestCase extends AbstractReedSolomonTestCase {
28
29   private static final int[] DM_CODE_TEST = { 142, 164, 186 };
30   private static final int[] DM_CODE_TEST_WITH_EC = { 142, 164, 186, 114, 25, 5, 88, 102 };
31   private static final int DM_CODE_ECC_BYTES = DM_CODE_TEST_WITH_EC.length - DM_CODE_TEST.length;
32   private static final int DM_CODE_CORRECTABLE = DM_CODE_ECC_BYTES / 2;
33
34   private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
35
36   @Test
37   public void testNoError() throws ReedSolomonException {
38     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
39     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
40     // no errors
41     checkQRRSDecode(received);
42   }
43
44   @Test
45   public void testOneError() throws ReedSolomonException {
46     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
47     Random random = getRandom();
48     for (int i = 0; i < received.length; i++) {
49       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
50       received[i] = random.nextInt(256);
51       checkQRRSDecode(received);
52     }
53   }
54
55   @Test
56   public void testMaxErrors() throws ReedSolomonException {
57     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
58     Random random = getRandom();
59     for (int test : DM_CODE_TEST) { // # iterations is kind of arbitrary
60       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
61       corrupt(received, DM_CODE_CORRECTABLE, random);
62       checkQRRSDecode(received);
63     }
64   }
65
66   @Test
67   public void testTooManyErrors() {
68     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
69     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
70     Random random = getRandom();
71     corrupt(received, DM_CODE_CORRECTABLE + 1, random);
72     try {
73       checkQRRSDecode(received);
74       fail("Should not have decoded");
75     } catch (ReedSolomonException rse) {
76       // good
77     }
78   }
79
80   private void checkQRRSDecode(int[] received) throws ReedSolomonException {
81     dmRSDecoder.decode(received, DM_CODE_ECC_BYTES);
82     for (int i = 0; i < DM_CODE_TEST.length; i++) {
83       assertEquals(received[i], DM_CODE_TEST[i]);
84     }
85   }
86
87 }