int main() { srand(time(NULL)); //new gameState struct gameState * randomState = malloc(sizeof(struct gameState)); int i; int numPlayers; int kingdomCards[10]; int randomSeed; int firstRun = 1; int returnValue; printf("Unit test 1\r\n"); printf("Conducting %d random trials.\r\n", UNITTEST1_TRIALS); int foundErrors = 0; int trials; for (trials = 0; trials < UNITTEST1_TRIALS; trials++ ){ //fuzz the state fuzzState(randomState); //semi-randomize inputs (within reason) numPlayers = randomNumPlayers(); unittest1_initiateKingdomCards(kingdomCards); randomSeed = rand(); //run function and store results returnValue = initializeGame(numPlayers, kingdomCards, randomSeed, randomState); //test outputs: if (firstRun == 1) TestRandom(); if (returnValue != -1) { foundErrors -= checkGameState(randomState, returnValue, numPlayers, kingdomCards); //subtracts a negative to add } firstRun = 0; } printf("Unit Test 1 Complete. I found %d errors\r\n", foundErrors); return 0; }
main(int argc, char * argv[]) { int unit4_test_trials = 10000; if (argc > 1) { unit4_test_trials = atoi(argv[1]); if (unit4_test_trials < 1) { printf("Usage: unittest3 <trials>\r\n"); exit(1); } } srand(time(NULL)); //new gameState struct gameState * gs = malloc(sizeof(struct gameState)); struct gameState * stateCopy = malloc(sizeof(struct gameState)); int i; int trial; int returnValue; int numberOfErrors = 0; int playerNum; printf("Unit test 4\r\n"); printf("Conducting %d random trials.\r\n", unit4_test_trials); for (trial = 0; trial < unit4_test_trials; trial++) { fuzzState(gs); //semi-randomize inputs (within reason) playerNum = randomNumber(2,MAX_PLAYERS); gs->deckCount[playerNum] = randomNumber(0,MAX_DECK); gs->handCount[playerNum] = randomNumber(0,MAX_HAND); gs->discardCount[playerNum] = randomNumber(0,MAX_DECK); for (i = 0; i < gs->deckCount[playerNum]; i++) { gs->deck[playerNum][i] = randomNumber(0, treasure_map); } for (i = 0; i < gs->handCount[playerNum]; i++) { gs->hand[playerNum][i] = randomNumber(0, treasure_map); } for (i = 0; i < gs->discardCount[playerNum]; i++) { gs->discard[playerNum][i] = randomNumber(0, treasure_map); } //for later comparison memcpy(stateCopy, gs, sizeof(struct gameState)); //perform function returnValue = scoreFor(playerNum, gs); //compare states if (memcmp( gs, stateCopy, sizeof(struct gameState)) != 0) { printf("Modification detected in state!\r\n"); numberOfErrors++; } else if (returnValue != -9999) { // Run check if (calcScore(playerNum, gs) != returnValue) { printf("Mismatch in score calculations. scoreFor(): %d, Expected: %d\r\n", returnValue, calcScore(playerNum, gs)); numberOfErrors++; } } } printf("Unit Test 4 Complete\r\n"); printf("Number of errors found: %d\r\n", numberOfErrors); free(gs); free(stateCopy); return 0; }
main(int argc, char * argv[]) { int unit3_test_trials = 10000; if (argc > 1) { unit3_test_trials = atoi(argv[1]); if (unit3_test_trials < 1) { printf("Usage: unittest3 <trials>\r\n"); exit(1); } } srand(time(NULL)); //new gameState struct gameState * gs = malloc(sizeof(struct gameState)); struct gameState * stateCopy = malloc(sizeof(struct gameState)); int i; int trial; int returnValue; int numberOfErrors = 0; printf("Unit test 3\r\n"); printf("Conducting %d random trials.\r\n", unit3_test_trials); for (trial = 0; trial < unit3_test_trials; trial++) { fuzzState(gs); //semi-randomize inputs (within reason) int zeroSupplyProvince = percentChanceIsOne(5); int zeroSuppliesInThree = percentChanceIsOne(5); if (zeroSupplyProvince == 1){ gs->supplyCount[province] = 0; } if (zeroSuppliesInThree == 1){ for (i = 0; i < 3; i++) { gs->supplyCount[rand()%(treasure_map+1)] = 0; } } //for later comparison memcpy(stateCopy, gs, sizeof(struct gameState)); //perform function returnValue = isGameOver(gs); //compare states if (memcmp( gs, stateCopy, sizeof(struct gameState)) != 0) { printf("Modification detected in state!\r\n"); numberOfErrors++; } else { if ((returnValue != 1) && ((depletedSupplies(gs) >= 3) || (gs->supplyCount[province] == 0))) { printf("State meets criteria for game over, but game over not declared!\r\n"); printf("returnValue = %d, depletedSupplies = %d, supplyCount[province] = %d\r\n", returnValue, depletedSupplies(gs), gs->supplyCount[province] ); numberOfErrors++; } if ((returnValue == 1) && ((depletedSupplies(gs) < 3) && (gs->supplyCount[province] != 0))) { printf("Game over declared, but state does not meet criteria!\r\n"); printf("returnValue = %d, depletedSupplies = %d, supplyCount[province] = %d\r\n", returnValue, depletedSupplies(gs), gs->supplyCount[province] ); numberOfErrors++; } } } printf("Unit Test 3 Complete\r\n"); printf("Number of errors found: %d\r\n", numberOfErrors); free(gs); free(stateCopy); return 0; }
int main(int argc, char * argv[]) { int test_trials = 10000; if (argc > 1) { test_trials = atoi(argv[1]); if (test_trials < 1) { printf("Usage: cardtest1 <trials>\r\n"); exit(1); } } srand(time(NULL)); //new gameState struct gameState * gs = malloc(sizeof(struct gameState)); struct gameState * stateCopy = malloc(sizeof(struct gameState)); int trial; int returnValue; int numberOfErrors = 0; int playerNum; int emptyHand; int emptyDiscard; int emptyDeck; int emptyThree; //not enough cards? int handPos; int drawCards; printf("Card Test 1\r\n"); printf("Conducting %d random trials.\r\n", test_trials); for (trial = 0; trial < test_trials; trial++) { printf("TRIAL %d\r\n", trial); fuzzState(gs); //semi-randomize inputs (within reason) playerNum = randomNumber(2, MAX_PLAYERS) - 2; emptyDeck = percentChanceIsOne(5); emptyDiscard = percentChanceIsOne(5); emptyHand = percentChanceIsOne(5); emptyThree = percentChanceIsOne(1); if (emptyDeck == 1 || emptyThree == 1) { gs->deckCount[playerNum] = 0; } else { gs->deckCount[playerNum] = randomNumber(1, 300); } if (emptyHand == 1 || emptyThree == 1) { gs->handCount[playerNum] = 1; //leave room for Smithy card } else { gs->handCount[playerNum] = randomNumber(2, 300); } if (emptyDiscard == 1 || emptyThree == 1) { gs->discardCount[playerNum] = 0; } else { gs->discardCount[playerNum] = randomNumber(1, 300); } gs->playedCardCount = randomNumber(0,gs->handCount[playerNum]); /*gs->deckCount[playerNum] = randomNumber(5, 300); gs->discardCount[playerNum] = randomNumber(5, 300); gs->handCount[playerNum] = randomNumber(5, 300); */ //set smithy card handPos = randomNumber(0, gs->handCount[playerNum]-1); gs->hand[playerNum][handPos] = smithy; //create copy for comparison later memcpy(stateCopy, gs, sizeof(struct gameState)); //RUN FUNCTION returnValue = playSmithy(handPos, playerNum, gs); //Check state if (stateCopy->deckCount[playerNum] < 3) { if (gs->discardCount[playerNum] != 1) { printf("discardCount is not as expected. Expected: 1, Actual: %d\r\n", gs->discardCount[playerNum]); numberOfErrors++; } } else { if (gs->discardCount[playerNum] != stateCopy->discardCount[playerNum] + 1) { printf("discardCount is not as expected. Expected: %d, Actual: %d\r\n",stateCopy->discardCount[playerNum] + 1, gs->discardCount[playerNum]); numberOfErrors++; } } //check top of discard for smithy if (gs->discard[playerNum][ gs->discardCount[playerNum] - 1 ] != smithy){ printf("top of discard mismatch. Expected: %d, Actual %d\r\n", smithy, gs->discard[playerNum][ gs->discardCount[playerNum] - 1 ]); numberOfErrors++; } if (stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum] >= 3) { drawCards = 3; } else { drawCards = stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum]; } //drawCards should be drawCards less pluss smithy if (gs->discardCount[playerNum] + gs->deckCount[playerNum] - drawCards + 1 != stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum]) { printf("deck + discard count is not as expected. Expected: %d, Actual: %d\r\n",stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum] - drawCards + 1, gs->discardCount[playerNum] + gs->deckCount[playerNum]); numberOfErrors++; } //Hand Should have drawCards extra cards minus smithy if (gs->handCount[playerNum] != stateCopy->handCount[playerNum] + drawCards - 1) { printf("handCount is not as expected. Expected: %d, Actual: %d\r\n",stateCopy->handCount[playerNum] + drawCards - 1, gs->handCount[playerNum]); numberOfErrors++; } } printf("Card Test 1 Complete\r\n"); printf("Number of errors found: %d\r\n", numberOfErrors); free(gs); free(stateCopy); return 0; }