1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! DVI parity: the same document through `tex` and through texrs.
//!
//! The second axis, and the attainable one. A DVI file is a short uncompressed
//! stream of drawing commands with no fonts inside it and no compression, so
//! byte-identical output is a realistic goal: for `two_words.tex` tex writes
//! 224 bytes and texrs writes 192, where the same document in PDF is tens of
//! kilobytes against hundreds of bytes. Every document that both engines set
//! now agrees on the CHARACTERS and on everything `Dvi::compare` can see; what
//! is left is where each of them lands.
//!
//! The structural comparison is `Dvi::compare`, which `-X dvi` already needed —
//! this adds the ladder and the oracle around it rather than a second way of
//! diffing two DVI files.
use crate::dvi::{Difference, Dvi};
use std::path::Path;
use std::process::Command;
/// A number no other call in this process will use.
///
/// The scratch directory used to be named by process id and case name alone,
/// so two tests running the same document in parallel threads shared one
/// directory and removed it under each other. That failed only when the tests
/// ran together, which is the worst way for a harness to fail.
fn unique_suffix() -> usize {
static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
/// The engine texrs is measured against here: real `tex`, not luatex, because
/// DVI is what tex writes natively.
pub struct Oracle {
pub program: String,
pub version: String,
}
/// `tex`, if it is installed.
pub fn oracle() -> Option<Oracle> {
let out = Command::new("tex").arg("--version").output().ok()?;
if !out.status.success() {
return None;
}
let text = String::from_utf8_lossy(&out.stdout).into_owned();
let version = text
.lines()
.next()
.unwrap_or_default()
.split_whitespace()
.nth(1)
.unwrap_or("unknown")
.to_string();
Some(Oracle {
program: "tex".to_string(),
version,
})
}
/// How far up the ladder two DVI files agree.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Rung {
/// One engine wrote a DVI and the other did not.
None,
/// Both wrote one, and both parse.
Parses,
/// ... with the same number of pages.
Pages,
/// ... setting the same characters.
Text,
/// ... asking for the same fonts, and drawing the same rules and specials:
/// everything `Dvi::compare` can see.
Structure,
/// ... byte for byte. The goal, and a reachable one.
Bytes,
}
impl Rung {
pub fn name(self) -> &'static str {
match self {
Rung::None => "NONE",
Rung::Parses => "PARSES",
Rung::Pages => "PAGES",
Rung::Text => "TEXT",
Rung::Structure => "STRUCTURE",
Rung::Bytes => "BYTES",
}
}
pub fn parse(s: &str) -> Option<Rung> {
Some(match s {
"NONE" => Rung::None,
"PARSES" => Rung::Parses,
"PAGES" => Rung::Pages,
"TEXT" => Rung::Text,
"STRUCTURE" => Rung::Structure,
"BYTES" => Rung::Bytes,
_ => return None,
})
}
}
/// What `tex` writes for `case`.
pub fn reference(oracle: &Oracle, case: &Path) -> Option<Vec<u8>> {
let dir = std::env::temp_dir().join(format!(
"texrs-dviparity-{}-{}-{}",
std::process::id(),
unique_suffix(),
case.file_stem()?.to_string_lossy()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).ok()?;
std::fs::copy(case, dir.join("case.tex")).ok()?;
let _ = Command::new(&oracle.program)
.args(["-interaction=nonstopmode", "case.tex"])
.env("SOURCE_DATE_EPOCH", "0")
.current_dir(&dir)
.output();
let dvi = std::fs::read(dir.join("case.dvi")).ok();
let _ = std::fs::remove_dir_all(&dir);
dvi
}
/// What texrs writes for the same document, in process.
pub fn subject(case: &Path) -> Option<Vec<u8>> {
let src = std::fs::read_to_string(case).ok()?;
let font = crate::typeset::find_font("cmr10")?;
crate::run_dvi(&src, &font, &crate::typeset::Layout::default()).ok()
}
/// How far the two files agree, and a line saying where they stop.
pub fn verdict(reference: Option<&Vec<u8>>, subject: Option<&Vec<u8>>) -> (Rung, String) {
let (r, s) = match (reference, subject) {
(None, None) => return (Rung::Bytes, "neither engine wrote a DVI".to_string()),
(Some(_), None) => return (Rung::None, "texrs wrote no DVI, tex did".to_string()),
(None, Some(_)) => return (Rung::None, "texrs wrote a DVI, tex wrote none".to_string()),
(Some(r), Some(s)) => (r, s),
};
if r == s {
return (Rung::Bytes, String::new());
}
let (Ok(rd), Ok(sd)) = (Dvi::parse(r), Dvi::parse(s)) else {
return (
Rung::None,
"one of the two files does not parse".to_string(),
);
};
// `Dvi::compare` is the reader `-X dvi` already needed. Its findings map
// onto the ladder in order, so the lowest one that fires is where the two
// files stop agreeing.
let differences = rd.compare(&sd);
for d in &differences {
if let Difference::Pages { left, right } = d {
return (Rung::Parses, format!("pages: tex {left}, texrs {right}"));
}
}
for d in &differences {
if let Difference::Text { at, left, right } = d {
return (
Rung::Pages,
format!(
"text differs at {at}: tex {:?}, texrs {:?}",
snippet(left, *at),
snippet(right, *at)
),
);
}
}
if !differences.is_empty() {
return (
Rung::Text,
format!(
"{} structural difference(s): {:?}",
differences.len(),
differences.first()
),
);
}
(
Rung::Structure,
format!("bytes: tex {}, texrs {}", r.len(), s.len()),
)
}
/// A few characters either side of `at`, for a message that fits on a line.
fn snippet(text: &str, at: usize) -> String {
text.chars().skip(at.saturating_sub(8)).take(24).collect()
}
/// How faithfully texrs's reader and writer carry a file they did not write.
///
/// A third axis, and the one that needs no typesetting: parse a DVI real `tex`
/// produced, write it back, and compare. Everything else here asks whether
/// texrs SETS a document as tex does; this asks only whether it can carry
/// tex's own file through unchanged, which is a smaller question with a
/// definite answer.
pub fn roundtrip(dvi: &[u8]) -> Result<Vec<usize>, String> {
let parsed = Dvi::parse(dvi)?;
let out = parsed.rewrite();
if out.len() != dvi.len() {
return Err(format!(
"length changed: {} in, {} out",
dvi.len(),
out.len()
));
}
Ok(dvi
.iter()
.zip(out.iter())
.enumerate()
.filter(|(_, (a, b))| a != b)
.map(|(i, _)| i)
.collect())
}
/// How far a file survives that round trip.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Trip {
/// texrs could not parse what tex wrote.
Unparsed,
/// It parsed and came back at a DIFFERENT length: the writer does not
/// choose the compact operand widths tex chose.
Rewritten,
/// ... at the same length, with bytes changed.
SameLength,
/// ... unchanged. The goal, and it needs no typesetting to reach.
Identical,
}
impl Trip {
pub fn name(self) -> &'static str {
match self {
Trip::Unparsed => "UNPARSED",
Trip::Rewritten => "REWRITTEN",
Trip::SameLength => "SAMELENGTH",
Trip::Identical => "IDENTICAL",
}
}
pub fn parse(s: &str) -> Option<Trip> {
Some(match s {
"UNPARSED" => Trip::Unparsed,
"REWRITTEN" => Trip::Rewritten,
"SAMELENGTH" => Trip::SameLength,
"IDENTICAL" => Trip::Identical,
_ => return None,
})
}
}
/// How far `dvi` survives the round trip, and a line saying what changed.
pub fn trip_verdict(dvi: &[u8]) -> (Trip, String) {
match roundtrip(dvi) {
Ok(d) if d.is_empty() => (Trip::Identical, String::new()),
Ok(d) => (
Trip::SameLength,
format!(
"{} of {} bytes differ, first at {}",
d.len(),
dvi.len(),
d.first().copied().unwrap_or(0)
),
),
Err(e) if e.starts_with("length changed") => (Trip::Rewritten, e),
Err(e) => (Trip::Unparsed, e),
}
}
/// Every `.tex` file in `dir` whose DVI is not `tex`'s byte for byte, each
/// with the rung it reached and why.
pub fn short_of_bytes(oracle: &Oracle, dir: &Path) -> Vec<String> {
let mut cases: Vec<std::path::PathBuf> = std::fs::read_dir(dir)
.map(|d| {
d.filter_map(|e| e.ok().map(|e| e.path()))
.filter(|p| p.extension().is_some_and(|x| x == "tex"))
.collect()
})
.unwrap_or_default();
cases.sort();
let mut short = Vec::new();
for case in cases {
let reference = reference(oracle, &case);
let mine = subject(&case);
let (rung, detail) = verdict(reference.as_ref(), mine.as_ref());
if rung != Rung::Bytes {
short.push(format!("{}: {} ({detail})", case.display(), rung.name()));
}
}
short
}