summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_getargs2.py
blob: 53ad20cab2385e4a50693b5ce28951ec43d41232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import unittest
from test import test_support
import sys

import warnings, re
warnings.filterwarnings("ignore", category=DeprecationWarning, module=__name__)

"""
> How about the following counterproposal. This also changes some of
> the other format codes to be a little more regular.
>
> Code C type Range check
>
> b unsigned char 0..UCHAR_MAX
> h signed short SHRT_MIN..SHRT_MAX
> B unsigned char none **
> H unsigned short none **
> k * unsigned long none
> I * unsigned int 0..UINT_MAX


> i int INT_MIN..INT_MAX
> l long LONG_MIN..LONG_MAX

> K * unsigned long long none
> L long long LLONG_MIN..LLONG_MAX

> Notes:
>
> * New format codes.
>
> ** Changed from previous "range-and-a-half" to "none"; the
> range-and-a-half checking wasn't particularly useful.

Plus a C API or two, e.g. PyInt_AsLongMask() ->
unsigned long and PyInt_AsLongLongMask() -> unsigned
long long (if that exists).
"""

LARGE = 0x7FFFFFFF
VERY_LARGE = 0xFF0000121212121212121242L

from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
     INT_MIN, LONG_MIN, LONG_MAX

from _testcapi import getargs_ul as ul_convert
from _testcapi import getargs_l as l_convert
try:
    from _testcapi import getargs_ll as ll_convert
    from _testcapi import getargs_ull as ull_convert
except ImportError:
    pass

# fake, they are not defined in Python's header files
LLONG_MAX = 2**63-1
LLONG_MIN = -2**63
ULLONG_MAX = 2**64-1

class Long:
    def __int__(self):
        return 99L

class Int:
    def __int__(self):
        return 99

class Unsigned_TestCase(unittest.TestCase):
    def test_b(self):
        # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
        self.failUnlessEqual(3, ul_convert("b", 3.14))
        self.failUnlessEqual(99, ul_convert("b", Long()))
        self.failUnlessEqual(99, ul_convert("b", Int()))

        self.assertRaises(OverflowError, ul_convert, "b", -1)
        self.failUnlessEqual(0, ul_convert("b", 0))
        self.failUnlessEqual(UCHAR_MAX, ul_convert("b", UCHAR_MAX))
        self.assertRaises(OverflowError, ul_convert, "b", UCHAR_MAX + 1)

        self.failUnlessEqual(42, ul_convert("b", 42))
        self.failUnlessEqual(42, ul_convert("b", 42L))
        self.assertRaises(OverflowError, ul_convert, "b", VERY_LARGE)

    def test_B(self):
        # B returns 'unsigned char', no range checking
        self.failUnless(3 == ul_convert("B", 3.14))
        self.failUnlessEqual(99, ul_convert("B", Long()))
        self.failUnlessEqual(99, ul_convert("B", Int()))

        self.failUnlessEqual(UCHAR_MAX, ul_convert("B", -1))
        self.failUnlessEqual(UCHAR_MAX, ul_convert("B", -1L))
        self.failUnlessEqual(0, ul_convert("B", 0))
        self.failUnlessEqual(UCHAR_MAX, ul_convert("B", UCHAR_MAX))
        self.failUnlessEqual(0, ul_convert("B", UCHAR_MAX+1))

        self.failUnlessEqual(42, ul_convert("B", 42))
        self.failUnlessEqual(42, ul_convert("B", 42L))
        self.failUnlessEqual(UCHAR_MAX & VERY_LARGE, ul_convert("B", VERY_LARGE))

    def test_H(self):
        # H returns 'unsigned short', no range checking
        self.failUnlessEqual(3, ul_convert("H", 3.14))
        self.failUnlessEqual(99, ul_convert("H", Long()))
        self.failUnlessEqual(99, ul_convert("H", Int()))

        self.failUnlessEqual(USHRT_MAX, ul_convert("H", -1))
        self.failUnlessEqual(0, ul_convert("H", 0))
        self.failUnlessEqual(USHRT_MAX, ul_convert("H", USHRT_MAX))
        self.failUnlessEqual(0, ul_convert("H", USHRT_MAX+1))

        self.failUnlessEqual(42, ul_convert("H", 42))
        self.failUnlessEqual(42, ul_convert("H", 42L))

        self.failUnlessEqual(VERY_LARGE & USHRT_MAX, ul_convert("H", VERY_LARGE))

    def test_I(self):
        # I returns 'unsigned int', no range checking
        self.failUnlessEqual(3, ul_convert("I", 3.14))
        self.failUnlessEqual(99, ul_convert("I", Long()))
        self.failUnlessEqual(99, ul_convert("I", Int()))

        self.failUnlessEqual(UINT_MAX, ul_convert("I", -1))
        self.failUnlessEqual(0, ul_convert("I", 0))
        self.failUnlessEqual(UINT_MAX, ul_convert("I", UINT_MAX))
        self.failUnlessEqual(0, ul_convert("I", UINT_MAX+1))

        self.failUnlessEqual(42, ul_convert("I", 42))
        self.failUnlessEqual(42, ul_convert("I", 42L))

        self.failUnlessEqual(VERY_LARGE & UINT_MAX, ul_convert("I", VERY_LARGE))

    def test_k(self):
        # k returns 'unsigned long', no range checking
        # it does not accept float, or instances with __int__
        self.assertRaises(TypeError, ul_convert, "k", 3.14)
        self.assertRaises(TypeError, ul_convert, "k", Long())
        self.assertRaises(TypeError, ul_convert, "k", Int())

        self.failUnlessEqual(ULONG_MAX, ul_convert("k", -1))
        self.failUnlessEqual(0, ul_convert("k", 0))
        self.failUnlessEqual(ULONG_MAX, ul_convert("k", ULONG_MAX))
        self.failUnlessEqual(0, ul_convert("k", ULONG_MAX+1))

        self.failUnlessEqual(42, ul_convert("k", 42))
        self.failUnlessEqual(42, ul_convert("k", 42L))

        self.failUnlessEqual(VERY_LARGE & ULONG_MAX, ul_convert("k", VERY_LARGE))

class Signed_TestCase(unittest.TestCase):
    def test_i(self):
        # i returns 'int', and does range checking (INT_MIN ... INT_MAX)
        self.failUnlessEqual(3, l_convert("i", 3.14))
        self.failUnlessEqual(99, l_convert("i", Long()))
        self.failUnlessEqual(99, l_convert("i", Int()))

        self.assertRaises(OverflowError, l_convert, "i", INT_MIN-1)
        self.failUnlessEqual(INT_MIN, l_convert("i", INT_MIN))
        self.failUnlessEqual(INT_MAX, l_convert("i", INT_MAX))
        self.assertRaises(OverflowError, l_convert, "i", INT_MAX+1)

        self.failUnlessEqual(42, l_convert("i", 42))
        self.failUnlessEqual(42, l_convert("i", 42L))
        self.assertRaises(OverflowError, l_convert, "i", VERY_LARGE)

    def test_l(self):
        # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
        self.failUnlessEqual(3, l_convert("l", 3.14))
        self.failUnlessEqual(99, l_convert("l", Long()))
        self.failUnlessEqual(99, l_convert("l", Int()))

        self.assertRaises(OverflowError, l_convert, "l", LONG_MIN-1)
        self.failUnlessEqual(LONG_MIN, l_convert("l", LONG_MIN))
        self.failUnlessEqual(LONG_MAX, l_convert("l", LONG_MAX))
        self.assertRaises(OverflowError, l_convert, "l", LONG_MAX+1)

        self.failUnlessEqual(42, l_convert("l", 42))
        self.failUnlessEqual(42, l_convert("l", 42L))
        self.assertRaises(OverflowError, l_convert, "l", VERY_LARGE)


class LongLong_TestCase(unittest.TestCase):
    def test_L(self):
        # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)

        # XXX There's a bug in getargs.c, format code "L":
        # If you pass something else than a Python long, you
        # get "Bad argument to internal function".

        # So these three tests are commented out:

##        self.failUnlessEqual(3, ll_convert("L", 3.14))
##        self.failUnlessEqual(99, ll_convert("L", Long()))
##        self.failUnlessEqual(99, ll_convert("L", Int()))

        self.assertRaises(OverflowError, ll_convert, "L", LLONG_MIN-1)
        self.failUnlessEqual(LLONG_MIN, ll_convert("L", LLONG_MIN))
        self.failUnlessEqual(LLONG_MAX, ll_convert("L", LLONG_MAX))
        self.assertRaises(OverflowError, ll_convert, "L", LLONG_MAX+1)

        self.failUnlessEqual(42, ll_convert("L", 42))
        self.failUnlessEqual(42, ll_convert("L", 42L))
        self.assertRaises(OverflowError, ll_convert, "L", VERY_LARGE)
        
    def test_K(self):
        # K return 'unsigned long long', no range checking
        self.assertRaises(TypeError, ull_convert, "K", 3.14)
        self.assertRaises(TypeError, ull_convert, "K", Long())
        self.assertRaises(TypeError, ull_convert, "K", Int())
        self.failUnlessEqual(ULLONG_MAX, ull_convert("K", ULLONG_MAX))
        self.failUnlessEqual(0, ull_convert("K", 0))
        self.failUnlessEqual(0, ull_convert("K", ULLONG_MAX+1))

        self.failUnlessEqual(42, ull_convert("K", 42))
        self.failUnlessEqual(42, ull_convert("K", 42L))

        self.failUnlessEqual(VERY_LARGE & ULLONG_MAX, ull_convert("K", VERY_LARGE))

def test_main():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(Signed_TestCase))
    suite.addTest(unittest.makeSuite(Unsigned_TestCase))
    try:
        ll_convert
    except NameError:
        pass # PY_LONG_LONG not available
    else:
        suite.addTest(unittest.makeSuite(LongLong_TestCase))
    test_support.run_suite(suite)

if __name__ == "__main__":
    test_main()