Unit tests for ptrdiff_t/size_t max/min in Python

This commit is contained in:
Alec Cooper 2016-01-06 17:45:21 -05:00
commit fc8e76544c
2 changed files with 42 additions and 0 deletions

View file

@ -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)