summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_statistics.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-03-12 07:43:27 (GMT)
committerGitHub <noreply@github.com>2019-03-12 07:43:27 (GMT)
commitfc06a192fdc44225ef1cc879f615a81931ad0a85 (patch)
tree3a6eb28aac5ebf320cb665fa53a9a08cf75fbcc0 /Lib/test/test_statistics.py
parent3e936431e23b424b1e4665e8165c245924f0ab02 (diff)
downloadcpython-fc06a192fdc44225ef1cc879f615a81931ad0a85.zip
cpython-fc06a192fdc44225ef1cc879f615a81931ad0a85.tar.gz
cpython-fc06a192fdc44225ef1cc879f615a81931ad0a85.tar.bz2
bpo-35892: Fix mode() and add multimode() (#12089)
Diffstat (limited to 'Lib/test/test_statistics.py')
-rw-r--r--Lib/test/test_statistics.py27
1 files changed, 19 insertions, 8 deletions
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index a63e4bf..26b22a1 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -1769,7 +1769,7 @@ class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
def test_range_data(self):
# Override test from UnivariateCommonMixin.
data = range(20, 50, 3)
- self.assertRaises(statistics.StatisticsError, self.func, data)
+ self.assertEqual(self.func(data), 20)
def test_nominal_data(self):
# Test mode with nominal data.
@@ -1790,13 +1790,14 @@ class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
# Test mode with bimodal data.
data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9]
assert data.count(2) == data.count(6) == 4
- # Check for an exception.
- self.assertRaises(statistics.StatisticsError, self.func, data)
+ # mode() should return 2, the first encounted mode
+ self.assertEqual(self.func(data), 2)
- def test_unique_data_failure(self):
- # Test mode exception when data points are all unique.
+ def test_unique_data(self):
+ # Test mode when data points are all unique.
data = list(range(10))
- self.assertRaises(statistics.StatisticsError, self.func, data)
+ # mode() should return 0, the first encounted mode
+ self.assertEqual(self.func(data), 0)
def test_none_data(self):
# Test that mode raises TypeError if given None as data.
@@ -1809,8 +1810,18 @@ class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
# Test that a Counter is treated like any other iterable.
data = collections.Counter([1, 1, 1, 2])
# Since the keys of the counter are treated as data points, not the
- # counts, this should raise.
- self.assertRaises(statistics.StatisticsError, self.func, data)
+ # counts, this should return the first mode encountered, 1
+ self.assertEqual(self.func(data), 1)
+
+
+class TestMultiMode(unittest.TestCase):
+
+ def test_basics(self):
+ multimode = statistics.multimode
+ self.assertEqual(multimode('aabbbbbbbbcc'), ['b'])
+ self.assertEqual(multimode('aabbbbccddddeeffffgg'), ['b', 'd', 'f'])
+ self.assertEqual(multimode(''), [])
+
class TestFMean(unittest.TestCase):