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 java.util.Random;
22 * @author srowen@google.com (Sean Owen)
23 * @author sanfordsquires
25 public final class ReedSolomonDecoderDataMatrixTestCase extends AbstractReedSolomonTestCase {
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_CORRECTABLE = (DM_CODE_TEST_WITH_EC.length - DM_CODE_TEST.length) / 2;
31 private final ReedSolomonDecoder dmRSDecoder = new ReedSolomonDecoder(GF256.DATA_MATRIX_FIELD);
33 public void testNoError() throws ReedSolomonException {
34 int[] received = new int[DM_CODE_TEST_WITH_EC.length];
35 System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
37 checkQRRSDecode(received);
40 public void testOneError() throws ReedSolomonException {
41 int[] received = new int[DM_CODE_TEST_WITH_EC.length];
42 Random random = new Random(0xDEADBEEFL);
43 for (int i = 0; i < received.length; i++) {
44 System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
45 received[i] = random.nextInt(256);
46 checkQRRSDecode(received);
50 public void testMaxErrors() throws ReedSolomonException {
51 int[] received = new int[DM_CODE_TEST_WITH_EC.length];
52 Random random = new Random(0xDEADBEEFL);
53 for (int i = 0; i < DM_CODE_TEST.length; i++) { // # iterations is kind of arbitrary
54 System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
55 corrupt(received, DM_CODE_CORRECTABLE, random);
56 checkQRRSDecode(received);
60 public void testTooManyErrors() {
61 int[] received = new int[DM_CODE_TEST_WITH_EC.length];
62 System.arraycopy(DM_CODE_TEST_WITH_EC, 0, received, 0, received.length);
63 Random random = new Random(0xDEADBEEFL);
64 corrupt(received, DM_CODE_CORRECTABLE + 1, random);
66 checkQRRSDecode(received);
67 fail("Should not have decoded");
68 } catch (ReedSolomonException rse) {
73 private void checkQRRSDecode(int[] received) throws ReedSolomonException {
74 dmRSDecoder.decode(received, 2 * DM_CODE_CORRECTABLE);
75 for (int i = 0; i < DM_CODE_TEST.length; i++) {
76 assertEquals(received[i], DM_CODE_TEST[i]);