#include #include #include #include #include "lottery.h" /* This program tests the functionality of the setLotteryTickets system call. It also tests to make sure the number of tickets assigned in the system is properly updated by process creation and deletion. Also, it is important to notice that some tests are dependent on the success of previous tests.*/ void main( int argc, char *argv ) { int rval; /* return value from system calls */ int fail; /* indicates one or more tests failed */ int child; /* child pid number */ /* initialize variables */ fail = 0; printf( "\n" ); /* ------------------------------------------------------------- * TEST 1 * ------------------------------------------------------------- * TASK: Assign 10 tickets to current process. * RVAL: Assuming there are 10 tickets left in the system, the * return value should be 10. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( getpid(), 10 ) ) == 10 ) { printf( " test 01...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 01...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 2 * ------------------------------------------------------------- * TASK: Assign 10 tickets to nonexistent process. * RVAL: The return value should be -1 to indicate failure. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( -1, 10 ) ) == -1 ) { printf( " test 02...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 02...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 3 * ------------------------------------------------------------- * TASK: Assign -10 tickets to current process. * RVAL: The return value should be -1 to indicate failure. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( getpid(), -10 ) ) == -1 ) { printf( " test 03...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 03...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 4 * ------------------------------------------------------------- * TASK: Assign 256 tickets to current process. * RVAL: The return value should be less than 256, indicating * that not all tickets could be assigned. This is because * other background process (such as init and sh) should * account for a few tickets in the system. Also, the * return value should NOT be -1, and should be the total * number of tickets assigned to the current process. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( getpid(), 256 ) ) < 256 && ( rval > 0 ) ) { printf( " test 04...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 04...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 5 * ------------------------------------------------------------- * TASK: Attempt to fork. * RVAL: The return value should be -1 to indicate failure. If * test 4 was successful, there should be no more tickets * available in the system. * ------------------------------------------------------------- */ if( ( rval = fork() ) == -1 ) { printf( " test 05...... [ pass ]\n" ); } else if( rval != 0 ) { fail = fail + 1; printf( " test 05...... [ fail ] (%i)\n", rval ); } else { return; } /* ------------------------------------------------------------- * TEST 6 * ------------------------------------------------------------- * TASK: Assign 10 tickets to current process. * RVAL: The return value should be 10, decreasing the number of * tickets assigned. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( getpid(), 10 ) ) == 10 ) { printf( " test 06...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 06...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 7 AND 8 * ------------------------------------------------------------- * TASK: Attempt to fork. If successful, assign 256 tickets to * the child process. * RVAL: If test 6 was successful, the return value should be 0 * or a positive number. For the second part, the reutrn * value should be a number less than 256, but greater * than zero. * ------------------------------------------------------------- */ if( ( rval = fork() ) != -1 ) { if( rval != 0 ) /* parent process */ { printf( " test 07...... [ pass ]\n" ); child = rval; /* used in next test */ sleep( 5 ); } else /* child process */ { if( ( rval = setLotteryTickets( getpid(), 256 ) ) < 256 && ( rval > 0 ) ) { printf( " test 08...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 08...... [ fail ] (%i)\n", rval ); } while( 1 ) { sleep( 1 ); } /* used in next test */ } } else { fail = fail + 2; printf( " test 07...... [ fail ] (%i)\n", rval ); printf( " test 08...... [ fail ] (n/a)\n" ); } /* ------------------------------------------------------------- * TEST 9 * ------------------------------------------------------------- * TASK: Assign 256 tickets to current process. * RVAL: Assuming the previous tests were successful, the return * value should be 10 (the previous amount of tickets this * process was assigned). This is because all available * tickets should be assigned. * ------------------------------------------------------------- */ if( ( rval = setLotteryTickets( getpid(), 256 ) ) == 10 ) { printf( " test 09...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 09...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * TEST 10 * ------------------------------------------------------------- * TASK: Kill the child process, and assign 256 tickets to the * current process. * RVAL: Assuming the previous tests were successful, the return * value should be less than 256, but greater than 10. * This is because the system should reclaim those tickets * previously assigned to the child process. * ------------------------------------------------------------- */ if( ( rval = kill( child, SIGKILL ) ) == 0 && ( rval = setLotteryTickets( getpid() , 256 ) ) < 256 && ( rval > 10 ) ) { printf( " test 10...... [ pass ]\n" ); } else { fail = fail + 1; printf( " test 10...... [ fail ] (%i)\n", rval ); } /* ------------------------------------------------------------- * OUTPUT FINAL RESULT * ------------------------------------------------------------- */ if( fail == 0 ) { printf( "\n final result: [ pass ]\n\n" ); } else { printf( "\n final result: [ fail ] (%i)\n\n", fail ); } return; }