Skip to content

Commit af09544

Browse files
committed
text: cleanup and flesh out missing tests
1 parent 163d212 commit af09544

10 files changed

Lines changed: 329 additions & 86 deletions

src/aztec/DecoderTest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// import org.junit.Assert;
2828

2929
use crate::{
30-
aztec::shared_test_methods::{stripSpace, toBitArray, toBooleanArray},
30+
common::test_utils::{strip_space, to_bit_array, to_boolean_array},
3131
common::BitMatrix,
3232
Point,
3333
};
@@ -73,10 +73,10 @@ fn test_high_level_decode() {
7373
}
7474

7575
fn test_high_level_decode_string(expectedString: &str, b: &str) {
76-
let bits = toBitArray(&stripSpace(b));
76+
let bits = to_bit_array(&strip_space(b));
7777
assert_eq!(
7878
expectedString,
79-
decoder::highLevelDecode(&toBooleanArray(&bits)).expect("highLevelDecode Failed"),
79+
decoder::highLevelDecode(&to_boolean_array(&bits)).expect("highLevelDecode Failed"),
8080
"highLevelDecode() failed for input bits: {b}"
8181
);
8282
}

src/aztec/EncoderTest.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616

1717
use crate::{
18-
aztec::{
19-
aztec_detector_result::AztecDetectorRXingResult,
20-
decoder,
21-
encoder::HighLevelEncoder,
22-
shared_test_methods::{stripSpace, toBitArray, toBooleanArray},
18+
aztec::{aztec_detector_result::AztecDetectorRXingResult, decoder, encoder::HighLevelEncoder},
19+
common::{
20+
test_utils::{strip_space, to_bit_array, to_boolean_array},
21+
CharacterSet,
2322
},
24-
common::CharacterSet,
2523
BarcodeFormat, EncodeHints, Point,
2624
};
2725

@@ -793,18 +791,18 @@ fn getPseudoRandom() -> rand::rngs::ThreadRng {
793791
fn testModeMessageComplex(compact: bool, layers: u32, words: u32, expected: &str) {
794792
let indata = aztec_encoder::generateModeMessage(compact, layers, words).expect("generate mode");
795793
assert_eq!(
796-
stripSpace(expected),
797-
stripSpace(&indata.to_string()),
794+
strip_space(expected),
795+
strip_space(&indata.to_string()),
798796
"generateModeMessage() failed"
799797
);
800798
}
801799

802800
fn testStuffBits(wordSize: usize, bits: &str, expected: &str) {
803-
let indata = toBitArray(bits);
801+
let indata = to_bit_array(bits);
804802
let stuffed = aztec_encoder::stuffBits(&indata, wordSize).unwrap();
805803
assert_eq!(
806-
stripSpace(expected),
807-
stripSpace(&stuffed.to_string()),
804+
strip_space(expected),
805+
strip_space(&stuffed.to_string()),
808806
"stuffBits() failed for input string: {bits}"
809807
);
810808
}
@@ -819,14 +817,14 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) {
819817
.encode()
820818
.expect("high level ok");
821819
// let bits = HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode();
822-
let receivedBits = stripSpace(&bits.to_string());
820+
let receivedBits = strip_space(&bits.to_string());
823821
assert_eq!(
824822
s,
825-
decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode")
823+
decoder::highLevelDecode(&to_boolean_array(&bits)).expect("must decode")
826824
);
827825
// dbg!(s, decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode"));
828826
assert_eq!(
829-
stripSpace(expectedBits),
827+
strip_space(expectedBits),
830828
receivedBits,
831829
"highLevelEncode() failed for input string: {s}"
832830
);
@@ -841,15 +839,15 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) {
841839
.encode()
842840
.expect("high level ok");
843841
// let bits = HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode();
844-
let receivedBits = stripSpace(&bits.to_string());
842+
let receivedBits = strip_space(&bits.to_string());
845843
assert_eq!(
846-
stripSpace(expectedBits),
844+
strip_space(expectedBits),
847845
receivedBits,
848846
"highLevelEncode() failed for input string: {s}"
849847
);
850848
assert_eq!(
851849
s,
852-
decoder::highLevelDecode(&toBooleanArray(&bits)).expect("must decode")
850+
decoder::highLevelDecode(&to_boolean_array(&bits)).expect("must decode")
853851
);
854852
}
855853

@@ -862,11 +860,11 @@ fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) {
862860
.encode()
863861
.expect("high level ok");
864862
//let bits = HighLevelEncoder::new(s.getBytes(StandardCharsets.ISO_8859_1)).encode().unwrap();
865-
let receivedBitCount = stripSpace(&bits.to_string()).len();
863+
let receivedBitCount = strip_space(&bits.to_string()).len();
866864
// dbg!(s, decoder::highLevelDecode(&toBooleanArray(&bits)).expect("should decode"));
867865
assert_eq!(
868866
s,
869-
decoder::highLevelDecode(&toBooleanArray(&bits)).expect("should decode")
867+
decoder::highLevelDecode(&to_boolean_array(&bits)).expect("should decode")
870868
);
871869
// assert!(
872870
// expectedReceivedBits as usize >= receivedBitCount,

src/aztec/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ mod DecoderTest;
1414
mod DetectorTest;
1515
#[cfg(test)]
1616
mod EncoderTest;
17-
18-
mod shared_test_methods;

src/aztec/shared_test_methods.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/common/BitArrayTestCase.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use crate::common::BIT_FIELD_BASE_BITS;
3030

3131
use super::{BitArray, BIT_FIELD_SHIFT_BITS};
32+
use crate::common::test_utils::arrays_are_equal;
3233
use rand::Rng;
3334

3435
#[test]
@@ -303,6 +304,23 @@ fn test_equals() {
303304
// assert_eq!(a.hash(), b.hash());
304305
}
305306

307+
#[test]
308+
fn test_reverse_with_modification() {
309+
let mut ba = BitArray::with_size(10);
310+
ba.set(0);
311+
ba.reverse();
312+
// Bit 0 was set, now bit 9 should be set (if it reverses 0..9)
313+
assert!(ba.get(9));
314+
assert!(!ba.get(0));
315+
316+
ba.set(8); // Now bits 8 and 9 are set
317+
ba.reverse();
318+
// If it correctly reverses the CURRENT bits, bit 0 and 1 should be set.
319+
// If it uses the stale cache, only bit 0 will be set.
320+
assert!(ba.get(0), "Bit 0 should be set");
321+
assert!(ba.get(1), "Bit 1 should be set (reverse of bit 8)");
322+
}
323+
306324
#[test]
307325
fn test_xor() {
308326
let val_1: super::BitFieldBaseType = 0b01_00_11;
@@ -365,13 +383,4 @@ fn bit_set(bits: &[super::BitFieldBaseType], i: usize) -> bool {
365383
(bits[i / BIT_FIELD_BASE_BITS] & (1 << (i & BIT_FIELD_SHIFT_BITS))) != 0
366384
}
367385

368-
fn arrays_are_equal<T: Eq + Default>(left: &[T], right: &[T], size: usize) -> bool {
369-
for i in 0..size {
370-
if left[i] != right[i] {
371-
return false;
372-
}
373-
}
374-
true
375-
}
376-
377386
// }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2026 RXing authors
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
use super::test_utils::MockLuminanceSource;
18+
use super::AdaptiveThresholdBinarizer;
19+
use crate::Binarizer;
20+
21+
#[test]
22+
fn test_adaptive_threshold_binarizer() {
23+
let width = 40;
24+
let height = 40;
25+
let mut luminances = vec![0; width * height];
26+
27+
// Create a pattern with alternating black and white bars
28+
for y in 0..height {
29+
for x in 0..width {
30+
if (x / 5) % 2 == 0 {
31+
luminances[y * width + x] = 0; // Black
32+
} else {
33+
luminances[y * width + x] = 255; // White
34+
}
35+
}
36+
}
37+
38+
let source = MockLuminanceSource::new(width, height, luminances);
39+
// Radius of 10 for adaptive threshold
40+
let binarizer = AdaptiveThresholdBinarizer::new(source, 10);
41+
let matrix = binarizer.get_black_matrix().unwrap();
42+
43+
assert_eq!(width as u32, matrix.getWidth());
44+
assert_eq!(height as u32, matrix.getHeight());
45+
46+
// Check points in the middle of bars to avoid boundary issues
47+
assert!(matrix.get(2, 2)); // First bar (black)
48+
assert!(!matrix.get(7, 7)); // Second bar (white)
49+
assert!(matrix.get(12, 12)); // Third bar (black)
50+
assert!(!matrix.get(17, 17)); // Fourth bar (white)
51+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2026 RXing authors
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
use super::test_utils::MockLuminanceSource;
18+
use super::HybridBinarizer;
19+
use crate::Binarizer;
20+
21+
#[test]
22+
fn test_hybrid_binarizer_small_image() {
23+
// Small image should fall back to GlobalHistogramBinarizer
24+
let width = 10;
25+
let height = 10;
26+
let mut luminances = vec![0; width * height];
27+
// Create a simple black and white pattern
28+
for y in 0..height {
29+
for x in 0..width {
30+
if x < 5 {
31+
luminances[y * width + x] = 0; // Black
32+
} else {
33+
luminances[y * width + x] = 255; // White
34+
}
35+
}
36+
}
37+
38+
let source = MockLuminanceSource::new(width, height, luminances);
39+
let binarizer = HybridBinarizer::new(source);
40+
let matrix = binarizer.get_black_matrix().unwrap();
41+
42+
assert_eq!(width as u32, matrix.getWidth());
43+
assert_eq!(height as u32, matrix.getHeight());
44+
45+
for y in 0..height {
46+
for x in 0..width {
47+
if x < 5 {
48+
assert!(matrix.get(x as u32, y as u32));
49+
} else {
50+
assert!(!matrix.get(x as u32, y as u32));
51+
}
52+
}
53+
}
54+
}
55+
56+
#[test]
57+
fn test_hybrid_binarizer_large_image() {
58+
// Large image uses local thresholding
59+
let width = 40; // HybridBinarizer::MINIMUM_DIMENSION is 40
60+
let height = 40;
61+
let mut luminances = vec![0; width * height];
62+
63+
// Create a pattern with a gradient to test local thresholding
64+
for y in 0..height {
65+
for x in 0..width {
66+
if x < 20 {
67+
luminances[y * width + x] = 50; // Dark grey
68+
} else {
69+
luminances[y * width + x] = 200; // Light grey
70+
}
71+
}
72+
}
73+
74+
let source = MockLuminanceSource::new(width, height, luminances);
75+
let binarizer = HybridBinarizer::new(source);
76+
let matrix = binarizer.get_black_matrix().unwrap();
77+
78+
assert_eq!(width as u32, matrix.getWidth());
79+
assert_eq!(height as u32, matrix.getHeight());
80+
81+
for y in 0..height {
82+
for x in 0..width {
83+
if x < 20 {
84+
assert!(
85+
matrix.get(x as u32, y as u32),
86+
"Bit at ({}, {}) should be set",
87+
x,
88+
y
89+
);
90+
} else {
91+
assert!(
92+
!matrix.get(x as u32, y as u32),
93+
"Bit at ({}, {}) should NOT be set",
94+
x,
95+
y
96+
);
97+
}
98+
}
99+
}
100+
}

src/common/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ pub mod reedsolomon;
33

44
use crate::Point;
55

6+
#[cfg(test)]
7+
pub mod test_utils;
8+
69
#[cfg(test)]
710
mod StringUtilsTestCase;
811

912
#[cfg(test)]
1013
mod BitArrayTestCase;
1114

15+
#[cfg(test)]
16+
mod hybrid_binarizer_test_case;
17+
18+
#[cfg(test)]
19+
mod adaptive_threshold_binarizer_test_case;
20+
1221
#[cfg(test)]
1322
pub(crate) mod bit_matrix_test_case;
1423

0 commit comments

Comments
 (0)