diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/random/normal_ut.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/random/normal_ut.cpp')
-rw-r--r-- | util/random/normal_ut.cpp | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/util/random/normal_ut.cpp b/util/random/normal_ut.cpp index c65f59d038..42b6cc4ba2 100644 --- a/util/random/normal_ut.cpp +++ b/util/random/normal_ut.cpp @@ -1,81 +1,81 @@ -#include "normal.h" -#include "fast.h" - +#include "normal.h" +#include "fast.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/vector.h> - -#include <functional> - + +#include <util/generic/vector.h> + +#include <functional> + Y_UNIT_TEST_SUITE(TestNormalDistribution) { Y_UNIT_TEST(TestDefined) { - volatile auto x = NormalRandom<float>(0, 1) + NormalRandom<double>(0, 1) + NormalRandom<long double>(0, 1); - - (void)x; - } - - template <class T> - static void TestMD(std::function<T()> f, T m, T d) { + volatile auto x = NormalRandom<float>(0, 1) + NormalRandom<double>(0, 1) + NormalRandom<long double>(0, 1); + + (void)x; + } + + template <class T> + static void TestMD(std::function<T()> f, T m, T d) { TVector<T> v; - - v.reserve(20000); - - for (size_t i = 0; i < 20000; ++i) { - v.push_back(f()); - } - - long double mm = 0; - long double vv = 0; - - for (auto x : v) { - mm += x; - } - + + v.reserve(20000); + + for (size_t i = 0; i < 20000; ++i) { + v.push_back(f()); + } + + long double mm = 0; + long double vv = 0; + + for (auto x : v) { + mm += x; + } + mm /= v.size(); - - for (auto x : v) { - vv += (mm - x) * (mm - x); - } - + + for (auto x : v) { + vv += (mm - x) * (mm - x); + } + vv /= v.size(); - - long double dd = std::sqrt(vv); - - UNIT_ASSERT_DOUBLES_EQUAL(m, mm, (m + 1) * 0.05); - UNIT_ASSERT_DOUBLES_EQUAL(d, dd, (d + 1) * 0.05); - } - + + long double dd = std::sqrt(vv); + + UNIT_ASSERT_DOUBLES_EQUAL(m, mm, (m + 1) * 0.05); + UNIT_ASSERT_DOUBLES_EQUAL(d, dd, (d + 1) * 0.05); + } + Y_UNIT_TEST(Test1) { - TestMD<float>(&StdNormalRandom<float>, 0, 1); - TestMD<double>(&StdNormalRandom<double>, 0, 1); - TestMD<long double>(&StdNormalRandom<long double>, 0, 1); - } - - template <class T> - std::function<T()> GenFunc1(T m, T d) { - return [m, d]() { - return NormalRandom<T>(m, d); - }; - } - - template <class T> - std::function<T()> GenFunc2(T m, T d) { - TFastRng<ui64> rng(17); - - return [rng, m, d]() mutable { - return NormalDistribution<T>(rng, m, d); - }; - } - + TestMD<float>(&StdNormalRandom<float>, 0, 1); + TestMD<double>(&StdNormalRandom<double>, 0, 1); + TestMD<long double>(&StdNormalRandom<long double>, 0, 1); + } + + template <class T> + std::function<T()> GenFunc1(T m, T d) { + return [m, d]() { + return NormalRandom<T>(m, d); + }; + } + + template <class T> + std::function<T()> GenFunc2(T m, T d) { + TFastRng<ui64> rng(17); + + return [rng, m, d]() mutable { + return NormalDistribution<T>(rng, m, d); + }; + } + Y_UNIT_TEST(Test2) { - TestMD<float>(GenFunc1<float>(2, 3), 2, 3); - TestMD<double>(GenFunc1<double>(3, 4), 3, 4); - TestMD<long double>(GenFunc1<long double>(4, 5), 4, 5); - } - + TestMD<float>(GenFunc1<float>(2, 3), 2, 3); + TestMD<double>(GenFunc1<double>(3, 4), 3, 4); + TestMD<long double>(GenFunc1<long double>(4, 5), 4, 5); + } + Y_UNIT_TEST(Test3) { - TestMD<float>(GenFunc2<float>(20, 30), 20, 30); - TestMD<double>(GenFunc2<double>(30, 40), 30, 40); - TestMD<long double>(GenFunc2<long double>(40, 50), 40, 50); - } -} + TestMD<float>(GenFunc2<float>(20, 30), 20, 30); + TestMD<double>(GenFunc2<double>(30, 40), 30, 40); + TestMD<long double>(GenFunc2<long double>(40, 50), 40, 50); + } +} |