// SPDX-License-Identifier: MPL-2.0 #include "sequa/core/sim/fixed.hpp" #include #include #include 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::max()); CHECK((max + Fixed::from_raw(1)).raw() == std::numeric_limits::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)); }