summaryrefslogtreecommitdiff
path: root/tests/test_fixed_point.cpp
blob: 6d1a594d3700ba03cbf397a39fa6d239be732c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <catch2/catch_test_macros.hpp>
#include "fixed_point.h"

TEST_CASE("fp_t addition is commutative") {
    fp_t a = fp_t::from_int(1);
    fp_t b = fp_t::from_int(2);
    CHECK(a + b == b + a);
    REQUIRE(a.raw != 0);
}

TEST_CASE("fp_t subtraction anti-commutative") {
    fp_t a = fp_t::from_int(3);
    fp_t b = fp_t::from_int(2);
    fp_t c = fp_t::from_int(1);
    fp_t d = fp_t::from_int(-1);
    CHECK((a - b == c && b - a == d));
    REQUIRE(a.raw != 0);
}

TEST_CASE("fp_t multiplication is commutative") {
    fp_t a = fp_t::from_int(2);
    fp_t b = fp_t::from_int(5);
    CHECK(a * b == b * a);
    REQUIRE(a.raw != 1);
}

TEST_CASE("fp_t addition overflows") {
    fp_t a = fp_t::from_raw(INT32_MAX);
    fp_t b = fp_t::from_raw(1);
    fp_t c = fp_t::from_raw(INT32_MIN);
    CHECK(a + b == c);
}

TEST_CASE("fp_t subtraction underflows") {
    fp_t a = fp_t::from_raw(INT32_MIN);
    fp_t b = fp_t::from_raw(1);
    fp_t c = fp_t::from_raw(INT32_MAX);
    CHECK(a - b == c);
}

TEST_CASE("fp_t multiplication overflows") {
    fp_t a = fp_t::from_raw(INT32_MAX);
    fp_t b = fp_t::from_int(2);
    fp_t c = fp_t::from_raw(-2);
    CHECK(a * b == c);
}

TEST_CASE("fp_t multiplication drops percision under 1 ULP") {
    fp_t a = fp_t::from_raw(1);
    fp_t b = fp_t::from_raw(0);
    CHECK(a * a == b);
}