Unit tests for ptrdiff_t/size_t max/min in Python
This commit is contained in:
parent
12b62a562d
commit
fc8e76544c
2 changed files with 42 additions and 0 deletions
|
|
@ -368,6 +368,17 @@ macro(size_t, pfx, sizet)
|
|||
virtual const char* pfx##_##str(type x) { return "name"; }
|
||||
%enddef
|
||||
|
||||
/* checking size_t and ptrdiff_t typemaps */
|
||||
%include "stdint.i"
|
||||
%inline {
|
||||
size_t get_size_min() { return 0; }
|
||||
size_t get_size_max() { return SIZE_MAX; }
|
||||
ptrdiff_t get_ptrdiff_min() { return PTRDIFF_MIN; }
|
||||
ptrdiff_t get_ptrdiff_max() { return PTRDIFF_MAX; }
|
||||
|
||||
size_t size_echo (size_t val) { return val; }
|
||||
ptrdiff_t ptrdiff_echo(ptrdiff_t val) { return val; }
|
||||
}
|
||||
|
||||
%inline {
|
||||
struct Foo
|
||||
|
|
|
|||
|
|
@ -573,3 +573,34 @@ if val_double(sys.maxint + 1) != float(sys.maxint + 1):
|
|||
raise RuntimeError, "bad double typemap"
|
||||
if val_double(-sys.maxint - 2) != float(-sys.maxint - 2):
|
||||
raise RuntimeError, "bad double typemap"
|
||||
|
||||
|
||||
# Check the minimum and maximum values that fit in ptrdiff_t and size_t
|
||||
def checkType(name, maxfunc, maxval, minfunc, minval, echofunc):
|
||||
if maxfunc() != maxval:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
if minfunc() != minval:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
if echofunc(maxval) != maxval:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
if echofunc(minval) != minval:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
error = 0
|
||||
try:
|
||||
echofunc(maxval + 1)
|
||||
error = 1
|
||||
except OverflowError:
|
||||
pass
|
||||
if error == 1:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
try:
|
||||
echofunc(minval - 1)
|
||||
error = 1
|
||||
except OverflowError:
|
||||
pass
|
||||
if error == 1:
|
||||
raise RuntimeError, "bad " + name + " typemap"
|
||||
|
||||
# sys.maxsize is the largest value supported by Py_ssize_t, which should be the same as ptrdiff_t
|
||||
checkType("ptrdiff_t", get_ptrdiff_max, sys.maxsize, get_ptrdiff_min, -(sys.maxsize + 1), ptrdiff_echo)
|
||||
checkType("size_t", get_size_max, (2 * sys.maxsize) + 1, get_size_min, 0, size_echo)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue