1 | n/a | from ctypes import * |
---|
2 | n/a | import unittest |
---|
3 | n/a | import struct |
---|
4 | n/a | |
---|
5 | n/a | def valid_ranges(*types): |
---|
6 | n/a | # given a sequence of numeric types, collect their _type_ |
---|
7 | n/a | # attribute, which is a single format character compatible with |
---|
8 | n/a | # the struct module, use the struct module to calculate the |
---|
9 | n/a | # minimum and maximum value allowed for this format. |
---|
10 | n/a | # Returns a list of (min, max) values. |
---|
11 | n/a | result = [] |
---|
12 | n/a | for t in types: |
---|
13 | n/a | fmt = t._type_ |
---|
14 | n/a | size = struct.calcsize(fmt) |
---|
15 | n/a | a = struct.unpack(fmt, (b"\x00"*32)[:size])[0] |
---|
16 | n/a | b = struct.unpack(fmt, (b"\xFF"*32)[:size])[0] |
---|
17 | n/a | c = struct.unpack(fmt, (b"\x7F"+b"\x00"*32)[:size])[0] |
---|
18 | n/a | d = struct.unpack(fmt, (b"\x80"+b"\xFF"*32)[:size])[0] |
---|
19 | n/a | result.append((min(a, b, c, d), max(a, b, c, d))) |
---|
20 | n/a | return result |
---|
21 | n/a | |
---|
22 | n/a | ArgType = type(byref(c_int(0))) |
---|
23 | n/a | |
---|
24 | n/a | unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong] |
---|
25 | n/a | signed_types = [c_byte, c_short, c_int, c_long, c_longlong] |
---|
26 | n/a | |
---|
27 | n/a | bool_types = [] |
---|
28 | n/a | |
---|
29 | n/a | float_types = [c_double, c_float] |
---|
30 | n/a | |
---|
31 | n/a | try: |
---|
32 | n/a | c_ulonglong |
---|
33 | n/a | c_longlong |
---|
34 | n/a | except NameError: |
---|
35 | n/a | pass |
---|
36 | n/a | else: |
---|
37 | n/a | unsigned_types.append(c_ulonglong) |
---|
38 | n/a | signed_types.append(c_longlong) |
---|
39 | n/a | |
---|
40 | n/a | try: |
---|
41 | n/a | c_bool |
---|
42 | n/a | except NameError: |
---|
43 | n/a | pass |
---|
44 | n/a | else: |
---|
45 | n/a | bool_types.append(c_bool) |
---|
46 | n/a | |
---|
47 | n/a | unsigned_ranges = valid_ranges(*unsigned_types) |
---|
48 | n/a | signed_ranges = valid_ranges(*signed_types) |
---|
49 | n/a | bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]] |
---|
50 | n/a | |
---|
51 | n/a | ################################################################ |
---|
52 | n/a | |
---|
53 | n/a | class NumberTestCase(unittest.TestCase): |
---|
54 | n/a | |
---|
55 | n/a | def test_default_init(self): |
---|
56 | n/a | # default values are set to zero |
---|
57 | n/a | for t in signed_types + unsigned_types + float_types: |
---|
58 | n/a | self.assertEqual(t().value, 0) |
---|
59 | n/a | |
---|
60 | n/a | def test_unsigned_values(self): |
---|
61 | n/a | # the value given to the constructor is available |
---|
62 | n/a | # as the 'value' attribute |
---|
63 | n/a | for t, (l, h) in zip(unsigned_types, unsigned_ranges): |
---|
64 | n/a | self.assertEqual(t(l).value, l) |
---|
65 | n/a | self.assertEqual(t(h).value, h) |
---|
66 | n/a | |
---|
67 | n/a | def test_signed_values(self): |
---|
68 | n/a | # see above |
---|
69 | n/a | for t, (l, h) in zip(signed_types, signed_ranges): |
---|
70 | n/a | self.assertEqual(t(l).value, l) |
---|
71 | n/a | self.assertEqual(t(h).value, h) |
---|
72 | n/a | |
---|
73 | n/a | def test_bool_values(self): |
---|
74 | n/a | from operator import truth |
---|
75 | n/a | for t, v in zip(bool_types, bool_values): |
---|
76 | n/a | self.assertEqual(t(v).value, truth(v)) |
---|
77 | n/a | |
---|
78 | n/a | def test_typeerror(self): |
---|
79 | n/a | # Only numbers are allowed in the constructor, |
---|
80 | n/a | # otherwise TypeError is raised |
---|
81 | n/a | for t in signed_types + unsigned_types + float_types: |
---|
82 | n/a | self.assertRaises(TypeError, t, "") |
---|
83 | n/a | self.assertRaises(TypeError, t, None) |
---|
84 | n/a | |
---|
85 | n/a | @unittest.skip('test disabled') |
---|
86 | n/a | def test_valid_ranges(self): |
---|
87 | n/a | # invalid values of the correct type |
---|
88 | n/a | # raise ValueError (not OverflowError) |
---|
89 | n/a | for t, (l, h) in zip(unsigned_types, unsigned_ranges): |
---|
90 | n/a | self.assertRaises(ValueError, t, l-1) |
---|
91 | n/a | self.assertRaises(ValueError, t, h+1) |
---|
92 | n/a | |
---|
93 | n/a | def test_from_param(self): |
---|
94 | n/a | # the from_param class method attribute always |
---|
95 | n/a | # returns PyCArgObject instances |
---|
96 | n/a | for t in signed_types + unsigned_types + float_types: |
---|
97 | n/a | self.assertEqual(ArgType, type(t.from_param(0))) |
---|
98 | n/a | |
---|
99 | n/a | def test_byref(self): |
---|
100 | n/a | # calling byref returns also a PyCArgObject instance |
---|
101 | n/a | for t in signed_types + unsigned_types + float_types + bool_types: |
---|
102 | n/a | parm = byref(t()) |
---|
103 | n/a | self.assertEqual(ArgType, type(parm)) |
---|
104 | n/a | |
---|
105 | n/a | |
---|
106 | n/a | def test_floats(self): |
---|
107 | n/a | # c_float and c_double can be created from |
---|
108 | n/a | # Python int and float |
---|
109 | n/a | class FloatLike(object): |
---|
110 | n/a | def __float__(self): |
---|
111 | n/a | return 2.0 |
---|
112 | n/a | f = FloatLike() |
---|
113 | n/a | for t in float_types: |
---|
114 | n/a | self.assertEqual(t(2.0).value, 2.0) |
---|
115 | n/a | self.assertEqual(t(2).value, 2.0) |
---|
116 | n/a | self.assertEqual(t(2).value, 2.0) |
---|
117 | n/a | self.assertEqual(t(f).value, 2.0) |
---|
118 | n/a | |
---|
119 | n/a | def test_integers(self): |
---|
120 | n/a | class FloatLike(object): |
---|
121 | n/a | def __float__(self): |
---|
122 | n/a | return 2.0 |
---|
123 | n/a | f = FloatLike() |
---|
124 | n/a | class IntLike(object): |
---|
125 | n/a | def __int__(self): |
---|
126 | n/a | return 2 |
---|
127 | n/a | i = IntLike() |
---|
128 | n/a | # integers cannot be constructed from floats, |
---|
129 | n/a | # but from integer-like objects |
---|
130 | n/a | for t in signed_types + unsigned_types: |
---|
131 | n/a | self.assertRaises(TypeError, t, 3.14) |
---|
132 | n/a | self.assertRaises(TypeError, t, f) |
---|
133 | n/a | self.assertEqual(t(i).value, 2) |
---|
134 | n/a | |
---|
135 | n/a | def test_sizes(self): |
---|
136 | n/a | for t in signed_types + unsigned_types + float_types + bool_types: |
---|
137 | n/a | try: |
---|
138 | n/a | size = struct.calcsize(t._type_) |
---|
139 | n/a | except struct.error: |
---|
140 | n/a | continue |
---|
141 | n/a | # sizeof of the type... |
---|
142 | n/a | self.assertEqual(sizeof(t), size) |
---|
143 | n/a | # and sizeof of an instance |
---|
144 | n/a | self.assertEqual(sizeof(t()), size) |
---|
145 | n/a | |
---|
146 | n/a | def test_alignments(self): |
---|
147 | n/a | for t in signed_types + unsigned_types + float_types: |
---|
148 | n/a | code = t._type_ # the typecode |
---|
149 | n/a | align = struct.calcsize("c%c" % code) - struct.calcsize(code) |
---|
150 | n/a | |
---|
151 | n/a | # alignment of the type... |
---|
152 | n/a | self.assertEqual((code, alignment(t)), |
---|
153 | n/a | (code, align)) |
---|
154 | n/a | # and alignment of an instance |
---|
155 | n/a | self.assertEqual((code, alignment(t())), |
---|
156 | n/a | (code, align)) |
---|
157 | n/a | |
---|
158 | n/a | def test_int_from_address(self): |
---|
159 | n/a | from array import array |
---|
160 | n/a | for t in signed_types + unsigned_types: |
---|
161 | n/a | # the array module doesn't support all format codes |
---|
162 | n/a | # (no 'q' or 'Q') |
---|
163 | n/a | try: |
---|
164 | n/a | array(t._type_) |
---|
165 | n/a | except ValueError: |
---|
166 | n/a | continue |
---|
167 | n/a | a = array(t._type_, [100]) |
---|
168 | n/a | |
---|
169 | n/a | # v now is an integer at an 'external' memory location |
---|
170 | n/a | v = t.from_address(a.buffer_info()[0]) |
---|
171 | n/a | self.assertEqual(v.value, a[0]) |
---|
172 | n/a | self.assertEqual(type(v), t) |
---|
173 | n/a | |
---|
174 | n/a | # changing the value at the memory location changes v's value also |
---|
175 | n/a | a[0] = 42 |
---|
176 | n/a | self.assertEqual(v.value, a[0]) |
---|
177 | n/a | |
---|
178 | n/a | |
---|
179 | n/a | def test_float_from_address(self): |
---|
180 | n/a | from array import array |
---|
181 | n/a | for t in float_types: |
---|
182 | n/a | a = array(t._type_, [3.14]) |
---|
183 | n/a | v = t.from_address(a.buffer_info()[0]) |
---|
184 | n/a | self.assertEqual(v.value, a[0]) |
---|
185 | n/a | self.assertIs(type(v), t) |
---|
186 | n/a | a[0] = 2.3456e17 |
---|
187 | n/a | self.assertEqual(v.value, a[0]) |
---|
188 | n/a | self.assertIs(type(v), t) |
---|
189 | n/a | |
---|
190 | n/a | def test_char_from_address(self): |
---|
191 | n/a | from ctypes import c_char |
---|
192 | n/a | from array import array |
---|
193 | n/a | |
---|
194 | n/a | a = array('b', [0]) |
---|
195 | n/a | a[0] = ord('x') |
---|
196 | n/a | v = c_char.from_address(a.buffer_info()[0]) |
---|
197 | n/a | self.assertEqual(v.value, b'x') |
---|
198 | n/a | self.assertIs(type(v), c_char) |
---|
199 | n/a | |
---|
200 | n/a | a[0] = ord('?') |
---|
201 | n/a | self.assertEqual(v.value, b'?') |
---|
202 | n/a | |
---|
203 | n/a | # array does not support c_bool / 't' |
---|
204 | n/a | @unittest.skip('test disabled') |
---|
205 | n/a | def test_bool_from_address(self): |
---|
206 | n/a | from ctypes import c_bool |
---|
207 | n/a | from array import array |
---|
208 | n/a | a = array(c_bool._type_, [True]) |
---|
209 | n/a | v = t.from_address(a.buffer_info()[0]) |
---|
210 | n/a | self.assertEqual(v.value, a[0]) |
---|
211 | n/a | self.assertEqual(type(v) is t) |
---|
212 | n/a | a[0] = False |
---|
213 | n/a | self.assertEqual(v.value, a[0]) |
---|
214 | n/a | self.assertEqual(type(v) is t) |
---|
215 | n/a | |
---|
216 | n/a | def test_init(self): |
---|
217 | n/a | # c_int() can be initialized from Python's int, and c_int. |
---|
218 | n/a | # Not from c_long or so, which seems strange, abc should |
---|
219 | n/a | # probably be changed: |
---|
220 | n/a | self.assertRaises(TypeError, c_int, c_long(42)) |
---|
221 | n/a | |
---|
222 | n/a | def test_float_overflow(self): |
---|
223 | n/a | import sys |
---|
224 | n/a | big_int = int(sys.float_info.max) * 2 |
---|
225 | n/a | for t in float_types + [c_longdouble]: |
---|
226 | n/a | self.assertRaises(OverflowError, t, big_int) |
---|
227 | n/a | if (hasattr(t, "__ctype_be__")): |
---|
228 | n/a | self.assertRaises(OverflowError, t.__ctype_be__, big_int) |
---|
229 | n/a | if (hasattr(t, "__ctype_le__")): |
---|
230 | n/a | self.assertRaises(OverflowError, t.__ctype_le__, big_int) |
---|
231 | n/a | |
---|
232 | n/a | @unittest.skip('test disabled') |
---|
233 | n/a | def test_perf(self): |
---|
234 | n/a | check_perf() |
---|
235 | n/a | |
---|
236 | n/a | from ctypes import _SimpleCData |
---|
237 | n/a | class c_int_S(_SimpleCData): |
---|
238 | n/a | _type_ = "i" |
---|
239 | n/a | __slots__ = [] |
---|
240 | n/a | |
---|
241 | n/a | def run_test(rep, msg, func, arg=None): |
---|
242 | n/a | ## items = [None] * rep |
---|
243 | n/a | items = range(rep) |
---|
244 | n/a | from time import clock |
---|
245 | n/a | if arg is not None: |
---|
246 | n/a | start = clock() |
---|
247 | n/a | for i in items: |
---|
248 | n/a | func(arg); func(arg); func(arg); func(arg); func(arg) |
---|
249 | n/a | stop = clock() |
---|
250 | n/a | else: |
---|
251 | n/a | start = clock() |
---|
252 | n/a | for i in items: |
---|
253 | n/a | func(); func(); func(); func(); func() |
---|
254 | n/a | stop = clock() |
---|
255 | n/a | print("%15s: %.2f us" % (msg, ((stop-start)*1e6/5/rep))) |
---|
256 | n/a | |
---|
257 | n/a | def check_perf(): |
---|
258 | n/a | # Construct 5 objects |
---|
259 | n/a | from ctypes import c_int |
---|
260 | n/a | |
---|
261 | n/a | REP = 200000 |
---|
262 | n/a | |
---|
263 | n/a | run_test(REP, "int()", int) |
---|
264 | n/a | run_test(REP, "int(999)", int) |
---|
265 | n/a | run_test(REP, "c_int()", c_int) |
---|
266 | n/a | run_test(REP, "c_int(999)", c_int) |
---|
267 | n/a | run_test(REP, "c_int_S()", c_int_S) |
---|
268 | n/a | run_test(REP, "c_int_S(999)", c_int_S) |
---|
269 | n/a | |
---|
270 | n/a | # Python 2.3 -OO, win2k, P4 700 MHz: |
---|
271 | n/a | # |
---|
272 | n/a | # int(): 0.87 us |
---|
273 | n/a | # int(999): 0.87 us |
---|
274 | n/a | # c_int(): 3.35 us |
---|
275 | n/a | # c_int(999): 3.34 us |
---|
276 | n/a | # c_int_S(): 3.23 us |
---|
277 | n/a | # c_int_S(999): 3.24 us |
---|
278 | n/a | |
---|
279 | n/a | # Python 2.2 -OO, win2k, P4 700 MHz: |
---|
280 | n/a | # |
---|
281 | n/a | # int(): 0.89 us |
---|
282 | n/a | # int(999): 0.89 us |
---|
283 | n/a | # c_int(): 9.99 us |
---|
284 | n/a | # c_int(999): 10.02 us |
---|
285 | n/a | # c_int_S(): 9.87 us |
---|
286 | n/a | # c_int_S(999): 9.85 us |
---|
287 | n/a | |
---|
288 | n/a | if __name__ == '__main__': |
---|
289 | n/a | ## check_perf() |
---|
290 | n/a | unittest.main() |
---|