randperm.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <test_common.h>
  2. #include <igl/randperm.h>
  3. #include <random>
  4. TEST_CASE("randperm: default_rng_reproduce_identity", "[igl]")
  5. {
  6. int n = 100;
  7. Eigen::VectorXi I1, I2;
  8. std::srand(6);
  9. igl::randperm(100, I1);
  10. std::srand(6);
  11. igl::randperm(100, I2);
  12. test_common::assert_eq(I1, I2);
  13. }
  14. namespace randperm
  15. {
  16. template<typename URBG>
  17. void test_reproduce()
  18. {
  19. int n = 100;
  20. Eigen::VectorXi I1, I2;
  21. Eigen::MatrixXi Ix1, Ix2;
  22. URBG rng1(6);
  23. URBG rng2(6);
  24. igl::randperm(100, I1, rng1);
  25. igl::randperm(100, I2, rng2);
  26. igl::randperm(100, Ix1, rng1);
  27. igl::randperm(100, Ix2, rng2);
  28. test_common::assert_eq(I1, I2);
  29. test_common::assert_eq(Ix1, Ix2);
  30. test_common::assert_neq(I1, Ix1);
  31. test_common::assert_neq(I2, Ix2);
  32. }
  33. }
  34. TEST_CASE("randperm: minstd_rand0_reproduce_identity", "[igl]")
  35. {
  36. randperm::test_reproduce<std::minstd_rand0>();
  37. }
  38. TEST_CASE("randperm: minstd_rand_reproduce_identity", "[igl]")
  39. {
  40. randperm::test_reproduce<std::minstd_rand>();
  41. }
  42. TEST_CASE("randperm: mt19937_reproduce_identity", "[igl]")
  43. {
  44. randperm::test_reproduce<std::mt19937>();
  45. }
  46. TEST_CASE("randperm: mt19937_64_reproduce_identity", "[igl]")
  47. {
  48. randperm::test_reproduce<std::mt19937_64>();
  49. }
  50. TEST_CASE("randperm: default_identity", "[igl]")
  51. {
  52. int n = 100;
  53. Eigen::VectorXi I1, I2;
  54. Eigen::MatrixXi Ix1, Ix2;
  55. std::srand(0);
  56. igl::randperm(100, I1);
  57. igl::randperm(100, Ix1);
  58. std::srand(0);
  59. igl::randperm(100, I2);
  60. igl::randperm(100, Ix2);
  61. test_common::assert_eq(I1, I2);
  62. test_common::assert_eq(Ix1, Ix2);
  63. }