1 #ifndef LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_TEST_TEST_MACROS_H_
2 #define LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_TEST_TEST_MACROS_H_
3
4 #include <gtest/gtest.h>
5
6 #include <cmath>
7
8 #include <private/dvr/numeric.h>
9
10 namespace android {
11 namespace dvr {
12
13 template <int N, typename A, typename B, typename T>
CmpArrayLikeFloatEq(const char * expectedStr,const char * actualStr,const char * toleranceStr,const A & expected,const B & actual,const T & tolerance)14 ::testing::AssertionResult CmpArrayLikeFloatEq(
15 const char* expectedStr, const char* actualStr, const char* toleranceStr,
16 const A& expected, const B& actual, const T& tolerance) {
17 for (int i = 0; i < N; ++i) {
18 if (!IsEqual(expected[i], actual[i], tolerance)) {
19 return ::testing::AssertionFailure()
20 << "\"" << expectedStr << "\" and \"" << actualStr
21 << "\" differ at element " << i << " by at least " << tolerance
22 << " : "
23 << " Expected \"" << expected[i] << "\", was \"" << actual[i]
24 << "\".";
25 }
26 }
27
28 return ::testing::AssertionSuccess();
29 }
30
31 template <int N, typename A, typename B, typename T>
CmpMatrixLikeFloatEq(const char * expectedStr,const char * actualStr,const char * toleranceStr,const A & expected,const B & actual,const T & tolerance)32 ::testing::AssertionResult CmpMatrixLikeFloatEq(
33 const char* expectedStr, const char* actualStr, const char* toleranceStr,
34 const A& expected, const B& actual, const T& tolerance) {
35 for (int r = 0; r < N; ++r) {
36 for (int c = 0; c < N; ++c) {
37 if (!IsEqual(expected(r, c), actual(r, c), tolerance)) {
38 return ::testing::AssertionFailure()
39 << "\"" << expectedStr << "\" and \"" << actualStr
40 << "\" differ at (" << r << "," << c << ")"
41 << " by at least " << tolerance << " : "
42 << " Expected \"" << expected(r, c) << "\", was \""
43 << actual(r, c) << "\".";
44 }
45 }
46 }
47
48 return ::testing::AssertionSuccess();
49 }
50
51 template <int N, typename A, typename B, typename T>
CmpArrayLikeFloatNe(const char * expectedStr,const char * actualStr,const char * toleranceStr,const A & expected,const B & actual,const T & tolerance)52 ::testing::AssertionResult CmpArrayLikeFloatNe(
53 const char* expectedStr, const char* actualStr, const char* toleranceStr,
54 const A& expected, const B& actual, const T& tolerance) {
55 for (int i = 0; i < N; ++i) {
56 if (!IsEqual(expected[i], actual[i], tolerance)) {
57 return ::testing::AssertionSuccess();
58 }
59 }
60
61 ::testing::Message message;
62 message << "Expected \"" << expectedStr
63 << "\" to differ from provided value \"" << actualStr
64 << "\" by at least " << tolerance << ".";
65
66 return ::testing::AssertionFailure(message);
67 }
68
69 template <int N, typename A, typename B, typename T>
CmpMatrixLikeFloatNe(const char * expectedStr,const char * actualStr,const char * toleranceStr,const A & expected,const B & actual,const T & tolerance)70 ::testing::AssertionResult CmpMatrixLikeFloatNe(
71 const char* expectedStr, const char* actualStr, const char* toleranceStr,
72 const A& expected, const B& actual, const T& tolerance) {
73 for (int r = 0; r < N; ++r) {
74 for (int c = 0; c < N; ++c) {
75 if (!IsEqual(expected(r, c), actual(r, c), tolerance)) {
76 return ::testing::AssertionSuccess();
77 }
78 }
79 }
80
81 ::testing::Message message;
82 message << "Expected \"" << expectedStr
83 << "\" to differ from provided value \"" << actualStr
84 << "\" by at least " << tolerance << ".";
85
86 return ::testing::AssertionFailure(message);
87 }
88
89 } // namespace dvr
90 } // namespace android
91
92 #define EXPECT_VEC3_NEAR(expected, actual, tol) \
93 EXPECT_PRED_FORMAT3(android::dvr::CmpArrayLikeFloatEq<3>, expected, actual, \
94 tol)
95
96 #define EXPECT_VEC3_NOT_NEAR(expected, actual, tol) \
97 EXPECT_PRED_FORMAT3(android::dvr::CmpArrayLikeFloatNe<3>, expected, actual, \
98 tol)
99
100 #define EXPECT_QUAT_NEAR(expected, actual, tol) \
101 EXPECT_PRED_FORMAT3(android::dvr::CmpArrayLikeFloatEq<3>, expected.coeffs(), \
102 actual.coeffs(), tol)
103
104 #define EXPECT_QUAT_NOT_NEAR(expected, actual, tol) \
105 EXPECT_PRED_FORMAT3(android::dvr::CmpArrayLikeFloatNe<3>, expected.coeffs(), \
106 actual.coeffs(), tol)
107
108 #define EXPECT_MAT4_NEAR(expected, actual, tol) \
109 EXPECT_PRED_FORMAT3(android::dvr::CmpMatrixLikeFloatEq<4>, expected, actual, \
110 tol)
111
112 #define EXPECT_MAT4_NOT_NEAR(expected, actual, tol) \
113 EXPECT_PRED_FORMAT3(android::dvr::CmpMatrixLikeFloatNe<4>, expected, actual, \
114 tol)
115
116 #define EXPECT_MAT3_NEAR(expected, actual, tol) \
117 EXPECT_PRED_FORMAT3(android::dvr \
118 : CmpMatrixLikeFloatEq<3>, expected, actual, tol)
119
120 #define EXPECT_MAT3_NOT_NEAR(expected, actual, tol) \
121 EXPECT_PRED_FORMAT3(android::dvr::CmpMatrixLikeFloatNe<3>, expected, actual, \
122 tol)
123
124 #endif // LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_TEST_TEST_MACROS_H_
125