oxedyne/fe2o3/fe2o3_austenite/tests/eval_carry.rs
6.5 KiB, 3 runs
created by r1870400018:68327, 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 | //! The per-page carry-state record of `austenite --eval --timings FILE.json` (speed programme U1). |
| 2 | //! |
| 3 | //! Each case compiles synthetic documents with the built binary and compares the `pages` list of the last |
| 4 | //! fixpoint pass. A record holds, for every page, the fingerprint of the pairs it was made from and one |
| 5 | //! fingerprint for each part of the state it began in. The cases pin that a part of the state the walk is |
| 6 | //! meant to carry does change the record where the documents differ: a footnote that spills onto the next |
| 7 | //! page, and the unplaced lines of a paragraph that splits over a page break. And that a one-letter edit |
| 8 | //! changes nothing after the page it is on, which only holds if source offsets are masked and locations |
| 9 | //! are stable across the edit (`eval::locate`). |
| 10 | |
| 11 | #[allow(dead_code)] |
| 12 | #[path = "eval_oracle/json.rs"] |
| 13 | mod json; |
| 14 | |
| 15 | use json::J; |
| 16 | |
| 17 | use std::path::PathBuf; |
| 18 | use std::process::Command; |
| 19 | |
| 20 | struct Page { |
| 21 | pairs: i64, |
| 22 | input: String, |
| 23 | entry: Vec<(String, String)>, // the entry's fields in the order the record names them |
| 24 | } |
| 25 | |
| 26 | impl Page { |
| 27 | fn field(&self, name: &str) -> &str { |
| 28 | match self.entry.iter().find(|(k, _)| k == name) { |
| 29 | Some((_, v)) => v, |
| 30 | None => panic!("the entry has no field {:?}: {:?}", name, self.entry), |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn case_dir(name: &str) -> PathBuf { |
| 36 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_carry").join(name); |
| 37 | let _ = std::fs::remove_dir_all(&dir); |
| 38 | std::fs::create_dir_all(&dir).expect("the case's directory"); |
| 39 | dir |
| 40 | } |
| 41 | |
| 42 | // The pages of the last pass of one compile of `doc`. |
| 43 | fn pages(case: &str, doc: &str) -> Vec<Page> { |
| 44 | let dir = case_dir(case); |
| 45 | let (main, rec, out) = (dir.join("main.typ"), dir.join("record.json"), dir.join("out")); |
| 46 | std::fs::write(&main, doc).expect("the case's source"); |
| 47 | let run = Command::new(env!("CARGO_BIN_EXE_austenite")) |
| 48 | .args(["--eval", "--timings", rec.to_str().expect("a path"), main.to_str().expect("a path"), out.to_str().expect("a path")]) |
| 49 | .output() |
| 50 | .expect("the built austenite binary"); |
| 51 | assert!(run.status.success(), "the compile succeeds:\n{}", String::from_utf8_lossy(&run.stderr)); |
| 52 | let record = json::parse(&std::fs::read_to_string(&rec).expect("the record")).expect("the record is JSON"); |
| 53 | let last = match record.get("pages") { |
| 54 | Some(J::Arr(passes)) => passes.last().expect("a pass"), |
| 55 | _ => panic!("the record has no pages array"), |
| 56 | }; |
| 57 | let list = match last { |
| 58 | J::Arr(v) => v, |
| 59 | _ => panic!("a pass's pages are not a list"), |
| 60 | }; |
| 61 | list.iter().map(|p| { |
| 62 | let entry = match p.get("entry") { |
| 63 | Some(J::Obj(kv)) => kv.iter().map(|(k, v)| (k.clone(), v.render())).collect(), |
| 64 | _ => panic!("a page has no entry"), |
| 65 | }; |
| 66 | Page { |
| 67 | pairs: p.get("pairs").and_then(|v| v.as_i64()).expect("the pair count"), |
| 68 | input: p.get("input").and_then(|v| v.as_str()).expect("the input fingerprint").to_string(), |
| 69 | entry, |
| 70 | } |
| 71 | }).collect() |
| 72 | } |
| 73 | |
| 74 | const SPILL_HEAD: &str = "#set page(width: 200pt, height: 150pt, margin: 16pt)\n"; |
| 75 | const FILLER: &str = "Filler paragraph text sets some words so the document runs on to more pages than one.\n\n"; |
| 76 | |
| 77 | // A footnote on the first page, with `note` as its text, and enough after it for more pages. |
| 78 | fn footnoted(note: &str) -> String { |
| 79 | format!("{}First page text, with a note.#footnote[{}] More first page text.\n\n{}", SPILL_HEAD, note, FILLER.repeat(8)) |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn a_document_compiled_twice_has_the_same_page_records() { |
| 84 | let doc = footnoted("A short note."); |
| 85 | let (a, b) = (pages("twice_a", &doc), pages("twice_b", &doc)); |
| 86 | assert!(a.len() >= 3, "the document makes several pages: {}", a.len()); |
| 87 | assert_eq!(a.len(), b.len(), "the same page count"); |
| 88 | for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() { |
| 89 | assert_eq!((x.pairs, &x.input, &x.entry), (y.pairs, &y.input, &y.entry), "page {} differs between two compiles", i + 1); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn a_footnote_that_spills_onto_the_next_page_changes_the_entry_there() { |
| 95 | let long = "A long note that goes on and on, with words enough to fill more than the room the first page leaves. ".repeat(12); |
| 96 | let (short, spilling) = (pages("note_short", &footnoted("A short note.")), pages("note_long", &footnoted(&long))); |
| 97 | // The first page begins before any flow is open, so its fields are the empty ones; take the second. |
| 98 | let none = short[1].field("foot_spill").to_string(); |
| 99 | assert!(short.iter().skip(1).all(|p| p.field("foot_spill") == none), "no spill in the short note's document"); |
| 100 | let at = spilling.iter().skip(1).position(|p| p.field("foot_spill") != none); |
| 101 | assert!(at.is_some(), "some page of the long note's document begins with a spilled footnote: {:?}", |
| 102 | spilling.iter().map(|p| p.field("foot_spill")).collect::<Vec<_>>()); |
| 103 | } |
| 104 | |
| 105 | // A paragraph of `words` words, the last of which is `last`, on pages short enough that it splits. |
| 106 | fn splitting(last: &str) -> String { |
| 107 | let body = "alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november ".repeat(14); |
| 108 | format!("#set page(width: 200pt, height: 110pt, margin: 12pt)\n{}{}\n\nAfter.\n", body, last) |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn a_paragraph_split_across_a_page_break_carries_its_unplaced_lines() { |
| 113 | let (a, b) = (pages("split_a", &splitting("oscar")), pages("split_b", &splitting("papap"))); |
| 114 | assert!(a.len() >= 3 && a.len() == b.len(), "the paragraph runs over several pages: {} and {}", a.len(), b.len()); |
| 115 | let at = a.iter().zip(b.iter()).position(|(x, y)| x.field("children") != y.field("children")); |
| 116 | assert!(at.is_some(), "the unplaced lines differ at some page: {:?}", |
| 117 | a.iter().map(|p| p.field("children")).collect::<Vec<_>>()); |
| 118 | assert_eq!(at, Some(1), "the lines left after the first page are the first to differ"); |
| 119 | } |
| 120 | |
| 121 | // Paragraphs of a few lines each, the first of which carries `mark` as a number. |
| 122 | fn paragraphs(mark: u32) -> String { |
| 123 | let mut doc = String::from("#set page(width: 200pt, height: 160pt, margin: 16pt)\n"); |
| 124 | for i in 1..=30u32 { |
| 125 | let n = if i == 1 { mark } else { 11 }; |
| 126 | doc.push_str(&format!( |
| 127 | "Paragraph {} carries the number {} among words that set the paragraph into several lines on the page.\n\n", i, n)); |
| 128 | } |
| 129 | doc |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn a_one_letter_edit_leaves_every_page_after_the_edited_one_equal() { |
| 134 | let (before, after) = (pages("edit_before", ¶graphs(11)), pages("edit_after", ¶graphs(12))); |
| 135 | assert!(before.len() >= 6 && before.len() == after.len(), "several pages, the same count: {} and {}", before.len(), after.len()); |
| 136 | assert!(before[0].input != after[0].input, "the edited page's own input differs"); |
| 137 | for i in 3..before.len() { |
| 138 | assert_eq!( |
| 139 | (before[i].pairs, &before[i].input, &before[i].entry), |
| 140 | (after[i].pairs, &after[i].input, &after[i].entry), |
| 141 | "page {} differs after a one-letter edit on the first page", i + 1); |
| 142 | } |
| 143 | } |