test_Math.cpp 3.06 KB
Newer Older
David Frank's avatar
David Frank committed
1
2
3
#include "doctest/doctest.h"

#include "Math.hpp"
andibraimllari's avatar
andibraimllari committed
4
5
6
#include "VolumeDescriptor.h"

#include <testHelpers.h>
David Frank's avatar
David Frank committed
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

TEST_SUITE_BEGIN("Math");

using namespace elsa;

TEST_CASE("Math::factorial")
{
    CHECK_EQ(1, math::factorial(0));
    CHECK_EQ(1, math::factorial(1));

    auto fac = 1;
    for (int i = 1; i < 10; ++i) {
        fac *= i;
        CHECK_EQ(fac, math::factorial(i));
    }
}

TEST_CASE("Math::binom")
{
    GIVEN("n == 10")
    {
        const index_t n = 10;

        WHEN("k larger than n") { CHECK_EQ(math::binom(n, 15), 0); }

        WHEN("k == 0 or k == n")
        {
            CHECK_EQ(math::binom(n, 0), 1);
            CHECK_EQ(math::binom(n, 10), 1);
        }

        CHECK_EQ(math::binom(n, 1), 10);
        CHECK_EQ(math::binom(n, 2), 45);
        CHECK_EQ(math::binom(n, 3), 120);
        CHECK_EQ(math::binom(n, 4), 210);
        CHECK_EQ(math::binom(n, 5), 252);
        CHECK_EQ(math::binom(n, 6), 210);
        CHECK_EQ(math::binom(n, 7), 120);
        CHECK_EQ(math::binom(n, 8), 45);
        CHECK_EQ(math::binom(n, 9), 10);
    }
}

TEST_CASE("Math::heaviside")
{
    constexpr index_t size = 200;
    constexpr real_t c = 0.5;
    const auto linspace = Vector_t<real_t>::LinSpaced(size, -2, 2);

    for (std::size_t i = 0; i < size; ++i) {
        auto res = math::heaviside(linspace[i], c);
        if (linspace[i] == 0.) {
            CHECK_EQ(res, c);
        } else if (linspace[i] < 0) {
            CHECK_EQ(res, 0);
        } else if (linspace[i] > 0) {
            CHECK_EQ(res, 1);
        }
    }
}

andibraimllari's avatar
andibraimllari committed
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
TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
{
    GIVEN("a DataContainer")
    {
        IndexVector_t sizeVector(2);
        sizeVector << 2, 4;
        VolumeDescriptor volDescr(sizeVector);

        Vector_t<TestType> vect1(volDescr.getNumberOfCoefficients());
        vect1 << 4, 2, 0.7f, 1, 0, 9, 53, 8;

        Vector_t<TestType> vect2(volDescr.getNumberOfCoefficients());
        vect2 << 5, 1, 6, 12, 22, 23, 9, 9;

        DataContainer<TestType> dataCont1(volDescr, vect1);

        DataContainer<TestType> dataCont2(volDescr, vect2);

        WHEN("running the Mean Squared Error")
        {
            auto meanSqErr = statistics::meanSquaredError<TestType>(dataCont1, dataCont2);
            THEN("it produces the correct result")
            {
                REQUIRE_UNARY(checkApproxEq(meanSqErr, 346.01125f));
            }
        }

        WHEN("running the Relative Error")
        {
            auto relErr = statistics::relativeError<TestType>(dataCont1, dataCont2);
            THEN("it produces the correct result")
            {
                REQUIRE_UNARY(checkApproxEq(relErr, 1.4157718f));
            }
        }

        WHEN("running the Peak Signal-to-Noise Ratio")
        {
AndiBraimllari's avatar
AndiBraimllari committed
106
107
            auto psnr = statistics::peakSignalToNoiseRatio<TestType>(dataCont1, dataCont2);
            auto expectedpsnr = static_cast<TestType>(9.09461);
andibraimllari's avatar
andibraimllari committed
108
109
110
111
112
113
114
115
            THEN("it produces the correct result")
            {
                REQUIRE_UNARY(checkApproxEq(psnr, expectedpsnr));
            }
        }
    }
}

David Frank's avatar
David Frank committed
116
TEST_SUITE_END();