use std::os::windows::io::AsRawHandle;
use super::{NumaNode, highest_numa_node, volume_numa_node};
fn scratch(tag: &str) -> (std::path::PathBuf, std::fs::File) {
let path = std::env::temp_dir().join(format!(
"win-numa-sys-node-{}-{tag}.tmp",
std::process::id()
));
let file = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&path)
.expect("a scratch file in the temp directory");
(path, file)
}
#[test]
fn a_node_round_trips_through_the_newtype() {
for raw in [0_u32, 1, 7, 63, u32::MAX - 1, u32::MAX] {
assert_eq!(NumaNode::new(raw).get(), raw, "for {raw}");
}
}
#[test]
fn nodes_compare_and_order_by_their_number() {
assert_eq!(NumaNode::new(3), NumaNode::new(3));
assert_ne!(NumaNode::new(3), NumaNode::new(4));
assert!(NumaNode::new(3) < NumaNode::new(4));
let mut nodes = [NumaNode::new(9), NumaNode::new(2), NumaNode::new(5)];
nodes.sort_unstable();
assert_eq!(
nodes,
[NumaNode::new(2), NumaNode::new(5), NumaNode::new(9)]
);
}
#[test]
fn display_names_the_thing_rather_than_printing_a_bare_integer() {
assert_eq!(NumaNode::new(0).to_string(), "node 0");
assert_eq!(NumaNode::new(12).to_string(), "node 12");
}
#[test]
fn the_highest_node_is_stable_across_calls() {
assert_eq!(highest_numa_node(), highest_numa_node());
}
#[test]
fn the_highest_node_is_a_number_not_a_count() {
if let Some(highest) = highest_numa_node() {
assert!(
highest.get() < u32::MAX,
"a real machine's highest node cannot be the no-preference sentinel"
);
}
}
#[test]
fn the_volume_query_answers_or_errors_but_never_fabricates() {
let (path, file) = scratch("answers-or-errors");
match volume_numa_node(file.as_raw_handle()) {
Ok(node) => assert!(
node.get() < u32::MAX,
"a reported node cannot be the no-preference sentinel"
),
Err(error) => assert_ne!(
error.kind(),
std::io::ErrorKind::Other,
"a failure should carry the OS error, not a placeholder"
),
}
drop(file);
let _ = std::fs::remove_file(path);
}
#[test]
fn the_volume_query_is_stable_for_one_handle() {
let (path, file) = scratch("stable");
let first = volume_numa_node(file.as_raw_handle()).ok();
let second = volume_numa_node(file.as_raw_handle()).ok();
assert_eq!(first, second);
drop(file);
let _ = std::fs::remove_file(path);
}
#[test]
fn an_invalid_handle_errors() {
let error = volume_numa_node(std::ptr::null_mut())
.expect_err("the null handle is not a file or directory handle");
assert!(
error.raw_os_error().is_some(),
"the failure should carry the OS error code, got {error:?}"
);
}