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