2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.common.reedsolomon;
19 import org.junit.Test;
21 import java.util.Random;
25 * @author sanfordsquires
27 public final class ReedSolomonDecoderDataMatrixTestCase extends AbstractReedSolomonTestCase {
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;
34 private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
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);
41 checkQRRSDecode(received);
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);
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);
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);
73 checkQRRSDecode(received);
74 fail("Should not have decoded");
75 } catch (ReedSolomonException rse) {
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]);