diff --git a/src/common/BitMatrixTestCase.java b/src/common/BitMatrixTestCase.java deleted file mode 100644 index b92705e..0000000 --- a/src/common/BitMatrixTestCase.java +++ /dev/null @@ -1,347 +0,0 @@ -/* - * Copyright 2007 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; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.Arrays; - -/** - * @author Sean Owen - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class BitMatrixTestCase extends Assert { - - private static final int[] BIT_MATRIX_POINTS = { 1, 2, 2, 0, 3, 1 }; - - @Test - public void testGetSet() { - BitMatrix matrix = new BitMatrix(33); - assertEquals(33, matrix.getHeight()); - for (int y = 0; y < 33; y++) { - for (int x = 0; x < 33; x++) { - if (y * x % 3 == 0) { - matrix.set(x, y); - } - } - } - for (int y = 0; y < 33; y++) { - for (int x = 0; x < 33; x++) { - assertEquals(y * x % 3 == 0, matrix.get(x, y)); - } - } - } - - @Test - public void testSetRegion() { - BitMatrix matrix = new BitMatrix(5); - matrix.setRegion(1, 1, 3, 3); - for (int y = 0; y < 5; y++) { - for (int x = 0; x < 5; x++) { - assertEquals(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y)); - } - } - } - - @Test - public void testEnclosing() { - BitMatrix matrix = new BitMatrix(5); - assertNull(matrix.getEnclosingRectangle()); - matrix.setRegion(1, 1, 1, 1); - assertArrayEquals(new int[] { 1, 1, 1, 1 }, matrix.getEnclosingRectangle()); - matrix.setRegion(1, 1, 3, 2); - assertArrayEquals(new int[] { 1, 1, 3, 2 }, matrix.getEnclosingRectangle()); - matrix.setRegion(0, 0, 5, 5); - assertArrayEquals(new int[] { 0, 0, 5, 5 }, matrix.getEnclosingRectangle()); - } - - @Test - public void testOnBit() { - BitMatrix matrix = new BitMatrix(5); - assertNull(matrix.getTopLeftOnBit()); - assertNull(matrix.getBottomRightOnBit()); - matrix.setRegion(1, 1, 1, 1); - assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit()); - assertArrayEquals(new int[] { 1, 1 }, matrix.getBottomRightOnBit()); - matrix.setRegion(1, 1, 3, 2); - assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit()); - assertArrayEquals(new int[] { 3, 2 }, matrix.getBottomRightOnBit()); - matrix.setRegion(0, 0, 5, 5); - assertArrayEquals(new int[] { 0, 0 }, matrix.getTopLeftOnBit()); - assertArrayEquals(new int[] { 4, 4 }, matrix.getBottomRightOnBit()); - } - - @Test - public void testRectangularMatrix() { - BitMatrix matrix = new BitMatrix(75, 20); - assertEquals(75, matrix.getWidth()); - assertEquals(20, matrix.getHeight()); - matrix.set(10, 0); - matrix.set(11, 1); - matrix.set(50, 2); - matrix.set(51, 3); - matrix.flip(74, 4); - matrix.flip(0, 5); - - // Should all be on - assertTrue(matrix.get(10, 0)); - assertTrue(matrix.get(11, 1)); - assertTrue(matrix.get(50, 2)); - assertTrue(matrix.get(51, 3)); - assertTrue(matrix.get(74, 4)); - assertTrue(matrix.get(0, 5)); - - // Flip a couple back off - matrix.flip(50, 2); - matrix.flip(51, 3); - assertFalse(matrix.get(50, 2)); - assertFalse(matrix.get(51, 3)); - } - - @Test - public void testRectangularSetRegion() { - BitMatrix matrix = new BitMatrix(320, 240); - assertEquals(320, matrix.getWidth()); - assertEquals(240, matrix.getHeight()); - matrix.setRegion(105, 22, 80, 12); - - // Only bits in the region should be on - for (int y = 0; y < 240; y++) { - for (int x = 0; x < 320; x++) { - assertEquals(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y)); - } - } - } - - @Test - public void testGetRow() { - BitMatrix matrix = new BitMatrix(102, 5); - for (int x = 0; x < 102; x++) { - if ((x & 0x03) == 0) { - matrix.set(x, 2); - } - } - - // Should allocate - BitArray array = matrix.getRow(2, null); - assertEquals(102, array.getSize()); - - // Should reallocate - BitArray array2 = new BitArray(60); - array2 = matrix.getRow(2, array2); - assertEquals(102, array2.getSize()); - - // Should use provided object, with original BitArray size - BitArray array3 = new BitArray(200); - array3 = matrix.getRow(2, array3); - assertEquals(200, array3.getSize()); - - for (int x = 0; x < 102; x++) { - boolean on = (x & 0x03) == 0; - assertEquals(on, array.get(x)); - assertEquals(on, array2.get(x)); - assertEquals(on, array3.get(x)); - } - } - - @Test - public void testRotate90Simple() { - BitMatrix matrix = new BitMatrix(3, 3); - matrix.set(0, 0); - matrix.set(0, 1); - matrix.set(1, 2); - matrix.set(2, 1); - - matrix.rotate90(); - - assertTrue(matrix.get(0, 2)); - assertTrue(matrix.get(1, 2)); - assertTrue(matrix.get(2, 1)); - assertTrue(matrix.get(1, 0)); - } - - @Test - public void testRotate180Simple() { - BitMatrix matrix = new BitMatrix(3, 3); - matrix.set(0, 0); - matrix.set(0, 1); - matrix.set(1, 2); - matrix.set(2, 1); - - matrix.rotate180(); - - assertTrue(matrix.get(2, 2)); - assertTrue(matrix.get(2, 1)); - assertTrue(matrix.get(1, 0)); - assertTrue(matrix.get(0, 1)); - } - - @Test - public void testRotate180() { - testRotate180(7, 4); - testRotate180(7, 5); - testRotate180(8, 4); - testRotate180(8, 5); - } - - @Test - public void testParse() { - BitMatrix emptyMatrix = new BitMatrix(3, 3); - BitMatrix fullMatrix = new BitMatrix(3, 3); - fullMatrix.setRegion(0, 0, 3, 3); - BitMatrix centerMatrix = new BitMatrix(3, 3); - centerMatrix.setRegion(1, 1, 1, 1); - BitMatrix emptyMatrix24 = new BitMatrix(2, 4); - - assertEquals(emptyMatrix, BitMatrix.parse(" \n \n \n", "x", " ")); - assertEquals(emptyMatrix, BitMatrix.parse(" \n \r\r\n \n\r", "x", " ")); - assertEquals(emptyMatrix, BitMatrix.parse(" \n \n ", "x", " ")); - - assertEquals(fullMatrix, BitMatrix.parse("xxx\nxxx\nxxx\n", "x", " ")); - - assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x", " ")); - assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x ", " ")); - try { - assertEquals(centerMatrix, BitMatrix.parse(" \n xy\n \n", "x", " ")); - fail(); - } catch (IllegalArgumentException ex) { - // good - } - - assertEquals(emptyMatrix24, BitMatrix.parse(" \n \n \n \n", "x", " ")); - - assertEquals(centerMatrix, BitMatrix.parse(centerMatrix.toString("x", "."), "x", ".")); - } - - @Test - public void testParseBoolean() { - BitMatrix emptyMatrix = new BitMatrix(3, 3); - BitMatrix fullMatrix = new BitMatrix(3, 3); - fullMatrix.setRegion(0, 0, 3, 3); - BitMatrix centerMatrix = new BitMatrix(3, 3); - centerMatrix.setRegion(1, 1, 1, 1); - BitMatrix emptyMatrix24 = new BitMatrix(2, 4); - - boolean[][] matrix = new boolean[3][3]; - assertEquals(emptyMatrix, BitMatrix.parse(matrix)); - matrix[1][1] = true; - assertEquals(centerMatrix, BitMatrix.parse(matrix)); - for (boolean[] arr : matrix) { - Arrays.fill(arr, true); - } - assertEquals(fullMatrix, BitMatrix.parse(matrix)); - } - - @Test - public void testUnset() { - BitMatrix emptyMatrix = new BitMatrix(3, 3); - BitMatrix matrix = emptyMatrix.clone(); - matrix.set(1, 1); - assertNotEquals(emptyMatrix, matrix); - matrix.unset(1, 1); - assertEquals(emptyMatrix, matrix); - matrix.unset(1, 1); - assertEquals(emptyMatrix, matrix); - } - - @Test - public void testXOR() { - BitMatrix emptyMatrix = new BitMatrix(3, 3); - BitMatrix fullMatrix = new BitMatrix(3, 3); - fullMatrix.setRegion(0, 0, 3, 3); - BitMatrix centerMatrix = new BitMatrix(3, 3); - centerMatrix.setRegion(1, 1, 1, 1); - BitMatrix invertedCenterMatrix = fullMatrix.clone(); - invertedCenterMatrix.unset(1, 1); - BitMatrix badMatrix = new BitMatrix(4, 4); - - testXOR(emptyMatrix, emptyMatrix, emptyMatrix); - testXOR(emptyMatrix, centerMatrix, centerMatrix); - testXOR(emptyMatrix, fullMatrix, fullMatrix); - - testXOR(centerMatrix, emptyMatrix, centerMatrix); - testXOR(centerMatrix, centerMatrix, emptyMatrix); - testXOR(centerMatrix, fullMatrix, invertedCenterMatrix); - - testXOR(invertedCenterMatrix, emptyMatrix, invertedCenterMatrix); - testXOR(invertedCenterMatrix, centerMatrix, fullMatrix); - testXOR(invertedCenterMatrix, fullMatrix, centerMatrix); - - testXOR(fullMatrix, emptyMatrix, fullMatrix); - testXOR(fullMatrix, centerMatrix, invertedCenterMatrix); - testXOR(fullMatrix, fullMatrix, emptyMatrix); - - try { - emptyMatrix.clone().xor(badMatrix); - fail(); - } catch (IllegalArgumentException ex) { - // good - } - - try { - badMatrix.clone().xor(emptyMatrix); - fail(); - } catch (IllegalArgumentException ex) { - // good - } - } - - public static String matrixToString(BitMatrix result) { - assertEquals(1, result.getHeight()); - StringBuilder builder = new StringBuilder(result.getWidth()); - for (int i = 0; i < result.getWidth(); i++) { - builder.append(result.get(i, 0) ? '1' : '0'); - } - return builder.toString(); - } - - private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) { - BitMatrix matrix = dataMatrix.clone(); - matrix.xor(flipMatrix); - assertEquals(expectedMatrix, matrix); - } - - private static void testRotate180(int width, int height) { - BitMatrix input = getInput(width, height); - input.rotate180(); - BitMatrix expected = getExpected(width, height); - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - assertEquals("(" + x + ',' + y + ')', expected.get(x, y), input.get(x, y)); - } - } - } - - private static BitMatrix getExpected(int width, int height) { - BitMatrix result = new BitMatrix(width, height); - for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) { - result.set(width - 1 - BIT_MATRIX_POINTS[i], height - 1 - BIT_MATRIX_POINTS[i + 1]); - } - return result; - } - - private static BitMatrix getInput(int width, int height) { - BitMatrix result = new BitMatrix(width, height); - for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) { - result.set(BIT_MATRIX_POINTS[i], BIT_MATRIX_POINTS[i + 1]); - } - return result; - } - -} diff --git a/src/common/BitMatrixTestCase.rs b/src/common/BitMatrixTestCase.rs new file mode 100644 index 0000000..04cfb5a --- /dev/null +++ b/src/common/BitMatrixTestCase.rs @@ -0,0 +1,371 @@ +/* + * Copyright 2007 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; + +// import org.junit.Assert; +// import org.junit.Test; + +// import java.util.Arrays; + +// /** +// * @author Sean Owen +// * @author dswitkin@google.com (Daniel Switkin) +// */ +// public final class BitMatrixTestCase extends Assert { + +use crate::common::BitArray; + +use super::BitMatrix; + + static BIT_MATRIX_POINTS : [u32;6] = [ 1, 2, 2, 0, 3, 1 ]; + + #[test] + fn test_get_set() { + let mut matrix = BitMatrix::with_single_dimension(33); + assert_eq!(33, matrix.getHeight()); + for y in 0..33 { + // for (int y = 0; y < 33; y++) { + for x in 0..33 { + // for (int x = 0; x < 33; x++) { + if y * x % 3 == 0 { + matrix.set(x, y); + } + } + } + for y in 0..33 { + // for (int y = 0; y < 33; y++) { + for x in 0..33 { + // for (int x = 0; x < 33; x++) { + assert_eq!(y * x % 3 == 0, matrix.get(x, y)); + } + } + } + + #[test] + fn test_set_region() { + let mut matrix = BitMatrix::with_single_dimension(5); + matrix.setRegion(1, 1, 3, 3); + for y in 0..5 { + // for (int y = 0; y < 5; y++) { + for x in 0..5{ + // for (int x = 0; x < 5; x++) { + assert_eq!(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y)); + } + } + } + + #[test] + fn test_enclosing() { + let mut matrix = BitMatrix::with_single_dimension(5); + assert!(matrix.getEnclosingRectangle().is_none()); + matrix.setRegion(1, 1, 1, 1); + assert_eq!(vec![ 1, 1, 1, 1 ], matrix.getEnclosingRectangle().unwrap()); + matrix.setRegion(1, 1, 3, 2); + assert_eq!(vec![ 1, 1, 3, 2 ], matrix.getEnclosingRectangle().unwrap()); + matrix.setRegion(0, 0, 5, 5); + assert_eq!(vec![ 0, 0, 5, 5 ], matrix.getEnclosingRectangle().unwrap()); + } + + #[test] + fn test_on_bit() { + let mut matrix = BitMatrix::with_single_dimension(5); + assert!(matrix.getTopLeftOnBit().is_none()); + assert!(matrix.getBottomRightOnBit().is_none()); + matrix.setRegion(1, 1, 1, 1); + assert_eq!(vec![ 1, 1 ], matrix.getTopLeftOnBit().unwrap()); + assert_eq!(vec![ 1, 1 ], matrix.getBottomRightOnBit().unwrap()); + matrix.setRegion(1, 1, 3, 2); + assert_eq!(vec![ 1, 1 ], matrix.getTopLeftOnBit().unwrap()); + assert_eq!(vec![ 3, 2 ], matrix.getBottomRightOnBit().unwrap()); + matrix.setRegion(0, 0, 5, 5); + assert_eq!(vec![ 0, 0 ], matrix.getTopLeftOnBit().unwrap()); + assert_eq!(vec![ 4, 4 ], matrix.getBottomRightOnBit().unwrap()); + } + + #[test] + fn test_rectangular_matrix() { + let mut matrix = BitMatrix::new(75, 20).unwrap(); + assert_eq!(75, matrix.getWidth()); + assert_eq!(20, matrix.getHeight()); + matrix.set(10, 0); + matrix.set(11, 1); + matrix.set(50, 2); + matrix.set(51, 3); + matrix.flip_coords(74, 4); + matrix.flip_coords(0, 5); + + // Should all be on + assert!(matrix.get(10, 0)); + assert!(matrix.get(11, 1)); + assert!(matrix.get(50, 2)); + assert!(matrix.get(51, 3)); + assert!(matrix.get(74, 4)); + assert!(matrix.get(0, 5)); + + // Flip a couple back off + matrix.flip_coords(50, 2); + matrix.flip_coords(51, 3); + assert!(!matrix.get(50, 2)); + assert!(!matrix.get(51, 3)); + } + + #[test] + fn test_rectangular_set_region() { + let mut matrix = BitMatrix::new(320, 240).unwrap(); + assert_eq!(320, matrix.getWidth()); + assert_eq!(240, matrix.getHeight()); + matrix.setRegion(105, 22, 80, 12); + + // Only bits in the region should be on + for y in 0..240 { + // for (int y = 0; y < 240; y++) { + for x in 0..320{ + // for (int x = 0; x < 320; x++) { + assert_eq!(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y)); + } + } + } + + #[test] + fn test_get_row() { + let mut matrix = BitMatrix::new(102, 5).unwrap(); + for x in 0..102 { + // for (int x = 0; x < 102; x++) { + if (x & 0x03) == 0 { + matrix.set(x, 2); + } + } + + // Should allocate + let array = matrix.getRow(2, &BitArray::new()); + assert_eq!(102, array.getSize()); + + // Should reallocate + let mut array2 = BitArray::with_size(60); + array2 = matrix.getRow(2, &array2); + assert_eq!(102, array2.getSize()); + + // Should use provided object, with original BitArray size + let mut array3 = BitArray::with_size(200); + array3 = matrix.getRow(2, &array3); + assert_eq!(200, array3.getSize()); + + for x in 0..102 { + // for (int x = 0; x < 102; x++) { + let on = (x & 0x03) == 0; + assert_eq!(on, array.get(x)); + assert_eq!(on, array2.get(x)); + assert_eq!(on, array3.get(x)); + } + } + + #[test] + fn test_rotate90_simple() { + let mut matrix = BitMatrix::new(3, 3).unwrap(); + matrix.set(0, 0); + matrix.set(0, 1); + matrix.set(1, 2); + matrix.set(2, 1); + + matrix.rotate90(); + + assert!(matrix.get(0, 2)); + assert!(matrix.get(1, 2)); + assert!(matrix.get(2, 1)); + assert!(matrix.get(1, 0)); + } + + #[test] + fn test_rotate180_simple() { + let mut matrix = BitMatrix::new(3, 3).unwrap(); + matrix.set(0, 0); + matrix.set(0, 1); + matrix.set(1, 2); + matrix.set(2, 1); + + matrix.rotate180(); + + assert!(matrix.get(2, 2)); + assert!(matrix.get(2, 1)); + assert!(matrix.get(1, 0)); + assert!(matrix.get(0, 1)); + } + + #[test] + fn test_rotate180_case() { + test_rotate_180(7, 4); + test_rotate_180(7, 5); + test_rotate_180(8, 4); + test_rotate_180(8, 5); + } + + #[test] + fn test_parse() { + let emptyMatrix = BitMatrix::new(3, 3).unwrap(); + let mut fullMatrix = BitMatrix::new(3, 3).unwrap(); + fullMatrix.setRegion(0, 0, 3, 3); + let mut centerMatrix = BitMatrix::new(3, 3).unwrap(); + centerMatrix.setRegion(1, 1, 1, 1); + let emptyMatrix24 = BitMatrix::new(2, 4).unwrap(); + + assert_eq!(emptyMatrix, BitMatrix::parse_strings(" \n \n \n", "x", " ").unwrap()); + assert_eq!(emptyMatrix, BitMatrix::parse_strings(" \n \r\r\n \n\r", "x", " ").unwrap()); + assert_eq!(emptyMatrix, BitMatrix::parse_strings(" \n \n ", "x", " ").unwrap()); + + assert_eq!(fullMatrix, BitMatrix::parse_strings("xxx\nxxx\nxxx\n", "x", " ").unwrap()); + + assert_eq!(centerMatrix, BitMatrix::parse_strings(" \n x \n \n", "x", " ").unwrap()); + assert_eq!(centerMatrix, BitMatrix::parse_strings(" \n x \n \n", "x ", " ").unwrap()); + + assert!(BitMatrix::parse_strings(" \n xy\n \n", "x", " ").is_err()); + + + assert_eq!(emptyMatrix24, BitMatrix::parse_strings(" \n \n \n \n", "x", " ").unwrap()); + + assert_eq!(centerMatrix, BitMatrix::parse_strings(¢erMatrix.toString("x", "."), "x", ".").unwrap()); + } + + #[test] + fn test_parse_boolean() { + let emptyMatrix = BitMatrix::new(3, 3).unwrap(); + let mut fullMatrix = BitMatrix::new(3, 3).unwrap(); + fullMatrix.setRegion(0, 0, 3, 3); + let mut centerMatrix = BitMatrix::new(3, 3).unwrap(); + centerMatrix.setRegion(1, 1, 1, 1); + let emptyMatrix24 = BitMatrix::new(2, 4).unwrap(); + + let mut matrix = vec![vec![false;3];3]; + // boolean[][] matrix = new boolean[3][3]; + assert_eq!(emptyMatrix, BitMatrix::parse_bools(&matrix)); + matrix[1][1] = true; + assert_eq!(centerMatrix, BitMatrix::parse_bools(&matrix)); + for arr in matrix.iter_mut() { + // for (boolean[] arr : matrix) { + arr[..].clone_from_slice(&[true, true, true]) + } + assert_eq!(fullMatrix, BitMatrix::parse_bools(&matrix)); + } + + #[test] + fn test_unset() { + let emptyMatrix = BitMatrix::new(3, 3).unwrap(); + let mut matrix = emptyMatrix.clone(); + matrix.set(1, 1); + assert_ne!(emptyMatrix, matrix); + matrix.unset(1, 1); + assert_eq!(emptyMatrix, matrix); + matrix.unset(1, 1); + assert_eq!(emptyMatrix, matrix); + } + + #[test] + fn test_xor_case() { + let emptyMatrix = BitMatrix::new(3, 3).unwrap(); + let mut fullMatrix = BitMatrix::new(3, 3).unwrap(); + fullMatrix.setRegion(0, 0, 3, 3); + let mut centerMatrix = BitMatrix::new(3, 3).unwrap(); + centerMatrix.setRegion(1, 1, 1, 1); + let mut invertedCenterMatrix = fullMatrix.clone(); + invertedCenterMatrix.unset(1, 1); + let badMatrix = BitMatrix::new(4, 4).unwrap(); + + test_XOR(&emptyMatrix, &emptyMatrix, &emptyMatrix); + test_XOR(&emptyMatrix, ¢erMatrix, ¢erMatrix); + test_XOR(&emptyMatrix, &fullMatrix, &fullMatrix); + + test_XOR(¢erMatrix, &emptyMatrix, ¢erMatrix); + test_XOR(¢erMatrix, ¢erMatrix, &emptyMatrix); + test_XOR(¢erMatrix, &fullMatrix, &invertedCenterMatrix); + + test_XOR(&invertedCenterMatrix, &emptyMatrix, &invertedCenterMatrix); + test_XOR(&invertedCenterMatrix, ¢erMatrix, &fullMatrix); + test_XOR(&invertedCenterMatrix, &fullMatrix, ¢erMatrix); + + test_XOR(&fullMatrix, &emptyMatrix, &fullMatrix); + test_XOR(&fullMatrix, ¢erMatrix, &invertedCenterMatrix); + test_XOR(&fullMatrix, &fullMatrix, &emptyMatrix); + + assert!(emptyMatrix.clone().xor(&badMatrix).is_err()); + // try { + // emptyMatrix.clone().xor(badMatrix); + // fail(); + // } catch (IllegalArgumentException ex) { + // // good + // } + + assert!(badMatrix.clone().xor(&emptyMatrix).is_err()); + // try { + // badMatrix.clone().xor(emptyMatrix); + // fail(); + // } catch (IllegalArgumentException ex) { + // // good + // } + } + + fn matrix_to_string( result:&BitMatrix) -> String{ + assert_eq!(1, result.getHeight()); + let mut builder = String::with_capacity(result.getWidth().try_into().unwrap()); + for i in 0..result.getWidth() { + // for (int i = 0; i < result.getWidth(); i++) { + builder.push(if result.get(i, 0) {'1'} else {'0'}); + } + return builder; + } + + fn test_XOR( dataMatrix: &BitMatrix, flipMatrix: &BitMatrix, expectedMatrix: &BitMatrix) { + let mut matrix = dataMatrix.clone(); + matrix.xor(flipMatrix); + assert_eq!(*expectedMatrix, matrix); + } + + fn test_rotate_180( width:u32, height:u32) { + let mut input = get_input(width, height); + input.rotate180(); + let expected = get_expected(width, height); + + for y in 0..height{ + // for (int y = 0; y < height; y++) { + for x in 0..width{ + // for (int x = 0; x < width; x++) { + assert_eq!( expected.get(x, y), input.get(x, y), "({},{})", x, y); + } + } + } + + fn get_expected( width:u32, height:u32) -> BitMatrix{ + let mut result = BitMatrix::new(width, height).unwrap(); + let mut i = 0; + while i < BIT_MATRIX_POINTS.len() { + // for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) { + result.set(width - 1 - BIT_MATRIX_POINTS[i], height - 1 - BIT_MATRIX_POINTS[i + 1]); + i += 2; + } + return result; + } + + fn get_input( width:u32, height:u32) -> BitMatrix{ + let mut result = BitMatrix::new(width, height).unwrap(); + let mut i = 0; + while i < BIT_MATRIX_POINTS.len(){ + // for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) { + result.set(BIT_MATRIX_POINTS[i], BIT_MATRIX_POINTS[i + 1]); + i+=2; + } + return result; + } + +// } diff --git a/src/common/mod.rs b/src/common/mod.rs index 2540f8b..bbe035f 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -16,6 +16,9 @@ mod StringUtilsTestCase; #[cfg(test)] mod BitArrayTestCase; + +#[cfg(test)] +mod BitMatrixTestCase; /* * Copyright (C) 2010 ZXing authors * @@ -1081,7 +1084,7 @@ impl BitMatrix { * @param row {@link BitArray} to copy from */ pub fn setRow(&mut self, y: u32, row: &BitArray) { - return self.bits[y as usize * self.rowSize..self.rowSize] + return self.bits[y as usize * self.rowSize..y as usize * self.rowSize + self.rowSize] .clone_from_slice(&row.getBitArray()[0..self.rowSize]); //System.arraycopy(row.getBitArray(), 0, self.bits, y * self.rowSize, self.rowSize); } @@ -1240,18 +1243,18 @@ impl BitMatrix { } pub fn getBottomRightOnBit(&self) -> Option> { - let mut bitsOffset = self.bits.len() - 1; - while bitsOffset >= 0 && self.bits[bitsOffset] == 0 { + let mut bitsOffset = self.bits.len() as i64 - 1; + while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 { bitsOffset -= 1; } if bitsOffset < 0 { return None; } - let y = bitsOffset / self.rowSize; - let mut x = (bitsOffset % self.rowSize) * 32; + let y = bitsOffset as usize / self.rowSize; + let mut x = (bitsOffset as usize % self.rowSize) * 32; - let theBits = self.bits[bitsOffset]; + let theBits = self.bits[bitsOffset as usize]; let mut bit = 31; while (theBits >> bit) == 0 { bit -= 1;