void nextTest() { State st; initState(&st); State sts[10]; int count = getNext(st, sts); assert(count == 9); int i, j; for (i = 0; i < count; ++i) for (j = i+1; j < count; ++j) assert(!compareStates(sts[i], sts[j])); }
int hardCodedCompare(struct gameState *pre, struct gameState *post, int *coinArray, int coins, int player) { int i; int bonus = rand() % 10; //Set coins to 0 and make copy of state pre->coins = 0; //Set hand to array passed to function for (i = 0; i < 5; i++) { pre->hand[player][i] = coinArray[i]; post->hand[player][i] = coinArray[i]; } //Copy state of game to compare against state after call to updateCoins memcpy(post, pre, sizeof(struct gameState)); updateCoins(player, post, bonus); if (compareStates(post, pre, bonus + coins) == -1) return -1; return 0; }
int main() { //set up variables used for testing int numPlayers, player, handPos, i; struct gameState pre, post; int numTests = 1000; int k[10] = {adventurer, council_room, feast, gardens, mine , remodel, smithy, village, baron, great_hall}; int seed; //loop for amount of tests for(i = 0; i < numTests; i++) { printf("****TEST %i****\n", i+1); //randomize each of our test variables numPlayers = floor(Random() * (MAX_PLAYERS-2)) + 2; //at least 2 players player = floor(Random() * numPlayers); printf("Player playing adventurer: #%i\n", player); //initialize game memset(&post, 23, sizeof(struct gameState)); seed = floor(Random() * MAX_DECK); int init = initializeGame(numPlayers, k, seed, &post); if(init != 0) { printf("Error initializing game.\n"); return -1; } //random player test post.whoseTurn = player; //fill out player decks fillDecks(&post); //fill out player hands fillHands(&post); //sneak an smithy into a random handPos so we can play it handPos = floor(Random() * post.handCount[player]); post.hand[player][handPos] = smithy; //copy game // memset(&pre, 23, sizeof(struct gameState)); memcpy(&pre, &post, sizeof(struct gameState)); // printf("The hand count for player %i is %i\n", player, post.handCount[player]); // printf("The deck count for player %i is %i\n", player, post.deckCount[player]); //play smithy int playcardState = playCard(handPos, 0,0,0, &post); if(playcardState != 0) { printf("There was an error playing the card\n"); } //imitate playing smithy testSmithy(&pre, handPos); //compare the game states compareStates(&pre, &post); printf("****FINISHED TEST %i****\n\n", i); } return 0; }
int main () { //gs1 will hold state after initialization, gs2 will be state after draw card struct gameState *gs1 = malloc(sizeof(struct gameState)); struct gameState *gs2 = malloc(sizeof(struct gameState)); //Cards used in test instance of game int gameCards[10] = {smithy, adventurer, council_room, feast, gardens, mine, remodel, village, baron, great_hall}; int numPlayers; int numCards = 10; int seed = 55; int i, j; //Index variables int bonus, maxBonus = 10; int passTest = 0; int card; int coinsValue; //Initialize a game if (initializeGame(2, gameCards, seed, gs1) == -1) { printf("Game state failed to initialize. No testing completed.\n"); return -1; } int currPlayer = whoseTurn(gs1); //Set coins to 0 before all testing gs1->coins = 0; //Copy game state for comparison after call to updateCoins() memcpy(gs2, gs1, sizeof(struct gameState)); printf("Beginning unit testing for updateCoins().\n"); //Testing with no coins and no bonus (10 iterations with random hands) printf("Testing with no treasure cards and no bonus.\n"); for (i = 0; i < 10; i++) { bonus = 0; //Fill hand with random non-treasure cards for (j = 0; j < 5; j++) { card = rand() % 10; //Update hands in both states to avoid false positives in comparison testing gs1->hand[currPlayer][j] = gameCards[card]; gs2->hand[currPlayer][j] = gameCards[card]; } updateCoins(currPlayer, gs1, bonus); //Nothing should have changed with call to updateCoins() if (compareStates(gs1, gs2, bonus) == -1) passTest = -1; } if (passTest != -1) printf("PASS all tests for condition with no treasure cards and no bonus.\n"); //Testing with no coins and random bonus printf("Testing with no treasure cards and random bonus.\n"); for (i = 0; i < 10; i++) { gs1->coins = 0; memcpy(gs2, gs1, sizeof(struct gameState)); bonus = rand() % maxBonus; //Fill hand with random non-treasure cards for (j = 0; j < 5; j++) { card = rand() % 10; //Update hands in both states to avoid false positives in comparison testing gs1->hand[currPlayer][j] = gameCards[card]; gs2->hand[currPlayer][j] = gameCards[card]; } updateCoins(currPlayer, gs1, bonus); //Nothing should have changed with call to updateCoins() if (compareStates(gs1, gs2, bonus) == -1) passTest = -1; } if (passTest != -1) printf("PASS all tests for condition with no treasure cards and random bonus.\n"); //Testing only gold coins, from 1 - 5 in hand printf("Testing for only gold treasure cards in hand with random bonus.\n"); for (i = 1; i <=5; i++ ) { gs1->coins = 0; memcpy(gs2, gs1, sizeof(struct gameState)); bonus = rand() % maxBonus; for (j = 0; j < 5; j++) { if (j < i) { gs1->hand[currPlayer][j] = gold; gs2->hand[currPlayer][j] = gold; } else { card = rand() % 10; gs1->hand[currPlayer][j] = gameCards[card]; gs2->hand[currPlayer][j] = gameCards[card]; } } //Get value of gold coins in hand int goldValue = (i * 3); updateCoins(currPlayer, gs1, bonus); if (compareStates(gs1, gs2, bonus + goldValue) == -1) passTest = -1; } if (passTest != -1) printf("PASS all tests with only gold coins and random bonus.\n"); //Testing only silver coins, from 1 - 5 in hand printf("Testing for only silver treasure cards in hand with random bonus.\n"); for (i = 1; i <=5; i++ ) { gs1->coins = 0; memcpy(gs2, gs1, sizeof(struct gameState)); bonus = rand() % maxBonus; for (j = 0; j < 5; j++) { if (j < i) { gs1->hand[currPlayer][j] = silver; gs2->hand[currPlayer][j] = silver; } else { card = rand() % 10; gs1->hand[currPlayer][j] = gameCards[card]; gs2->hand[currPlayer][j] = gameCards[card]; } } //Get value of silver coins in hand int silverValue = i * 2; updateCoins(currPlayer, gs1, bonus); if (compareStates(gs1, gs2, bonus + silverValue) == -1) passTest = -1; } if (passTest != -1) printf("PASS all tests with only silver coins and random bonus.\n"); //Testing only copper coins, from 1 - 5 in hand printf("Testing for only copper treasure cards in hand with random bonus.\n"); for (i = 1; i <=5; i++ ) { gs1->coins = 0; memcpy(gs2, gs1, sizeof(struct gameState)); bonus = rand() % maxBonus; for (j = 0; j < 5; j++) { if (j < i) { gs1->hand[currPlayer][j] = copper; gs2->hand[currPlayer][j] = copper; } else { card = rand() % 10; gs1->hand[currPlayer][j] = gameCards[card]; gs2->hand[currPlayer][j] = gameCards[card]; } } //Get value of copper coins in hand int copperValue = i; updateCoins(currPlayer, gs1, bonus); if (compareStates(gs1, gs2, bonus + copperValue) == -1) passTest = -1; } if (passTest != -1) printf("PASS all tests with only copper coins and random bonus.\n"); //Testing mix of coin values - HARD CODED printf("Testing mixes of coin types.\n"); //Gold and silver int gsArray[5] = {gold, silver, silver, gold, silver}; coinsValue = 12; if (hardCodedCompare(gs2, gs1, gsArray, coinsValue, currPlayer) != -1) printf("PASS test with gold and silver coins.\n"); //Gold and copper int gcArray[5] = {copper, gold, gold, copper, copper}; coinsValue = 9; if (hardCodedCompare(gs2, gs1, gcArray, coinsValue, currPlayer) != -1) printf("PASS test with gold and copper coins.\n"); //Silver and copper int scArray[5] = {copper, silver, copper, copper, silver}; coinsValue = 7; if (hardCodedCompare(gs2, gs1, scArray, coinsValue, currPlayer) != -1) printf("PASS test with silver and copper coins.\n"); //Gold, silver, and copper int gscArray[5] = {gold, silver, copper, gold, copper}; coinsValue = 10; if (hardCodedCompare(gs2, gs1, gscArray, coinsValue, currPlayer) != -1) printf("PASS test with gold, silver, and copper coins.\n"); return 0; }