forked from rive-app/rive-cpp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_test.cpp
More file actions
126 lines (112 loc) · 4.31 KB
/
math_test.cpp
File metadata and controls
126 lines (112 loc) · 4.31 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <catch.hpp>
#include <rive/math/math_types.hpp>
using namespace rive;
constexpr float kInf = std::numeric_limits<float>::infinity();
constexpr float kNaN = std::numeric_limits<float>::quiet_NaN();
// Check math::ieee_float_divide.
TEST_CASE("ieee_float_divide", "[math]")
{
// Make sure we're doing division.
CHECK(math::ieee_float_divide(100, 10) == 10);
// Returns +/-Inf if b == 0.
CHECK(math::ieee_float_divide(5, 0) == kInf);
CHECK(math::ieee_float_divide(5, -0) == kInf);
CHECK(math::ieee_float_divide(-3, 0) == -kInf);
CHECK(math::ieee_float_divide(-3, -0) == -kInf);
CHECK(math::ieee_float_divide(kInf, 0) == kInf);
CHECK(math::ieee_float_divide(-kInf, 0) == -kInf);
CHECK(math::ieee_float_divide(kInf, -0) == kInf);
CHECK(math::ieee_float_divide(-kInf, -0) == -kInf);
// Returns 0 if b == +/-Inf.
CHECK(math::ieee_float_divide(1, kInf) == 0);
CHECK(math::ieee_float_divide(-100, kInf) == 0);
CHECK(math::ieee_float_divide(std::numeric_limits<float>::max(), kInf) == 0);
CHECK(math::ieee_float_divide(std::numeric_limits<float>::max(), -kInf) == 0);
CHECK(math::ieee_float_divide(-std::numeric_limits<float>::max(), -kInf) == 0);
CHECK(math::ieee_float_divide(-std::numeric_limits<float>::max(), kInf) == 0);
CHECK(math::ieee_float_divide(0, kInf) == 0);
CHECK(math::ieee_float_divide(0, -kInf) == 0);
CHECK(math::ieee_float_divide(-0, -kInf) == 0);
CHECK(math::ieee_float_divide(-0, kInf) == 0);
// Returns NaN if a and b are both zero.
CHECK(std::isnan(math::ieee_float_divide(0, 0)));
CHECK(std::isnan(math::ieee_float_divide(0, -0)));
CHECK(std::isnan(math::ieee_float_divide(-0, 0)));
CHECK(std::isnan(math::ieee_float_divide(-0, -0)));
// Returns NaN if b and are both infinite.
CHECK(std::isnan(math::ieee_float_divide(kInf, kInf)));
CHECK(std::isnan(math::ieee_float_divide(kInf, -kInf)));
CHECK(std::isnan(math::ieee_float_divide(-kInf, kInf)));
CHECK(std::isnan(math::ieee_float_divide(kInf, -kInf)));
// Returns NaN a or b is NaN.
CHECK(std::isnan(math::ieee_float_divide(kNaN, 1)));
CHECK(std::isnan(math::ieee_float_divide(kInf, kNaN)));
}
// Check math::bit_cast.
TEST_CASE("bit_cast", "[math]")
{
CHECK(math::bit_cast<float>(0x3f800000) == 1);
CHECK(math::bit_cast<float>((1u << 31) | 0x3f800000) == -1);
CHECK(math::bit_cast<float>(0x7f800000) == kInf);
CHECK(math::bit_cast<float>((1u << 31) | 0x7f800000) == -kInf);
CHECK(std::isnan(math::bit_cast<float>(0x7fc00000)));
}
// Check math::clz*
TEST_CASE("clz", "[math]")
{
CHECK(math::clz32(1) == 31);
CHECK(math::clz32(-1) == 0);
for (int i = 0; i < 32; ++i)
{
CHECK(math::clz32(1 << i) == 31 - i);
CHECK(math::clz32((1 << i) | (rand() & ((1 << i) - 1))) == 31 - i);
}
CHECK(math::clz64(1) == 63);
CHECK(math::clz64(-1) == 0);
for (int i = 0; i < 64; ++i)
{
CHECK(math::clz64(1ll << i) == 63 - i);
CHECK(math::clz64((1ll << i) | (rand() & ((1ll << i) - 1))) == 63 - i);
}
}
// Check math::rotateleft32
TEST_CASE("rotateleft32", "[math]")
{
CHECK(math::rotateleft32(0xabcdef01, 24) == 0x01abcdef);
CHECK(math::rotateleft32(0xffff0000, 16) == 0x0000ffff);
}
// Check math::msb.
TEST_CASE("msb", "[math]")
{
CHECK(math::msb(0) == 0);
CHECK(math::msb(1) == 1);
CHECK(math::msb(2) == 2);
CHECK(math::msb(3) == 2);
CHECK(math::msb(4) == 3);
CHECK(math::msb(5) == 3);
CHECK(math::msb(6) == 3);
CHECK(math::msb(7) == 3);
CHECK(math::msb(8) == 4);
CHECK(math::msb(9) == 4);
for (int i = 0; i < 29; ++i)
{
CHECK(math::msb(10 << i) == 4 + i);
}
CHECK(math::msb(0xffffffff) == 32);
}
// Check math::round_up_to_multiple_of.
TEST_CASE("round_up_to_multiple_of", "[math]")
{
CHECK(math::round_up_to_multiple_of<4>(0) == 0);
CHECK(math::round_up_to_multiple_of<4>(3) == 4);
CHECK(math::round_up_to_multiple_of<8>(16) == 16);
CHECK(math::round_up_to_multiple_of<8>(24) == 24);
CHECK(math::round_up_to_multiple_of<8>(25) == 32);
CHECK(math::round_up_to_multiple_of<8>(31) == 32);
CHECK(math::round_up_to_multiple_of<8>(32) == 32);
for (size_t i = 0; i < 10; ++i)
{
CHECK(math::round_up_to_multiple_of<1>(i) == i);
}
CHECK(math::round_up_to_multiple_of<2>(~size_t(0)) == 0);
}