use std::process::Command;
fn ilo() -> Command {
Command::new(env!("CARGO_BIN_EXE_ilo"))
}
#[cfg(feature = "cranelift")]
const ENGINES: &[&str] = &["--vm", "--jit"];
#[cfg(not(feature = "cranelift"))]
const ENGINES: &[&str] = &["--vm"];
fn run_ok(engine: &str, src: &str, entry: &str) -> String {
let out = ilo()
.args([src, engine, entry])
.output()
.expect("failed to run ilo");
assert!(
out.status.success(),
"ilo {engine} {src:?} {entry:?} failed: stderr={}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
#[test]
fn idxof_found_at_start() {
let src = "f>O n;idxof \"hello\" \"hel\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "0", "engine={e}");
}
}
#[test]
fn idxof_found_in_middle() {
let src = "f>O n;idxof \"hello world\" \"world\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "6", "engine={e}");
}
}
#[test]
fn idxof_found_at_end() {
let src = "f>O n;idxof \"abcdef\" \"ef\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "4", "engine={e}");
}
}
#[test]
fn idxof_single_char() {
let src = "f>O n;idxof \"abcde\" \"c\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "2", "engine={e}");
}
}
#[test]
fn idxof_not_found_returns_nil() {
let src = "f>n;?? (idxof \"hello\" \"xyz\") -1";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "-1", "engine={e}");
}
}
#[test]
fn idxof_empty_needle_returns_zero() {
let src = "f>O n;idxof \"hello\" \"\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "0", "engine={e}");
}
}
#[test]
fn idxof_empty_haystack_not_found() {
let src = "f>n;?? (idxof \"\" \"a\") -1";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "-1", "engine={e}");
}
}
#[test]
fn idxof_both_empty_returns_zero() {
let src = "f>O n;idxof \"\" \"\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "0", "engine={e}");
}
}
#[test]
fn idxof_unicode_codepoint_index() {
let src = "f>O n;idxof \"café\" \"é\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "3", "engine={e}");
}
}
#[test]
fn idxof_unicode_multibyte_substring() {
let src = "f>O n;idxof \"日本語\" \"本語\"";
for e in ENGINES {
assert_eq!(run_ok(e, src, "f"), "1", "engine={e}");
}
}