Skip to content

Commit 29a4444

Browse files
committed
Update "test_random.py from CPython v3.11.2"
1 parent db102cf commit 29a4444

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

Lib/test/test_random.py

+42-35
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ def __hash__(self):
5252
self.gen.seed(arg)
5353

5454
for arg in [1+2j, tuple('abc'), MySeed()]:
55-
with self.assertWarns(DeprecationWarning):
55+
with self.assertRaises(TypeError):
5656
self.gen.seed(arg)
5757

5858
for arg in [list(range(3)), dict(one=1)]:
59-
with self.assertWarns(DeprecationWarning):
60-
self.assertRaises(TypeError, self.gen.seed, arg)
59+
self.assertRaises(TypeError, self.gen.seed, arg)
6160
self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
6261
self.assertRaises(TypeError, type(self.gen), [])
6362

@@ -110,22 +109,28 @@ def test_shuffle(self):
110109
self.assertTrue(lst != shuffled_lst)
111110
self.assertRaises(TypeError, shuffle, (1, 2, 3))
112111

113-
def test_shuffle_random_argument(self):
114-
# Test random argument to shuffle.
115-
shuffle = self.gen.shuffle
116-
mock_random = unittest.mock.Mock(return_value=0.5)
117-
seq = bytearray(b'abcdefghijk')
118-
with self.assertWarns(DeprecationWarning):
119-
shuffle(seq, mock_random)
120-
mock_random.assert_called_with()
121-
122112
def test_choice(self):
123113
choice = self.gen.choice
124114
with self.assertRaises(IndexError):
125115
choice([])
126116
self.assertEqual(choice([50]), 50)
127117
self.assertIn(choice([25, 75]), [25, 75])
128118

119+
def test_choice_with_numpy(self):
120+
# Accommodation for NumPy arrays which have disabled __bool__().
121+
# See: https://github.com/python/cpython/issues/100805
122+
choice = self.gen.choice
123+
124+
class NA(list):
125+
"Simulate numpy.array() behavior"
126+
def __bool__(self):
127+
raise RuntimeError
128+
129+
with self.assertRaises(IndexError):
130+
choice(NA([]))
131+
self.assertEqual(choice(NA([50])), 50)
132+
self.assertIn(choice(NA([25, 75])), [25, 75])
133+
129134
def test_sample(self):
130135
# For the entire allowable range of 0 <= k <= N, validate that
131136
# the sample is of the correct length and contains only unique items
@@ -169,7 +174,7 @@ def test_sample_on_dicts(self):
169174
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
170175

171176
def test_sample_on_sets(self):
172-
with self.assertWarns(DeprecationWarning):
177+
with self.assertRaises(TypeError):
173178
population = {10, 20, 30, 40, 50, 60, 70}
174179
self.gen.sample(population, k=5)
175180

@@ -391,23 +396,6 @@ def test_pickling(self):
391396
restoredseq = [newgen.random() for i in range(10)]
392397
self.assertEqual(origseq, restoredseq)
393398

394-
@test.support.cpython_only
395-
def test_bug_41052(self):
396-
# _random.Random should not be allowed to serialization
397-
import _random
398-
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
399-
r = _random.Random()
400-
self.assertRaises(TypeError, pickle.dumps, r, proto)
401-
402-
@test.support.cpython_only
403-
def test_bug_42008(self):
404-
# _random.Random should call seed with first element of arg tuple
405-
import _random
406-
r1 = _random.Random()
407-
r1.seed(8675309)
408-
r2 = _random.Random(8675309)
409-
self.assertEqual(r1.random(), r2.random())
410-
411399
# TODO: RUSTPYTHON AttributeError: 'super' object has no attribute 'getstate'
412400
@unittest.expectedFailure
413401
def test_bug_1727780(self):
@@ -445,6 +433,10 @@ def test_randbytes(self):
445433
self.assertRaises(ValueError, self.gen.randbytes, -1)
446434
self.assertRaises(TypeError, self.gen.randbytes, 1.0)
447435

436+
def test_mu_sigma_default_args(self):
437+
self.assertIsInstance(self.gen.normalvariate(), float)
438+
self.assertIsInstance(self.gen.gauss(), float)
439+
448440

449441
try:
450442
random.SystemRandom().random()
@@ -592,6 +584,25 @@ def test_randbelow_logic(self, _log=log, int=int):
592584
self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
593585

594586

587+
class TestRawMersenneTwister(unittest.TestCase):
588+
@test.support.cpython_only
589+
def test_bug_41052(self):
590+
# _random.Random should not be allowed to serialization
591+
import _random
592+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
593+
r = _random.Random()
594+
self.assertRaises(TypeError, pickle.dumps, r, proto)
595+
596+
@test.support.cpython_only
597+
def test_bug_42008(self):
598+
# _random.Random should call seed with first element of arg tuple
599+
import _random
600+
r1 = _random.Random()
601+
r1.seed(8675309)
602+
r2 = _random.Random(8675309)
603+
self.assertEqual(r1.random(), r2.random())
604+
605+
595606
class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
596607
gen = random.Random()
597608

@@ -846,10 +857,6 @@ def test_randbelow_without_getrandbits(self):
846857
maxsize+1, maxsize=maxsize
847858
)
848859
self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
849-
# issue 33203: test that _randbelow returns zero on
850-
# n == 0 also in its getrandbits-independent branch.
851-
x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
852-
self.assertEqual(x, 0)
853860

854861
# This might be going too far to test a single line, but because of our
855862
# noble aim of achieving 100% test coverage we need to write a case in
@@ -1331,7 +1338,7 @@ def test__all__(self):
13311338
# tests validity but not completeness of the __all__ list
13321339
self.assertTrue(set(random.__all__) <= set(dir(random)))
13331340

1334-
@unittest.skipUnless(hasattr(os, "fork"), "fork() required")
1341+
@test.support.requires_fork()
13351342
def test_after_fork(self):
13361343
# Test the global Random instance gets reseeded in child
13371344
r, w = os.pipe()

0 commit comments

Comments
 (0)