diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-01-31 15:52:05 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-01-31 15:52:05 (GMT) |
commit | 5b8132ffa34244cc24b603d8462e6e733e014e71 (patch) | |
tree | 9ed5a901229b5dfef77b2f88ddf961f57a211f1a /Modules/_testcapimodule.c | |
parent | 89fc4f3e5623d24632b1daf503a2f32ac2bd0d66 (diff) | |
download | cpython-5b8132ffa34244cc24b603d8462e6e733e014e71.zip cpython-5b8132ffa34244cc24b603d8462e6e733e014e71.tar.gz cpython-5b8132ffa34244cc24b603d8462e6e733e014e71.tar.bz2 |
_PyLong_NumBits(): The definition of this was too specific to the quirky
needs of pickling longs. Backed off to a definition that's much easier
to understand. The pickler will have to work a little harder, but other
uses are more likely to be correct <0.5 wink>.
_PyLong_Sign(): New teensy function to characterize a long, as to <0, ==0,
or >0.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 9359188..4c3c2aa 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -334,37 +334,43 @@ test_u_code(PyObject *self) #endif -/* Simple test of _PyLong_NumBits. */ +/* Simple test of _PyLong_NumBits and _PyLong_Sign. */ static PyObject * test_long_numbits(PyObject *self) { - struct pair { + struct triple { 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}}; + size_t nbits; + int sign; + } testcases[] = {{0, 0, 0}, + {1L, 1, 1}, + {-1L, 1, -1}, + {2L, 2, 1}, + {-2L, 2, -1}, + {3L, 2, 1}, + {-3L, 2, -1}, + {4L, 3, 1}, + {-4L, 3, -1}, + {0x7fffL, 15, 1}, /* one Python long digit */ + {-0x7fffL, 15, -1}, + {0xffffL, 16, 1}, + {-0xffffL, 16, -1}, + {0xfffffffL, 28, 1}, + {-0xfffffffL, 28, -1}}; int i; - for (i = 0; i < sizeof(testcases) / sizeof(struct pair); ++i) { - long input = testcases[i].input; - PyObject *plong = PyLong_FromLong(input); + for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { + PyObject *plong = PyLong_FromLong(testcases[i].input); size_t nbits = _PyLong_NumBits(plong); + int sign = _PyLong_Sign(plong); Py_DECREF(plong); - if (nbits != testcases[i].output) + if (nbits != testcases[i].nbits) return raiseTestError("test_long_numbits", - "wrong result"); + "wrong result for _PyLong_NumBits"); + if (sign != testcases[i].sign) + return raiseTestError("test_long_numbits", + "wrong result for _PyLong_Sign"); } Py_INCREF(Py_None); return Py_None; |