summaryrefslogtreecommitdiffstats
path: root/src/test/mathutiltest.cpp
blob: f9c017cecd8c7e97375469991d7641396544807d (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
#include <limits>

#include <gtest/gtest.h>
#include <QtDebug>

#include "util/math.h"
#include "util/denormalsarezero.h"

namespace {

class MathUtilTest : public testing::Test {
  protected:
    static const int MIN;
    static const int MAX;

    static const int VALUE_MIN;
    static const int VALUE_MAX;
};

const int MathUtilTest::MIN = -10;
const int MathUtilTest::MAX = 10;

const int MathUtilTest::VALUE_MIN = 2 * MathUtilTest::MIN;
const int MathUtilTest::VALUE_MAX = 2 * MathUtilTest::MAX;

TEST_F(MathUtilTest, MathClampUnsafe) {
    for (int i = VALUE_MIN; i <= VALUE_MAX; ++i) {
        EXPECT_LE(MIN, math_clamp(i, MIN, MAX));
        EXPECT_GE(MAX, math_clamp(i, MIN, MAX));
        EXPECT_EQ(MIN, math_clamp(i, MIN, MIN));
        EXPECT_EQ(MAX, math_clamp(i, MAX, MAX));
        if (MIN >= i) {
            EXPECT_EQ(MIN, math_clamp(i, MIN, MAX));
        }
        if (MAX <= i) {
            EXPECT_EQ(MAX, math_clamp(i, MIN, MAX));
        }
    }
}

TEST_F(MathUtilTest, IsNaN) {
    // Test floats can be recognized as nan.
    EXPECT_FALSE(isnan(0.0f));
    EXPECT_TRUE(isnan(std::numeric_limits<float>::quiet_NaN()));

    // Test doubles can be recognized as nan.
    EXPECT_FALSE(isnan(0.0));
    EXPECT_TRUE(isnan(std::numeric_limits<double>::quiet_NaN()));
}

TEST_F(MathUtilTest, IsInf) {
    // Test floats can be recognized as infinity.
    EXPECT_FALSE(isinf(0.0f));
    EXPECT_TRUE(isinf(std::numeric_limits<float>::infinity()));

    // Test doubles can be recognized as infinity.
    EXPECT_FALSE(isinf(0.0f));
    EXPECT_TRUE(isinf(std::numeric_limits<double>::infinity()));
}

TEST_F(MathUtilTest, Denormal) {
#ifdef __SSE__

    _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
    _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);

    volatile float fDenormal = std::numeric_limits<float>::min() / 2.0f;
    EXPECT_NE(0.0f, fDenormal);

    volatile double dDenormal = std::numeric_limits<double>::min() / 2.0;
    EXPECT_NE(0.0, dDenormal);

    _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
    _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);

    fDenormal = std::numeric_limits<float>::min() / 2.0f;
    EXPECT_EQ(0.0f, fDenormal);

    dDenormal = std::numeric_limits<double>::min() / 2.0;
    EXPECT_EQ(0.0, dDenormal);

#endif
}


}  // namespace