]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/test/test_builtin_converters.py
1 # Copyright David Abrahams 2004. Distributed under the Boost
2 # Software License, Version 1.0. (See accompanying
3 # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 if (sys
.version_info
.major
>= 3):
8 >>> from builtin_converters_ext import *
10 # Provide values for integer converter tests
11 >>> def _signed_values(s):
12 ... base = 2 ** (8 * s - 1)
13 ... return [[-base, -1, 1, base - 1], [-base - 1, base]]
14 >>> def _unsigned_values(s):
15 ... base = 2 ** (8 * s)
16 ... return [[1, base - 1], [long(-1), -1, base]]
18 # Wrappers to simplify tests
19 >>> def should_pass(method, values):
20 ... result = map(method, values[0])
21 ... if result != values[0]:
22 ... print("Got %s but expected %s" % (result, values[0]))
23 >>> def test_overflow(method, values):
24 ... for v in values[1]:
26 ... except OverflowError: pass
27 ... else: print("OverflowError expected")
29 # Synthesize idendity functions in case long long not supported
30 >>> if not 'rewrap_value_long_long' in dir():
31 ... def rewrap_value_long_long(x): return long(x)
32 ... def rewrap_value_unsigned_long_long(x): return long(x)
33 ... def rewrap_const_reference_long_long(x): return long(x)
34 ... def rewrap_const_reference_unsigned_long_long(x): return long(x)
35 >>> if not 'long_long_size' in dir():
36 ... def long_long_size(): return long_size()
38 >>> try: bool_exists = bool
41 ... rewrap_value_bool(True)
42 ... rewrap_value_bool(False)
46 >>> rewrap_value_bool(None)
48 >>> rewrap_value_bool(0)
50 >>> rewrap_value_bool(33)
52 >>> rewrap_value_char('x')
55 Note that there's currently silent truncation of strings passed to
58 >>> rewrap_value_char('xy')
60 >>> rewrap_value_signed_char(42)
62 >>> rewrap_value_unsigned_char(42)
64 >>> rewrap_value_int(42)
66 >>> rewrap_value_unsigned_int(42)
68 >>> rewrap_value_short(42)
70 >>> rewrap_value_unsigned_short(42)
72 >>> rewrap_value_long(42)
74 >>> rewrap_value_unsigned_long(42)
77 test unsigned long values which don't fit in a signed long.
78 strip any 'L' characters in case the platform has > 32 bit longs
80 >>> hex(rewrap_value_unsigned_long(0x80000001L)).replace('L','')
83 >>> rewrap_value_long_long(42) == 42
85 >>> rewrap_value_unsigned_long_long(42) == 42
88 show that we have range checking.
90 >>> should_pass(rewrap_value_signed_char, _signed_values(char_size()))
91 >>> should_pass(rewrap_value_short, _signed_values(short_size()))
92 >>> should_pass(rewrap_value_int, _signed_values(int_size()))
93 >>> should_pass(rewrap_value_long, _signed_values(long_size()))
94 >>> should_pass(rewrap_value_long_long, _signed_values(long_long_size()))
96 >>> should_pass(rewrap_value_unsigned_char, _unsigned_values(char_size()))
97 >>> should_pass(rewrap_value_unsigned_short, _unsigned_values(short_size()))
98 >>> should_pass(rewrap_value_unsigned_int, _unsigned_values(int_size()))
99 >>> should_pass(rewrap_value_unsigned_long, _unsigned_values(long_size()))
100 >>> should_pass(rewrap_value_unsigned_long_long,
101 ... _unsigned_values(long_long_size()))
103 >>> test_overflow(rewrap_value_signed_char, _signed_values(char_size()))
104 >>> test_overflow(rewrap_value_short, _signed_values(short_size()))
105 >>> test_overflow(rewrap_value_int, _signed_values(int_size()))
106 >>> test_overflow(rewrap_value_long, _signed_values(long_size()))
107 >>> test_overflow(rewrap_value_long_long, _signed_values(long_long_size()))
109 >>> test_overflow(rewrap_value_unsigned_char, _unsigned_values(char_size()))
110 >>> test_overflow(rewrap_value_unsigned_short, _unsigned_values(short_size()))
111 >>> test_overflow(rewrap_value_unsigned_int, _unsigned_values(int_size()))
112 >>> test_overflow(rewrap_value_unsigned_long, _unsigned_values(long_size()))
114 # Exceptionally for PyLong_AsUnsignedLongLong(), a negative value raises
115 # TypeError on Python versions prior to 2.7
116 >>> for v in _unsigned_values(long_long_size())[1]:
117 ... try: rewrap_value_unsigned_long_long(v)
118 ... except (OverflowError, TypeError): pass
119 ... else: print("OverflowError or TypeError expected")
121 >>> assert abs(rewrap_value_float(4.2) - 4.2) < .000001
122 >>> rewrap_value_double(4.2) - 4.2
124 >>> rewrap_value_long_double(4.2) - 4.2
127 >>> assert abs(rewrap_value_complex_float(4+.2j) - (4+.2j)) < .000001
128 >>> assert abs(rewrap_value_complex_double(4+.2j) - (4+.2j)) < .000001
129 >>> assert abs(rewrap_value_complex_long_double(4+.2j) - (4+.2j)) < .000001
131 >>> rewrap_value_cstring('hello, world')
133 >>> rewrap_value_string('yo, wassup?')
136 >>> print(rewrap_value_wstring(u'yo, wassup?'))
139 test that overloading on unicode works:
141 >>> print(rewrap_value_string(u'yo, wassup?'))
144 wrap strings with embedded nulls:
146 >>> rewrap_value_string('yo,\0wassup?')
149 >>> rewrap_value_handle(1)
152 >>> assert rewrap_value_handle(x) is x
153 >>> assert rewrap_value_object(x) is x
155 Note that we can currently get a mutable pointer into an immutable
158 >>> rewrap_value_mutable_cstring('hello, world')
161 >>> rewrap_const_reference_bool(None)
163 >>> rewrap_const_reference_bool(0)
166 >>> try: rewrap_const_reference_bool('yes')
167 ... except TypeError: pass
168 ... else: print('expected a TypeError exception')
170 >>> rewrap_const_reference_char('x')
173 Note that there's currently silent truncation of strings passed to
176 >>> rewrap_const_reference_char('xy')
178 >>> rewrap_const_reference_signed_char(42)
180 >>> rewrap_const_reference_unsigned_char(42)
182 >>> rewrap_const_reference_int(42)
184 >>> rewrap_const_reference_unsigned_int(42)
186 >>> rewrap_const_reference_short(42)
188 >>> rewrap_const_reference_unsigned_short(42)
190 >>> rewrap_const_reference_long(42)
192 >>> rewrap_const_reference_unsigned_long(42)
194 >>> rewrap_const_reference_long_long(42) == 42
196 >>> rewrap_const_reference_unsigned_long_long(42) == 42
200 >>> assert abs(rewrap_const_reference_float(4.2) - 4.2) < .000001
201 >>> rewrap_const_reference_double(4.2) - 4.2
203 >>> rewrap_const_reference_long_double(4.2) - 4.2
206 >>> assert abs(rewrap_const_reference_complex_float(4+.2j) - (4+.2j)) < .000001
207 >>> assert abs(rewrap_const_reference_complex_double(4+.2j) - (4+.2j)) < .000001
208 >>> assert abs(rewrap_const_reference_complex_long_double(4+.2j) - (4+.2j)) < .000001
210 >>> rewrap_const_reference_cstring('hello, world')
212 >>> rewrap_const_reference_string('yo, wassup?')
215 >>> rewrap_const_reference_handle(1)
218 >>> assert rewrap_const_reference_handle(x) is x
219 >>> assert rewrap_const_reference_object(x) is x
220 >>> assert rewrap_reference_object(x) is x
223 Check that None <==> NULL
225 >>> rewrap_const_reference_cstring(None)
227 But None cannot be converted to a string object:
229 >>> try: rewrap_const_reference_string(None)
230 ... except TypeError: pass
231 ... else: print('expected a TypeError exception')
233 Now check implicit conversions between floating/integer types
235 >>> rewrap_const_reference_float(42)
238 >>> rewrap_const_reference_float(long(42))
241 >>> try: rewrap_const_reference_int(42.0)
242 ... except TypeError: pass
243 ... else: print('expected a TypeError exception')
245 >>> rewrap_value_float(42)
248 >>> try: rewrap_value_int(42.0)
249 ... except TypeError: pass
250 ... else: print('expected a TypeError exception')
252 Check that classic classes also work
255 ... def __int__(self):
257 ... def __float__(self):
259 ... def __complex__(self):
260 ... return complex(4+.2j)
261 ... def __str__(self):
264 >>> try: rewrap_const_reference_float(FortyTwo())
265 ... except TypeError: pass
266 ... else: print('expected a TypeError exception')
268 >>> try: rewrap_value_int(FortyTwo())
269 ... except TypeError: pass
270 ... else: print('expected a TypeError exception')
272 >>> try: rewrap_const_reference_string(FortyTwo())
273 ... except TypeError: pass
274 ... else: print('expected a TypeError exception')
276 >>> try: rewrap_value_complex_double(FortyTwo())
277 ... except TypeError: pass
278 ... else: print('expected a TypeError exception')
280 # show that arbitrary handle<T> instantiations can be returned
281 >>> assert get_type(1) is type(1)
283 >>> assert return_null_handle() is None
286 def run(args
= None):
289 import builtin_converters_ext
291 if 'rewrap_value_long_long' in dir(builtin_converters_ext
):
292 print('LONG_LONG supported, testing...')
294 print('LONG_LONG not supported, skipping those tests...')
298 return doctest
.testmod(sys
.modules
.get(__name__
))
300 if __name__
== '__main__':
304 if (status
== 0): print("Done.")