CMake: Add option to link against system-wide Gflags library
[blender.git] / tests / gtests / testing / testing.h
1 #ifndef __BLENDER_TESTING_H__
2 #define __BLENDER_TESTING_H__
3
4 #include "glog/logging.h"
5 #include "gflags/gflags.h"
6 #include "gtest/gtest.h"
7
8 #define EXPECT_V3_NEAR(a, b, eps) \
9   { \
10     EXPECT_NEAR(a[0], b[0], eps); \
11     EXPECT_NEAR(a[1], b[1], eps); \
12     EXPECT_NEAR(a[2], b[2], eps); \
13   } (void) 0
14
15 #define EXPECT_V4_NEAR(a, b, eps) \
16 { \
17         EXPECT_NEAR(a[0], b[0], eps); \
18         EXPECT_NEAR(a[1], b[1], eps); \
19         EXPECT_NEAR(a[2], b[2], eps); \
20         EXPECT_NEAR(a[3], b[3], eps); \
21         } (void) 0
22
23 #define EXPECT_M3_NEAR(a, b, eps) \
24 do { \
25         EXPECT_V3_NEAR(a[0], b[0], eps); \
26         EXPECT_V3_NEAR(a[1], b[1], eps); \
27         EXPECT_V3_NEAR(a[2], b[2], eps); \
28 } while(false);
29
30 #define EXPECT_M4_NEAR(a, b, eps) \
31 do { \
32         EXPECT_V3_NEAR(a[0], b[0], eps); \
33         EXPECT_V3_NEAR(a[1], b[1], eps); \
34         EXPECT_V3_NEAR(a[2], b[2], eps); \
35         EXPECT_V4_NEAR(a[3], b[3], eps); \
36 } while(false);
37
38 #define EXPECT_MATRIX_NEAR(a, b, tolerance) \
39 do { \
40   bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
41   EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
42   EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
43   if (dims_match) { \
44     for (int r = 0; r < a.rows(); ++r) { \
45       for (int c = 0; c < a.cols(); ++c) { \
46         EXPECT_NEAR(a(r, c), b(r, c), tolerance) \
47           << "r=" << r << ", c=" << c << "."; \
48       } \
49     } \
50   } \
51 } while(false);
52
53 #define EXPECT_MATRIX_NEAR_ZERO(a, tolerance) \
54 do { \
55   for (int r = 0; r < a.rows(); ++r) { \
56     for (int c = 0; c < a.cols(); ++c) { \
57       EXPECT_NEAR(0.0, a(r, c), tolerance) \
58         << "r=" << r << ", c=" << c << "."; \
59     } \
60   } \
61 } while(false);
62
63 #define EXPECT_MATRIX_EQ(a, b) \
64 do { \
65   bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
66   EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
67   EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
68   if (dims_match) { \
69     for (int r = 0; r < a.rows(); ++r) { \
70       for (int c = 0; c < a.cols(); ++c) { \
71         EXPECT_EQ(a(r, c), b(r, c)) \
72           << "r=" << r << ", c=" << c << "."; \
73       } \
74     } \
75   } \
76 } while(false);
77
78 // Check that sin(angle(a, b)) < tolerance.
79 #define EXPECT_MATRIX_PROP(a, b, tolerance) \
80 do { \
81   bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
82   EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
83   EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
84   if (dims_match) { \
85     double c = CosinusBetweenMatrices(a, b); \
86     if (c * c < 1) { \
87       double s = sqrt(1 - c * c); \
88       EXPECT_NEAR(0, s, tolerance); \
89     } \
90   } \
91 } while(false);
92
93 #ifdef LIBMV_NUMERIC_NUMERIC_H
94 template<class TMat>
95 double CosinusBetweenMatrices(const TMat &a, const TMat &b) {
96   return (a.array() * b.array()).sum() /
97       libmv::FrobeniusNorm(a) / libmv::FrobeniusNorm(b);
98 }
99 #endif
100
101 template <typename T>
102 inline void EXPECT_EQ_ARRAY(const T *expected, T *actual, const size_t N) {
103   for(size_t i = 0; i < N; ++i) {
104     EXPECT_EQ(expected[i], actual[i]);
105   }
106 }
107
108 template <typename T>
109 inline void EXPECT_EQ_ARRAY_ND(const T *expected, T *actual, const size_t N, const size_t D) {
110   for(size_t i = 0; i < N; ++i) {
111     for(size_t j = 0; j < D; ++j) {
112       EXPECT_EQ(expected[i][j], actual[i][j]);
113     }
114   }
115 }
116
117 #endif  // __BLENDER_TESTING_H__