385 lines
13 KiB
Rust
385 lines
13 KiB
Rust
//! Native box/mode/state -> hpack/vpack, independently compared with INITEX.
|
|
#[path = "support/font_world.rs"]
|
|
mod font_world;
|
|
use font_world::FontFiles;
|
|
use std::{fs, process::Command};
|
|
use texrra_tex_core::execution::*;
|
|
const SETUP: &str = r"\catcode123=1 \catcode125=2 \catcode35=6 \font\p=cmr10 \p \hsize=100pt \baselineskip=12pt \parfillskip=0pt plus1fil \boxmaxdepth=100pt \hbadness=10000 \vbadness=10000 ";
|
|
fn run(body: &str) -> Execution {
|
|
Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 1_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "boxes.tex".into(),
|
|
data: (format!("{SETUP}{body}\\end")).as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap()
|
|
}
|
|
fn messages(e: &Execution) -> Vec<String> {
|
|
e.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::Message { text, .. } = e {
|
|
Some(text.clone())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn transferred_nodes_can_be_repacked_without_spending_the_live_box_capacity() {
|
|
// Match the captured INITEX shape of the CVPR box-capacity reproducer.
|
|
let material = r"\kern1sp".repeat(500);
|
|
let source = format!(
|
|
r"\catcode123=1 \catcode125=2
|
|
\setbox2=\vbox{{\hrule width2pt height2pt {material}}}
|
|
\def\repack{{\ifnum\count0<2050 \advance\count0 by1
|
|
\setbox2=\vbox{{\unvbox2}}\expandafter\repack\fi}}
|
|
\repack
|
|
\message{{REPACKED=\number\count0|BOX=\number\wd2,\number\ht2,\number\dp2}}
|
|
\shipout\box2 \end"
|
|
);
|
|
let execution = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 2_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.with_native_pages()
|
|
.run(
|
|
SourceFile {
|
|
name: "repack.tex".into(),
|
|
data: source.into_bytes(),
|
|
},
|
|
&mut NoFiles,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(messages(&execution), ["REPACKED=2050|BOX=131072,131572,0"]);
|
|
let pages: Vec<_> = execution
|
|
.events
|
|
.iter()
|
|
.filter_map(|event| match event {
|
|
Event::Shipout(page) => Some(page),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(pages.len(), 1);
|
|
assert_eq!(pages[0].box_node.node_count(), 502);
|
|
assert!(execution.state.box_register(2).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn box_capacity_checks_live_copies_retained_pages_and_single_boxes() {
|
|
let material = r"\kern1sp".repeat(40);
|
|
let run = |body: &str| {
|
|
Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
tokens: 64,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.with_native_pages()
|
|
.run_detailed(
|
|
SourceFile {
|
|
name: "box-capacity.tex".into(),
|
|
data: format!(r"\catcode123=1 \catcode125=2 {body}\end").into_bytes(),
|
|
},
|
|
&mut NoFiles,
|
|
)
|
|
};
|
|
let over = [
|
|
(format!(r"\setbox0=\vbox{{{material}{material}}}"), 0),
|
|
(format!(r"\setbox0=\vbox{{{material}}}\setbox1=\copy0"), 0),
|
|
(
|
|
format!(r"\shipout\vbox{{{material}}}\shipout\vbox{{{material}}}"),
|
|
1,
|
|
),
|
|
];
|
|
for (body, retained) in over {
|
|
let failed = run(&body).unwrap_err();
|
|
assert!(
|
|
failed
|
|
.error
|
|
.message
|
|
.contains("native box node capacity exceeded"),
|
|
"{}",
|
|
failed.error
|
|
);
|
|
let published = failed
|
|
.evidence
|
|
.events
|
|
.iter()
|
|
.filter(|event| matches!(event, Event::Shipout(_)))
|
|
.count();
|
|
assert_eq!(published, retained);
|
|
}
|
|
let body = format!(r"\setbox0=\vbox{{{material}}}\setbox0=\vbox{{{material}}}");
|
|
let final_box = run(&body).unwrap().state.box_register(0).unwrap().clone();
|
|
assert_eq!(final_box.node_count(), 41);
|
|
}
|
|
|
|
#[test]
|
|
fn box_registers_scopes_moves_and_dimensions_preserve_native_contents() {
|
|
let e = run(
|
|
r"\setbox0=\vbox{\hbox{Ag}\kern3pt\hbox{bp}}{\setbox0=\hbox{X}\setbox1=\copy0\relax\ifhbox1\message{H}\fi}\ifvoid1\message{VOID}\fi\ifvbox0\message{V}\fi\wd0=40pt{\ht0=20pt}\message{\the\wd0/\the\ht0}\setbox2=\box0\relax\ifvoid0\message{MOVED}\fi\copy2",
|
|
);
|
|
assert_eq!(messages(&e), ["H", "VOID", "V", "40.0pt/20.0pt", "MOVED"]);
|
|
let b = e.state.box_register(2).unwrap();
|
|
assert_eq!(b.width, 40 * 65536);
|
|
assert_eq!(b.height, 20 * 65536);
|
|
assert!(e.state.box_register(0).is_none());
|
|
let copy: Execution = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
|
|
assert_eq!(copy, e);
|
|
assert!(e.reads.contains(&Dependency::BoxRegister(2)));
|
|
assert!(!e.document().unwrap().units.is_empty());
|
|
}
|
|
#[test]
|
|
fn box_modes_group_types_prevdepth_and_restoration_are_real() {
|
|
let e = run(
|
|
r"\message{\the\prevdepth}\setbox0=\vbox{\ifinner\message{INNER-V}\fi\message{\the\currentgrouptype}\prevdepth=2pt\hbox{A}\message{\the\prevdepth}\vskip4pt\message{\the\prevdepth}\hrule\message{\the\prevdepth}\setbox1=\hbox{\ifinner\message{INNER-H}\fi\message{\the\currentgrouptype}X}\noindent A\ifinner\message{BAD}\fi\par}\message{\the\prevdepth/\the\currentgrouptype}",
|
|
);
|
|
assert_eq!(
|
|
messages(&e),
|
|
[
|
|
"-1000.0pt",
|
|
"INNER-V",
|
|
"4",
|
|
"0.0pt",
|
|
"0.0pt",
|
|
"-1000.0pt",
|
|
"INNER-H",
|
|
"3",
|
|
"-1000.0pt/0"
|
|
]
|
|
);
|
|
assert!(e.state.box_register(1).is_none());
|
|
}
|
|
#[test]
|
|
fn unfinished_boxes_wrong_modes_and_unsupported_material_fail() {
|
|
for text in [
|
|
r"\vbox{",
|
|
r"\vbox{\end}",
|
|
r"\hbox{\vskip1pt}",
|
|
r"A\prevdepth=1pt",
|
|
r"\vbox{\write0{bad}}",
|
|
r"\vbox{\endgroup}",
|
|
] {
|
|
let result = Executor::default().run(
|
|
SourceFile {
|
|
name: "bad.tex".into(),
|
|
data: text.as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
);
|
|
assert!(result.is_err(), "{text}");
|
|
}
|
|
let text = format!("{}{}", r"\vbox{".repeat(40), "}".repeat(40));
|
|
assert!(
|
|
Executor::default()
|
|
.run(
|
|
SourceFile {
|
|
name: "depth.tex".into(),
|
|
data: text.into_bytes(),
|
|
},
|
|
&mut FontFiles
|
|
)
|
|
.unwrap_err()
|
|
.message
|
|
.contains("capacity")
|
|
);
|
|
}
|
|
#[test]
|
|
fn native_packing_dimensions_badness_and_diagnostics_match_actual_initex() {
|
|
let dir = std::env::temp_dir().join(format!("texrra-box-oracle-{}", std::process::id()));
|
|
fs::create_dir_all(&dir).unwrap();
|
|
#[derive(Default)]
|
|
struct Host(Vec<String>);
|
|
impl SourceProvider for Host {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
|
|
Err("no files".into())
|
|
}
|
|
fn observe_message(&mut self, text: &str) -> Result<(), String> {
|
|
self.0.push(text.into());
|
|
Ok(())
|
|
}
|
|
}
|
|
let mut cases = Vec::new();
|
|
// Formerly rejected as unimplemented; retain the same legal box body in
|
|
// the real INITEX success oracle now that signed flexibility is supported.
|
|
cases.push(r"\setbox0=\vbox{\vskip0pt plus-1pt}".into());
|
|
for kind in ["vbox", "vtop"] {
|
|
for spec in ["", "to0pt", "to10pt", "to30pt", "spread5pt", "spread-3pt"] {
|
|
for material in [
|
|
r"",
|
|
r"\kern3pt",
|
|
r"\hbox{Ag}",
|
|
r"\hbox{Ag}\kern2pt\hbox{bp}",
|
|
r"\hbox{A}\vskip2pt plus3pt minus1pt\hbox{gp}",
|
|
r"\vskip1pt plus1fil\hbox{Ag}",
|
|
r"\hbox{A}\vss\hbox{gp}",
|
|
r"\hbox{A}\vskip0pt plus1fill\hbox{gp}",
|
|
r"\hbox{Ag}\hrule width12pt height2pt depth1pt\hbox{bp}",
|
|
r"\vbox{\hbox{Ag}\kern2pt}\hbox{bp}",
|
|
] {
|
|
cases.push(format!(r"\setbox0=\{kind} {spec}{{{material}}}"));
|
|
}
|
|
}
|
|
}
|
|
for spec in ["", "to0pt", "to10pt", "to50pt", "spread5pt", "spread-3pt"] {
|
|
for material in [
|
|
"",
|
|
r"Ag bp",
|
|
r"Ag\hskip2pt plus3pt minus1pt bp",
|
|
r"Ag\hskip1pt plus1fil bp",
|
|
r"A\vbox{\hbox{g}\kern2pt}B",
|
|
] {
|
|
cases.push(format!(r"\setbox0=\hbox {spec}{{{material}}}"));
|
|
}
|
|
}
|
|
for max in [0, 1, 2, -1] {
|
|
cases.push(format!(
|
|
r"\boxmaxdepth={max}pt\setbox0=\vbox{{\hbox{{gp}}}}"
|
|
));
|
|
}
|
|
for threshold in [0, 99, 100, 10000] {
|
|
for fuzz in [0, 1, 100] {
|
|
cases.push(format!(
|
|
r"\vbadness={threshold}\vfuzz={fuzz}pt\setbox0=\vbox to5pt{{\kern10pt}}"
|
|
));
|
|
cases.push(format!(
|
|
r"\vbadness={threshold}\vfuzz={fuzz}pt\setbox0=\vbox to20pt{{\vskip10pt plus5pt}}"
|
|
));
|
|
}
|
|
}
|
|
cases.extend(
|
|
[
|
|
r"\vbadness=10000\setbox0=\vbox to20pt{\vbadness=0\vskip10pt plus5pt}",
|
|
r"\vbadness=0\setbox0=\vbox to20pt{\vbadness=10000\vskip10pt plus5pt}",
|
|
r"\setbox0=\vbox{\boxmaxdepth=0pt\hbox{gp}}",
|
|
r"\setbox0=\vbox{Alpha beta gamma delta epsilon zeta eta theta iota kappa lambda.\par}",
|
|
r"\setbox0=\vbox{\hbox{Ag}\prevdepth=-1000pt\hbox{bp}}",
|
|
r"\setbox0=\vbox{\hbox{Ag}\prevdepth=2pt\hbox{bp}}",
|
|
]
|
|
.into_iter()
|
|
.map(str::to_owned),
|
|
);
|
|
for body in [
|
|
r"\vbadness=-1\setbox0=\vbox{}",
|
|
r"\vbadness=-1\setbox0=\vbox to20pt{}",
|
|
r"\vbadness=-1\setbox0=\vbox to20pt{\vfil}",
|
|
r"\vbadness=-1\setbox0=\vbox{\kern2pt}",
|
|
r"\hbadness=-1\setbox0=\hbox to20pt{}",
|
|
r"\hbadness=-1\setbox0=\hbox spread2pt{Ag\hskip0pt plus1fil}",
|
|
r"\hbadness=-1\setbox0=\vbox{Alpha beta gamma delta epsilon zeta eta theta.\par}",
|
|
r"\setbox0=\vbox{\parskip=4pt\noindent A\par\noindent\par B\par}",
|
|
] {
|
|
cases.push(body.into());
|
|
}
|
|
cases.extend(
|
|
[
|
|
r"\setbox0=\hbox{Ag}{\setbox0=\hbox{X}}",
|
|
r"\setbox0=\hbox{Ag}{\global\setbox0=\vbox{\hbox{X}}}",
|
|
r"\setbox0=\hbox{Ag}{\wd0=40pt}",
|
|
r"\setbox0=\hbox{Ag}{\ht0=20pt\dp0=4pt}",
|
|
r"\setbox0=\hbox{Ag}{\setbox1=\box0\relax}",
|
|
r"\setbox0=\hbox{A}{\setbox0=\hbox{B}\setbox1=\box0\relax}",
|
|
r"\globaldefs=1{\setbox0=\hbox{A}{\setbox0=\hbox{B}}}",
|
|
r"\globaldefs=1{\setbox0=\hbox{\globaldefs=-1 A}}",
|
|
r"\setbox32767=\vbox{\hbox{Ag}}\setbox0=\copy32767\relax",
|
|
r"\wd0=10pt\ht0=20pt\dp0=4pt",
|
|
]
|
|
.into_iter()
|
|
.map(str::to_owned),
|
|
);
|
|
cases.extend([
|
|
r"\setbox0=\vbox\relax{\hbox{Ag}}",
|
|
r"\let\bgroup={\let\egroup=}\setbox0=\vbox\bgroup\hbox{Ag}\egroup",
|
|
r"\let\bgroup={\let\egroup=}\def\open{\ifnum\currentgrouptype=4\bgroup\else\errmessage{wrong group}\fi}\setbox0=\vbox\relax\open\hbox{Ag}\egroup",
|
|
].into_iter().map(str::to_owned));
|
|
for (i, body) in cases.iter().enumerate() {
|
|
let common = format!(
|
|
"{SETUP}{body}\\message{{PACK=\\number\\wd0,\\number\\ht0,\\number\\dp0,\\the\\badness;}}\\end\n"
|
|
);
|
|
let mut host = Host::default();
|
|
let e = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 1_000_000,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "oracle.tex".into(),
|
|
data: common.as_bytes().to_vec(),
|
|
},
|
|
&mut host,
|
|
)
|
|
.unwrap_or_else(|e| panic!("case {i}: {body}: {e}"));
|
|
fs::write(dir.join("probe.tex"), common).unwrap();
|
|
let output = match Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(&dir)
|
|
.output()
|
|
{
|
|
Ok(o) => o,
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
eprintln!("pdftex absent: box oracle skipped");
|
|
fs::remove_dir_all(dir).unwrap();
|
|
return;
|
|
}
|
|
Err(e) => panic!("{e}"),
|
|
};
|
|
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
|
|
assert!(output.status.success(), "case {i}: {body}\n{log}");
|
|
let expected = format!(
|
|
"PACK={};",
|
|
log.split("PACK=")
|
|
.nth(1)
|
|
.unwrap()
|
|
.split(';')
|
|
.next()
|
|
.unwrap()
|
|
.replace('\n', "")
|
|
);
|
|
assert_eq!(
|
|
messages(&e).last().unwrap(),
|
|
&expected,
|
|
"case {i}: {body}\n{log}"
|
|
);
|
|
for label in ["Underfull", "Loose", "Tight", "Overfull"] {
|
|
let native = host
|
|
.0
|
|
.iter()
|
|
.filter(|s| s.starts_with(&format!("\n{label} \\")))
|
|
.count();
|
|
let reference = log.matches(&format!("{label} \\")).count();
|
|
assert_eq!(
|
|
native, reference,
|
|
"diagnostics case {i}: {body}\n{log}\n{:?}",
|
|
host.0
|
|
);
|
|
}
|
|
}
|
|
eprintln!("{} native box hpack/vpack INITEX cases passed", cases.len());
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|