cargo fmt

This commit is contained in:
Henry Schimke
2022-10-17 10:51:08 -05:00
parent 111fe47566
commit ae11af8c5b
136 changed files with 2117 additions and 1776 deletions

View File

@@ -29,127 +29,126 @@
use super::BitArray;
use rand::Rng;
#[test]
fn test_get_set() {
let mut array = BitArray::with_size(33);
#[test]
fn test_get_set() {
let mut array = BitArray::with_size(33);
for i in 0..33 {
// for (int i = 0; i < 33; i++) {
assert!(!array.get(i));
array.set(i);
assert!(array.get(i));
// for (int i = 0; i < 33; i++) {
assert!(!array.get(i));
array.set(i);
assert!(array.get(i));
}
}
}
#[test]
fn test_get_next_set1() {
let array = BitArray::with_size(32);
#[test]
fn test_get_next_set1() {
let array = BitArray::with_size(32);
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!( 32, array.getNextSet(i), "{}", i);
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(32, array.getNextSet(i), "{}", i);
}
let array = BitArray::with_size(33);
for i in 0..array.getSize(){
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!( 33, array.getNextSet(i), "{}", i);
let array = BitArray::with_size(33);
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(33, array.getNextSet(i), "{}", i);
}
}
#[test]
fn test_get_next_set2() {
let mut array = BitArray::with_size(33);
array.set(31);
for i in 0 ..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(if i <= 31 { 31} else {33}, array.getNextSet(i), "{}", i);
}
array = BitArray::with_size(33);
array.set(32);
for i in 0 ..array.getSize(){
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(32, array.getNextSet(i), "{}", i);
}
}
#[test]
fn test_get_next_set3() {
let mut array = BitArray::with_size(63);
array.set(31);
array.set(32);
for i in 0 .. array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
let expected;
if i <= 31 {
expected = 31;
} else if i == 32 {
expected = 32;
} else {
expected = 63;
}
assert_eq!(expected, array.getNextSet(i), "{}", i);
}
}
}
#[test]
fn test_get_next_set4() {
let mut array = BitArray::with_size(63);
#[test]
fn test_get_next_set2() {
let mut array = BitArray::with_size(33);
array.set(31);
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(if i <= 31 { 31 } else { 33 }, array.getNextSet(i), "{}", i);
}
array = BitArray::with_size(33);
array.set(32);
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
assert_eq!(32, array.getNextSet(i), "{}", i);
}
}
#[test]
fn test_get_next_set3() {
let mut array = BitArray::with_size(63);
array.set(31);
array.set(32);
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
let expected;
if i <= 31 {
expected = 31;
} else if i == 32 {
expected = 32;
} else {
expected = 63;
}
assert_eq!(expected, array.getNextSet(i), "{}", i);
}
}
#[test]
fn test_get_next_set4() {
let mut array = BitArray::with_size(63);
array.set(33);
array.set(40);
for i in 0..array.getSize(){
// for (int i = 0; i < array.getSize(); i++) {
let expected;
if i <= 33 {
expected = 33;
} else if i <= 40 {
expected = 40;
} else {
expected = 63;
}
assert_eq!( expected, array.getNextSet(i), "{}", i);
}
}
#[test]
fn test_get_next_set5() {
let mut r = rand::thread_rng();
for _i in 0 .. 10 {
// for (int i = 0; i < 10; i++) {
let mut array = BitArray::with_size(1 + r.gen_range(0..100));
let numSet = r.gen_range(0..20);
for _j in 0..numSet {
// for (int j = 0; j < numSet; j++) {
array.set(r.gen_range(0..array.getSize()));
}
let numQueries = r.gen_range(0..20);
for _j in 0..numQueries {
// for (int j = 0; j < numQueries; j++) {
let query = r.gen_range(0..array.getSize());
let mut expected = query;
while expected < array.getSize() && !array.get(expected) {
expected+=1;
for i in 0..array.getSize() {
// for (int i = 0; i < array.getSize(); i++) {
let expected;
if i <= 33 {
expected = 33;
} else if i <= 40 {
expected = 40;
} else {
expected = 63;
}
let actual = array.getNextSet(query);
assert_eq!(expected, actual);
}
assert_eq!(expected, array.getNextSet(i), "{}", i);
}
}
}
#[test]
fn test_get_next_set5() {
let mut r = rand::thread_rng();
for _i in 0..10 {
// for (int i = 0; i < 10; i++) {
let mut array = BitArray::with_size(1 + r.gen_range(0..100));
let numSet = r.gen_range(0..20);
for _j in 0..numSet {
// for (int j = 0; j < numSet; j++) {
array.set(r.gen_range(0..array.getSize()));
}
let numQueries = r.gen_range(0..20);
for _j in 0..numQueries {
// for (int j = 0; j < numQueries; j++) {
let query = r.gen_range(0..array.getSize());
let mut expected = query;
while expected < array.getSize() && !array.get(expected) {
expected += 1;
}
let actual = array.getNextSet(query);
assert_eq!(expected, actual);
}
}
}
#[test]
fn test_set_bulk() {
let mut array = BitArray::with_size(64);
#[test]
fn test_set_bulk() {
let mut array = BitArray::with_size(64);
array.setBulk(32, 0xFFFF0000);
for i in 0..48 {
// for (int i = 0; i < 48; i++) {
assert!(!array.get(i));
// for (int i = 0; i < 48; i++) {
assert!(!array.get(i));
}
for i in 48..64 {
// for (int i = 48; i < 64; i++) {
assert!(array.get(i));
// for (int i = 48; i < 64; i++) {
assert!(array.get(i));
}
}
}
#[test]
fn test_append_bit(){
#[test]
fn test_append_bit() {
let mut array = BitArray::new();
array.appendBits(0x000001E, 6).expect("must append)");
let mut array_2 = BitArray::new();
@@ -161,57 +160,57 @@ use rand::Rng;
array_2.appendBit(false);
assert_eq!(array, array_2)
}
}
#[test]
fn test_set_range() {
let mut array = BitArray::with_size(64);
#[test]
fn test_set_range() {
let mut array = BitArray::with_size(64);
array.setRange(28, 36).unwrap();
assert!(!array.get(27));
for i in 28..36 {
// for (int i = 28; i < 36; i++) {
assert!(array.get(i));
// for (int i = 28; i < 36; i++) {
assert!(array.get(i));
}
assert!(!array.get(36));
}
}
#[test]
fn test_clear() {
let mut array = BitArray::with_size(32);
#[test]
fn test_clear() {
let mut array = BitArray::with_size(32);
for i in 0..32 {
// for (int i = 0; i < 32; i++) {
array.set(i);
// for (int i = 0; i < 32; i++) {
array.set(i);
}
array.clear();
for i in 0..32 {
// for (int i = 0; i < 32; i++) {
assert!(!array.get(i));
// for (int i = 0; i < 32; i++) {
assert!(!array.get(i));
}
}
}
#[test]
fn test_flip() {
let mut array = BitArray::with_size(32);
#[test]
fn test_flip() {
let mut array = BitArray::with_size(32);
assert!(!array.get(5));
array.flip(5);
assert!(array.get(5));
array.flip(5);
assert!(!array.get(5));
}
}
#[test]
fn test_get_array() {
let mut array = BitArray::with_size(64);
#[test]
fn test_get_array() {
let mut array = BitArray::with_size(64);
array.set(0);
array.set(63);
let ints = array.getBitArray();
assert_eq!(1, ints[0]);
assert_eq!(0b10_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00, ints[1]);
}
}
#[test]
fn test_is_range() {
let mut array = BitArray::with_size(64);
#[test]
fn test_is_range() {
let mut array = BitArray::with_size(64);
assert!(array.isRange(0, 64, false).unwrap());
assert!(!array.isRange(0, 64, true).unwrap());
array.set(32);
@@ -221,42 +220,46 @@ use rand::Rng;
array.set(34);
assert!(!array.isRange(31, 35, true).unwrap());
for i in 0..31 {
// for (int i = 0; i < 31; i++) {
array.set(i);
// for (int i = 0; i < 31; i++) {
array.set(i);
}
assert!(array.isRange(0, 33, true).unwrap());
for i in 33..64 {
// for (int i = 33; i < 64; i++) {
array.set(i);
// for (int i = 33; i < 64; i++) {
array.set(i);
}
assert!(array.isRange(0, 64, true).unwrap());
assert!(!array.isRange(0, 64, false).unwrap());
}
}
#[test]
fn reverse_algorithm_test() {
let oldBits:Vec<u32> = vec![128, 256, 512, 6453324, 50934953];
#[test]
fn reverse_algorithm_test() {
let oldBits: Vec<u32> = vec![128, 256, 512, 6453324, 50934953];
for size in 1..160 {
// for (int size = 1; size < 160; size++) {
let newBitsOriginal = reverse_original(&oldBits.clone(), size);
let mut newBitArray = BitArray::with_initial_values(oldBits.clone(), size);
newBitArray.reverse();
let newBitsNew = newBitArray.getBitArray();
assert!(arrays_are_equal(&newBitsOriginal, &newBitsNew, size / 32 + 1));
// for (int size = 1; size < 160; size++) {
let newBitsOriginal = reverse_original(&oldBits.clone(), size);
let mut newBitArray = BitArray::with_initial_values(oldBits.clone(), size);
newBitArray.reverse();
let newBitsNew = newBitArray.getBitArray();
assert!(arrays_are_equal(
&newBitsOriginal,
&newBitsNew,
size / 32 + 1
));
}
}
}
#[test]
fn test_clone() {
let array = BitArray::with_size(32);
#[test]
fn test_clone() {
let array = BitArray::with_size(32);
array.clone().set(0);
assert!(!array.get(0));
}
}
#[test]
fn test_equals() {
let mut a = BitArray::with_size(32);
let mut b = BitArray::with_size(32);
#[test]
fn test_equals() {
let mut a = BitArray::with_size(32);
let mut b = BitArray::with_size(32);
assert_eq!(a, b);
// assert_eq!(a.hash(), b.hash());
assert_ne!(a, BitArray::with_size(31));
@@ -266,31 +269,31 @@ use rand::Rng;
b.set(16);
assert_eq!(a, b);
// assert_eq!(a.hash(), b.hash());
}
}
fn reverse_original( oldBits:&[u32], size:usize) -> Vec<u32>{
let mut newBits = vec![0;oldBits.len()];
fn reverse_original(oldBits: &[u32], size: usize) -> Vec<u32> {
let mut newBits = vec![0; oldBits.len()];
for i in 0..size {
// for (int i = 0; i < size; i++) {
if bit_set(oldBits, size - i - 1) {
newBits[i / 32 as usize] |= 1 << (i & 0x1F);
}
// for (int i = 0; i < size; i++) {
if bit_set(oldBits, size - i - 1) {
newBits[i / 32 as usize] |= 1 << (i & 0x1F);
}
}
return newBits;
}
}
fn bit_set( bits:&[u32], i:usize) -> bool{
fn bit_set(bits: &[u32], i: usize) -> bool {
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
}
}
fn arrays_are_equal( left :&[u32], right:&[u32], size:usize) -> bool{
fn arrays_are_equal(left: &[u32], right: &[u32], size: usize) -> bool {
for i in 0..size {
// for (int i = 0; i < size; i++) {
if left[i] != right[i] {
return false;
}
// for (int i = 0; i < size; i++) {
if left[i] != right[i] {
return false;
}
}
return true;
}
}
// }

View File

@@ -31,74 +31,74 @@ use crate::common::BitArray;
use super::BitMatrix;
static BIT_MATRIX_POINTS : [u32;6] = [ 1, 2, 2, 0, 3, 1 ];
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);
#[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 (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));
}
// 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);
#[test]
fn test_set_region() {
let mut matrix = BitMatrix::with_single_dimension(5);
matrix.setRegion(1, 1, 3, 3).expect("must set");
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));
}
// 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);
#[test]
fn test_enclosing() {
let mut matrix = BitMatrix::with_single_dimension(5);
assert!(matrix.getEnclosingRectangle().is_none());
matrix.setRegion(1, 1, 1, 1).expect("must set");
assert_eq!(vec![ 1, 1, 1, 1 ], matrix.getEnclosingRectangle().unwrap());
assert_eq!(vec![1, 1, 1, 1], matrix.getEnclosingRectangle().unwrap());
matrix.setRegion(1, 1, 3, 2).expect("must set");
assert_eq!(vec![ 1, 1, 3, 2 ], matrix.getEnclosingRectangle().unwrap());
assert_eq!(vec![1, 1, 3, 2], matrix.getEnclosingRectangle().unwrap());
matrix.setRegion(0, 0, 5, 5).expect("must set");
assert_eq!(vec![ 0, 0, 5, 5 ], matrix.getEnclosingRectangle().unwrap());
}
assert_eq!(vec![0, 0, 5, 5], matrix.getEnclosingRectangle().unwrap());
}
#[test]
fn test_on_bit() {
let mut matrix = BitMatrix::with_single_dimension(5);
#[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).expect("must set");
assert_eq!(vec![ 1, 1 ], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![ 1, 1 ], matrix.getBottomRightOnBit().unwrap());
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![1, 1], matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(1, 1, 3, 2).expect("must set");
assert_eq!(vec![ 1, 1 ], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![ 3, 2 ], matrix.getBottomRightOnBit().unwrap());
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![3, 2], matrix.getBottomRightOnBit().unwrap());
matrix.setRegion(0, 0, 5, 5).expect("must set");
assert_eq!(vec![ 0, 0 ], matrix.getTopLeftOnBit().unwrap());
assert_eq!(vec![ 4, 4 ], matrix.getBottomRightOnBit().unwrap());
}
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();
#[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);
@@ -121,33 +121,33 @@ use super::BitMatrix;
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();
#[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).expect("must set");
// 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));
}
// 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();
#[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);
}
// for (int x = 0; x < 102; x++) {
if (x & 0x03) == 0 {
matrix.set(x, 2);
}
}
// Should allocate
@@ -155,27 +155,27 @@ use super::BitMatrix;
assert_eq!(102, array.getSize());
// Should reallocate
let mut array2 = BitArray::with_size(60);
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);
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));
// 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();
#[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);
@@ -187,11 +187,11 @@ use super::BitMatrix;
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();
#[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);
@@ -203,85 +203,108 @@ use super::BitMatrix;
assert!(matrix.get(2, 1));
assert!(matrix.get(1, 0));
assert!(matrix.get(0, 1));
}
}
#[test]
fn test_rotate180_case() {
#[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();
#[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).expect("must set");
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
centerMatrix.setRegion(1, 1, 1, 1).expect("must set");
let emptyMatrix24 = BitMatrix::new(2, 4).unwrap();
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!(
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!(
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!(
centerMatrix,
BitMatrix::parse_strings(" \n x \n \n", "x", " ").unwrap()
);
assert_eq!(
centerMatrix,
BitMatrix::parse_strings(" \n x \n \n", "x ", " ").unwrap()
);
assert_eq!(emptyMatrix24, BitMatrix::parse_strings(" \n \n \n \n", "x", " ").unwrap());
assert!(BitMatrix::parse_strings(" \n xy\n \n", "x", " ").is_err());
assert_eq!(centerMatrix, BitMatrix::parse_strings(&centerMatrix.toString("x", "."), "x", ".").unwrap());
}
assert_eq!(
emptyMatrix24,
BitMatrix::parse_strings(" \n \n \n \n", "x", " ").unwrap()
);
#[test]
fn test_parse_boolean() {
let emptyMatrix = BitMatrix::new(3, 3).unwrap();
let mut fullMatrix = BitMatrix::new(3, 3).unwrap();
assert_eq!(
centerMatrix,
BitMatrix::parse_strings(&centerMatrix.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).expect("must set");
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
centerMatrix.setRegion(1, 1, 1, 1).expect("must set");
let emptyMatrix24 = BitMatrix::new(2, 4).unwrap();
let emptyMatrix24 = BitMatrix::new(2, 4).unwrap();
let mut matrix = vec![vec![false;3];3];
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])
// for (boolean[] arr : matrix) {
arr[..].clone_from_slice(&[true, true, true])
}
assert_eq!(fullMatrix, BitMatrix::parse_bools(&matrix));
}
}
#[test]
fn test_unset() {
#[test]
fn test_unset() {
let emptyMatrix = BitMatrix::new(3, 3).unwrap();
let mut matrix = emptyMatrix.clone();
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();
#[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).expect("must set");
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
let mut centerMatrix = BitMatrix::new(3, 3).unwrap();
centerMatrix.setRegion(1, 1, 1, 1).expect("must set");
let mut invertedCenterMatrix = fullMatrix.clone();
let mut invertedCenterMatrix = fullMatrix.clone();
invertedCenterMatrix.unset(1, 1);
let badMatrix = BitMatrix::new(4, 4).unwrap();
let badMatrix = BitMatrix::new(4, 4).unwrap();
test_XOR(&emptyMatrix, &emptyMatrix, &emptyMatrix);
test_XOR(&emptyMatrix, &centerMatrix, &centerMatrix);
@@ -314,58 +337,61 @@ use super::BitMatrix;
// } catch (IllegalArgumentException ex) {
// // good
// }
}
}
fn matrix_to_string( result:&BitMatrix) -> String{
fn matrix_to_string(result: &BitMatrix) -> String {
assert_eq!(1, result.getHeight());
let mut builder = String::with_capacity(result.getWidth().try_into().unwrap());
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'});
// 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) {
fn test_XOR(dataMatrix: &BitMatrix, flipMatrix: &BitMatrix, expectedMatrix: &BitMatrix) {
let mut matrix = dataMatrix.clone();
matrix.xor(flipMatrix).expect("must set");
assert_eq!(*expectedMatrix, matrix);
}
}
fn test_rotate_180( width:u32, height:u32) {
let mut input = get_input(width, height);
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);
}
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();
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;
// 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();
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;
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;
}
}
// }

View File

@@ -26,10 +26,10 @@
use super::BitSource;
#[test]
fn test_source() {
let bytes:Vec<u8> = vec![ 1, 2, 3, 4, 5];
let mut source = BitSource::new(bytes);
#[test]
fn test_source() {
let bytes: Vec<u8> = vec![1, 2, 3, 4, 5];
let mut source = BitSource::new(bytes);
assert_eq!(40, source.available());
assert_eq!(0, source.readBits(1).unwrap());
assert_eq!(39, source.available());
@@ -45,6 +45,6 @@ use super::BitSource;
assert_eq!(6, source.available());
assert_eq!(5, source.readBits(6).unwrap());
assert_eq!(0, source.available());
}
}
// }
// }

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -19,7 +18,7 @@
// import java.util.Arrays;
use std::{fmt, cmp};
use std::{cmp, fmt};
use crate::Exceptions;
@@ -402,4 +401,4 @@ impl fmt::Display for BitArray {
}
write!(f, "{}", _str)
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -393,7 +392,7 @@ impl BitMatrix {
pub fn rotate180(&mut self) {
let mut topRow = BitArray::with_size(self.width as usize);
let mut bottomRow = BitArray::with_size(self.width as usize);
let maxHeight = (self.height + 1) / 2;
let maxHeight = (self.height + 1) / 2;
for i in 0..maxHeight {
//for (int i = 0; i < maxHeight; i++) {
topRow = self.getRow(i, &topRow);
@@ -410,9 +409,9 @@ impl BitMatrix {
* Modifies this {@code BitMatrix} to represent the same but rotated 90 degrees counterclockwise
*/
pub fn rotate90(&mut self) {
let newWidth = self.height;
let newHeight = self.width;
let newRowSize = (newWidth + 31) / 32;
let newWidth = self.height;
let newHeight = self.width;
let newRowSize = (newWidth + 31) / 32;
let mut newBits = vec![0; (newRowSize * newHeight).try_into().unwrap()];
for y in 0..self.height {
@@ -625,4 +624,4 @@ impl fmt::Display for BitMatrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.toString("X ", " "))
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -122,4 +121,4 @@ impl BitSource {
pub fn available(&self) -> usize {
return 8 * (self.bytes.len() - self.byte_offset) - self.bit_offset;
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2008 ZXing authors
*
@@ -65,4 +64,4 @@ impl BitSourceBuilder {
}
&self.output
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2008 ZXing authors
*
@@ -286,4 +285,4 @@ impl CharacterSetECI {
_ => None,
}
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -206,4 +205,4 @@ impl DecoderRXingResult {
pub fn getSymbologyModifier(&self) -> u32 {
self.symbologyModifier
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -21,7 +20,7 @@
use crate::Exceptions;
use super::{BitMatrix, PerspectiveTransform, GridSampler};
use super::{BitMatrix, GridSampler, PerspectiveTransform};
/**
* @author Sean Owen
@@ -118,4 +117,4 @@ impl GridSampler for DefaultGridSampler {
}
return Ok(bits);
}
}
}

View File

@@ -4,4 +4,4 @@ mod monochrome_rectangle_detector;
pub use monochrome_rectangle_detector::*;
mod white_rectangle_detector;
pub use white_rectangle_detector::*;
pub use white_rectangle_detector::*;

View File

@@ -16,7 +16,7 @@
//package com.google.zxing.common.detector;
use crate::{Exceptions, RXingResultPoint, common::BitMatrix, ResultPoint};
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
/**
* <p>A somewhat generic detector that looks for a barcode-like rectangular region within an image.
@@ -34,7 +34,9 @@ pub struct MonochromeRectangleDetector {
impl MonochromeRectangleDetector {
pub fn new(image: &BitMatrix) -> Self {
Self { image: image.clone() }
Self {
image: image.clone(),
}
}
/**
@@ -50,7 +52,7 @@ impl MonochromeRectangleDetector {
pub fn detect(&self) -> Result<Vec<RXingResultPoint>, Exceptions> {
let height = self.image.getHeight() as i32;
let width = self.image.getWidth() as i32;
let halfHeight= height / 2;
let halfHeight = height / 2;
let halfWidth = width / 2;
let deltaY = 1.max(height as i32 / (MAX_MODULES * 8));
let deltaX = 1.max(width as i32 / (MAX_MODULES * 8));
@@ -168,48 +170,37 @@ impl MonochromeRectangleDetector {
}
if range.is_none() {
if let Some(lastRange) = lastRange_z {
// lastRange was found
if deltaX == 0 {
let lastY = y - deltaY;
if lastRange[0] < centerX {
if lastRange[1] > centerX {
// straddle, choose one or the other based on direction
return Ok(RXingResultPoint::new(
lastRange[if deltaY > 0 { 0 } else { 1 }] as f32,
lastY as f32,
));
// lastRange was found
if deltaX == 0 {
let lastY = y - deltaY;
if lastRange[0] < centerX {
if lastRange[1] > centerX {
// straddle, choose one or the other based on direction
return Ok(RXingResultPoint::new(
lastRange[if deltaY > 0 { 0 } else { 1 }] as f32,
lastY as f32,
));
}
return Ok(RXingResultPoint::new(lastRange[0] as f32, lastY as f32));
} else {
return Ok(RXingResultPoint::new(lastRange[1] as f32, lastY as f32));
}
return Ok(RXingResultPoint::new(
lastRange[0] as f32,
lastY as f32,
));
} else {
return Ok(RXingResultPoint::new(
lastRange[1] as f32,
lastY as f32,
));
}
} else {
let lastX = x - deltaX;
if lastRange[0] < centerY {
if lastRange[1] > centerY {
return Ok(RXingResultPoint::new(
lastX as f32,
lastRange[if deltaX < 0 { 0 } else { 1 }] as f32,
));
let lastX = x - deltaX;
if lastRange[0] < centerY {
if lastRange[1] > centerY {
return Ok(RXingResultPoint::new(
lastX as f32,
lastRange[if deltaX < 0 { 0 } else { 1 }] as f32,
));
}
return Ok(RXingResultPoint::new(lastX as f32, lastRange[0] as f32));
} else {
return Ok(RXingResultPoint::new(lastX as f32, lastRange[1] as f32));
}
return Ok(RXingResultPoint::new(
lastX as f32,
lastRange[0] as f32,
));
} else {
return Ok(RXingResultPoint::new(
lastX as f32,
lastRange[1] as f32,
));
}
}
}}else {
} else {
return Err(Exceptions::NotFoundException("".to_owned()));
}
lastRange_z = range;
@@ -309,4 +300,4 @@ impl MonochromeRectangleDetector {
None
};
}
}
}

View File

@@ -16,7 +16,7 @@
//package com.google.zxing.common.detector;
use crate::{RXingResultPoint, Exceptions, common::BitMatrix, ResultPoint};
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
use super::MathUtils;
@@ -59,13 +59,7 @@ impl WhiteRectangleDetector {
* @param y y position of search center
* @throws NotFoundException if image is too small to accommodate {@code initSize}
*/
pub fn new(
image: &BitMatrix,
initSize: i32,
x: i32,
y: i32,
) -> Result<Self, Exceptions> {
pub fn new(image: &BitMatrix, initSize: i32, x: i32, y: i32) -> Result<Self, Exceptions> {
let halfsize = initSize / 2;
let leftInit = x - halfsize;
@@ -81,7 +75,7 @@ impl WhiteRectangleDetector {
return Err(Exceptions::NotFoundException("".to_owned()));
}
Ok(Self{
Ok(Self {
image: image.clone(),
height: image.getHeight() as i32,
width: image.getWidth() as i32,
@@ -126,7 +120,9 @@ impl WhiteRectangleDetector {
// . |
// .....
let mut right_border_not_white = true;
while (right_border_not_white || !at_least_one_black_point_found_on_right) && right < self.width {
while (right_border_not_white || !at_least_one_black_point_found_on_right)
&& right < self.width
{
right_border_not_white = self.contains_black_point(up, down, right, false);
if right_border_not_white {
right += 1;
@@ -146,7 +142,8 @@ impl WhiteRectangleDetector {
// . .
// .___.
let mut bottom_border_not_white = true;
while (bottom_border_not_white || !at_least_one_black_point_found_on_bottom) && down < self.height
while (bottom_border_not_white || !at_least_one_black_point_found_on_bottom)
&& down < self.height
{
bottom_border_not_white = self.contains_black_point(left, right, down, true);
if bottom_border_not_white {

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2021 ZXing authors
*
@@ -24,7 +23,7 @@
// import java.util.ArrayList;
// import java.util.List;
use encoding::{EncodingRef, Encoding};
use encoding::{Encoding, EncodingRef};
use unicode_segmentation::UnicodeSegmentation;
use super::CharacterSetECI;
@@ -251,4 +250,4 @@ impl ECIEncoderSet {
let encoder = self.encoders[encoderIndex];
encoder.encode(s, encoding::EncoderTrap::Replace).unwrap()
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2021 ZXing authors
*
@@ -106,4 +105,4 @@ pub trait ECIInput {
*/
fn getECIValue(&self, index: usize) -> Result<u32, Exceptions>;
fn haveNCharacters(&self, index: usize, n: usize) -> bool;
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2022 ZXing authors
*
@@ -24,7 +23,7 @@
use std::fmt;
use encoding::{EncodingRef, Encoding};
use encoding::{Encoding, EncodingRef};
use crate::Exceptions;
@@ -81,7 +80,11 @@ impl ECIStringBuilder {
* @param value string to append
*/
pub fn append_string(&mut self, value: &str) {
value.as_bytes().iter().map(|b| self.current_bytes.push(*b)).count();
value
.as_bytes()
.iter()
.map(|b| self.current_bytes.push(*b))
.count();
// self.current_bytes.push(value.as_bytes());
}
@@ -170,4 +173,4 @@ impl fmt::Display for ECIStringBuilder {
//self.encodeCurrentBytesIfAny();
write!(f, "{}", self.result)
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2009 ZXing authors
*
@@ -21,9 +20,9 @@
// import com.google.zxing.LuminanceSource;
// import com.google.zxing.NotFoundException;
use crate::{Exceptions, LuminanceSource, Binarizer};
use crate::{Binarizer, Exceptions, LuminanceSource};
use super::{BitMatrix, BitArray};
use super::{BitArray, BitMatrix};
/**
* This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
@@ -244,4 +243,4 @@ impl GlobalHistogramBinarizer {
Ok((bestValley as u32) << GlobalHistogramBinarizer::LUMINANCE_SHIFT)
}
}
}

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -197,4 +196,4 @@ pub trait GridSampler {
}
Ok(())
}
}
}

View File

@@ -20,9 +20,9 @@
// import com.google.zxing.LuminanceSource;
// import com.google.zxing.NotFoundException;
use crate::{LuminanceSource, Binarizer, Exceptions};
use crate::{Binarizer, Exceptions, LuminanceSource};
use super::{GlobalHistogramBinarizer, BitMatrix, BitArray};
use super::{BitArray, BitMatrix, GlobalHistogramBinarizer};
/**
* This class implements a local thresholding algorithm, which while slower than the
@@ -317,4 +317,4 @@ impl HybridBinarizer {
}
blackPoints
}
}
}

View File

@@ -20,7 +20,7 @@
// import java.util.ArrayList;
// import java.util.List;
use std::{rc::Rc, fmt};
use std::{fmt, rc::Rc};
use encoding::EncodingRef;
use unicode_segmentation::UnicodeSegmentation;
@@ -141,7 +141,7 @@ impl ECIInput for MinimalECIInput {
if index >= self.length() as u32 {
return Err(Exceptions::IndexOutOfBoundsException(index.to_string()));
}
Ok(self.bytes[index as usize] > 255)// && self.bytes[index as usize] <= u16::MAX)
Ok(self.bytes[index as usize] > 255) // && self.bytes[index as usize] <= u16::MAX)
}
/**
@@ -383,11 +383,11 @@ impl MinimalECIInput {
}
}
struct InputEdge {
c: String,
encoderIndex: usize, //the encoding of this edge
previous: Option<Rc<InputEdge>>,
cachedTotalSize: usize,
struct InputEdge {
c: String,
encoderIndex: usize, //the encoding of this edge
previous: Option<Rc<InputEdge>>,
cachedTotalSize: usize,
}
impl InputEdge {
pub fn new(
@@ -493,4 +493,4 @@ impl fmt::Display for MinimalECIInput {
}
write!(f, "{}", result)
}
}
}

View File

@@ -18,7 +18,6 @@ mod BitSourceTestCase;
#[cfg(test)]
mod PerspectiveTransformTestCase;
mod string_utils;
pub use string_utils::*;
@@ -99,9 +98,8 @@ pub use eci_encoder_set::*;
mod minimal_eci_input;
pub use minimal_eci_input::*;
mod global_histogram_binarizer;
pub use global_histogram_binarizer::*;
mod hybrid_binarizer;
pub use hybrid_binarizer::*;
pub use hybrid_binarizer::*;

View File

@@ -1,4 +1,3 @@
/*
* Copyright 2007 ZXing authors
*
@@ -210,4 +209,4 @@ impl PerspectiveTransform {
self.a13 * other.a31 + self.a23 * other.a32 + self.a33 * other.a33,
);
}
}
}

View File

@@ -2,7 +2,7 @@ use std::fmt;
use crate::Exceptions;
use super::{GenericGFRef, GenericGFPoly};
use super::{GenericGFPoly, GenericGFRef};
/**
* <p>This class contains utility methods for performing mathematical operations over
@@ -175,4 +175,4 @@ impl fmt::Display for GenericGF {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "GF({:#06x},{}", self.primitive, self.size)
}
}
}

View File

@@ -20,7 +20,7 @@ use std::fmt;
use crate::Exceptions;
use super::{GenericGFRef, GenericGF};
use super::{GenericGF, GenericGFRef};
/**
* <p>Represents a polynomial whose coefficients are elements of a GF.
@@ -337,4 +337,4 @@ impl fmt::Display for GenericGFPoly {
}
write!(f, "{}", result)
}
}
}

View File

@@ -78,4 +78,4 @@ mod reedsolomon_decoder;
pub use reedsolomon_decoder::*;
mod reedsolomon_encoder;
pub use reedsolomon_encoder::*;
pub use reedsolomon_encoder::*;

View File

@@ -18,7 +18,7 @@
use crate::Exceptions;
use super::{GenericGFRef, GenericGFPoly, GenericGF};
use super::{GenericGF, GenericGFPoly, GenericGFRef};
/**
* <p>Implements Reed-Solomon decoding, as the name implies.</p>
@@ -308,4 +308,4 @@ impl ReedSolomonDecoder {
}
return result;
}
}
}

View File

@@ -20,9 +20,9 @@
// import java.nio.charset.StandardCharsets;
// import java.util.Map;
use encoding::{EncodingRef, Encoding};
use encoding::{Encoding, EncodingRef};
use crate::{DecodingHintDictionary, DecodeHintType, DecodeHintValue};
use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary};
use lazy_static::lazy_static;
@@ -272,4 +272,4 @@ impl StringUtils {
// Otherwise, we take a wild guess with platform encoding
return encoding::all::UTF_8;
}
}
}