Пример #1
0
int main (int argc, char *argv[])
{
    int i;
    Item it;
    Queue q1,q2;

    printf("Test 1: Create queues\n");
    q1 = createQueue();
    q2 = createQueue();
    assert(q1 != NULL);
    assert(q2 != NULL);
    assert(queueLength(q1) == 0);
    assert(queueLength(q2) == 0);
    printf("Passed\n");

    printf("Test 2: Add to queues\n");
    for (i = 1; i <= MAX; i++) {
        enterQueue(q2,i);
        enterQueue(q1,i);
        assert(queueLength(q2) == i);
        assert(queueLength(q1) == i);
    }
    printf("Final q1: ");
    showQueue(q1);
    printf("Final q2: ");
    showQueue(q2);
    printf("Passed\n");

    printf("Test 3: Remove from queues\n");
    for (i = 1; i <= MAX; i++) {
        it = leaveQueue(q1);
        assert(queueLength(q1) == MAX-i);
        assert(i == it);
        it = leaveQueue(q2);
        assert(queueLength(q2) == MAX-i);
        assert(i == it);
    }
    printf("Passed\n");

    printf("Test 4: Destroy queues\n");
    dropQueue(q1);
    dropQueue(q2);
    printf("Passed\n");

    printf("Test 5: Remove from emoty queue\n");
    printf("This test should fail an assertion\n");
    q1 = createQueue();
    it = leaveQueue(q1);
    printf("Passed\n");

    return 0;
}
Пример #2
0
int main (int argc, char *argv[]){

	Queue q = newQueue();

	assert(queueEmpty(q));

	queueJoin(q, 5);
	queueJoin(q, 8);
	queueJoin(q, 3);
	queueJoin(q, 9);
	queueJoin(q, 2);

	printQueue(q);

	assert(!queueEmpty(q));

	assert(queueLeave(q) == 5);
	assert(queueLeave(q) == 8);
	assert(queueLeave(q) == 3);
	assert(queueLeave(q) == 9);
	assert(queueLeave(q) == 2);

	assert(queueEmpty(q));

	dropQueue(q);

	return EXIT_SUCCESS;
}
Пример #3
0
// road rail and sea are flags, TRUE or FALSE (const)
LocationID * connectedLocations(HunterView currentView, int * numLocations, LocationID from, 
                              PlayerID player, Round round, int road, int rail, int sea) {
	assert(from >= 0);
    int i, j;
    int isConnected[NUM_MAP_LOCATIONS];
    int numIsConnected = 0;
    int railTravelLength = (round + player) % 4;
    
    for (i = 0; i < NUM_MAP_LOCATIONS; i++) {
        // long if statement which essentially says:
        // if want road, and connected by road,
        // or want rail, and allowed to use rail, and connected by rail,
        // or want sea, and connected by sea.
        if ((road && (currentView->map[from][i] == ROAD || currentView->map[from][i] == BOTH))
                || (rail && player != PLAYER_DRACULA && railTravelLength > 0 &&
                     (currentView->map[from][i] == RAIL || currentView->map[from][i] == BOTH))
                || (sea && currentView->map[from][i] == SEA)) {
            isConnected[i] = TRUE;
            numIsConnected++;
        } else {
            isConnected[i] = FALSE;
        }
    }
    
    // adds "from" here to avoid being overwritten by above code
    // doing this saves putting extra checks in above code
    if (!isConnected[from]) {
        isConnected[from] = TRUE;
        numIsConnected++;
    }
    
    // BFS for finding rail travel connections
    // if railTravelLength is 1, already checked above to avoid unnecessary BFS
    if (rail && player != PLAYER_DRACULA && railTravelLength > 1) {
        Queue q = newQueue();
        int distance[NUM_MAP_LOCATIONS];
        for (i = 0; i < NUM_MAP_LOCATIONS; i++) {
            distance[i] = -1;
        }
        distance[from] = 0;
        QueueJoin(q, from);
        while (!QueueIsEmpty(q)) {
            LocationID l = QueueLeave(q);
            if (distance[l] >= railTravelLength) {
                break; // already searched deep enough
            }
            for (i = 0; i < NUM_MAP_LOCATIONS; i++) {
                if (distance[i] == -1 && (currentView->map[l][i] == RAIL || currentView->map[l][i] == BOTH)) {
                    distance[i] = 1 + distance[l];
                    QueueJoin(q, i);
                }
            }
        }
        dropQueue(q);
        for (i = 0; i < NUM_MAP_LOCATIONS; i++) {
            if (distance[i] >= 0 && distance[i] <= railTravelLength && !isConnected[i]) {
                isConnected[i] = TRUE;
                numIsConnected++;
            }
        }
    }
    
    // Copy any reached location into new array to return
    LocationID * connections = malloc(sizeof(LocationID) * numIsConnected);
    *numLocations = numIsConnected;
    i = 0;
    for (j = 0; j < NUM_MAP_LOCATIONS; j++) {
        if (isConnected[j]) {
            connections[i] = j;
            i++;
        }
    }
    assert(i == numIsConnected); // will fail if numIsConnected was miscalculated
    return connections;
}