Debian Codegraph @

src/test/01-Basic/234-shred-ancestor.ck

raw
1 // validate that Shred.ancestor() correctly gets the top-level
2 // shred through multiple layers of recursion
3
4 // count the number of recursive calls
5 0 => int count;
6
7 fun void validateAncestor(int want_ancestor, int depth)
8 {
9 count++;
10 me.ancestor().id() => int got_ancestor;
11
12 if( want_ancestor != got_ancestor )
13 {
14 <<< "FAILURE, spork~ validateAncestor(...)'s ancestor shred should be the top-level shred" >>>;
15 me.exit();
16 }
17
18 if( depth > 1 )
19 {
20 spork ~ validateAncestor(want_ancestor, depth-1);
21 }
22 // let time pass to let children shreds run
23 samp => now;
24 }
25
26 // this should be the top-level shred (and has no ancestor)
27 if( me.ancestor() != me )
28 {
29 <<< "FAILURE, the top-level shred should have be me" >>>;
30 me.exit();
31 }
32
33 100 => int recursive_calls;
34 spork ~ validateAncestor( me.id(), recursive_calls );
35
36 // let time pass to let children shreds run
37 samp => now;
38
39 // check count
40 if( count != recursive_calls )
41 {
42 <<< "FAILURE: incorrect number of recursive calls made.",
43 "got:", count, "want:", recursive_calls >>>;
44 me.exit();
45 }
46
47 // done
48 <<< "success" >>>;
49