use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
fn extract_toml_paths(content: &str) -> HashSet<String> {
let mut paths = HashSet::new();
for line in content.lines() {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix("path = \"") {
if let Some(path) = rest.strip_suffix('"') {
paths.insert(path.to_owned());
}
}
}
paths
}
fn extract_inventory_phases(content: &str) -> HashMap<String, String> {
let mut map = HashMap::new();
let mut current_path: Option<String> = None;
let mut current_phase: Option<String> = None;
for line in content.lines() {
let trimmed = line.trim();
if trimmed.starts_with("[[item]]") {
if let (Some(p), Some(ph)) = (current_path.take(), current_phase.take()) {
map.insert(p, ph);
}
} else if let Some(rest) = trimmed.strip_prefix("path = \"") {
if let Some(path) = rest.strip_suffix('"') {
current_path = Some(path.to_owned());
}
} else if let Some(rest) = trimmed.strip_prefix("c8_phase = \"") {
if let Some(phase) = rest.strip_suffix('"') {
current_phase = Some(phase.to_owned());
}
}
}
if let (Some(p), Some(ph)) = (current_path, current_phase) {
map.insert(p, ph);
}
map
}
struct SubPhaseFile {
phase: &'static str,
filename: &'static str,
}
const CONFORMANCE_FILES: &[SubPhaseFile] = &[
SubPhaseFile {
phase: "C8.1",
filename: "conformance_gpu_allocator.rs",
},
SubPhaseFile {
phase: "C8.2",
filename: "conformance_gpu_kernels.rs",
},
SubPhaseFile {
phase: "C8.3",
filename: "conformance_gpu_blas.rs",
},
SubPhaseFile {
phase: "C8.4",
filename: "conformance_gpu_backend.rs",
},
];
#[test]
fn every_public_item_has_a_conformance_reference_or_exclusion() {
let tests_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests"].iter().collect();
let inventory_path = tests_dir
.join("conformance")
.join("_surface_inventory.toml");
let inventory_raw = std::fs::read_to_string(&inventory_path).unwrap_or_else(|e| {
panic!(
"Cannot read surface inventory at {}: {e}",
inventory_path.display()
)
});
let inventory_phases = extract_inventory_phases(&inventory_raw);
let all_items: HashSet<String> = inventory_phases.keys().cloned().collect();
let exclusions_path = tests_dir
.join("conformance")
.join("_surface_exclusions.toml");
let exclusions_raw = std::fs::read_to_string(&exclusions_path).unwrap_or_else(|e| {
panic!(
"Cannot read exclusions file at {}: {e}",
exclusions_path.display()
)
});
let excluded_items = extract_toml_paths(&exclusions_raw);
let mut phase_text: HashMap<&'static str, String> = HashMap::new();
let mut missing_phases: Vec<&'static str> = Vec::new();
for entry in CONFORMANCE_FILES {
let path = tests_dir.join(entry.filename);
match std::fs::read_to_string(&path) {
Ok(text) => {
phase_text.insert(entry.phase, text);
}
Err(_) => {
missing_phases.push(entry.phase);
}
}
}
if !missing_phases.is_empty() {
let mut sorted = missing_phases.clone();
sorted.sort_unstable();
eprintln!(
"coverage_gate: conformance files not yet on disk for phases: {:?}",
sorted
);
eprintln!(" These sub-phases will be reconciled after their dispatch lands.");
}
let combined_text: String = phase_text.values().cloned().collect::<Vec<_>>().join("\n");
let c84_text = phase_text
.get("C8.4")
.expect("conformance_gpu_backend.rs must exist — this is the C8.4 gate file");
let mut uncovered_hard: Vec<String> = Vec::new();
let mut uncovered_soft: Vec<(String, String)> = Vec::new();
for item in &all_items {
if excluded_items.contains(item.as_str()) {
continue;
}
let phase = inventory_phases
.get(item)
.map(|s| s.as_str())
.unwrap_or("unknown");
let leaf = item.rsplit("::").next().unwrap_or(item.as_str());
let in_combined = combined_text.contains(leaf) || combined_text.contains(item.as_str());
match phase {
"C8.4" => {
let referenced = c84_text.contains(leaf) || c84_text.contains(item.as_str());
if !referenced {
uncovered_hard.push(format!("{item} (leaf={leaf})"));
}
}
"C8.1" | "C8.2" | "C8.3" => {
if missing_phases.contains(&phase) {
uncovered_soft.push((item.clone(), format!("{phase} [file absent]")));
} else if !in_combined {
uncovered_soft.push((item.clone(), phase.to_owned()));
}
}
_ => {
if !in_combined {
uncovered_soft.push((item.clone(), "unknown".to_owned()));
}
}
}
}
if !uncovered_soft.is_empty() {
eprintln!(
"coverage_gate: {} item(s) with soft-pending coverage:",
uncovered_soft.len()
);
for (path, phase) in &uncovered_soft {
eprintln!(" [{phase}] {path}");
}
eprintln!(" Items in absent-file phases become hard failures once those files land.");
}
assert!(
uncovered_hard.is_empty(),
"coverage_gate HARD FAIL: {} item(s) lack a conformance reference and are not excluded:\n{}",
uncovered_hard.len(),
uncovered_hard.join("\n")
);
let pending_count = uncovered_soft
.iter()
.filter(|(_, p)| p.contains("file absent"))
.count();
let soft_present_count = uncovered_soft.len() - pending_count;
let covered_count = all_items
.len()
.saturating_sub(excluded_items.len())
.saturating_sub(uncovered_soft.len());
eprintln!(
"coverage_gate: {} total items; {} excluded; {} soft-pending (present files); \
{} pending (files absent); {} hard-covered",
all_items.len(),
excluded_items.len(),
soft_present_count,
pending_count,
covered_count,
);
}
#[test]
fn c8_4_key_names_all_referenced() {
let backend_path: PathBuf = [
env!("CARGO_MANIFEST_DIR"),
"tests",
"conformance_gpu_backend.rs",
]
.iter()
.collect();
let text = std::fs::read_to_string(&backend_path)
.expect("conformance_gpu_backend.rs must exist for this gate to run");
let required = [
"CudaBackendImpl",
"init_cuda_backend",
"get_cuda_device",
"default_device",
"add_f32",
"sub_f32",
"mul_f32",
"div_f32",
"neg_f32",
"relu_f32",
"exp_f32",
"log_f32",
"sqrt_f32",
"pow_f32",
"abs_f32",
"sigmoid_f32",
"tanh_f32",
"add_f64",
"sub_f64",
"mul_f64",
"neg_f64",
"matmul_f32",
"cpu_to_gpu",
"gpu_to_cpu",
"alloc_zeros",
"clone_buffer",
"has_inf_nan_f32",
"raw_device_ptr",
"buffer_elem_size",
"GpuTensor",
"GpuFloat",
"tensor_to_gpu",
"tensor_to_cpu",
"CapturePool",
"CapturedGraph",
"CaptureMode",
"CaptureStatus",
"GraphPoolHandle",
"graph_pool_handle",
"capture_pool_for_handle",
"release_graph_pool_handle",
"num_replays",
"is_uploaded",
"upload",
"PhiloxGenerator",
"PhiloxState",
"CudaRngManager",
"cuda_rng_manager",
"fork_rng",
"join_rng",
"GpuError",
"GpuResult",
"InvalidDevice",
"DeviceMismatch",
"OutOfMemory",
"BudgetExceeded",
"LengthMismatch",
"ShapeMismatch",
"Unsupported",
"InvalidState",
"CudaAllocator",
"CudaBuffer",
];
let missing: Vec<&&str> = required.iter().filter(|s| !text.contains(**s)).collect();
assert!(
missing.is_empty(),
"c8_4 key names missing from conformance_gpu_backend.rs: {missing:?}"
);
}
#[test]
fn surface_inventory_and_exclusions_are_readable() {
let tests_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests"].iter().collect();
let inventory_path = tests_dir
.join("conformance")
.join("_surface_inventory.toml");
let inventory_raw =
std::fs::read_to_string(&inventory_path).expect("_surface_inventory.toml must be readable");
let phases = extract_inventory_phases(&inventory_raw);
assert!(
!phases.is_empty(),
"inventory should contain at least one item (got 0)"
);
let exclusions_path = tests_dir
.join("conformance")
.join("_surface_exclusions.toml");
let exclusions_raw = std::fs::read_to_string(&exclusions_path)
.expect("_surface_exclusions.toml must be readable");
let excluded = extract_toml_paths(&exclusions_raw);
for path in &excluded {
assert!(
phases.contains_key(path.as_str()),
"exclusion '{path}' is not in _surface_inventory.toml — stale exclusion?"
);
}
eprintln!(
"meta: inventory has {} items ({} C8.1, {} C8.2, {} C8.3, {} C8.4); \
exclusions has {} items",
phases.len(),
phases.values().filter(|p| p.as_str() == "C8.1").count(),
phases.values().filter(|p| p.as_str() == "C8.2").count(),
phases.values().filter(|p| p.as_str() == "C8.3").count(),
phases.values().filter(|p| p.as_str() == "C8.4").count(),
excluded.len(),
);
}
#[test]
fn peer_conformance_files_presence_report() {
let tests_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests"].iter().collect();
let peer_files = [
("C8.1", "conformance_gpu_allocator.rs"),
("C8.2", "conformance_gpu_kernels.rs"),
("C8.3", "conformance_gpu_blas.rs"),
];
let mut present = Vec::new();
let mut absent = Vec::new();
for (phase, filename) in &peer_files {
let path = tests_dir.join(filename);
if path.exists() {
present.push(*phase);
} else {
absent.push(*phase);
}
}
if !absent.is_empty() {
eprintln!(
"peer_presence: C8 sub-phases not yet on disk (soft-pending): {:?}",
absent
);
eprintln!(" Gate will enforce hard-fail once these files land.");
}
if !present.is_empty() {
eprintln!("peer_presence: present peer files: {:?}", present);
}
}