randperm.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  31. }
  32. TEST_CASE("randperm: minstd_rand0_reproduce_identity", "[igl]")
  33. {
  34. randperm::test_reproduce<std::minstd_rand0>();
  35. }
  36. TEST_CASE("randperm: minstd_rand_reproduce_identity", "[igl]")
  37. {
  38. randperm::test_reproduce<std::minstd_rand>();
  39. }
  40. TEST_CASE("randperm: mt19937_reproduce_identity", "[igl]")
  41. {
  42. randperm::test_reproduce<std::mt19937>();
  43. }
  44. TEST_CASE("randperm: mt19937_64_reproduce_identity", "[igl]")
  45. {
  46. randperm::test_reproduce<std::mt19937_64>();
  47. }
  48. TEST_CASE("randperm: ranlux24_base_reproduce_identity", "[igl]")
  49. {
  50. randperm::test_reproduce<std::ranlux24_base>();
  51. }
  52. TEST_CASE("randperm: ranlux48_base_reproduce_identity", "[igl]")
  53. {
  54. randperm::test_reproduce<std::ranlux48_base>();
  55. }
  56. TEST_CASE("randperm: ranlux24_reproduce_identity", "[igl]")
  57. {
  58. randperm::test_reproduce<std::ranlux24>();
  59. }
  60. TEST_CASE("randperm: ranlux48_reproduce_identity", "[igl]")
  61. {
  62. randperm::test_reproduce<std::ranlux48>();
  63. }
  64. TEST_CASE("randperm: knuth_b_reproduce_identity", "[igl]")
  65. {
  66. randperm::test_reproduce<std::knuth_b>();
  67. }
  68. TEST_CASE("randperm: default_identity", "[igl]")
  69. {
  70. int n = 100;
  71. Eigen::VectorXi I1, I2;
  72. Eigen::MatrixXi Ix1, Ix2;
  73. std::srand(0);
  74. igl::randperm(100, I1);
  75. igl::randperm(100, Ix1);
  76. std::srand(0);
  77. igl::randperm(100, I2);
  78. igl::randperm(100, Ix2);
  79. test_common::assert_eq(I1, I2);
  80. test_common::assert_eq(Ix1, Ix2);
  81. }