diff --git a/src/common/reedsolomon/GenericGFPolyTestCase.java b/src/common/reedsolomon/GenericGFPolyTestCase.java deleted file mode 100644 index 9174b84..0000000 --- a/src/common/reedsolomon/GenericGFPolyTestCase.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2018 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.common.reedsolomon; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Tests {@link GenericGFPoly}. - */ -public final class GenericGFPolyTestCase extends Assert { - - private static final GenericGF FIELD = GenericGF.QR_CODE_FIELD_256; - - @Test - public void testPolynomialString() { - assertEquals("0", FIELD.getZero().toString()); - assertEquals("-1", FIELD.buildMonomial(0, -1).toString()); - GenericGFPoly p = new GenericGFPoly(FIELD, new int[] {3, 0, -2, 1, 1}); - assertEquals("a^25x^4 - ax^2 + x + 1", p.toString()); - p = new GenericGFPoly(FIELD, new int[] {3}); - assertEquals("a^25", p.toString()); - } - - @Test - public void testZero() { - assertEquals(FIELD.getZero(),FIELD.buildMonomial(1, 0)); - assertEquals(FIELD.getZero(), FIELD.buildMonomial(1, 2).multiply(0)); - } - - @Test - public void testEvaluate() { - assertEquals(3, FIELD.buildMonomial(0, 3).evaluateAt(0)); - } - -} diff --git a/src/common/reedsolomon/GenericGFPolyTestCase.rs b/src/common/reedsolomon/GenericGFPolyTestCase.rs new file mode 100644 index 0000000..37aefff --- /dev/null +++ b/src/common/reedsolomon/GenericGFPolyTestCase.rs @@ -0,0 +1,49 @@ +/* + * Copyright 2018 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//package com.google.zxing.common.reedsolomon; + +//import org.junit.Assert; +//import org.junit.Test; + +use super::{GenericGF, GenericGFPoly}; + +/** + * Tests {@link GenericGFPoly}. + */ + + const FIELD :GenericGF= super::QR_CODE_FIELD_256; + + #[test] + fn testPolynomialString() { + assert_eq!("0", FIELD.getZero().to_string()); + assert_eq!("-1", FIELD.buildMonomial(0, -1).to_string()); + let p = GenericGFPoly::new(Box::new(FIELD), &vec![3, 0, -2, 1, 1]).unwrap(); + assert_eq!("a^25x^4 - ax^2 + x + 1", p.to_string()); + let p = GenericGFPoly::new(Box::new(FIELD), &vec![3]).unwrap(); + assert_eq!("a^25", p.to_string()); + } + + #[test] + fn testZero() { + assert_eq!(*FIELD.getZero(),*FIELD.buildMonomial(1, 0)); + assert_eq!(*FIELD.getZero(), *FIELD.buildMonomial(1, 2).multiply(0).unwrap()); + } + + #[test] + fn testEvaluate() { + assert_eq!(3, FIELD.buildMonomial(0, 3).evaluateAt(0)); + } \ No newline at end of file diff --git a/src/common/reedsolomon/ReedSolomonTestCase.java b/src/common/reedsolomon/ReedSolomonTestCase.rs similarity index 96% rename from src/common/reedsolomon/ReedSolomonTestCase.java rename to src/common/reedsolomon/ReedSolomonTestCase.rs index 5d62072..0122cfa 100644 --- a/src/common/reedsolomon/ReedSolomonTestCase.java +++ b/src/common/reedsolomon/ReedSolomonTestCase.rs @@ -14,25 +14,25 @@ * limitations under the License. */ -package com.google.zxing.common.reedsolomon; +//package com.google.zxing.common.reedsolomon; -import org.junit.Assert; -import org.junit.Test; +// import org.junit.Assert; +// import org.junit.Test; -import java.util.Arrays; -import java.util.BitSet; -import java.util.Random; +// import java.util.Arrays; +// import java.util.BitSet; +// import java.util.Random; /** * @author Rustam Abdullaev */ -public final class ReedSolomonTestCase extends Assert { +//public final class ReedSolomonTestCase extends Assert { - private static final int DECODER_RANDOM_TEST_ITERATIONS = 3; - private static final int DECODER_TEST_ITERATIONS = 10; + const DECODER_RANDOM_TEST_ITERATIONS :i32 = 3; + const DECODER_TEST_ITERATIONS :i32= 10; - @Test - public void testDataMatrix() { + #[test] + fn testDataMatrix() { // real life test cases testEncodeDecode(GenericGF.DATA_MATRIX_FIELD_256, new int[] { 142, 164, 186 }, new int[] { 114, 25, 5, 88, 102 }); @@ -55,8 +55,8 @@ public final class ReedSolomonTestCase extends Assert { testEncodeDecodeRandom(GenericGF.DATA_MATRIX_FIELD_256, 220, 35); } - @Test - public void testQRCode() { + #[test] + fn testQRCode() { // Test case from example given in ISO 18004, Annex I testEncodeDecode(GenericGF.QR_CODE_FIELD_256, new int[] { @@ -86,8 +86,8 @@ public final class ReedSolomonTestCase extends Assert { testEncodeDecodeRandom(GenericGF.QR_CODE_FIELD_256, 220, 35); } - @Test - public void testAztec() { + #[test] + fn testAztec() { // real life test cases testEncodeDecode(GenericGF.AZTEC_PARAM, new int[] { 0x5, 0x6 }, new int[] { 0x3, 0x2, 0xB, 0xB, 0x7 }); @@ -412,7 +412,7 @@ public final class ReedSolomonTestCase extends Assert { testEncodeDecodeRandom(GenericGF.AZTEC_DATA_12, 3072, 1023); } - public static void corrupt(int[] received, int howMany, Random random, int max) { + fn corrupt(int[] received, int howMany, Random random, int max) { BitSet corrupted = new BitSet(received.length); for (int j = 0; j < howMany; j++) { int location = random.nextInt(received.length); @@ -426,7 +426,7 @@ public final class ReedSolomonTestCase extends Assert { } } - private static void testEncodeDecodeRandom(GenericGF field, int dataSize, int ecSize) { + fn testEncodeDecodeRandom(GenericGF field, int dataSize, int ecSize) { assertTrue("Invalid data size for " + field, dataSize > 0 && dataSize <= field.getSize() - 3); assertTrue("Invalid ECC size for " + field, ecSize > 0 && ecSize + dataSize <= field.getSize()); ReedSolomonEncoder encoder = new ReedSolomonEncoder(field); @@ -449,12 +449,12 @@ public final class ReedSolomonTestCase extends Assert { } } - private static void testEncodeDecode(GenericGF field, int[] dataWords, int[] ecWords) { + fn testEncodeDecode(GenericGF field, int[] dataWords, int[] ecWords) { testEncoder(field, dataWords, ecWords); testDecoder(field, dataWords, ecWords); } - private static void testEncoder(GenericGF field, int[] dataWords, int[] ecWords) { + fn testEncoder(GenericGF field, int[] dataWords, int[] ecWords) { ReedSolomonEncoder encoder = new ReedSolomonEncoder(field); int[] messageExpected = new int[dataWords.length + ecWords.length]; int[] message = new int[dataWords.length + ecWords.length]; @@ -466,7 +466,7 @@ public final class ReedSolomonTestCase extends Assert { messageExpected, message); } - private static void testDecoder(GenericGF field, int[] dataWords, int[] ecWords) { + fn testDecoder(GenericGF field, int[] dataWords, int[] ecWords) { ReedSolomonDecoder decoder = new ReedSolomonDecoder(field); int[] message = new int[dataWords.length + ecWords.length]; int maxErrors = ecWords.length / 2; @@ -501,7 +501,7 @@ public final class ReedSolomonTestCase extends Assert { } } - private static void assertDataEquals(String message, int[] expected, int[] received) { + fn assertDataEquals(String message, int[] expected, int[] received) { for (int i = 0; i < expected.length; i++) { if (expected[i] != received[i]) { fail(message + ". Mismatch at " + i + ". Expected " + arrayToString(expected) + ", got " + @@ -510,7 +510,7 @@ public final class ReedSolomonTestCase extends Assert { } } - private static String arrayToString(int[] data) { + fn String arrayToString(int[] data) { StringBuilder sb = new StringBuilder("{"); for (int i = 0; i < data.length; i++) { sb.append(String.format(i > 0 ? ",%X" : "%X", data[i])); @@ -518,8 +518,8 @@ public final class ReedSolomonTestCase extends Assert { return sb.append('}').toString(); } - private static Random getPseudoRandom() { + fn Random getPseudoRandom() { return new Random(0xDEADBEEF); } -} +//} diff --git a/src/common/reedsolomon/mod.rs b/src/common/reedsolomon/mod.rs index cd46acc..0e039bc 100644 --- a/src/common/reedsolomon/mod.rs +++ b/src/common/reedsolomon/mod.rs @@ -3,6 +3,9 @@ use std::fmt; use crate::exceptions::*; use std::hash::Hash; +#[cfg(test)] +mod GenericGFPolyTestCase; + /* * Copyrigh&t 2007 ZXing authors * @@ -77,7 +80,8 @@ pub const MAXICODE_FIELD_64: GenericGF = AZTEC_DATA_6; * @author Sean Owen * @author David Olivier */ -pub struct GenericGF { +#[derive(Debug)] + pub struct GenericGF { expTable: Vec, logTable: Vec, zero: Box, @@ -251,11 +255,19 @@ impl fmt::Display for GenericGF { * * @author Sean Owen */ +#[derive(Debug)] pub struct GenericGFPoly { field: Box, coefficients: Vec, } +impl PartialEq for GenericGFPoly { + fn eq(&self, other: &Self) -> bool { + self.to_string() == other.to_string() + } +} +impl Eq for GenericGFPoly {} + impl GenericGFPoly { /** * @param field the {@link GenericGF} instance representing the field to use