test_common.h 4.8 KB

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