Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/test/test_builtin_converters.py @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 6.5 KB
Line 
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)
4r"""
5>>> from builtin_converters_ext import *
6
7# Synthesize idendity functions in case long long not supported
8>>> if not 'rewrap_value_long_long' in dir():
9...     def rewrap_value_long_long(x): return long(x)
10...     def rewrap_value_unsigned_long_long(x): return long(x)
11...     def rewrap_const_reference_long_long(x): return long(x)
12...     def rewrap_const_reference_unsigned_long_long(x): return long(x)
13
14>>> try: bool_exists = bool
15... except: pass
16... else:
17...     rewrap_value_bool(True)
18...     rewrap_value_bool(False)
19True
20False
21
22>>> rewrap_value_bool(None)
230
24>>> rewrap_value_bool(0)
250
26>>> rewrap_value_bool(33)
271
28>>> rewrap_value_char('x')
29'x'
30
31  Note that there's currently silent truncation of strings passed to
32  char arguments.
33
34>>> rewrap_value_char('xy')
35'x'
36>>> rewrap_value_signed_char(42)
3742
38>>> rewrap_value_unsigned_char(42)
3942
40>>> rewrap_value_int(42)
4142
42>>> rewrap_value_unsigned_int(42)
4342
44>>> rewrap_value_short(42)
4542
46>>> rewrap_value_unsigned_short(42)
4742
48>>> rewrap_value_long(42)
4942
50>>> rewrap_value_unsigned_long(42)
5142
52
53    test unsigned long values which don't fit in a signed long.
54    strip any 'L' characters in case the platform has > 32 bit longs
55       
56>>> hex(rewrap_value_unsigned_long(0x80000001L)).replace('L','')
57'0x80000001'
58
59>>> rewrap_value_long_long(42)
6042L
61>>> rewrap_value_unsigned_long_long(42)
6242L
63
64   show that we have range checking.
65 
66>>> try: rewrap_value_unsigned_short(-42)
67... except OverflowError: pass
68... else: print 'expected an OverflowError!'
69
70>>> try: rewrap_value_int(sys.maxint * 2)
71... except OverflowError: pass
72... else: print 'expected an OverflowError!'
73
74
75>>> assert abs(rewrap_value_float(4.2) - 4.2) < .000001
76>>> rewrap_value_double(4.2) - 4.2
770.0
78>>> rewrap_value_long_double(4.2) - 4.2
790.0
80
81>>> assert abs(rewrap_value_complex_float(4+.2j) - (4+.2j)) < .000001
82>>> assert abs(rewrap_value_complex_double(4+.2j) - (4+.2j)) < .000001
83>>> assert abs(rewrap_value_complex_long_double(4+.2j) - (4+.2j)) < .000001
84
85>>> rewrap_value_cstring('hello, world')
86'hello, world'
87>>> rewrap_value_string('yo, wassup?')
88'yo, wassup?'
89
90>>> try:
91...     if unicode: pass
92... except:
93...     print "u'yo, wassup?'"
94... else:
95...     eval("rewrap_value_wstring(u'yo, wassup?')")
96u'yo, wassup?'
97   
98   test that overloading on unicode works:
99
100>>> try:
101...     if unicode: pass
102... except:
103...     print "u'yo, wassup?'"
104... else:
105...     eval("rewrap_value_string(u'yo, wassup?')")
106u'yo, wassup?'
107
108   wrap strings with embedded nulls:
109   
110>>> rewrap_value_string('yo,\0wassup?')
111'yo,\x00wassup?'
112
113>>> rewrap_value_handle(1)
1141
115>>> x = 'hi'
116>>> assert rewrap_value_handle(x) is x
117>>> assert rewrap_value_object(x) is x
118
119  Note that we can currently get a mutable pointer into an immutable
120  Python string:
121 
122>>> rewrap_value_mutable_cstring('hello, world')
123'hello, world'
124
125>>> rewrap_const_reference_bool(None)
1260
127>>> rewrap_const_reference_bool(0)
1280
129
130>>> try: rewrap_const_reference_bool('yes')
131... except TypeError: pass
132... else: print 'expected a TypeError exception'
133
134>>> rewrap_const_reference_char('x')
135'x'
136
137  Note that there's currently silent truncation of strings passed to
138  char arguments.
139
140>>> rewrap_const_reference_char('xy')
141'x'
142>>> rewrap_const_reference_signed_char(42)
14342
144>>> rewrap_const_reference_unsigned_char(42)
14542
146>>> rewrap_const_reference_int(42)
14742
148>>> rewrap_const_reference_unsigned_int(42)
14942
150>>> rewrap_const_reference_short(42)
15142
152>>> rewrap_const_reference_unsigned_short(42)
15342
154>>> rewrap_const_reference_long(42)
15542
156>>> rewrap_const_reference_unsigned_long(42)
15742
158>>> rewrap_const_reference_long_long(42)
15942L
160>>> rewrap_const_reference_unsigned_long_long(42)
16142L
162
163
164>>> assert abs(rewrap_const_reference_float(4.2) - 4.2) < .000001
165>>> rewrap_const_reference_double(4.2) - 4.2
1660.0
167>>> rewrap_const_reference_long_double(4.2) - 4.2
1680.0
169
170>>> assert abs(rewrap_const_reference_complex_float(4+.2j) - (4+.2j)) < .000001
171>>> assert abs(rewrap_const_reference_complex_double(4+.2j) - (4+.2j)) < .000001
172>>> assert abs(rewrap_const_reference_complex_long_double(4+.2j) - (4+.2j)) < .000001
173
174>>> rewrap_const_reference_cstring('hello, world')
175'hello, world'
176>>> rewrap_const_reference_string('yo, wassup?')
177'yo, wassup?'
178
179>>> rewrap_const_reference_handle(1)
1801
181>>> x = 'hi'
182>>> assert rewrap_const_reference_handle(x) is x
183>>> assert rewrap_const_reference_object(x) is x
184>>> assert rewrap_reference_object(x) is x
185
186
187Check that None <==> NULL
188
189>>> rewrap_const_reference_cstring(None)
190
191But None cannot be converted to a string object:
192
193>>> try: rewrap_const_reference_string(None)
194... except TypeError: pass
195... else: print 'expected a TypeError exception'
196
197Now check implicit conversions between floating/integer types
198
199>>> rewrap_const_reference_float(42)
20042.0
201
202>>> rewrap_const_reference_float(42L)
20342.0
204
205>>> try: rewrap_const_reference_int(42.0)
206... except TypeError: pass
207... else: print 'expected a TypeError exception'
208
209>>> rewrap_value_float(42)
21042.0
211
212>>> try: rewrap_value_int(42.0)
213... except TypeError: pass
214... else: print 'expected a TypeError exception'
215
216Check that classic classes also work
217
218>>> class FortyTwo:
219...     def __int__(self):
220...         return 42
221...     def __float__(self):
222...         return 42.0
223...     def __complex__(self):
224...         return complex(4+.2j)
225...     def __str__(self):
226...         return '42'
227
228>>> try: rewrap_const_reference_float(FortyTwo())
229... except TypeError: pass
230... else: print 'expected a TypeError exception'
231
232>>> try: rewrap_value_int(FortyTwo())
233... except TypeError: pass
234... else: print 'expected a TypeError exception'
235
236>>> try: rewrap_const_reference_string(FortyTwo())
237... except TypeError: pass
238... else: print 'expected a TypeError exception'
239
240>>> try: rewrap_value_complex_double(FortyTwo())
241... except TypeError: pass
242... else: print 'expected a TypeError exception'
243
244# show that arbitrary handle<T> instantiations can be returned
245>>> assert get_type(1) is type(1)
246
247>>> assert return_null_handle() is None
248"""
249
250def run(args = None):
251    import sys
252    import doctest
253    import builtin_converters_ext
254   
255    if 'rewrap_value_long_long' in dir(builtin_converters_ext):
256        print 'LONG_LONG supported, testing...'
257    else:
258        print 'LONG_LONG not supported, skipping those tests...'
259       
260    if args is not None:
261        sys.argv = args
262    return doctest.testmod(sys.modules.get(__name__))
263   
264if __name__ == '__main__':
265    print "running..."
266    import sys
267    status = run()[0]
268    if (status == 0): print "Done."
269    sys.exit(status)
Note: See TracBrowser for help on using the repository browser.