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) |
---|
4 | r""" |
---|
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) |
---|
19 | True |
---|
20 | False |
---|
21 | |
---|
22 | >>> rewrap_value_bool(None) |
---|
23 | 0 |
---|
24 | >>> rewrap_value_bool(0) |
---|
25 | 0 |
---|
26 | >>> rewrap_value_bool(33) |
---|
27 | 1 |
---|
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) |
---|
37 | 42 |
---|
38 | >>> rewrap_value_unsigned_char(42) |
---|
39 | 42 |
---|
40 | >>> rewrap_value_int(42) |
---|
41 | 42 |
---|
42 | >>> rewrap_value_unsigned_int(42) |
---|
43 | 42 |
---|
44 | >>> rewrap_value_short(42) |
---|
45 | 42 |
---|
46 | >>> rewrap_value_unsigned_short(42) |
---|
47 | 42 |
---|
48 | >>> rewrap_value_long(42) |
---|
49 | 42 |
---|
50 | >>> rewrap_value_unsigned_long(42) |
---|
51 | 42 |
---|
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) |
---|
60 | 42L |
---|
61 | >>> rewrap_value_unsigned_long_long(42) |
---|
62 | 42L |
---|
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 |
---|
77 | 0.0 |
---|
78 | >>> rewrap_value_long_double(4.2) - 4.2 |
---|
79 | 0.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?')") |
---|
96 | u'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?')") |
---|
106 | u'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) |
---|
114 | 1 |
---|
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) |
---|
126 | 0 |
---|
127 | >>> rewrap_const_reference_bool(0) |
---|
128 | 0 |
---|
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) |
---|
143 | 42 |
---|
144 | >>> rewrap_const_reference_unsigned_char(42) |
---|
145 | 42 |
---|
146 | >>> rewrap_const_reference_int(42) |
---|
147 | 42 |
---|
148 | >>> rewrap_const_reference_unsigned_int(42) |
---|
149 | 42 |
---|
150 | >>> rewrap_const_reference_short(42) |
---|
151 | 42 |
---|
152 | >>> rewrap_const_reference_unsigned_short(42) |
---|
153 | 42 |
---|
154 | >>> rewrap_const_reference_long(42) |
---|
155 | 42 |
---|
156 | >>> rewrap_const_reference_unsigned_long(42) |
---|
157 | 42 |
---|
158 | >>> rewrap_const_reference_long_long(42) |
---|
159 | 42L |
---|
160 | >>> rewrap_const_reference_unsigned_long_long(42) |
---|
161 | 42L |
---|
162 | |
---|
163 | |
---|
164 | >>> assert abs(rewrap_const_reference_float(4.2) - 4.2) < .000001 |
---|
165 | >>> rewrap_const_reference_double(4.2) - 4.2 |
---|
166 | 0.0 |
---|
167 | >>> rewrap_const_reference_long_double(4.2) - 4.2 |
---|
168 | 0.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) |
---|
180 | 1 |
---|
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 | |
---|
187 | Check that None <==> NULL |
---|
188 | |
---|
189 | >>> rewrap_const_reference_cstring(None) |
---|
190 | |
---|
191 | But 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 | |
---|
197 | Now check implicit conversions between floating/integer types |
---|
198 | |
---|
199 | >>> rewrap_const_reference_float(42) |
---|
200 | 42.0 |
---|
201 | |
---|
202 | >>> rewrap_const_reference_float(42L) |
---|
203 | 42.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) |
---|
210 | 42.0 |
---|
211 | |
---|
212 | >>> try: rewrap_value_int(42.0) |
---|
213 | ... except TypeError: pass |
---|
214 | ... else: print 'expected a TypeError exception' |
---|
215 | |
---|
216 | Check 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 | |
---|
250 | def 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 | |
---|
264 | if __name__ == '__main__': |
---|
265 | print "running..." |
---|
266 | import sys |
---|
267 | status = run()[0] |
---|
268 | if (status == 0): print "Done." |
---|
269 | sys.exit(status) |
---|