Files
FnixLib/tests/test_core.c
T

135 lines
4.3 KiB
C

/* Pruebas sin pantalla: errores, tiempo y validación de argumentos. */
#include "check.h"
#include <fnix/fnix.h>
#include <string.h>
static void test_error_strings(void)
{
CHECK(strlen(fnix_error_string(FNIX_OK)) > 0);
CHECK(strlen(fnix_error_string(FNIX_ERROR_INVALID_ARGUMENT)) > 0);
CHECK(strlen(fnix_error_string(FNIX_ERROR_OUT_OF_MEMORY)) > 0);
CHECK(strlen(fnix_error_string(FNIX_ERROR_PLATFORM)) > 0);
CHECK(strlen(fnix_error_string(FNIX_ERROR_UNSUPPORTED)) > 0);
CHECK(strlen(fnix_error_string((FnixError)12345)) > 0);
}
static void test_last_error(void)
{
fnix_clear_error();
CHECK(fnix_last_error_code() == FNIX_OK);
CHECK(fnix_last_error() != NULL);
CHECK(strcmp(fnix_last_error(), "") == 0);
FnixCanvas *canvas = fnix_canvas_create(0, 10);
CHECK(canvas == NULL);
CHECK(fnix_last_error_code() == FNIX_ERROR_INVALID_ARGUMENT);
CHECK(strlen(fnix_last_error()) > 0);
/* Un éxito no borra el último error (comportamiento tipo errno). */
canvas = fnix_canvas_create(4, 4);
CHECK(canvas != NULL);
CHECK(fnix_last_error_code() == FNIX_ERROR_INVALID_ARGUMENT);
fnix_canvas_destroy(canvas);
fnix_clear_error();
CHECK(fnix_last_error_code() == FNIX_OK);
CHECK(strcmp(fnix_last_error(), "") == 0);
}
static void test_canvas_validation(void)
{
CHECK(fnix_canvas_create(-1, 5) == NULL);
CHECK(fnix_canvas_create(5, -1) == NULL);
CHECK(fnix_canvas_create(FNIX_CANVAS_MAX_SIZE + 1, 5) == NULL);
CHECK(fnix_canvas_create(5, FNIX_CANVAS_MAX_SIZE + 1) == NULL);
FnixCanvas *canvas = fnix_canvas_create(7, 3);
CHECK(canvas != NULL);
CHECK(fnix_canvas_width(canvas) == 7);
CHECK(fnix_canvas_height(canvas) == 3);
CHECK(fnix_canvas_pixels(canvas) != NULL);
/* Lienzo nuevo: todo negro. */
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 7; x++) {
const FnixColor c = fnix_canvas_get_pixel(canvas, x, y);
CHECK(c.r == 0 && c.g == 0 && c.b == 0 && c.a == 255);
}
}
/* Fuera de rango o NULL: transparente / 0. */
CHECK(fnix_canvas_get_pixel(canvas, -1, 0).a == 0);
CHECK(fnix_canvas_get_pixel(canvas, 7, 0).a == 0);
CHECK(fnix_canvas_get_pixel(canvas, 0, 3).a == 0);
CHECK(fnix_canvas_get_pixel(NULL, 0, 0).a == 0);
CHECK(fnix_canvas_width(NULL) == 0);
CHECK(fnix_canvas_height(NULL) == 0);
CHECK(fnix_canvas_pixels(NULL) == NULL);
/* Destruir el lienzo de destino limpia el destino. */
fnix_set_target(canvas);
CHECK(fnix_get_target() == canvas);
fnix_canvas_destroy(canvas);
CHECK(fnix_get_target() == NULL);
fnix_canvas_destroy(NULL); /* no debe fallar */
}
static void test_window_validation(void)
{
/* Estas comprobaciones ocurren antes de tocar el sistema gráfico. */
fnix_clear_error();
CHECK(fnix_window_create(NULL, 100, 100) == NULL);
CHECK(fnix_last_error_code() == FNIX_ERROR_INVALID_ARGUMENT);
fnix_clear_error();
CHECK(fnix_window_create("x", 0, 100) == NULL);
CHECK(fnix_last_error_code() == FNIX_ERROR_INVALID_ARGUMENT);
CHECK(fnix_window_create("x", 100, -5) == NULL);
CHECK(fnix_window_create("x", FNIX_CANVAS_MAX_SIZE + 1, 100) == NULL);
/* Funciones con ventana NULL: seguras. */
fnix_window_destroy(NULL);
fnix_begin_frame(NULL);
fnix_end_frame(NULL);
fnix_window_request_close(NULL);
fnix_window_set_target_fps(NULL, 30);
CHECK(fnix_window_should_close(NULL));
CHECK(fnix_window_width(NULL) == 0);
CHECK(fnix_window_height(NULL) == 0);
CHECK(fnix_window_canvas(NULL) == NULL);
CHECK(fnix_window_delta_time(NULL) == 0.0);
}
static void test_time(void)
{
double previous = fnix_time_now();
for (int i = 0; i < 1000; i++) {
const double now = fnix_time_now();
CHECK(now >= previous);
previous = now;
}
const double before = fnix_time_now();
fnix_time_sleep(0.05);
const double elapsed = fnix_time_now() - before;
CHECK(elapsed >= 0.05);
CHECK(elapsed < 0.5);
const double before_zero = fnix_time_now();
fnix_time_sleep(0.0);
fnix_time_sleep(-3.0);
CHECK(fnix_time_now() - before_zero < 0.01);
}
int main(void)
{
test_error_strings();
test_last_error();
test_canvas_validation();
test_window_validation();
test_time();
return CHECK_REPORT("test_core");
}