Untitled

 avatar
unknown
java
a month ago
3.9 kB
2
Indexable
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "test.h"
#include "game.h"
#include "player.h"
#include "dictionary.h"

// Dictionary tests
void test_initDictionary(void) {
    Dictionary dictionary;
    initDictionary(&dictionary);
    assert(dictionary.size > 0);
    freeDictionary(&dictionary);
}

void test_checkWord(void) {
    Dictionary dictionary;
    initDictionary(&dictionary);
    assert(checkWord(&dictionary, "EXEMPLE") == 1);
    assert(checkWord(&dictionary, "nonexistent") == 0);
    freeDictionary(&dictionary);
}

void test_clearDictionary(void) {
    Dictionary dictionary;
    initDictionary(&dictionary);
    freeDictionary(&dictionary);
    assert(dictionary.size == 0);
}

// Game tests
void test_initGame(void) {
    Game game;
    initGame(&game);
    assert(game.numberOfPlayers == 2);
}

void test_addPlayer(void) {
    Game game;
    game.numberOfPlayers = 0;
    initDeck(&game.deck);
    addPlayer(&game);
    assert(game.numberOfPlayers == 1);
}

void test_nextPlayer(void) {
    Game game;
    game.currentPlayer = 1;
    nextPlayer(&game);
    assert(game.currentPlayer == 2);
}

void test_compareInitialWords(void) {
    assert(compareInitialWords("apple", "banana") == -1);
    assert(compareInitialWords("banana", "apple") == 1);
    assert(compareInitialWords("apple", "apple") == 0);
}

void test_turnActionFromChar(void) {
    assert(turnActionFromChar('-') == EXCHANGE);
    assert(turnActionFromChar('R') == RECTO);
    assert(turnActionFromChar('V') == VERSO);
    assert(turnActionFromChar('r') == REPORT);
    assert(turnActionFromChar('v') == REPORT);
    assert(turnActionFromChar('x') == INVALID);
}

void test_initTurnInput(void) {
    TurnInput turnInput;
    initTurnInput(&turnInput, "R (APPLE)");
    assert(turnInput.action == RECTO);
    assert(strcmp(turnInput.fullWord, "APPLE") == 0);
}

void test_isInitialPlayValid(void) {
    Game game;
    Player player;
    initDictionary(&game.dictionary);
    initPlayer(&player, &game.deck, 1);
    assert(isInitialPlayValid(&game, "APPLE", &player) == 0);
}

void test_isPlayValid(void) {
    Game game;
    Player player;
    TurnInput turnInput;
    initDictionary(&game.dictionary);
    initPlayer(&player, &game.deck, 1);
    initTurnInput(&turnInput, "R (APPLE)");
    assert(isPlayValid(&game, &player, &turnInput) == 0);
}

void test_returnLetterToDeck(void) {
    Game game;
    initGame(&game);
    game.currentPlayer = 1;
    giveLetter(&game.players[0], 'A');
    returnLetterToDeck(&game, 'A');
    assert(game.players[0].handsize == INITIAL_HAND);
}

void test_addInitialWordsToRail(void) {
    Game game;
    initGame(&game);
    addInitialWordsToRail(&game, "APPLE");
    assert(strcmp(game.rail.recto.data, "APPLE") == 0);
}

// Player tests
void test_giveLetter(void) {
    Player player;
    initPlayer(&player, NULL, 1);
    giveLetter(&player, 'A');
    assert(player.handsize == INITIAL_HAND + 1);
    assert(player.hand.data[INITIAL_HAND] == 'A');
}

void test_deleteLetter(void) {
    Player player;
    initPlayer(&player, NULL, 1);
    giveLetter(&player, 'A');
    deleteLetter(&player, 'A');
    assert(player.handsize == INITIAL_HAND);
}

void test_deleteLetters(void) {
    Player player;
    initPlayer(&player, NULL, 1);
    giveLetter(&player, 'A');
    giveLetter(&player, 'B');
    deleteLetters(&player, "AB");
    assert(player.handsize == INITIAL_HAND);
}


void testEverything(void) {
    test_initDictionary();
    test_checkWord();
    test_clearDictionary();
    test_initGame();
    test_addPlayer();
    test_nextPlayer();
    test_compareInitialWords();
    test_turnActionFromChar();
    test_initTurnInput();
    test_isInitialPlayValid();
    test_isPlayValid();
    test_returnLetterToDeck();
    test_addInitialWordsToRail();
    test_giveLetter();
    test_deleteLetter();
    test_deleteLetters();
}
Editor is loading...
Leave a Comment