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 | ''' |
---|
5 | >>> from callbacks_ext import * |
---|
6 | |
---|
7 | >>> def double(x): |
---|
8 | ... return x + x |
---|
9 | ... |
---|
10 | >>> apply_int_int(double, 42) |
---|
11 | 84 |
---|
12 | >>> apply_void_int(double, 42) |
---|
13 | |
---|
14 | >>> def identity(x): |
---|
15 | ... return x |
---|
16 | |
---|
17 | Once we have array conversion support, this test will fail. Er, |
---|
18 | succeed<wink>: |
---|
19 | |
---|
20 | >>> try: apply_to_string_literal(identity) |
---|
21 | ... except ReferenceError: pass # expected |
---|
22 | ... else: print 'expected an exception!' |
---|
23 | |
---|
24 | >>> try: apply_X_ref_handle(lambda ignored:X(42), None) |
---|
25 | ... except ReferenceError: pass # expected |
---|
26 | ... else: print 'expected an exception!' |
---|
27 | |
---|
28 | >>> x = X(42) |
---|
29 | >>> x.y = X(7) |
---|
30 | >>> apply_X_ref_handle(lambda z:z.y, x).value() |
---|
31 | 7 |
---|
32 | |
---|
33 | >>> x = apply_X_X(identity, X(42)) |
---|
34 | >>> x.value() |
---|
35 | 42 |
---|
36 | >>> x_count() |
---|
37 | 1 |
---|
38 | >>> del x |
---|
39 | >>> x_count() |
---|
40 | 0 |
---|
41 | |
---|
42 | >>> def increment(x): |
---|
43 | ... x.set(x.value() + 1) |
---|
44 | ... |
---|
45 | >>> x = X(42) |
---|
46 | >>> apply_void_X_ref(increment, x) |
---|
47 | >>> x.value() |
---|
48 | 43 |
---|
49 | |
---|
50 | >>> apply_void_X_cref(increment, x) |
---|
51 | >>> x.value() # const-ness is not respected, sorry! |
---|
52 | 44 |
---|
53 | |
---|
54 | >>> last_x = 1 |
---|
55 | >>> def decrement(x): |
---|
56 | ... global last_x |
---|
57 | ... last_x = x |
---|
58 | ... if x is not None: |
---|
59 | ... x.set(x.value() - 1) |
---|
60 | |
---|
61 | >>> apply_void_X_ptr(decrement, x) |
---|
62 | >>> x.value() |
---|
63 | 43 |
---|
64 | >>> last_x.value() |
---|
65 | 43 |
---|
66 | >>> increment(last_x) |
---|
67 | >>> x.value() |
---|
68 | 44 |
---|
69 | >>> last_x.value() |
---|
70 | 44 |
---|
71 | |
---|
72 | >>> apply_void_X_ptr(decrement, None) |
---|
73 | >>> assert last_x is None |
---|
74 | >>> x.value() |
---|
75 | 44 |
---|
76 | |
---|
77 | >>> last_x = 1 |
---|
78 | >>> apply_void_X_deep_ptr(decrement, None) |
---|
79 | >>> assert last_x is None |
---|
80 | >>> x.value() |
---|
81 | 44 |
---|
82 | |
---|
83 | >>> apply_void_X_deep_ptr(decrement, x) |
---|
84 | >>> x.value() |
---|
85 | 44 |
---|
86 | >>> last_x.value() |
---|
87 | 43 |
---|
88 | |
---|
89 | >>> y = apply_X_ref_handle(identity, x) |
---|
90 | >>> assert y.value() == x.value() |
---|
91 | >>> increment(x) |
---|
92 | >>> assert y.value() == x.value() |
---|
93 | |
---|
94 | >>> y = apply_X_ptr_handle_cref(identity, x) |
---|
95 | >>> assert y.value() == x.value() |
---|
96 | >>> increment(x) |
---|
97 | >>> assert y.value() == x.value() |
---|
98 | |
---|
99 | >>> y = apply_X_ptr_handle_cref(identity, None) |
---|
100 | >>> y |
---|
101 | |
---|
102 | >>> def new_x(ignored): |
---|
103 | ... return X(666) |
---|
104 | ... |
---|
105 | >>> try: apply_X_ref_handle(new_x, 1) |
---|
106 | ... except ReferenceError: pass |
---|
107 | ... else: print 'no error' |
---|
108 | |
---|
109 | >>> try: apply_X_ptr_handle_cref(new_x, 1) |
---|
110 | ... except ReferenceError: pass |
---|
111 | ... else: print 'no error' |
---|
112 | |
---|
113 | >>> try: apply_cstring_cstring(identity, 'hello') |
---|
114 | ... except ReferenceError: pass |
---|
115 | ... else: print 'no error' |
---|
116 | |
---|
117 | >>> apply_char_char(identity, 'x') |
---|
118 | 'x' |
---|
119 | |
---|
120 | >>> apply_cstring_pyobject(identity, 'hello') |
---|
121 | 'hello' |
---|
122 | |
---|
123 | >>> apply_cstring_pyobject(identity, None) |
---|
124 | |
---|
125 | |
---|
126 | >>> apply_char_char(identity, 'x') |
---|
127 | 'x' |
---|
128 | |
---|
129 | >>> assert apply_to_own_type(identity) is type(identity) |
---|
130 | |
---|
131 | >>> assert apply_object_object(identity, identity) is identity |
---|
132 | ''' |
---|
133 | |
---|
134 | def run(args = None): |
---|
135 | import sys |
---|
136 | import doctest |
---|
137 | |
---|
138 | if args is not None: |
---|
139 | sys.argv = args |
---|
140 | return doctest.testmod(sys.modules.get(__name__)) |
---|
141 | |
---|
142 | if __name__ == '__main__': |
---|
143 | print "running..." |
---|
144 | import sys |
---|
145 | status = run()[0] |
---|
146 | if (status == 0): print "Done." |
---|
147 | sys.exit(status) |
---|