diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/random/shuffle_ut.cpp |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/random/shuffle_ut.cpp')
-rw-r--r-- | util/random/shuffle_ut.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/util/random/shuffle_ut.cpp b/util/random/shuffle_ut.cpp new file mode 100644 index 00000000000..87cbae94c00 --- /dev/null +++ b/util/random/shuffle_ut.cpp @@ -0,0 +1,75 @@ +#include "fast.h" +#include "shuffle.h" +#include "mersenne.h" + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/generic/ylimits.h> + +Y_UNIT_TEST_SUITE(TRandUtilsTest) { + template <typename... A> + static void TestRange(A&&... args) { + TString s0, s1; + ShuffleRange(s1, args...); + s1 = "0"; + ShuffleRange(s1, args...); + s1 = "01"; + ShuffleRange(s1, args...); + s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + s0 = s1.copy(); + ShuffleRange(s1, args...); + UNIT_ASSERT(s0 != s1); // if shuffle does work, chances it will fail are 1 to 64!. + } + + template <typename... A> + static void TestIter(A&&... args) { + TString s0, s1; + + auto f = [&]() { + auto b = s1.begin(); + auto e = s1.end(); + + Shuffle(b, e, args...); + }; + + s1 = "0"; + f(); + + s1 = "01"; + f(); + + s1 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + s0 = s1.copy(); + f(); + + UNIT_ASSERT(s0 != s1); // if shuffle does work, chances it will fail are 1 to 64!. + } + + Y_UNIT_TEST(TestShuffle) { + TestRange(); + } + + Y_UNIT_TEST(TestShuffleMersenne64) { + TMersenne<ui64> prng(42); + + TestRange(prng); + } + + Y_UNIT_TEST(TestShuffleMersenne32) { + TMersenne<ui32> prng(24); + + TestIter(prng); + } + + Y_UNIT_TEST(TestShuffleFast32) { + TFastRng32 prng(24, 0); + + TestIter(prng); + } + + Y_UNIT_TEST(TestShuffleFast64) { + TFastRng64 prng(24, 0, 25, 1); + + TestIter(prng); + } +} |