Spaces:
Sleeping
Sleeping
File size: 5,668 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
from sympy.parsing.sym_expr import SymPyExpression
from sympy.testing.pytest import raises
from sympy.external import import_module
lfortran = import_module('lfortran')
cin = import_module('clang.cindex', import_kwargs = {'fromlist': ['cindex']})
if lfortran and cin:
from sympy.codegen.ast import (Variable, IntBaseType, FloatBaseType, String,
Declaration, FloatType)
from sympy.core import Integer, Float
from sympy.core.symbol import Symbol
expr1 = SymPyExpression()
src = """\
integer :: a, b, c, d
real :: p, q, r, s
"""
def test_c_parse():
src1 = """\
int a, b = 4;
float c, d = 2.4;
"""
expr1.convert_to_expr(src1, 'c')
ls = expr1.return_expr()
assert ls[0] == Declaration(
Variable(
Symbol('a'),
type=IntBaseType(String('intc'))
)
)
assert ls[1] == Declaration(
Variable(
Symbol('b'),
type=IntBaseType(String('intc')),
value=Integer(4)
)
)
assert ls[2] == Declaration(
Variable(
Symbol('c'),
type=FloatType(
String('float32'),
nbits=Integer(32),
nmant=Integer(23),
nexp=Integer(8)
)
)
)
assert ls[3] == Declaration(
Variable(
Symbol('d'),
type=FloatType(
String('float32'),
nbits=Integer(32),
nmant=Integer(23),
nexp=Integer(8)
),
value=Float('2.3999999999999999', precision=53)
)
)
def test_fortran_parse():
expr = SymPyExpression(src, 'f')
ls = expr.return_expr()
assert ls[0] == Declaration(
Variable(
Symbol('a'),
type=IntBaseType(String('integer')),
value=Integer(0)
)
)
assert ls[1] == Declaration(
Variable(
Symbol('b'),
type=IntBaseType(String('integer')),
value=Integer(0)
)
)
assert ls[2] == Declaration(
Variable(
Symbol('c'),
type=IntBaseType(String('integer')),
value=Integer(0)
)
)
assert ls[3] == Declaration(
Variable(
Symbol('d'),
type=IntBaseType(String('integer')),
value=Integer(0)
)
)
assert ls[4] == Declaration(
Variable(
Symbol('p'),
type=FloatBaseType(String('real')),
value=Float('0.0', precision=53)
)
)
assert ls[5] == Declaration(
Variable(
Symbol('q'),
type=FloatBaseType(String('real')),
value=Float('0.0', precision=53)
)
)
assert ls[6] == Declaration(
Variable(
Symbol('r'),
type=FloatBaseType(String('real')),
value=Float('0.0', precision=53)
)
)
assert ls[7] == Declaration(
Variable(
Symbol('s'),
type=FloatBaseType(String('real')),
value=Float('0.0', precision=53)
)
)
def test_convert_py():
src1 = (
src +
"""\
a = b + c
s = p * q / r
"""
)
expr1.convert_to_expr(src1, 'f')
exp_py = expr1.convert_to_python()
assert exp_py == [
'a = 0',
'b = 0',
'c = 0',
'd = 0',
'p = 0.0',
'q = 0.0',
'r = 0.0',
's = 0.0',
'a = b + c',
's = p*q/r'
]
def test_convert_fort():
src1 = (
src +
"""\
a = b + c
s = p * q / r
"""
)
expr1.convert_to_expr(src1, 'f')
exp_fort = expr1.convert_to_fortran()
assert exp_fort == [
' integer*4 a',
' integer*4 b',
' integer*4 c',
' integer*4 d',
' real*8 p',
' real*8 q',
' real*8 r',
' real*8 s',
' a = b + c',
' s = p*q/r'
]
def test_convert_c():
src1 = (
src +
"""\
a = b + c
s = p * q / r
"""
)
expr1.convert_to_expr(src1, 'f')
exp_c = expr1.convert_to_c()
assert exp_c == [
'int a = 0',
'int b = 0',
'int c = 0',
'int d = 0',
'double p = 0.0',
'double q = 0.0',
'double r = 0.0',
'double s = 0.0',
'a = b + c;',
's = p*q/r;'
]
def test_exceptions():
src = 'int a;'
raises(ValueError, lambda: SymPyExpression(src))
raises(ValueError, lambda: SymPyExpression(mode = 'c'))
raises(NotImplementedError, lambda: SymPyExpression(src, mode = 'd'))
elif not lfortran and not cin:
def test_raise():
raises(ImportError, lambda: SymPyExpression('int a;', 'c'))
raises(ImportError, lambda: SymPyExpression('integer :: a', 'f'))
|