forked from simonfuhrmann/mve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtest_functions.cc
84 lines (70 loc) · 2.72 KB
/
gtest_functions.cc
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
/*
* Test cases for math functions.
* Written by Simon Fuhrmann and Michael Waechter.
*/
#include <gtest/gtest.h>
#include "math/vector.h"
#include "math/matrix.h"
#include "math/functions.h"
#include "math/matrix_tools.h"
TEST(MathFunctionsTest, GaussianTest)
{
using namespace math;
EXPECT_EQ(gaussian(0.0f, 1.0f), 1.0f);
EXPECT_NEAR(gaussian( 1.0f, 1.0f), 0.606530659712633f, 1e-14f);
EXPECT_NEAR(gaussian(-1.0f, 1.0f), 0.606530659712633f, 1e-14f);
EXPECT_NEAR(gaussian( 2.0f, 1.0f), 0.135335283236613f, 1e-14f);
EXPECT_NEAR(gaussian(-2.0f, 1.0f), 0.135335283236613f, 1e-14f);
EXPECT_NEAR(gaussian( 1.0f, 2.0f), 0.882496902584595f, 1e-14f);
EXPECT_NEAR(gaussian(-1.0f, 2.0f), 0.882496902584595f, 1e-14f);
EXPECT_NEAR(gaussian( 2.0f, 2.0f), 0.606530659712633f, 1e-14f);
EXPECT_NEAR(gaussian(-2.0f, 2.0f), 0.606530659712633f, 1e-14f);
}
TEST(MathFunctionsTest, RoundTest)
{
EXPECT_EQ(1.0f, math::round(1.1f));
EXPECT_EQ(2.0f, math::round(1.5f));
EXPECT_EQ(2.0f, math::round(1.7f));
EXPECT_EQ(-1.0f, math::round(-0.5f));
EXPECT_EQ(-1.0f, math::round(-0.7f));
EXPECT_EQ(-1.0f, math::round(-1.1f));
EXPECT_EQ(-2.0f, math::round(-1.5f));
EXPECT_EQ(0.0f, math::round(-0.4f));
EXPECT_EQ(1.0, math::round(1.1));
EXPECT_EQ(2.0, math::round(1.5));
EXPECT_EQ(2.0, math::round(1.7));
EXPECT_EQ(-1.0, math::round(-0.5));
EXPECT_EQ(-1.0, math::round(-0.7));
EXPECT_EQ(-1.0, math::round(-1.1));
EXPECT_EQ(-2.0, math::round(-1.5));
EXPECT_EQ(0.0, math::round(-0.4));
}
TEST(MathFunctionsTest, FastPowTest)
{
EXPECT_EQ(1, math::fastpow(10, 0));
EXPECT_EQ(10, math::fastpow(10, 1));
EXPECT_EQ(100, math::fastpow(10, 2));
EXPECT_EQ(1000, math::fastpow(10, 3));
EXPECT_EQ(1, math::fastpow(2, 0));
EXPECT_EQ(2, math::fastpow(2, 1));
EXPECT_EQ(4, math::fastpow(2, 2));
EXPECT_EQ(8, math::fastpow(2, 3));
EXPECT_EQ(16, math::fastpow(2, 4));
EXPECT_EQ(32, math::fastpow(2, 5));
EXPECT_EQ(64, math::fastpow(2, 6));
EXPECT_EQ(128, math::fastpow(2, 7));
EXPECT_EQ(256, math::fastpow(2, 8));
EXPECT_EQ(512, math::fastpow(2, 9));
EXPECT_EQ(1024, math::fastpow(2, 10));
}
TEST(MathFunctionsTest, PopcountReturnsTheNumberOfOneBitsInAnUnsignedInteger)
{
EXPECT_EQ(0, math::popcount(std::uint32_t(0x0)));
EXPECT_EQ(0, math::popcount(std::uint64_t(0x0)));
EXPECT_EQ(32, math::popcount(std::uint32_t(-1)));
EXPECT_EQ(64, math::popcount(std::uint64_t(-1)));
EXPECT_EQ(16, math::popcount(std::uint32_t(0xF0F0F0F0)));
EXPECT_EQ(32, math::popcount(std::uint64_t(0xF0F0F0F0F0F0F0F0)));
EXPECT_EQ(9, math::popcount(std::uint32_t(0x13030303)));
EXPECT_EQ(17, math::popcount(std::uint64_t(0x1303030303030303)));
}