summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-09-07 07:08:44 (GMT)
committerRaymond Hettinger <python@rcn.com>2016-09-07 07:08:44 (GMT)
commit28aa4a06842bb4a27b6c41b61bc52da74cad669e (patch)
tree4c3e5bae19b3f200e3c825012efd27dc5dd1e954 /Lib/test
parentc98b26a6ac0e5e68dd2eb820430cc2a44b4df019 (diff)
downloadcpython-28aa4a06842bb4a27b6c41b61bc52da74cad669e.zip
cpython-28aa4a06842bb4a27b6c41b61bc52da74cad669e.tar.gz
cpython-28aa4a06842bb4a27b6c41b61bc52da74cad669e.tar.bz2
Rename weighted_choices() to just choices()
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_random.py54
1 files changed, 27 insertions, 27 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index b3741a8..9c1383d 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -142,8 +142,8 @@ class TestBasicOps:
def test_sample_on_dicts(self):
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
- def test_weighted_choices(self):
- weighted_choices = self.gen.weighted_choices
+ def test_choices(self):
+ choices = self.gen.choices
data = ['red', 'green', 'blue', 'yellow']
str_data = 'abcd'
range_data = range(4)
@@ -151,63 +151,63 @@ class TestBasicOps:
# basic functionality
for sample in [
- weighted_choices(5, data),
- weighted_choices(5, data, range(4)),
- weighted_choices(k=5, population=data, weights=range(4)),
- weighted_choices(k=5, population=data, cum_weights=range(4)),
+ choices(5, data),
+ choices(5, data, range(4)),
+ choices(k=5, population=data, weights=range(4)),
+ choices(k=5, population=data, cum_weights=range(4)),
]:
self.assertEqual(len(sample), 5)
self.assertEqual(type(sample), list)
self.assertTrue(set(sample) <= set(data))
# test argument handling
- with self.assertRaises(TypeError): # missing arguments
- weighted_choices(2)
+ with self.assertRaises(TypeError): # missing arguments
+ choices(2)
- self.assertEqual(weighted_choices(0, data), []) # k == 0
- self.assertEqual(weighted_choices(-1, data), []) # negative k behaves like ``[0] * -1``
+ self.assertEqual(choices(0, data), []) # k == 0
+ self.assertEqual(choices(-1, data), []) # negative k behaves like ``[0] * -1``
with self.assertRaises(TypeError):
- weighted_choices(2.5, data) # k is a float
+ choices(2.5, data) # k is a float
- self.assertTrue(set(weighted_choices(5, str_data)) <= set(str_data)) # population is a string sequence
- self.assertTrue(set(weighted_choices(5, range_data)) <= set(range_data)) # population is a range
+ self.assertTrue(set(choices(5, str_data)) <= set(str_data)) # population is a string sequence
+ self.assertTrue(set(choices(5, range_data)) <= set(range_data)) # population is a range
with self.assertRaises(TypeError):
- weighted_choices(2.5, set_data) # population is not a sequence
+ choices(2.5, set_data) # population is not a sequence
- self.assertTrue(set(weighted_choices(5, data, None)) <= set(data)) # weights is None
- self.assertTrue(set(weighted_choices(5, data, weights=None)) <= set(data))
+ self.assertTrue(set(choices(5, data, None)) <= set(data)) # weights is None
+ self.assertTrue(set(choices(5, data, weights=None)) <= set(data))
with self.assertRaises(ValueError):
- weighted_choices(5, data, [1,2]) # len(weights) != len(population)
+ choices(5, data, [1,2]) # len(weights) != len(population)
with self.assertRaises(IndexError):
- weighted_choices(5, data, [0]*4) # weights sum to zero
+ choices(5, data, [0]*4) # weights sum to zero
with self.assertRaises(TypeError):
- weighted_choices(5, data, 10) # non-iterable weights
+ choices(5, data, 10) # non-iterable weights
with self.assertRaises(TypeError):
- weighted_choices(5, data, [None]*4) # non-numeric weights
+ choices(5, data, [None]*4) # non-numeric weights
for weights in [
[15, 10, 25, 30], # integer weights
[15.1, 10.2, 25.2, 30.3], # float weights
[Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights
[True, False, True, False] # booleans (include / exclude)
]:
- self.assertTrue(set(weighted_choices(5, data, weights)) <= set(data))
+ self.assertTrue(set(choices(5, data, weights)) <= set(data))
with self.assertRaises(ValueError):
- weighted_choices(5, data, cum_weights=[1,2]) # len(weights) != len(population)
+ choices(5, data, cum_weights=[1,2]) # len(weights) != len(population)
with self.assertRaises(IndexError):
- weighted_choices(5, data, cum_weights=[0]*4) # cum_weights sum to zero
+ choices(5, data, cum_weights=[0]*4) # cum_weights sum to zero
with self.assertRaises(TypeError):
- weighted_choices(5, data, cum_weights=10) # non-iterable cum_weights
+ choices(5, data, cum_weights=10) # non-iterable cum_weights
with self.assertRaises(TypeError):
- weighted_choices(5, data, cum_weights=[None]*4) # non-numeric cum_weights
+ choices(5, data, cum_weights=[None]*4) # non-numeric cum_weights
with self.assertRaises(TypeError):
- weighted_choices(5, data, range(4), cum_weights=range(4)) # both weights and cum_weights
+ choices(5, data, range(4), cum_weights=range(4)) # both weights and cum_weights
for weights in [
[15, 10, 25, 30], # integer cum_weights
[15.1, 10.2, 25.2, 30.3], # float cum_weights
[Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights
]:
- self.assertTrue(set(weighted_choices(5, data, cum_weights=weights)) <= set(data))
+ self.assertTrue(set(choices(5, data, cum_weights=weights)) <= set(data))
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In