use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
fn committed_corpus() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("corpus")
}
fn file_names(dir: &Path) -> BTreeSet<String> {
std::fs::read_dir(dir)
.expect("read corpus directory")
.flatten()
.filter_map(|e| e.file_name().to_str().map(String::from))
.collect()
}
#[test]
fn corpus_regenerates_byte_identically() {
let committed = committed_corpus();
assert!(
committed.is_dir(),
"conformance corpus missing — run `cargo run --example gen_corpus`"
);
let scratch = TempDir::new().unwrap();
let status = Command::new(env!("CARGO"))
.args(["run", "--quiet", "--example", "gen_corpus", "--"])
.arg(scratch.path())
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("run the corpus generator");
assert!(status.success(), "corpus generator failed");
let fresh_names = file_names(scratch.path());
let committed_names = file_names(&committed);
assert_eq!(
committed_names, fresh_names,
"corpus file set differs — regenerate and commit `tests/corpus/`"
);
for name in &committed_names {
let a = std::fs::read(committed.join(name)).unwrap();
let b = std::fs::read(scratch.path().join(name)).unwrap();
assert_eq!(
a,
b,
"corpus artifact `{}` differs from freshly generated output — \
rerun `cargo run --example gen_corpus` and commit the result \
(committed {} bytes, generated {} bytes)",
name,
a.len(),
b.len()
);
}
}
#[test]
fn corpus_covers_the_reader_feature_matrix() {
let names = file_names(&committed_corpus());
let has = |needle: &str| names.iter().any(|n| n.contains(needle) && n.ends_with(".htt"));
for required in [
"empty",
"structural_only",
"uncompressed",
"zstd",
"wal_live",
"wal_batched",
"deletes_and_updates",
"unicode_keys",
"deep_hierarchy",
"gacl",
"meaning_addressed",
"quantized",
"epochs",
"history_sidecar",
"torn_tail",
] {
assert!(has(required), "conformance corpus is missing a `{}` case", required);
}
for name in names.iter().filter(|n| n.ends_with(".htt")) {
let expected = name.replace(".htt", ".expected.json");
assert!(
names.contains(&expected),
"corpus file `{}` has no `{}` alongside it",
name,
expected
);
}
}