oxedyne/fe2o3/fe2o3_austenite/tests/eval_intro.rs
36.7 KiB, 35 runs
created by r1870400018:63596, 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 | //! Introspection and the layout fixpoint (U8) against the host's `typst` 0.15.x, on the synthetic |
| 2 | //! fixtures in `tests/fixtures/eval/intro/`. |
| 3 | //! |
| 4 | //! Until the document layouter can place pages, fixtures run through [`Flat`], a layouter that realises |
| 5 | //! the document level by level and places every element on the page its page breaks give it, streaming |
| 6 | //! each page to the sink as it ends, which is all a query, a counter or a state can see. A line |
| 7 | //! `// intro: needs <parts>` names the units a fixture waits on (`layout`, `boxheight`, |
| 8 | //! `numbering`, `heading`, `figure`, `array`, `par`, `selector`), each probed at run time, so a fixture is checked the moment its |
| 9 | //! parts exist and is reported, not silently passed, until then. A fixture marked `oracle: rejects` must |
| 10 | //! fail with Typst's first error, message and line:column. |
| 11 | //! |
| 12 | //! EVAL_ORACLE_TYPST=~/bin/typst cargo test -p oxedyne_fe2o3_austenite --test eval_intro -- --nocapture |
| 13 | |
| 14 | #[allow(dead_code)] |
| 15 | #[path = "eval_oracle/mod.rs"] |
| 16 | mod harness; |
| 17 | |
| 18 | use harness::austenite::to_json; |
| 19 | use harness::corpus::{ |
| 20 | self, |
| 21 | Expect, |
| 22 | Filter, |
| 23 | Fixture, |
| 24 | }; |
| 25 | use harness::json::{ |
| 26 | self, |
| 27 | J, |
| 28 | }; |
| 29 | use harness::oracle::Oracle; |
| 30 | |
| 31 | use oxedyne_fe2o3_austenite::eval::content::{ |
| 32 | Content, |
| 33 | Elem, |
| 34 | ElemKind, |
| 35 | FieldId, |
| 36 | }; |
| 37 | use oxedyne_fe2o3_austenite::eval::fixpoint::{ |
| 38 | self, |
| 39 | Laid, |
| 40 | Layouter, |
| 41 | PageSink, |
| 42 | PageSource, |
| 43 | }; |
| 44 | use oxedyne_fe2o3_austenite::eval::intro::{ |
| 45 | self, |
| 46 | Builder, |
| 47 | Counter, |
| 48 | CounterKey, |
| 49 | CounterUpdate, |
| 50 | Introspector, |
| 51 | State, |
| 52 | }; |
| 53 | use oxedyne_fe2o3_austenite::eval::lib; |
| 54 | use oxedyne_fe2o3_austenite::eval::locate::Location; |
| 55 | use oxedyne_fe2o3_austenite::eval::realise::{ |
| 56 | realise, |
| 57 | RealiseMode, |
| 58 | Tag, |
| 59 | }; |
| 60 | use oxedyne_fe2o3_austenite::eval::select::Selector; |
| 61 | use oxedyne_fe2o3_austenite::eval::styles::StyleChain; |
| 62 | use oxedyne_fe2o3_austenite::eval::value::{ |
| 63 | Label, |
| 64 | Value, |
| 65 | }; |
| 66 | use oxedyne_fe2o3_austenite::eval::eval::{ |
| 67 | eval_string, |
| 68 | EvalMode, |
| 69 | }; |
| 70 | use oxedyne_fe2o3_austenite::eval::{ |
| 71 | eval_source, |
| 72 | Engine, |
| 73 | World, |
| 74 | }; |
| 75 | use oxedyne_fe2o3_austenite::ir::Sp; |
| 76 | use oxedyne_fe2o3_austenite::ledger::Position; |
| 77 | use oxedyne_fe2o3_austenite::page::{ |
| 78 | Frame, |
| 79 | Page, |
| 80 | PageGeometry, |
| 81 | }; |
| 82 | use oxedyne_fe2o3_austenite::syntax::Span; |
| 83 | |
| 84 | use oxedyne_fe2o3_core::prelude::*; |
| 85 | use oxedyne_fe2o3_hash::fingerprint::Fingerprint; |
| 86 | |
| 87 | use std::path::PathBuf; |
| 88 | use std::sync::Arc; |
| 89 | |
| 90 | /// Realises level by level and records each element's start tag on the page the page breaks before it |
| 91 | /// give it, handing each page to the sink when the next begins. It lays nothing out, so it answers every |
| 92 | /// question but where on the page. |
| 93 | struct Flat; |
| 94 | |
| 95 | /// What [`Flat`] carries down the walk: the page it is on, whether anything but tags has landed there, |
| 96 | /// the page's numbering, read from the first styles the page meets, and the records made so far. |
| 97 | struct Walk { |
| 98 | page: u32, |
| 99 | filled: bool, |
| 100 | numbering: Option<Value>, |
| 101 | records: i32, |
| 102 | } |
| 103 | |
| 104 | impl Layouter for Flat { |
| 105 | fn lay<S: PageSink>( |
| 106 | &mut self, |
| 107 | engine: &mut Engine, |
| 108 | content: &Content, |
| 109 | styles: &StyleChain, |
| 110 | builder: &mut Builder, |
| 111 | sink: &mut S, |
| 112 | ) |
| 113 | -> Outcome<u32> |
| 114 | { |
| 115 | let mut w = Walk { page: 1, filled: false, numbering: None, records: 0 }; |
| 116 | res!(walk(engine, content, styles, RealiseMode::Document, builder, sink, &mut w, 0)); |
| 117 | res!(end_page(engine, builder, sink, &mut w)); |
| 118 | Ok(w.page) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn end_page<S: PageSink>(engine: &mut Engine, builder: &mut Builder, sink: &mut S, w: &mut Walk) -> Outcome<()> { |
| 123 | let geom = PageGeometry::new(Sp::from_pt(595.0), Sp::from_pt(842.0), Sp::from_pt(72.0)); |
| 124 | builder.page(w.page, &w.numbering.clone().unwrap_or(Value::None)); |
| 125 | sink.page(engine, Page::new(w.page, geom, Frame::new())) |
| 126 | } |
| 127 | |
| 128 | /// The page numbering a chain sets, `none` where the page element has no such field yet. |
| 129 | fn numbering(styles: &StyleChain) -> Outcome<Value> { |
| 130 | match ElemKind::Page.field_id("numbering") { |
| 131 | Some(id) => Ok(res!(styles.get(ElemKind::Page, id)).unwrap_or(Value::None)), |
| 132 | None => Ok(Value::None), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #[allow(clippy::too_many_arguments)] |
| 137 | fn walk<S: PageSink>( |
| 138 | engine: &mut Engine, |
| 139 | content: &Content, |
| 140 | styles: &StyleChain, |
| 141 | mode: RealiseMode, |
| 142 | builder: &mut Builder, |
| 143 | sink: &mut S, |
| 144 | w: &mut Walk, |
| 145 | depth: usize, |
| 146 | ) |
| 147 | -> Outcome<()> |
| 148 | { |
| 149 | if depth > 64 { |
| 150 | return Err(err!("Flat layout nested past 64 levels."; Excessive)); |
| 151 | } |
| 152 | let pairs = res!(realise(engine, content, styles, mode)); |
| 153 | for p in pairs { |
| 154 | if w.numbering.is_none() { |
| 155 | w.numbering = Some(res!(numbering(&p.styles))); |
| 156 | } |
| 157 | match &p.tag { |
| 158 | Some(Tag::Start(c)) => { |
| 159 | builder.record(c, Position::new(w.page, Sp::ZERO, Sp(w.records)), None); |
| 160 | w.records += 1; |
| 161 | } |
| 162 | Some(Tag::End(_)) => (), |
| 163 | None => { |
| 164 | let e = match p.content.elem() { |
| 165 | Some(e) => e, |
| 166 | None => continue, |
| 167 | }; |
| 168 | match e.kind { |
| 169 | ElemKind::Pagebreak => { |
| 170 | // A weak break on a page that holds nothing yet collapses, as Typst's does. |
| 171 | let weak = matches!(p.content.field("weak"), Some(Value::Bool(true))); |
| 172 | if weak && !w.filled { |
| 173 | continue; |
| 174 | } |
| 175 | res!(end_page(engine, builder, sink, w)); |
| 176 | w.page += 1; |
| 177 | w.filled = false; |
| 178 | w.numbering = None; |
| 179 | continue; |
| 180 | } |
| 181 | // Values that are not shown, and maths, which only an equation's layout realises. |
| 182 | ElemKind::Metadata | ElemKind::CounterUpdate | ElemKind::StateUpdate |
| 183 | | ElemKind::Equation => continue, |
| 184 | _ => w.filled = true, |
| 185 | } |
| 186 | for (_, v) in &e.fields { |
| 187 | res!(walk_value(engine, v, &p.styles, builder, sink, w, depth + 1)); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | Ok(()) |
| 193 | } |
| 194 | |
| 195 | #[allow(clippy::too_many_arguments)] |
| 196 | fn walk_value<S: PageSink>( |
| 197 | engine: &mut Engine, |
| 198 | v: &Value, |
| 199 | styles: &StyleChain, |
| 200 | builder: &mut Builder, |
| 201 | sink: &mut S, |
| 202 | w: &mut Walk, |
| 203 | depth: usize, |
| 204 | ) |
| 205 | -> Outcome<()> |
| 206 | { |
| 207 | match v { |
| 208 | Value::Content(c) => walk(engine, c, styles, RealiseMode::Flow, builder, sink, w, depth), |
| 209 | Value::Array(a) => { |
| 210 | for x in a.iter() { |
| 211 | res!(walk_value(engine, x, styles, builder, sink, w, depth)); |
| 212 | } |
| 213 | Ok(()) |
| 214 | } |
| 215 | _ => Ok(()), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Counts what the fixpoint hands it and keeps no page: the pages of the pass in progress, the |
| 220 | /// discarded passes, the finishes, and what the finishing pass held. |
| 221 | #[derive(Debug, Default)] |
| 222 | struct Counting { |
| 223 | pages: u32, |
| 224 | discards: u32, |
| 225 | finishes: u32, |
| 226 | final_pages: u32, |
| 227 | final_records: usize, |
| 228 | } |
| 229 | |
| 230 | impl PageSink for Counting { |
| 231 | fn page(&mut self, _engine: &mut Engine, _page: Page) -> Outcome<()> { |
| 232 | self.pages += 1; |
| 233 | Ok(()) |
| 234 | } |
| 235 | |
| 236 | fn discard_pass(&mut self) -> Outcome<()> { |
| 237 | self.discards += 1; |
| 238 | self.pages = 0; |
| 239 | Ok(()) |
| 240 | } |
| 241 | |
| 242 | fn finish(&mut self, _engine: &mut Engine, intro: &Introspector) -> Outcome<()> { |
| 243 | self.finishes += 1; |
| 244 | self.final_pages = self.pages; |
| 245 | self.final_records = intro.len(); |
| 246 | Ok(()) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Capabilities other units supply, probed so a waiting fixture turns on by itself. |
| 251 | |
| 252 | fn has_numbering() -> bool { |
| 253 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 254 | lib::numbering::apply(&mut engine, &Value::str("1.a"), &[1, 2]).is_ok() |
| 255 | } |
| 256 | |
| 257 | fn has_layout(root: &std::path::Path) -> bool { |
| 258 | let mut world = World::new(root.to_path_buf()); |
| 259 | let id = match world.add_source(root.join("__probe.typ"), "x".to_string()) { |
| 260 | Ok(id) => id, |
| 261 | Err(_) => return false, |
| 262 | }; |
| 263 | let mut engine = Engine::new(world); |
| 264 | match eval_source(&mut engine, id) { |
| 265 | Ok(m) => fixpoint::run(&mut engine, &m, &mut Counting::default()).is_ok(), |
| 266 | Err(_) => false, |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | fn has_array() -> bool { |
| 271 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 272 | let v = eval_string(&mut engine, "(1, 2).map(x => x).len() + if type(1) == int { 3 } else { 0 }", EvalMode::Code, |
| 273 | lib::library(), oxedyne_fe2o3_austenite::syntax::Span::detached()); |
| 274 | matches!(v, Ok(Value::Int(5))) |
| 275 | } |
| 276 | |
| 277 | fn has_selector() -> bool { |
| 278 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 279 | let v = eval_string(&mut engine, "selector(<a>)", EvalMode::Code, lib::library(), |
| 280 | oxedyne_fe2o3_austenite::syntax::Span::detached()); |
| 281 | matches!(v, Ok(Value::Selector(_))) |
| 282 | } |
| 283 | |
| 284 | /// Does a document of `src` realise, with the schemas its elements' default shows need? A heading's show |
| 285 | /// reads `text.size` and a figure's `block.breakable`, so the element existing is not enough. |
| 286 | fn realises(src: &str) -> bool { |
| 287 | let root = PathBuf::from("/"); |
| 288 | let mut world = World::new(root.clone()); |
| 289 | let id = match world.add_source(root.join("__probe_realise.typ"), src.to_string()) { |
| 290 | Ok(id) => id, |
| 291 | Err(_) => return false, |
| 292 | }; |
| 293 | let mut engine = Engine::new(world); |
| 294 | match eval_source(&mut engine, id) { |
| 295 | Ok(m) => realise(&mut engine, &m.content, &StyleChain::root(), RealiseMode::Document).is_ok(), |
| 296 | Err(_) => false, |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /// Runs a document of `src` through the fixpoint and returns what its `<probe>`s hold, or `None` when it |
| 301 | /// does not lay out. |
| 302 | fn laid_probes(src: &str) -> Option<Vec<J>> { |
| 303 | let root = PathBuf::from("/"); |
| 304 | let mut world = World::new(root.clone()); |
| 305 | let id = world.add_source(root.join("__probe_laid.typ"), src.to_string()).ok()?; |
| 306 | let mut engine = Engine::new(world); |
| 307 | let m = eval_source(&mut engine, id).ok()?; |
| 308 | let laid = fixpoint::run(&mut engine, &m, &mut Counting::default()).ok()?; |
| 309 | probes(&laid.intro).ok() |
| 310 | } |
| 311 | |
| 312 | /// Does an inline `box(height:)` take its height, as `measure` reports it? |
| 313 | fn has_box_height() -> bool { |
| 314 | match laid_probes("#context [#metadata(measure(box(width: 3pt, height: 4pt))) <probe>]") { |
| 315 | Some(v) => v.iter().any(|j| j.render().contains("4pt")), |
| 316 | None => false, |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | fn has_elem(kind: ElemKind) -> bool { |
| 321 | if kind.fields().is_empty() || kind.field_id("numbering").is_none() { |
| 322 | return false; |
| 323 | } |
| 324 | match kind { |
| 325 | ElemKind::Heading => realises("= A"), |
| 326 | ElemKind::Figure => realises("#figure(rect(), caption: [c])"), |
| 327 | _ => true, |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /// The parts a fixture needs that are not yet here. |
| 332 | fn missing(fx: &Fixture, layout: bool, numbering: bool) -> Vec<&'static str> { |
| 333 | let line = fx.text.lines().find(|l| l.trim_start().starts_with("// intro:")).unwrap_or(""); |
| 334 | let mut out = Vec::new(); |
| 335 | for (word, have, name) in [ |
| 336 | ("layout", layout, "layout (U6a/U6b)"), |
| 337 | ("boxheight", has_box_height(), "inline `box(height:)` (U6b-S2)"), |
| 338 | ("numbering", numbering, "numbering (U3)"), |
| 339 | ("heading", has_elem(ElemKind::Heading), "heading (U5)"), |
| 340 | ("figure", has_elem(ElemKind::Figure), "figure (U5)"), |
| 341 | ("array", has_array(), "array methods (U3)"), |
| 342 | ("par", ElemKind::Par.field_id("body").is_some(), "par (U5)"), |
| 343 | ("selector", has_selector(), "the `selector` global (U0 `library()` calling `select::define`)"), |
| 344 | ] { |
| 345 | if line.contains("needs") && line.contains(word) && !have { |
| 346 | out.push(name); |
| 347 | } |
| 348 | } |
| 349 | out |
| 350 | } |
| 351 | |
| 352 | struct Run { |
| 353 | laid: std::result::Result<Laid, String>, |
| 354 | first: Option<(String, Option<(usize, usize)>)>, |
| 355 | warnings: Vec<String>, |
| 356 | sink: Counting, |
| 357 | } |
| 358 | |
| 359 | fn run(fx: &Fixture, flat: bool) -> Run { |
| 360 | let mut world = World::new(fx.root.clone()); |
| 361 | let mut out = Run { |
| 362 | laid: Err("not run".to_string()), |
| 363 | first: None, |
| 364 | warnings: Vec::new(), |
| 365 | sink: Counting::default(), |
| 366 | }; |
| 367 | let id = match world.load(&fx.path) { |
| 368 | Ok(id) => id, |
| 369 | Err(e) => { |
| 370 | out.laid = Err(fmt!("load: {}", e.plain())); |
| 371 | return out; |
| 372 | } |
| 373 | }; |
| 374 | let mut engine = Engine::new(world); |
| 375 | out.laid = match eval_source(&mut engine, id) { |
| 376 | Ok(m) => { |
| 377 | let r = if flat { |
| 378 | fixpoint::run_with(&mut engine, &m, &mut Flat, &mut out.sink) |
| 379 | } else { |
| 380 | fixpoint::run(&mut engine, &m, &mut out.sink) |
| 381 | }; |
| 382 | r.map_err(|e| e.plain()) |
| 383 | } |
| 384 | Err(e) => Err(fmt!("eval: {}", e.plain())), |
| 385 | }; |
| 386 | out.first = engine.diags.iter().find(|d| d.is_error()).map(|d| { |
| 387 | let pos = engine.world.sources.iter() |
| 388 | .find(|s| s.id == d.span.file && !d.span.is_detached()) |
| 389 | .map(|s| s.line_col(d.span.start)); |
| 390 | (d.message.clone(), pos) |
| 391 | }); |
| 392 | out.warnings = engine.diags.iter().filter(|d| !d.is_error()).map(|d| d.message.clone()).collect(); |
| 393 | out |
| 394 | } |
| 395 | |
| 396 | fn probes(intro: &Introspector) -> std::result::Result<Vec<J>, String> { |
| 397 | let found = match intro.query(&Selector::Label(Label::new("probe"))) { |
| 398 | Ok(f) => f, |
| 399 | Err(e) => return Err(e.plain()), |
| 400 | }; |
| 401 | found.iter().map(|e| match e.field("value") { |
| 402 | Some(v) => Ok(to_json(v)), |
| 403 | None => Err("a <probe> has no `value` field".to_string()), |
| 404 | }).collect() |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn intro_fixtures_match_the_typst_oracle() { |
| 409 | let oracle = match Oracle::find() { |
| 410 | Ok(Some(o)) => o, |
| 411 | Ok(None) => { |
| 412 | println!("EVAL_ORACLE_SKIP=1: the introspection oracle was skipped explicitly"); |
| 413 | return; |
| 414 | } |
| 415 | Err(e) => panic!("{}", e.plain()), |
| 416 | }; |
| 417 | let fixtures = match corpus::discover(&Filter::only("intro")) { |
| 418 | Ok(f) => f, |
| 419 | Err(e) => panic!("{}", e.plain()), |
| 420 | }; |
| 421 | assert!(fixtures.len() >= 15, "the intro corpus shrank to {} fixtures", fixtures.len()); |
| 422 | let root = corpus::fixtures_dir().join("intro"); |
| 423 | let layout = has_layout(&root); |
| 424 | let numbering = has_numbering(); |
| 425 | let (mut passed, mut failed, mut waiting) = (Vec::new(), Vec::new(), Vec::new()); |
| 426 | for fx in &fixtures { |
| 427 | let lacks = missing(fx, layout, numbering); |
| 428 | if !lacks.is_empty() { |
| 429 | waiting.push(fmt!("{}: waits on {}", fx.id(), lacks.join(", "))); |
| 430 | continue; |
| 431 | } |
| 432 | let flat = !layout; |
| 433 | let got = run(fx, flat); |
| 434 | let verdict: std::result::Result<(), Vec<String>> = match fx.expect { |
| 435 | Expect::Rejects => { |
| 436 | let want = match oracle.compiles(fx) { |
| 437 | Ok(Err(msg)) => msg, |
| 438 | Ok(Ok(())) => panic!("{}: typst accepts a fixture marked `oracle: rejects`", fx.id()), |
| 439 | Err(e) => panic!("{}: {}", fx.id(), e.plain()), |
| 440 | }; |
| 441 | let (msg, pos) = harness::split_first_error(&want); |
| 442 | match harness::compare_first_error(&msg, pos, got.first.as_ref()) { |
| 443 | harness::Verdict::Pass => Ok(()), |
| 444 | harness::Verdict::Fail(d) => Err(d), |
| 445 | other => Err(vec![fmt!("{:?}", other)]), |
| 446 | } |
| 447 | } |
| 448 | Expect::Accepts => { |
| 449 | let want = match oracle.probes(fx) { |
| 450 | Ok(Ok(v)) => v, |
| 451 | Ok(Err(e)) => panic!("{}: typst: {}", fx.id(), e), |
| 452 | Err(e) => panic!("{}: {}", fx.id(), e.plain()), |
| 453 | }; |
| 454 | match &got.laid { |
| 455 | Err(e) => Err(vec![fmt!("austenite: {} (first error: {:?})", e, got.first)]), |
| 456 | Ok(laid) => match probes(&laid.intro) { |
| 457 | Err(e) => Err(vec![e]), |
| 458 | Ok(v) => { |
| 459 | let mut d = Vec::new(); |
| 460 | json::diff("probes", &J::Arr(want), &J::Arr(v), &mut d); |
| 461 | // Typst gives up after five passes; so must Austenite, and say so. |
| 462 | let warned = got.warnings.iter().any(|w| w.contains("did not converge")); |
| 463 | if fx.name.starts_with("nonconverge") && (laid.converged || laid.passes != 5 || !warned) { |
| 464 | d.push(fmt!("expected five passes and a warning, got {} pass(es), converged {}", |
| 465 | laid.passes, laid.converged)); |
| 466 | } |
| 467 | if !fx.name.starts_with("nonconverge") && !laid.converged { |
| 468 | d.push(fmt!("did not converge in {} passes", laid.passes)); |
| 469 | } |
| 470 | // Every pass but the last is discarded, and the last is finished once. |
| 471 | let s = &got.sink; |
| 472 | if s.discards + 1 != laid.passes || s.finishes != 1 || s.final_pages != laid.pages |
| 473 | || s.final_records != laid.intro.len() |
| 474 | { |
| 475 | d.push(fmt!("the sink saw {} discard(s), {} finish(es) and {} final page(s) \ |
| 476 | for {} pass(es) of {} page(s)", s.discards, s.finishes, s.final_pages, |
| 477 | laid.passes, laid.pages)); |
| 478 | } |
| 479 | if d.is_empty() { Ok(()) } else { Err(d) } |
| 480 | } |
| 481 | }, |
| 482 | } |
| 483 | } |
| 484 | }; |
| 485 | match verdict { |
| 486 | Ok(()) => passed.push(fx.id()), |
| 487 | Err(d) => failed.push(fmt!("{}:\n {}", fx.id(), d.join("\n "))), |
| 488 | } |
| 489 | } |
| 490 | println!("intro: {} passed, {} failed, {} waiting (layout {}, numbering {})", |
| 491 | passed.len(), failed.len(), waiting.len(), layout, numbering); |
| 492 | for w in &waiting { |
| 493 | println!(" waiting {}", w); |
| 494 | } |
| 495 | for f in &failed { |
| 496 | println!(" FAILED {}", f); |
| 497 | } |
| 498 | assert!(failed.is_empty(), "{} introspection fixture(s) disagree with typst", failed.len()); |
| 499 | assert!(passed.len() >= 12, "only {} fixtures were checked; the rest are waiting", passed.len()); |
| 500 | } |
| 501 | |
| 502 | /// A metadata element with a value, a label and a location, as realisation would prepare it. |
| 503 | fn located(v: i64, label: &str, loc: u64) -> Content { |
| 504 | let mut e = Elem::new(ElemKind::Metadata, vec![(FieldId(0), Value::Int(v))], Span::detached()); |
| 505 | e.label = Some(Label::new(label)); |
| 506 | e.location = Some(Location(loc)); |
| 507 | e.prepared = true; |
| 508 | Content::from_elem(e) |
| 509 | } |
| 510 | |
| 511 | fn at(page: u32, y: i32) -> Position { Position::new(page, Sp::ZERO, Sp(y)) } |
| 512 | |
| 513 | fn values<'a, I: Iterator<Item = &'a Content>>(elems: I) -> Vec<i64> { |
| 514 | elems.filter_map(|e| match e.field("value") { Some(Value::Int(i)) => Some(*i), _ => None }).collect() |
| 515 | } |
| 516 | |
| 517 | fn queried(intro: &Introspector, sel: &Selector) -> Vec<i64> { values(intro.query(sel).unwrap_or_default().iter()) } |
| 518 | |
| 519 | /// Before and after bound by the first match of their bound, inclusively unless told otherwise, and a |
| 520 | /// location the pages never placed counts everything as before it. |
| 521 | #[test] |
| 522 | fn introspector_bounds_and_counts_by_document_order() { |
| 523 | let mut b = Builder::new(); |
| 524 | for (v, l, n) in [(1, "a", 10), (2, "b", 11), (3, "a", 12), (4, "b", 13)] { |
| 525 | b.record(&located(v, l, n), at(1, n as i32), None); |
| 526 | } |
| 527 | let intro = b.finish(); |
| 528 | let a = Selector::Label(Label::new("a")); |
| 529 | let bl = Arc::new(Selector::Label(Label::new("b"))); |
| 530 | let before = Selector::Before { selector: Arc::new(a.clone()), end: bl.clone(), inclusive: true }; |
| 531 | let after = Selector::After { selector: Arc::new(a.clone()), start: bl, inclusive: false }; |
| 532 | assert_eq!(queried(&intro, &before), vec![1]); |
| 533 | assert_eq!(queried(&intro, &after), vec![3]); |
| 534 | assert_eq!(intro.count_before(&a, Location(11)).ok(), Some(1)); |
| 535 | assert_eq!(intro.count_before(&a, Location(12)).ok(), Some(2)); |
| 536 | assert_eq!(intro.count_before(&a, Location(99)).ok(), Some(2)); |
| 537 | assert_eq!(queried(&intro, &Selector::Elem(ElemKind::Metadata, None)), vec![1, 2, 3, 4]); |
| 538 | // A second placement of the same location keeps the first. |
| 539 | let mut twice = Builder::new(); |
| 540 | twice.record(&located(1, "a", 10), at(1, 0), None); |
| 541 | twice.record(&located(9, "a", 10), at(2, 0), None); |
| 542 | let twice = twice.finish(); |
| 543 | assert_eq!(twice.len(), 1); |
| 544 | assert_eq!(twice.page(Location(10)), Some(1)); |
| 545 | } |
| 546 | |
| 547 | /// Out-of-flow material is read where its parent stands: a footnote's entry just after the footnote, a |
| 548 | /// float's body at its mark, and material under a parent nothing marks where it was placed. U6b's |
| 549 | /// `Order` read it this way; the builder now does. |
| 550 | #[test] |
| 551 | fn builder_reads_out_of_flow_material_where_its_parent_stands() { |
| 552 | let mut b = Builder::new(); |
| 553 | b.record(&located(1, "a", 1), at(1, 0), None); // a footnote |
| 554 | b.mark(100); // where a float was written |
| 555 | b.record(&located(2, "b", 2), at(1, 10), None); |
| 556 | b.record(&located(3, "c", 3), at(2, 0), Some(100)); // the float's body, placed a page later |
| 557 | b.record(&located(4, "d", 4), at(1, 90), Some(1)); // the footnote's entry, at the page foot |
| 558 | b.record(&located(5, "e", 5), at(2, 5), Some(999)); // under a parent nothing marks |
| 559 | b.record(&located(6, "f", 6), at(2, 9), Some(4)); // nested: under the entry |
| 560 | let intro = b.finish(); |
| 561 | assert_eq!(values(intro.elems()), vec![1, 4, 6, 3, 2, 5]); |
| 562 | assert_eq!(intro.order(Location(3)), Some(3)); |
| 563 | assert_eq!(intro.position(Location(3)), Some(at(2, 0)), "a record keeps where it was placed"); |
| 564 | } |
| 565 | |
| 566 | /// Page numbering is held once per run of equal numberings; a page given twice counts once. |
| 567 | #[test] |
| 568 | fn builder_holds_page_numbering_as_runs() { |
| 569 | let mut b = Builder::new(); |
| 570 | b.record(&located(1, "a", 1), at(3, 0), None); |
| 571 | b.page(1, &Value::str("1")); |
| 572 | b.page(2, &Value::str("1")); |
| 573 | b.page(3, &Value::str("i")); |
| 574 | b.page(3, &Value::str("x")); |
| 575 | b.page(4, &Value::str("i")); |
| 576 | let intro = b.finish(); |
| 577 | assert_eq!(intro.pages(), 4); |
| 578 | let shown = |p: u32| match intro.numbering_of_page(p) { |
| 579 | Value::Str(s) => s.to_string(), |
| 580 | Value::None => "none".to_string(), |
| 581 | other => fmt!("{:?}", other), |
| 582 | }; |
| 583 | assert_eq!([shown(1), shown(2), shown(3), shown(4), shown(5)], ["1", "1", "i", "i", "none"]); |
| 584 | assert!(matches!(intro.page_numbering(Location(1)), Value::Str(s) if s.as_str() == "i")); |
| 585 | assert_eq!(intro.numbering_runs(), 2, "two runs, not four pages' worth"); |
| 586 | } |
| 587 | |
| 588 | /// The `.prl` ledger comes from the introspector: one `Location` anchor per located element. |
| 589 | #[test] |
| 590 | fn ledger_is_derived_from_the_introspector() { |
| 591 | let mut b = Builder::new(); |
| 592 | b.record(&located(1, "a", 0xbeef), Position::new(2, Sp::from_pt(10.0), Sp::from_pt(20.0)), None); |
| 593 | b.page(1, &Value::None); |
| 594 | b.page(2, &Value::None); |
| 595 | let ledger = b.finish().ledger(); |
| 596 | assert_eq!(ledger.total_pages, 2); |
| 597 | assert_eq!(ledger.get(&Location(0xbeef).anchor()).map(|a| a.pos), |
| 598 | Some(Position::new(2, Sp::from_pt(10.0), Sp::from_pt(20.0)))); |
| 599 | } |
| 600 | |
| 601 | /// A page source that makes pages up to `last`, recording one element on each, as `driver::place_page` |
| 602 | /// records a page's elements when it places the page. |
| 603 | struct Numbered { |
| 604 | next: u32, |
| 605 | last: u32, |
| 606 | } |
| 607 | |
| 608 | impl PageSource for Numbered { |
| 609 | fn next_page(&mut self, _engine: &mut Engine, builder: &mut Builder) -> Outcome<Option<(Page, Value)>> { |
| 610 | if self.next > self.last { |
| 611 | return Ok(None); |
| 612 | } |
| 613 | let n = self.next; |
| 614 | self.next += 1; |
| 615 | builder.record(&located(n as i64, "p", 0x100 + n as u64), at(n, 0), None); |
| 616 | let geom = PageGeometry::new(Sp::from_pt(100.0), Sp::from_pt(100.0), Sp::ZERO); |
| 617 | let numbering = if n < 3 { Value::str("1") } else { Value::str("i") }; |
| 618 | Ok(Some((Page::new(n, geom, Frame::new()), numbering))) |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /// `place` streams: every page reaches the sink as it is made, its numbering reaches the builder, and |
| 623 | /// the count comes back. |
| 624 | #[test] |
| 625 | fn place_streams_each_page_to_the_sink() -> Outcome<()> { |
| 626 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 627 | let mut builder = Builder::new(); |
| 628 | let mut sink = Counting::default(); |
| 629 | let n = res!(fixpoint::place(&mut engine, &mut Numbered { next: 1, last: 4 }, &mut builder, &mut sink)); |
| 630 | assert_eq!(n, 4); |
| 631 | assert_eq!(sink.pages, 4); |
| 632 | let intro = builder.finish(); |
| 633 | assert_eq!(intro.pages(), 4); |
| 634 | assert_eq!(intro.len(), 4); |
| 635 | assert_eq!(intro.page(Location(0x103)), Some(3)); |
| 636 | assert!(matches!(intro.numbering_of_page(2), Value::Str(s) if s.as_str() == "1")); |
| 637 | assert!(matches!(intro.numbering_of_page(4), Value::Str(s) if s.as_str() == "i")); |
| 638 | Ok(()) |
| 639 | } |
| 640 | |
| 641 | /// `measure` lays out under a fresh root locator and puts the document's back: measured content takes no |
| 642 | /// ordinal, so the next element of the document is located as if nothing had been measured, and the |
| 643 | /// fresh root hands a measured element the location the document's first such element has, as Typst's |
| 644 | /// `Locator::root()` does. |
| 645 | #[test] |
| 646 | fn measure_takes_none_of_the_documents_ordinals() -> Outcome<()> { |
| 647 | let fp = Fingerprint::of(b"a heading"); |
| 648 | let mut e = Engine::new(World::new(PathBuf::from("/"))); |
| 649 | let first = e.locator.locate(ElemKind::Heading, fp); |
| 650 | let inside = res!(intro::detached(&mut e, |e| { |
| 651 | let a = e.locator.locate(ElemKind::Heading, fp); |
| 652 | let b = e.locator.locate(ElemKind::Heading, fp); |
| 653 | Ok((a, b)) |
| 654 | })); |
| 655 | let second = e.locator.locate(ElemKind::Heading, fp); |
| 656 | // What a document that measured nothing locates its second heading at. |
| 657 | let mut quiet = Engine::new(World::new(PathBuf::from("/"))); |
| 658 | assert_eq!(quiet.locator.locate(ElemKind::Heading, fp), first); |
| 659 | let want = quiet.locator.locate(ElemKind::Heading, fp); |
| 660 | assert_eq!(second, want, "the second heading of the document took an ordinal the measured layout used"); |
| 661 | assert_eq!(inside.0, first, "a measured element is located under a fresh root"); |
| 662 | assert_ne!(inside.0, inside.1, "ordinals still count inside the measured layout"); |
| 663 | assert_ne!(second, first); |
| 664 | // The locator comes back when the layout fails. |
| 665 | let failed: Outcome<()> = intro::detached(&mut e, |e| { |
| 666 | e.locator.locate(ElemKind::Heading, fp); |
| 667 | Err(err!("a measured layout failed"; Test)) |
| 668 | }); |
| 669 | assert!(failed.is_err()); |
| 670 | let third = e.locator.locate(ElemKind::Heading, fp); |
| 671 | assert_eq!(third, quiet.locator.locate(ElemKind::Heading, fp), "an error left the measured ordinals behind"); |
| 672 | Ok(()) |
| 673 | } |
| 674 | |
| 675 | /// Runs `src` through the fixpoint with [`Flat`] and returns how it ended with the diagnostics it left. |
| 676 | fn flat_run(src: &str) -> Outcome<(std::result::Result<Laid, String>, Vec<(bool, String, Span)>)> { |
| 677 | let root = PathBuf::from("/"); |
| 678 | let mut world = World::new(root.clone()); |
| 679 | let id = res!(world.add_source(root.join("__delay.typ"), src.to_string())); |
| 680 | let mut engine = Engine::new(world); |
| 681 | let module = res!(eval_source(&mut engine, id)); |
| 682 | let laid = fixpoint::run_with(&mut engine, &module, &mut Flat, &mut Counting::default()).map_err(|e| e.plain()); |
| 683 | let diags = engine.diags.iter().map(|d| (d.is_error(), d.message.clone(), d.span)).collect(); |
| 684 | Ok((laid, diags)) |
| 685 | } |
| 686 | |
| 687 | /// A show rule's error is delayed (Typst's `Engine::delay`): a reference above its target fails in the first |
| 688 | /// pass, which has not seen the label, shows as nothing, and the second pass settles it. No error survives. |
| 689 | #[test] |
| 690 | fn a_reference_above_its_target_does_not_fail_the_first_pass() -> Outcome<()> { |
| 691 | let (laid, diags) = res!(flat_run("#set heading(numbering: \"1.\")\nSee @h and @h again.\n= Heading <h>\n")); |
| 692 | let laid = res!(laid.map_err(|e| err!("the compile failed: {}", e; Test))); |
| 693 | assert!(laid.passes >= 2, "the reference settled in {} pass(es)", laid.passes); |
| 694 | assert!(laid.converged); |
| 695 | let errors: Vec<_> = diags.iter().filter(|d| d.0).collect(); |
| 696 | assert!(errors.is_empty(), "errors survived the final pass: {:?}", errors); |
| 697 | Ok(()) |
| 698 | } |
| 699 | |
| 700 | /// A label no element carries stays an error through every pass, and the final one reports it with Typst's |
| 701 | /// message, once for each place that refers to it. |
| 702 | #[test] |
| 703 | fn a_label_no_element_carries_fails_the_final_pass() -> Outcome<()> { |
| 704 | let (laid, diags) = res!(flat_run("See @nowhere and @nowhere.\n")); |
| 705 | assert!(laid.is_err(), "a reference to a label that does not exist compiled"); |
| 706 | let errors: Vec<_> = diags.iter().filter(|d| d.0).collect(); |
| 707 | assert_eq!(errors.len(), 2, "one error for each reference: {:?}", errors); |
| 708 | assert!(errors.iter().all(|d| d.1 == "label `<nowhere>` does not exist in the document"), "{:?}", errors); |
| 709 | assert_ne!(errors[0].2, errors[1].2, "the two references are at different places"); |
| 710 | Ok(()) |
| 711 | } |
| 712 | |
| 713 | /// An error met again at one span with one message is reported once (Typst's `deduplicate`): a loop's body |
| 714 | /// refers to the missing label at one span however many times it runs. |
| 715 | #[test] |
| 716 | fn a_delayed_error_met_again_at_one_span_is_reported_once() -> Outcome<()> { |
| 717 | let (laid, diags) = res!(flat_run("#for i in range(3) [@nowhere ]\n")); |
| 718 | assert!(laid.is_err()); |
| 719 | let errors: Vec<_> = diags.iter().filter(|d| d.0).collect(); |
| 720 | assert_eq!(errors.len(), 1, "{:?}", errors); |
| 721 | Ok(()) |
| 722 | } |
| 723 | |
| 724 | /// What a body realises to when it is laid out again at its place (Typst's `Locator::relayout`): the footnote |
| 725 | /// in it is located as it was the first time, whatever was realised in between, and a second element of the |
| 726 | /// same kind and span at the same level has a place, and so a footnote, of its own. |
| 727 | #[test] |
| 728 | fn a_body_realised_again_at_its_place_is_located_alike() -> Outcome<()> { |
| 729 | let root = PathBuf::from("/"); |
| 730 | let mut world = World::new(root.clone()); |
| 731 | let src = "#block[A note#footnote[One.]]\n\n#for i in range(2) [#block[A note#footnote[Two.]]]\n"; |
| 732 | let id = res!(world.add_source(root.join("__place.typ"), src.to_string())); |
| 733 | let mut engine = Engine::new(world); |
| 734 | let module = res!(eval_source(&mut engine, id)); |
| 735 | let styles = StyleChain::root(); |
| 736 | let pairs = res!(realise(&mut engine, &module.content, &styles, RealiseMode::Document)); |
| 737 | let blocks: Vec<Content> = pairs.iter().filter(|p| p.content.is(ElemKind::Block)).map(|p| p.content.clone()).collect(); |
| 738 | assert_eq!(blocks.len(), 3, "three blocks realise"); |
| 739 | let places: Vec<_> = blocks.iter().map(|b| b.place()).collect(); |
| 740 | assert!(places.iter().all(|p| p.is_some()), "a block is given a place when it is realised"); |
| 741 | assert_ne!(places[1], places[2], "two blocks from one source span have places of their own"); |
| 742 | |
| 743 | // The footnote's location in the realised body of a block, at the block's place or nowhere in particular. |
| 744 | let note_in = |engine: &mut Engine, b: &Content, at: Option<oxedyne_fe2o3_austenite::eval::locate::Place>| -> Outcome<Location> { |
| 745 | let body = match b.field("body") { |
| 746 | Some(Value::Content(c)) => c.clone(), |
| 747 | _ => return Err(err!("a block without a body"; Test)), |
| 748 | }; |
| 749 | let pairs = res!(engine.within(at, |e| realise(e, &body, &styles, RealiseMode::Flow))); |
| 750 | for p in &pairs { |
| 751 | if let Some(Tag::Start(c)) = &p.tag { |
| 752 | if c.is(ElemKind::Footnote) { |
| 753 | return c.location().ok_or_else(|| err!("a footnote without a location"; Test)); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | let par = pairs.iter().find(|p| p.content.is(ElemKind::Par)); |
| 758 | match par { |
| 759 | Some(p) => { |
| 760 | let inner = match p.content.field("body") { |
| 761 | Some(Value::Content(c)) => c.clone(), |
| 762 | _ => return Err(err!("a paragraph without a body"; Test)), |
| 763 | }; |
| 764 | let kids = res!(engine.within(p.content.place(), |e| realise(e, &inner, &p.styles, RealiseMode::Inline))); |
| 765 | for k in &kids { |
| 766 | if let Some(Tag::Start(c)) = &k.tag { |
| 767 | if c.is(ElemKind::Footnote) { |
| 768 | return c.location().ok_or_else(|| err!("a footnote without a location"; Test)); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | Err(err!("no footnote in the paragraph"; Test)) |
| 773 | }, |
| 774 | None => Err(err!("no footnote and no paragraph in the block"; Test)), |
| 775 | } |
| 776 | }; |
| 777 | let first = res!(note_in(&mut engine, &blocks[0], places[0])); |
| 778 | // Something else realised in between takes ordinals of the document's own locator. |
| 779 | let _ = res!(note_in(&mut engine, &blocks[1], places[1])); |
| 780 | let again = res!(note_in(&mut engine, &blocks[0], places[0])); |
| 781 | assert_eq!(first, again, "the same block realised again at its place located its footnote anew"); |
| 782 | let other = res!(note_in(&mut engine, &blocks[1], places[1])); |
| 783 | assert_ne!(first, other, "two blocks share a footnote location"); |
| 784 | // Without the place, the second realisation takes the next ordinal: the fault the place cures. |
| 785 | let loose_a = res!(note_in(&mut engine, &blocks[0], None)); |
| 786 | let loose_b = res!(note_in(&mut engine, &blocks[0], None)); |
| 787 | assert_ne!(loose_a, loose_b, "ordinals did not count in the document's locator"); |
| 788 | Ok(()) |
| 789 | } |
| 790 | |
| 791 | /// Typst's `CounterState::step`, on the cases `counter_str.typ` checks against the oracle end to end, |
| 792 | /// spelt out so a regression names the rule rather than a fixture. |
| 793 | #[test] |
| 794 | fn counter_step_pads_with_zero_and_truncates() { |
| 795 | let mut s = vec![2]; |
| 796 | intro::step(&mut s, 3, 1); |
| 797 | assert_eq!(s, vec![2, 0, 1]); |
| 798 | let mut s = vec![2, 5, 9]; |
| 799 | intro::step(&mut s, 2, 1); |
| 800 | assert_eq!(s, vec![2, 6]); |
| 801 | let mut s = vec![1]; |
| 802 | intro::step(&mut s, 1, 3); |
| 803 | assert_eq!(s, vec![4]); |
| 804 | } |
| 805 | |
| 806 | /// The introspector's own cost per located element, on a document of 4,000 labelled metadata elements |
| 807 | /// laid out by [`Flat`]: the record and the location, kind and label indices, by capacity. The elements' |
| 808 | /// own fields are counted apart, since realisation made them and the module shares their bodies. |
| 809 | #[test] |
| 810 | fn introspector_cost_per_element() -> Outcome<()> { |
| 811 | let mut src = String::new(); |
| 812 | for i in 0..4000 { |
| 813 | src.push_str(&fmt!("#metadata({}) <m>\n", i)); |
| 814 | } |
| 815 | let root = PathBuf::from("/"); |
| 816 | let mut world = World::new(root.clone()); |
| 817 | let id = res!(world.add_source(root.join("__cost.typ"), src)); |
| 818 | let mut engine = Engine::new(world); |
| 819 | let module = res!(eval_source(&mut engine, id)); |
| 820 | let laid = res!(fixpoint::run_with(&mut engine, &module, &mut Flat, &mut Counting::default())); |
| 821 | let n = laid.intro.len(); |
| 822 | assert_eq!(n, 4000); |
| 823 | let own = laid.intro.footprint() as f64 / n as f64; |
| 824 | let elem = laid.intro.elems().map(|e| match e.elem() { |
| 825 | Some(x) => std::mem::size_of::<Elem>() + 2 * std::mem::size_of::<usize>() |
| 826 | + x.fields.capacity() * std::mem::size_of::<(FieldId, Value)>(), |
| 827 | None => 0, |
| 828 | }).sum::<usize>() as f64 / n as f64; |
| 829 | println!("introspector: {:.1} bytes per element of its own (a record is {} bytes); each prepared \ |
| 830 | metadata element holds {:.1} bytes more", own, std::mem::size_of::<intro::Record>(), elem); |
| 831 | assert!(own <= 96.0, "the introspector costs {:.1} bytes per element", own); |
| 832 | Ok(()) |
| 833 | } |
| 834 | |
| 835 | /// A counter update element as `counter(key).update(..)` makes it, with a location. |
| 836 | fn counter_update(key: &CounterKey, update: &CounterUpdate, loc: u64) -> Content { |
| 837 | let kind = ElemKind::CounterUpdate; |
| 838 | let fields = vec![ |
| 839 | (kind.field_id("key").unwrap_or(FieldId(0)), key.to_value()), |
| 840 | (kind.field_id("update").unwrap_or(FieldId(1)), update.to_value()), |
| 841 | ]; |
| 842 | let mut e = Elem::new(kind, fields, Span::detached()); |
| 843 | e.location = Some(Location(loc)); |
| 844 | e.prepared = true; |
| 845 | Content::from_elem(e) |
| 846 | } |
| 847 | |
| 848 | /// A state update element as `state(key).update(v)` makes it, with a location. |
| 849 | fn state_update(key: &str, v: i64, loc: u64) -> Content { |
| 850 | let kind = ElemKind::StateUpdate; |
| 851 | let fields = vec![ |
| 852 | (kind.field_id("key").unwrap_or(FieldId(0)), Value::str(key.to_string())), |
| 853 | (kind.field_id("update").unwrap_or(FieldId(1)), Value::Int(v)), |
| 854 | ]; |
| 855 | let mut e = Elem::new(kind, fields, Span::detached()); |
| 856 | e.location = Some(Location(loc)); |
| 857 | e.prepared = true; |
| 858 | Content::from_elem(e) |
| 859 | } |
| 860 | |
| 861 | /// The counter at `loc` the slow way: every update in document order up to it, folded afresh. Only |
| 862 | /// `Set` and `Step` updates are used, so no engine is needed to apply them. |
| 863 | fn counter_rescan(intro: &Introspector, counter: &Counter, loc: Location) -> Vec<u64> { |
| 864 | let sel = match &counter.key { |
| 865 | CounterKey::Selector(s) => Some(s.clone()), |
| 866 | _ => None, |
| 867 | }; |
| 868 | let at = intro.order(loc); |
| 869 | let mut state = vec![if counter.is_page() { 1 } else { 0 }]; |
| 870 | let mut pg = 1u32; |
| 871 | for (i, e) in intro.elems().enumerate() { |
| 872 | if at.map(|a| i > a).unwrap_or(false) { |
| 873 | break; |
| 874 | } |
| 875 | let selected = e.is(ElemKind::CounterUpdate) |
| 876 | || sel.as_ref().map(|s| s.matches(e, None).unwrap_or(false)).unwrap_or(false); |
| 877 | let moves = !e.is(ElemKind::CounterUpdate) |
| 878 | || e.field("key").and_then(CounterKey::from_value).map(|k| k.same(&counter.key)).unwrap_or(false); |
| 879 | if !selected || !moves { |
| 880 | continue; |
| 881 | } |
| 882 | if counter.is_page() { |
| 883 | if let Some(l) = e.location() { |
| 884 | let p = intro.page(l).unwrap_or(1); |
| 885 | if p > pg { |
| 886 | intro::step(&mut state, 1, (p - pg) as u64); |
| 887 | } |
| 888 | pg = p; |
| 889 | } |
| 890 | } |
| 891 | match intro::element_update(e) { |
| 892 | Some(CounterUpdate::Set(ns)) => state = ns, |
| 893 | Some(CounterUpdate::Step(l)) => intro::step(&mut state, l, 1), |
| 894 | _ => (), |
| 895 | } |
| 896 | } |
| 897 | if counter.is_page() { |
| 898 | let p = intro.page(loc).unwrap_or(1); |
| 899 | if p > pg { |
| 900 | intro::step(&mut state, 1, (p - pg) as u64); |
| 901 | } |
| 902 | } |
| 903 | state |
| 904 | } |
| 905 | |
| 906 | /// The state at `loc` the slow way: the last update of its key at or before `loc`. |
| 907 | fn state_rescan(intro: &Introspector, key: &str, init: i64, loc: Location) -> i64 { |
| 908 | let at = intro.order(loc); |
| 909 | let mut v = init; |
| 910 | for (i, e) in intro.elems().enumerate() { |
| 911 | if at.map(|a| i > a).unwrap_or(false) { |
| 912 | break; |
| 913 | } |
| 914 | if !e.is(ElemKind::StateUpdate) { |
| 915 | continue; |
| 916 | } |
| 917 | if let (Some(Value::Str(k)), Some(Value::Int(n))) = (e.field("key"), e.field("update")) { |
| 918 | if **k == *key { |
| 919 | v = *n; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | v |
| 924 | } |
| 925 | |
| 926 | /// A counter or state read finds its stop by the document order of the updates that moved it, and so |
| 927 | /// answers as a rescan of every update before the location would: for several keys, updates of other |
| 928 | /// keys between, a selector key counting the elements it selects, the page counter, a location the |
| 929 | /// pages never placed, and a location no update stands at. The reads it records answer the same |
| 930 | /// against the introspector, so the pass holds, and a second ask records nothing new. |
| 931 | #[test] |
| 932 | fn counter_and_state_reads_equal_a_rescan() -> Outcome<()> { |
| 933 | let (a, b, z) = (CounterKey::Str("a".to_string()), CounterKey::Str("b".to_string()), CounterKey::Str("z".to_string())); |
| 934 | let meta = CounterKey::Selector(Selector::Elem(ElemKind::Metadata, None)); |
| 935 | let mut bld = Builder::new(); |
| 936 | let mut loc = 0u64; |
| 937 | let mut put = |c: Content, page: u32| { loc += 1; bld.record(&c, at(page, loc as i32), None); }; |
| 938 | put(located(10, "m", 1), 1); |
| 939 | put(counter_update(&a, &CounterUpdate::Step(1), 2), 1); |
| 940 | put(counter_update(&b, &CounterUpdate::Set(vec![7]), 3), 1); |
| 941 | put(located(11, "m", 4), 1); |
| 942 | put(counter_update(&a, &CounterUpdate::Step(2), 5), 2); |
| 943 | put(state_update("s", 5, 6), 2); |
| 944 | put(counter_update(&meta, &CounterUpdate::Set(vec![40]), 7), 2); |
| 945 | put(located(12, "m", 8), 2); |
| 946 | put(counter_update(&CounterKey::Page, &CounterUpdate::Step(1), 9), 3); |
| 947 | put(counter_update(&a, &CounterUpdate::Set(vec![1, 2]), 10), 3); |
| 948 | put(state_update("s", 9, 11), 3); |
| 949 | put(state_update("t", 3, 12), 3); |
| 950 | put(located(13, "m", 13), 4); |
| 951 | put(counter_update(&a, &CounterUpdate::Step(1), 14), 4); |
| 952 | let intro = bld.finish(); |
| 953 | |
| 954 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 955 | engine.intro = Arc::new(intro.clone()); |
| 956 | let counters = [ |
| 957 | Counter::new(a), Counter::new(b), Counter::new(z), Counter::new(meta), Counter::page(), |
| 958 | Counter::of(ElemKind::Metadata), |
| 959 | ]; |
| 960 | let mut locs: Vec<Location> = (1..=14).map(Location).collect(); |
| 961 | locs.push(Location(999)); // never placed |
| 962 | for _ in 0..2 { |
| 963 | for c in &counters { |
| 964 | for l in &locs { |
| 965 | let want = counter_rescan(&intro, c, *l); |
| 966 | let got = res!(intro::counter_at(&mut engine, c, *l)); |
| 967 | assert_eq!(got, want, "counter {:?} at {:?}", c.key, l); |
| 968 | } |
| 969 | } |
| 970 | for (key, init) in [("s", 0i64), ("t", 1), ("none", 2)] { |
| 971 | let s = State { key: key.to_string(), init: Value::Int(init) }; |
| 972 | for l in &locs { |
| 973 | let want = state_rescan(&intro, key, init, *l); |
| 974 | match res!(intro::state_at(&mut engine, &s, *l)) { |
| 975 | Value::Int(got) => assert_eq!(got, want, "state {} at {:?}", key, l), |
| 976 | other => return Err(err!("state {} at {:?} read {:?}", key, l, other; Test)), |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | // The first round made every distinct read; the second makes none. |
| 981 | if engine.reads.all.is_empty() { |
| 982 | return Err(err!("no read was recorded"; Test)); |
| 983 | } |
| 984 | } |
| 985 | let made = engine.reads.all.len(); |
| 986 | let _ = res!(intro::counter_at(&mut engine, &counters[0], Location(2))); |
| 987 | assert_eq!(engine.reads.all.len(), made, "a question asked again was recorded again"); |
| 988 | assert!(res!(engine.reads.holds(&intro)), "a recorded answer differs from the introspector's own"); |
| 989 | Ok(()) |
| 990 | } |