mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-40331: Increase test coverage for the statistics module (GH-19608)
This commit is contained in:
parent
42bae3a3d9
commit
b809717c1e
1 changed files with 60 additions and 0 deletions
|
|
@ -1004,6 +1004,10 @@ class ConvertTest(unittest.TestCase):
|
||||||
x = statistics._convert(nan, type(nan))
|
x = statistics._convert(nan, type(nan))
|
||||||
self.assertTrue(_nan_equal(x, nan))
|
self.assertTrue(_nan_equal(x, nan))
|
||||||
|
|
||||||
|
def test_invalid_input_type(self):
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
statistics._convert(None, float)
|
||||||
|
|
||||||
|
|
||||||
class FailNegTest(unittest.TestCase):
|
class FailNegTest(unittest.TestCase):
|
||||||
"""Test _fail_neg private function."""
|
"""Test _fail_neg private function."""
|
||||||
|
|
@ -1033,6 +1037,50 @@ class FailNegTest(unittest.TestCase):
|
||||||
self.assertEqual(errmsg, msg)
|
self.assertEqual(errmsg, msg)
|
||||||
|
|
||||||
|
|
||||||
|
class FindLteqTest(unittest.TestCase):
|
||||||
|
# Test _find_lteq private function.
|
||||||
|
|
||||||
|
def test_invalid_input_values(self):
|
||||||
|
for a, x in [
|
||||||
|
([], 1),
|
||||||
|
([1, 2], 3),
|
||||||
|
([1, 3], 2)
|
||||||
|
]:
|
||||||
|
with self.subTest(a=a, x=x):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
statistics._find_lteq(a, x)
|
||||||
|
|
||||||
|
def test_locate_successfully(self):
|
||||||
|
for a, x, expected_i in [
|
||||||
|
([1, 1, 1, 2, 3], 1, 0),
|
||||||
|
([0, 1, 1, 1, 2, 3], 1, 1),
|
||||||
|
([1, 2, 3, 3, 3], 3, 2)
|
||||||
|
]:
|
||||||
|
with self.subTest(a=a, x=x):
|
||||||
|
self.assertEqual(expected_i, statistics._find_lteq(a, x))
|
||||||
|
|
||||||
|
|
||||||
|
class FindRteqTest(unittest.TestCase):
|
||||||
|
# Test _find_rteq private function.
|
||||||
|
|
||||||
|
def test_invalid_input_values(self):
|
||||||
|
for a, l, x in [
|
||||||
|
([1], 2, 1),
|
||||||
|
([1, 3], 0, 2)
|
||||||
|
]:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
statistics._find_rteq(a, l, x)
|
||||||
|
|
||||||
|
def test_locate_successfully(self):
|
||||||
|
for a, l, x, expected_i in [
|
||||||
|
([1, 1, 1, 2, 3], 0, 1, 2),
|
||||||
|
([0, 1, 1, 1, 2, 3], 0, 1, 3),
|
||||||
|
([1, 2, 3, 3, 3], 0, 3, 4)
|
||||||
|
]:
|
||||||
|
with self.subTest(a=a, l=l, x=x):
|
||||||
|
self.assertEqual(expected_i, statistics._find_rteq(a, l, x))
|
||||||
|
|
||||||
|
|
||||||
# === Tests for public functions ===
|
# === Tests for public functions ===
|
||||||
|
|
||||||
class UnivariateCommonMixin:
|
class UnivariateCommonMixin:
|
||||||
|
|
@ -1476,6 +1524,18 @@ class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
|
||||||
with self.subTest(values=values):
|
with self.subTest(values=values):
|
||||||
self.assertRaises(exc, self.func, values)
|
self.assertRaises(exc, self.func, values)
|
||||||
|
|
||||||
|
def test_invalid_type_error(self):
|
||||||
|
# Test error is raised when input contains invalid type(s)
|
||||||
|
for data in [
|
||||||
|
['3.14'], # single string
|
||||||
|
['1', '2', '3'], # multiple strings
|
||||||
|
[1, '2', 3, '4', 5], # mixed strings and valid integers
|
||||||
|
[2.3, 3.4, 4.5, '5.6'] # only one string and valid floats
|
||||||
|
]:
|
||||||
|
with self.subTest(data=data):
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
self.func(data)
|
||||||
|
|
||||||
def test_ints(self):
|
def test_ints(self):
|
||||||
# Test harmonic mean with ints.
|
# Test harmonic mean with ints.
|
||||||
data = [2, 4, 4, 8, 16, 16]
|
data = [2, 4, 4, 8, 16, 16]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue