aboutsummaryrefslogtreecommitdiffstats
path: root/util/random/mersenne_ut.cpp
blob: 0a113acdf1e577ba2c0335a3f58899387c73b292 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "mersenne.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/stream/output.h>

#define UI32(x) x##ul

Y_UNIT_TEST_SUITE(TMersenneRndTest) {
    template <class T>
    inline void Test(const T* res, size_t len) {
        TMersenne<T> m;

        for (size_t i = 0; i < len; ++i) {
            UNIT_ASSERT_EQUAL(m.GenRand(), res[i]);
        }
    }

    Y_UNIT_TEST(Test32) {
        const ui32 res[] = {
            UI32(2325592414),
            UI32(482149846),
            UI32(4177211283),
            UI32(3872387439),
            UI32(1663027210),
            UI32(2005191859),
            UI32(666881213),
            UI32(3289399202),
            UI32(2514534568),
            UI32(3882134983),
        };

        Test<ui32>(res, Y_ARRAY_SIZE(res));
    }

    Y_UNIT_TEST(Test64) {
        const ui64 res[] = {
            ULL(13735441942630277712),
            ULL(10468394322237346228),
            ULL(5051557175812687784),
            ULL(8252857936377966838),
            ULL(4330799099585512958),
            ULL(327094098846779408),
            ULL(6143667654738189122),
            ULL(6387112078486713335),
            ULL(3862502196831460488),
            ULL(11231499428520958715),
        };

        Test<ui64>(res, Y_ARRAY_SIZE(res));
    }

    Y_UNIT_TEST(TestGenRand64) {
        TMersenne<ui32> rng;

        for (size_t i = 0; i < 100; ++i) {
            UNIT_ASSERT(rng.GenRand64() > ULL(12345678912));
        }
    }

    Y_UNIT_TEST(TestCopy32) {
        TMersenne<ui32> r1(1);
        TMersenne<ui32> r2(2);

        UNIT_ASSERT_VALUES_UNEQUAL(r1.GenRand(), r2.GenRand());

        r2 = r1;

        UNIT_ASSERT_VALUES_EQUAL(r1.GenRand(), r2.GenRand());
    }

    Y_UNIT_TEST(TestCopy64) {
        TMersenne<ui64> r1(1);
        TMersenne<ui64> r2(2);

        UNIT_ASSERT_VALUES_UNEQUAL(r1.GenRand(), r2.GenRand());

        r2 = r1;

        UNIT_ASSERT_VALUES_EQUAL(r1.GenRand(), r2.GenRand());
    }
} // Y_UNIT_TEST_SUITE(TMersenneRndTest)