Virgule fixe Q16.16, générateur PCG32 à graine explicite, horloge à pas fixe exacte, simulation d'un micro-mode de démonstration (marqué SIMULÉ), empreinte FNV-1a de l'état, enregistrement binaire versionné des entrées, rejeu avec d'autres réglages de ressenti et détection du premier tick divergent. Empreinte de référence multiplateforme figée. 22 tests (Debug, Release, ASan).
53 lines
1.7 KiB
C++
53 lines
1.7 KiB
C++
// SPDX-License-Identifier: MPL-2.0
|
|
#include "sequa/core/sim/fixed.hpp"
|
|
|
|
#include <doctest/doctest.h>
|
|
|
|
#include <cstdint>
|
|
#include <limits>
|
|
|
|
using sequa::core::sim::Fixed;
|
|
|
|
TEST_CASE("Fixed : conversions entieres et rationnelles")
|
|
{
|
|
CHECK(Fixed::from_int(3).raw() == 3 * 65536);
|
|
CHECK(Fixed::from_int(-2).raw() == -2 * 65536);
|
|
CHECK(Fixed::from_ratio(1, 2).raw() == 32768);
|
|
CHECK(Fixed::from_ratio(1, 60).raw() == 1092); // arrondi vers zero
|
|
CHECK(Fixed::from_ratio(-1, 60).raw() == -1092);
|
|
}
|
|
|
|
TEST_CASE("Fixed : arithmetique exacte et arrondis documentes")
|
|
{
|
|
const Fixed a = Fixed::from_int(5);
|
|
const Fixed b = Fixed::from_ratio(1, 4);
|
|
CHECK((a + b).raw() == 5 * 65536 + 16384);
|
|
CHECK((a - b).raw() == 5 * 65536 - 16384);
|
|
CHECK((a * b).raw() == 5 * 16384);
|
|
CHECK((a / b).raw() == 20 * 65536);
|
|
CHECK((-a).raw() == -5 * 65536);
|
|
|
|
// Produit : arrondi vers moins l'infini ; quotient : vers zero.
|
|
const Fixed tiny = Fixed::from_raw(1);
|
|
const Fixed half = Fixed::from_ratio(1, 2);
|
|
CHECK((tiny * half).raw() == 0);
|
|
CHECK(((-tiny) * half).raw() == -1);
|
|
CHECK((Fixed::from_raw(-1) / Fixed::from_int(2)).raw() == 0);
|
|
}
|
|
|
|
TEST_CASE("Fixed : un depassement est modulaire, donc deterministe")
|
|
{
|
|
const Fixed max = Fixed::from_raw(std::numeric_limits<std::int32_t>::max());
|
|
CHECK((max + Fixed::from_raw(1)).raw() == std::numeric_limits<std::int32_t>::min());
|
|
}
|
|
|
|
TEST_CASE("Fixed : comparaisons, abs et clamp")
|
|
{
|
|
const Fixed one = Fixed::from_int(1);
|
|
CHECK(one > Fixed{});
|
|
CHECK(abs(-one) == one);
|
|
CHECK(clamp(Fixed::from_int(7), -one, one) == one);
|
|
CHECK(clamp(Fixed::from_int(-7), -one, one) == -one);
|
|
CHECK(Fixed::from_ratio(3, 2).to_double() == doctest::Approx(1.5));
|
|
}
|