use super::AnalysisResult;
pub fn generate_fkr_tests(analysis: &AnalysisResult) -> String {
let mut tests = String::new();
tests.push_str(&format!(
r#"//! Auto-generated PTX FKR tests for {}
//! Generated by trueno-ptx-debug
//!
//! Run: cargo test -p trueno-gpu --test ptx_fkr --features "cuda"
#![cfg(feature = "cuda")]
use trueno_gpu::kernels::Kernel;
"#,
analysis.module_name
));
for (id, category, description, result) in &analysis.falsification_report.results {
if result.is_fail() {
let id_lower = id.to_lowercase();
let module_name = analysis.module_name.to_lowercase().replace('-', "_");
tests.push_str(&format!(
r#"
/// FKR test for {id}: {desc}
/// Category: {cat:?}
#[test]
fn fkr_{id_lower}_{module}() {{
// This test verifies the absence of the detected pattern
// Detected issue: {desc}
todo!("Implement FKR test for {id}");
}}
"#,
id = id,
id_lower = id_lower,
desc = description,
cat = category,
module = module_name,
));
}
}
for bug in analysis.bugs.bugs() {
let bug_id = format!("{}", bug.class).to_lowercase().replace(' ', "_");
let module_name = analysis.module_name.to_lowercase().replace('-', "_");
tests.push_str(&format!(
r#"
/// FKR test for bug: {bug_class}
/// Location: {loc}
/// Severity: {sev}
#[test]
fn fkr_bug_{bug_id}_{module}() {{
// This test verifies the issue is resolved
// Issue: {msg}
// Mitigation: {mit}
todo!("Implement FKR test for {bug_class}");
}}
"#,
bug_class = bug.class,
loc = bug.location,
sev = bug.class.severity(),
bug_id = bug_id,
module = module_name,
msg = bug.message,
mit = bug.class.mitigation(),
));
}
tests.push_str(&format!(
r#"
/// Summary test - verifies overall PTX quality
#[test]
fn fkr_summary_{module}() {{
// Falsification Score: {score:.1}/100
// Confidence: {confidence:.1}%
// Critical bugs absent: {critical_absent}
// The kernel should pass with score >= 90
assert!({score:.1} >= 70.0, "Falsification score too low: {score:.1}");
}}
"#,
module = analysis.module_name.to_lowercase().replace('-', "_"),
score = analysis.falsification_score,
confidence = analysis.confidence * 100.0,
critical_absent = analysis.falsification_report.critical_bugs_absent(),
));
tests
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bugs::BugRegistry;
use crate::falsification::FalsificationRegistry;
use crate::parser::Parser;
#[test]
fn test_fkr_generation() {
let ptx = r#"
.version 8.0
.target sm_70
.address_size 64
.entry test() { ret; }
"#;
let mut parser = Parser::new(ptx).expect("parser creation should succeed");
let module = parser.parse().expect("parsing should succeed");
let registry = FalsificationRegistry::new();
let report = registry.evaluate(&module);
let result = AnalysisResult::new("test_kernel", report, BugRegistry::new());
let fkr = generate_fkr_tests(&result);
assert!(fkr.contains("Auto-generated PTX FKR tests"));
assert!(fkr.contains("test_kernel"));
assert!(fkr.contains("fkr_summary_"));
}
}