Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/test_builtin_converters.py @ 12

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

added boost

File size: 6.4 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 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>>> rewrap_value_bool(None)
150
16>>> rewrap_value_bool(0)
170
18>>> rewrap_value_bool(33)
191
20>>> rewrap_value_char('x')
21'x'
22
23  Note that there's currently silent truncation of strings passed to
24  char arguments.
25
26>>> rewrap_value_char('xy')
27'x'
28>>> rewrap_value_signed_char(42)
2942
30>>> rewrap_value_unsigned_char(42)
3142
32>>> rewrap_value_int(42)
3342
34>>> rewrap_value_unsigned_int(42)
3542
36>>> rewrap_value_short(42)
3742
38>>> rewrap_value_unsigned_short(42)
3942
40>>> rewrap_value_long(42)
4142
42>>> rewrap_value_unsigned_long(42)
4342
44
45    test unsigned long values which don't fit in a signed long.
46    strip any 'L' characters in case the platform has > 32 bit longs
47       
48>>> hex(rewrap_value_unsigned_long(0x80000001L)).replace('L','')
49'0x80000001'
50
51>>> rewrap_value_long_long(42)
5242L
53>>> rewrap_value_unsigned_long_long(42)
5442L
55
56   show that we have range checking.
57 
58>>> try: rewrap_value_unsigned_short(-42)
59... except OverflowError: pass
60... else: print 'expected an OverflowError!'
61
62>>> try: rewrap_value_int(sys.maxint * 2)
63... except OverflowError: pass
64... else: print 'expected an OverflowError!'
65
66
67>>> assert abs(rewrap_value_float(4.2) - 4.2) < .000001
68>>> rewrap_value_double(4.2) - 4.2
690.0
70>>> rewrap_value_long_double(4.2) - 4.2
710.0
72
73>>> assert abs(rewrap_value_complex_float(4+.2j) - (4+.2j)) < .000001
74>>> assert abs(rewrap_value_complex_double(4+.2j) - (4+.2j)) < .000001
75>>> assert abs(rewrap_value_complex_long_double(4+.2j) - (4+.2j)) < .000001
76
77>>> rewrap_value_cstring('hello, world')
78'hello, world'
79>>> rewrap_value_string('yo, wassup?')
80'yo, wassup?'
81
82>>> try:
83...     if unicode: pass
84... except:
85...     print "u'yo, wassup?'"
86... else:
87...     eval("rewrap_value_wstring(u'yo, wassup?')")
88u'yo, wassup?'
89   
90   test that overloading on unicode works:
91
92>>> try:
93...     if unicode: pass
94... except:
95...     print "u'yo, wassup?'"
96... else:
97...     eval("rewrap_value_string(u'yo, wassup?')")
98u'yo, wassup?'
99
100   wrap strings with embedded nulls:
101   
102>>> rewrap_value_string('yo,\0wassup?')
103'yo,\x00wassup?'
104
105>>> rewrap_value_handle(1)
1061
107>>> x = 'hi'
108>>> assert rewrap_value_handle(x) is x
109>>> assert rewrap_value_object(x) is x
110
111  Note that we can currently get a mutable pointer into an immutable
112  Python string:
113 
114>>> rewrap_value_mutable_cstring('hello, world')
115'hello, world'
116
117>>> rewrap_const_reference_bool(None)
1180
119>>> rewrap_const_reference_bool(0)
1200
121
122>>> try: rewrap_const_reference_bool('yes')
123... except TypeError: pass
124... else: print 'expected a TypeError exception'
125
126>>> rewrap_const_reference_char('x')
127'x'
128
129  Note that there's currently silent truncation of strings passed to
130  char arguments.
131
132>>> rewrap_const_reference_char('xy')
133'x'
134>>> rewrap_const_reference_signed_char(42)
13542
136>>> rewrap_const_reference_unsigned_char(42)
13742
138>>> rewrap_const_reference_int(42)
13942
140>>> rewrap_const_reference_unsigned_int(42)
14142
142>>> rewrap_const_reference_short(42)
14342
144>>> rewrap_const_reference_unsigned_short(42)
14542
146>>> rewrap_const_reference_long(42)
14742
148>>> rewrap_const_reference_unsigned_long(42)
14942
150>>> rewrap_const_reference_long_long(42)
15142L
152>>> rewrap_const_reference_unsigned_long_long(42)
15342L
154
155
156>>> assert abs(rewrap_const_reference_float(4.2) - 4.2) < .000001
157>>> rewrap_const_reference_double(4.2) - 4.2
1580.0
159>>> rewrap_const_reference_long_double(4.2) - 4.2
1600.0
161
162>>> assert abs(rewrap_const_reference_complex_float(4+.2j) - (4+.2j)) < .000001
163>>> assert abs(rewrap_const_reference_complex_double(4+.2j) - (4+.2j)) < .000001
164>>> assert abs(rewrap_const_reference_complex_long_double(4+.2j) - (4+.2j)) < .000001
165
166>>> rewrap_const_reference_cstring('hello, world')
167'hello, world'
168>>> rewrap_const_reference_string('yo, wassup?')
169'yo, wassup?'
170
171>>> rewrap_const_reference_handle(1)
1721
173>>> x = 'hi'
174>>> assert rewrap_const_reference_handle(x) is x
175>>> assert rewrap_const_reference_object(x) is x
176>>> assert rewrap_reference_object(x) is x
177
178
179Check that None <==> NULL
180
181>>> rewrap_const_reference_cstring(None)
182
183But None cannot be converted to a string object:
184
185>>> try: rewrap_const_reference_string(None)
186... except TypeError: pass
187... else: print 'expected a TypeError exception'
188
189Now check implicit conversions between floating/integer types
190
191>>> rewrap_const_reference_float(42)
19242.0
193
194>>> rewrap_const_reference_float(42L)
19542.0
196
197>>> try: rewrap_const_reference_int(42.0)
198... except TypeError: pass
199... else: print 'expected a TypeError exception'
200
201>>> rewrap_value_float(42)
20242.0
203
204>>> try: rewrap_value_int(42.0)
205... except TypeError: pass
206... else: print 'expected a TypeError exception'
207
208Check that classic classes also work
209
210>>> class FortyTwo:
211...     def __int__(self):
212...         return 42
213...     def __float__(self):
214...         return 42.0
215...     def __complex__(self):
216...         return complex(4+.2j)
217...     def __str__(self):
218...         return '42'
219
220>>> try: rewrap_const_reference_float(FortyTwo())
221... except TypeError: pass
222... else: print 'expected a TypeError exception'
223
224>>> try: rewrap_value_int(FortyTwo())
225... except TypeError: pass
226... else: print 'expected a TypeError exception'
227
228>>> try: rewrap_const_reference_string(FortyTwo())
229... except TypeError: pass
230... else: print 'expected a TypeError exception'
231
232>>> try: rewrap_value_complex_double(FortyTwo())
233... except TypeError: pass
234... else: print 'expected a TypeError exception'
235
236# show that arbitrary handle<T> instantiations can be returned
237>>> assert get_type(1) is type(1)
238
239>>> assert return_null_handle() is None
240"""
241
242def run(args = None):
243    import sys
244    import doctest
245    import builtin_converters
246   
247    if 'rewrap_value_long_long' in dir(builtin_converters):
248        print 'LONG_LONG supported, testing...'
249    else:
250        print 'LONG_LONG not supported, skipping those tests...'
251       
252    if args is not None:
253        sys.argv = args
254    return doctest.testmod(sys.modules.get(__name__))
255   
256if __name__ == '__main__':
257    print "running..."
258    import sys
259    status = run()[0]
260    if (status == 0): print "Done."
261    sys.exit(status)
Note: See TracBrowser for help on using the repository browser.