Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | import sys |
| 4 | |
| 5 | import warnings, re |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 6 | warnings.filterwarnings("ignore", |
| 7 | category=DeprecationWarning, |
| 8 | message=".*integer argument expected, got float", |
| 9 | module=__name__) |
| 10 | warnings.filterwarnings("ignore", |
| 11 | category=DeprecationWarning, |
| 12 | message=".*integer argument expected, got float", |
| 13 | module="unittest") |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 14 | |
| 15 | """ |
| 16 | > How about the following counterproposal. This also changes some of |
| 17 | > the other format codes to be a little more regular. |
| 18 | > |
| 19 | > Code C type Range check |
| 20 | > |
| 21 | > b unsigned char 0..UCHAR_MAX |
Guido van Rossum | bbb931b | 2003-04-18 00:13:53 +0000 | [diff] [blame] | 22 | > h signed short SHRT_MIN..SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 23 | > B unsigned char none ** |
| 24 | > H unsigned short none ** |
| 25 | > k * unsigned long none |
| 26 | > I * unsigned int 0..UINT_MAX |
| 27 | |
| 28 | |
| 29 | > i int INT_MIN..INT_MAX |
| 30 | > l long LONG_MIN..LONG_MAX |
| 31 | |
| 32 | > K * unsigned long long none |
| 33 | > L long long LLONG_MIN..LLONG_MAX |
| 34 | |
| 35 | > Notes: |
| 36 | > |
| 37 | > * New format codes. |
| 38 | > |
| 39 | > ** Changed from previous "range-and-a-half" to "none"; the |
| 40 | > range-and-a-half checking wasn't particularly useful. |
| 41 | |
| 42 | Plus a C API or two, e.g. PyInt_AsLongMask() -> |
| 43 | unsigned long and PyInt_AsLongLongMask() -> unsigned |
| 44 | long long (if that exists). |
| 45 | """ |
| 46 | |
| 47 | LARGE = 0x7FFFFFFF |
| 48 | VERY_LARGE = 0xFF0000121212121212121242L |
| 49 | |
| 50 | from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
| 51 | INT_MIN, LONG_MIN, LONG_MAX |
| 52 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 53 | # fake, they are not defined in Python's header files |
| 54 | LLONG_MAX = 2**63-1 |
| 55 | LLONG_MIN = -2**63 |
| 56 | ULLONG_MAX = 2**64-1 |
| 57 | |
| 58 | class Long: |
| 59 | def __int__(self): |
| 60 | return 99L |
| 61 | |
| 62 | class Int: |
| 63 | def __int__(self): |
| 64 | return 99 |
| 65 | |
| 66 | class Unsigned_TestCase(unittest.TestCase): |
| 67 | def test_b(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 68 | from _testcapi import getargs_b |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 69 | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 70 | self.failUnlessEqual(3, getargs_b(3.14)) |
| 71 | self.failUnlessEqual(99, getargs_b(Long())) |
| 72 | self.failUnlessEqual(99, getargs_b(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 73 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 74 | self.assertRaises(OverflowError, getargs_b, -1) |
| 75 | self.failUnlessEqual(0, getargs_b(0)) |
| 76 | self.failUnlessEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
| 77 | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 78 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 79 | self.failUnlessEqual(42, getargs_b(42)) |
| 80 | self.failUnlessEqual(42, getargs_b(42L)) |
| 81 | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 82 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 83 | def test_B(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 84 | from _testcapi import getargs_B |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 85 | # B returns 'unsigned char', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 86 | self.failUnlessEqual(3, getargs_B(3.14)) |
| 87 | self.failUnlessEqual(99, getargs_B(Long())) |
| 88 | self.failUnlessEqual(99, getargs_B(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 89 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 90 | self.failUnlessEqual(UCHAR_MAX, getargs_B(-1)) |
| 91 | self.failUnlessEqual(UCHAR_MAX, getargs_B(-1L)) |
| 92 | self.failUnlessEqual(0, getargs_B(0)) |
| 93 | self.failUnlessEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 94 | self.failUnlessEqual(0, getargs_B(UCHAR_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 95 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 96 | self.failUnlessEqual(42, getargs_B(42)) |
| 97 | self.failUnlessEqual(42, getargs_B(42L)) |
| 98 | self.failUnlessEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 99 | |
| 100 | def test_H(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 101 | from _testcapi import getargs_H |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 102 | # H returns 'unsigned short', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 103 | self.failUnlessEqual(3, getargs_H(3.14)) |
| 104 | self.failUnlessEqual(99, getargs_H(Long())) |
| 105 | self.failUnlessEqual(99, getargs_H(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 106 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 107 | self.failUnlessEqual(USHRT_MAX, getargs_H(-1)) |
| 108 | self.failUnlessEqual(0, getargs_H(0)) |
| 109 | self.failUnlessEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 110 | self.failUnlessEqual(0, getargs_H(USHRT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 111 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 112 | self.failUnlessEqual(42, getargs_H(42)) |
| 113 | self.failUnlessEqual(42, getargs_H(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 114 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 115 | self.failUnlessEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 116 | |
| 117 | def test_I(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 118 | from _testcapi import getargs_I |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 119 | # I returns 'unsigned int', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 120 | self.failUnlessEqual(3, getargs_I(3.14)) |
| 121 | self.failUnlessEqual(99, getargs_I(Long())) |
| 122 | self.failUnlessEqual(99, getargs_I(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 123 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 124 | self.failUnlessEqual(UINT_MAX, getargs_I(-1)) |
| 125 | self.failUnlessEqual(0, getargs_I(0)) |
| 126 | self.failUnlessEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 127 | self.failUnlessEqual(0, getargs_I(UINT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 128 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 129 | self.failUnlessEqual(42, getargs_I(42)) |
| 130 | self.failUnlessEqual(42, getargs_I(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 131 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 132 | self.failUnlessEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 133 | |
| 134 | def test_k(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 135 | from _testcapi import getargs_k |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 136 | # k returns 'unsigned long', no range checking |
| 137 | # it does not accept float, or instances with __int__ |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 138 | self.assertRaises(TypeError, getargs_k, 3.14) |
| 139 | self.assertRaises(TypeError, getargs_k, Long()) |
| 140 | self.assertRaises(TypeError, getargs_k, Int()) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 141 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 142 | self.failUnlessEqual(ULONG_MAX, getargs_k(-1)) |
| 143 | self.failUnlessEqual(0, getargs_k(0)) |
| 144 | self.failUnlessEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 145 | self.failUnlessEqual(0, getargs_k(ULONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 146 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 147 | self.failUnlessEqual(42, getargs_k(42)) |
| 148 | self.failUnlessEqual(42, getargs_k(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 149 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 150 | self.failUnlessEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 151 | |
| 152 | class Signed_TestCase(unittest.TestCase): |
| 153 | def test_i(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 154 | from _testcapi import getargs_i |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 155 | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 156 | self.failUnlessEqual(3, getargs_i(3.14)) |
| 157 | self.failUnlessEqual(99, getargs_i(Long())) |
| 158 | self.failUnlessEqual(99, getargs_i(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 159 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 160 | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
| 161 | self.failUnlessEqual(INT_MIN, getargs_i(INT_MIN)) |
| 162 | self.failUnlessEqual(INT_MAX, getargs_i(INT_MAX)) |
| 163 | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 164 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 165 | self.failUnlessEqual(42, getargs_i(42)) |
| 166 | self.failUnlessEqual(42, getargs_i(42L)) |
| 167 | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 168 | |
| 169 | def test_l(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 170 | from _testcapi import getargs_l |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 171 | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 172 | self.failUnlessEqual(3, getargs_l(3.14)) |
| 173 | self.failUnlessEqual(99, getargs_l(Long())) |
| 174 | self.failUnlessEqual(99, getargs_l(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 175 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 176 | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
| 177 | self.failUnlessEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 178 | self.failUnlessEqual(LONG_MAX, getargs_l(LONG_MAX)) |
| 179 | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 180 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 181 | self.failUnlessEqual(42, getargs_l(42)) |
| 182 | self.failUnlessEqual(42, getargs_l(42L)) |
| 183 | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | class LongLong_TestCase(unittest.TestCase): |
| 187 | def test_L(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 188 | from _testcapi import getargs_L |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 189 | # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX) |
| 190 | |
| 191 | # XXX There's a bug in getargs.c, format code "L": |
| 192 | # If you pass something else than a Python long, you |
| 193 | # get "Bad argument to internal function". |
| 194 | |
| 195 | # So these three tests are commented out: |
| 196 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 197 | ## self.failUnlessEqual(3, getargs_L(3.14)) |
| 198 | ## self.failUnlessEqual(99, getargs_L(Long())) |
| 199 | ## self.failUnlessEqual(99, getargs_L(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 200 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 201 | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
| 202 | self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 203 | self.failUnlessEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
| 204 | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 205 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 206 | self.failUnlessEqual(42, getargs_L(42)) |
| 207 | self.failUnlessEqual(42, getargs_L(42L)) |
| 208 | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 209 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 210 | def test_K(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 211 | from _testcapi import getargs_K |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 212 | # K return 'unsigned long long', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 213 | self.assertRaises(TypeError, getargs_K, 3.14) |
| 214 | self.assertRaises(TypeError, getargs_K, Long()) |
| 215 | self.assertRaises(TypeError, getargs_K, Int()) |
| 216 | self.failUnlessEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 217 | self.failUnlessEqual(0, getargs_K(0)) |
| 218 | self.failUnlessEqual(0, getargs_K(ULLONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 219 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 220 | self.failUnlessEqual(42, getargs_K(42)) |
| 221 | self.failUnlessEqual(42, getargs_K(42L)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 222 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 223 | self.failUnlessEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 224 | |
| 225 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame^] | 226 | tests = [Signed_TestCase, Unsigned_TestCase] |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 227 | try: |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 228 | from _testcapi import getargs_L, getargs_K |
| 229 | except ImportError: |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 230 | pass # PY_LONG_LONG not available |
| 231 | else: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame^] | 232 | tests.append(LongLong_TestCase) |
| 233 | test_support.run_unittest(*tests) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 234 | |
| 235 | if __name__ == "__main__": |
| 236 | test_main() |