File: test_random.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (142 lines) | stat: -rw-r--r-- 4,761 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class AppTestRandom:
    spaceconfig = {
        "usemodules": ['_random', 'time'],
    }

    def test_dict(self):
        import _random
        _random.__dict__  # crashes if entries in __init__.py can't be resolved

    def test_random(self):
        import _random
        # XXX quite a bad test
        rnd = _random.Random(1)
        lst1 = [rnd.random() for i in range(100)]
        rnd.seed(1)
        lst2 = [rnd.random() for i in range(100)]
        assert lst1 == lst2
        smaller = 0
        for elt in lst1:
            assert 0 <= elt <= 1
            if elt < 0.5:
                smaller += 1
        # quite unlikely to fail, but well
        assert smaller > 10

    def test_getstate_setstate(self):
        import _random
        rnd1 = _random.Random()
        rnd1.random()
        rnd2 = _random.Random()
        assert rnd1.getstate() != rnd2.getstate()
        state = rnd2.getstate()
        rnd1.setstate(state)
        assert [rnd1.random() for i in range(100)] == [
                    rnd2.random() for i in range(100)]

    def test_setstate_negative(self):
        # XXX does only make sense on a 32 bit platform
        import _random
        rnd1 = _random.Random()
        # does not crash
        rnd1.setstate((-1, ) * 624 + (0, ))

    def test_failed_setstate(self):
        import _random
        rnd = _random.Random()
        rnd.seed()
        start_state = rnd.getstate()
        raises(TypeError, rnd.setstate, None)
        raises(ValueError, rnd.setstate, (1, 2, 3))
        raises(TypeError, rnd.setstate, ('a',)*624 + (1,))
        raises(ValueError, rnd.setstate, (1,)*624 + (625,))
        # None of these failed calls should have changed the state
        assert rnd.getstate() == start_state

    def test_state_repr(self):
        # since app-level jumpahead salts with repr(state),
        # it is important the repr is consistent with cpython
        import _random
        rnd = _random.Random()
        rnd.seed(1234)
        state = rnd.getstate()
        s = repr(state)
        assert len(s) == 7331

    def test_seed(self):
        import _random, sys
        rnd = _random.Random()
        rnd.seed()
        different_nums = []
        mask = sys.maxsize * 2 + 1
        for obj in ["spam and eggs", 3.14, 1+2j, 'a', tuple('abc')]:
            nums = []
            for o in [obj, hash(obj) & mask, -(hash(obj) & mask)]:
                rnd.seed(o)
                nums.append([rnd.random() for i in range(100)])
            n1 = nums[0]
            different_nums.append(n1)
            for n2 in nums[1:]:
                assert n1 == n2
        n1 = different_nums[0]
        for n2 in different_nums[1:]:
            assert n1 != n2

    def test_seedargs(self):
        import _random
        rnd = _random.Random()
        for arg in [None, 0, 1, -1, 10**20, -(10**20),
                    3.14, 1+2j, 'a', tuple('abc'), 0xffffffffff]:
            rnd.seed(arg)
        for arg in [[1, 2, 3], dict(one=1)]:
            raises(TypeError, rnd.seed, arg)
        raises(TypeError, rnd.seed, 1, 2)
        raises(TypeError, type(rnd), [])

    def test_seed_uses_the_time(self):
        import _random
        rnd = _random.Random()
        rnd.seed()
        state1 = rnd.getstate()
        import time; time.sleep(0.01)
        rnd.seed()
        state2 = rnd.getstate()
        assert state1 != state2

    def test_randbits(self):
        import _random
        rnd = _random.Random()
        for n in range(1, 10):
            k = rnd.getrandbits(n)
            assert 0 <= k < 2 ** n
        for n in range(10, 1000, 15):
            k = rnd.getrandbits(n)
            assert 0 <= k < 2 ** n
        assert rnd.getrandbits(30) != 0 # Fails every 1e10 runs
        assert rnd.getrandbits(0) == 0
        raises(ValueError, rnd.getrandbits, -1)


    def test_subclass(self):
        import _random
        class R(_random.Random):
            def __init__(self, x=1):
                self.x = x
        r = R(x=15)
        assert r.x == 15

    def test_exact_result(self):
        # this passes on CPython 2.7.9 on Linux 32 and Linux 64
        import _random
        rnd = _random.Random(-3**31)
        x = rnd.random()
        assert x == 0.8181851342382107

        rnd = _random.Random(23)
        x = rnd.getrandbits(15)
        assert x == 30305
        x = rnd.getrandbits(64)
        assert x == 17498688041408372654
        x = rnd.getrandbits(1000)
        assert x == 7853306986850247776154838978003111681903290767754178881472967951394642454293814053922268204822337224228382717987163860072072211540796042432365942518253892663330370311555959936030836699709599651063582158137533337405488381774503601280079968480579765012148668221201232526424837774516436871086966847306285