8000 Few coverage nitpicks for the math module · python/cpython@3e3807b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e3807b

Browse files
committed
Few coverage nitpicks for the math module
- input checks for math_1(L882), math_1a(L916), math_2(L957,L964), hypot(L2614), log(L2207), ldexp(L2061) and dist(L2519,L2520,L2567). - rewrite math_floor like math_ceil (cover L1132) - drop inaccessible "if" branch (L3546) in perm_comb_small() - improve fsum coverage for exceptional cases (L1302,L1321,L1339,L1390), ditto fmod(L2271) - rewrite modf to fix inaccessible case(L2122), ditto for pow(L3016) (all line numbers wrt the main branch)
1 parent 1e703a4 commit 3e3807b

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

Lib/test/test_math.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ def testAtan2(self):
324324
self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
325325
self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
326326
self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
327+
self.ftest('atan2(1, -1)', math.atan2(1, -1), 3*math.pi/4)
327328

328329
# math.atan2(0, x)
329330
self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
@@ -598,6 +599,7 @@ def testFmod(self):
598599
self.assertEqual(math.fmod(-3.0, NINF), -3.0)
599600
self.assertEqual(math.fmod(0.0, 3.0), 0.0)
600601
self.assertEqual(math.fmod(0.0, NINF), 0.0)
602+
self.assertRaises(ValueError, math.fmod, INF, INF)
601603

602604
def testFrexp(self):
603605
self.assertRaises(TypeError, math.frexp)
@@ -714,6 +716,11 @@ def msum(iterable):
714716
s = msum(vals)
715717
self.assertEqual(msum(vals), math.fsum(vals))
716718

719+
self.assertEqual(math.fsum([1.0, math.inf]), math.inf)
720+
self.assertRaises(OverflowError, math.fsum, [1e+308, 1e+308])
721+
self.assertRaises(ValueError, math.fsum, [math.inf, -math.inf])
722+
self.assertRaises(TypeError, math.fsum, ['spam'])
723+
717724
def testGcd(self):
718725
gcd = math.gcd
719726
self.assertEqual(gcd(0, 0), 0)
@@ -831,6 +838,8 @@ def testHypot(self):
831838
scale = FLOAT_MIN / 2.0 ** exp
832839
self.assertEqual(math.hypot(4*scale, 3*scale), 5*scale)
833840

841+
self.assertRaises(TypeError, math.hypot, *([1.0]*18), 'spam')
842+
834843
@requires_IEEE_754
835844
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
836845
"hypot() loses accuracy on machines with double rounding")
@@ -966,13 +975,19 @@ class T(tuple):
966975
dist((1, 2, 3, 4), (5, 6, 7))
967976
with self.assertRaises(ValueError): # Check dimension agree
968977
dist((1, 2, 3), (4, 5, 6, 7))
978+
with self.assertRaises(TypeError):
979+
dist((1,)*17 + ("spam",), (1,)*18)
969980
with self.assertRaises(TypeError): # Rejects invalid types
970981
dist("abc", "xyz")
971982
int_too_big_for_float = 10 ** (sys.float_info.max_10_exp + 5)
972983
with self.assertRaises((ValueError, OverflowError)):
973984
dist((1, int_too_big_for_float), (2, 3))
974985
with self.assertRaises((ValueError, OverflowError)):
975986
dist((2, 3), (1, int_too_big_for_float))
987+
with self.assertRaises(TypeError):
988+
dist((1,), 2)
989+
with self.assertRaises(TypeError):
990+
dist([1], 2)
976991

977992
# Verify that the one dimensional case is equivalent to abs()
978993
for i in range(20):
@@ -1111,6 +1126,7 @@ def test_lcm(self):
11111126

11121127
def testLdexp(self):
11131128
self.assertRaises(TypeError, math.ldexp)
1129+
self.assertRaises(TypeError, math.ldexp, 2.0, 1.1)
11141130
self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
11151131
self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
11161132
self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
@@ -1153,6 +1169,7 @@ def testLog(self):
11531169
2302.5850929940457)
11541170
self.assertRaises(ValueError, math.log, -1.5)
11551171
self.assertRaises(ValueError, math.log, -10**1000)
1172+
self.assertRaises(ValueError, math.log, 10, -10)
11561173
self.assertRaises(ValueError, math.log, NINF)
11571174
self.assertEqual(math.log(INF), INF)
11581175
self.assertTrue(math.isnan(math.log(NAN)))
@@ -2364,6 +2381,14 @@ def __float__(self):
23642381
# argument to a float.
23652382
self.assertFalse(getattr(y, "converted", False))
23662383

2384+
def test_input_exceptions(self):
2385+
self.assertRaises(TypeError, math.exp, "spam")
2386+
self.assertRaises(TypeError, math.erf, "spam")
2387+
self.assertRaises(TypeError, math.atan2, "spam", 1.0)
2388+
self.assertRaises(TypeError, math.atan2, 1.0, "spam")
2389+
self.assertRaises(TypeError, math.atan2, 1.0)
2390+
self.assertRaises(TypeError, math.atan2, 1.0, 2.0, 3.0)
2391+
23672392
# Custom assertions.
23682393

23692394
def assertIsNaN(self, value):

Modules/mathmodule.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,13 +1112,7 @@ static PyObject *
11121112
math_floor(PyObject *module, PyObject *number)
11131113
/*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/
11141114
{
1115-
double x;
1116-
1117-
if (PyFloat_CheckExact(number)) {
1118-
x = PyFloat_AS_DOUBLE(number);
1119-
}
1120-
else
1121-
{
1115+
if (!PyFloat_CheckExact(number)) {
11221116
math_module_state *state = get_math_module_state(module);
11231117
PyObject *method = _PyObject_LookupSpecial(number, state->str___floor__);
11241118
if (method != NULL) {
@@ -1128,10 +1122,11 @@ math_floor(PyObject *module, PyObject *number)
11281122
}
11291123
if (PyErr_Occurred())
11301124
return NULL;
1131-
x = PyFloat_AsDouble(number);
1132-
if (x == -1.0 && PyErr_Occurred())
1133-
return NULL;
11341125
}
1126+
double x = PyFloat_AsDouble(number);
1127+
if (x == -1.0 && PyErr_Occurred())
1128+
return NULL;
1129+
11351130
return PyLong_FromDouble(floor(x));
11361131
}
11371132

@@ -2116,12 +2111,10 @@ math_modf_impl(PyObject *module, double x)
21162111
double y;
21172112
/* some platforms don't do the right thing for NaNs and
21182113
infinities, so we take care of special cases directly. */
2119-
if (!Py_IS_FINITE(x)) {
2120-
if (Py_IS_INFINITY(x))
2121-
return Py_BuildValue("(dd)", copysign(0., x), x);
2122-
else if (Py_IS_NAN(x))
2123-
return Py_BuildValue("(dd)", x, x);
2124-
}
2114+
if (Py_IS_INFINITY(x))
2115+
return Py_BuildValue("(dd)", copysign(0., x), x);
2116+
else if (Py_IS_NAN(x))
2117+
return Py_BuildValue("(dd)", x, x);
21252118

21262119
errno = 0;
21272120
x = modf(x, &y);
@@ -3013,7 +3006,7 @@ math_pow_impl(PyObject *module, double x, double y)
30133006
else /* y < 0. */
30143007
r = odd_y ? copysign(0., x) : 0.;
30153008
}
3016-
else if (Py_IS_INFINITY(y)) {
3009+
else { /* Py_IS_INFINITY(y) */
30173010
if (fabs(x) == 1.0)
30183011
r = 1.;
30193012
else if (y > 0. && fabs(x) > 1.0)
@@ -3543,10 +3536,6 @@ static const uint8_t factorial_trailing_zeros[] = {
35433536
static PyObject *
35443537
perm_comb_small(unsigned long long n, unsigned long long k, int iscomb)
35453538
{
3546-
if (k == 0) {
3547-
return PyLong_FromLong(1);
3548-
}
3549-
35503539
/* For small enough n and k the result fits in the 64-bit range and can
35513540
* be calculated without allocating intermediate PyLong objects. */
35523541
if (iscomb) {

0 commit comments

Comments
 (0)
0