use crate::qrcode::decoder::{ErrorCorrectionLevel, FormatInformation};
const MASKED_TEST_FORMAT_INFO: u32 = 0x2BED;
const UNMASKED_TEST_FORMAT_INFO: u32 = MASKED_TEST_FORMAT_INFO ^ 0x5412;
#[test]
fn testBitsDiffering() {
assert_eq!(0, FormatInformation::numBitsDiffering(1, 1));
assert_eq!(1, FormatInformation::numBitsDiffering(0, 2));
assert_eq!(2, FormatInformation::numBitsDiffering(1, 2));
assert_eq!(32, FormatInformation::numBitsDiffering(u32::MAX, 0));
}
#[test]
fn testDecode() {
let expected = FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO,
MASKED_TEST_FORMAT_INFO,
);
assert!(expected.is_some());
let expected = expected.unwrap();
assert_eq!(0x07, expected.getDataMask());
assert_eq!(ErrorCorrectionLevel::Q, expected.getErrorCorrectionLevel());
assert_eq!(
expected,
FormatInformation::decodeFormatInformation(
UNMASKED_TEST_FORMAT_INFO,
MASKED_TEST_FORMAT_INFO
)
.expect("return")
);
}
#[test]
fn testDecodeWithBitDifference() {
let expected = FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO,
MASKED_TEST_FORMAT_INFO,
)
.unwrap();
assert_eq!(
expected,
FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x01,
MASKED_TEST_FORMAT_INFO ^ 0x01
)
.expect("return")
);
assert_eq!(
expected,
FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x03,
MASKED_TEST_FORMAT_INFO ^ 0x03
)
.expect("return")
);
assert_eq!(
expected,
FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x07,
MASKED_TEST_FORMAT_INFO ^ 0x07
)
.expect("return")
);
assert!(FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x0F,
MASKED_TEST_FORMAT_INFO ^ 0x0F
)
.is_none());
}
#[test]
fn testDecodeWithMisread() {
let expected = FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO,
MASKED_TEST_FORMAT_INFO,
)
.unwrap();
assert_eq!(
expected,
FormatInformation::decodeFormatInformation(
MASKED_TEST_FORMAT_INFO ^ 0x03,
MASKED_TEST_FORMAT_INFO ^ 0x0F
)
.unwrap()
);
}