@@ -1004,6 +1004,10 @@ def test_nan(self):
1004
1004
x = statistics ._convert (nan , type (nan ))
1005
1005
self .assertTrue (_nan_equal (x , nan ))
1006
1006
1007
+ def test_invalid_input_type (self ):
1008
+ with self .assertRaises (TypeError ):
1009
+ statistics ._convert (None , float )
1010
+
1007
1011
1008
1012
class FailNegTest (unittest .TestCase ):
1009
1013
"""Test _fail_neg private function."""
@@ -1033,6 +1037,50 @@ def test_error_msg(self):
1033
1037
self .assertEqual (errmsg , msg )
1034
1038
1035
1039
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
+
1036
1084
# === Tests for public functions ===
1037
1085
1038
1086
class UnivariateCommonMixin :
@@ -1476,6 +1524,18 @@ def test_negative_error(self):
1476
1524
with self .subTest (values = values ):
1477
1525
self .assertRaises (exc , self .func , values )
1478
1526
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
+
1479
1539
def test_ints (self ):
1480
1540
# Test harmonic mean with ints.
1481
1541
data = [2 , 4 , 4 , 8 , 16 , 16 ]
0 commit comments