summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-06-12 20:10:01 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-06-12 20:10:01 (GMT)
commit91621dbcbe2891c50e5ade04310eb164f9da17f4 (patch)
tree644381a0ea8c56d882bd97172cc473e72cc024cc /Modules/_testcapimodule.c
parent8bc84b4391583b1bac5eb05556f20ead8a428e88 (diff)
downloadcpython-91621dbcbe2891c50e5ade04310eb164f9da17f4.zip
cpython-91621dbcbe2891c50e5ade04310eb164f9da17f4.tar.gz
cpython-91621dbcbe2891c50e5ade04310eb164f9da17f4.tar.bz2
The merest start of a test for the PyLong_{As,From}{Unsigned,}LongLong()
functions. I intend to replace their guts with calls to the new _PyLong_{As,From}ByteArray() functions, but AFAICT there's no tests for them at all now; I also suspect PyLong_AsLongLong() isn't catching all overflow cases, but without a std test to demonstrate that why should you believe me <wink>. Also added a raiseTestError() utility function.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c61
1 files changed, 57 insertions, 4 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 24cf2b3..a49c60a 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -9,6 +9,22 @@
static PyObject *TestError; /* set to exception object in init */
+/* Raise TestError with test_name + ": " + msg, and return NULL. */
+
+static PyObject *
+raiseTestError(const char* test_name, const char* msg)
+{
+ char buf[2048];
+
+ if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
+ PyErr_SetString(TestError, "internal error msg too large");
+ else {
+ sprintf(buf, "%s: %s", test_name, msg);
+ PyErr_SetString(TestError, buf);
+ }
+ return NULL;
+}
+
/* Test #defines from config.h (particularly the SIZEOF_* defines).
The ones derived from autoconf on the UNIX-like OSes can be relied
@@ -145,7 +161,7 @@ test_dict_iteration(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, ":test_dict_iteration"))
return NULL;
-
+
for (i = 0; i < 200; i++) {
if (test_dict_inner(i) < 0) {
return NULL;
@@ -156,10 +172,47 @@ test_dict_iteration(PyObject* self, PyObject* args)
return Py_None;
}
+#ifdef HAVE_LONG_LONG
+
+/* Basic sanity checks for PyLong_{As, From}{Unsigned,}LongLong(). */
+
+static PyObject *
+test_longlong_api(PyObject* self, PyObject* args)
+{
+ /* unsigned LONG_LONG uinput, uoutput; */
+ LONG_LONG input, output;
+ PyObject *pyresult;
+
+ if (!PyArg_ParseTuple(args, ":test_longlong_api"))
+ return NULL;
+
+ input = 0;
+ pyresult = PyLong_FromLongLong(input);
+ if (pyresult == NULL)
+ return raiseTestError("test_longlong_api",
+ "unexpected null result");
+ output = PyLong_AsLongLong(pyresult);
+ if (output == (LONG_LONG)-1 && PyErr_Occurred())
+ return raiseTestError("test_longlong_api",
+ "unexpected -1 result");
+ if (output != input)
+ return raiseTestError("test_longlong_api",
+ "output != input");
+ Py_DECREF(pyresult);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+#endif
+
static PyMethodDef TestMethods[] = {
- {"test_config", test_config, METH_VARARGS},
- {"test_list_api", test_list_api, METH_VARARGS},
- {"test_dict_iteration", test_dict_iteration, METH_VARARGS},
+ {"test_config", test_config, METH_VARARGS},
+ {"test_list_api", test_list_api, METH_VARARGS},
+ {"test_dict_iteration", test_dict_iteration, METH_VARARGS},
+#ifdef HAVE_LONG_LONG
+ {"test_longlong_api", test_longlong_api, METH_VARARGS},
+#endif
{NULL, NULL} /* sentinel */
};