Files
texrra-engine/crates/texrra-tex-core/tests/execution_glue_components.rs
T

200 lines
6.4 KiB
Rust

//! Real glue-component scanners versus e-TeX, including retained zero orders.
#[path = "support/font_world.rs"]
mod font_world;
use std::{
fs,
process::Command,
sync::atomic::{AtomicU64, Ordering},
};
use texrra_tex_core::execution::*;
const FONTS: &str = include_str!("../../../examples/native-bootstrap/math-fonts.tex");
struct Host;
impl SourceProvider for Host {
font_world::font_resources!();
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
Err(name.into())
}
fn diagnostic(&mut self, _: WriteDestination, _: &str) -> Result<(), String> {
Ok(())
}
}
fn source(body: &str) -> String {
format!("{FONTS}\\newlinechar=-1\n{body}\\end\n")
}
fn native(text: &str) -> Result<Execution, Error> {
Executor::new(
State::initex().with_etex(),
Limits {
steps: 2_000_000,
..Limits::default()
},
)
.run(
SourceFile {
name: "probe.tex".into(),
data: text.as_bytes().to_vec(),
},
&mut Host,
)
}
fn reference(text: &str) -> (bool, String) {
static ID: AtomicU64 = AtomicU64::new(0);
let dir = std::env::temp_dir().join(format!(
"texrra-glue-components-{}-{}",
std::process::id(),
ID.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&dir).unwrap();
fs::write(dir.join("probe.tex"), text).unwrap();
let result = Command::new("pdftex")
.args([
"-ini",
"-etex",
"-interaction=nonstopmode",
"-halt-on-error",
"probe.tex",
])
.current_dir(&dir)
.output()
.unwrap();
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
fs::remove_dir_all(dir).unwrap();
(result.status.success(), log)
}
fn markers(text: &str) -> Vec<String> {
let mut rest = text;
let mut out = Vec::new();
while let Some(start) = rest.find("GLUE") {
rest = &rest[start..];
let end = rest.find(';').unwrap() + 1;
out.push(rest[..end].into());
rest = &rest[end..];
}
out
}
fn compare(body: &str) {
let text = source(body);
let execution = native(&text).unwrap_or_else(|e| panic!("{e:?}"));
let (ok, log) = reference(&text);
assert!(ok, "{log}");
let actual: Vec<_> = execution
.events
.iter()
.flat_map(|e| match e {
Event::Message { text, .. } | Event::Write { text, .. } => markers(text),
_ => Vec::new(),
})
.collect();
assert!(!actual.is_empty());
assert_eq!(actual, markers(&log), "{log}");
}
fn audit(label: &str, value: &str) -> String {
format!(
r"\message{{GLUE{label}=[\the\gluestretch {value}|\the\glueshrink {value}|\the\gluestretchorder {value}|\the\glueshrinkorder {value}];}}"
)
}
#[test]
fn literal_register_parameter_and_expression_components_match_etex() {
let mut body = String::new();
let mut cases = 0;
for stretch in ["pt", "fil", "fill", "filll"] {
for shrink in ["pt", "fil", "fill", "filll"] {
for amount in [-65537, 0, 131073] {
let a = amount as f64 / 65536.0;
let spec = format!("7pt plus{a:.10}{stretch} minus{a:.10}{shrink}");
body.push_str(&audit(&format!("literal{cases}"), &spec));
body.push_str(&format!(r"\skip0={spec}\relax"));
body.push_str(&audit(&format!("register{cases}"), r"\skip0"));
cases += 1;
}
}
}
for (i, spec) in [
"0pt plus0fil minus0filll",
"0pt",
"1sp plus-1fil minus2fill",
]
.iter()
.enumerate()
{
body.push_str(&audit(&format!("zero{i}"), spec));
body.push_str(&format!(r"\skip0={spec}\relax"));
body.push_str(&audit(&format!("trapped{i}"), r"\skip0"));
}
body.push_str(r"\baselineskip=12pt plus2fil minus3fill\relax");
body.push_str(&audit("parameter", r"\baselineskip"));
body.push_str(&audit(
"expression",
r"\glueexpr 4pt plus2fil-1pt plus3fil\relax",
));
compare(&body);
eprintln!("{cases} paired literal/register component cases matched INITEX");
}
#[test]
fn aliases_scalar_coercion_real_box_consumers_and_write_match_etex() {
compare(
r"
\let\stretch=\gluestretch\let\order=\gluestretchorder
\skip0=1pt plus2.5fill minus-3.25fil\relax
\dimen0=\dimexpr\stretch\skip0*3/2\relax
\count0=\numexpr\order\skip0+100\relax
\message{GLUEscalars=[\the\dimen0|\the\count0|\number\glueshrink\skip0];}
\ifdim\gluestretch\skip0=2.5pt\else\errmessage{wrong stretch dimension}\fi
\setbox0=\hbox{A\kern\gluestretch\skip0 B\kern\glueshrink\skip0 C}
\message{GLUEbox=[\the\wd0];}
\immediate\write16{GLUEwrite=[\the\gluestretch\skip0|\the\gluestretchorder\skip0];}
\def\done{\message{GLUEassignment=[done];}}
\afterassignment\done
\message{GLUEpending=[\the\gluestretch\skip0];}\count1=7\relax
",
);
for name in [
"gluestretch",
"glueshrink",
"gluestretchorder",
"glueshrinkorder",
] {
assert_eq!(
State::initex().meaning(&CommandName::Control(name.into())),
Meaning::Undefined
);
assert!(matches!(
State::initex()
.with_etex()
.meaning(&CommandName::Control(name.into())),
Meaning::Primitive(_)
));
}
let state = State::initex().with_etex();
let restored: State = serde_json::from_str(&serde_json::to_string(&state).unwrap()).unwrap();
assert_eq!(state.fingerprint(), restored.fingerprint());
}
#[test]
fn incompatible_units_and_nested_scanner_capacity_are_strict() {
for primitive in [
"gluestretch",
"glueshrink",
"gluestretchorder",
"glueshrinkorder",
] {
let text = source(&format!(
r"\muskip0=2mu plus3fil\relax\message{{GLUEbad=[\the\{primitive}\muskip0];}}"
));
let e = native(&text)
.err()
.expect("mu glue must not be silently converted");
assert!(e.message.contains("mu units"), "{e}");
let (ok, log) = reference(&text);
assert!(!ok && log.contains("Incompatible glue units"), "{log}");
}
let text = source(&format!(
r"\message{{GLUEdepth=[\the {}0pt];}}",
r"\gluestretch ".repeat(64)
));
let e = native(&text)
.err()
.expect("nested value scans must be bounded");
assert!(e.message.contains("nesting limit"), "{e}");
}