diff options
author | Alec Cooper <ahnolds@gmail.com> | 2016-01-06 17:45:21 -0500 |
---|---|---|
committer | Alec Cooper <ahnolds@gmail.com> | 2016-01-06 17:45:21 -0500 |
commit | fc8e76544c8fd9845a25903ca7d3074c487205b7 (patch) | |
tree | c2284c8190da727790079860fc09815bbe97d895 | |
parent | 12b62a562d0f4ac78ace09935140dc8833653f10 (diff) | |
download | swig-fc8e76544c8fd9845a25903ca7d3074c487205b7.tar.gz |
Unit tests for ptrdiff_t/size_t max/min in Python
-rw-r--r-- | Examples/test-suite/primitive_types.i | 11 | ||||
-rw-r--r-- | Examples/test-suite/python/primitive_types_runme.py | 31 |
2 files changed, 42 insertions, 0 deletions
diff --git a/Examples/test-suite/primitive_types.i b/Examples/test-suite/primitive_types.i index 29b44ec8c..17b7a8335 100644 --- a/Examples/test-suite/primitive_types.i +++ b/Examples/test-suite/primitive_types.i @@ -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 diff --git a/Examples/test-suite/python/primitive_types_runme.py b/Examples/test-suite/python/primitive_types_runme.py index 2f8b2d99c..c04dc9552 100644 --- a/Examples/test-suite/python/primitive_types_runme.py +++ b/Examples/test-suite/python/primitive_types_runme.py @@ -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) |