aboutsummaryrefslogtreecommitdiff
path: root/Client/ThirdParty/fpm/tests/arithmetic.cpp
blob: c49222a1590900675b1b84cb371e5ed73fe9ea29 (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
53
#include "common.hpp"

TEST(arithmethic, negation)
{
    using P = fpm::fixed_24_8;

    EXPECT_EQ(P(-13.125), -P( 13.125));
    EXPECT_EQ(P( 13.125), -P(-13.125));
}

TEST(arithmethic, addition)
{
    using P = fpm::fixed_24_8;

    EXPECT_EQ(P(10.75), P(3.5) + P(7.25));
}

TEST(arithmethic, subtraction)
{
    using P = fpm::fixed_24_8;

    EXPECT_EQ(P(-3.75), P(3.5) - P(7.25));
}

TEST(arithmethic, multiplication)
{
    using P = fpm::fixed_24_8;

    EXPECT_EQ(P(-25.375), P(3.5) * P(-7.25));
}

TEST(arithmethic, division)
{
    using P = fpm::fixed_24_8;

    EXPECT_EQ(P(3.5 / 7.25), P(3.5) / P(7.25));
    EXPECT_EQ(P(-3.5 / 7.25), P(-3.5) / P(7.25));
    EXPECT_EQ(P(3.5 / -7.25), P(3.5) / P(-7.25));
    EXPECT_EQ(P(-3.5 / -7.25), P(-3.5) / P(-7.25));

#ifndef NDEBUG
    EXPECT_DEATH(P(1) / P(0), "");
#endif
}

TEST(arithmethic, division_range)
{
    using P = fpm::fixed<std::int32_t, std::int64_t, 12>;

    // These calculation will overflow and produce
    // wrong results without the intermediate type.
    EXPECT_EQ(P(32), P(256) / P(8));
}