common_ops_ut.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "common_ops.h"
  2. #include "random.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/digest/numeric.h>
  5. #include <random>
  6. Y_UNIT_TEST_SUITE(TestCommonRNG) {
  7. template <class T>
  8. struct TRng: public TCommonRNG<T, TRng<T>> {
  9. inline T GenRand() noexcept {
  10. return IntHash(C_++);
  11. }
  12. T C_ = RandomNumber<T>();
  13. };
  14. Y_UNIT_TEST(TestUniform1) {
  15. TRng<ui32> r;
  16. for (size_t i = 0; i < 1000; ++i) {
  17. UNIT_ASSERT(r.Uniform(10) < 10);
  18. }
  19. }
  20. Y_UNIT_TEST(TestUniform2) {
  21. TRng<ui32> r;
  22. for (size_t i = 0; i < 1000; ++i) {
  23. UNIT_ASSERT(r.Uniform(1) == 0);
  24. }
  25. }
  26. Y_UNIT_TEST(TestUniform3) {
  27. TRng<ui32> r;
  28. for (size_t i = 0; i < 1000; ++i) {
  29. auto x = r.Uniform(100, 200);
  30. UNIT_ASSERT(x >= 100);
  31. UNIT_ASSERT(x < 200);
  32. }
  33. }
  34. Y_UNIT_TEST(TestStlCompatibility) {
  35. {
  36. TRng<ui32> r;
  37. r.C_ = 17;
  38. std::normal_distribution<float> nd(0, 1);
  39. UNIT_ASSERT_DOUBLES_EQUAL(nd(r), -0.877167, 0.01);
  40. }
  41. {
  42. TRng<ui64> r;
  43. r.C_ = 17;
  44. std::normal_distribution<double> nd(0, 1);
  45. UNIT_ASSERT_DOUBLES_EQUAL(nd(r), -0.5615566731, 0.01);
  46. }
  47. {
  48. TRng<ui16> r;
  49. r.C_ = 17;
  50. std::normal_distribution<long double> nd(0, 1);
  51. UNIT_ASSERT_DOUBLES_EQUAL(nd(r), -0.430375088, 0.01);
  52. }
  53. }
  54. }