Exemplo n.º 1
0
void unittest1_initiateKingdomCards(int * kingdomCards) {
		
	int i;
	int randCard;
	size_t chosenCardsMemSize = sizeof(int)*(treasure_map+1);
	int * chosenCards = malloc(chosenCardsMemSize);

		
	memset(chosenCards, 0, chosenCardsMemSize);

	int duplicateCardsAllowed = percentChanceIsOne(UNITTEST1_CHANCE_DUPLICATE_CARDS);
	for (i = 0; i < 10; i++){
		if (duplicateCardsAllowed == 0) {
			randCard = randomNumber(0, treasure_map);
			while (chosenCards[randCard] == 1) {
				randCard = randomNumber(0, treasure_map);
			}
			kingdomCards[i] = randCard;
			chosenCards[randCard] = 1;
		} else {
			kingdomCards[i] = randomNumber(0, treasure_map);
		}
	}
		
	free(chosenCards);
}
Exemplo n.º 2
0
int randomNumPlayers(){
	//Number of players
	int numPlayers = randomNumber(2, MAX_PLAYERS);
	
	//on occasion, attempt to place out of bounds
	if (percentChanceIsOne(UNITTEST1_CHANCE_PLAYER_OUT_OF_BOUNDS) == 1) {
		numPlayers = randomNumber(-1000,1000);
	}
	
	return numPlayers;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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;
}