Refactor Examples to use assert_eq

This commit updates all example files to use `assert_eq!` for verifying
output, replacing the previous `Output:` comments. This change makes the
examples more robust and self-testing.

The benchmark results in some examples have also been slightly adjusted,
reflecting minor performance variations after the refactoring.

Additionally, a runtime panic handling mechanism has been introduced in
the VM for function calls, which improves error reporting for unexpected
panics.
This commit is contained in:
2026-03-26 15:07:48 +01:00
parent 0088a644eb
commit 16af3ae9fc
40 changed files with 313 additions and 235 deletions
+15 -38
View File
@@ -25,53 +25,30 @@ pub fn run_functional_tests() -> Vec<TestResult> {
pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult> {
let mut results = Vec::new();
let entries = fs::read_dir("examples").unwrap();
let output_re = Regex::new(r";; Output: (.*)").unwrap();
for entry in entries.filter_map(|e| e.ok()) {
let mut env = Environment::new(); // Fresh environment per test file
let mut env = Environment::new();
env.optimization = enabled;
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "myc") {
let content = fs::read_to_string(&path).unwrap();
let name = path.file_name().unwrap().to_string_lossy().to_string();
let expected_output = output_re
.captures(&content)
.map(|m| m.get(1).unwrap().as_str().trim().to_string());
if content.contains(";; Skip:") {
continue;
}
if let Some(expected) = expected_output {
match env.run_script(&content) {
Ok(val) => {
let val_str = format!("{}", val);
if val_str == expected {
results.push(TestResult {
name,
success: true,
message: format!("OK: {}", val_str),
});
} else {
results.push(TestResult {
name,
success: false,
message: format!(
"Opt {}: Expected {}, got {}",
if enabled { "ON" } else { "OFF" },
expected,
val_str
),
});
}
}
Err(e) => results.push(TestResult {
name,
success: false,
message: format!(
"Opt {}: Error: {}",
if enabled { "ON" } else { "OFF" },
e
),
}),
}
match env.run_script(&content) {
Ok(_) => results.push(TestResult {
name,
success: true,
message: "OK".to_string(),
}),
Err(e) => results.push(TestResult {
name,
success: false,
message: format!("Opt {}: {}", if enabled { "ON" } else { "OFF" }, e),
}),
}
}
}