Debian Codegraph @

pypy/module/_random/test/test_random.py

raw symbols SCIP
1 class AppTestRandom:
2 spaceconfig = {
3 "usemodules": ['_random', 'time'],
4 }
5
6 def test_dict(self):
7 import _random
8 _random.__dict__ # crashes if entries in __init__.py can't be resolved
9
10 def test_random(self):
11 import _random
12 # XXX quite a bad test
13 rnd = _random.Random(1)
14 lst1 = [rnd.random() for i in range(100)]
15 rnd.seed(1)
16 lst2 = [rnd.random() for i in range(100)]
17 assert lst1 == lst2
18 smaller = 0
19 for elt in lst1:
20 assert 0 <= elt <= 1
21 if elt < 0.5:
22 smaller += 1
23 # quite unlikely to fail, but well
24 assert smaller > 10
25
26 def test_getstate_setstate(self):
27 import _random
28 rnd1 = _random.Random()
29 rnd1.random()
30 rnd2 = _random.Random()
31 assert rnd1.getstate() != rnd2.getstate()
32 state = rnd2.getstate()
33 rnd1.setstate(state)
34 assert [rnd1.random() for i in range(100)] == [
35 rnd2.random() for i in range(100)]
36
37 def test_setstate_negative(self):
38 # XXX does only make sense on a 32 bit platform
39 import _random
40 rnd1 = _random.Random()
41 # does not crash
42 rnd1.setstate((-1, ) * 624 + (0, ))
43
44 def test_failed_setstate(self):
45 import _random
46 rnd = _random.Random()
47 rnd.seed()
48 start_state = rnd.getstate()
49 raises(TypeError, rnd.setstate, None)
50 raises(ValueError, rnd.setstate, (1, 2, 3))
51 raises(TypeError, rnd.setstate, ('a',)*624 + (1,))
52 raises(ValueError, rnd.setstate, (1,)*624 + (625,))
53 # None of these failed calls should have changed the state
54 assert rnd.getstate() == start_state
55
56 def test_state_repr(self):
57 # since app-level jumpahead salts with repr(state),
58 # it is important the repr is consistent with cpython
59 import _random
60 rnd = _random.Random()
61 rnd.seed(1234)
62 state = rnd.getstate()
63 s = repr(state)
64 assert len(s) == 7331
65
66 def test_seed(self):
67 import _random, sys
68 rnd = _random.Random()
69 rnd.seed()
70 different_nums = []
71 mask = sys.maxsize * 2 + 1
72 for obj in ["spam and eggs", 3.14, 1+2j, 'a', tuple('abc')]:
73 nums = []
74 for o in [obj, hash(obj) & mask, -(hash(obj) & mask)]:
75 rnd.seed(o)
76 nums.append([rnd.random() for i in range(100)])
77 n1 = nums[0]
78 different_nums.append(n1)
79 for n2 in nums[1:]:
80 assert n1 == n2
81 n1 = different_nums[0]
82 for n2 in different_nums[1:]:
83 assert n1 != n2
84
85 def test_seedargs(self):
86 import _random
87 rnd = _random.Random()
88 for arg in [None, 0, 1, -1, 10**20, -(10**20),
89 3.14, 1+2j, 'a', tuple('abc'), 0xffffffffff]:
90 rnd.seed(arg)
91 for arg in [[1, 2, 3], dict(one=1)]:
92 raises(TypeError, rnd.seed, arg)
93 raises(TypeError, rnd.seed, 1, 2)
94 raises(TypeError, type(rnd), [])
95
96 def test_seed_uses_the_time(self):
97 import _random
98 rnd = _random.Random()
99 rnd.seed()
100 state1 = rnd.getstate()
101 import time; time.sleep(0.01)
102 rnd.seed()
103 state2 = rnd.getstate()
104 assert state1 != state2
105
106 def test_randbits(self):
107 import _random
108 rnd = _random.Random()
109 for n in range(1, 10):
110 k = rnd.getrandbits(n)
111 assert 0 <= k < 2 ** n
112 for n in range(10, 1000, 15):
113 k = rnd.getrandbits(n)
114 assert 0 <= k < 2 ** n
115 assert rnd.getrandbits(30) != 0 # Fails every 1e10 runs
116 assert rnd.getrandbits(0) == 0
117 raises(ValueError, rnd.getrandbits, -1)
118
119
120 def test_subclass(self):
121 import _random
122 class R(_random.Random):
123 def __init__(self, x=1):
124 self.x = x
125 r = R(x=15)
126 assert r.x == 15
127
128 def test_exact_result(self):
129 # this passes on CPython 2.7.9 on Linux 32 and Linux 64
130 import _random
131 rnd = _random.Random(-3**31)
132 x = rnd.random()
133 assert x == 0.8181851342382107
134
135 rnd = _random.Random(23)
136 x = rnd.getrandbits(15)
137 assert x == 30305
138 x = rnd.getrandbits(64)
139 assert x == 17498688041408372654
140 x = rnd.getrandbits(1000)
141 assert x == 7853306986850247776154838978003111681903290767754178881472967951394642454293814053922268204822337224228382717987163860072072211540796042432365942518253892663330370311555959936030836699709599651063582158137533337405488381774503601280079968480579765012148668221201232526424837774516436871086966847306285
142