summaryrefslogtreecommitdiffhomepage
path: root/test/math.cpp
blob: 64ab11645b9472f083d4408ef29406d04fab2141 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "app.hpp"
#include <Magnum/Math/Functions.h>

namespace floormat {

namespace {

constexpr bool test_double_sqrt()
{
    using F = double;
    constexpr auto eps = F(1e-11);

    static_assert(Math::abs(Math::sqrt((F)3)    - (F)1.73205080757) < eps);
    return true;
}

template<typename F>
bool test_sqrt()
{
    constexpr auto eps = F(1e-11);
    constexpr auto test = [](double x)
    {
        auto= (F)x;
        auto y1 = Math::sqrt();
        auto y2 = std::sqrt();
        return Math::abs(y1 - y2) < eps;
    };

    static_assert(Math::abs(Math::sqrt((F)0)    - (F)0) < eps);
    static_assert(Math::abs(Math::sqrt((F)0.5)  - (F)0.70710678118) < eps);
    static_assert(Math::abs(Math::sqrt((F)1e-8) - (F)0.0001)        < eps);
    static_assert(Math::abs(Math::sqrt((F)2)    - (F)1.41421356237) < eps);
    static_assert(Math::abs(Math::sqrt((F)3)    - (F)1.73205080757) < (F)1e-6);

    static_assert(Math::sqrt((F)0)  == (F)0);
    static_assert(Math::sqrt((F)1)  == (F)1);
    static_assert(Math::sqrt((F)4)  == (F)2);
    static_assert(Math::sqrt((F)9)  == (F)3);
    static_assert(Math::sqrt((F)36) == (F)6);

    fm_assert(test(0));
    fm_assert(test(0.5));
    fm_assert(test(1.5));
    fm_assert(test(2));
    fm_assert(test(3));
    fm_assert(test(0));
    fm_assert(test(42));
    fm_assert(test(41.5));
    fm_assert(test(1e-8));
    fm_assert(test(1e8));
    fm_assert(test(1.23456789));
    fm_assert(test(1e10 + 1.23456789));
    fm_assert(test(531610));
    fm_assert(test(1e10));
    fm_assert(test(1 << 20));

    return true;
}

template<typename F>
constexpr bool test_floor()
{
    fm_assert(Math::floor((F)-1.5) == -2);
    fm_assert(Math::floor((F)0) == 0);
    fm_assert(Math::floor((F)1) == 1);
    fm_assert(Math::floor((F)-1) == -1);
    fm_assert(Math::floor((F)-2) == -2);
    fm_assert(Math::floor((F)1.0000001) == 1);
    fm_assert(Math::floor((F)-1.000001) == -2);
    fm_assert(Math::floor((F)1e-8) == 0);
    fm_assert(Math::floor((F)-1e-8) == -1);

    return true;
}

template<typename F>
constexpr bool test_ceil()
{
    fm_assert(Math::ceil((F)-1.5) == -1);
    fm_assert(Math::ceil((F)0) == 0);
    fm_assert(Math::ceil((F)1) == 1);
    fm_assert(Math::ceil((F)-1) == -1);
    fm_assert(Math::ceil((F)-2) == -2);
    fm_assert(Math::ceil((F)1.0000001) == 2);
    fm_assert(Math::ceil((F)-1.000001) == -1);
    fm_assert(Math::ceil((F)1e-8) == 1);
    fm_assert(Math::ceil((F)-1e-8) == 0);

    return true;
}

} // namespace

void Test::test_math()
{
    static_assert(test_double_sqrt());
    fm_assert(test_sqrt<float>());
    fm_assert(test_sqrt<double>());
    static_assert(test_floor<float>());
    static_assert(test_floor<double>());
    static_assert(test_ceil<float>());
    static_assert(test_ceil<double>());
}

} // namespace floormat