Oversize files before/after: igpu 2 -> 0 (lib.rs 1386 into 12 modules with the long Gpu and Recorder impls cut between methods, shaders.rs 440 into WGSL constant modules), gptoss-igpu 1 -> 0, gptoss-npu 1 -> 0, openvino 1 -> 0, shm 1 -> 0 (the unix and windows mapping code in their own files). unsafe blocks and SAFETY comments moved verbatim (openvino 110 -> 110). Census prod/test (fn>30 / cx>10 / nesting>4 / wide lines): igpu 12/12, 1/1, 0/0, 5/0 -> 10/12, 1/1, 0/0, 5/0; gptoss-igpu, gptoss-npu, openvino and shm unchanged. Test ids and outcomes unchanged.
53 lines
1.5 KiB
Rust
53 lines
1.5 KiB
Rust
//! Unit tests of the file mappings (moved out of `lib.rs` by STRA-338; the test ids are unchanged).
|
|
|
|
use super::*;
|
|
|
|
fn temp(name: &str) -> std::path::PathBuf {
|
|
std::env::temp_dir().join(format!("leleka-shm-{name}-{}", std::process::id()))
|
|
}
|
|
|
|
#[test]
|
|
fn a_read_map_shows_the_file() {
|
|
let path = temp("read");
|
|
std::fs::write(&path, b"hello mapping").unwrap();
|
|
let map = ReadMap::open(&path).unwrap();
|
|
assert_eq!(map.as_bytes(), b"hello mapping");
|
|
drop(map);
|
|
std::fs::remove_file(&path).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn two_mappings_of_one_region_see_each_others_writes() {
|
|
let path = temp("shared");
|
|
let a = Shared::create(&path, 4096).unwrap();
|
|
let b = Shared::open(&path).unwrap();
|
|
assert_eq!(b.len(), 4096);
|
|
a.write(100, b"from a").unwrap();
|
|
let mut got = [0u8; 6];
|
|
b.read(100, &mut got).unwrap();
|
|
assert_eq!(&got, b"from a");
|
|
b.write(4090, b"tail!!").unwrap();
|
|
let mut tail = [0u8; 6];
|
|
a.read(4090, &mut tail).unwrap();
|
|
assert_eq!(&tail, b"tail!!");
|
|
drop((a, b));
|
|
std::fs::remove_file(&path).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn out_of_range_access_is_an_error() {
|
|
let path = temp("range");
|
|
let a = Shared::create(&path, 64).unwrap();
|
|
assert!(a.write(60, &[0u8; 8]).is_err());
|
|
assert!(a.read(usize::MAX, &mut [0u8; 1]).is_err());
|
|
assert!(a.address_of(0, 65).is_err());
|
|
assert!(a.address_of(0, 64).is_ok());
|
|
drop(a);
|
|
std::fs::remove_file(&path).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_region_is_refused() {
|
|
assert!(Shared::create(&temp("empty"), 0).is_err());
|
|
}
|