#![allow(dead_code)]
use std::fs;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use vyre_conform::enforce::category::Category;
use vyre_conform::enforce::CategoryFinding;
use vyre_conform::spec::law::AlgebraicLaw;
use vyre_conform::{types::DataType, types::OpSignature, OpSpec};
pub fn cpu_u32_zero(_input: &[u8]) -> Vec<u8> {
vec![0; 4]
}
pub fn wgsl_noop() -> String {
"fn vyre_op(index: u32, input_len: u32) -> u32 { return 0u; }".to_string()
}
pub fn synthetic_binary_spec(id: &'static str, laws: Vec<AlgebraicLaw>) -> OpSpec {
let mut spec = vyre_conform::specs::primitive::xor::spec();
spec.id = id;
spec.category = Category::A {
composition_of: vec!["primitive.bitwise.xor"],
};
spec.signature = OpSignature {
inputs: vec![DataType::U32, DataType::U32],
output: DataType::U32,
};
spec.cpu_fn = cpu_u32_zero;
spec.wgsl_fn = wgsl_noop;
spec.alt_wgsl_fns = Vec::new();
spec.laws = laws;
spec.equivalence_classes = Vec::new();
spec.boundary_values = Vec::new();
spec
}
pub fn synthetic_unary_spec(id: &'static str, laws: Vec<AlgebraicLaw>) -> OpSpec {
let mut spec = synthetic_binary_spec(id, laws);
spec.signature = OpSignature {
inputs: vec![DataType::U32],
output: DataType::U32,
};
spec
}
pub struct TempDir {
pub path: PathBuf,
}
impl Drop for TempDir {
fn drop(&mut self) {
if let Err(e) = fs::remove_dir_all(&self.path) {
panic!("failed to remove temp dir {}: {}", self.path.display(), e);
}
}
}
impl Deref for TempDir {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.path
}
}
pub fn line_of(source: &str, needle: &str) -> usize {
source
.lines()
.enumerate()
.find(|(_, line)| line.contains(needle))
.map(|(i, _)| i + 1)
.unwrap_or_else(|| panic!("needle {needle:?} not found"))
}
pub fn assert_located(findings: &[CategoryFinding], needle: &str, line: usize) {
let f = findings
.iter()
.find(|f| f.message().contains(needle))
.unwrap_or_else(|| panic!("no finding for {needle:?}; got {findings:?}"));
let location = f.location();
let loc = location
.as_ref()
.unwrap_or_else(|| panic!("finding has no location: {f:?}"));
let delta = if loc.line >= line {
loc.line - line
} else {
line - loc.line
};
assert!(
delta <= 2,
"expected finding around line {line} (±2), got {}: {}",
loc.line,
f.message()
);
}