feat: Add more tests to b2p edge cases

This commit is contained in:
0xalivecow 2024-11-02 14:18:10 +01:00
parent 8db0bbaa63
commit 26ca12b419
No known key found for this signature in database
2 changed files with 64 additions and 18 deletions

View file

@ -51,4 +51,46 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn block2poly_task03() -> Result<()> {
let block: Value = json!({"block" : "AAAAAAAAAAAAAAAAAAAAAA==", "semantic" : "gcm"});
let coefficients: Vec<u8> = vec![];
assert_eq!(
block2poly(&block)?,
coefficients,
"Coefficients were: {:?}",
block2poly(&block)?
);
Ok(())
}
#[test]
fn block2poly_task04() -> Result<()> {
let block: Value = json!({"block" : "", "semantic" : "gcm"});
let coefficients: Vec<u8> = vec![];
assert_eq!(
block2poly(&block)?,
coefficients,
"Coefficients were: {:?}",
block2poly(&block)?
);
Ok(())
}
#[test]
fn block2poly_task_empty_xex() -> Result<()> {
let block: Value = json!({"block" : "", "semantic" : "xex"});
let coefficients: Vec<u8> = vec![];
assert_eq!(
block2poly(&block)?,
coefficients,
"Coefficients were: {:?}",
block2poly(&block)?
);
Ok(())
}
} }

View file

@ -104,31 +104,35 @@ pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
} }
pub fn block_2_polynomial(block: Vec<u8>, semantic: &str) -> Result<Vec<u8>> { pub fn block_2_polynomial(block: Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
let mut output: Vec<u8> = vec![]; if block.len() == 0 {
match semantic { Ok(Vec::new())
"xex" => { } else {
for i in 0u8..=15 { let mut output: Vec<u8> = vec![];
for j in 0u8..=7 { match semantic {
if (block[i as usize] >> j) & 1 == 1 { "xex" => {
output.push(8 * i + j); for i in 0u8..=15 {
for j in 0u8..=7 {
if (block[i as usize] >> j) & 1 == 1 {
output.push(8 * i + j);
}
} }
} }
output.sort();
Ok(output)
} }
output.sort(); "gcm" => {
Ok(output) for i in 0u8..=15 {
} for j in 0u8..=7 {
"gcm" => { if (block[i as usize] >> j) & 1 == 1 {
for i in 0u8..=15 { output.push(8 * i + 7 - j);
for j in 0u8..=7 { }
if (block[i as usize] >> j) & 1 == 1 {
output.push(8 * i + 7 - j);
} }
} }
output.sort();
Ok(output)
} }
output.sort(); _ => Err(anyhow!("Error in b2p")),
Ok(output)
} }
_ => Err(anyhow!("Error in b2p")),
} }
} }