fast_ut.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "fast.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(TTestFastRng) {
  4. Y_UNIT_TEST(Test1) {
  5. TFastRng32 rng1(17, 0);
  6. TReallyFastRng32 rng2(17);
  7. for (size_t i = 0; i < 1000; ++i) {
  8. UNIT_ASSERT_VALUES_EQUAL(rng1.GenRand(), rng2.GenRand());
  9. }
  10. }
  11. static ui64 R1[] = {
  12. 37,
  13. 43,
  14. 76,
  15. 17,
  16. 12,
  17. 87,
  18. 60,
  19. 4,
  20. 83,
  21. 47,
  22. 57,
  23. 81,
  24. 28,
  25. 45,
  26. 66,
  27. 74,
  28. 18,
  29. 17,
  30. 18,
  31. 75,
  32. };
  33. Y_UNIT_TEST(Test2) {
  34. TFastRng64 rng(0, 1, 2, 3);
  35. for (auto& i : R1) {
  36. UNIT_ASSERT_VALUES_EQUAL(rng.Uniform(100u), i);
  37. }
  38. }
  39. Y_UNIT_TEST(TestAdvance) {
  40. TReallyFastRng32 rng1(17);
  41. TReallyFastRng32 rng2(17);
  42. for (size_t i = 0; i < 100; i++) {
  43. rng1.GenRand();
  44. }
  45. rng2.Advance(100);
  46. UNIT_ASSERT_VALUES_EQUAL(rng1.GenRand(), rng2.GenRand());
  47. TFastRng64 rng3(0, 1, 2, 3);
  48. TFastRng64 rng4(0, 1, 2, 3);
  49. for (size_t i = 0; i < 100; i++) {
  50. rng3.GenRand();
  51. }
  52. rng4.Advance(100);
  53. UNIT_ASSERT_VALUES_EQUAL(rng3.GenRand(), rng4.GenRand());
  54. }
  55. Y_UNIT_TEST(TestAdvanceBoundaries) {
  56. TReallyFastRng32 rng1(17);
  57. TReallyFastRng32 rng2(17);
  58. TReallyFastRng32 rng3(17);
  59. rng2.Advance(0);
  60. rng3.Advance(1);
  61. UNIT_ASSERT_VALUES_EQUAL(rng1.GenRand(), rng2.GenRand());
  62. UNIT_ASSERT_VALUES_EQUAL(rng1.GenRand(), rng3.GenRand());
  63. }
  64. Y_UNIT_TEST(TestCopy) {
  65. TReallyFastRng32 r1(1);
  66. TReallyFastRng32 r2(2);
  67. UNIT_ASSERT_VALUES_UNEQUAL(r1.GenRand(), r2.GenRand());
  68. r2 = r1;
  69. UNIT_ASSERT_VALUES_EQUAL(r1.GenRand(), r2.GenRand());
  70. }
  71. Y_UNIT_TEST(Test3) {
  72. TFastRng64 rng(17);
  73. UNIT_ASSERT_VALUES_EQUAL(rng.GenRand(), ULL(14895365814383052362));
  74. }
  75. Y_UNIT_TEST(TestCompile) {
  76. TFastRng<ui32> rng1(1);
  77. TFastRng<ui64> rng2(2);
  78. TFastRng<size_t> rng3(3);
  79. rng1.GenRand();
  80. rng2.GenRand();
  81. rng3.GenRand();
  82. }
  83. const char* RNG_DATA = "At the top of the department store I,"
  84. "I bought a fur coat with fur I"
  85. "But apparently I made a blunder here -"
  86. "Doha does not warm ... Absolutely.";
  87. Y_UNIT_TEST(TestStreamCtor1) {
  88. TMemoryInput mi(RNG_DATA, strlen(RNG_DATA));
  89. TFastRng<ui32> rng(mi);
  90. UNIT_ASSERT_VALUES_EQUAL(rng.GenRand(), 1449109131u);
  91. }
  92. Y_UNIT_TEST(TestStreamCtor2) {
  93. TMemoryInput mi(RNG_DATA, strlen(RNG_DATA));
  94. TFastRng<ui64> rng(mi);
  95. UNIT_ASSERT_VALUES_EQUAL(rng.GenRand(), ULL(6223876579076085114));
  96. }
  97. } // Y_UNIT_TEST_SUITE(TTestFastRng)