Files
2026_TD3_DEV51_Qualite_Algo/tests/test_banque.c
T

102 lines
2.9 KiB
C
Raw Normal View History

/* Cas limites et comparaisons avec des algorithmes de référence simples. */
#define main banque_main
#include "../banque.c"
#undef main
#include <assert.h>
#include <limits.h>
static int comparer_reference(const void *a, const void *b)
{
const Transaction *ta = a;
const Transaction *tb = b;
if (ta->montant < tb->montant)
return -1;
if (ta->montant > tb->montant)
return 1;
if (ta->id < tb->id)
return -1;
return ta->id > tb->id;
}
static void verifier_tri(Transaction *t, int n)
{
Transaction attendu[128];
assert(n <= 128);
memcpy(attendu, t, (size_t)n * sizeof(*t));
qsort(attendu, (size_t)n, sizeof(*attendu), comparer_reference);
trier_par_montant(t, n);
for (int i = 0; i < n; i++) {
assert(t[i].id == attendu[i].id);
assert(t[i].client == attendu[i].client);
assert(t[i].jour == attendu[i].jour);
assert(t[i].montant == attendu[i].montant);
}
}
static void tester_tri(void)
{
trier_par_montant(NULL, 0);
Transaction limites[] = {
{INT_MAX, 1, 2, LONG_MAX}, {3, 4, 5, 0},
{INT_MIN, 6, 7, LONG_MIN}, {1, 8, 9, 0}, {2, 10, 11, LONG_MAX}
};
verifier_tri(limites, 5);
verifier_tri(limites, 5); /* déjà trié */
Transaction t[128];
for (int n = 1; n <= 128; n++) {
for (int i = 0; i < n; i++)
t[i] = (Transaction){n - i, i, i + 1, (i * 37) % 11 - 5};
verifier_tri(t, n);
for (int i = 0; i < n; i++)
t[i] = (Transaction){i, i, i + 1, n - i};
verifier_tri(t, n); /* ordre inverse */
}
}
static void tester_clients_distincts(void)
{
assert(nb_clients_distincts(NULL, 0) == 0);
Transaction t[NB_CLIENTS];
for (int i = 0; i < NB_CLIENTS; i++)
t[i] = (Transaction){i, NB_CLIENTS - 1, 0, 0};
assert(nb_clients_distincts(t, NB_CLIENTS) == 1);
t[0].client = 0;
assert(nb_clients_distincts(t, NB_CLIENTS) == 2);
for (int i = 0; i < NB_CLIENTS; i++)
t[i].client = i;
assert(nb_clients_distincts(t, NB_CLIENTS) == NB_CLIENTS);
assert(nb_clients_distincts(t, 1) == 1);
}
static void tester_totaux_clients(void)
{
Transaction t[] = {
{0, 0, 0, 100}, {1, NB_CLIENTS - 1, 1, -50},
{2, 0, 2, -40}, {3, NB_CLIENTS - 1, 3, 50}, {4, 7, 4, 0}
};
long totaux[NB_CLIENTS];
for (int i = 0; i < NB_CLIENTS; i++)
totaux[i] = 123;
total_par_client(t, 5, totaux);
for (int c = 0; c < NB_CLIENTS; c++) {
long attendu = 0;
for (int i = 0; i < 5; i++)
if (t[i].client == c)
attendu += t[i].montant;
assert(totaux[c] == attendu);
}
total_par_client(NULL, 0, totaux);
for (int c = 0; c < NB_CLIENTS; c++)
assert(totaux[c] == 0);
}
int main(void)
{
tester_tri();
tester_clients_distincts();
tester_totaux_clients();
puts("Tests banque : OK");
return 0;
}