use crate::{
common::BitArray,
qrcode::{
common::{ErrorCorrectionLevel, Version},
encoder::matrix_util,
},
};
use crate::common::cpp_essentials::ByteMatrix;
#[test]
fn testToString() {
let mut array = ByteMatrix::new(3, 3);
array.set(0, 0, 0);
array.set(1, 0, 1);
array.set(2, 0, 0);
array.set(0, 1, 1);
array.set(1, 1, 0);
array.set(2, 1, 1);
array.set(0, 2, -1i8 as u8);
array.set(1, 2, -1i8 as u8);
array.set(2, 2, -1i8 as u8);
let expected = " 0 1 0\n 1 0 1\n \n";
assert_eq!(expected, array.to_string());
}
#[test]
fn testClearMatrix() {
let mut matrix = ByteMatrix::new(2, 2);
matrix_util::clearMatrix(&mut matrix);
assert_eq!(-1i8 as u8, matrix.get(0, 0));
assert_eq!(-1i8 as u8, matrix.get(1, 0));
assert_eq!(-1i8 as u8, matrix.get(0, 1));
assert_eq!(-1i8 as u8, matrix.get(1, 1));
}
#[test]
fn test_embed_basic_patterns1() {
let mut matrix = ByteMatrix::new(21, 21);
matrix_util::clearMatrix(&mut matrix);
matrix_util::embedBasicPatterns(
Version::getVersionForNumber(1).expect("version ok"),
&mut matrix,
)
.expect("op ok");
let expected = r" 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1
0
1
0
1
0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 0
1 0 0 0 0 0 1 0
1 0 1 1 1 0 1 0
1 0 1 1 1 0 1 0
1 0 1 1 1 0 1 0
1 0 0 0 0 0 1 0
1 1 1 1 1 1 1 0
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testEmbedBasicPatterns2() {
let mut matrix = ByteMatrix::new(25, 25);
matrix_util::clearMatrix(&mut matrix);
matrix_util::embedBasicPatterns(
Version::getVersionForNumber(2).expect("version ok"),
&mut matrix,
)
.expect("op ok");
let expected = r" 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1
1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1
0
1
0
1
0
1
0
1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 0 1
1 0 0 0 0 0 1 0 1 0 0 0 1
1 0 1 1 1 0 1 0 1 1 1 1 1
1 0 1 1 1 0 1 0
1 0 1 1 1 0 1 0
1 0 0 0 0 0 1 0
1 1 1 1 1 1 1 0
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testEmbedTypeInfo() {
let mut matrix = ByteMatrix::new(21, 21);
matrix_util::clearMatrix(&mut matrix);
matrix_util::embedTypeInfo(&ErrorCorrectionLevel::M, 5, &mut matrix).expect("op ok");
let expected = r" 0
1
1
1
0
0
1
1 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0
0
0
0
0
0
0
1
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testEmbedVersionInfo() {
let mut matrix = ByteMatrix::new(21, 21);
matrix_util::clearMatrix(&mut matrix);
matrix_util::maybeEmbedVersionInfo(
Version::getVersionForNumber(7).expect("Version"),
&mut matrix,
)
.expect("op ok");
let expected = r" 0 0 1
0 1 0
0 1 0
0 1 1
1 1 1
0 0 0
0 0 0 0 1 0
0 1 1 1 1 0
1 0 0 1 1 0
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testEmbedDataBits() {
let mut matrix = ByteMatrix::new(21, 21);
matrix_util::clearMatrix(&mut matrix);
matrix_util::embedBasicPatterns(
Version::getVersionForNumber(1).expect("version"),
&mut matrix,
)
.expect("op ok");
let bits = BitArray::new();
matrix_util::embedDataBits(&bits, -1, &mut matrix).expect("append");
let expected = r" 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1
1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testBuildMatrix() {
let bytes = [
32, 65, 205, 69, 41, 220, 46, 128, 236, 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237,
85, 224, 96, 74, 219, 61,
];
let mut bits = BitArray::new();
for c in bytes {
bits.appendBits(c, 8).expect("append");
}
let mut matrix = ByteMatrix::new(21, 21);
matrix_util::buildMatrix(
&bits,
&ErrorCorrectionLevel::H,
Version::getVersionForNumber(1).expect("version"), 3, &mut matrix,
)
.expect("append");
let expected = r" 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1
1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1
1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0
1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0
1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0
1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0
0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1
1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0
1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0
1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1
1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0
1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0
1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0
1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0
";
assert_eq!(expected, matrix.to_string());
}
#[test]
fn testFindMSBSet() {
assert_eq!(0, matrix_util::findMSBSet(0));
assert_eq!(1, matrix_util::findMSBSet(1));
assert_eq!(8, matrix_util::findMSBSet(0x80));
assert_eq!(32, matrix_util::findMSBSet(0x80000000));
}
#[test]
fn testCalculateBCHCode() {
assert_eq!(0xdc, matrix_util::calculateBCHCode(5, 0x537).expect("ok"));
assert_eq!(
0x1c2,
matrix_util::calculateBCHCode(0x13, 0x537).expect("ok")
);
assert_eq!(
0x214,
matrix_util::calculateBCHCode(0x1b, 0x537).expect("ok")
);
assert_eq!(0xc94, matrix_util::calculateBCHCode(7, 0x1f25).expect("ok"));
assert_eq!(0x5bc, matrix_util::calculateBCHCode(8, 0x1f25).expect("ok"));
assert_eq!(0xa99, matrix_util::calculateBCHCode(9, 0x1f25).expect("ok"));
assert_eq!(
0x4d3,
matrix_util::calculateBCHCode(10, 0x1f25).expect("ok")
);
assert_eq!(
0x9a6,
matrix_util::calculateBCHCode(20, 0x1f25).expect("ok")
);
assert_eq!(
0xd75,
matrix_util::calculateBCHCode(30, 0x1f25).expect("ok")
);
assert_eq!(
0xc69,
matrix_util::calculateBCHCode(40, 0x1f25).expect("ok")
);
}
#[test]
fn testMakeVersionInfoBits() {
let mut bits = BitArray::new();
matrix_util::makeVersionInfoBits(Version::getVersionForNumber(7).expect("version"), &mut bits)
.expect("op ok");
assert_eq!(" ...XXXXX ..X..X.X ..", bits.to_string());
}
#[test]
fn testMakeTypeInfoInfoBits() {
let mut bits = BitArray::new();
matrix_util::makeTypeInfoBits(&ErrorCorrectionLevel::M, 5, &mut bits).expect("append");
assert_eq!(" X......X X..XXX.", bits.to_string());
}