Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/fingerprint.rs

16.4 KiB, 2 runs

created by r1870400018:68579, 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//! U5: content fingerprints stand for what the content is, never for where it was written. The same text at a
2//! shifted offset fingerprints alike; a change to any field, label, kind, guard, style, recipe or closure
3//! gives a different one; and a fingerprint read before a change is never the one read after it.
4
5use oxedyne_fe2o3_austenite::eval::content::{
6 build,
7 synthesise,
8 Content,
9 Elem,
10 ElemKind,
11 FieldId,
12 Sequence,
13 Styled,
14};
15use oxedyne_fe2o3_austenite::eval::fp::{
16 elem_fp,
17 Shared,
18};
19use oxedyne_fe2o3_austenite::eval::locate::{
20 Location,
21 Place,
22};
23use oxedyne_fe2o3_austenite::eval::ops::content_add;
24use oxedyne_fe2o3_austenite::eval::styles::{
25 Property,
26 RecipeIndex,
27 StyleChain,
28 Style,
29 Styles,
30};
31use oxedyne_fe2o3_austenite::eval::value::{
32 Label,
33 Value,
34};
35use oxedyne_fe2o3_austenite::eval::{
36 eval_source,
37 Engine,
38 World,
39};
40use oxedyne_fe2o3_austenite::syntax::{
41 FileId,
42 Span,
43};
44
45use oxedyne_fe2o3_core::prelude::*;
46use oxedyne_fe2o3_hash::fingerprint::Fingerprint;
47
48use std::path::PathBuf;
49
50// What a source evaluates to, fingerprinted.
51fn fp(src: &str) -> Outcome<Fingerprint> {
52 let root = PathBuf::from("/");
53 let mut world = World::new(root.clone());
54 let id = res!(world.add_source(root.join("__fp.typ"), src.to_string()));
55 let mut engine = Engine::new(world);
56 let module = res!(eval_source(&mut engine, id));
57 Ok(module.content.fingerprint())
58}
59
60// A metadata element, which holds one value in field 0.
61fn meta(v: Value, span: Span) -> Elem {
62 Elem::new(ElemKind::Metadata, vec![(FieldId(0), v)], span)
63}
64
65fn elem(e: Elem) -> Content { Content::from_elem(e) }
66
67// Two sources of the same shape, the second written at other offsets.
68const SHIFTED: &[(&str, &str, &str)] = &[
69 ("a closure numbering",
70 "#let a = 1\n#heading(numbering: n => [Part #n])[T]",
71 "#let aaaaaaaaaaaaaaaa = 12345\n#heading(numbering: n => [Part #n])[T]"),
72 ("a closure that captures",
73 "#let k = 5\n#let a = 1\n#heading(numbering: n => [#k #n])[T]",
74 "#let k = 5\n#let aaaaaaaaaaaaaaaa = 12345\n#heading(numbering: n => [#k #n])[T]"),
75 ("a show rule with a closure",
76 "#let a = 1\n#show heading: it => [X #it.body]\n#heading[T]",
77 "#let aaaaaaaaaaaaaaaa = 12345\n#show heading: it => [X #it.body]\n#heading[T]"),
78 ("a set rule",
79 "#let a = 1\n#set text(size: 10pt)\nHello",
80 "#let aaaaaaaaaaaaaaaa = 12345\n#set text(size: 10pt)\nHello"),
81 ("a label",
82 "#let a = 1\n#heading[T] <lab>",
83 "#let aaaaaaaaaaaaaaaa = 12345\n#heading[T] <lab>"),
84 ("a selector with fields",
85 "#let a = 1\n#show heading.where(level: 1): it => it\n#heading[T]",
86 "#let aaaaaaaaaaaaaaaa = 12345\n#show heading.where(level: 1): it => it\n#heading[T]"),
87];
88
89// Two sources that differ in one thing, which the fingerprint must see.
90const DIFFERENT: &[(&str, &str, &str)] = &[
91 ("a field value", "#heading[A]", "#heading[B]"),
92 ("a level", "#heading(level: 1)[A]", "#heading(level: 2)[A]"),
93 ("an extra field", "#heading[A]", "#heading(outlined: false)[A]"),
94 ("a field id", "#heading(outlined: false)[A]", "#heading(bookmarked: false)[A]"),
95 ("a label", "#heading[A] <a>", "#heading[A] <b>"),
96 ("a missing label", "#heading[A]", "#heading[A] <a>"),
97 ("a kind", "#heading[A]", "#strong[A]"),
98 ("a style property value", "#set text(size: 10pt)\nA", "#set text(size: 11pt)\nA"),
99 ("a style property", "#set text(size: 10pt)\nA", "#set text(weight: 700)\nA"),
100 ("a recipe selector", "#show heading: it => it\nA", "#show strong: it => it\nA"),
101 ("a recipe selector field", "#show heading.where(level: 1): it => it\nA", "#show heading.where(level: 2): it => it\nA"),
102 ("a recipe transform function", "#show heading: it => [X]\nA", "#show heading: it => [Y]\nA"),
103 ("a recipe transform content", "#show heading: [X]\nA", "#show heading: [Y]\nA"),
104 ("a closure body", "#heading(numbering: n => [A #n])[T]", "#heading(numbering: n => [B #n])[T]"),
105 ("a closure capture", "#let c = 1\n#heading(numbering: n => [#c #n])[T]",
106 "#let c = 2\n#heading(numbering: n => [#c #n])[T]"),
107 ("a closure parameter default", "#heading(numbering: (n, d: 1) => [#n #d])[T]", "#heading(numbering: (n, d: 2) => [#n #d])[T]"),
108 ("a sequence order", "#heading[A] #strong[B]", "#strong[B] #heading[A]"),
109 ("a sequence length", "A B", "A B C"),
110 ("a nested field value", "#strong[#emph[A]]", "#strong[#emph[B]]"),
111 ("an array value in a field", "#metadata((1, 2, 3))", "#metadata((1, 2, 4))"),
112 ("a dictionary key in a field", "#metadata((a: 1))", "#metadata((b: 1))"),
113 ("a colour in a field", "#text(fill: red)[A]", "#text(fill: blue)[A]"),
114 ("a length in a field", "#h(10pt)", "#h(12pt)"),
115];
116
117#[test]
118fn the_same_text_at_shifted_offsets_fingerprints_alike() -> Outcome<()> {
119 for (name, a, b) in SHIFTED {
120 assert_ne!(a, b, "{}: the two sources are the same text", name);
121 let fa = res!(fp(a));
122 let fb = res!(fp(b));
123 assert_eq!(fa, fb, "{}: a shifted offset changed the fingerprint", name);
124 assert_eq!(fa, res!(fp(a)), "{}: two evaluations of one source differ", name);
125 }
126 assert_eq!(SHIFTED.len(), 6, "a case was dropped");
127 Ok(())
128}
129
130#[test]
131fn a_change_to_any_part_of_the_content_changes_the_fingerprint() -> Outcome<()> {
132 for (name, a, b) in DIFFERENT {
133 let fa = res!(fp(a));
134 let fb = res!(fp(b));
135 assert_ne!(fa, fb, "{}: the fingerprint did not see the change", name);
136 assert_eq!(fa, res!(fp(a)), "{}: two evaluations of one source differ", name);
137 }
138 assert_eq!(DIFFERENT.len(), 23, "a case was dropped");
139 Ok(())
140}
141
142#[test]
143fn spans_locations_and_places_are_not_in_a_content_fingerprint() {
144 let a = elem(meta(Value::Int(7), Span::new(FileId(1), 4, 9)));
145 let b = elem(meta(Value::Int(7), Span::new(FileId(3), 400, 900)));
146 assert_eq!(a.fingerprint(), b.fingerprint(), "a span entered the fingerprint");
147 let mut placed = meta(Value::Int(7), Span::detached());
148 placed.location = Some(Location(11));
149 placed.place = Some(Place(12));
150 assert_eq!(a.fingerprint(), elem(placed).fingerprint(), "a location or place entered the fingerprint");
151}
152
153#[test]
154fn a_guard_a_label_and_the_prepared_flag_are_in_a_content_fingerprint() {
155 let plain = elem(meta(Value::Int(7), Span::detached())).fingerprint();
156 let mut guarded = meta(Value::Int(7), Span::detached());
157 guarded.guards.push(RecipeIndex(2));
158 assert_ne!(plain, elem(guarded.clone()).fingerprint(), "a guard was not seen");
159 let mut other = meta(Value::Int(7), Span::detached());
160 other.guards.push(RecipeIndex(3));
161 assert_ne!(elem(guarded.clone()).fingerprint(), elem(other).fingerprint(), "a guard's index was not seen");
162 // Guards are a set: the order they were added in is not a difference.
163 let mut two = meta(Value::Int(7), Span::detached());
164 two.guards = vec![RecipeIndex(1), RecipeIndex(2)];
165 let mut flip = meta(Value::Int(7), Span::detached());
166 flip.guards = vec![RecipeIndex(2), RecipeIndex(1)];
167 assert_eq!(elem(two).fingerprint(), elem(flip).fingerprint(), "the order of guards was seen");
168 let mut labelled = meta(Value::Int(7), Span::detached());
169 labelled.label = Some(Label::new("x"));
170 assert_ne!(plain, elem(labelled).fingerprint(), "a label was not seen");
171 let mut prepared = meta(Value::Int(7), Span::detached());
172 prepared.prepared = true;
173 assert_ne!(plain, elem(prepared).fingerprint(), "the prepared flag was not seen");
174}
175
176#[test]
177fn the_order_fields_were_set_in_is_not_a_difference() {
178 let mut a = meta(Value::Int(1), Span::detached());
179 a.fields.push((FieldId(1), Value::Int(2)));
180 let mut b = meta(Value::Int(2), Span::detached());
181 b.fields = vec![(FieldId(1), Value::Int(2)), (FieldId(0), Value::Int(1))];
182 assert_eq!(elem(a).fingerprint(), elem(b).fingerprint(), "the order of fields was seen");
183}
184
185// Mutation. A fingerprint read before a change is never the one read after it. Each case reads the
186// fingerprint of a node held by one handle only, so the change is made in place, where a stale cell
187// would show; changes it; and compares with a node built afresh to the same shape, whose cell has never
188// been read.
189
190// The fingerprint after a change is a new one, and the one a cold node of the same shape has.
191fn fresh(what: &str, warm: Fingerprint, now: &Content, cold: &Content) {
192 assert_ne!(warm, now.fingerprint(), "{}: the old fingerprint survived the change", what);
193 assert_eq!(now.fingerprint(), cold.fingerprint(), "{}: the fingerprint is not a cold one", what);
194}
195
196fn t(s: &str) -> Content { Content::text(s) }
197
198fn seq(v: &[&str]) -> Content { Content::sequence(v.iter().map(|s| t(s)).collect()) }
199
200fn prop(n: i64) -> Style {
201 Style::Property(Property::new(ElemKind::Text, FieldId(1), Value::Int(n), Span::detached()))
202}
203
204fn styles(n: &[i64]) -> Styles { Styles::from_vec(n.iter().map(|n| prop(*n)).collect()) }
205
206#[test]
207fn setting_a_field_of_a_warm_element_gives_a_fresh_fingerprint() {
208 let mut c = elem(meta(Value::Int(1), Span::detached()));
209 let warm = c.fingerprint();
210 c.set(FieldId(0), Value::Int(2));
211 fresh("an existing field", warm, &c, &elem(meta(Value::Int(2), Span::detached())));
212 let warm = c.fingerprint();
213 c.set(FieldId(1), Value::Int(5));
214 let cold = Elem::new(ElemKind::Metadata, vec![(FieldId(0), Value::Int(2)), (FieldId(1), Value::Int(5))], Span::detached());
215 fresh("a new field", warm, &c, &elem(cold));
216 if let Content::Elem(e) = &c {
217 assert_eq!(c.fingerprint(), elem_fp(e), "the cell and a pure recomputation differ");
218 }
219 let warm = c.fingerprint();
220 c.set(FieldId(1), Value::Int(5));
221 assert_eq!(warm, c.fingerprint(), "setting a field to the value it has changed the fingerprint");
222}
223
224#[test]
225fn labelling_warm_content_gives_a_fresh_fingerprint() {
226 let label = || Label::new("x");
227 // An element.
228 let c = elem(meta(Value::Int(7), Span::detached()));
229 let warm = c.fingerprint();
230 let c = c.labelled(label());
231 let mut cold = meta(Value::Int(7), Span::detached());
232 cold.label = Some(label());
233 fresh("an element", warm, &c, &elem(cold));
234 // A sequence.
235 let c = seq(&["a", "b"]);
236 let warm = c.fingerprint();
237 let c = c.labelled(label());
238 let mut cold = Sequence::new(vec![t("a"), t("b")]);
239 cold.label = Some(label());
240 fresh("a sequence", warm, &c, &Content::Sequence(Shared::new(cold)));
241 // Styled content, which labels its child.
242 let c = t("a").styled(styles(&[1]));
243 let warm = c.fingerprint();
244 let c = c.labelled(label());
245 let mut child = Elem::new(ElemKind::Text, vec![(FieldId(0), Value::str("a"))], Span::detached());
246 child.label = Some(label());
247 fresh("styled content", warm, &c, &Content::Styled(Shared::new(Styled::new(elem(child), styles(&[1])))));
248}
249
250#[test]
251fn a_place_and_a_span_change_no_fingerprint() {
252 let mut c = elem(meta(Value::Int(7), Span::new(FileId(1), 4, 9)));
253 let warm = c.fingerprint();
254 c.set_place(Place(3));
255 assert_eq!(warm, c.fingerprint(), "a place changed the fingerprint");
256 let c = c.with_span(Span::new(FileId(2), 40, 90));
257 assert_eq!(warm, c.fingerprint(), "a span changed the fingerprint");
258 let c = seq(&["a", "b"]);
259 let warm = c.fingerprint();
260 let c = c.with_span(Span::new(FileId(2), 40, 90));
261 assert_eq!(warm, c.fingerprint(), "a sequence's span changed the fingerprint");
262}
263
264#[test]
265fn adding_to_a_warm_sequence_gives_a_fresh_fingerprint() {
266 let x = seq(&["a", "b"]);
267 let warm = x.fingerprint();
268 fresh("sequence and sequence", warm, &content_add(x, seq(&["c", "d"])), &seq(&["a", "b", "c", "d"]));
269 let x = seq(&["a", "b"]);
270 let warm = x.fingerprint();
271 fresh("sequence and content", warm, &content_add(x, t("c")), &seq(&["a", "b", "c"]));
272 let y = seq(&["b", "c"]);
273 let warm = y.fingerprint();
274 fresh("content and sequence", warm, &content_add(t("a"), y), &seq(&["a", "b", "c"]));
275 let a = t("a");
276 let warm = a.fingerprint();
277 fresh("content and content", warm, &content_add(a, t("b")), &seq(&["a", "b"]));
278}
279
280#[test]
281fn a_child_changed_through_its_parent_gives_the_parent_a_fresh_fingerprint() {
282 let mut c = seq(&["a", "b"]);
283 let warm = c.fingerprint();
284 if let Content::Sequence(s) = &mut c {
285 s.edit().children[0].set(FieldId(0), Value::str("z"));
286 }
287 fresh("a sequence's child", warm, &c, &seq(&["z", "b"]));
288 let mut c = t("a").styled(styles(&[1]));
289 let warm = c.fingerprint();
290 if let Content::Styled(s) = &mut c {
291 s.edit().child.set(FieldId(0), Value::str("z"));
292 }
293 fresh("styled content's child", warm, &c, &t("z").styled(styles(&[1])));
294}
295
296#[test]
297fn growing_a_warm_style_list_gives_a_fresh_fingerprint() {
298 let mut s = styles(&[1]);
299 let warm = s.fingerprint();
300 s.push(prop(2));
301 assert_ne!(warm, s.fingerprint(), "push: the old fingerprint survived");
302 assert_eq!(s.fingerprint(), styles(&[1, 2]).fingerprint(), "push: not a cold fingerprint");
303 let warm = s.fingerprint();
304 s.extend(&styles(&[3, 4]));
305 assert_ne!(warm, s.fingerprint(), "extend: the old fingerprint survived");
306 assert_eq!(s.fingerprint(), styles(&[1, 2, 3, 4]).fingerprint(), "extend: not a cold fingerprint");
307 let warm = s.fingerprint();
308 s.apply_outer(&styles(&[0]));
309 assert_ne!(warm, s.fingerprint(), "apply_outer: the old fingerprint survived");
310 assert_eq!(s.fingerprint(), styles(&[0, 1, 2, 3, 4]).fingerprint(), "apply_outer: not a cold fingerprint");
311 assert_ne!(styles(&[1, 2]).fingerprint(), styles(&[2, 1]).fingerprint(), "the order of styles was not seen");
312 // The list inside styled content changes its fingerprint too, though the styled node is a level up.
313 let mut c = t("a").styled(styles(&[1]));
314 let warm = c.fingerprint();
315 if let Content::Styled(h) = &mut c {
316 h.edit().styles.push(prop(2));
317 }
318 fresh("styled content's styles", warm, &c, &t("a").styled(styles(&[1, 2])));
319}
320
321#[test]
322fn a_clone_that_is_changed_leaves_the_original_fingerprint_alone() {
323 let a = elem(meta(Value::Int(1), Span::detached()));
324 let warm = a.fingerprint();
325 let mut b = a.clone();
326 assert_eq!(b.fingerprint(), warm, "a clone does not fingerprint as its original");
327 b.set(FieldId(0), Value::Int(2));
328 assert_eq!(a.fingerprint(), warm, "the original's fingerprint moved");
329 assert_ne!(b.fingerprint(), warm, "the clone's fingerprint did not move");
330 let a = seq(&["a", "b"]);
331 let warm = a.fingerprint();
332 let b = content_add(a.clone(), t("c"));
333 assert_eq!(a.fingerprint(), warm, "the original sequence's fingerprint moved");
334 assert_eq!(a.fingerprint(), seq(&["a", "b"]).fingerprint(), "the original sequence changed");
335 assert_ne!(b.fingerprint(), warm, "the extended clone's fingerprint did not move");
336 let a = styles(&[1]);
337 let warm = a.fingerprint();
338 let mut b = a.clone();
339 assert!(a.ptr_eq(&b), "a clone of a style list does not share it");
340 b.push(prop(2));
341 assert!(!a.ptr_eq(&b), "a changed clone still shares the list");
342 assert_eq!(a.fingerprint(), warm, "the original style list's fingerprint moved");
343 assert_ne!(b.fingerprint(), warm, "the changed clone's fingerprint did not move");
344}
345
346#[test]
347fn synthesising_a_warm_heading_gives_a_fresh_fingerprint() -> Outcome<()> {
348 let mut engine = Engine::new(World::new(PathBuf::from("/")));
349 let mut h = res!(build(&mut engine, ElemKind::Heading, vec![("body", Value::Content(t("T")))], Span::detached()));
350 let warm = h.fingerprint();
351 res!(synthesise(&mut engine, &mut h, &StyleChain::root()));
352 assert_ne!(warm, h.fingerprint(), "synthesis left the old fingerprint");
353 if let Content::Elem(e) = &h {
354 assert_eq!(h.fingerprint(), elem_fp(e), "the cell and a pure recomputation differ");
355 }
356 Ok(())
357}
358
359// A chain of functions in which each calls the one before it twice, through two names, so that the closure
360// tree a naive walk unfolds doubles at every level while the closures themselves stay few.
361fn chain(depth: usize, base: &str) -> String {
362 let mut s = format!("#let f0(x) = {}\n", base);
363 for i in 1..=depth {
364 s.push_str(&fmt!("#let a{i} = f{p}\n#let b{i} = f{p}\n#let f{i}(x) = a{i}(b{i}(x))\n", i = i, p = i - 1));
365 }
366 s.push_str(&fmt!("#metadata(f{})\n", depth));
367 s
368}
369
370// Evaluates and fingerprints on a thread of its own, so that a walk that never ends fails the test rather than
371// holding the run for ever.
372fn fp_within(src: String, secs: u64) -> Fingerprint {
373 let (tx, rx) = std::sync::mpsc::channel();
374 std::thread::spawn(move || {
375 let _ = tx.send(fp(&src).map_err(|e| fmt!("{}", e)));
376 });
377 match rx.recv_timeout(std::time::Duration::from_secs(secs)) {
378 Ok(Ok(f)) => f,
379 Ok(Err(e)) => panic!("the chain did not evaluate: {}", e),
380 Err(_) => panic!("fingerprinting a chain of shared closures took over {} s: a closure reached by many paths is walked once per path", secs),
381 }
382}
383
384#[test]
385fn closures_reached_by_many_paths_fingerprint_in_time_proportional_to_the_closures() {
386 let a = fp_within(chain(48, "x"), 20);
387 let b = fp_within(chain(48, "x + 1"), 20);
388 assert_ne!(a, b, "a change at the foot of the chain did not reach its head");
389 assert_eq!(a, fp_within(chain(48, "x"), 20), "two evaluations of one chain differ");
390}