- Timestamp:
- Jan 6, 2024, 8:14:15 AM (11 months ago)
- Branches:
- master
- Children:
- 4d689e2
- Parents:
- 40002c5
- Location:
- tests/exceptions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/exceptions/hotpotato.cfa
r40002c5 rf0c9c9b 5 5 6 6 struct Potato { 7 8 unsigned int deadline;// when timer goes off9 unsigned int timer;// up counter to deadline7 PRNG & prng; 8 unsigned int deadline; // when timer goes off 9 unsigned int timer; // up counter to deadline 10 10 }; // Potato 11 11 … … 16 16 &potato.prng = &prng; 17 17 reset( potato, maxTicks ); 18 } // Potato18 } // Potato 19 19 20 20 coroutine Player { 21 22 int id;// player identity23 Potato & potato;// potato being tossed24 Player * partner[2];// left and right player21 PRNG & prng; 22 int id; // player identity 23 Potato & potato; // potato being tossed 24 Player * partner[2]; // left and right player 25 25 }; // Player 26 26 … … 29 29 player.id = id; 30 30 &player.potato = &potato; 31 } // Player31 } // Player 32 32 33 33 Player & umpire; … … 39 39 40 40 void reset( Potato & potato, unsigned int maxTicks ) with(potato) { 41 41 if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional 42 42 deadline = prng( prng, 1, maxTicks ); 43 43 timer = 0; … … 60 60 enum { LEFT = 0, RIGHT = 1 }; 61 61 62 static void vote( Player & player, Election & election ) { 63 62 static void vote( Player & player, Election & election ) { // cause partner to vote 63 resumeAt( player, election ); 64 64 resume( player ); 65 65 } // vote … … 76 76 77 77 void main( Player & player ) with(player) { 78 suspend;// return immediately after establishing starter78 suspend; // return immediately after establishing starter 79 79 try { 80 81 poll();// check for non-local exceptions before proceeding82 83 84 85 86 87 88 countdown( potato );// player is eliminated if countdown() returned true89 90 91 92 resume( *partner[ side ] );// random toss left/right93 80 for ( ;; ) { 81 poll(); // check for non-local exceptions before proceeding 82 83 if ( partner[LEFT] == &player ) { // stop when only one player 84 sout | id | " wins the Match!"; 85 return; 86 } // exit 87 88 countdown( potato ); // player is eliminated if countdown() returned true 89 90 size_t side = prng( prng, 2 ); 91 sout | id | " -> " | nonl; 92 resume( *partner[ side ] ); // random toss left/right 93 } // for 94 94 } catchResume( Terminate * v ) { 95 95 v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node 96 96 v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT]; 97 97 delete( v->victim ); 98 98 reset( potato ); 99 99 sout | "U " | nonl; // start new game … … 102 102 sout | "election"; 103 103 sout | " -> " | id | nonl; 104 if ( id > getId( umpire ) ) &umpire = &player; 104 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far 105 105 vote( *partner[RIGHT], *election ); 106 106 } catchResume ( Explode * ) { 107 108 109 id = -1;// remove from election110 vote( *partner[RIGHT], ExceptionInst( Election ) );// start election111 112 113 114 115 resume( umpire );// resume umpire to terminate this player116 assert( false );// no return117 107 sout | id | " is eliminated"; 108 if ( &player == &umpire ) { 109 id = -1; // remove from election 110 vote( *partner[RIGHT], ExceptionInst( Election ) ); // start election 111 try { poll(); } catchResume( Election * election ) {} // handle end of election 112 sout | " : umpire " | getId( umpire ); 113 } // if 114 resumeAt( umpire, ExceptionInst( Terminate, &player ) ); 115 resume( umpire ); // resume umpire to terminate this player 116 assert( false ); // no return 117 } // try 118 118 } // main 119 119 … … 151 151 case 1: ; // defaults 152 152 default: // too many arguments 153 throw ExceptionInst( cmd_error );153 throw ExceptionInst( cmd_error ); 154 154 } // choose 155 155 } catch( exception_t * ) { // catch any 156 156 exit | "Usage: " | argv[0] 157 158 159 157 | " [ games (>=0) | 'd' (default " | DefaultGames 158 | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers 159 | ") [ seed (>0) | 'd' (random) ] ] ]"; 160 160 } // try 161 161 sout | numGames | numPlayers | seed; -
tests/exceptions/hotpotato_checked.cfa
r40002c5 rf0c9c9b 5 5 6 6 struct Potato { 7 8 unsigned int deadline;// when timer goes off9 unsigned int timer;// up counter to deadline7 PRNG & prng; 8 unsigned int deadline; // when timer goes off 9 unsigned int timer; // up counter to deadline 10 10 }; // Potato 11 11 … … 16 16 &potato.prng = &prng; 17 17 reset( potato, maxTicks ); 18 } // Potato18 } // Potato 19 19 20 20 coroutine Player { 21 22 int id;// player identity23 Potato & potato;// potato being tossed24 Player * partner[2];// left and right player21 PRNG & prng; 22 int id; // player identity 23 Potato & potato; // potato being tossed 24 Player * partner[2]; // left and right player 25 25 }; // Player 26 26 … … 29 29 player.id = id; 30 30 &player.potato = &potato; 31 } // Player31 } // Player 32 32 33 33 Player & umpire; … … 39 39 40 40 void reset( Potato & potato, unsigned int maxTicks ) with(potato) { 41 41 if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional 42 42 deadline = prng( prng, 1, maxTicks ); 43 43 timer = 0; … … 66 66 static void terminate( Player & player ) { // resume umpire 67 67 resume( player ); 68 69 68 checked_poll(); 69 sout | "THIS SHOULD NOT BE REACHED"; 70 70 } // terminate 71 71 … … 74 74 partner[RIGHT] = &rp; 75 75 resume( player ); // establish main as starter for termination 76 76 checked_poll(); 77 77 } // init 78 78 … … 82 82 83 83 void toss( Player & player ) { // tossed the potato 84 84 resume( player ); 85 85 checked_poll(); 86 86 } // toss … … 88 88 void main( Player & player ) with(player) { 89 89 try { 90 enable_ehm();// allow delivery of nonlocal exceptions91 suspend;// return immediately after establishing starter92 93 94 95 96 97 98 99 100 101 countdown( potato );// player is eliminated if countdown() returned true102 103 104 105 toss( *partner[ side ] );// random toss left/right106 107 90 enable_ehm(); // allow delivery of nonlocal exceptions 91 suspend; // return immediately after establishing starter 92 checked_poll(); 93 94 for ( ;; ) { 95 checked_poll(); 96 if ( partner[LEFT] == &player ) { // stop when only one player 97 sout | id | " wins the Match!"; 98 return; 99 } // exit 100 101 countdown( potato ); // player is eliminated if countdown() returned true 102 103 size_t side = prng( prng, 2 ); 104 sout | id | " -> " | nonl; 105 toss( *partner[ side ] ); // random toss left/right 106 } // for 107 disable_ehm(); 108 108 } catchResume( Terminate * v ) { 109 109 v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node 110 110 v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT]; 111 111 delete( v->victim ); 112 112 reset( potato ); 113 113 sout | "U " | nonl; // start new game … … 116 116 sout | "election"; 117 117 sout | " -> " | id | nonl; 118 if ( id > getId( umpire ) ) &umpire = &player; 119 120 disable_ehm();// disable ehm since we can't handle execption thrown in vote here and want to poll later118 if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far 119 resumeAt( *partner[RIGHT], *election ); 120 disable_ehm(); // disable ehm since we can't handle execption thrown in vote here and want to poll later 121 121 vote( *partner[RIGHT] ); 122 enable_ehm();// enable after vote122 enable_ehm(); // enable after vote 123 123 } catchResume( Explode * ) { 124 125 126 127 id = -1;// remove from election128 129 vote( *partner[RIGHT] );// start election130 131 132 133 134 135 136 137 assert( false );// no return138 124 sout | id | " is eliminated"; 125 if ( &player == &umpire ) { 126 try { 127 id = -1; // remove from election 128 resumeAt( *partner[RIGHT], ExceptionInst( Election ) ); 129 vote( *partner[RIGHT] ); // start election 130 checked_poll(); 131 } catchResume( Election * election ) { 132 sout | " : umpire " | getId( umpire ); 133 } // try 134 } // if 135 resumeAt( umpire, ExceptionInst( Terminate, &player ) ); 136 terminate( umpire ); 137 assert( false ); // no return 138 } // try 139 139 } // main 140 140 … … 172 172 case 1: ; // defaults 173 173 default: // too many arguments 174 throw ExceptionInst( cmd_error );174 throw ExceptionInst( cmd_error ); 175 175 } // choose 176 176 } catch( exception_t * ) { // catch any 177 177 exit | "Usage: " | argv[0] 178 179 180 178 | " [ games (>=0) | 'd' (default " | DefaultGames 179 | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers 180 | ") [ seed (>0) | 'd' (random) ] ] ]"; 181 181 } // try 182 182 sout | numGames | numPlayers | seed;
Note: See TracChangeset
for help on using the changeset viewer.