Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/f2py/tests/test_callback.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 from __future__ import division, absolute_import, print_function | |
2 | |
3 from numpy.testing import * | |
4 from numpy import array | |
5 import math | |
6 import util | |
7 import textwrap | |
8 | |
9 class TestF77Callback(util.F2PyTest): | |
10 code = """ | |
11 subroutine t(fun,a) | |
12 integer a | |
13 cf2py intent(out) a | |
14 external fun | |
15 call fun(a) | |
16 end | |
17 | |
18 subroutine func(a) | |
19 cf2py intent(in,out) a | |
20 integer a | |
21 a = a + 11 | |
22 end | |
23 | |
24 subroutine func0(a) | |
25 cf2py intent(out) a | |
26 integer a | |
27 a = 11 | |
28 end | |
29 | |
30 subroutine t2(a) | |
31 cf2py intent(callback) fun | |
32 integer a | |
33 cf2py intent(out) a | |
34 external fun | |
35 call fun(a) | |
36 end | |
37 | |
38 subroutine string_callback(callback, a) | |
39 external callback | |
40 double precision callback | |
41 double precision a | |
42 character*1 r | |
43 cf2py intent(out) a | |
44 r = 'r' | |
45 a = callback(r) | |
46 end | |
47 | |
48 """ | |
49 | |
50 @dec.slow | |
51 def test_all(self): | |
52 for name in "t,t2".split(","): | |
53 self.check_function(name) | |
54 | |
55 @dec.slow | |
56 def test_docstring(self): | |
57 expected = """ | |
58 a = t(fun,[fun_extra_args]) | |
59 | |
60 Wrapper for ``t``. | |
61 | |
62 Parameters | |
63 ---------- | |
64 fun : call-back function | |
65 | |
66 Other Parameters | |
67 ---------------- | |
68 fun_extra_args : input tuple, optional | |
69 Default: () | |
70 | |
71 Returns | |
72 ------- | |
73 a : int | |
74 | |
75 Notes | |
76 ----- | |
77 Call-back functions:: | |
78 | |
79 def fun(): return a | |
80 Return objects: | |
81 a : int | |
82 """ | |
83 assert_equal(self.module.t.__doc__, textwrap.dedent(expected).lstrip()) | |
84 | |
85 def check_function(self, name): | |
86 t = getattr(self.module, name) | |
87 r = t(lambda : 4) | |
88 assert_( r==4, repr(r)) | |
89 r = t(lambda a:5, fun_extra_args=(6,)) | |
90 assert_( r==5, repr(r)) | |
91 r = t(lambda a:a, fun_extra_args=(6,)) | |
92 assert_( r==6, repr(r)) | |
93 r = t(lambda a:5+a, fun_extra_args=(7,)) | |
94 assert_( r==12, repr(r)) | |
95 r = t(lambda a:math.degrees(a), fun_extra_args=(math.pi,)) | |
96 assert_( r==180, repr(r)) | |
97 r = t(math.degrees, fun_extra_args=(math.pi,)) | |
98 assert_( r==180, repr(r)) | |
99 | |
100 r = t(self.module.func, fun_extra_args=(6,)) | |
101 assert_( r==17, repr(r)) | |
102 r = t(self.module.func0) | |
103 assert_( r==11, repr(r)) | |
104 r = t(self.module.func0._cpointer) | |
105 assert_( r==11, repr(r)) | |
106 class A(object): | |
107 def __call__(self): | |
108 return 7 | |
109 def mth(self): | |
110 return 9 | |
111 a = A() | |
112 r = t(a) | |
113 assert_( r==7, repr(r)) | |
114 r = t(a.mth) | |
115 assert_( r==9, repr(r)) | |
116 | |
117 def test_string_callback(self): | |
118 | |
119 def callback(code): | |
120 if code == 'r': | |
121 return 0 | |
122 else: | |
123 return 1 | |
124 | |
125 f = getattr(self.module, 'string_callback') | |
126 r = f(callback) | |
127 assert_(r == 0, repr(r)) | |
128 | |
129 | |
130 if __name__ == "__main__": | |
131 import nose | |
132 nose.runmodule() |