mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
This commit is contained in:
parent
7b1bee47ae
commit
70099a1555
2 changed files with 28 additions and 26 deletions
|
@ -381,29 +381,33 @@ class ComplexTest(unittest.TestCase):
|
||||||
for num in nums:
|
for num in nums:
|
||||||
self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
|
self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr_str(self):
|
||||||
self.assertEqual(repr(1+6j), '(1+6j)')
|
def test(v, expected, test_fn=self.assertEqual):
|
||||||
self.assertEqual(repr(1-6j), '(1-6j)')
|
test_fn(repr(v), expected)
|
||||||
|
test_fn(str(v), expected)
|
||||||
|
|
||||||
self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
|
test(1+6j, '(1+6j)')
|
||||||
|
test(1-6j, '(1-6j)')
|
||||||
|
|
||||||
|
test(-(1+0j), '(-1+-0j)', test_fn=self.assertNotEqual)
|
||||||
|
|
||||||
|
test(complex(1., INF), "(1+infj)")
|
||||||
|
test(complex(1., -INF), "(1-infj)")
|
||||||
|
test(complex(INF, 1), "(inf+1j)")
|
||||||
|
test(complex(-INF, INF), "(-inf+infj)")
|
||||||
|
test(complex(NAN, 1), "(nan+1j)")
|
||||||
|
test(complex(1, NAN), "(1+nanj)")
|
||||||
|
test(complex(NAN, NAN), "(nan+nanj)")
|
||||||
|
|
||||||
|
test(complex(0, INF), "infj")
|
||||||
|
test(complex(0, -INF), "-infj")
|
||||||
|
test(complex(0, NAN), "nanj")
|
||||||
|
|
||||||
self.assertEqual(1-6j,complex(repr(1-6j)))
|
self.assertEqual(1-6j,complex(repr(1-6j)))
|
||||||
self.assertEqual(1+6j,complex(repr(1+6j)))
|
self.assertEqual(1+6j,complex(repr(1+6j)))
|
||||||
self.assertEqual(-6j,complex(repr(-6j)))
|
self.assertEqual(-6j,complex(repr(-6j)))
|
||||||
self.assertEqual(6j,complex(repr(6j)))
|
self.assertEqual(6j,complex(repr(6j)))
|
||||||
|
|
||||||
self.assertEqual(repr(complex(1., INF)), "(1+infj)")
|
|
||||||
self.assertEqual(repr(complex(1., -INF)), "(1-infj)")
|
|
||||||
self.assertEqual(repr(complex(INF, 1)), "(inf+1j)")
|
|
||||||
self.assertEqual(repr(complex(-INF, INF)), "(-inf+infj)")
|
|
||||||
self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)")
|
|
||||||
self.assertEqual(repr(complex(1, NAN)), "(1+nanj)")
|
|
||||||
self.assertEqual(repr(complex(NAN, NAN)), "(nan+nanj)")
|
|
||||||
|
|
||||||
self.assertEqual(repr(complex(0, INF)), "infj")
|
|
||||||
self.assertEqual(repr(complex(0, -INF)), "-infj")
|
|
||||||
self.assertEqual(repr(complex(0, NAN)), "nanj")
|
|
||||||
|
|
||||||
def test_neg(self):
|
def test_neg(self):
|
||||||
self.assertEqual(-(1+6j), -1-6j)
|
self.assertEqual(-(1+6j), -1-6j)
|
||||||
|
|
||||||
|
|
|
@ -324,10 +324,11 @@ complex_dealloc(PyObject *op)
|
||||||
op->ob_type->tp_free(op);
|
op->ob_type->tp_free(op);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
complex_format(PyComplexObject *v, int precision, char format_code)
|
complex_repr(PyComplexObject *v)
|
||||||
{
|
{
|
||||||
|
int precision = 0;
|
||||||
|
char format_code = 'r';
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
|
|
||||||
|
@ -344,6 +345,8 @@ complex_format(PyComplexObject *v, int precision, char format_code)
|
||||||
char *tail = "";
|
char *tail = "";
|
||||||
|
|
||||||
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
|
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
|
||||||
|
/* Real part is +0: just output the imaginary part and do not
|
||||||
|
include parens. */
|
||||||
re = "";
|
re = "";
|
||||||
im = PyOS_double_to_string(v->cval.imag, format_code,
|
im = PyOS_double_to_string(v->cval.imag, format_code,
|
||||||
precision, 0, NULL);
|
precision, 0, NULL);
|
||||||
|
@ -352,7 +355,8 @@ complex_format(PyComplexObject *v, int precision, char format_code)
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Format imaginary part with sign, real part without */
|
/* Format imaginary part with sign, real part without. Include
|
||||||
|
parens in the result. */
|
||||||
pre = PyOS_double_to_string(v->cval.real, format_code,
|
pre = PyOS_double_to_string(v->cval.real, format_code,
|
||||||
precision, 0, NULL);
|
precision, 0, NULL);
|
||||||
if (!pre) {
|
if (!pre) {
|
||||||
|
@ -371,7 +375,7 @@ complex_format(PyComplexObject *v, int precision, char format_code)
|
||||||
tail = ")";
|
tail = ")";
|
||||||
}
|
}
|
||||||
/* Alloc the final buffer. Add one for the "j" in the format string,
|
/* Alloc the final buffer. Add one for the "j" in the format string,
|
||||||
and one for the trailing zero. */
|
and one for the trailing zero byte. */
|
||||||
len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
|
len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
|
||||||
buf = PyMem_Malloc(len);
|
buf = PyMem_Malloc(len);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
|
@ -388,12 +392,6 @@ complex_format(PyComplexObject *v, int precision, char format_code)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
complex_repr(PyComplexObject *v)
|
|
||||||
{
|
|
||||||
return complex_format(v, 0, 'r');
|
|
||||||
}
|
|
||||||
|
|
||||||
static Py_hash_t
|
static Py_hash_t
|
||||||
complex_hash(PyComplexObject *v)
|
complex_hash(PyComplexObject *v)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue