Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,262 @@
|
||||
//! Native typed quantities, independently checked against real pdfTeX INITEX.
|
||||
#[path = "support/font_world.rs"]
|
||||
mod font_world;
|
||||
use font_world::FontFiles;
|
||||
use std::{collections::BTreeMap, fs, process::Command};
|
||||
use texrra_tex_core::execution::*;
|
||||
|
||||
fn file(text: &str) -> SourceFile {
|
||||
SourceFile {
|
||||
name: "main.tex".into(),
|
||||
data: text.as_bytes().to_vec(),
|
||||
}
|
||||
}
|
||||
fn run(text: &str) -> Execution {
|
||||
Executor::new(State::default().with_etex(), Limits::default())
|
||||
.run(file(text), &mut FontFiles)
|
||||
.unwrap()
|
||||
}
|
||||
fn messages(e: &Execution) -> Vec<String> {
|
||||
e.events
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
Event::Message { text, .. } => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_character_meanings_are_typed_scoped_and_cannot_paint_text() {
|
||||
let e = run(r"\mathchardef\a=32767 \let\b=\a {\mathchardef\a=1}\message{\the\a/\meaning\b}");
|
||||
assert_eq!(
|
||||
e.state.meaning(&CommandName::Control("a".into())),
|
||||
Meaning::MathCharGiven(32767)
|
||||
);
|
||||
assert_eq!(messages(&e), [r#"32767/\mathchar"7FFF"#]);
|
||||
for source in [
|
||||
r"\mathchardef\a=-1",
|
||||
r"\mathchardef\a=32768",
|
||||
r"\mathchardef\a=65 \a",
|
||||
] {
|
||||
assert!(
|
||||
Executor::default()
|
||||
.run(file(source), &mut FontFiles)
|
||||
.is_err(),
|
||||
"{source}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quantity_scopes_aliases_state_reads_and_snapshot_restore() {
|
||||
let e = run(
|
||||
r"\dimendef\d=300 \skipdef\s=301 \muskipdef\m=302 \d=2.5pt \s=1pt plus2fill minus1pt \m=3mu {\d=9pt {\global\s=4pt}\s=7pt}\message{\the\d/\the\s/\the\m}",
|
||||
);
|
||||
assert_eq!(messages(&e), ["2.5pt/4.0pt/3.0mu"]);
|
||||
assert!(e.reads.contains(&Dependency::Register(Register {
|
||||
kind: RegisterKind::Dimen,
|
||||
index: 300
|
||||
})));
|
||||
assert_eq!(
|
||||
e.state.register(Register {
|
||||
kind: RegisterKind::Dimen,
|
||||
index: 300
|
||||
}),
|
||||
RegisterValue::Dimension(163840)
|
||||
);
|
||||
let copy: Execution = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
|
||||
assert_eq!(e, copy);
|
||||
let next = Executor::from_execution(copy, Limits::default())
|
||||
.run(file(r"\advance\d by1pt\message{\the\d}"), &mut FontFiles)
|
||||
.unwrap();
|
||||
assert_eq!(messages(&next), ["3.5pt"]);
|
||||
let e = run(r"{\dimen0=1pt \skip0=1pt \muskip0=1mu}");
|
||||
assert_eq!(e.outgoing_state, run("").outgoing_state);
|
||||
let e = run(r"\globaldefs=-1 {\global\dimen0=1pt}\message{\the\dimen0}");
|
||||
assert_eq!(messages(&e), ["0.0pt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_quantities_overflows_units_and_readonly_positions_fail() {
|
||||
let relative = run(r"\dimen0=1em");
|
||||
assert_eq!(
|
||||
relative
|
||||
.state
|
||||
.register(Register {
|
||||
kind: RegisterKind::Dimen,
|
||||
index: 0
|
||||
})
|
||||
.width(),
|
||||
relative
|
||||
.state
|
||||
.fonts()
|
||||
.font(relative.state.text_font())
|
||||
.unwrap()
|
||||
.quad()
|
||||
);
|
||||
for source in [
|
||||
r"\dimen0=16384pt",
|
||||
r"\dimen0=16383.999999pt",
|
||||
r"\dimen0=1pt \divide\dimen0 by0",
|
||||
r"\skip0=1pt plus16384fil",
|
||||
r"\skip0=1pt plus1fillll",
|
||||
r"\muskip0=1pt",
|
||||
r"\dimen0=1mu",
|
||||
r"\muskip0=1mu \number\muskip0",
|
||||
r"\dimen0=\muskip0",
|
||||
r"\dimen32768=0pt",
|
||||
r"\dimendef\d=32768",
|
||||
r"\inputlineno=7",
|
||||
r"\dimexpr1pt\relax",
|
||||
r"\dimen0=1 .25pt",
|
||||
] {
|
||||
assert!(
|
||||
Executor::new(State::default().with_etex(), Limits::default())
|
||||
.run(file(source), &mut FontFiles)
|
||||
.is_err(),
|
||||
"{source}"
|
||||
);
|
||||
}
|
||||
for source in [
|
||||
format!("\\number{}0", "\\count".repeat(130)),
|
||||
format!("\\number{}0", "\\dimen".repeat(130)),
|
||||
format!("{}\\relax", "\\expandafter\\relax".repeat(130)),
|
||||
] {
|
||||
let error = Executor::default()
|
||||
.run(file(&source), &mut FontFiles)
|
||||
.unwrap_err();
|
||||
assert!(error.message.contains("nesting limit"), "{}", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
struct Files(BTreeMap<String, String>);
|
||||
impl SourceProvider for Files {
|
||||
font_world::font_resources!();
|
||||
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
||||
Ok(SourceFile {
|
||||
name: name.into(),
|
||||
data: (self.0.get(name).ok_or("missing")?.clone())
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
})
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn physical_input_line_tracks_nested_files_not_macro_definition_spans() {
|
||||
let mut files = Files(BTreeMap::from([(
|
||||
"child.tex".into(),
|
||||
"% first\r\n\\message{\\the\\inputlineno}\r\\endinput\n".into(),
|
||||
)]));
|
||||
let e = Executor::default().run(file("\\def\\line{\\the\\inputlineno}\n\\message{\\line}\n\\input child.tex\n\\message{\\line}\n"),&mut files).unwrap();
|
||||
assert_eq!(messages(&e), ["2", "2", "4"]);
|
||||
assert_eq!(e.input_position, Some((0, 4)));
|
||||
assert!(e.reads.contains(&Dependency::InputPosition(1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_units_glue_and_printing_match_actual_pdftex() {
|
||||
let mut cases = Vec::new();
|
||||
for unit in ["pt", "sp", "bp", "in", "pc", "cm", "mm", "dd", "cc"] {
|
||||
for number in [
|
||||
"0",
|
||||
"0.00001",
|
||||
"0.9999999999999999",
|
||||
"1.23456789",
|
||||
"12.75",
|
||||
"-2.5",
|
||||
"+0.5",
|
||||
"1,125",
|
||||
"-0.00002",
|
||||
] {
|
||||
cases.push(format!(
|
||||
r"\dimen0={number}{unit} \report{{\number\dimen0/\the\dimen0}}"
|
||||
));
|
||||
}
|
||||
}
|
||||
for n in [
|
||||
-1073741823,
|
||||
-1000001,
|
||||
-65536,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
32767,
|
||||
32768,
|
||||
65535,
|
||||
65536,
|
||||
1073741823,
|
||||
] {
|
||||
cases.push(format!(r"\dimen0={n}sp \edef\repr{{\the\dimen0}}\dimen1=\repr\relax \ifdim\dimen0=\dimen1\report{{\repr}}\else\errmessage{{roundtrip failure}}\fi"));
|
||||
}
|
||||
for source in [
|
||||
r"\skip0=1pt plus2filll minus3fil \advance\skip0 by2pt plus4fill minus1fil \report{\the\skip0}",
|
||||
r"\skip0=0pt plus1fil \advance\skip0 by0pt plus-1fil \advance\skip0 by0pt plus2pt \report{\the\skip0}",
|
||||
r"\muskip0=-1.25mu plus2fil minus3mu \muskip1=-\muskip0 \multiply\muskip1 by3 \divide\muskip1 by2 \report{\the\muskip1}",
|
||||
r"\dimen0=-5sp \dimen1=1.5\dimen0 \report{\number\dimen1}",
|
||||
r"\count0=-65536 \dimen0=1.125\count0 \report{\the\dimen0}",
|
||||
r"{\let\out=\report\catcode112=12 \dimen0=1pT \out{\the\dimen0}}",
|
||||
r"\dimen0=.pt \dimen1=1.25pt \report{\the\dimen0/\the\dimen1}",
|
||||
r"\skip0=1pt plus2fil L L \report{\the\skip0}",
|
||||
] {
|
||||
cases.push(source.into());
|
||||
}
|
||||
let source = format!(
|
||||
"\\catcode123=1 \\catcode125=2 \\catcode35=6\n\\def\\report#1{{\\message{{Q:#1:END}}}}\n{}\n\\end\n",
|
||||
cases.join("\n")
|
||||
);
|
||||
let dir = std::env::temp_dir().join(format!("texrra-quantity-oracle-{}", std::process::id()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
fs::write(dir.join("probe.tex"), &source).unwrap();
|
||||
let output = match Command::new("pdftex")
|
||||
.args([
|
||||
"-ini",
|
||||
"-etex",
|
||||
"-interaction=nonstopmode",
|
||||
"-halt-on-error",
|
||||
"probe.tex",
|
||||
])
|
||||
.current_dir(&dir)
|
||||
.output()
|
||||
{
|
||||
Ok(output) => output,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
eprintln!("pdftex unavailable: quantity oracle skipped");
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
return;
|
||||
}
|
||||
Err(e) => panic!("{e}"),
|
||||
};
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stdout)
|
||||
);
|
||||
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
|
||||
let expected: Vec<_> = log
|
||||
.split("Q:")
|
||||
.skip(1)
|
||||
.map(|s| format!("Q:{}:END", s.split(":END").next().unwrap()))
|
||||
.collect();
|
||||
let actual = Executor::new(
|
||||
State::initex().with_etex(),
|
||||
Limits {
|
||||
steps: 1_000_000,
|
||||
..Limits::default()
|
||||
},
|
||||
)
|
||||
.run(file(&source), &mut FontFiles)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
messages(&actual),
|
||||
expected,
|
||||
"dimension/glue probe order corresponds to generated cases"
|
||||
);
|
||||
assert_eq!(expected.len(), cases.len());
|
||||
eprintln!(
|
||||
"{} native dimension/glue pdfTeX comparisons passed",
|
||||
cases.len()
|
||||
);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user