Skip to content

Commit 1460879

Browse files
committed
Add test
1 parent 7436752 commit 1460879

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Lib/test/test_capi/test_long.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,38 @@ def test_long_getsign(self):
643643

644644
# CRASHES getsign(NULL)
645645

646+
def test_long_ispositive(self):
647+
ispositive = _testcapi.pylong_ispositive
648+
self.assertEqual(ispositive(1), 1)
649+
self.assertEqual(ispositive(123), 1)
650+
self.assertEqual(ispositive(-1), 0)
651+
self.assertEqual(ispositive(0), 0)
652+
self.assertEqual(ispositive(True), 1)
653+
self.assertEqual(ispositive(False), 0)
654+
self.assertEqual(ispositive(IntSubclass(-1)), 0)
655+
self.assertRaises(TypeError, ispositive, 1.0)
656+
self.assertRaises(TypeError, ispositive, Index(123))
657+
658+
def test_long_isnegative(self):
659+
isnegative = _testcapi.pylong_isnegative
660+
self.assertEqual(isnegative(1), 0)
661+
self.assertEqual(isnegative(123), 0)
662+
self.assertEqual(isnegative(-1), 1)
663+
self.assertEqual(isnegative(0), 0)
664+
self.assertEqual(isnegative(True), 0)
665+
self.assertEqual(isnegative(False), 0)
666+
self.assertEqual(isnegative(IntSubclass(-1)), 1)
667+
self.assertRaises(TypeError, isnegative, 1.0)
668+
self.assertRaises(TypeError, isnegative, Index(123))
669+
670+
def test_long_iszero(self):
671+
iszero = _testcapi.pylong_iszero
672+
self.assertEqual(iszero(1), 0)
673+
self.assertEqual(iszero(-1), 0)
674+
self.assertEqual(iszero(0), 1)
675+
self.assertRaises(TypeError, iszero, 1.0)
676+
self.assertRaises(TypeError, iszero, Index(123))
677+
646678
def test_long_asint32(self):
647679
# Test PyLong_AsInt32() and PyLong_FromInt32()
648680
to_int32 = _testlimitedcapi.pylong_asint32

Modules/_testcapi/long.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ pylong_getsign(PyObject *module, PyObject *arg)
105105
}
106106

107107

108+
static PyObject *
109+
pylong_ispositive(PyObject *module, PyObject *arg)
110+
{
111+
int s;
112+
if ((s = PyLong_IsPositive(arg)) == -1) {
113+
return NULL;
114+
}
115+
return PyLong_FromLong(s);
116+
}
117+
118+
119+
static PyObject *
120+
pylong_isnegative(PyObject *module, PyObject *arg)
121+
{
122+
int s;
123+
if ((s = PyLong_IsNegative(arg)) == -1) {
124+
return NULL;
125+
}
126+
return PyLong_FromLong(s);
127+
}
128+
129+
130+
static PyObject *
131+
pylong_iszero(PyObject *module, PyObject *arg)
132+
{
133+
int s;
134+
if ((s = PyLong_IsZero(arg)) == -1) {
135+
return NULL;
136+
}
137+
return PyLong_FromLong(s);
138+
}
139+
140+
108141
static PyObject *
109142
pylong_aspid(PyObject *module, PyObject *arg)
110143
{
@@ -124,6 +157,9 @@ static PyMethodDef test_methods[] = {
124157
{"pylong_fromnativebytes", pylong_fromnativebytes, METH_VARARGS},
125158
{"pylong_getsign", pylong_getsign, METH_O},
126159
{"pylong_aspid", pylong_aspid, METH_O},
160+
{"pylong_ispositive", pylong_ispositive, METH_O},
161+
{"pylong_isnegative", pylong_isnegative, METH_O},
162+
{"pylong_iszero", pylong_iszero, METH_O},
127163
{NULL},
128164
};
129165

0 commit comments

Comments
 (0)