aboutsummaryrefslogtreecommitdiffstats
path: root/util/random/normal_ut.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/random/normal_ut.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/random/normal_ut.cpp')
-rw-r--r--util/random/normal_ut.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/util/random/normal_ut.cpp b/util/random/normal_ut.cpp
new file mode 100644
index 0000000000..42b6cc4ba2
--- /dev/null
+++ b/util/random/normal_ut.cpp
@@ -0,0 +1,81 @@
+#include "normal.h"
+#include "fast.h"
+
+#include <library/cpp/testing/unittest/registar.h>
+
+#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) {
+ 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;
+ }
+
+ mm /= v.size();
+
+ 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);
+ }
+
+ 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);
+ };
+ }
+
+ 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);
+ }
+
+ 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);
+ }
+}