use std::path::PathBuf;
use std::process::Command;
fn fixture(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures")
.join(name)
}
fn compile_c_fixture(name: &str) -> PathBuf {
let source = fixture(&format!("{name}.c"));
let out_dir = std::env::temp_dir().join("general-simulator-cli-test-fixtures");
std::fs::create_dir_all(&out_dir).expect("create fixture output dir");
let lib_path = out_dir.join(format!("lib{name}.so"));
let status = Command::new("cc")
.args(["-shared", "-fPIC", "-O0", "-o"])
.arg(&lib_path)
.arg(&source)
.status()
.expect("run cc to compile fixture");
assert!(status.success(), "cc failed to compile fixture {name}");
lib_path
}
fn write_devices_file(contents: &str) -> PathBuf {
let out_dir = std::env::temp_dir().join("general-simulator-cli-test-fixtures");
std::fs::create_dir_all(&out_dir).expect("create fixture output dir");
let path = out_dir.join(format!(
"devices-sampletime-{}.txt",
std::process::id() as u64 * 1000 + contents.len() as u64
));
std::fs::write(&path, contents).expect("write devices file");
path
}
fn run(args: &[&str]) -> std::process::Output {
Command::new(env!("CARGO_BIN_EXE_general-simulator"))
.args(args)
.output()
.expect("failed to run general-simulator binary")
}
fn run_and_read_last_column(devices: &str, tfinal: &str, dt: &str) -> Vec<(f64, f64)> {
let netlist = write_devices_file("V1 a 0 5\nR1 a 0 1000\n");
let devices_path = write_devices_file(devices);
let output = run(&[
netlist.to_str().unwrap(),
"--devices",
devices_path.to_str().unwrap(),
"--mode",
"transient",
"--tfinal",
tfinal,
"--dt",
dt,
]);
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).unwrap();
stdout
.lines()
.skip(1)
.map(|l| {
let mut cols = l.split(',');
let t: f64 = cols.next().unwrap().parse().unwrap();
let v: f64 = cols.next_back().unwrap().parse().unwrap();
(t, v)
})
.collect()
}
#[test]
fn ts_variable_follows_the_block_own_doubling_schedule() {
let lib = compile_c_fixture("variable_sample_time");
let devices = format!(
"SRC kind=const value=1\n\
CNT kind=cscript lib={} in=SRC ts=variable\n",
lib.display()
);
let rows = run_and_read_last_column(&devices, "20.0", "1.0");
let value_at = |t_target: f64| -> f64 {
rows.iter()
.find(|(t, _)| (*t - t_target).abs() < 1e-9)
.unwrap_or_else(|| panic!("no row at t={t_target}, rows={rows:?}"))
.1
};
assert_eq!(value_at(1.0), 1.0);
assert_eq!(value_at(2.0), 2.0);
assert_eq!(value_at(3.0), 2.0); assert_eq!(value_at(4.0), 3.0);
assert_eq!(value_at(7.0), 3.0); assert_eq!(value_at(8.0), 4.0);
assert_eq!(value_at(15.0), 4.0); assert_eq!(value_at(16.0), 5.0);
assert_eq!(value_at(20.0), 5.0); }
#[test]
fn ts_offset_delays_the_first_hit_without_changing_the_period() {
let lib = compile_c_fixture("call_counter");
let devices = format!(
"SRC kind=const value=0\n\
CNT kind=cscript lib={} in=SRC ts=2.0 to=0.7\n",
lib.display()
);
let rows = run_and_read_last_column(&devices, "5.0", "0.1");
let value_at = |t_target: f64| -> f64 {
rows.iter()
.find(|(t, _)| (*t - t_target).abs() < 1e-6)
.unwrap_or_else(|| panic!("no row at t={t_target}, rows={rows:?}"))
.1
};
assert_eq!(value_at(0.6), 0.0); assert_eq!(value_at(0.7), 1.0); assert_eq!(value_at(2.6), 1.0); assert_eq!(value_at(2.7), 2.0); assert_eq!(value_at(4.7), 3.0); }
#[test]
fn ts_offset_omitted_reproduces_the_pre_existing_immediate_first_hit_behavior() {
let lib = compile_c_fixture("call_counter");
let devices = format!(
"SRC kind=const value=0\n\
CNT kind=cscript lib={} in=SRC ts=2.0\n",
lib.display()
);
let rows = run_and_read_last_column(&devices, "5.0", "0.1");
let value_at = |t_target: f64| -> f64 {
rows.iter()
.find(|(t, _)| (*t - t_target).abs() < 1e-6)
.unwrap_or_else(|| panic!("no row at t={t_target}, rows={rows:?}"))
.1
};
assert_eq!(value_at(0.1), 1.0); assert_eq!(value_at(2.0), 1.0); assert_eq!(value_at(2.1), 2.0); }