summaryrefslogtreecommitdiff
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-09-06 04:25:54 +0000
committerRaymond Hettinger <python@rcn.com>2003-09-06 04:25:54 +0000
commit7bdbe3db9f65afabf1a7536eb286fa4afa11b02b (patch)
tree88cf2930be8666d70ddba8e0b438b8a8f1c380e9 /Lib/test/test_random.py
parent7c234089ccb096aa53b27b0875d211aeb27a8d2d (diff)
downloadcpython-7bdbe3db9f65afabf1a7536eb286fa4afa11b02b.tar.gz
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
random.sample() uses one of two algorithms depending on the ratio of the sample size to the population size. One of the algorithms accepted any iterable population argument so long as it defined __len__(). The other had a stronger requirement that the population argument be indexable. While it met the documentation specifications which insisted that the population argument be a sequence, it made random.sample() less usable with sets. So, the second algorithm was modified to coerce non-indexable iterables and dictionaries into a tuple before proceeding.
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index c9103e8f87..fbd418457a 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -86,6 +86,16 @@ class TestBasicOps(unittest.TestCase):
else:
self.fail()
+ def test_sample_inputs(self):
+ # SF bug #801342 -- population can be any iterable defining __len__()
+ from sets import Set
+ self.gen.sample(Set(range(20)), 2)
+ self.gen.sample(range(20), 2)
+ self.gen.sample(xrange(20), 2)
+ self.gen.sample(dict.fromkeys('abcdefghijklmnopqrst'), 2)
+ self.gen.sample(str('abcdefghijklmnopqrst'), 2)
+ self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
+
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In
# particular, through 2.2.1 it failed to reset a piece of state used