use crate::test_mode::base::decide;
use cubecl::prelude::Client;
use std::fmt::Display;
#[track_caller]
pub fn skip_unless_cpu(client: &Client) -> bool {
if client.properties().hardware.num_cpu_cores.is_none() {
TestOutcome::Validated(ValidationResult::Skipped(
"needs a CPU backend; this runtime reports no CPU cores".to_string(),
))
.enforce();
return true;
}
false
}
#[track_caller]
pub fn skip_unless_plane_holds(client: &Client, units: u32) -> bool {
let plane = client.properties().hardware.plane_size_max;
if plane < units {
TestOutcome::Validated(ValidationResult::Skipped(format!(
"needs a plane of at least {units} units; this runtime reports {plane}"
)))
.enforce();
return true;
}
false
}
#[derive(Debug)]
pub enum ExecutionOutcome {
Executed,
CompileError(String),
}
#[derive(Debug)]
pub enum ValidationResult {
Pass,
Fail(String),
Error(String),
Skipped(String),
}
#[derive(Debug)]
pub enum TestOutcome {
Validated(ValidationResult),
CompileError(String),
}
impl TestOutcome {
#[track_caller]
pub fn enforce(self) {
decide(self).enforce();
}
}
#[derive(Debug)]
pub enum TestDecision {
Accept,
Reject(String),
}
impl TestDecision {
#[track_caller]
pub fn enforce(self) {
match self {
TestDecision::Accept => {}
TestDecision::Reject(reason) => panic!("Test failed: {}", reason),
}
}
}
impl ValidationResult {
pub fn as_test_outcome(self) -> TestOutcome {
TestOutcome::Validated(self)
}
}
impl<E: Display> From<Result<(), E>> for ExecutionOutcome {
fn from(result: Result<(), E>) -> Self {
match result {
Ok(_) => ExecutionOutcome::Executed,
Err(err) => ExecutionOutcome::CompileError(format!("Test did not run: {}", err)),
}
}
}