Skip to content

Commit b809717

Browse files
authored
bpo-40331: Increase test coverage for the statistics module (GH-19608)
1 parent 42bae3a commit b809717

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Lib/test/test_statistics.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,10 @@ def test_nan(self):
10041004
x = statistics._convert(nan, type(nan))
10051005
self.assertTrue(_nan_equal(x, nan))
10061006

1007+
def test_invalid_input_type(self):
1008+
with self.assertRaises(TypeError):
1009+
statistics._convert(None, float)
1010+
10071011

10081012
class FailNegTest(unittest.TestCase):
10091013
"""Test _fail_neg private function."""
@@ -1033,6 +1037,50 @@ def test_error_msg(self):
10331037
self.assertEqual(errmsg, msg)
10341038

10351039

1040+
class FindLteqTest(unittest.TestCase):
1041+
# Test _find_lteq private function.
1042+
1043+
def test_invalid_input_values(self):
1044+
for a, x in [
1045+
([], 1),
1046+
([1, 2], 3),
1047+
([1, 3], 2)
1048+
]:
1049+
with self.subTest(a=a, x=x):
1050+
with self.assertRaises(ValueError):
1051+
statistics._find_lteq(a, x)
1052+
1053+
def test_locate_successfully(self):
1054+
for a, x, expected_i in [
1055+
([1, 1, 1, 2, 3], 1, 0),
1056+
([0, 1, 1, 1, 2, 3], 1, 1),
1057+
([1, 2, 3, 3, 3], 3, 2)
1058+
]:
1059+
with self.subTest(a=a, x=x):
1060+
self.assertEqual(expected_i, statistics._find_lteq(a, x))
1061+
1062+
1063+
class FindRteqTest(unittest.TestCase):
1064+
# Test _find_rteq private function.
1065+
1066+
def test_invalid_input_values(self):
1067+
for a, l, x in [
1068+
([1], 2, 1),
1069+
([1, 3], 0, 2)
1070+
]:
1071+
with self.assertRaises(ValueError):
1072+
statistics._find_rteq(a, l, x)
1073+
1074+
def test_locate_successfully(self):
1075+
for a, l, x, expected_i in [
1076+
([1, 1, 1, 2, 3], 0, 1, 2),
1077+
([0, 1, 1, 1, 2, 3], 0, 1, 3),
1078+
([1, 2, 3, 3, 3], 0, 3, 4)
1079+
]:
1080+
with self.subTest(a=a, l=l, x=x):
1081+
self.assertEqual(expected_i, statistics._find_rteq(a, l, x))
1082+
1083+
10361084
# === Tests for public functions ===
10371085

10381086
class UnivariateCommonMixin:
@@ -1476,6 +1524,18 @@ def test_negative_error(self):
14761524
with self.subTest(values=values):
14771525
self.assertRaises(exc, self.func, values)
14781526

1527+
def test_invalid_type_error(self):
1528+
# Test error is raised when input contains invalid type(s)
1529+
for data in [
1530+
['3.14'], # single string
1531+
['1', '2', '3'], # multiple strings
1532+
[1, '2', 3, '4', 5], # mixed strings and valid integers
1533+
[2.3, 3.4, 4.5, '5.6'] # only one string and valid floats
1534+
]:
1535+
with self.subTest(data=data):
1536+
with self.assertRaises(TypeError):
1537+
self.func(data)
1538+
14791539
def test_ints(self):
14801540
# Test harmonic mean with ints.
14811541
data = [2, 4, 4, 8, 16, 16]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy