test_common.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <igl/STR.h>
  21. template<>
  22. struct Catch::StringMaker<std::tuple<int,int,int> >
  23. {
  24. static std::string convert(std::tuple<int,int,int> const& t)
  25. {
  26. return
  27. STR("("<<std::get<0>(t)<<","<<std::get<1>(t)<<","<<std::get<2>(t)<<")");
  28. }
  29. };
  30. template<>
  31. struct Catch::StringMaker<std::tuple<int,int,double> >
  32. {
  33. static std::string convert(std::tuple<int,int,double> const& t)
  34. {
  35. return
  36. STR("("<<std::get<0>(t)<<","<<std::get<1>(t)<<","<<std::get<2>(t)<<")");
  37. }
  38. };
  39. namespace test_common
  40. {
  41. template<typename Param, typename Fun>
  42. void run_test_cases(const std::vector<Param> &params, Fun test_case)
  43. {
  44. for(const auto &p : params)
  45. {
  46. // Can't use INFO( p ) because we're not sure how to print p
  47. test_case(p);
  48. }
  49. }
  50. template<typename Fun>
  51. void run_test_cases(const std::vector<std::string> &params, Fun test_case)
  52. {
  53. for(const auto &p : params)
  54. {
  55. INFO( p );
  56. test_case(p);
  57. }
  58. }
  59. inline std::vector<std::string> closed_genus_0_meshes()
  60. {
  61. return
  62. {
  63. "cube.obj",
  64. "decimated-knight.obj",
  65. "boolean_minus_test_cube.obj",
  66. "boolean_minus_test_green.obj",
  67. };
  68. };
  69. inline std::vector<std::string> closed_manifold_meshes()
  70. {
  71. std::vector<std::string> meshes = closed_genus_0_meshes();
  72. meshes.insert(meshes.end(),
  73. {
  74. "TinyTorus.obj",
  75. });
  76. return meshes;
  77. };
  78. inline std::vector<std::string> manifold_meshes()
  79. {
  80. std::vector<std::string> meshes = closed_manifold_meshes();
  81. meshes.insert(meshes.end(),
  82. {
  83. "bunny.off",
  84. "elephant.off",
  85. "hemisphere.obj",
  86. });
  87. return meshes;
  88. };
  89. inline std::vector<std::string> tet_meshes()
  90. {
  91. return
  92. {
  93. "decimated-knight.mesh"
  94. };
  95. };
  96. inline std::vector<std::string> all_meshes()
  97. {
  98. std::vector<std::string> meshes = manifold_meshes();
  99. meshes.insert(meshes.end(),
  100. {
  101. "truck.obj",
  102. });
  103. return meshes;
  104. };
  105. inline std::string data_path(std::string s)
  106. {
  107. return std::string(LIBIGL_DATA_DIR) + "/" + s;
  108. };
  109. template <typename DerivedA, typename DerivedB>
  110. void assert_eq(
  111. const Eigen::MatrixBase<DerivedA> & A,
  112. const Eigen::MatrixBase<DerivedB> & B)
  113. {
  114. // Sizes should match
  115. REQUIRE(A.rows() == B.rows());
  116. REQUIRE(A.cols() == B.cols());
  117. for(int i = 0;i<A.rows();i++)
  118. {
  119. for(int j = 0;j<A.cols();j++)
  120. {
  121. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  122. // know where the disagreement is.
  123. std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  124. std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  125. REQUIRE(Aijv == Bijv);
  126. }
  127. }
  128. }
  129. template <typename DerivedA, typename DerivedB>
  130. void assert_neq(
  131. const Eigen::MatrixBase<DerivedA> & A,
  132. const Eigen::MatrixBase<DerivedB> & B)
  133. {
  134. // Sizes should match
  135. REQUIRE(A.rows() == B.rows());
  136. REQUIRE(A.cols() == B.cols());
  137. bool all_equals = true;
  138. for(int i = 0;i<A.rows();i++)
  139. {
  140. for(int j = 0;j<A.cols();j++)
  141. {
  142. if (A(i,j) != B(i,j))
  143. {
  144. all_equals = false;
  145. }
  146. }
  147. }
  148. REQUIRE_FALSE(all_equals);
  149. }
  150. template <typename DerivedA, typename DerivedB>
  151. void assert_eq(
  152. const Eigen::SparseMatrix<DerivedA> & A,
  153. const Eigen::SparseMatrix<DerivedB> & B)
  154. {
  155. // Sizes should match
  156. REQUIRE(A.rows() == B.rows());
  157. REQUIRE(A.cols() == B.cols());
  158. Eigen::Matrix<long int,Eigen::Dynamic, 1> AI,AJ;
  159. Eigen::Matrix<long int,Eigen::Dynamic, 1> BI,BJ;
  160. Eigen::Matrix<DerivedA,Eigen::Dynamic, 1> AV;
  161. Eigen::Matrix<DerivedB,Eigen::Dynamic, 1> BV;
  162. // Assumes A and B are in same Major Ordering
  163. igl::find(A,AI,AJ,AV);
  164. igl::find(B,BI,BJ,BV);
  165. // This doesn't generalized to assert_near nicely, and it makes it hard to
  166. // tell which entries are different:
  167. assert_eq(AI,BI);
  168. assert_eq(AJ,BJ);
  169. assert_eq(AV,BV);
  170. }
  171. template <typename DerivedA, typename DerivedB, typename EpsType>
  172. void assert_near(
  173. const Eigen::MatrixBase<DerivedA> & A,
  174. const Eigen::MatrixBase<DerivedB> & B,
  175. const EpsType & eps)
  176. {
  177. // Sizes should match
  178. REQUIRE(A.rows() == B.rows());
  179. REQUIRE(A.cols() == B.cols());
  180. for(int i = 0;i<A.rows();i++)
  181. {
  182. for(int j = 0;j<A.cols();j++)
  183. {
  184. // Create an ijv tuple to trick GoogleTest into printing (i,j) so we
  185. // know where the disagreement is.
  186. //
  187. // Equivalent to ASSERT_NEAR(Aijv,Bijv)
  188. CAPTURE( i );
  189. CAPTURE( j );
  190. {
  191. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)};
  192. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)+eps};
  193. REQUIRE(A(i,j) < B(i,j)+eps);
  194. }
  195. {
  196. // std::tuple<int,int,typename DerivedA::Scalar> Aijv {i,j,A(i,j)+eps};
  197. // std::tuple<int,int,typename DerivedB::Scalar> Bijv {i,j,B(i,j)};
  198. REQUIRE(A(i,j)+eps > B(i,j));
  199. }
  200. }
  201. }
  202. }
  203. }