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