random_ut.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "random.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/ylimits.h>
  4. template <class T>
  5. static inline void AssertRange(T v, T r1, T r2) {
  6. UNIT_ASSERT(v >= r1);
  7. UNIT_ASSERT(v < r2);
  8. }
  9. Y_UNIT_TEST_SUITE(TRandomNumberTest) {
  10. template <typename T>
  11. void TestAll(T n) {
  12. for (T i = 0; i < n; ++i) {
  13. while (RandomNumber<T>(n) != i) {
  14. }
  15. }
  16. }
  17. template <typename T>
  18. void TestSome(T n) {
  19. for (int i = 0; i < 100; ++i) {
  20. UNIT_ASSERT(RandomNumber<T>(n) < n);
  21. }
  22. }
  23. template <typename T>
  24. void TestType() {
  25. TestAll<T>(1);
  26. TestAll<T>(2);
  27. TestAll<T>(3);
  28. TestAll<T>(4);
  29. TestAll<T>(5);
  30. TestAll<T>(6);
  31. TestAll<T>(9);
  32. TestAll<T>(15);
  33. TestAll<T>(16);
  34. TestSome<T>(Max<T>());
  35. TestSome<T>(Max<T>() - 1);
  36. TestSome<T>(Max<T>() - 2);
  37. TestSome<T>(Max<T>() - 3);
  38. TestSome<T>(Max<T>() - 4);
  39. TestSome<T>(Max<T>() - 5);
  40. TestSome<T>(Max<T>() - 7);
  41. TestSome<T>(Max<T>() - 8);
  42. TestSome<T>(Max<T>() - 2222);
  43. TestSome<T>(Max<T>() - 22222);
  44. }
  45. Y_UNIT_TEST(TestWithLimit) {
  46. TestType<unsigned short>();
  47. TestType<unsigned int>();
  48. TestType<unsigned long>();
  49. TestType<unsigned long long>();
  50. }
  51. Y_UNIT_TEST(TestRandomNumberFloat) {
  52. for (size_t i = 0; i < 1000; ++i) {
  53. AssertRange<float>(RandomNumber<float>(), 0.0, 1.0);
  54. }
  55. }
  56. Y_UNIT_TEST(TestRandomNumberDouble) {
  57. for (size_t i = 0; i < 1000; ++i) {
  58. AssertRange<double>(RandomNumber<double>(), 0.0, 1.0);
  59. }
  60. }
  61. Y_UNIT_TEST(TestRandomNumberLongDouble) {
  62. for (size_t i = 0; i < 1000; ++i) {
  63. AssertRange<long double>(RandomNumber<long double>(), 0.0, 1.0);
  64. }
  65. }
  66. Y_UNIT_TEST(TestBoolean) {
  67. while (RandomNumber<bool>()) {
  68. }
  69. while (!RandomNumber<bool>()) {
  70. }
  71. }
  72. Y_UNIT_TEST(TestResetSeed) {
  73. SetRandomSeed(42);
  74. for (const ui32 el : {
  75. 102,
  76. 179,
  77. 92,
  78. 14,
  79. 106,
  80. 71,
  81. 188,
  82. 20,
  83. 102,
  84. 121,
  85. 210,
  86. 214,
  87. 74,
  88. 202,
  89. 87,
  90. 116,
  91. 99,
  92. 103,
  93. 151,
  94. 130,
  95. 149,
  96. 52,
  97. 1,
  98. 87,
  99. 235,
  100. 157,
  101. 37,
  102. 129,
  103. 191,
  104. 187,
  105. 20,
  106. 160,
  107. 203,
  108. 57,
  109. 21,
  110. 252,
  111. 235,
  112. 88,
  113. 48,
  114. 218,
  115. 58,
  116. 254,
  117. 169,
  118. 255,
  119. 219,
  120. 187,
  121. 207,
  122. 14,
  123. 189,
  124. 189,
  125. 174,
  126. 189,
  127. 50,
  128. 107,
  129. 54,
  130. 243,
  131. 63,
  132. 248,
  133. 130,
  134. 228,
  135. 50,
  136. 134,
  137. 20,
  138. 72,
  139. }) {
  140. UNIT_ASSERT_EQUAL(RandomNumber<ui32>(1 << 8), el);
  141. }
  142. }
  143. }