test_common.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. // These are not directly used but would otherwise be included in most files.
  3. // Leaving them included here.
  4. #include <igl/read_triangle_mesh.h>
  5. #include <igl/readDMAT.h>
  6. #include <igl/find.h>
  7. #include <Eigen/Core>
  8. #include <catch2/catch.hpp>
  9. #include <cctype>
  10. #include <string>
  11. #include <functional>
  12. #include <algorithm>
  13. #include <tuple>
  14. // Disable lengthy tests in debug mode
  15. #ifdef NDEBUG
  16. #define IGL_DEBUG_OFF ""
  17. #else
  18. #define IGL_DEBUG_OFF "[!hide]"
  19. #endif
  20. #if !defined(NDEBUG) && defined(__linux__)
  21. #include <fenv.h>
  22. #define IGL_PUSH_FPE \
  23. fexcept_t current_exceptions; \
  24. fegetexceptflag(&current_exceptions, FE_ALL_EXCEPT); \
  25. fedisableexcept(FE_ALL_EXCEPT);
  26. #define IGL_POP_FPE \
  27. /* Surprise! Use fesetexceptflag here not feenableexcept */ \
  28. fesetexceptflag(&current_exceptions,FE_ALL_EXCEPT);
  29. #else
  30. // No-op
  31. #define IGL_PUSH_FPE
  32. #define IGL_POP_FPE
  33. #endif
  34. #include <igl/STR.h>
  35. template<>
  36. struct Catch::StringMaker<std::tuple<int,int,int> >
  37. {
  38. static std::string convert(std::tuple<int,int,int> const& t)
  39. {
  40. return
  41. STR("("<<std::get<0>(t)<<","<<std::get<1>(t)<<","<<std::get<2>(t)<<")");
  42. }
  43. };
  44. template<>
  45. struct Catch::StringMaker<std::tuple<int,int,double> >
  46. {
  47. static std::string convert(std::tuple<int,int,double> const& t)
  48. {
  49. return
  50. STR("("<<std::get<0>(t)<<","<<std::get<1>(t)<<","<<std::get<2>(t)<<")");
  51. }
  52. };
  53. namespace test_common
  54. {
  55. template<typename Param, typename Fun>
  56. void run_test_cases(const std::vector<Param> &params, Fun test_case)
  57. {
  58. for(const auto &p : params)
  59. {
  60. // Can't use INFO( p ) because we're not sure how to print p
  61. test_case(p);
  62. }
  63. }
  64. template<typename Fun>
  65. void run_test_cases(const std::vector<std::string> &params, Fun test_case)
  66. {
  67. for(const auto &p : params)
  68. {
  69. INFO( p );
  70. test_case(p);
  71. }
  72. }
  73. inline std::vector<std::string> closed_genus_0_meshes()
  74. {
  75. return
  76. {
  77. "cube.obj",
  78. "decimated-knight.obj",
  79. "boolean_minus_test_cube.obj",
  80. "boolean_minus_test_green.obj",
  81. };
  82. };
  83. inline std::vector<std::string> closed_manifold_meshes()
  84. {
  85. std::vector<std::string> meshes = closed_genus_0_meshes();
  86. meshes.insert(meshes.end(),
  87. {
  88. "TinyTorus.obj",
  89. });
  90. return meshes;
  91. };
  92. inline std::vector<std::string> manifold_meshes()
  93. {
  94. std::vector<std::string> meshes = closed_manifold_meshes();
  95. meshes.insert(meshes.end(),
  96. {
  97. "bunny.off",
  98. "elephant.off",
  99. "hemisphere.obj",
  100. });
  101. return meshes;
  102. };
  103. inline std::vector<std::string> tet_meshes()
  104. {
  105. return
  106. {
  107. "decimated-knight.mesh"
  108. };
  109. };
  110. inline std::vector<std::string> all_meshes()
  111. {
  112. std::vector<std::string> meshes = manifold_meshes();
  113. meshes.insert(meshes.end(),
  114. {
  115. "truck.obj",
  116. });
  117. return meshes;
  118. };
  119. inline std::string data_path(std::string s)
  120. {
  121. return std::string(LIBIGL_DATA_DIR) + "/" + s;
  122. };
  123. template <typename DerivedA, typename DerivedB>
  124. void assert_eq(
  125. const Eigen::MatrixBase<DerivedA> & A,
  126. const Eigen::MatrixBase<DerivedB> & B)
  127. {
  128. // Sizes should match
  129. REQUIRE(A.rows() == B.rows());
  130. REQUIRE(A.cols() == B.cols());
  131. for(int i = 0;i<A.rows();i++)
  132. {
  133. for(int j = 0;j<A.cols();j++)
  134. {
  135. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  136. // know where the disagreement is.
  137. std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  138. std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  139. REQUIRE(Aijv == Bijv);
  140. }
  141. }
  142. }
  143. template <typename DerivedA, typename DerivedB>
  144. void assert_neq(
  145. const Eigen::MatrixBase<DerivedA> & A,
  146. const Eigen::MatrixBase<DerivedB> & B)
  147. {
  148. // Sizes should match
  149. REQUIRE(A.rows() == B.rows());
  150. REQUIRE(A.cols() == B.cols());
  151. bool all_equals = true;
  152. for(int i = 0;i<A.rows();i++)
  153. {
  154. for(int j = 0;j<A.cols();j++)
  155. {
  156. if (A(i,j) != B(i,j))
  157. {
  158. all_equals = false;
  159. }
  160. }
  161. }
  162. REQUIRE_FALSE(all_equals);
  163. }
  164. template <typename DerivedA, typename DerivedB>
  165. void assert_eq(
  166. const Eigen::SparseMatrix<DerivedA> & A,
  167. const Eigen::SparseMatrix<DerivedB> & B)
  168. {
  169. // Sizes should match
  170. REQUIRE(A.rows() == B.rows());
  171. REQUIRE(A.cols() == B.cols());
  172. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  173. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  174. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  175. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  176. // Assumes A and B are in same Major Ordering
  177. igl::find(A,AI,AJ,AV);
  178. igl::find(B,BI,BJ,BV);
  179. // This doesn't generalized to assert_near nicely, and it makes it hard to
  180. // tell which entries are different:
  181. assert_eq(AI,BI);
  182. assert_eq(AJ,BJ);
  183. assert_eq(AV,BV);
  184. }
  185. template <typename DerivedA, typename DerivedB, typename EpsType>
  186. void assert_near(
  187. const Eigen::MatrixBase<DerivedA> & A,
  188. const Eigen::MatrixBase<DerivedB> & B,
  189. const EpsType & eps)
  190. {
  191. // Sizes should match
  192. REQUIRE(A.rows() == B.rows());
  193. REQUIRE(A.cols() == B.cols());
  194. for(int i = 0;i<A.rows();i++)
  195. {
  196. for(int j = 0;j<A.cols();j++)
  197. {
  198. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  199. // know where the disagreement is.
  200. //
  201. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  202. CAPTURE( i );
  203. CAPTURE( j );
  204. {
  205. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  206. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  207. REQUIRE(A(i,j) < B(i,j)+eps);
  208. }
  209. {
  210. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  211. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  212. REQUIRE(A(i,j)+eps > B(i,j));
  213. }
  214. }
  215. }
  216. }
  217. }