use std::env;
use std::fs::File;
use std::fs::OpenOptions;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use crate::io::file::CacheFile;
use crate::io::file::PositionedIo;
#[track_caller]
pub fn assert_golden(actual: &[u8], fixture: &str) -> Vec<u8> {
let golden = sparse_golden(fixture);
assert_eq!(actual.len(), golden.len(), "golden byte length differs");
if let Some(offset) = actual.iter().zip(&golden).position(|(a, b)| a != b) {
panic!(
"golden byte mismatch at offset {offset:#06x}: expected {:#04x}, got {:#04x}",
golden[offset], actual[offset]
);
}
golden
}
#[track_caller]
fn sparse_golden(input: &str) -> Vec<u8> {
let mut output: Option<Vec<u8>> = None;
for raw_line in input.lines() {
let line = raw_line.split('#').next().unwrap().trim();
if line.is_empty() {
continue;
}
let mut fields = line.split_whitespace();
let first = fields.next().unwrap();
if first == "length" {
let length = fields.next().unwrap().parse::<usize>().unwrap();
assert!(output.replace(vec![0_u8; length]).is_none());
continue;
}
let offset = usize::from_str_radix(first, 16).unwrap();
let encoded = fields.next().unwrap();
assert_eq!(encoded.len() % 2, 0);
let bytes = encoded
.as_bytes()
.as_chunks::<2>()
.0
.iter()
.map(|pair| u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap())
.collect::<Vec<_>>();
let output = output.as_mut().expect("golden length must come first");
output[offset..offset + bytes.len()].copy_from_slice(&bytes);
assert!(fields.next().is_none());
}
output.expect("golden fixture must declare its length")
}
pub struct TestFile {
path: PathBuf,
}
impl TestFile {
pub fn new(label: &str) -> Self {
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
Self {
path: env::temp_dir().join(format!("cache2-{label}-{}-{id}.tmp", std::process::id())),
}
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn open(&self) -> File {
OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&self.path)
.unwrap()
}
pub fn create_new(&self) -> File {
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&self.path)
.unwrap()
}
pub fn io(&self) -> Arc<dyn PositionedIo> {
Arc::new(CacheFile::open(&self.path).unwrap())
}
}
impl Drop for TestFile {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}