#![cfg(not(target_pointer_width = "32"))]
use hdf5::Extent;
use hdf5::file::LibraryVersion;
use hdf5_pure::{File, FileBuilder};
use tempfile::tempdir;
const SIZES: &[usize] = &[20, 300, 2000, 50000, 140000];
fn write_with_c(path: &std::path::Path, n: usize) {
let file = hdf5::File::with_options()
.with_fapl(|p| p.libver_bounds(LibraryVersion::V110, LibraryVersion::latest()))
.create(path)
.unwrap();
let ds = file
.new_dataset::<i32>()
.chunk((1,))
.shape((Extent::resizable(n),))
.create("d")
.unwrap();
let data: Vec<i32> = (0..n as i32).collect();
ds.write(&data).unwrap();
file.close().unwrap();
}
fn write_with_pure(path: &std::path::Path, n: usize) {
let data: Vec<i32> = (0..n as i32).collect();
let mut b = FileBuilder::new();
b.create_dataset("d")
.with_i32_data(&data)
.with_shape(&[n as u64])
.with_maxshape(&[u64::MAX])
.with_chunks(&[1]);
b.write(path).unwrap();
}
#[test]
fn pure_writes_c_reads() {
for &n in SIZES {
let dir = tempdir().unwrap();
let path = dir.path().join("ea.h5");
write_with_pure(&path, n);
let file = hdf5::File::open(&path).unwrap();
let ds = file.dataset("d").unwrap();
let values = ds.read_raw::<i32>().unwrap();
let expected: Vec<i32> = (0..n as i32).collect();
assert_eq!(values.len(), n, "C read wrong length for n={n}");
assert_eq!(values, expected, "C read wrong data for n={n}");
}
}
#[test]
fn c_writes_pure_reads() {
for &n in SIZES {
let dir = tempdir().unwrap();
let path = dir.path().join("ea.h5");
write_with_c(&path, n);
let bytes = std::fs::read(&path).unwrap();
let file = File::from_bytes(bytes).unwrap();
let ds = file.dataset("d").unwrap();
let values = ds.read_i32().unwrap();
let expected: Vec<i32> = (0..n as i32).collect();
assert_eq!(values.len(), n, "hdf5-pure read wrong length for n={n}");
assert_eq!(values, expected, "hdf5-pure read wrong data for n={n}");
}
}
#[test]
fn eahd_stats_match_c() {
fn stats_of(path: &std::path::Path) -> Vec<u64> {
let b = std::fs::read(path).unwrap();
let h = (0..b.len() - 4).find(|&i| &b[i..i + 4] == b"EAHD").unwrap();
(0..6)
.map(|k| {
let p = h + 12 + k * 8;
u64::from_le_bytes(b[p..p + 8].try_into().unwrap())
})
.collect()
}
for &n in SIZES {
let dir = tempdir().unwrap();
let c_path = dir.path().join("c.h5");
let pure_path = dir.path().join("pure.h5");
write_with_c(&c_path, n);
write_with_pure(&pure_path, n);
assert_eq!(
stats_of(&pure_path),
stats_of(&c_path),
"EAHD stats differ from the C library at n={n}"
);
}
}