shuffle_ut.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "fast.h"
  2. #include "shuffle.h"
  3. #include "mersenne.h"
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <util/generic/ylimits.h>
  6. Y_UNIT_TEST_SUITE(TRandUtilsTest) {
  7. template <typename... A>
  8. static void TestRange(A&&... args) {
  9. TString s0, s1;
  10. ShuffleRange(s1, args...);
  11. s1 = "0";
  12. ShuffleRange(s1, args...);
  13. s1 = "01";
  14. ShuffleRange(s1, args...);
  15. s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  16. s0 = s1.copy();
  17. ShuffleRange(s1, args...);
  18. UNIT_ASSERT(s0 != s1); // if shuffle does work, chances it will fail are 1 to 64!.
  19. }
  20. template <typename... A>
  21. static void TestIter(A&&... args) {
  22. TString s0, s1;
  23. auto f = [&]() {
  24. auto b = s1.begin();
  25. auto e = s1.end();
  26. Shuffle(b, e, args...);
  27. };
  28. s1 = "0";
  29. f();
  30. s1 = "01";
  31. f();
  32. s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  33. s0 = s1.copy();
  34. f();
  35. UNIT_ASSERT(s0 != s1); // if shuffle does work, chances it will fail are 1 to 64!.
  36. }
  37. template <typename... A>
  38. static void TestIterPartial(A&&... args) {
  39. TString s0, s1;
  40. auto f = [&](int shuffledSize) {
  41. auto b = s1.begin();
  42. auto e = s1.end();
  43. PartialShuffle(b, e, shuffledSize, args...);
  44. };
  45. s1 = "";
  46. f(0);
  47. s1 = "01";
  48. f(1);
  49. s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.;?!";
  50. s0 = s1.copy();
  51. f(2);
  52. size_t matchesCount = 0;
  53. for (size_t i = 0; i < s0.size(); ++i) {
  54. matchesCount += (s0[i] == s1[i]);
  55. }
  56. UNIT_ASSERT(matchesCount >= s1.size() - 4); // partial shuffle doesn't make non-necessary swaps.
  57. s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.;?!";
  58. s0 = s1.copy();
  59. f(64);
  60. UNIT_ASSERT(s0 != s1); // if partial shuffle does work, chances it will fail are 1 to 64!.
  61. }
  62. Y_UNIT_TEST(TestShuffle) {
  63. TestRange();
  64. TestIterPartial();
  65. }
  66. Y_UNIT_TEST(TestShuffleMersenne64) {
  67. TMersenne<ui64> prng(42);
  68. TestRange(prng);
  69. TestIterPartial(prng);
  70. }
  71. Y_UNIT_TEST(TestShuffleMersenne32) {
  72. TMersenne<ui32> prng(24);
  73. TestIter(prng);
  74. TestIterPartial(prng);
  75. }
  76. Y_UNIT_TEST(TestShuffleFast32) {
  77. TFastRng32 prng(24, 0);
  78. TestIter(prng);
  79. TestIterPartial(prng);
  80. }
  81. Y_UNIT_TEST(TestShuffleFast64) {
  82. TFastRng64 prng(24, 0, 25, 1);
  83. TestIter(prng);
  84. TestIterPartial(prng);
  85. }
  86. }