Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 7 | from math import log, exp, sqrt, pi |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 8 | from sets import Set |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 9 | from test import test_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""" |
| 18 | return [self.gen.random() for i in xrange(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 | 785d0a3 | 2003-02-21 01:41:36 +0000 | [diff] [blame] | 23 | time.sleep(1.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): |
| 37 | for arg in [None, 0, 0L, 1, 1L, -1, -1L, 10**20, -(10**20), |
| 38 | 3.14, 1+2j, 'a', tuple('abc')]: |
| 39 | self.gen.seed(arg) |
| 40 | for arg in [range(3), dict(one=1)]: |
| 41 | self.assertRaises(TypeError, self.gen.seed, arg) |
| 42 | |
| 43 | def test_jumpahead(self): |
| 44 | self.gen.seed() |
| 45 | state1 = self.gen.getstate() |
| 46 | self.gen.jumpahead(100) |
| 47 | state2 = self.gen.getstate() # s/b distinct from state1 |
| 48 | self.assertNotEqual(state1, state2) |
| 49 | self.gen.jumpahead(100) |
| 50 | state3 = self.gen.getstate() # s/b distinct from state2 |
| 51 | self.assertNotEqual(state2, state3) |
| 52 | |
| 53 | self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg |
| 54 | self.assertRaises(TypeError, self.gen.jumpahead, "ick") # wrong type |
| 55 | self.assertRaises(TypeError, self.gen.jumpahead, 2.3) # wrong type |
| 56 | self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many |
| 57 | |
| 58 | def test_sample(self): |
| 59 | # For the entire allowable range of 0 <= k <= N, validate that |
| 60 | # the sample is of the correct length and contains only unique items |
| 61 | N = 100 |
| 62 | population = xrange(N) |
| 63 | for k in xrange(N+1): |
| 64 | s = self.gen.sample(population, k) |
| 65 | self.assertEqual(len(s), k) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 66 | uniq = Set(s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 67 | self.assertEqual(len(uniq), k) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 68 | self.failUnless(uniq <= Set(population)) |
Raymond Hettinger | 8ec7881 | 2003-01-04 05:55:11 +0000 | [diff] [blame] | 69 | self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 70 | |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 71 | def test_sample_distribution(self): |
| 72 | # For the entire allowable range of 0 <= k <= N, validate that |
| 73 | # sample generates all possible permutations |
| 74 | n = 5 |
| 75 | pop = range(n) |
| 76 | trials = 10000 # large num prevents false negatives without slowing normal case |
| 77 | def factorial(n): |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 78 | return reduce(int.__mul__, xrange(1, n), 1) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 79 | for k in xrange(n): |
| 80 | expected = factorial(n) / factorial(n-k) |
| 81 | perms = {} |
| 82 | for i in xrange(trials): |
| 83 | perms[tuple(self.gen.sample(pop, k))] = None |
| 84 | if len(perms) == expected: |
| 85 | break |
| 86 | else: |
| 87 | self.fail() |
| 88 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 89 | def test_gauss(self): |
| 90 | # Ensure that the seed() method initializes all the hidden state. In |
| 91 | # particular, through 2.2.1 it failed to reset a piece of state used |
| 92 | # by (and only by) the .gauss() method. |
| 93 | |
| 94 | for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
| 95 | self.gen.seed(seed) |
| 96 | x1 = self.gen.random() |
| 97 | y1 = self.gen.gauss(0, 1) |
| 98 | |
| 99 | self.gen.seed(seed) |
| 100 | x2 = self.gen.random() |
| 101 | y2 = self.gen.gauss(0, 1) |
| 102 | |
| 103 | self.assertEqual(x1, x2) |
| 104 | self.assertEqual(y1, y2) |
| 105 | |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame^] | 106 | def test_pickling(self): |
| 107 | state = pickle.dumps(self.gen) |
| 108 | origseq = [self.gen.random() for i in xrange(10)] |
| 109 | newgen = pickle.loads(state) |
| 110 | restoredseq = [newgen.random() for i in xrange(10)] |
| 111 | self.assertEqual(origseq, restoredseq) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 112 | |
| 113 | class WichmannHill_TestBasicOps(TestBasicOps): |
| 114 | gen = random.WichmannHill() |
| 115 | |
| 116 | def test_strong_jumpahead(self): |
| 117 | # tests that jumpahead(n) semantics correspond to n calls to random() |
| 118 | N = 1000 |
| 119 | s = self.gen.getstate() |
| 120 | self.gen.jumpahead(N) |
| 121 | r1 = self.gen.random() |
| 122 | # now do it the slow way |
| 123 | self.gen.setstate(s) |
| 124 | for i in xrange(N): |
| 125 | self.gen.random() |
| 126 | r2 = self.gen.random() |
| 127 | self.assertEqual(r1, r2) |
| 128 | |
| 129 | def test_gauss_with_whseed(self): |
| 130 | # Ensure that the seed() method initializes all the hidden state. In |
| 131 | # particular, through 2.2.1 it failed to reset a piece of state used |
| 132 | # by (and only by) the .gauss() method. |
| 133 | |
| 134 | for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
| 135 | self.gen.whseed(seed) |
| 136 | x1 = self.gen.random() |
| 137 | y1 = self.gen.gauss(0, 1) |
| 138 | |
| 139 | self.gen.whseed(seed) |
| 140 | x2 = self.gen.random() |
| 141 | y2 = self.gen.gauss(0, 1) |
| 142 | |
| 143 | self.assertEqual(x1, x2) |
| 144 | self.assertEqual(y1, y2) |
| 145 | |
| 146 | class MersenneTwister_TestBasicOps(TestBasicOps): |
| 147 | gen = random.Random() |
| 148 | |
| 149 | def test_referenceImplementation(self): |
| 150 | # Compare the python implementation with results from the original |
| 151 | # code. Create 2000 53-bit precision random floats. Compare only |
| 152 | # the last ten entries to show that the independent implementations |
| 153 | # are tracking. Here is the main() function needed to create the |
| 154 | # list of expected random numbers: |
| 155 | # void main(void){ |
| 156 | # int i; |
| 157 | # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; |
| 158 | # init_by_array(init, length); |
| 159 | # for (i=0; i<2000; i++) { |
| 160 | # printf("%.15f ", genrand_res53()); |
| 161 | # if (i%5==4) printf("\n"); |
| 162 | # } |
| 163 | # } |
| 164 | expected = [0.45839803073713259, |
| 165 | 0.86057815201978782, |
| 166 | 0.92848331726782152, |
| 167 | 0.35932681119782461, |
| 168 | 0.081823493762449573, |
| 169 | 0.14332226470169329, |
| 170 | 0.084297823823520024, |
| 171 | 0.53814864671831453, |
| 172 | 0.089215024911993401, |
| 173 | 0.78486196105372907] |
| 174 | |
| 175 | self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) |
| 176 | actual = self.randomlist(2000)[-10:] |
| 177 | for a, e in zip(actual, expected): |
| 178 | self.assertAlmostEqual(a,e,places=14) |
| 179 | |
| 180 | def test_strong_reference_implementation(self): |
| 181 | # Like test_referenceImplementation, but checks for exact bit-level |
| 182 | # equality. This should pass on any box where C double contains |
| 183 | # at least 53 bits of precision (the underlying algorithm suffers |
| 184 | # no rounding errors -- all results are exact). |
| 185 | from math import ldexp |
| 186 | |
| 187 | expected = [0x0eab3258d2231fL, |
| 188 | 0x1b89db315277a5L, |
| 189 | 0x1db622a5518016L, |
| 190 | 0x0b7f9af0d575bfL, |
| 191 | 0x029e4c4db82240L, |
| 192 | 0x04961892f5d673L, |
| 193 | 0x02b291598e4589L, |
| 194 | 0x11388382c15694L, |
| 195 | 0x02dad977c9e1feL, |
| 196 | 0x191d96d4d334c6L] |
| 197 | |
| 198 | self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) |
| 199 | actual = self.randomlist(2000)[-10:] |
| 200 | for a, e in zip(actual, expected): |
| 201 | self.assertEqual(long(ldexp(a, 53)), e) |
| 202 | |
| 203 | def test_long_seed(self): |
| 204 | # This is most interesting to run in debug mode, just to make sure |
| 205 | # nothing blows up. Under the covers, a dynamically resized array |
| 206 | # is allocated, consuming space proportional to the number of bits |
| 207 | # in the seed. Unfortunately, that's a quadratic-time algorithm, |
| 208 | # so don't make this horribly big. |
| 209 | seed = (1L << (10000 * 8)) - 1 # about 10K bytes |
| 210 | self.gen.seed(seed) |
| 211 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 212 | _gammacoeff = (0.9999999999995183, 676.5203681218835, -1259.139216722289, |
| 213 | 771.3234287757674, -176.6150291498386, 12.50734324009056, |
| 214 | -0.1385710331296526, 0.9934937113930748e-05, 0.1659470187408462e-06) |
| 215 | |
| 216 | def gamma(z, cof=_gammacoeff, g=7): |
| 217 | z -= 1.0 |
| 218 | sum = cof[0] |
| 219 | for i in xrange(1,len(cof)): |
| 220 | sum += cof[i] / (z+i) |
| 221 | z += 0.5 |
| 222 | return (z+g)**z / exp(z+g) * sqrt(2*pi) * sum |
| 223 | |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 224 | class TestDistributions(unittest.TestCase): |
| 225 | def test_zeroinputs(self): |
| 226 | # Verify that distributions can handle a series of zero inputs' |
| 227 | g = random.Random() |
| 228 | x = [g.random() for i in xrange(50)] + [0.0]*5 |
| 229 | g.random = x[:].pop; g.uniform(1,10) |
| 230 | g.random = x[:].pop; g.paretovariate(1.0) |
| 231 | g.random = x[:].pop; g.expovariate(1.0) |
| 232 | g.random = x[:].pop; g.weibullvariate(1.0, 1.0) |
| 233 | g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 234 | g.random = x[:].pop; g.gauss(0.0, 1.0) |
| 235 | g.random = x[:].pop; g.lognormvariate(0.0, 1.0) |
| 236 | g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) |
| 237 | g.random = x[:].pop; g.gammavariate(0.01, 1.0) |
| 238 | g.random = x[:].pop; g.gammavariate(1.0, 1.0) |
| 239 | g.random = x[:].pop; g.gammavariate(200.0, 1.0) |
| 240 | g.random = x[:].pop; g.betavariate(3.0, 3.0) |
| 241 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 242 | def test_avg_std(self): |
| 243 | # Use integration to test distribution average and standard deviation. |
| 244 | # Only works for distributions which do not consume variates in pairs |
| 245 | g = random.Random() |
| 246 | N = 5000 |
| 247 | x = [i/float(N) for i in xrange(1,N)] |
| 248 | for variate, args, mu, sigmasqrd in [ |
| 249 | (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
| 250 | (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
| 251 | (g.paretovariate, (5.0,), 5.0/(5.0-1), |
| 252 | 5.0/((5.0-1)**2*(5.0-2))), |
| 253 | (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), |
| 254 | gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: |
| 255 | g.random = x[:].pop |
| 256 | y = [] |
| 257 | for i in xrange(len(x)): |
| 258 | try: |
| 259 | y.append(variate(*args)) |
| 260 | except IndexError: |
| 261 | pass |
| 262 | s1 = s2 = 0 |
| 263 | for e in y: |
| 264 | s1 += e |
| 265 | s2 += (e - mu) ** 2 |
| 266 | N = len(y) |
| 267 | self.assertAlmostEqual(s1/N, mu, 2) |
| 268 | self.assertAlmostEqual(s2/(N-1), sigmasqrd, 2) |
| 269 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 270 | class TestModule(unittest.TestCase): |
| 271 | def testMagicConstants(self): |
| 272 | self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) |
| 273 | self.assertAlmostEqual(random.TWOPI, 6.28318530718) |
| 274 | self.assertAlmostEqual(random.LOG4, 1.38629436111989) |
| 275 | self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) |
| 276 | |
| 277 | def test__all__(self): |
| 278 | # tests validity but not completeness of the __all__ list |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 279 | self.failUnless(Set(random.__all__) <= Set(dir(random))) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 280 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 281 | def test_main(verbose=None): |
Raymond Hettinger | 27922ee | 2003-05-03 03:38:01 +0000 | [diff] [blame] | 282 | testclasses = (WichmannHill_TestBasicOps, |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 283 | MersenneTwister_TestBasicOps, |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 284 | TestDistributions, |
Raymond Hettinger | 27922ee | 2003-05-03 03:38:01 +0000 | [diff] [blame] | 285 | TestModule) |
| 286 | test_support.run_unittest(*testclasses) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 287 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 288 | # verify reference counting |
| 289 | import sys |
| 290 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 291 | counts = [None] * 5 |
| 292 | for i in xrange(len(counts)): |
Raymond Hettinger | 27922ee | 2003-05-03 03:38:01 +0000 | [diff] [blame] | 293 | test_support.run_unittest(*testclasses) |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 294 | counts[i] = sys.gettotalrefcount() |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 295 | print counts |
| 296 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 297 | if __name__ == "__main__": |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 298 | test_main(verbose=True) |