Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_session.rs

16.5 KiB, 23 runs

created by r1870400018:67945, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A [`Session`] compiles warm: it starts a compile from the introspector of the one before, keeps its fonts,
2//! and keeps what it keeps even when a compile fails. Warm must end where a cold compile ends, so each case
3//! compares a session's bytes with a fresh session's for the same source, and counts the passes the warm
4//! start saves.
5
6use oxedyne_fe2o3_austenite::caches::{
7 self,
8 Budgets,
9};
10use oxedyne_fe2o3_austenite::compile::{
11 self,
12 Evaluated,
13 Session,
14};
15use oxedyne_fe2o3_austenite::emit::sinks::PdfSink;
16use oxedyne_fe2o3_austenite::flow::text::FontStore;
17use oxedyne_fe2o3_hash::fingerprint::Fingerprint;
18
19use std::path::{
20 Path,
21 PathBuf,
22};
23use std::sync::Arc;
24use std::time::Duration;
25
26// The footer reads the final page count, so a cold compile needs a second pass to learn it.
27const HEAD: &str = "#set page(width: 220pt, height: 130pt, margin: 16pt, numbering: \"1\",\n\
28 footer: context [Page #counter(page).display() of #counter(page).final().first()])\n\
29 #set text(size: 9pt)\n";
30
31const PARA: &str = "A paragraph with enough ordinary words in it to run on to a second line of the page.\n\n";
32
33fn dir(name: &str) -> PathBuf {
34 let d = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_session").join(name);
35 let _ = std::fs::remove_dir_all(&d);
36 std::fs::create_dir_all(&d).expect("the case's directory");
37 std::fs::canonicalize(&d).expect("the case's canonical path")
38}
39
40fn source(paras: usize, tag: &str) -> String {
41 let mut s = String::from(HEAD);
42 for i in 0..paras {
43 s.push_str(&format!("{} {}\n\n", PARA.trim_end(), if i == 0 { tag } else { "" }));
44 }
45 s
46}
47
48// Compiles the file `main` under `session`, giving the PDF, the passes, and whether it started warm.
49fn pdf(session: &mut Session, main: &Path, cold: bool) -> (Vec<u8>, u32, bool) {
50 let mut sink = PdfSink::new().expect("a PDF sink");
51 let root = main.parent().expect("the main's directory");
52 let done: Evaluated = session.compile(main, root, &mut sink, None, None, cold).expect("the main is readable");
53 let passes = match &done.laid {
54 Ok(l) => l.passes,
55 Err(e) => panic!("the compile failed: {}", e.plain()),
56 };
57 let bytes = sink.output().expect("a finished PDF").to_vec();
58 (bytes, passes, done.warm)
59}
60
61fn fresh() -> Session { Session::new(FontStore::default()) }
62
63fn write(path: &Path, text: &str) {
64 std::fs::write(path, text).expect("a project file");
65}
66
67#[test]
68fn a_second_compile_starts_warm_and_settles_in_one_pass_to_the_cold_bytes() {
69 let main = dir("again").join("main.typ");
70 write(&main, &source(9, "q"));
71 let mut s = fresh();
72 let (cold, cold_passes, was_warm) = pdf(&mut s, &main, false);
73 assert!(!was_warm, "the first compile has nothing to start from");
74 assert!(cold_passes >= 2, "the footer needs a second pass cold, took {}", cold_passes);
75 let (warm, warm_passes, was_warm) = pdf(&mut s, &main, false);
76 assert!(was_warm, "the second compile starts from the first's introspector");
77 assert_eq!(warm_passes, 1, "and settles in one pass");
78 assert!(warm == cold, "warm and cold bytes differ");
79}
80
81#[test]
82fn an_edit_compiled_warm_equals_the_same_source_compiled_cold() {
83 let main = dir("edit").join("main.typ");
84 write(&main, &source(9, "q"));
85 let mut s = fresh();
86 let _ = pdf(&mut s, &main, false);
87 // A letter, then enough text to add pages, then back to fewer: the page count the footer reads moves.
88 for (paras, tag) in [(9, "x"), (24, "x"), (24, "z"), (3, "z")] {
89 write(&main, &source(paras, tag));
90 let (warm, _, was_warm) = pdf(&mut s, &main, false);
91 let (cold, _, _) = pdf(&mut fresh(), &main, false);
92 assert!(was_warm, "the compile of {} paragraphs starts warm", paras);
93 assert!(warm == cold, "warm and cold differ at {} paragraphs, tag {}", paras, tag);
94 }
95}
96
97#[test]
98fn a_cold_request_ignores_the_kept_introspector_and_still_keeps_the_new_one() {
99 let main = dir("cold").join("main.typ");
100 write(&main, &source(9, "q"));
101 let mut s = fresh();
102 let (first, first_passes, _) = pdf(&mut s, &main, false);
103 let (again, again_passes, was_warm) = pdf(&mut s, &main, true);
104 assert!(!was_warm, "an explicit output starts cold");
105 assert_eq!(again_passes, first_passes, "and takes the passes a first compile takes");
106 assert!(again == first, "and gives the same bytes");
107 let (_, passes, was_warm) = pdf(&mut s, &main, false);
108 assert!(was_warm && passes == 1, "yet it left its introspector for the compile after it");
109}
110
111#[test]
112fn a_compile_that_lays_no_pages_keeps_the_last_introspector() {
113 let main = dir("broken").join("main.typ");
114 write(&main, &source(9, "q"));
115 let mut s = fresh();
116 let _ = pdf(&mut s, &main, false);
117 let kept = s.intro().expect("pages were laid").clone();
118 // A bracket left open: the source does not evaluate, so no page is laid.
119 write(&main, &format!("{}#[an open bracket\n", source(9, "q")));
120 let mut sink = PdfSink::new().expect("a PDF sink");
121 let root = main.parent().expect("the main's directory");
122 let done = s.compile(&main, root, &mut sink, None, None, false).expect("the main is readable");
123 assert!(done.laid.is_err(), "the broken source must fail");
124 assert!(Arc::ptr_eq(&kept, s.intro().expect("still kept")), "a failed compile leaves the introspector alone");
125 write(&main, &source(9, "x"));
126 let (warm, passes, was_warm) = pdf(&mut s, &main, false);
127 let (cold, _, _) = pdf(&mut fresh(), &main, false);
128 assert!(was_warm && passes == 1, "the compile after the failure starts warm");
129 assert!(warm == cold, "and ends where a cold one ends");
130}
131
132// Numbered sections under an outline, each with a paragraph that reads the heading counter where it stands and
133// refers to the next section. Section 3 holds the same read in a table cell, a list item, a stack child and a
134// block, so that each way of laying a body out gives its content a place. The whole document is the `body` of a
135// template whose heading rule reads the counter in a context that mentions `it.body`, a field and not the
136// template's `body`: the context must not carry the document in its fingerprint, or a letter typed anywhere
137// would move the location of every heading's context. `site` is the one place a word is mistyped ("teh" for
138// "the", the same width); `extra` adds a paragraph to section 1.
139const SITES: usize = 5;
140
141fn numbered(site: Option<usize>, extra: bool) -> String {
142 let w = |k: usize| if site == Some(k) { "teh" } else { "the" };
143 let mut s = String::from("#set page(width: 220pt, height: 600pt, margin: 16pt)\n#set text(size: 9pt)\n\
144 #set heading(numbering: \"1.\")\n\
145 #let wrap(body) = {\n\
146 show heading: it => [#it#context { let t = it.body; counter(heading).display() }]\n\
147 body\n\
148 }\n\
149 #show: wrap\n#outline()\n");
150 for i in 0..6 {
151 s.push_str(&format!("\n= Section {} <s{}>\n\n", i, i));
152 s.push_str(&format!("Paragraph {} says {} here, in section #context counter(heading).display(), and points to @s{}.\n",
153 i, if i == 2 { w(0) } else { "the" }, (i + 1) % 6));
154 if i == 1 && extra {
155 s.push_str("\nAn added paragraph that reads #context counter(heading).display() as well.\n");
156 }
157 if i == 3 {
158 s.push_str(&format!("\n#table(columns: 1, [Cell says {} in section #context counter(heading).display().])\n", w(1)));
159 s.push_str(&format!("\n- Item says {} in section #context counter(heading).display().\n", w(2)));
160 s.push_str(&format!("\n#stack(dir: ttb, [Stack says {} in section #context counter(heading).display().])\n", w(3)));
161 s.push_str(&format!("\n#block(inset: 4pt)[Block says {} in section #context counter(heading).display().]\n", w(4)));
162 }
163 }
164 s
165}
166
167#[test]
168fn a_word_mistyped_in_content_that_reads_a_counter_settles_warm_in_one_pass_to_the_cold_bytes() {
169 let main = dir("mistyped").join("main.typ");
170 write(&main, &numbered(None, false));
171 let mut s = fresh();
172 let (_, first, _) = pdf(&mut s, &main, false);
173 assert!(first >= 2, "the outline and the references need a second pass cold, took {}", first);
174 for site in 0..SITES {
175 for text in [numbered(Some(site), false), numbered(None, false)] {
176 write(&main, &text);
177 let (warm, passes, was_warm) = pdf(&mut s, &main, false);
178 let (cold, _, _) = pdf(&mut fresh(), &main, false);
179 assert!(was_warm, "the edit at site {} starts warm", site);
180 assert_eq!(passes, 1, "the edit at site {} settles in one pass", site);
181 assert!(warm == cold, "warm and cold bytes differ at site {}", site);
182 }
183 }
184}
185
186// A paragraph inserted renumbers the places of the containers after it, so the warm first pass cannot answer
187// for what they hold; it takes more than one pass, and ends where a cold compile ends.
188#[test]
189fn a_paragraph_inserted_settles_warm_in_few_passes_to_the_cold_bytes() {
190 let main = dir("inserted").join("main.typ");
191 write(&main, &numbered(None, false));
192 let mut s = fresh();
193 let _ = pdf(&mut s, &main, false);
194 for text in [numbered(None, true), numbered(None, false)] {
195 write(&main, &text);
196 let (warm, passes, was_warm) = pdf(&mut s, &main, false);
197 let (cold, _, _) = pdf(&mut fresh(), &main, false);
198 assert!(was_warm, "the structural edit starts warm");
199 assert!(passes <= 3, "the structural edit took {} passes", passes);
200 assert!(warm == cold, "warm and cold bytes differ after a structural edit");
201 }
202}
203
204#[test]
205fn a_font_added_on_disc_renews_the_store_and_an_unchanged_one_keeps_it() {
206 let root = dir("fonts");
207 let fonts = root.join("fonts");
208 std::fs::create_dir_all(&fonts).expect("the font directory");
209 write(&fonts.join("one.ttf"), "not a font");
210 let main = root.join("main.typ");
211 write(&main, &source(2, "q"));
212 let mut store = FontStore::default();
213 store.add_dir(fonts.clone());
214 let mut s = Session::new(store);
215 let _ = pdf(&mut s, &main, false);
216 let held = |s: &Session| s.fonts().scanned().iter().filter(|p| p.extension().is_some()).count();
217 assert_eq!(held(&s), 1, "the store has met the one file");
218 let _ = pdf(&mut s, &main, false);
219 assert_eq!(held(&s), 1, "an unchanged directory keeps the store");
220 std::thread::sleep(Duration::from_millis(60));
221 write(&fonts.join("two.ttf"), "not a font either");
222 let _ = pdf(&mut s, &main, false);
223 assert_eq!(held(&s), 2, "a file added beside it renews the store, which then meets both");
224}
225
226// ┌───────────────────────────────────────────────────────────────────────────┐
227// │ THE CONFIGURATION THE CACHES ARE SCOPED BY │
228// └───────────────────────────────────────────────────────────────────────────┘
229
230fn noto() -> Vec<u8> {
231 let at = Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("fe2o3_font").join("fonts").join("NotoSans-Regular.ttf");
232 std::fs::read(at).expect("the Noto Sans file")
233}
234
235fn pair(k: &str, v: &str) -> (String, String) { (k.to_string(), v.to_string()) }
236
237#[test]
238fn the_configuration_is_the_faces_the_inputs_and_the_engine_and_the_order_of_inputs_is_a_change() {
239 let faces = Fingerprint::of(b"faces");
240 let one = [pair("lang", "en"), pair("draft", "yes")];
241 let swapped = [pair("draft", "yes"), pair("lang", "en")];
242 let base = caches::configuration(faces, &one, "abc");
243 assert_eq!(base, caches::configuration(faces, &one, "abc"), "the same configuration is the same fingerprint");
244 // A document reads `sys.inputs` in the order given (`keys()`, a `for` loop), so the order is a change.
245 assert_ne!(base, caches::configuration(faces, &swapped, "abc"), "the order the inputs come in is a change");
246 assert_ne!(base, caches::configuration(faces, &[pair("lang", "en")], "abc"), "an input dropped is a change");
247 assert_ne!(base, caches::configuration(faces, &[pair("lang", "en"), pair("draft", "no")], "abc"),
248 "an input's value changed is a change");
249 assert_ne!(base, caches::configuration(faces, &[pair("lang", "en"), pair("final", "yes")], "abc"),
250 "an input's name changed is a change");
251 assert_ne!(base, caches::configuration(faces, &[], "abc"), "no inputs at all is a change");
252 assert_ne!(base, caches::configuration(Fingerprint::of(b"other faces"), &one, "abc"), "other faces are a change");
253 assert_ne!(base, caches::configuration(faces, &one, "abd"), "another engine is a change");
254 // A key and a value that run together read as another pair.
255 assert_ne!(caches::configuration(faces, &[pair("ab", "c")], "abc"),
256 caches::configuration(faces, &[pair("a", "bc")], "abc"), "where a pair divides is part of it");
257}
258
259#[test]
260fn the_face_set_is_told_by_its_families_and_its_bytes_in_the_order_added() {
261 let mut plain = FontStore::default();
262 let mut again = FontStore::default();
263 let mut more = FontStore::default();
264 more.add_bytes(noto());
265 let a = plain.book().expect("a book").fingerprint();
266 let b = again.book().expect("a book").fingerprint();
267 let c = more.book().expect("a book").fingerprint();
268 assert_eq!(a, b, "two books made alike have one fingerprint");
269 assert_ne!(a, c, "a face added moves it");
270 let mut flipped = noto();
271 let last = flipped.len() - 1;
272 flipped[last] ^= 1;
273 let mut changed = FontStore::default();
274 changed.add_bytes(flipped);
275 let altered = changed.book().expect("a book");
276 assert_eq!(altered.len(), more.book().expect("a book").len(), "the altered file still parses, as a face");
277 assert_ne!(c, altered.fingerprint(), "a byte of the file changed moves it");
278}
279
280#[test]
281fn a_new_face_empties_the_caches_and_the_same_faces_do_not() {
282 let main = dir("config").join("main.typ");
283 write(&main, &source(3, "q"));
284 let mut s = fresh();
285 assert!(s.caches().config().is_none(), "a session that has compiled nothing holds no configuration");
286 let _ = pdf(&mut s, &main, false);
287 let first = s.caches().config().expect("a compile sets the configuration");
288 assert_eq!(s.caches().resets(), 0, "the first compile has nothing to empty");
289 let _ = pdf(&mut s, &main, false);
290 assert_eq!((s.caches().config(), s.caches().resets()), (Some(first), 0), "the same faces leave the caches");
291 // The configuration is the one the parts name, read where the compile read them.
292 let faces = s.fonts().clone().book().expect("a book").fingerprint();
293 assert_eq!(first, caches::configuration(faces, s.inputs(), compile::engine_git_hash()));
294 assert!(s.caches().pars.len() > 0, "the paragraphs were kept under the first faces");
295 let mut store = FontStore::default();
296 store.add_bytes(noto());
297 s.set_fonts(store);
298 let _ = pdf(&mut s, &main, false);
299 assert_ne!(s.caches().config(), Some(first), "a face added moves the configuration");
300 assert_eq!(s.caches().resets(), 1, "and the caches were emptied once for it");
301 let _ = pdf(&mut s, &main, false);
302 assert_eq!(s.caches().resets(), 1, "and not again while the faces stand");
303 // A main with no paragraph under other faces keeps none: without the emptying, what the last compile
304 // touched would outlive one sweep.
305 write(&main, "#set page(width: 220pt, height: 130pt)\n");
306 s.set_fonts(FontStore::default());
307 let _ = pdf(&mut s, &main, false);
308 assert_eq!((s.caches().resets(), s.caches().pars.len()), (2, 0), "a face change left paragraphs in the memo");
309}
310
311#[test]
312fn the_budgets_are_the_hosts_the_shaped_run_cache_takes_its_share_and_the_counters_read_it() {
313 let main = dir("budgets").join("main.typ");
314 write(&main, &source(6, "q"));
315 let mut sink = PdfSink::new().expect("a PDF sink");
316 let root = main.parent().expect("the main's directory");
317 let mut s = fresh();
318 assert_eq!(s.budgets(), Budgets::default());
319 assert_eq!((s.budgets().shapes, s.budgets().pars), (32 << 20, 16 << 20), "the defaults");
320 let done = s.compile(&main, root, &mut sink, None, None, false).expect("the main is readable");
321 assert_eq!(done.counters.shapes.budget, 32 << 20, "the shaped-run cache holds the default share");
322 assert!(done.counters.shapes.misses > 0 && done.counters.shapes.entries > 0, "and the compile shaped into it");
323 assert_eq!(s.caches().counters, done.counters, "the session keeps what the result reports");
324 assert_eq!((done.counters.replayed, done.counters.relaid), (0, 0), "the page counts are zero until the ledger exists");
325 // Five of the six paragraphs are alike, and a paragraph alike in children, settings and width shares the
326 // first one's lines, so a first compile already hits.
327 let c = done.counters;
328 assert!(c.par_misses > 0 && c.par_hits + c.par_misses >= 6, "every paragraph laid is a hit or a miss: {:?}", c);
329 let set = Budgets { shapes: 1 << 20, pars: 2 << 20, ledger: 3 << 20 };
330 s.set_budgets(set);
331 assert_eq!(s.budgets(), set);
332 assert_eq!(s.fonts().clone().book().expect("a book").shape_stats().expect("stats").budget, 1 << 20,
333 "the shaped-run cache takes its budget at once");
334 let mut sink = PdfSink::new().expect("a PDF sink");
335 let done = s.compile(&main, root, &mut sink, None, None, false).expect("the main is readable");
336 assert_eq!(done.counters.shapes.budget, 1 << 20);
337 assert_eq!(s.caches().resets(), 0, "a budget is not part of the configuration");
338}