Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_random.SystemRandom_TestBasicOps Class Reference
Inheritance diagram for test.test_random.SystemRandom_TestBasicOps:
Inheritance graph
[legend]
Collaboration diagram for test.test_random.SystemRandom_TestBasicOps:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_autoseed
def test_saverestore
def test_seedargs
def test_gauss
def test_pickling
def test_53_bits_per_float
def test_bigrand
def test_bigrand_ranges
def test_rangelimits
def test_genrandbits
def test_randbelow_logic
def randomlist
def test_choice
def test_sample
def test_sample_distribution
def test_sample_inputs
def test_sample_on_dicts
def test_bug_1727780
def test_bug_9025

Static Public Attributes

tuple gen = random.SystemRandom()

Detailed Description

Definition at line 141 of file test_random.py.


Member Function Documentation

def test.test_random.TestBasicOps.randomlist (   self,
  n 
) [inherited]
Helper function to make a list of random numbers

Definition at line 16 of file test_random.py.

00016 
00017     def randomlist(self, n):
00018         """Helper function to make a list of random numbers"""
00019         return [self.gen.random() for i in range(n)]

Here is the caller graph for this function:

Definition at line 164 of file test_random.py.

00164 
00165     def test_53_bits_per_float(self):
00166         # This should pass whenever a C double has 53 bit precision.
00167         span = 2 ** 53
00168         cum = 0
00169         for i in range(100):
00170             cum |= int(self.gen.random() * span)
00171         self.assertEqual(cum, span-1)

Here is the call graph for this function:

Reimplemented from test.test_random.TestBasicOps.

Definition at line 144 of file test_random.py.

00144 
00145     def test_autoseed(self):
00146         # Doesn't need to do anything except not fail
00147         self.gen.seed()

Definition at line 172 of file test_random.py.

00172 
00173     def test_bigrand(self):
00174         # The randrange routine should build-up the required number of bits
00175         # in stages so that all bit positions are active.
00176         span = 2 ** 500
00177         cum = 0
00178         for i in range(100):
00179             r = self.gen.randrange(span)
00180             self.assertTrue(0 <= r < span)
00181             cum |= r
00182         self.assertEqual(cum, span-1)

Here is the call graph for this function:

Definition at line 183 of file test_random.py.

00183 
00184     def test_bigrand_ranges(self):
00185         for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
00186             start = self.gen.randrange(2 ** i)
00187             stop = self.gen.randrange(2 ** (i-2))
00188             if stop <= start:
00189                 return
00190             self.assertTrue(start <= self.gen.randrange(start, stop) < stop)

Here is the call graph for this function:

Definition at line 120 of file test_random.py.

00120 
00121     def test_bug_1727780(self):
00122         # verify that version-2-pickles can be loaded
00123         # fine, whether they are created on 32-bit or 64-bit
00124         # platforms, and that version-3-pickles load fine.
00125         files = [("randv2_32.pck", 780),
00126                  ("randv2_64.pck", 866),
00127                  ("randv3.pck", 343)]
00128         for file, value in files:
00129             f = open(support.findfile(file),"rb")
00130             r = pickle.load(f)
00131             f.close()
00132             self.assertEqual(int(r.random()*1000), value)

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_bug_9025 (   self) [inherited]

Definition at line 133 of file test_random.py.

00133 
00134     def test_bug_9025(self):
00135         # Had problem with an uneven distribution in int(n*random())
00136         # Verify the fix by checking that distributions fall within expectations.
00137         n = 100000
00138         randrange = self.gen.randrange
00139         k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
00140         self.assertTrue(0.30 < k/n < .37, (k/n))

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_choice (   self) [inherited]

Definition at line 45 of file test_random.py.

00045 
00046     def test_choice(self):
00047         choice = self.gen.choice
00048         with self.assertRaises(IndexError):
00049             choice([])
00050         self.assertEqual(choice([50]), 50)
00051         self.assertIn(choice([25, 75]), [25, 75])

Here is the call graph for this function:

Reimplemented from test.test_random.TestBasicOps.

Definition at line 156 of file test_random.py.

00156 
00157     def test_gauss(self):
00158         self.gen.gauss_next = None
00159         self.gen.seed(100)
00160         self.assertEqual(self.gen.gauss_next, None)

Here is the call graph for this function:

Definition at line 196 of file test_random.py.

00196 
00197     def test_genrandbits(self):
00198         # Verify ranges
00199         for k in range(1, 1000):
00200             self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
00201 
00202         # Verify all bits active
00203         getbits = self.gen.getrandbits
00204         for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
00205             cum = 0
00206             for i in range(100):
00207                 cum |= getbits(span)
00208             self.assertEqual(cum, 2**span-1)
00209 
00210         # Verify argument checking
00211         self.assertRaises(TypeError, self.gen.getrandbits)
00212         self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
00213         self.assertRaises(ValueError, self.gen.getrandbits, 0)
00214         self.assertRaises(ValueError, self.gen.getrandbits, -1)
00215         self.assertRaises(TypeError, self.gen.getrandbits, 10.1)

Here is the call graph for this function:

Reimplemented from test.test_random.TestBasicOps.

Definition at line 161 of file test_random.py.

00161 
00162     def test_pickling(self):
00163         self.assertRaises(NotImplementedError, pickle.dumps, self.gen)

Here is the call graph for this function:

def test.test_random.SystemRandom_TestBasicOps.test_randbelow_logic (   self,
  _log = log,
  int = int 
)

Definition at line 216 of file test_random.py.

00216 
00217     def test_randbelow_logic(self, _log=log, int=int):
00218         # check bitcount transition points:  2**i and 2**(i+1)-1
00219         # show that: k = int(1.001 + _log(n, 2))
00220         # is equal to or one greater than the number of bits in n
00221         for i in range(1, 1000):
00222             n = 1 << i # check an exact power of two
00223             numbits = i+1
00224             k = int(1.00001 + _log(n, 2))
00225             self.assertEqual(k, numbits)
00226             self.assertEqual(n, 2**(k-1))
00227 
00228             n += n - 1      # check 1 below the next power of two
00229             k = int(1.00001 + _log(n, 2))
00230             self.assertIn(k, [numbits, numbits+1])
00231             self.assertTrue(2**k > n > 2**(k-2))
00232 
00233             n -= n >> 15     # check a little farther below the next power of two
00234             k = int(1.00001 + _log(n, 2))
00235             self.assertEqual(k, numbits)        # note the stronger assertion
00236             self.assertTrue(2**k > n > 2**(k-1))   # note the stronger assertion
00237 

Here is the call graph for this function:

Definition at line 191 of file test_random.py.

00191 
00192     def test_rangelimits(self):
00193         for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
00194             self.assertEqual(set(range(start,stop)),
00195                 set([self.gen.randrange(start,stop) for i in range(100)]))

Here is the call graph for this function:

def test.test_random.TestBasicOps.test_sample (   self) [inherited]

Definition at line 52 of file test_random.py.

00052 
00053     def test_sample(self):
00054         # For the entire allowable range of 0 <= k <= N, validate that
00055         # the sample is of the correct length and contains only unique items
00056         N = 100
00057         population = range(N)
00058         for k in range(N+1):
00059             s = self.gen.sample(population, k)
00060             self.assertEqual(len(s), k)
00061             uniq = set(s)
00062             self.assertEqual(len(uniq), k)
00063             self.assertTrue(uniq <= set(population))
00064         self.assertEqual(self.gen.sample([], 0), [])  # test edge case N==k==0

Here is the call graph for this function:

Definition at line 65 of file test_random.py.

00065 
00066     def test_sample_distribution(self):
00067         # For the entire allowable range of 0 <= k <= N, validate that
00068         # sample generates all possible permutations
00069         n = 5
00070         pop = range(n)
00071         trials = 10000  # large num prevents false negatives without slowing normal case
00072         def factorial(n):
00073             if n == 0:
00074                 return 1
00075             return n * factorial(n - 1)
00076         for k in range(n):
00077             expected = factorial(n) // factorial(n-k)
00078             perms = {}
00079             for i in range(trials):
00080                 perms[tuple(self.gen.sample(pop, k))] = None
00081                 if len(perms) == expected:
00082                     break
00083             else:
00084                 self.fail()

Here is the call graph for this function:

Definition at line 85 of file test_random.py.

00085 
00086     def test_sample_inputs(self):
00087         # SF bug #801342 -- population can be any iterable defining __len__()
00088         self.gen.sample(set(range(20)), 2)
00089         self.gen.sample(range(20), 2)
00090         self.gen.sample(range(20), 2)
00091         self.gen.sample(str('abcdefghijklmnopqrst'), 2)
00092         self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)

Here is the call graph for this function:

Definition at line 93 of file test_random.py.

00093 
00094     def test_sample_on_dicts(self):
00095         self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)

Here is the call graph for this function:

Reimplemented from test.test_random.TestBasicOps.

Definition at line 148 of file test_random.py.

00148 
00149     def test_saverestore(self):
00150         self.assertRaises(NotImplementedError, self.gen.getstate)
00151         self.assertRaises(NotImplementedError, self.gen.setstate, None)

Here is the call graph for this function:

Reimplemented from test.test_random.TestBasicOps.

Definition at line 152 of file test_random.py.

00152 
00153     def test_seedargs(self):
00154         # Doesn't need to do anything except not fail
00155         self.gen.seed(100)


Member Data Documentation

Definition at line 142 of file test_random.py.


The documentation for this class was generated from the following file: