/* tests.c -- rangkaian test untuk modul game. */

#include "game.h"

#include <stdio.h>

static int lulus = 0;
static int gagal = 0;

#define PERIKSA(kondisi, nama)                              \
    do {                                                    \
        if (kondisi) { lulus++; printf("[LULUS] %s\n", nama); } \
        else { gagal++; printf("[GAGAL] %s\n", nama); }     \
    } while (0)

static void test_rentang_rahasia(void) {
    /* Karena angka acak, kita tes sebanyak 1000 kali untuk memastikan
     * seluruh hasil berada dalam rentang sah.
     */
    game_init();
    int semua_sah = 1;
    for (int i = 0; i < 1000; i++) {
        int r = game_angka_rahasia();
        if (r < GAME_TEBAK_MIN || r > GAME_TEBAK_MAKS) {
            semua_sah = 0;
            break;
        }
    }
    PERIKSA(semua_sah, "Angka rahasia selalu di rentang sah");
}

static void test_periksa_benar(void) {
    TebakStatus s = game_periksa(50, 50, 1);
    PERIKSA(s == TEBAK_BENAR, "Tebakan 50 dengan rahasia 50 -> BENAR");
}

static void test_periksa_kecil(void) {
    TebakStatus s = game_periksa(30, 50, 1);
    PERIKSA(s == TEBAK_KECIL, "Tebakan 30 dengan rahasia 50 -> KECIL");
}

static void test_periksa_besar(void) {
    TebakStatus s = game_periksa(70, 50, 1);
    PERIKSA(s == TEBAK_BESAR, "Tebakan 70 dengan rahasia 50 -> BESAR");
}

static void test_periksa_invalid(void) {
    TebakStatus s = game_periksa(0, 50, 1);
    PERIKSA(s == TEBAK_INVALID, "Tebakan 0 di luar rentang -> INVALID");
}

static void test_periksa_habis(void) {
    TebakStatus s = game_periksa(50, 50, GAME_MAKS_RONDE + 1);
    PERIKSA(s == TEBAK_HABIS, "Ronde > MAKs -> HABIS");
}

static void test_skor_ronde1(void) {
    int skor = game_hitung_skor_aman(1);
    PERIKSA(skor == GAME_SKOR_AWAL, "Menang ronde 1 -> skor penuh");
}

static void test_skor_ronde5(void) {
    int skor = game_hitung_skor_aman(5);
    PERIKSA(skor == 60, "Menang ronde 5 -> skor 60");
}

static void test_skor_invalid(void) {
    int skor = game_hitung_skor_aman(0);
    PERIKSA(skor == 0, "Ronde 0 -> skor 0");
    skor = game_hitung_skor_aman(GAME_MAKS_RONDE + 1);
    PERIKSA(skor == 0, "Ronde > MAKs -> skor 0");
}

int main(void) {
    test_rentang_rahasia();
    test_periksa_benar();
    test_periksa_kecil();
    test_periksa_besar();
    test_periksa_invalid();
    test_periksa_habis();
    test_skor_ronde1();
    test_skor_ronde5();
    test_skor_invalid();
    printf("\nRingkasan: %d lulus, %d gagal\n", lulus, gagal);
    return gagal == 0 ? 0 : 1;
}
