From 9c7d3f30e6fce07a0bf2a056e1723aaf6ab541af Mon Sep 17 00:00:00 2001 From: nshaheed Date: Sun, 5 Nov 2023 12:13:01 -0800 Subject: [PATCH 1/5] add Shred.parent() --- examples/shred/parent.ck | 15 +++++++++++++++ src/core/chuck_lang.cpp | 12 ++++++++++++ src/core/chuck_lang.h | 1 + src/test/01-Basic/232-shred-parent.ck | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 examples/shred/parent.ck create mode 100644 src/test/01-Basic/232-shred-parent.ck diff --git a/examples/shred/parent.ck b/examples/shred/parent.ck new file mode 100644 index 000000000..39682f424 --- /dev/null +++ b/examples/shred/parent.ck @@ -0,0 +1,15 @@ + + +// If parent.ck is the top-level shred, then me.parent() will be null. +// Otherwise it will be a parent. +<<< "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~ findTheParent(); +me.yield(); // yield to let validateParent(...) run diff --git a/src/core/chuck_lang.cpp b/src/core/chuck_lang.cpp index 75d9529a5..dec29dbda 100644 --- a/src/core/chuck_lang.cpp +++ b/src/core/chuck_lang.cpp @@ -600,6 +600,11 @@ t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type ) func->doc = "get the operand stack size hint (in bytes) for shreds sporked from this one."; if( !type_engine_import_mfun( env, func ) ) goto error; + // add parent() | 1.5.1.9 (nshaheed) + func = make_new_sfun( "Shred", "parent", shred_parent ); + func->doc = "get Shred corresponding to current Shred's parent. Returns null if there is no parent Shred."; + if( !type_engine_import_sfun( env, func ) ) goto error; + // add examples if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork.ck" ) ) goto error; @@ -2528,6 +2533,13 @@ CK_DLL_MFUN( shred_cget_hintChildRegSize ) // 1.5.1.5 } +CK_DLL_SFUN( shred_parent ) // added 1.5.1.9 (nshaheed) +{ + Chuck_VM_Shred * parent = SHRED->parent; + + RETURN->v_object = parent; +} + //----------------------------------------------------------------------------- // string API //----------------------------------------------------------------------------- diff --git a/src/core/chuck_lang.h b/src/core/chuck_lang.h index 34dfb4a85..2236e7e90 100644 --- a/src/core/chuck_lang.h +++ b/src/core/chuck_lang.h @@ -169,6 +169,7 @@ CK_DLL_MFUN( shred_sourcePath ); // added 1.3.0.0 CK_DLL_MFUN( shred_sourceDir ); // added 1.3.0.0 CK_DLL_MFUN( shred_sourceDir2 ); // added 1.3.2.0 CK_DLL_SFUN( shred_fromId ); // added 1.3.2.0 +CK_DLL_SFUN( shred_parent ); // added 1.5.1.9 (nshaheed) CK_DLL_MFUN( shred_ctrl_hintChildMemSize ); // added 1.5.1.5 CK_DLL_MFUN( shred_cget_hintChildMemSize ); // added 1.5.1.5 CK_DLL_MFUN( shred_ctrl_hintChildRegSize ); // added 1.5.1.5 diff --git a/src/test/01-Basic/232-shred-parent.ck b/src/test/01-Basic/232-shred-parent.ck new file mode 100644 index 000000000..56cd2f1fe --- /dev/null +++ b/src/test/01-Basic/232-shred-parent.ck @@ -0,0 +1,18 @@ +fun void validateParent(int main_shred_id) { + me.parent().id() => int parent_id; + + if (parent_id != main_shred_id) { + <<< "FAILURE, spork~ validateParent(...)'s 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~ validateParent(me.id()); +me.yield(); // yield to let validateParent(...) run +<<< "success" >>>; \ No newline at end of file From 75208f12bf3b51b4cab9f28da48639718a51ccda Mon Sep 17 00:00:00 2001 From: nshaheed Date: Sun, 5 Nov 2023 12:14:38 -0800 Subject: [PATCH 2/5] Shred.parent() formatting changes --- examples/shred/parent.ck | 2 -- src/test/01-Basic/232-shred-parent.ck | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/shred/parent.ck b/examples/shred/parent.ck index 39682f424..cd1c36de2 100644 --- a/examples/shred/parent.ck +++ b/examples/shred/parent.ck @@ -1,5 +1,3 @@ - - // If parent.ck is the top-level shred, then me.parent() will be null. // Otherwise it will be a parent. <<< "The top-level shred's ID is:", me.id() >>>; diff --git a/src/test/01-Basic/232-shred-parent.ck b/src/test/01-Basic/232-shred-parent.ck index 56cd2f1fe..708ce5c30 100644 --- a/src/test/01-Basic/232-shred-parent.ck +++ b/src/test/01-Basic/232-shred-parent.ck @@ -15,4 +15,4 @@ if (me.parent() != null) { spork~ validateParent(me.id()); me.yield(); // yield to let validateParent(...) run -<<< "success" >>>; \ No newline at end of file +<<< "success" >>>; From d085fe8d095cbcc07a1243827f3218e07bb2af3e Mon Sep 17 00:00:00 2001 From: nshaheed Date: Sun, 5 Nov 2023 12:15:19 -0800 Subject: [PATCH 3/5] link shred/parent.ck to examples --- src/core/chuck_lang.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/chuck_lang.cpp b/src/core/chuck_lang.cpp index dec29dbda..245fd4eda 100644 --- a/src/core/chuck_lang.cpp +++ b/src/core/chuck_lang.cpp @@ -611,6 +611,7 @@ t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type ) if( !type_engine_import_add_ex( env, "shred/spork2.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2-exit.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2-remove.ck" ) ) goto error; + if( !type_engine_import_add_ex( env, "shred/parent.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/broadcast.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal4.ck" ) ) goto error; From de564c44f7e7f2dcb4bd209caa0a34ecb83a87f4 Mon Sep 17 00:00:00 2001 From: nshaheed Date: Sun, 5 Nov 2023 13:35:23 -0800 Subject: [PATCH 4/5] added Shred.ancestor() --- examples/shred/ancestor.ck | 21 +++++++++++ src/core/chuck_lang.cpp | 21 +++++++++++ src/core/chuck_lang.h | 1 + .../01-Basic/233-shred-parent-recursive.ck | 36 +++++++++++++++++++ src/test/01-Basic/234-shred-ancestor.ck | 36 +++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 examples/shred/ancestor.ck create mode 100644 src/test/01-Basic/233-shred-parent-recursive.ck create mode 100644 src/test/01-Basic/234-shred-ancestor.ck diff --git a/examples/shred/ancestor.ck b/examples/shred/ancestor.ck new file mode 100644 index 000000000..c1376c1db --- /dev/null +++ b/examples/shred/ancestor.ck @@ -0,0 +1,21 @@ +// If ancestor.ck is the top-level shred, then me.ancestor() will be null. +// Otherwise it will be a Shred. +<<< "The top-level shred's ID is:", me.id() >>>; +<<< "The top-level shred's ancestor is:", me.ancestor(), "\n" >>>; + +fun void findTheAncestor(int depth) { + if (depth <= 0) return; + + <<< "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~ findTheAncestor(depth-1); + samp => now; +} + +// Recursively spork function this 10 times. +// me.ancestor() will always point to the top-level shred. +spork~ findTheAncestor(10); +samp => now; + diff --git a/src/core/chuck_lang.cpp b/src/core/chuck_lang.cpp index 245fd4eda..37cf84757 100644 --- a/src/core/chuck_lang.cpp +++ b/src/core/chuck_lang.cpp @@ -605,6 +605,11 @@ t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type ) func->doc = "get Shred corresponding to current Shred's parent. Returns null if there is no parent Shred."; if( !type_engine_import_sfun( env, func ) ) goto error; + // add ancestor() | 1.5.1.9 (nshaheed) + func = make_new_sfun( "Shred", "ancestor", shred_ancestor ); + func->doc = "get Shred corresponding to current Shred's ancestor (the top-level shred). Returns null if the current Shred is the top-level shred."; + if( !type_engine_import_sfun( env, func ) ) goto error; + // add examples if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork.ck" ) ) goto error; @@ -612,6 +617,7 @@ t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type ) if( !type_engine_import_add_ex( env, "shred/spork2-exit.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2-remove.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/parent.ck" ) ) goto error; + if( !type_engine_import_add_ex( env, "shred/ancestor.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/broadcast.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal4.ck" ) ) goto error; @@ -2541,6 +2547,21 @@ CK_DLL_SFUN( shred_parent ) // added 1.5.1.9 (nshaheed) RETURN->v_object = parent; } + +CK_DLL_SFUN( shred_ancestor ) // added 1.5.1.9 (nshaheed) +{ + Chuck_VM_Shred * curr = SHRED; + + // if there is no ancestor at all, return null + if (curr->parent == nullptr) return; + + while (curr->parent != nullptr) { + curr = curr->parent; + } + + RETURN->v_object = curr; +} + //----------------------------------------------------------------------------- // string API //----------------------------------------------------------------------------- diff --git a/src/core/chuck_lang.h b/src/core/chuck_lang.h index 2236e7e90..ccdec43b0 100644 --- a/src/core/chuck_lang.h +++ b/src/core/chuck_lang.h @@ -170,6 +170,7 @@ CK_DLL_MFUN( shred_sourceDir ); // added 1.3.0.0 CK_DLL_MFUN( shred_sourceDir2 ); // added 1.3.2.0 CK_DLL_SFUN( shred_fromId ); // added 1.3.2.0 CK_DLL_SFUN( shred_parent ); // added 1.5.1.9 (nshaheed) +CK_DLL_SFUN( shred_ancestor ); // added 1.5.1.9 (nshaheed) CK_DLL_MFUN( shred_ctrl_hintChildMemSize ); // added 1.5.1.5 CK_DLL_MFUN( shred_cget_hintChildMemSize ); // added 1.5.1.5 CK_DLL_MFUN( shred_ctrl_hintChildRegSize ); // added 1.5.1.5 diff --git a/src/test/01-Basic/233-shred-parent-recursive.ck b/src/test/01-Basic/233-shred-parent-recursive.ck new file mode 100644 index 000000000..ae3c48b19 --- /dev/null +++ b/src/test/01-Basic/233-shred-parent-recursive.ck @@ -0,0 +1,36 @@ +// 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); + +samp => now; + +if (count != recursive_calls) { + <<< "FAILURE, incorrect number of recursive calls made. Got", count, "want", recursive_calls >>>; + me.exit(); +} +<<< "success" >>>; + diff --git a/src/test/01-Basic/234-shred-ancestor.ck b/src/test/01-Basic/234-shred-ancestor.ck new file mode 100644 index 000000000..6cd70f6c8 --- /dev/null +++ b/src/test/01-Basic/234-shred-ancestor.ck @@ -0,0 +1,36 @@ +// Validate that Shred.ancestor() correctly gets the top-level shred through multiple layers of recursion. + +0 => int count; // count the number of recursive calls + +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); + } + samp => now; +} + +// this should be the top-level shred (and has no ancestor) +if (me.ancestor() != null) { + <<< "FAILURE, the top-level shred should have no ancestor" >>>; + me.exit(); +} + +100 => int recursive_calls; +spork~ validateAncestor(me.id(), recursive_calls); + +samp => now; + +if (count != recursive_calls) { + <<< "FAILURE, incorrect number of recursive calls made. Got", count, "want", recursive_calls >>>; + me.exit(); +} +<<< "success" >>>; + From 5e5abc45f595f60538e4553502337ccffeb803e3 Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Mon, 6 Nov 2023 00:36:42 -0800 Subject: [PATCH 5/5] update shred.ancestor() to always return top-level shred; formatting --- examples/shred/ancestor.ck | 40 +++++++++++------ examples/shred/parent.ck | 24 +++++++---- src/core/chuck_lang.cpp | 27 +++++++----- src/test/01-Basic/232-shred-parent.ck | 29 +++++++++---- .../01-Basic/233-shred-parent-recursive.ck | 39 +++++++++++------ src/test/01-Basic/234-shred-ancestor.ck | 43 ++++++++++++------- 6 files changed, 130 insertions(+), 72 deletions(-) diff --git a/examples/shred/ancestor.ck b/examples/shred/ancestor.ck index c1376c1db..a14740435 100644 --- a/examples/shred/ancestor.ck +++ b/examples/shred/ancestor.ck @@ -1,21 +1,33 @@ -// If ancestor.ck is the top-level shred, then me.ancestor() will be null. -// Otherwise it will be a Shred. -<<< "The top-level shred's ID is:", me.id() >>>; -<<< "The top-level shred's ancestor is:", me.ancestor(), "\n" >>>; +// 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 -fun void findTheAncestor(int depth) { - if (depth <= 0) return; +// 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" >>>; - <<< "The sporked shred's ID is:", me.id() >>>; +// 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" >>>; + <<< "the sporked shred's ancestor ID is:", me.ancestor().id(), "\n" >>>; - spork~ findTheAncestor(depth-1); - samp => now; + // 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() will always point to the top-level shred. -spork~ findTheAncestor(10); -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; diff --git a/examples/shred/parent.ck b/examples/shred/parent.ck index cd1c36de2..f0fd376d0 100644 --- a/examples/shred/parent.ck +++ b/examples/shred/parent.ck @@ -1,13 +1,19 @@ -// If parent.ck is the top-level shred, then me.parent() will be null. -// Otherwise it will be a parent. -<<< "The top-level shred's ID is:", me.id() >>>; -<<< "The top-level shred's parent is:", me.parent() >>>; +// 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 -fun void findTheParent() { +<<< "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() >>>; + <<< "the sporked shred's ID is:", me.id() >>>; + <<< "the sporked shred's parent ID is:", me.parent().id() >>>; } -spork~ findTheParent(); -me.yield(); // yield to let validateParent(...) run +// spork a child shred +spork ~ findTheParent(); + +// yield to let validateParent(...) run +me.yield(); diff --git a/src/core/chuck_lang.cpp b/src/core/chuck_lang.cpp index 37cf84757..6733ff1a7 100644 --- a/src/core/chuck_lang.cpp +++ b/src/core/chuck_lang.cpp @@ -602,25 +602,25 @@ t_CKBOOL init_class_shred( Chuck_Env * env, Chuck_Type * type ) // add parent() | 1.5.1.9 (nshaheed) func = make_new_sfun( "Shred", "parent", shred_parent ); - func->doc = "get Shred corresponding to current Shred's parent. Returns null if there is no parent Shred."; + func->doc = "get the calling shred's parent shred (i.e., the shred that sporked the calling shred). Returns null if there is no parent Shred. (Related: see Shred.ancestor())"; if( !type_engine_import_sfun( env, func ) ) goto error; // add ancestor() | 1.5.1.9 (nshaheed) func = make_new_sfun( "Shred", "ancestor", shred_ancestor ); - func->doc = "get Shred corresponding to current Shred's ancestor (the top-level shred). Returns null if the current Shred is the top-level shred."; + func->doc = "get the calling shred's \"ancestor\" shred (i.e., the top-level shred). Returns itself if the calling shred is the top-level shred. (Related: see Shred.parent())"; if( !type_engine_import_sfun( env, func ) ) goto error; // add examples - if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2-exit.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "shred/spork2-remove.ck" ) ) goto error; - if( !type_engine_import_add_ex( env, "shred/parent.ck" ) ) goto error; - if( !type_engine_import_add_ex( env, "shred/ancestor.ck" ) ) goto error; + if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/broadcast.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal.ck" ) ) goto error; if( !type_engine_import_add_ex( env, "event/signal4.ck" ) ) goto error; + if( !type_engine_import_add_ex( env, "shred/parent.ck" ) ) goto error; + if( !type_engine_import_add_ex( env, "shred/ancestor.ck" ) ) goto error; // end the class import type_engine_import_class_end( env ); @@ -2542,23 +2542,28 @@ CK_DLL_MFUN( shred_cget_hintChildRegSize ) // 1.5.1.5 CK_DLL_SFUN( shred_parent ) // added 1.5.1.9 (nshaheed) { + // get the parent Chuck_VM_Shred * parent = SHRED->parent; - + // set return value RETURN->v_object = parent; } CK_DLL_SFUN( shred_ancestor ) // added 1.5.1.9 (nshaheed) { + // current shred Chuck_VM_Shred * curr = SHRED; - // if there is no ancestor at all, return null - if (curr->parent == nullptr) return; - - while (curr->parent != nullptr) { - curr = curr->parent; + // iterate up until parent is null; it's possible that + // ancestor() returns the calling shred, if called on + // the top-level "ancestor" shred + while( curr->parent != NULL ) + { + // set curr as parent + curr = curr->parent; } + // set return value RETURN->v_object = curr; } diff --git a/src/test/01-Basic/232-shred-parent.ck b/src/test/01-Basic/232-shred-parent.ck index 708ce5c30..453e31211 100644 --- a/src/test/01-Basic/232-shred-parent.ck +++ b/src/test/01-Basic/232-shred-parent.ck @@ -1,18 +1,29 @@ -fun void validateParent(int main_shred_id) { +// test shred.parent() + +// check shred parent +fun void validateParent(int main_shred_id) +{ + // id of parent me.parent().id() => int parent_id; - if (parent_id != main_shred_id) { - <<< "FAILURE, spork~ validateParent(...)'s parent shred should be the top-level shred" >>>; - me.exit(); + // 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(); +if( me.parent() != null ) +{ + <<< "FAILURE, the top-level shred should have no parent" >>>; + me.exit(); } -spork~ validateParent(me.id()); -me.yield(); // yield to let validateParent(...) run +// spork +spork ~ validateParent( me.id() ); +// yield to let validateParent(...) run +me.yield(); +// print <<< "success" >>>; diff --git a/src/test/01-Basic/233-shred-parent-recursive.ck b/src/test/01-Basic/233-shred-parent-recursive.ck index ae3c48b19..66331ce1e 100644 --- a/src/test/01-Basic/233-shred-parent-recursive.ck +++ b/src/test/01-Basic/233-shred-parent-recursive.ck @@ -1,36 +1,47 @@ -// Validate that Shred.parent() correctly gets the parent shred through multiple layers of recursion. +// 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) { +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( want_parent != got_parent ) + { + <<< "FAILURE, spork~ validateParent(...)'s parent shred is not matching" >>>; + me.exit(); } - if (depth > 1) { - spork~ validateParent(me.id(), depth-1); + 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(); +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); +spork~ validateParent( me.id(), recursive_calls ); +// pass time (or yield) to let children shreds run samp => now; -if (count != recursive_calls) { - <<< "FAILURE, incorrect number of recursive calls made. Got", count, "want", recursive_calls >>>; - me.exit(); +// check count +if( count != recursive_calls ) +{ + <<< "FAILURE: incorrect number of recursive calls made. got:", + count, "want:", recursive_calls >>>; + me.exit(); } + +// done <<< "success" >>>; diff --git a/src/test/01-Basic/234-shred-ancestor.ck b/src/test/01-Basic/234-shred-ancestor.ck index 6cd70f6c8..dbf7665d2 100644 --- a/src/test/01-Basic/234-shred-ancestor.ck +++ b/src/test/01-Basic/234-shred-ancestor.ck @@ -1,36 +1,49 @@ -// Validate that Shred.ancestor() correctly gets the top-level shred through multiple layers of recursion. +// validate that Shred.ancestor() correctly gets the top-level +// shred through multiple layers of recursion -0 => int count; // count the number of recursive calls +// count the number of recursive calls +0 => int count; -fun void validateAncestor(int want_ancestor, int depth) { +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( 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); + 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() != null) { - <<< "FAILURE, the top-level shred should have no ancestor" >>>; - me.exit(); +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); +spork ~ validateAncestor( me.id(), recursive_calls ); +// let time pass to let children shreds run samp => now; -if (count != recursive_calls) { - <<< "FAILURE, incorrect number of recursive calls made. Got", count, "want", recursive_calls >>>; - me.exit(); +// check count +if( count != recursive_calls ) +{ + <<< "FAILURE: incorrect number of recursive calls made.", + "got:", count, "want:", recursive_calls >>>; + me.exit(); } + +// done <<< "success" >>>;