testing.h 3.8 KB

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