Show More
@@ -1,58 +1,61 | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Tests for the Cython magics extension.""" |
|
2 | """Tests for the Cython magics extension.""" | |
3 |
|
3 | |||
4 | import os |
|
4 | import os | |
5 | import nose.tools as nt |
|
5 | import nose.tools as nt | |
6 |
|
6 | |||
7 | from IPython.testing import decorators as dec |
|
7 | from IPython.testing import decorators as dec | |
8 | from IPython.utils import py3compat |
|
8 | from IPython.utils import py3compat | |
9 |
|
9 | |||
10 | code = py3compat.str_to_unicode("""def f(x): |
|
10 | code = py3compat.str_to_unicode("""def f(x): | |
11 | return 2*x |
|
11 | return 2*x | |
12 | """) |
|
12 | """) | |
13 |
|
13 | |||
14 | try: |
|
14 | try: | |
15 | import Cython |
|
15 | import Cython | |
16 | except: |
|
16 | except: | |
17 | __test__ = False |
|
17 | __test__ = False | |
18 |
|
18 | |||
19 | ip = get_ipython() |
|
19 | ip = get_ipython() | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | def setup(): |
|
22 | def setup(): | |
23 | ip.extension_manager.load_extension('cythonmagic') |
|
23 | ip.extension_manager.load_extension('cythonmagic') | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def test_cython_inline(): |
|
26 | def test_cython_inline(): | |
27 | ip.ex('a=10; b=20') |
|
27 | ip.ex('a=10; b=20') | |
28 | result = ip.run_cell_magic('cython_inline','','return a+b') |
|
28 | result = ip.run_cell_magic('cython_inline','','return a+b') | |
29 | nt.assert_equal(result, 30) |
|
29 | nt.assert_equal(result, 30) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | def test_cython_pyximport(): |
|
32 | def test_cython_pyximport(): | |
33 | module_name = '_test_cython_pyximport' |
|
33 | module_name = '_test_cython_pyximport' | |
34 | ip.run_cell_magic('cython_pyximport', module_name, code) |
|
34 | ip.run_cell_magic('cython_pyximport', module_name, code) | |
35 | ip.ex('g = f(10)') |
|
35 | ip.ex('g = f(10)') | |
36 | nt.assert_equal(ip.user_ns['g'], 20.0) |
|
36 | nt.assert_equal(ip.user_ns['g'], 20.0) | |
|
37 | ip.run_cell_magic('cython_pyximport', module_name, code) | |||
|
38 | ip.ex('h = f(-10)') | |||
|
39 | nt.assert_equal(ip.user_ns['h'], -20.0) | |||
37 | try: |
|
40 | try: | |
38 | os.remove(module_name+'.pyx') |
|
41 | os.remove(module_name+'.pyx') | |
39 | except OSError: |
|
42 | except OSError: | |
40 | pass |
|
43 | pass | |
41 |
|
44 | |||
42 |
|
45 | |||
43 | def test_cython(): |
|
46 | def test_cython(): | |
44 | ip.run_cell_magic('cython', '', code) |
|
47 | ip.run_cell_magic('cython', '', code) | |
45 | ip.ex('g = f(10)') |
|
48 | ip.ex('g = f(10)') | |
46 | nt.assert_equal(ip.user_ns['g'], 20.0) |
|
49 | nt.assert_equal(ip.user_ns['g'], 20.0) | |
47 |
|
50 | |||
48 |
|
51 | |||
49 | @dec.skip_win32 |
|
52 | @dec.skip_win32 | |
50 | def test_extlibs(): |
|
53 | def test_extlibs(): | |
51 | code = py3compat.str_to_unicode(""" |
|
54 | code = py3compat.str_to_unicode(""" | |
52 | from libc.math cimport sin |
|
55 | from libc.math cimport sin | |
53 | x = sin(0.0) |
|
56 | x = sin(0.0) | |
54 | """) |
|
57 | """) | |
55 | ip.user_ns['x'] = 1 |
|
58 | ip.user_ns['x'] = 1 | |
56 | ip.run_cell_magic('cython', '-l m', code) |
|
59 | ip.run_cell_magic('cython', '-l m', code) | |
57 | nt.assert_equal(ip.user_ns['x'], 0) |
|
60 | nt.assert_equal(ip.user_ns['x'], 0) | |
58 |
|
61 |
General Comments 0
You need to be logged in to leave comments.
Login now