Files
Tahgibu/tests/core_tests.cpp
T
2026-09-29 23:35:03 -03:00

102 lines
3.1 KiB
C++

// Pruebas del núcleo. No usan raylib.
#include <cmath>
#include <cstdio>
#include <limits>
#include <stdexcept>
#include "core/Constants.hpp"
#include "core/Frame.hpp"
#include "core/Project.hpp"
namespace {
int g_failures = 0;
void check(bool condition, const char* expression, int line) {
if (!condition) {
std::fprintf(stderr, "FALLO (línea %d): %s\n", line, expression);
++g_failures;
}
}
bool near(double a, double b) {
return std::fabs(a - b) < 1e-9;
}
} // namespace
#define CHECK(expr) check((expr), #expr, __LINE__)
int main() {
using namespace tahgibu;
// --- Límite de FPS -----------------------------------------------------
CHECK(Frame::clampFps(120.0f) == kMaxFps);
CHECK(Frame::clampFps(60.0f) == 60.0f);
CHECK(Frame::clampFps(1.0f) == 1.0f);
CHECK(Frame::clampFps(0.0f) == kMinFps);
CHECK(Frame::clampFps(-5.0f) == kMinFps);
CHECK(Frame::clampFps(std::numeric_limits<float>::quiet_NaN()) == kDefaultFps);
CHECK(Frame::clampFps(std::numeric_limits<float>::infinity()) == kDefaultFps);
// --- Frame ---------------------------------------------------------------
Frame frame;
CHECK(!frame.hasFpsOverride());
CHECK(near(frame.durationSeconds(24.0f), 1.0 / 24.0));
frame.setFpsOverride(1.0f);
CHECK(frame.hasFpsOverride());
CHECK(near(frame.durationSeconds(60.0f), 1.0));
frame.setFpsOverride(1000.0f); // nunca por encima de 60
CHECK(frame.effectiveFps(24.0f) == kMaxFps);
frame.clearFpsOverride();
CHECK(!frame.hasFpsOverride());
CHECK(frame.effectiveFps(30.0f) == 30.0f);
CHECK(frame.effectiveFps(500.0f) == kMaxFps); // la base también se limita
// --- Project -------------------------------------------------------------
Project project;
CHECK(project.frameCount() == 1);
CHECK(project.baseFps() == kDefaultFps);
project.setBaseFps(999.0f);
CHECK(project.baseFps() == kMaxFps);
project.setBaseFps(std::numeric_limits<float>::quiet_NaN());
CHECK(project.baseFps() == kDefaultFps);
// Ejemplo de la especificación: A(60) B(60) C(1) D(60)
Project example;
example.setBaseFps(60.0f);
example.addFrame(); // B
const std::size_t c = example.addFrame(); // C
example.addFrame(); // D
example.frame(c).setFpsOverride(1.0f);
CHECK(example.frameCount() == 4);
CHECK(near(example.frameDurationSeconds(0), 1.0 / 60.0));
CHECK(near(example.frameDurationSeconds(c), 1.0));
CHECK(near(example.totalDurationSeconds(), 3.0 / 60.0 + 1.0));
// Cambiar el FPS base afecta a los frames sin FPS propio, pero no al de C.
example.setBaseFps(30.0f);
CHECK(near(example.frameDurationSeconds(0), 1.0 / 30.0));
CHECK(near(example.frameDurationSeconds(c), 1.0));
bool threw = false;
try {
example.frame(99);
} catch (const std::out_of_range&) {
threw = true;
}
CHECK(threw);
if (g_failures == 0) {
std::puts("OK: todas las pruebas del núcleo pasaron.");
return 0;
}
std::fprintf(stderr, "%d prueba(s) fallaron.\n", g_failures);
return 1;
}