use std::path::{Path, PathBuf};
use super::{decode_packets, decode_visits};
const MARGIN: f64 = 1.0 / 32.0;
fn files(dir: &Path, out: &mut Vec<PathBuf>) {
for entry in std::fs::read_dir(dir).expect("read fixtures").flatten() {
let path = entry.path();
if path.is_dir() {
files(&path, out);
} else if path.extension().is_some_and(|e| e == "j2k" || e == "jp2") {
out.push(path);
}
}
}
pub(crate) fn walk(bytes: &[u8], reduction: u8) -> Option<(u64, u64)> {
let codestream = if bytes.starts_with(&[0xFF, 0x4F]) {
bytes
} else {
crate::jp2::parse(bytes, &crate::WorkLimit::Default)
.ok()?
.codestream
};
let parsed = crate::codestream::parse_header(codestream).ok()?;
let cs = crate::codestream::parse_tiles(codestream, parsed).ok()?;
let mut left = u64::MAX;
for (index, tile) in cs.tiles.iter().enumerate() {
let header = cs.tile_header(index).ok()?;
crate::check_reduction(&header, reduction, Some(index)).ok()?;
decode_packets(&header, tile, &mut left, u64::MAX, reduction).ok()?;
}
Some((u64::MAX - left, crate::tile_bytes(&cs)))
}
#[test]
fn the_work_bound_grows_with_the_tile_data() {
assert_eq!(decode_visits(0), 1 << 24);
assert_eq!(decode_visits(1 << 19), 1 << 24);
assert_eq!(decode_visits((1 << 19) + 1), (1 << 24) + 32);
assert_eq!(decode_visits(1 << 24), 1 << 29);
assert_eq!(decode_visits(u64::MAX), u64::MAX);
}
#[test]
fn every_committed_file_walks_far_below_the_work_bound() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
let mut paths = Vec::new();
files(&root, &mut paths);
paths.sort();
let mut rows = Vec::new();
for path in &paths {
let name = path.strip_prefix(&root).unwrap().display().to_string();
if name.starts_with("work_bound/") && !name.starts_with("work_bound/opj_") {
continue;
}
let bytes = std::fs::read(path).unwrap();
for reduction in 0..=3 {
if let Some((spent, data)) = walk(&bytes, reduction) {
let bound = decode_visits(data);
let name = format!("{name} -r {reduction}");
rows.push((spent as f64 / bound as f64, spent, data, name));
}
}
}
rows.sort_by(|a, b| b.0.total_cmp(&a.0));
eprintln!("fraction of bound\tvisits\tdata bytes\tvisits per byte\tfile");
for (fraction, spent, data, name) in &rows {
let per_byte = *spent as f64 / (*data).max(1) as f64;
eprintln!("{fraction:.6}\t{spent}\t{data}\t{per_byte:.3}\t{name}");
}
let walked = rows.iter().filter(|row| row.3.ends_with(" -r 0")).count();
eprintln!("{walked} files walked of {}", paths.len());
assert!(walked > 100, "{walked} files walked");
assert!(rows.iter().any(|row| row.3.starts_with("work_bound/opj_")));
for (fraction, _, _, name) in &rows {
assert!(*fraction <= MARGIN, "{name} spends {fraction} of its bound");
}
}