Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 4 | import random |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 5 | import time |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 6 | import pickle |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 7 | import warnings |
Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 8 | from math import log, exp, pi, fsum, sin |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 11 | class TestBasicOps(unittest.TestCase): |
| 12 | # Superclass with tests common to all generators. |
| 13 | # Subclasses must arrange for self.gen to retrieve the Random instance |
| 14 | # to be tested. |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 15 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 16 | def randomlist(self, n): |
| 17 | """Helper function to make a list of random numbers""" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 18 | return [self.gen.random() for i in range(n)] |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 19 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 20 | def test_autoseed(self): |
| 21 | self.gen.seed() |
| 22 | state1 = self.gen.getstate() |
Raymond Hettinger | 3081d59 | 2003-08-09 18:30:57 +0000 | [diff] [blame] | 23 | time.sleep(0.1) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 24 | self.gen.seed() # diffent seeds at different times |
| 25 | state2 = self.gen.getstate() |
| 26 | self.assertNotEqual(state1, state2) |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 27 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 28 | def test_saverestore(self): |
| 29 | N = 1000 |
| 30 | self.gen.seed() |
| 31 | state = self.gen.getstate() |
| 32 | randseq = self.randomlist(N) |
| 33 | self.gen.setstate(state) # should regenerate the same sequence |
| 34 | self.assertEqual(randseq, self.randomlist(N)) |
| 35 | |
| 36 | def test_seedargs(self): |
Mark Dickinson | 95aeae0 | 2012-06-24 11:05:30 +0100 | [diff] [blame] | 37 | # Seed value with a negative hash. |
| 38 | class MySeed(object): |
| 39 | def __hash__(self): |
| 40 | return -1729 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 41 | for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20), |
Mark Dickinson | 95aeae0 | 2012-06-24 11:05:30 +0100 | [diff] [blame] | 42 | 3.14, 1+2j, 'a', tuple('abc'), MySeed()]: |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 43 | self.gen.seed(arg) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 44 | for arg in [list(range(3)), dict(one=1)]: |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 45 | self.assertRaises(TypeError, self.gen.seed, arg) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 46 | self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4) |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 47 | self.assertRaises(TypeError, type(self.gen), []) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 48 | |
Andrew Svetlov | a2dfc35 | 2013-04-12 23:21:01 +0300 | [diff] [blame^] | 49 | def test_triangular(self): |
| 50 | # Check that triangular() correctly handles bad input. See issue 13355. |
| 51 | |
| 52 | # mode > high. |
| 53 | with self.assertRaises(ValueError): |
| 54 | random.triangular(mode=2) |
| 55 | with self.assertRaises(ValueError): |
| 56 | random.triangular(low=1, high=10, mode=11) |
| 57 | with self.assertRaises(ValueError): |
| 58 | random.triangular(low=1, high=1, mode=11) |
| 59 | |
| 60 | # mode < low. |
| 61 | with self.assertRaises(ValueError): |
| 62 | random.triangular(mode=-1) |
| 63 | with self.assertRaises(ValueError): |
| 64 | random.triangular(low=1, high=10, mode=0) |
| 65 | with self.assertRaises(ValueError): |
| 66 | random.triangular(low=1, high=1, mode=0) |
| 67 | |
| 68 | # low > high |
| 69 | with self.assertRaises(ValueError): |
| 70 | random.triangular(low=5, high=2) |
| 71 | with self.assertRaises(ValueError): |
| 72 | random.triangular(low=5, high=2, mode=1) |
| 73 | with self.assertRaises(ValueError): |
| 74 | random.triangular(low=-2, high=-5) |
| 75 | |
| 76 | self.assertEqual(random.triangular(low=10, high=10), 10) |
| 77 | self.assertEqual(random.triangular(low=10, high=10, mode=10), 10) |
| 78 | |
Raymond Hettinger | dc4872e | 2010-09-07 10:06:56 +0000 | [diff] [blame] | 79 | def test_choice(self): |
| 80 | choice = self.gen.choice |
| 81 | with self.assertRaises(IndexError): |
| 82 | choice([]) |
| 83 | self.assertEqual(choice([50]), 50) |
| 84 | self.assertIn(choice([25, 75]), [25, 75]) |
| 85 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 86 | def test_sample(self): |
| 87 | # For the entire allowable range of 0 <= k <= N, validate that |
| 88 | # the sample is of the correct length and contains only unique items |
| 89 | N = 100 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 90 | population = range(N) |
| 91 | for k in range(N+1): |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 92 | s = self.gen.sample(population, k) |
| 93 | self.assertEqual(len(s), k) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 94 | uniq = set(s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 95 | self.assertEqual(len(uniq), k) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 96 | self.assertTrue(uniq <= set(population)) |
Raymond Hettinger | 8ec7881 | 2003-01-04 05:55:11 +0000 | [diff] [blame] | 97 | self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 98 | |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 99 | def test_sample_distribution(self): |
| 100 | # For the entire allowable range of 0 <= k <= N, validate that |
| 101 | # sample generates all possible permutations |
| 102 | n = 5 |
| 103 | pop = range(n) |
| 104 | trials = 10000 # large num prevents false negatives without slowing normal case |
| 105 | def factorial(n): |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 106 | if n == 0: |
| 107 | return 1 |
| 108 | return n * factorial(n - 1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 109 | for k in range(n): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 110 | expected = factorial(n) // factorial(n-k) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 111 | perms = {} |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 112 | for i in range(trials): |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 113 | perms[tuple(self.gen.sample(pop, k))] = None |
| 114 | if len(perms) == expected: |
| 115 | break |
| 116 | else: |
| 117 | self.fail() |
| 118 | |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 119 | def test_sample_inputs(self): |
| 120 | # SF bug #801342 -- population can be any iterable defining __len__() |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 121 | self.gen.sample(set(range(20)), 2) |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 122 | self.gen.sample(range(20), 2) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 123 | self.gen.sample(range(20), 2) |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 124 | self.gen.sample(str('abcdefghijklmnopqrst'), 2) |
| 125 | self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) |
| 126 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 127 | def test_sample_on_dicts(self): |
Raymond Hettinger | 1acde19 | 2008-01-14 01:00:53 +0000 | [diff] [blame] | 128 | self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 129 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 130 | def test_gauss(self): |
| 131 | # Ensure that the seed() method initializes all the hidden state. In |
| 132 | # particular, through 2.2.1 it failed to reset a piece of state used |
| 133 | # by (and only by) the .gauss() method. |
| 134 | |
| 135 | for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
| 136 | self.gen.seed(seed) |
| 137 | x1 = self.gen.random() |
| 138 | y1 = self.gen.gauss(0, 1) |
| 139 | |
| 140 | self.gen.seed(seed) |
| 141 | x2 = self.gen.random() |
| 142 | y2 = self.gen.gauss(0, 1) |
| 143 | |
| 144 | self.assertEqual(x1, x2) |
| 145 | self.assertEqual(y1, y2) |
| 146 | |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 147 | def test_pickling(self): |
| 148 | state = pickle.dumps(self.gen) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 149 | origseq = [self.gen.random() for i in range(10)] |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 150 | newgen = pickle.loads(state) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 151 | restoredseq = [newgen.random() for i in range(10)] |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 152 | self.assertEqual(origseq, restoredseq) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 153 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 154 | def test_bug_1727780(self): |
| 155 | # verify that version-2-pickles can be loaded |
| 156 | # fine, whether they are created on 32-bit or 64-bit |
| 157 | # platforms, and that version-3-pickles load fine. |
| 158 | files = [("randv2_32.pck", 780), |
| 159 | ("randv2_64.pck", 866), |
| 160 | ("randv3.pck", 343)] |
| 161 | for file, value in files: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 162 | f = open(support.findfile(file),"rb") |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 163 | r = pickle.load(f) |
| 164 | f.close() |
Raymond Hettinger | 0515661 | 2010-09-07 04:44:52 +0000 | [diff] [blame] | 165 | self.assertEqual(int(r.random()*1000), value) |
| 166 | |
| 167 | def test_bug_9025(self): |
| 168 | # Had problem with an uneven distribution in int(n*random()) |
| 169 | # Verify the fix by checking that distributions fall within expectations. |
| 170 | n = 100000 |
| 171 | randrange = self.gen.randrange |
| 172 | k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n)) |
| 173 | self.assertTrue(0.30 < k/n < .37, (k/n)) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 174 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 175 | class SystemRandom_TestBasicOps(TestBasicOps): |
| 176 | gen = random.SystemRandom() |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 177 | |
| 178 | def test_autoseed(self): |
| 179 | # Doesn't need to do anything except not fail |
| 180 | self.gen.seed() |
| 181 | |
| 182 | def test_saverestore(self): |
| 183 | self.assertRaises(NotImplementedError, self.gen.getstate) |
| 184 | self.assertRaises(NotImplementedError, self.gen.setstate, None) |
| 185 | |
| 186 | def test_seedargs(self): |
| 187 | # Doesn't need to do anything except not fail |
| 188 | self.gen.seed(100) |
| 189 | |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 190 | def test_gauss(self): |
| 191 | self.gen.gauss_next = None |
| 192 | self.gen.seed(100) |
| 193 | self.assertEqual(self.gen.gauss_next, None) |
| 194 | |
| 195 | def test_pickling(self): |
| 196 | self.assertRaises(NotImplementedError, pickle.dumps, self.gen) |
| 197 | |
| 198 | def test_53_bits_per_float(self): |
| 199 | # This should pass whenever a C double has 53 bit precision. |
| 200 | span = 2 ** 53 |
| 201 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 202 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 203 | cum |= int(self.gen.random() * span) |
| 204 | self.assertEqual(cum, span-1) |
| 205 | |
| 206 | def test_bigrand(self): |
| 207 | # The randrange routine should build-up the required number of bits |
| 208 | # in stages so that all bit positions are active. |
| 209 | span = 2 ** 500 |
| 210 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 211 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 212 | r = self.gen.randrange(span) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 213 | self.assertTrue(0 <= r < span) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 214 | cum |= r |
| 215 | self.assertEqual(cum, span-1) |
| 216 | |
| 217 | def test_bigrand_ranges(self): |
| 218 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
| 219 | start = self.gen.randrange(2 ** i) |
| 220 | stop = self.gen.randrange(2 ** (i-2)) |
| 221 | if stop <= start: |
| 222 | return |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 223 | self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 224 | |
| 225 | def test_rangelimits(self): |
| 226 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
| 227 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 228 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 229 | |
| 230 | def test_genrandbits(self): |
| 231 | # Verify ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 232 | for k in range(1, 1000): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 233 | self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 234 | |
| 235 | # Verify all bits active |
| 236 | getbits = self.gen.getrandbits |
| 237 | for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
| 238 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 239 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 240 | cum |= getbits(span) |
| 241 | self.assertEqual(cum, 2**span-1) |
| 242 | |
| 243 | # Verify argument checking |
| 244 | self.assertRaises(TypeError, self.gen.getrandbits) |
| 245 | self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
| 246 | self.assertRaises(ValueError, self.gen.getrandbits, 0) |
| 247 | self.assertRaises(ValueError, self.gen.getrandbits, -1) |
| 248 | self.assertRaises(TypeError, self.gen.getrandbits, 10.1) |
| 249 | |
| 250 | def test_randbelow_logic(self, _log=log, int=int): |
| 251 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 252 | # show that: k = int(1.001 + _log(n, 2)) |
| 253 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 254 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 255 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 256 | numbits = i+1 |
| 257 | k = int(1.00001 + _log(n, 2)) |
| 258 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 259 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 260 | |
| 261 | n += n - 1 # check 1 below the next power of two |
| 262 | k = int(1.00001 + _log(n, 2)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 263 | self.assertIn(k, [numbits, numbits+1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 264 | self.assertTrue(2**k > n > 2**(k-2)) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 265 | |
| 266 | n -= n >> 15 # check a little farther below the next power of two |
| 267 | k = int(1.00001 + _log(n, 2)) |
| 268 | self.assertEqual(k, numbits) # note the stronger assertion |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 269 | self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 270 | |
| 271 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 272 | class MersenneTwister_TestBasicOps(TestBasicOps): |
| 273 | gen = random.Random() |
| 274 | |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 275 | def test_guaranteed_stable(self): |
| 276 | # These sequences are guaranteed to stay the same across versions of python |
| 277 | self.gen.seed(3456147, version=1) |
| 278 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 279 | ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1', |
| 280 | '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1']) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 281 | self.gen.seed("the quick brown fox", version=2) |
| 282 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
Raymond Hettinger | 3fcf002 | 2010-12-08 01:13:53 +0000 | [diff] [blame] | 283 | ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4', |
| 284 | '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1']) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 285 | |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 286 | def test_setstate_first_arg(self): |
| 287 | self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) |
| 288 | |
| 289 | def test_setstate_middle_arg(self): |
| 290 | # Wrong type, s/b tuple |
| 291 | self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) |
| 292 | # Wrong length, s/b 625 |
| 293 | self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) |
| 294 | # Wrong type, s/b tuple of 625 ints |
| 295 | self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) |
| 296 | # Last element s/b an int also |
| 297 | self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) |
| 298 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 299 | def test_referenceImplementation(self): |
| 300 | # Compare the python implementation with results from the original |
| 301 | # code. Create 2000 53-bit precision random floats. Compare only |
| 302 | # the last ten entries to show that the independent implementations |
| 303 | # are tracking. Here is the main() function needed to create the |
| 304 | # list of expected random numbers: |
| 305 | # void main(void){ |
| 306 | # int i; |
| 307 | # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; |
| 308 | # init_by_array(init, length); |
| 309 | # for (i=0; i<2000; i++) { |
| 310 | # printf("%.15f ", genrand_res53()); |
| 311 | # if (i%5==4) printf("\n"); |
| 312 | # } |
| 313 | # } |
| 314 | expected = [0.45839803073713259, |
| 315 | 0.86057815201978782, |
| 316 | 0.92848331726782152, |
| 317 | 0.35932681119782461, |
| 318 | 0.081823493762449573, |
| 319 | 0.14332226470169329, |
| 320 | 0.084297823823520024, |
| 321 | 0.53814864671831453, |
| 322 | 0.089215024911993401, |
| 323 | 0.78486196105372907] |
| 324 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 325 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 326 | actual = self.randomlist(2000)[-10:] |
| 327 | for a, e in zip(actual, expected): |
| 328 | self.assertAlmostEqual(a,e,places=14) |
| 329 | |
| 330 | def test_strong_reference_implementation(self): |
| 331 | # Like test_referenceImplementation, but checks for exact bit-level |
| 332 | # equality. This should pass on any box where C double contains |
| 333 | # at least 53 bits of precision (the underlying algorithm suffers |
| 334 | # no rounding errors -- all results are exact). |
| 335 | from math import ldexp |
| 336 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 337 | expected = [0x0eab3258d2231f, |
| 338 | 0x1b89db315277a5, |
| 339 | 0x1db622a5518016, |
| 340 | 0x0b7f9af0d575bf, |
| 341 | 0x029e4c4db82240, |
| 342 | 0x04961892f5d673, |
| 343 | 0x02b291598e4589, |
| 344 | 0x11388382c15694, |
| 345 | 0x02dad977c9e1fe, |
| 346 | 0x191d96d4d334c6] |
| 347 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 348 | actual = self.randomlist(2000)[-10:] |
| 349 | for a, e in zip(actual, expected): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 350 | self.assertEqual(int(ldexp(a, 53)), e) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 351 | |
| 352 | def test_long_seed(self): |
| 353 | # This is most interesting to run in debug mode, just to make sure |
| 354 | # nothing blows up. Under the covers, a dynamically resized array |
| 355 | # is allocated, consuming space proportional to the number of bits |
| 356 | # in the seed. Unfortunately, that's a quadratic-time algorithm, |
| 357 | # so don't make this horribly big. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 358 | seed = (1 << (10000 * 8)) - 1 # about 10K bytes |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 359 | self.gen.seed(seed) |
| 360 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 361 | def test_53_bits_per_float(self): |
| 362 | # This should pass whenever a C double has 53 bit precision. |
| 363 | span = 2 ** 53 |
| 364 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 365 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 366 | cum |= int(self.gen.random() * span) |
| 367 | self.assertEqual(cum, span-1) |
| 368 | |
| 369 | def test_bigrand(self): |
| 370 | # The randrange routine should build-up the required number of bits |
| 371 | # in stages so that all bit positions are active. |
| 372 | span = 2 ** 500 |
| 373 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 374 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 375 | r = self.gen.randrange(span) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 376 | self.assertTrue(0 <= r < span) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 377 | cum |= r |
| 378 | self.assertEqual(cum, span-1) |
| 379 | |
| 380 | def test_bigrand_ranges(self): |
| 381 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
| 382 | start = self.gen.randrange(2 ** i) |
| 383 | stop = self.gen.randrange(2 ** (i-2)) |
| 384 | if stop <= start: |
| 385 | return |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 386 | self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 387 | |
| 388 | def test_rangelimits(self): |
| 389 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 390 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 391 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 392 | |
| 393 | def test_genrandbits(self): |
| 394 | # Verify cross-platform repeatability |
| 395 | self.gen.seed(1234567) |
| 396 | self.assertEqual(self.gen.getrandbits(100), |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 397 | 97904845777343510404718956115) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 398 | # Verify ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 399 | for k in range(1, 1000): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 400 | self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 401 | |
| 402 | # Verify all bits active |
| 403 | getbits = self.gen.getrandbits |
| 404 | for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
| 405 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 406 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 407 | cum |= getbits(span) |
| 408 | self.assertEqual(cum, 2**span-1) |
| 409 | |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 410 | # Verify argument checking |
| 411 | self.assertRaises(TypeError, self.gen.getrandbits) |
| 412 | self.assertRaises(TypeError, self.gen.getrandbits, 'a') |
| 413 | self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
| 414 | self.assertRaises(ValueError, self.gen.getrandbits, 0) |
| 415 | self.assertRaises(ValueError, self.gen.getrandbits, -1) |
| 416 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 417 | def test_randbelow_logic(self, _log=log, int=int): |
| 418 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 419 | # show that: k = int(1.001 + _log(n, 2)) |
| 420 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 421 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 422 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 423 | numbits = i+1 |
| 424 | k = int(1.00001 + _log(n, 2)) |
| 425 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 426 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 427 | |
| 428 | n += n - 1 # check 1 below the next power of two |
| 429 | k = int(1.00001 + _log(n, 2)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 430 | self.assertIn(k, [numbits, numbits+1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 431 | self.assertTrue(2**k > n > 2**(k-2)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 432 | |
| 433 | n -= n >> 15 # check a little farther below the next power of two |
| 434 | k = int(1.00001 + _log(n, 2)) |
| 435 | self.assertEqual(k, numbits) # note the stronger assertion |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 436 | self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 437 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 438 | def test_randrange_bug_1590891(self): |
| 439 | start = 1000000000000 |
| 440 | stop = -100000000000000000000 |
| 441 | step = -200 |
| 442 | x = self.gen.randrange(start, stop, step) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 443 | self.assertTrue(stop < x <= start) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 444 | self.assertEqual((x+stop)%step, 0) |
| 445 | |
Raymond Hettinger | 2d0c256 | 2009-02-19 09:53:18 +0000 | [diff] [blame] | 446 | def gamma(z, sqrt2pi=(2.0*pi)**0.5): |
| 447 | # Reflection to right half of complex plane |
| 448 | if z < 0.5: |
| 449 | return pi / sin(pi*z) / gamma(1.0-z) |
| 450 | # Lanczos approximation with g=7 |
| 451 | az = z + (7.0 - 0.5) |
| 452 | return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([ |
| 453 | 0.9999999999995183, |
| 454 | 676.5203681218835 / z, |
| 455 | -1259.139216722289 / (z+1.0), |
| 456 | 771.3234287757674 / (z+2.0), |
| 457 | -176.6150291498386 / (z+3.0), |
| 458 | 12.50734324009056 / (z+4.0), |
| 459 | -0.1385710331296526 / (z+5.0), |
| 460 | 0.9934937113930748e-05 / (z+6.0), |
| 461 | 0.1659470187408462e-06 / (z+7.0), |
| 462 | ]) |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 463 | |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 464 | class TestDistributions(unittest.TestCase): |
| 465 | def test_zeroinputs(self): |
| 466 | # Verify that distributions can handle a series of zero inputs' |
| 467 | g = random.Random() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 468 | x = [g.random() for i in range(50)] + [0.0]*5 |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 469 | g.random = x[:].pop; g.uniform(1,10) |
| 470 | g.random = x[:].pop; g.paretovariate(1.0) |
| 471 | g.random = x[:].pop; g.expovariate(1.0) |
| 472 | g.random = x[:].pop; g.weibullvariate(1.0, 1.0) |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 473 | g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0) |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 474 | g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 475 | g.random = x[:].pop; g.gauss(0.0, 1.0) |
| 476 | g.random = x[:].pop; g.lognormvariate(0.0, 1.0) |
| 477 | g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) |
| 478 | g.random = x[:].pop; g.gammavariate(0.01, 1.0) |
| 479 | g.random = x[:].pop; g.gammavariate(1.0, 1.0) |
| 480 | g.random = x[:].pop; g.gammavariate(200.0, 1.0) |
| 481 | g.random = x[:].pop; g.betavariate(3.0, 3.0) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 482 | g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 483 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 484 | def test_avg_std(self): |
| 485 | # Use integration to test distribution average and standard deviation. |
| 486 | # Only works for distributions which do not consume variates in pairs |
| 487 | g = random.Random() |
| 488 | N = 5000 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 489 | x = [i/float(N) for i in range(1,N)] |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 490 | for variate, args, mu, sigmasqrd in [ |
| 491 | (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 492 | (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0), |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 493 | (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 494 | (g.vonmisesvariate, (1.23, 0), pi, pi**2/3), |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 495 | (g.paretovariate, (5.0,), 5.0/(5.0-1), |
| 496 | 5.0/((5.0-1)**2*(5.0-2))), |
| 497 | (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), |
| 498 | gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: |
| 499 | g.random = x[:].pop |
| 500 | y = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 501 | for i in range(len(x)): |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 502 | try: |
| 503 | y.append(variate(*args)) |
| 504 | except IndexError: |
| 505 | pass |
| 506 | s1 = s2 = 0 |
| 507 | for e in y: |
| 508 | s1 += e |
| 509 | s2 += (e - mu) ** 2 |
| 510 | N = len(y) |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 511 | self.assertAlmostEqual(s1/N, mu, places=2, |
| 512 | msg='%s%r' % (variate.__name__, args)) |
| 513 | self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2, |
| 514 | msg='%s%r' % (variate.__name__, args)) |
| 515 | |
| 516 | def test_constant(self): |
| 517 | g = random.Random() |
| 518 | N = 100 |
| 519 | for variate, args, expected in [ |
| 520 | (g.uniform, (10.0, 10.0), 10.0), |
| 521 | (g.triangular, (10.0, 10.0), 10.0), |
Andrew Svetlov | a2dfc35 | 2013-04-12 23:21:01 +0300 | [diff] [blame^] | 522 | (g.triangular, (10.0, 10.0, 10.0), 10.0), |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 523 | (g.expovariate, (float('inf'),), 0.0), |
| 524 | (g.vonmisesvariate, (3.0, float('inf')), 3.0), |
| 525 | (g.gauss, (10.0, 0.0), 10.0), |
| 526 | (g.lognormvariate, (0.0, 0.0), 1.0), |
| 527 | (g.lognormvariate, (-float('inf'), 0.0), 0.0), |
| 528 | (g.normalvariate, (10.0, 0.0), 10.0), |
| 529 | (g.paretovariate, (float('inf'),), 1.0), |
| 530 | (g.weibullvariate, (10.0, float('inf')), 10.0), |
| 531 | (g.weibullvariate, (0.0, 10.0), 0.0), |
| 532 | ]: |
| 533 | for i in range(N): |
| 534 | self.assertEqual(variate(*args), expected) |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 535 | |
Mark Dickinson | be5f919 | 2013-02-10 14:16:10 +0000 | [diff] [blame] | 536 | def test_von_mises_range(self): |
| 537 | # Issue 17149: von mises variates were not consistently in the |
| 538 | # range [0, 2*PI]. |
| 539 | g = random.Random() |
| 540 | N = 100 |
| 541 | for mu in 0.0, 0.1, 3.1, 6.2: |
| 542 | for kappa in 0.0, 2.3, 500.0: |
| 543 | for _ in range(N): |
| 544 | sample = g.vonmisesvariate(mu, kappa) |
| 545 | self.assertTrue( |
| 546 | 0 <= sample <= random.TWOPI, |
| 547 | msg=("vonmisesvariate({}, {}) produced a result {} out" |
| 548 | " of range [0, 2*pi]").format(mu, kappa, sample)) |
| 549 | |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 550 | def test_von_mises_large_kappa(self): |
| 551 | # Issue #17141: vonmisesvariate() was hang for large kappas |
| 552 | random.vonmisesvariate(0, 1e15) |
| 553 | random.vonmisesvariate(0, 1e100) |
| 554 | |
| 555 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 556 | class TestModule(unittest.TestCase): |
| 557 | def testMagicConstants(self): |
| 558 | self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) |
| 559 | self.assertAlmostEqual(random.TWOPI, 6.28318530718) |
| 560 | self.assertAlmostEqual(random.LOG4, 1.38629436111989) |
| 561 | self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) |
| 562 | |
| 563 | def test__all__(self): |
| 564 | # tests validity but not completeness of the __all__ list |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 565 | self.assertTrue(set(random.__all__) <= set(dir(random))) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 566 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 567 | def test_random_subclass_with_kwargs(self): |
| 568 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 569 | class Subclass(random.Random): |
| 570 | def __init__(self, newarg=None): |
| 571 | random.Random.__init__(self) |
| 572 | Subclass(newarg=1) |
| 573 | |
| 574 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 575 | def test_main(verbose=None): |
Raymond Hettinger | 28de64f | 2008-01-13 23:40:30 +0000 | [diff] [blame] | 576 | testclasses = [MersenneTwister_TestBasicOps, |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 577 | TestDistributions, |
Raymond Hettinger | b871763 | 2004-09-04 20:13:29 +0000 | [diff] [blame] | 578 | TestModule] |
| 579 | |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 580 | try: |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 581 | random.SystemRandom().random() |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 582 | except NotImplementedError: |
| 583 | pass |
| 584 | else: |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 585 | testclasses.append(SystemRandom_TestBasicOps) |
Raymond Hettinger | b871763 | 2004-09-04 20:13:29 +0000 | [diff] [blame] | 586 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 587 | support.run_unittest(*testclasses) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 588 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 589 | # verify reference counting |
| 590 | import sys |
| 591 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 592 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 593 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 594 | support.run_unittest(*testclasses) |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 595 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 596 | print(counts) |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 597 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 598 | if __name__ == "__main__": |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 599 | test_main(verbose=True) |