[pypy-svn] r32469 - in pypy/dist/pypy/module/_random: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Tue Sep 19 00:16:07 CEST 2006
Author: cfbolz
Date: Tue Sep 19 00:16:04 2006
New Revision: 32469
Modified:
pypy/dist/pypy/module/_random/interp_random.py
pypy/dist/pypy/module/_random/test/test_mersenne.py
pypy/dist/pypy/module/_random/test/test_random.py
Log:
oops, all seeds were effectively the same :-(
Modified: pypy/dist/pypy/module/_random/interp_random.py
==============================================================================
--- pypy/dist/pypy/module/_random/interp_random.py (original)
+++ pypy/dist/pypy/module/_random/interp_random.py Tue Sep 19 00:16:04 2006
@@ -35,15 +35,18 @@
w_n = space.abs(space.hash(w_n))
key = []
w_one = space.newlong(1)
+ w_two = space.newlong(2)
w_thirtytwo = space.newlong(32)
# 0xffffffff
- w_masklower = space.sub(space.pow(w_one, w_thirtytwo, space.w_None),
+ w_masklower = space.sub(space.pow(w_two, w_thirtytwo, space.w_None),
w_one)
while space.is_true(w_n):
w_chunk = space.and_(w_n, w_masklower)
chunk = r_uint(space.int_w(w_chunk))
key.append(chunk)
w_n = space.rshift(w_n, w_thirtytwo)
+ if not key:
+ key = [r_uint(0)]
self._rnd.init_by_array(key)
seed.unwrap_spec = ['self', ObjSpace, W_Root]
Modified: pypy/dist/pypy/module/_random/test/test_mersenne.py
==============================================================================
--- pypy/dist/pypy/module/_random/test/test_mersenne.py (original)
+++ pypy/dist/pypy/module/_random/test/test_mersenne.py Tue Sep 19 00:16:04 2006
@@ -22,6 +22,9 @@
assert rnd.state[:14] == [2147483648, 1269538435, 699006892, 381364451,
172015551, 3237099449, 3609464087, 2187366456, 654585064,
2665903765, 3735624613, 1241943673, 2038528247, 3774211972]
+ # try arrays of various sizes to test for corner cases
+ for size in [N, N - 1, N + 1, N // 2, 2 * N]:
+ rnd.init_by_array(range(N))
def test_jumpahead():
rnd = Random()
Modified: pypy/dist/pypy/module/_random/test/test_random.py
==============================================================================
--- pypy/dist/pypy/module/_random/test/test_random.py (original)
+++ pypy/dist/pypy/module/_random/test/test_random.py Tue Sep 19 00:16:04 2006
@@ -39,14 +39,30 @@
import _random
rnd = _random.Random()
rnd.seed()
+ different_nums = []
for obj in ["spam and eggs", 3.14, 1+2j, 'a', tuple('abc')]:
nums = []
for o in [obj, hash(obj), -hash(obj)]:
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, 0L, 1, 1L, -1, -1L, 10**20, -(10**20),
+ 3.14, 1+2j, 'a', tuple('abc')]:
+ rnd.seed(arg)
+ for arg in [range(3), dict(one=1)]:
+ raises(TypeError, rnd.seed, arg)
+ raises(TypeError, rnd.seed, 1, 2)
+ raises(TypeError, type(rnd), [])
def test_randbits(self):
import math
More information about the Pypy-commit
mailing list