| | 7 | # Use ctypes to get native C type sizes |
| | 8 | >>> from ctypes import sizeof, c_char, c_short, c_int, c_long, c_longlong |
| | 9 | >>> def test_values_signed(t): |
| | 10 | ... base = 2 ** (8 * sizeof(t) - 1) |
| | 11 | ... return [[-base, -1, 1, base - 1], [-base - 1, base]] |
| | 12 | >>> def test_values_unsigned(t): |
| | 13 | ... base = 2 ** (8 * sizeof(t)) |
| | 14 | ... return [[1, base - 1], [-1L, -1, base]] |
| | 15 | >>> def should_pass(method, values): |
| | 16 | ... result = map(method, values) |
| | 17 | ... if result != values: |
| | 18 | ... print "Got %s but expected %s" % (result, values) |
| | 19 | >>> def test_overflow(method, values): |
| | 20 | ... for v in values: |
| | 21 | ... try: method(v) |
| | 22 | ... except OverflowError: pass |
| | 23 | ... else: print "OverflowError expected" |
| | 24 | |
| 70 | | >>> try: rewrap_value_int(sys.maxint * 2) |
| 71 | | ... except OverflowError: pass |
| 72 | | ... else: print 'expected an OverflowError!' |
| | 84 | >>> should_pass(rewrap_value_signed_char, test_values_signed(c_char)[0]) |
| | 85 | >>> should_pass(rewrap_value_short, test_values_signed(c_short)[0]) |
| | 86 | >>> should_pass(rewrap_value_int, test_values_signed(c_int)[0]) |
| | 87 | >>> should_pass(rewrap_value_long, test_values_signed(c_long)[0]) |
| | 88 | >>> should_pass(rewrap_value_long_long, test_values_signed(c_longlong)[0]) |
| | 90 | >>> should_pass(rewrap_value_unsigned_char, test_values_unsigned(c_char)[0]) |
| | 91 | >>> should_pass(rewrap_value_unsigned_short, test_values_unsigned(c_short)[0]) |
| | 92 | >>> should_pass(rewrap_value_unsigned_int, test_values_unsigned(c_int)[0]) |
| | 93 | >>> should_pass(rewrap_value_unsigned_long, test_values_unsigned(c_long)[0]) |
| | 94 | >>> should_pass(rewrap_value_unsigned_long_long, |
| | 95 | ... test_values_unsigned(c_longlong)[0]) |
| | 97 | >>> test_overflow(rewrap_value_signed_char, test_values_signed(c_char)[1]) |
| | 98 | >>> test_overflow(rewrap_value_short, test_values_signed(c_short)[1]) |
| | 99 | >>> test_overflow(rewrap_value_int, test_values_signed(c_int)[1]) |
| | 100 | >>> test_overflow(rewrap_value_long, test_values_signed(c_long)[1]) |
| | 101 | >>> test_overflow(rewrap_value_long_long, test_values_signed(c_longlong)[1]) |
| | 102 | |
| | 103 | >>> test_overflow(rewrap_value_unsigned_char, test_values_unsigned(c_char)[1]) |
| | 104 | >>> test_overflow(rewrap_value_unsigned_short, test_values_unsigned(c_short)[1]) |
| | 105 | >>> test_overflow(rewrap_value_unsigned_int, test_values_unsigned(c_int)[1]) |
| | 106 | >>> test_overflow(rewrap_value_unsigned_long, test_values_unsigned(c_long)[1]) |
| | 107 | |
| | 108 | # Exceptionally for PyLong_AsUnsignedLongLong(), a negative value raises |
| | 109 | # TypeError on Python versions prior to 2.7 |
| | 110 | >>> for v in test_values_unsigned(c_longlong)[1]: |
| | 111 | ... try: rewrap_value_unsigned_long_long(v) |
| | 112 | ... except (OverflowError, TypeError): pass |
| | 113 | ... else: print "OverflowError or TypeError expected" |
| | 114 | |