#include "random.h" #include #include template static inline void AssertRange(T v, T r1, T r2) { UNIT_ASSERT(v >= r1); UNIT_ASSERT(v < r2); } Y_UNIT_TEST_SUITE(TRandomNumberTest) { template void TestAll(T n) { for (T i = 0; i < n; ++i) { while (RandomNumber(n) != i) { } } } template void TestSome(T n) { for (int i = 0; i < 100; ++i) { UNIT_ASSERT(RandomNumber(n) < n); } } template void TestType() { TestAll(1); TestAll(2); TestAll(3); TestAll(4); TestAll(5); TestAll(6); TestAll(9); TestAll(15); TestAll(16); TestSome(Max()); TestSome(Max() - 1); TestSome(Max() - 2); TestSome(Max() - 3); TestSome(Max() - 4); TestSome(Max() - 5); TestSome(Max() - 7); TestSome(Max() - 8); TestSome(Max() - 2222); TestSome(Max() - 22222); } Y_UNIT_TEST(TestWithLimit) { TestType(); TestType(); TestType(); TestType(); } Y_UNIT_TEST(TestRandomNumberFloat) { for (size_t i = 0; i < 1000; ++i) { AssertRange(RandomNumber(), 0.0, 1.0); } } Y_UNIT_TEST(TestRandomNumberDouble) { for (size_t i = 0; i < 1000; ++i) { AssertRange(RandomNumber(), 0.0, 1.0); } } Y_UNIT_TEST(TestRandomNumberLongDouble) { for (size_t i = 0; i < 1000; ++i) { AssertRange(RandomNumber(), 0.0, 1.0); } } Y_UNIT_TEST(TestBoolean) { while (RandomNumber()) { } while (!RandomNumber()) { } } Y_UNIT_TEST(TestResetSeed) { SetRandomSeed(42); for (const ui32 el : { 102, 179, 92, 14, 106, 71, 188, 20, 102, 121, 210, 214, 74, 202, 87, 116, 99, 103, 151, 130, 149, 52, 1, 87, 235, 157, 37, 129, 191, 187, 20, 160, 203, 57, 21, 252, 235, 88, 48, 218, 58, 254, 169, 255, 219, 187, 207, 14, 189, 189, 174, 189, 50, 107, 54, 243, 63, 248, 130, 228, 50, 134, 20, 72, }) { UNIT_ASSERT_EQUAL(RandomNumber(1 << 8), el); } } } // Y_UNIT_TEST_SUITE(TRandomNumberTest)