use super::*;
fn cpu_within(deadline: std::time::Duration) -> Option<f32> {
let mut sampler = Sampler::new();
let give_up = std::time::Instant::now() + deadline;
let mut spun: u64 = 0;
loop {
let until = std::time::Instant::now() + std::time::Duration::from_millis(120);
while std::time::Instant::now() < until {
spun = spun.wrapping_add(1).wrapping_mul(2_654_435_761);
}
assert!(spun != 1, "the loop must not be optimised away");
if let Some(cpu) = sampler.sample(Path::new(".")).cpu {
return Some(cpu);
}
if std::time::Instant::now() >= give_up {
return None;
}
}
}
#[test]
fn test9031_a_fraction_never_divides_by_nothing() {
let empty = Fraction { used: 0, total: 0 };
assert_eq!(empty.ratio(), 0.0, "a total of nothing is not an error");
assert_eq!(empty.free(), 0);
let half = Fraction {
used: 4 * 1024,
total: 8 * 1024,
};
assert!((half.ratio() - 0.5).abs() < f32::EPSILON);
assert_eq!(half.free(), 4 * 1024);
let over = Fraction {
used: 10,
total: 8,
};
assert_eq!(over.free(), 0);
}
#[test]
fn test9032_the_first_sample_reports_no_processor_figure() {
let mut sampler = Sampler::new();
let first = sampler.sample(Path::new("."));
assert!(
first.cpu.is_none(),
"there is no processor figure until there are two readings"
);
if cfg!(any(target_os = "linux", target_os = "macos")) {
let cpu = cpu_within(std::time::Duration::from_secs(5))
.expect("both hosts keep cumulative processor counters");
assert!(
(0.0..=1.0).contains(&cpu),
"processor use is a fraction, not a percentage: {cpu}"
);
}
}
#[test]
#[cfg(target_os = "macos")]
fn test9039_macos_answers_for_the_processor_and_the_gpu() {
let cpu = cpu_within(std::time::Duration::from_secs(5))
.expect("macOS keeps the counters behind host_statistics");
assert!((0.0..=1.0).contains(&cpu), "{cpu}");
let mut sampler = Sampler::new();
let sample = sampler.sample(Path::new("."));
let gpu = sample
.gpu
.expect("the accelerator reports its own utilisation");
assert!((0.0..=1.0).contains(&gpu), "{gpu}");
assert!(
sample.vram.is_none(),
"unified memory has no separate pool to report"
);
}
#[test]
fn test9033_every_answer_is_a_fraction_or_nothing() {
let mut sampler = Sampler::new();
let sample = sampler.sample(Path::new("."));
for (name, fraction) in [
("memory", sample.memory),
("vram", sample.vram),
("disk", sample.disk),
] {
let Some(fraction) = fraction else { continue };
assert!(fraction.total > 0, "{name} reported a total of nothing");
assert!(
(0.0..=1.0).contains(&fraction.ratio()),
"{name} is out of range: {}",
fraction.ratio()
);
}
for (name, load) in [("cpu", sample.cpu), ("gpu", sample.gpu)] {
let Some(load) = load else { continue };
assert!((0.0..=1.0).contains(&load), "{name} is out of range: {load}");
}
if let Some(disk) = sample.disk {
assert!(disk.total > 0, "the workspace is on a volume with a size");
}
}
#[test]
fn test9034_the_disk_is_the_one_the_path_is_on() {
let root = disk(Path::new("/"));
let temporary = disk(&std::env::temp_dir());
assert!(root.is_some(), "/ is on a volume");
assert!(temporary.is_some(), "the temp directory is on a volume");
}