summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-01-28 20:37:45 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-01-28 20:37:45 (GMT)
commitbaefd9e552723c6489c69cf5df93f82b473550a2 (patch)
tree25300bbc824f4b60c7c7ac1a352d9246ab0e2169 /Modules
parent3d8c01b31c1a58d2181f78c5df2b0e79131046c0 (diff)
downloadcpython-baefd9e552723c6489c69cf5df93f82b473550a2.zip
cpython-baefd9e552723c6489c69cf5df93f82b473550a2.tar.gz
cpython-baefd9e552723c6489c69cf5df93f82b473550a2.tar.bz2
Added new private API function _PyLong_NumBits. This will be used at the
start for the C implemention of new pickle LONG1 and LONG4 opcodes (the linear-time way to pickle a long is to call _PyLong_AsByteArray, but the caller has no idea how big an array to allocate, and correct calculation is a bit subtle).
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c41
1 files changed, 39 insertions, 2 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 2054c80..9359188 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -36,7 +36,7 @@ sizeof_error(const char* fatname, const char* typename,
int expected, int got)
{
char buf[1024];
- PyOS_snprintf(buf, sizeof(buf),
+ PyOS_snprintf(buf, sizeof(buf),
"%.200s #define == %d but sizeof(%.200s) == %d",
fatname, expected, typename, got);
PyErr_SetString(TestError, buf);
@@ -326,7 +326,7 @@ test_u_code(PyObject *self)
len != PyUnicode_GET_SIZE(obj))
return raiseTestError("test_u_code",
"u# code returned wrong values for u'test'");
-
+
Py_DECREF(tuple);
Py_INCREF(Py_None);
return Py_None;
@@ -334,6 +334,42 @@ test_u_code(PyObject *self)
#endif
+/* Simple test of _PyLong_NumBits. */
+static PyObject *
+test_long_numbits(PyObject *self)
+{
+ struct pair {
+ long input;
+ size_t output;
+ } testcases[] = {{0, 1},
+ {1L, 2},
+ {-1L, 2},
+ {2L, 3},
+ {-2L, 3},
+ {3L, 3},
+ {-3L, 3},
+ {4L, 4},
+ {-4L, 4},
+ {0x7fffL, 16}, /* one Python long digit */
+ {-0x7fffL, 16},
+ {0xfffffffL, 29},
+ {-0xfffffffL, 29}};
+ int i;
+
+ for (i = 0; i < sizeof(testcases) / sizeof(struct pair); ++i) {
+ long input = testcases[i].input;
+ PyObject *plong = PyLong_FromLong(input);
+ size_t nbits = _PyLong_NumBits(plong);
+
+ Py_DECREF(plong);
+ if (nbits != testcases[i].output)
+ return raiseTestError("test_long_numbits",
+ "wrong result");
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static PyObject *
raise_exception(PyObject *self, PyObject *args)
{
@@ -366,6 +402,7 @@ static PyMethodDef TestMethods[] = {
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
{"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
+ {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
#ifdef HAVE_LONG_LONG
{"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS},
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},