21 #ifndef LIB_LIGHTMETRICA_TEST_BASE_MATH_H
22 #define LIB_LIGHTMETRICA_TEST_BASE_MATH_H
25 #ifndef LM_ENABLE_MULTI_PRECISION
26 #define LM_ENABLE_MULTI_PRECISION
28 #include <lightmetrica/math.types.h>
29 #include <lightmetrica/math.basic.h>
30 #include <lightmetrica/align.h>
32 namespace mp = boost::multiprecision;
35 LM_TEST_NAMESPACE_BEGIN
37 typedef ::testing::Types<float, double, Math::BigFloat> MathTestTypes;
43 inline ::testing::AssertionResult ExpectNearRelative(
const T& expected,
const T& actual,
const T& epsilon)
45 T diff = Math::Abs(expected - actual) / Math::Abs(expected);
48 return ::testing::AssertionFailure()
49 <<
"Expected " << expected <<
", "
50 <<
"Actual " << actual <<
", "
51 <<
"Diff " << diff <<
", "
52 <<
"Epsilon " << epsilon;
56 return ::testing::AssertionSuccess();
61 inline ::testing::AssertionResult ExpectNear(
const T& expected,
const T& actual,
const T& epsilon)
63 T diff = Math::Abs(expected - actual);
66 return ::testing::AssertionFailure()
67 <<
"Expected " << expected <<
", "
68 <<
"Actual " << actual <<
", "
69 <<
"Diff " << diff <<
", "
70 <<
"Epsilon " << epsilon;
74 return ::testing::AssertionSuccess();
79 inline ::testing::AssertionResult ExpectNear(
const T& expected,
const T& actual)
81 return ExpectNear(expected, actual, Math::TConstants<T>::EpsLarge());
85 inline ::testing::AssertionResult ExpectNear<Math::BigFloat>(
const Math::BigFloat& expected,
const Math::BigFloat& actual)
87 Math::BigFloat diff = mp::abs(expected - actual);
88 Math::BigFloat epsilon = Math::TConstants<Math::BigFloat>::EpsLarge();
91 return ::testing::AssertionFailure()
92 <<
"Expected " << expected.str() <<
", "
93 <<
"Actual " << actual.str() <<
", "
94 <<
"Diff " << diff.str() <<
", "
95 <<
"Epsilon " << epsilon.str();
99 return ::testing::AssertionSuccess();
103 template <
typename T>
104 inline ::testing::AssertionResult ExpectVec2Near(
const Math::TVec2<T>& expect,
const Math::TVec2<T>& actual)
106 for (
int i = 0; i < 2; i++)
108 auto result = ExpectNear(expect[i], actual[i]);
115 return ::testing::AssertionSuccess();
118 template <
typename T>
119 inline ::testing::AssertionResult ExpectVec3Near(
const Math::TVec3<T>& expect,
const Math::TVec3<T>& actual,
const T& epsilon)
121 for (
int i = 0; i < 3; i++)
123 auto result = ExpectNear(expect[i], actual[i], epsilon);
130 return ::testing::AssertionSuccess();
133 template <
typename T>
134 inline ::testing::AssertionResult ExpectVec3Near(
const Math::TVec3<T>& expect,
const Math::TVec3<T>& actual)
136 for (
int i = 0; i < 3; i++)
138 auto result = ExpectNear(expect[i], actual[i]);
145 return ::testing::AssertionSuccess();
148 template <
typename T>
149 inline ::testing::AssertionResult ExpectVec4Near(
const Math::TVec4<T>& expect,
const Math::TVec4<T>& actual)
151 for (
int i = 0; i < 4; i++)
153 auto result = ExpectNear(expect[i], actual[i]);
160 return ::testing::AssertionSuccess();
163 template <
typename T>
164 inline ::testing::AssertionResult ExpectMat3Near(
const Math::TMat3<T>& expect,
const Math::TMat3<T>& actual)
166 for (
int i = 0; i < 3; i++)
168 for (
int j = 0; j < 3; j++)
170 auto result = ExpectNear(expect[i][j], actual[i][j]);
178 return ::testing::AssertionSuccess();
181 template <
typename T>
182 inline ::testing::AssertionResult ExpectMat4Near(
const Math::TMat4<T>& expect,
const Math::TMat4<T>& actual)
184 for (
int i = 0; i < 4; i++)
186 for (
int j = 0; j < 4; j++)
188 auto result = ExpectNear(expect[i][j], actual[i][j]);
196 return ::testing::AssertionSuccess();
199 LM_TEST_NAMESPACE_END
202 #endif // LIB_LIGHTMETRICA_TEST_BASE_MATH_H
Definition: base.math.h:40