test_common.h 6.3 KB

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