Changes in / [8655363:b262cb3]


Ignore:
Location:
tests/exceptions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tests/exceptions/hotpotato.cfa

    r8655363 rb262cb3  
    55
    66struct Potato {
    7         PRNG & prng;
    8         unsigned int deadline;                                                          // when timer goes off
    9         unsigned int timer;                                                                     // up counter to deadline
     7        PRNG & prng;
     8        unsigned int deadline;                                                          // when timer goes off
     9        unsigned int timer;                                                                     // up counter to deadline
    1010}; // Potato
    1111
     
    1616        &potato.prng = &prng;
    1717        reset( potato, maxTicks );
    18                 } // Potato
     18} // Potato
    1919
    2020coroutine Player {
    21         PRNG & prng;
    22         int id;                                                                                         // player identity
    23         Potato & potato;                                                                        // potato being tossed
    24         Player * partner[2];                                                            // left and right player
     21        PRNG & prng;
     22        int id;                                                                                         // player identity
     23        Potato & potato;                                                                        // potato being tossed
     24        Player * partner[2];                                                            // left and right player
    2525}; // Player
    2626
     
    2929        player.id = id;
    3030        &player.potato = &potato;
    31                 } // Player
     31} // Player
    3232
    3333Player & umpire;
     
    3939
    4040void reset( Potato & potato, unsigned int maxTicks ) with(potato) {
    41         if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
     41  if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
    4242        deadline = prng( prng, 1, maxTicks );
    4343        timer = 0;
     
    6060enum { LEFT = 0, RIGHT = 1 };
    6161
    62 static void vote( Player & player, Election & election ) { // cause partner to vote
    63         resumeAt( player, election );
     62static void vote( Player & player, Election & election ) {                                      // cause partner to vote
     63    resumeAt( player, election );
    6464        resume( player );
    6565} // vote
     
    7676
    7777void main( Player & player ) with(player) {
    78         suspend;                                                                                        // return immediately after establishing starter
     78    suspend;                                                                    // return immediately after establishing starter
    7979        try {
    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
     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
    9494        } catchResume( Terminate * v ) {
    9595                v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node
    9696                v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT];
    97                 delete( v->victim );
     97        delete( v->victim );
    9898                reset( potato );
    9999                sout | "U " | nonl;                                                             // start new game
     
    102102                sout | "election";
    103103                sout | " -> " | id | nonl;
    104                 if ( id > getId( umpire ) ) &umpire = &player;  // set umpire to highest id so far
     104                if ( id > getId( umpire ) ) &umpire = &player; // set umpire to highest id so far
    105105                vote( *partner[RIGHT], *election );
    106106        } catchResume ( Explode * ) {
    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
     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
    118118} // main
    119119
     
    151151                  case 1: ;                                                                             // defaults
    152152                  default:                                                                              // too many arguments
    153                           throw ExceptionInst( cmd_error );
     153                        throw ExceptionInst( cmd_error );
    154154                } // choose
    155155        } catch( exception_t * ) {                                                      // catch any
    156156                exit | "Usage: " | argv[0]
    157                         | " [ games (>=0) | 'd' (default " | DefaultGames
    158                         | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
    159                         | ") [ seed (>0) | 'd' (random) ] ] ]";
     157                         | " [ games (>=0) | 'd' (default " | DefaultGames
     158                         | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
     159                         | ") [ seed (>0) | 'd' (random) ] ] ]";
    160160        } // try
    161161        sout | numGames | numPlayers | seed;
  • tests/exceptions/hotpotato_checked.cfa

    r8655363 rb262cb3  
    55
    66struct Potato {
    7         PRNG & prng;
    8         unsigned int deadline;                                                          // when timer goes off
    9         unsigned int timer;                                                                     // up counter to deadline
     7        PRNG & prng;
     8        unsigned int deadline;                                                          // when timer goes off
     9        unsigned int timer;                                                                     // up counter to deadline
    1010}; // Potato
    1111
     
    1616        &potato.prng = &prng;
    1717        reset( potato, maxTicks );
    18                 } // Potato
     18} // Potato
    1919
    2020coroutine Player {
    21         PRNG & prng;
    22         int id;                                                                                         // player identity
    23         Potato & potato;                                                                        // potato being tossed
    24         Player * partner[2];                                                            // left and right player
     21        PRNG & prng;
     22        int id;                                                                                         // player identity
     23        Potato & potato;                                                                        // potato being tossed
     24        Player * partner[2];                                                            // left and right player
    2525}; // Player
    2626
     
    2929        player.id = id;
    3030        &player.potato = &potato;
    31                 } // Player
     31} // Player
    3232
    3333Player & umpire;
     
    3939
    4040void reset( Potato & potato, unsigned int maxTicks ) with(potato) {
    41         if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
     41  if ( maxTicks < 2 ) abort( "Hot Potato initialized with less than 2 ticks" ); // optional
    4242        deadline = prng( prng, 1, maxTicks );
    4343        timer = 0;
     
    6666static void terminate( Player & player ) {                              // resume umpire
    6767        resume( player );
    68         checked_poll();
    69         sout | "THIS SHOULD NOT BE REACHED";
     68    checked_poll();
     69    sout | "THIS SHOULD NOT BE REACHED";
    7070} // terminate
    7171
     
    7474        partner[RIGHT] = &rp;
    7575        resume( player );                                                                       // establish main as starter for termination
    76         checked_poll();
     76    checked_poll();
    7777} // init
    7878
     
    8282
    8383void toss( Player & player ) {                                                  // tossed the potato
    84         resume( player );
     84    resume( player );
    8585        checked_poll();
    8686} // toss
     
    8888void main( Player & player ) with(player) {
    8989        try {
    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();
     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();
    108108        } catchResume( Terminate * v ) {
    109109                v->victim->partner[LEFT]->partner[RIGHT] = v->victim->partner[RIGHT]; // unlink node
    110110                v->victim->partner[RIGHT]->partner[LEFT] = v->victim->partner[LEFT];
    111                 delete( v->victim );
     111        delete( v->victim );
    112112                reset( potato );
    113113                sout | "U " | nonl;                                                             // start new game
     
    116116                sout | "election";
    117117                sout | " -> " | id | nonl;
    118                 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
     118                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
    121121                vote( *partner[RIGHT] );
    122                 enable_ehm();                                                                   // enable after vote
     122        enable_ehm();               // enable after vote
    123123        } catchResume( Explode * ) {
    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
     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
    139139} // main
    140140
     
    172172                  case 1: ;                                                                             // defaults
    173173                  default:                                                                              // too many arguments
    174                           throw ExceptionInst( cmd_error );
     174                        throw ExceptionInst( cmd_error );
    175175                } // choose
    176176        } catch( exception_t * ) {                                                      // catch any
    177177                exit | "Usage: " | argv[0]
    178                         | " [ games (>=0) | 'd' (default " | DefaultGames
    179                         | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
    180                         | ") [ seed (>0) | 'd' (random) ] ] ]";
     178                         | " [ games (>=0) | 'd' (default " | DefaultGames
     179                         | ") [ players (>=2) | 'd' (random " | MinNoPlayers | "-" | MaxNoPlayers
     180                         | ") [ seed (>0) | 'd' (random) ] ] ]";
    181181        } // try
    182182        sout | numGames | numPlayers | seed;
Note: See TracChangeset for help on using the changeset viewer.