use rust_hdf5::{AllocTime, H5File, Selection};
fn tmp(name: &str) -> std::path::PathBuf {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"rust_hdf5_alloc_time_{}_{}_{}.h5",
name,
std::process::id(),
n
))
}
#[test]
fn compact_dataset_with_no_fill_value_is_early() {
let path = tmp("compact_no_fill");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>()
.shape([4])
.compact()
.create("d")
.unwrap();
}
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("d").unwrap().alloc_time().unwrap(),
AllocTime::Early
);
std::fs::remove_file(&path).ok();
}
#[test]
fn compact_dataset_with_fill_value_is_early() {
let path = tmp("compact_fill");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>()
.shape([4])
.compact()
.fill_value(7)
.create("d")
.unwrap();
}
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("d").unwrap().alloc_time().unwrap(),
AllocTime::Early
);
std::fs::remove_file(&path).ok();
}
#[test]
fn contiguous_dataset_with_no_fill_value_is_late() {
let path = tmp("contiguous_no_fill");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>().shape([4]).create("d").unwrap();
}
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("d").unwrap().alloc_time().unwrap(),
AllocTime::Late
);
std::fs::remove_file(&path).ok();
}
#[test]
fn chunked_dataset_with_no_fill_value_is_incr() {
let path = tmp("chunked_no_fill");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>()
.shape([4])
.chunk(&[2])
.create("d")
.unwrap();
}
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("d").unwrap().alloc_time().unwrap(),
AllocTime::Incr
);
std::fs::remove_file(&path).ok();
}
#[test]
fn virtual_dataset_with_no_fill_value_is_incr() {
let path = tmp("virtual_no_fill");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>()
.shape([4])
.virtual_mapping(Selection::All, ".", "src", Selection::All)
.create("vds")
.unwrap();
}
let file = H5File::open(&path).unwrap();
assert_eq!(
file.dataset("vds").unwrap().alloc_time().unwrap(),
AllocTime::Incr
);
std::fs::remove_file(&path).ok();
}
#[test]
fn alloc_time_unavailable_in_write_mode() {
let path = tmp("write_mode");
let file = H5File::create(&path).unwrap();
let ds = file.new_dataset::<i32>().shape([4]).create("d").unwrap();
assert!(ds.alloc_time().is_err());
std::fs::remove_file(&path).ok();
}
#[test]
fn creating_an_external_dataset_with_a_fill_value_writes_no_raw_data_file() {
let dir = tmp("external_fill").with_extension("");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("ext.h5");
{
let file = H5File::create(&path).unwrap();
file.new_dataset::<i32>()
.shape([4])
.external(&[("raw.bin", 0, 16)])
.efile_prefix(dir.display().to_string())
.fill_value(7i32)
.create("d")
.unwrap();
file.close().unwrap();
}
assert!(
!dir.join("raw.bin").exists(),
"the fill value must not reach storage this writer never allocated"
);
let file = H5File::open(&path).unwrap();
assert!(
file.dataset_with(
"d",
rust_hdf5::DatasetAccess::new().efile_prefix(dir.display().to_string())
)
.unwrap()
.read_raw::<i32>()
.is_err(),
"reading before any write fails on the missing file rather than reporting the fill"
);
std::fs::remove_dir_all(&dir).ok();
}