Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-IMP-PREVIEW-READ: the corpus test asks preview_plan for every file

goujicheng103089535a17
diff --git a/crates/raw/tests/sparse_corpus.rs b/crates/raw/tests/sparse_corpus.rs
index 3ab735299..15e6398b2 100644
--- a/crates/raw/tests/sparse_corpus.rs
+++ b/crates/raw/tests/sparse_corpus.rs
@@ -64,45 +64,71 @@ fn partial_reads_describe_raws_as_whole_files_do() {
paths.sort();
// format → (files ready, bytes read, bytes total, files read whole)
let mut stats: BTreeMap<String, (usize, u64, u64, usize)> = BTreeMap::new();
+ let mut preview_stats: BTreeMap<String, (usize, u64, u64, usize)> = BTreeMap::new();
for p in paths {
let name = p.file_name().unwrap().to_string_lossy().to_string();
let file = std::fs::read(&p).unwrap();
let format = probe(&file).map_or("?".to_string(), |f| format!("{f:?}"));
- let (plan, zeros, loaded) = run(&file, 0, false);
+ let whole = describe(&file);
+ let preview = embedded_preview(&file);
let entry = stats.entry(format.clone()).or_default();
- match plan {
- HeaderPlan::Ready => {
+ let header_ready = match run(&file, 0, false) {
+ (HeaderPlan::Ready, zeros, loaded) => {
let (plan_ff, ones, loaded_ff) = run(&file, 0xff, false);
assert_eq!((plan_ff, &loaded_ff), (HeaderPlan::Ready, &loaded), "{name}: the plan read skipped bytes");
- let whole = describe(&file);
assert_eq!(describe(&zeros), whole, "{name}: differs from the whole file (skipped bytes 0)");
assert_eq!(describe(&ones), whole, "{name}: differs from the whole file (skipped bytes 0xFF)");
entry.0 += 1;
entry.1 += loaded.total();
entry.2 += file.len() as u64;
- // with the preview (for a raw shown from it), the preview is what the whole file gives too
- let preview = embedded_preview(&file);
- for fill in [0, 0xff] {
- let (plan, buf, _) = run(&file, fill, true);
- assert_eq!(plan, HeaderPlan::Ready, "{name}: preview plan");
- assert_eq!(describe(&buf), whole, "{name}: preview plan, skipped bytes {fill}");
- assert!(embedded_preview(&buf) == preview, "{name}: a different embedded preview (skipped bytes {fill})");
- }
if loaded.total() * 4 > file.len() as u64 {
eprintln!("{name}: read {:.0} %", loaded.total() as f64 * 100.0 / file.len() as f64);
}
if matches!(format.as_str(), "Cr3" | "Dng") && file.len() > 8 << 20 {
assert!(loaded.total() * 4 < file.len() as u64, "{name}: read {} of {} bytes", loaded.total(), file.len());
}
+ true
}
- HeaderPlan::Whole(why) => {
+ (HeaderPlan::Whole(why), _, _) => {
entry.3 += 1;
eprintln!("{name}: whole ({why})");
+ false
+ }
+ (HeaderPlan::Need(_), _, _) => unreachable!(),
+ };
+ // The preview plan (what the display path reads) is asked for **every** file, because a
+ // header probe that reads samples (a NEF, an ORF, a PEF) makes the plan above `Whole` and
+ // would otherwise hide its preview from this check. Ready means the preview handed on is the
+ // whole file's, byte for byte, with the skipped bytes zeroed and again 0xFF; `Whole` is a
+ // legitimate answer here (that format keeps reading whole for a preview too) and is counted.
+ let pentry = preview_stats.entry(format.clone()).or_default();
+ match run(&file, 0, true) {
+ (HeaderPlan::Ready, buf, loaded_p) => {
+ if header_ready {
+ assert_eq!(describe(&buf), whole, "{name}: preview plan differs from the whole file");
+ }
+ assert!(embedded_preview(&buf) == preview, "{name}: a different embedded preview");
+ let (plan_ff, buf_ff, loaded_ff) = run(&file, 0xff, true);
+ assert_eq!((plan_ff, loaded_ff.total()), (HeaderPlan::Ready, loaded_p.total()), "{name}: the preview plan read skipped bytes");
+ assert!(embedded_preview(&buf_ff) == preview, "{name}: a different embedded preview (skipped bytes 0xFF)");
+ pentry.0 += 1;
+ pentry.1 += loaded_p.total();
+ pentry.2 += file.len() as u64;
}
- HeaderPlan::Need(_) => unreachable!(),
+ (HeaderPlan::Whole(why), _, _) => {
+ pentry.3 += 1;
+ eprintln!("{name}: preview whole ({why})");
+ }
+ (HeaderPlan::Need(_), _, _) => unreachable!(),
}
}
for (format, (ready, read, total, whole)) in stats {
eprintln!("{format:>10}: {ready} described from {:.1} % of their bytes, {whole} read whole", read as f64 * 100.0 / total.max(1) as f64);
}
+ for (format, (ready, read, total, whole)) in preview_stats {
+ eprintln!(
+ "{format:>10} preview: {ready} previews read from {:.1} % of their bytes, {whole} read whole",
+ read as f64 * 100.0 / total.max(1) as f64
+ );
+ }
}