randperm.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: ranlux24_base_reproduce_identity", "[igl]")
  51. {
  52. randperm::test_reproduce<std::ranlux24_base>();
  53. }
  54. TEST_CASE("randperm: ranlux48_base_reproduce_identity", "[igl]")
  55. {
  56. randperm::test_reproduce<std::ranlux48_base>();
  57. }
  58. TEST_CASE("randperm: ranlux24_reproduce_identity", "[igl]")
  59. {
  60. randperm::test_reproduce<std::ranlux24>();
  61. }
  62. TEST_CASE("randperm: ranlux48_reproduce_identity", "[igl]")
  63. {
  64. randperm::test_reproduce<std::ranlux48>();
  65. }
  66. TEST_CASE("randperm: knuth_b_reproduce_identity", "[igl]")
  67. {
  68. randperm::test_reproduce<std::knuth_b>();
  69. }
  70. TEST_CASE("randperm: default_identity", "[igl]")
  71. {
  72. int n = 100;
  73. Eigen::VectorXi I1, I2;
  74. Eigen::MatrixXi Ix1, Ix2;
  75. std::srand(0);
  76. igl::randperm(100, I1);
  77. igl::randperm(100, Ix1);
  78. std::srand(0);
  79. igl::randperm(100, I2);
  80. igl::randperm(100, Ix2);
  81. test_common::assert_eq(I1, I2);
  82. test_common::assert_eq(Ix1, Ix2);
  83. }