blob: 90ca303eca5585692963eecbac17ed3a24924c15 [file] [log] [blame]
Thomas Hellera4ea6032003-04-17 18:55:45 +00001import unittest
2from test import test_support
3import sys
4
5import warnings, re
Thomas Heller54aa5782003-04-24 16:15:29 +00006warnings.filterwarnings("ignore",
7 category=DeprecationWarning,
8 message=".*integer argument expected, got float",
9 module=__name__)
10warnings.filterwarnings("ignore",
11 category=DeprecationWarning,
12 message=".*integer argument expected, got float",
13 module="unittest")
Thomas Hellera4ea6032003-04-17 18:55:45 +000014
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 Rossumbbb931b2003-04-18 00:13:53 +000022> h signed short SHRT_MIN..SHRT_MAX
Thomas Hellera4ea6032003-04-17 18:55:45 +000023> 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
42Plus a C API or two, e.g. PyInt_AsLongMask() ->
43unsigned long and PyInt_AsLongLongMask() -> unsigned
44long long (if that exists).
45"""
46
47LARGE = 0x7FFFFFFF
48VERY_LARGE = 0xFF0000121212121212121242L
49
50from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
Georg Brandl7f573f72006-04-13 07:59:30 +000051 INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
Thomas Hellera4ea6032003-04-17 18:55:45 +000052
Thomas Hellera4ea6032003-04-17 18:55:45 +000053# fake, they are not defined in Python's header files
54LLONG_MAX = 2**63-1
55LLONG_MIN = -2**63
56ULLONG_MAX = 2**64-1
57
58class Long:
59 def __int__(self):
60 return 99L
61
62class Int:
63 def __int__(self):
64 return 99
65
66class Unsigned_TestCase(unittest.TestCase):
67 def test_b(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000068 from _testcapi import getargs_b
Thomas Hellera4ea6032003-04-17 18:55:45 +000069 # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
Thomas Heller54aa5782003-04-24 16:15:29 +000070 self.failUnlessEqual(3, getargs_b(3.14))
71 self.failUnlessEqual(99, getargs_b(Long()))
72 self.failUnlessEqual(99, getargs_b(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000073
Thomas Heller54aa5782003-04-24 16:15:29 +000074 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 Hellera4ea6032003-04-17 18:55:45 +000078
Thomas Heller54aa5782003-04-24 16:15:29 +000079 self.failUnlessEqual(42, getargs_b(42))
80 self.failUnlessEqual(42, getargs_b(42L))
81 self.assertRaises(OverflowError, getargs_b, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +000082
Thomas Hellera4ea6032003-04-17 18:55:45 +000083 def test_B(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000084 from _testcapi import getargs_B
Thomas Hellera4ea6032003-04-17 18:55:45 +000085 # B returns 'unsigned char', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +000086 self.failUnlessEqual(3, getargs_B(3.14))
87 self.failUnlessEqual(99, getargs_B(Long()))
88 self.failUnlessEqual(99, getargs_B(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000089
Thomas Heller54aa5782003-04-24 16:15:29 +000090 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 Hellera4ea6032003-04-17 18:55:45 +000095
Thomas Heller54aa5782003-04-24 16:15:29 +000096 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 Hellera4ea6032003-04-17 18:55:45 +000099
100 def test_H(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000101 from _testcapi import getargs_H
Thomas Hellera4ea6032003-04-17 18:55:45 +0000102 # H returns 'unsigned short', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +0000103 self.failUnlessEqual(3, getargs_H(3.14))
104 self.failUnlessEqual(99, getargs_H(Long()))
105 self.failUnlessEqual(99, getargs_H(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000106
Thomas Heller54aa5782003-04-24 16:15:29 +0000107 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 Hellera4ea6032003-04-17 18:55:45 +0000111
Thomas Heller54aa5782003-04-24 16:15:29 +0000112 self.failUnlessEqual(42, getargs_H(42))
113 self.failUnlessEqual(42, getargs_H(42L))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000114
Thomas Heller54aa5782003-04-24 16:15:29 +0000115 self.failUnlessEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000116
117 def test_I(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000118 from _testcapi import getargs_I
Thomas Hellera4ea6032003-04-17 18:55:45 +0000119 # I returns 'unsigned int', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +0000120 self.failUnlessEqual(3, getargs_I(3.14))
121 self.failUnlessEqual(99, getargs_I(Long()))
122 self.failUnlessEqual(99, getargs_I(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000123
Thomas Heller54aa5782003-04-24 16:15:29 +0000124 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 Hellera4ea6032003-04-17 18:55:45 +0000128
Thomas Heller54aa5782003-04-24 16:15:29 +0000129 self.failUnlessEqual(42, getargs_I(42))
130 self.failUnlessEqual(42, getargs_I(42L))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000131
Thomas Heller54aa5782003-04-24 16:15:29 +0000132 self.failUnlessEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000133
134 def test_k(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000135 from _testcapi import getargs_k
Thomas Hellera4ea6032003-04-17 18:55:45 +0000136 # k returns 'unsigned long', no range checking
137 # it does not accept float, or instances with __int__
Thomas Heller54aa5782003-04-24 16:15:29 +0000138 self.assertRaises(TypeError, getargs_k, 3.14)
139 self.assertRaises(TypeError, getargs_k, Long())
140 self.assertRaises(TypeError, getargs_k, Int())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000141
Thomas Heller54aa5782003-04-24 16:15:29 +0000142 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 Hellera4ea6032003-04-17 18:55:45 +0000146
Thomas Heller54aa5782003-04-24 16:15:29 +0000147 self.failUnlessEqual(42, getargs_k(42))
148 self.failUnlessEqual(42, getargs_k(42L))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000149
Thomas Heller54aa5782003-04-24 16:15:29 +0000150 self.failUnlessEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000151
152class Signed_TestCase(unittest.TestCase):
153 def test_i(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000154 from _testcapi import getargs_i
Thomas Hellera4ea6032003-04-17 18:55:45 +0000155 # i returns 'int', and does range checking (INT_MIN ... INT_MAX)
Thomas Heller54aa5782003-04-24 16:15:29 +0000156 self.failUnlessEqual(3, getargs_i(3.14))
157 self.failUnlessEqual(99, getargs_i(Long()))
158 self.failUnlessEqual(99, getargs_i(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000159
Thomas Heller54aa5782003-04-24 16:15:29 +0000160 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 Hellera4ea6032003-04-17 18:55:45 +0000164
Thomas Heller54aa5782003-04-24 16:15:29 +0000165 self.failUnlessEqual(42, getargs_i(42))
166 self.failUnlessEqual(42, getargs_i(42L))
167 self.assertRaises(OverflowError, getargs_i, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000168
169 def test_l(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000170 from _testcapi import getargs_l
Thomas Hellera4ea6032003-04-17 18:55:45 +0000171 # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
Thomas Heller54aa5782003-04-24 16:15:29 +0000172 self.failUnlessEqual(3, getargs_l(3.14))
173 self.failUnlessEqual(99, getargs_l(Long()))
174 self.failUnlessEqual(99, getargs_l(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000175
Thomas Heller54aa5782003-04-24 16:15:29 +0000176 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 Hellera4ea6032003-04-17 18:55:45 +0000180
Thomas Heller54aa5782003-04-24 16:15:29 +0000181 self.failUnlessEqual(42, getargs_l(42))
182 self.failUnlessEqual(42, getargs_l(42L))
183 self.assertRaises(OverflowError, getargs_l, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000184
Georg Brandl7f573f72006-04-13 07:59:30 +0000185 def test_n(self):
186 from _testcapi import getargs_n
Tim Peters8e6480c2006-04-13 22:58:42 +0000187 # n returns 'Py_ssize_t', and does range checking
Georg Brandl7f573f72006-04-13 07:59:30 +0000188 # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)
189 self.failUnlessEqual(3, getargs_n(3.14))
190 self.failUnlessEqual(99, getargs_n(Long()))
191 self.failUnlessEqual(99, getargs_n(Int()))
192
193 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
194 self.failUnlessEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))
195 self.failUnlessEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX))
196 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)
197
198 self.failUnlessEqual(42, getargs_n(42))
199 self.failUnlessEqual(42, getargs_n(42L))
200 self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
201
Thomas Hellera4ea6032003-04-17 18:55:45 +0000202
203class LongLong_TestCase(unittest.TestCase):
204 def test_L(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000205 from _testcapi import getargs_L
Thomas Hellera4ea6032003-04-17 18:55:45 +0000206 # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
Martin v. Löwis6ce7ed22005-03-03 12:26:35 +0000207 self.failUnlessRaises(TypeError, getargs_L, "Hello")
208 self.failUnlessEqual(3, getargs_L(3.14))
209 self.failUnlessEqual(99, getargs_L(Long()))
210 self.failUnlessEqual(99, getargs_L(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000211
Thomas Heller54aa5782003-04-24 16:15:29 +0000212 self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
213 self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN))
214 self.failUnlessEqual(LLONG_MAX, getargs_L(LLONG_MAX))
215 self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000216
Thomas Heller54aa5782003-04-24 16:15:29 +0000217 self.failUnlessEqual(42, getargs_L(42))
218 self.failUnlessEqual(42, getargs_L(42L))
219 self.assertRaises(OverflowError, getargs_L, VERY_LARGE)
Tim Peters0eadaac2003-04-24 16:02:54 +0000220
Thomas Hellera4ea6032003-04-17 18:55:45 +0000221 def test_K(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000222 from _testcapi import getargs_K
Thomas Hellera4ea6032003-04-17 18:55:45 +0000223 # K return 'unsigned long long', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +0000224 self.assertRaises(TypeError, getargs_K, 3.14)
225 self.assertRaises(TypeError, getargs_K, Long())
226 self.assertRaises(TypeError, getargs_K, Int())
227 self.failUnlessEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
228 self.failUnlessEqual(0, getargs_K(0))
229 self.failUnlessEqual(0, getargs_K(ULLONG_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000230
Thomas Heller54aa5782003-04-24 16:15:29 +0000231 self.failUnlessEqual(42, getargs_K(42))
232 self.failUnlessEqual(42, getargs_K(42L))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000233
Thomas Heller54aa5782003-04-24 16:15:29 +0000234 self.failUnlessEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000235
Georg Brandl5f135782006-07-26 08:03:10 +0000236
237class Tuple_TestCase(unittest.TestCase):
238 def test_tuple(self):
239 from _testcapi import getargs_tuple
Tim Peters95621b22006-07-26 23:23:15 +0000240
Georg Brandl5f135782006-07-26 08:03:10 +0000241 ret = getargs_tuple(1, (2, 3))
242 self.assertEquals(ret, (1,2,3))
243
244 # make sure invalid tuple arguments are handled correctly
245 class seq:
246 def __len__(self):
247 return 2
248 def __getitem__(self, n):
249 raise ValueError
250 self.assertRaises(TypeError, getargs_tuple, 1, seq())
251
252
Thomas Hellera4ea6032003-04-17 18:55:45 +0000253def test_main():
Georg Brandl5f135782006-07-26 08:03:10 +0000254 tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase]
Thomas Hellera4ea6032003-04-17 18:55:45 +0000255 try:
Thomas Heller54aa5782003-04-24 16:15:29 +0000256 from _testcapi import getargs_L, getargs_K
257 except ImportError:
Thomas Hellera4ea6032003-04-17 18:55:45 +0000258 pass # PY_LONG_LONG not available
259 else:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000260 tests.append(LongLong_TestCase)
261 test_support.run_unittest(*tests)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000262
263if __name__ == "__main__":
264 test_main()