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 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) |
---|
15 | 0 |
---|
16 | >>> rewrap_value_bool(0) |
---|
17 | 0 |
---|
18 | >>> rewrap_value_bool(33) |
---|
19 | 1 |
---|
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) |
---|
29 | 42 |
---|
30 | >>> rewrap_value_unsigned_char(42) |
---|
31 | 42 |
---|
32 | >>> rewrap_value_int(42) |
---|
33 | 42 |
---|
34 | >>> rewrap_value_unsigned_int(42) |
---|
35 | 42 |
---|
36 | >>> rewrap_value_short(42) |
---|
37 | 42 |
---|
38 | >>> rewrap_value_unsigned_short(42) |
---|
39 | 42 |
---|
40 | >>> rewrap_value_long(42) |
---|
41 | 42 |
---|
42 | >>> rewrap_value_unsigned_long(42) |
---|
43 | 42 |
---|
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) |
---|
52 | 42L |
---|
53 | >>> rewrap_value_unsigned_long_long(42) |
---|
54 | 42L |
---|
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 |
---|
69 | 0.0 |
---|
70 | >>> rewrap_value_long_double(4.2) - 4.2 |
---|
71 | 0.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?')") |
---|
88 | u'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?')") |
---|
98 | u'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) |
---|
106 | 1 |
---|
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) |
---|
118 | 0 |
---|
119 | >>> rewrap_const_reference_bool(0) |
---|
120 | 0 |
---|
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) |
---|
135 | 42 |
---|
136 | >>> rewrap_const_reference_unsigned_char(42) |
---|
137 | 42 |
---|
138 | >>> rewrap_const_reference_int(42) |
---|
139 | 42 |
---|
140 | >>> rewrap_const_reference_unsigned_int(42) |
---|
141 | 42 |
---|
142 | >>> rewrap_const_reference_short(42) |
---|
143 | 42 |
---|
144 | >>> rewrap_const_reference_unsigned_short(42) |
---|
145 | 42 |
---|
146 | >>> rewrap_const_reference_long(42) |
---|
147 | 42 |
---|
148 | >>> rewrap_const_reference_unsigned_long(42) |
---|
149 | 42 |
---|
150 | >>> rewrap_const_reference_long_long(42) |
---|
151 | 42L |
---|
152 | >>> rewrap_const_reference_unsigned_long_long(42) |
---|
153 | 42L |
---|
154 | |
---|
155 | |
---|
156 | >>> assert abs(rewrap_const_reference_float(4.2) - 4.2) < .000001 |
---|
157 | >>> rewrap_const_reference_double(4.2) - 4.2 |
---|
158 | 0.0 |
---|
159 | >>> rewrap_const_reference_long_double(4.2) - 4.2 |
---|
160 | 0.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) |
---|
172 | 1 |
---|
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 | |
---|
179 | Check that None <==> NULL |
---|
180 | |
---|
181 | >>> rewrap_const_reference_cstring(None) |
---|
182 | |
---|
183 | But 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 | |
---|
189 | Now check implicit conversions between floating/integer types |
---|
190 | |
---|
191 | >>> rewrap_const_reference_float(42) |
---|
192 | 42.0 |
---|
193 | |
---|
194 | >>> rewrap_const_reference_float(42L) |
---|
195 | 42.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) |
---|
202 | 42.0 |
---|
203 | |
---|
204 | >>> try: rewrap_value_int(42.0) |
---|
205 | ... except TypeError: pass |
---|
206 | ... else: print 'expected a TypeError exception' |
---|
207 | |
---|
208 | Check 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 | |
---|
242 | def 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 | |
---|
256 | if __name__ == '__main__': |
---|
257 | print "running..." |
---|
258 | import sys |
---|
259 | status = run()[0] |
---|
260 | if (status == 0): print "Done." |
---|
261 | sys.exit(status) |
---|