657c8a467a86e17d6bdca04851adca70d58c81c5
[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 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 ReedSolomonDecoderDataMatrixTestCase extends TestCase {
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_CORRECTABLE = (DM_CODE_TEST_WITH_EC.length - DM_CODE_TEST.length) / 2;
32
33   private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
34
35   public void testNoError() throws ReedSolomonException {
36     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
37     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
38     // no errors
39     checkQRRSDecode(received);
40   }
41
42   public void testOneError() throws ReedSolomonException {
43     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
44     Random random = new Random(0xDEADBEEFL);
45     for (int i = 0; i < received.length; i++) {
46       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
47       received[i] = random.nextInt(256);
48       checkQRRSDecode(received);
49     }
50   }
51
52   public void testMaxErrors() throws ReedSolomonException {
53     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
54     Random random = new Random(0xDEADBEEFL);
55     for (int i = 0; i < DM_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
56       System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
57       // TODO we aren't testing max errors really since there is a known bug here
58       corrupt(received, DM_CODE_CORRECTABLE - 1, random);
59       checkQRRSDecode(received);
60     }
61   }
62
63   public void testTooManyErrors() {
64     int[] received = new int[DM_CODE_TEST_WITH_EC.length];
65     System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
66     Random random = new Random(0xDEADBEEFL);
67     corrupt(received, DM_CODE_CORRECTABLE + 1, random);
68     try {
69       checkQRRSDecode(received);
70       fail("Should not have decoded");
71     } catch (ReedSolomonException rse) {
72       // good
73     }
74   }
75
76   private void checkQRRSDecode(int[] received) throws ReedSolomonException {
77     dmRSDecoder.decode(received, 2 * DM_CODE_CORRECTABLE, true);
78     for (int i = 0; i < DM_CODE_TEST.length; i++) {
79       assertEquals(received[i], DM_CODE_TEST[i]);
80     }
81   }
82
83   private static void corrupt(int[] received, int howMany, Random random) {
84     BitSet corrupted = new BitSet(received.length);
85     for (int j = 0; j < howMany; j++) {
86       int location = random.nextInt(received.length);
87       if (corrupted.get(location)) {
88         j--;
89       } else {
90         corrupted.set(location);
91         int newByte = random.nextInt(256);
92         received[location] = newByte;
93       }
94     }
95   }
96 }