#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::io::Cursor;
use apfs_core::spaceman::is_block_free;
const CHAIN: &[u8] = include_bytes!("../../tests/data/apfs_container_chain.bin");
const BLOCK_SIZE: usize = 4096;
const SPACEMAN_BLOCK: u64 = 11;
#[test]
fn allocated_blocks_report_not_free() {
let mut r = Cursor::new(CHAIN);
for b in [0u64, 11, 12, 331, 332, 342, 343, 344] {
let free = is_block_free(&mut r, SPACEMAN_BLOCK, b, BLOCK_SIZE)
.unwrap_or_else(|e| panic!("is_block_free({b}): {e:?}"));
assert!(!free, "block {b} holds a live object, must be allocated");
}
}
#[test]
fn free_blocks_report_free() {
let mut r = Cursor::new(CHAIN);
for b in [345u64, 346, 1000, 30_000, 32_757] {
let free = is_block_free(&mut r, SPACEMAN_BLOCK, b, BLOCK_SIZE)
.unwrap_or_else(|e| panic!("is_block_free({b}): {e:?}"));
assert!(free, "block {b} is past the carved data, must be free");
}
}
#[test]
fn free_count_matches_spaceman_accounting() {
let mut r = Cursor::new(CHAIN);
let block_count: u64 = 32_758;
let mut allocated = 0u64;
for b in 0..block_count {
if !is_block_free(&mut r, SPACEMAN_BLOCK, b, BLOCK_SIZE).expect("is_block_free") {
allocated += 1;
}
}
assert_eq!(
allocated, 345,
"allocated-block count vs spaceman free_count"
);
}