[Python-checkins] python/dist/src/Lib/test test_random.py, 1.12.8.1, 1.12.8.2

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Sep 5 15:40:32 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv8317/Lib/test

Modified Files:
      Tag: release23-maint
	test_random.py 
Log Message:
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.



Index: test_random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_random.py,v
retrieving revision 1.12.8.1
retrieving revision 1.12.8.2
diff -C2 -d -r1.12.8.1 -r1.12.8.2
*** test_random.py	9 Aug 2003 18:20:16 -0000	1.12.8.1
--- test_random.py	5 Sep 2003 21:40:30 -0000	1.12.8.2
***************
*** 87,90 ****
--- 87,101 ----
                  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(unicode('abcdefghijklmnopqrst'), 2)
+         self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
+ 
      def test_gauss(self):
          # Ensure that the seed() method initializes all the hidden state.  In





More information about the Python-checkins mailing list