-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from nshaheed/shred-parent
Add Shred.parent() & Shred.ancestor()
- Loading branch information
Showing
7 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Shred.ancestor() returns the top-level shred that is directly or | ||
// indirectly a parent shred of the calling shred; useful for getting | ||
// information relevant to top-level shreds; related: parent.ck | ||
|
||
// test me | ||
<<< "the top-level shred's ID is:", me.id() >>>; | ||
// my ancestor | ||
<<< "the top-level shred's ancestor ID is:", me.ancestor().id(), "\n" >>>; | ||
|
||
// recursive function to test finding ancestor from different | ||
// "generations" of shreds | ||
fun void findTheAncestor( int generation ) | ||
{ | ||
// stop recursing | ||
if( generation <= 0 ) return; | ||
|
||
// calling shred | ||
<<< "the sporked shred's ID is:", me.id() >>>; | ||
// this will always be the same as the top-level shred id | ||
<<< "the sporked shred's ancestor ID is:", me.ancestor().id(), "\n" >>>; | ||
|
||
// spork a child shred | ||
spork ~ findTheAncestor( generation-1 ); | ||
// wait a bit to give child shred a chance to run | ||
1::samp => now; | ||
} | ||
|
||
// recursively spork function this 10 times; me.ancestor() should always | ||
// point to the top-level shred | ||
spork ~ findTheAncestor(10); | ||
|
||
// wait to give children shreds a chance to run | ||
1::samp => now; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// return a shred's parent shred (i.e., the shred that sporked it) | ||
// if the shred is a top-level shred, then me.parent() will return null | ||
// related: ancestor.ck | ||
|
||
<<< "the top-level shred's ID is:", me.id() >>>; | ||
<<< "the top-level shred's parent is:", me.parent() >>>; | ||
|
||
fun void findTheParent() | ||
{ | ||
// this will be the same as the top-level shred id | ||
<<< "the sporked shred's ID is:", me.id() >>>; | ||
<<< "the sporked shred's parent ID is:", me.parent().id() >>>; | ||
} | ||
|
||
// spork a child shred | ||
spork ~ findTheParent(); | ||
|
||
// yield to let validateParent(...) run | ||
me.yield(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// test shred.parent() | ||
|
||
// check shred parent | ||
fun void validateParent(int main_shred_id) | ||
{ | ||
// id of parent | ||
me.parent().id() => int parent_id; | ||
|
||
// make sure same id | ||
if( parent_id != main_shred_id ) | ||
{ | ||
<<< "FAILURE: parent shred should be the top-level shred" >>>; | ||
me.exit(); | ||
} | ||
} | ||
|
||
// this should be the top-level shred (and has no parent) | ||
if( me.parent() != null ) | ||
{ | ||
<<< "FAILURE, the top-level shred should have no parent" >>>; | ||
me.exit(); | ||
} | ||
|
||
// spork | ||
spork ~ validateParent( me.id() ); | ||
// yield to let validateParent(...) run | ||
me.yield(); | ||
<<< "success" >>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// validate that Shred.parent() correctly gets the parent shred | ||
// through multiple layers of recursion. | ||
|
||
0 => int count; // count the number of recursive calls | ||
|
||
fun void validateParent( int want_parent, int depth ) | ||
{ | ||
count++; | ||
me.parent().id() => int got_parent; | ||
|
||
if( want_parent != got_parent ) | ||
{ | ||
<<< "FAILURE, spork~ validateParent(...)'s parent shred is not matching" >>>; | ||
me.exit(); | ||
} | ||
|
||
if( depth > 1 ) | ||
{ | ||
spork~ validateParent( me.id(), depth-1 ); | ||
} | ||
samp => now; | ||
} | ||
|
||
// this should be the top-level shred (and has no parent) | ||
if( me.parent() != null ) | ||
{ | ||
<<< "FAILURE, the top-level shred should have no parent" >>>; | ||
me.exit(); | ||
} | ||
|
||
100 => int recursive_calls; | ||
spork~ validateParent( me.id(), recursive_calls ); | ||
|
||
// pass time (or yield) to let children shreds run | ||
samp => now; | ||
|
||
// check count | ||
if( count != recursive_calls ) | ||
{ | ||
<<< "FAILURE: incorrect number of recursive calls made. got:", | ||
count, "want:", recursive_calls >>>; | ||
me.exit(); | ||
} | ||
|
||
// done | ||
<<< "success" >>>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// validate that Shred.ancestor() correctly gets the top-level | ||
// shred through multiple layers of recursion | ||
|
||
// count the number of recursive calls | ||
0 => int count; | ||
|
||
fun void validateAncestor(int want_ancestor, int depth) | ||
{ | ||
count++; | ||
me.ancestor().id() => int got_ancestor; | ||
|
||
if( want_ancestor != got_ancestor ) | ||
{ | ||
<<< "FAILURE, spork~ validateAncestor(...)'s ancestor shred should be the top-level shred" >>>; | ||
me.exit(); | ||
} | ||
|
||
if( depth > 1 ) | ||
{ | ||
spork ~ validateAncestor(want_ancestor, depth-1); | ||
} | ||
// let time pass to let children shreds run | ||
samp => now; | ||
} | ||
|
||
// this should be the top-level shred (and has no ancestor) | ||
if( me.ancestor() != me ) | ||
{ | ||
<<< "FAILURE, the top-level shred should have be me" >>>; | ||
me.exit(); | ||
} | ||
|
||
100 => int recursive_calls; | ||
spork ~ validateAncestor( me.id(), recursive_calls ); | ||
|
||
// let time pass to let children shreds run | ||
samp => now; | ||
|
||
// check count | ||
if( count != recursive_calls ) | ||
{ | ||
<<< "FAILURE: incorrect number of recursive calls made.", | ||
"got:", count, "want:", recursive_calls >>>; | ||
me.exit(); | ||
} | ||
|
||
// done | ||
<<< "success" >>>; | ||
|