// Pruebas de Tally. Solo usan lo que promete tally.h, así que no saben cuál implementación
// se enlazó con ellas.
#include <assert.h>
#include <stdio.h>

#include "tally.h"

static void test_empty(void) {
    Tally *tally = tally_new();
    assert(tally != NULL);
    assert(tally_count(tally) == 0);
    assert(tally_sum(tally) == 0);
    tally_free(tally);
}

static void test_some_values(void) {
    Tally *tally = tally_new();
    assert(tally != NULL);
    assert(tally_add(tally, 3));
    assert(tally_add(tally, 9));
    assert(tally_add(tally, 4));
    assert(tally_count(tally) == 3);
    assert(tally_sum(tally) == 16);
    assert(tally_max(tally) == 9);
    tally_free(tally);
}

// El primer valor es el mayor, y es negativo: el máximo no puede empezar en 0.
static void test_negative_values(void) {
    Tally *tally = tally_new();
    assert(tally != NULL);
    assert(tally_add(tally, -2));
    assert(tally_add(tally, -7));
    assert(tally_max(tally) == -2);
    assert(tally_sum(tally) == -9);
    tally_free(tally);
}

// Más valores que la capacidad inicial de cualquier implementación razonable.
static void test_many_values(void) {
    Tally *tally = tally_new();
    assert(tally != NULL);
    for (int i = 1; i <= 1000; i++) {
        assert(tally_add(tally, i));
    }
    assert(tally_count(tally) == 1000);
    assert(tally_sum(tally) == 500500);
    assert(tally_max(tally) == 1000);
    tally_free(tally);
}

static void test_two_at_once(void) {
    Tally *first = tally_new();
    Tally *second = tally_new();
    assert(first != NULL && second != NULL);
    assert(tally_add(first, 5));
    assert(tally_count(second) == 0);
    tally_free(first);
    tally_free(second);
}

int main(void) {
    test_empty();
    test_some_values();
    test_negative_values();
    test_many_values();
    test_two_at_once();

    printf("Todas las pruebas pasaron\n");
    return 0;
}
