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 211 212
|
#!/usr/bin/env -S python3 -m pytest -vv
import math
import numpy
import pytest
import os
import tempfile
import warnings
if 'ADTTMP' in os.environ:
os.chdir(os.environ['ADTTMP'])
import GDL
class GDLFile (object):
'''Temporary GDL test source file. The file name is taken from the
procedure/function definition which has to be in the first line.
This class is to be used in a `with` statement, which returns the function
resp. procedure name. After leaving the with block, the file is removed.
Example:
code = """
function myfun
return, 1
end
"""
with GDLFile(code) as name:
assert GDL.function(name) == 1
'''
def __init__(self, code):
defline = code.strip().split('\n')[0].split()
if len(defline) > 0 and defline[0].lower() in ('pro', 'function'):
self.name = defline[1].lower().replace(',','')
self.fname = self.name + '.pro'
else:
self.name = None
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.fname = tempfile.mkstemp()[1]
self.code = code
def __enter__(self):
fp = open(self.fname, 'w')
fp.write(self.code)
fp.close()
return self.name or self.fname
def __exit__(self, t, value, trace):
os.unlink(self.fname)
def test_function_internal():
'''Call the internal sin() function'''
assert GDL.function('sin', 1.0) == math.sin(1.0)
def test_function_user():
'''Call the sinus function via a user defined function'''
code = '''
function MYSIN, arg
return, sin(arg)
end
'''
with GDLFile(code) as name:
assert GDL.function(name, 1.0) == math.sin(1.0)
@pytest.mark.parametrize('arg', [
'Hello, world', '', '\n',
u'Hello, world',
-1.2, 1e-39, 0.0, 0.05, 1.0, 1e128, float('inf'), float('-inf'),
-1, 0, 1, 1000, 2**31-1, -2**31,
# 2**45-1, # GDL returns int
# complex(1.,1.), # Aborts execution
# complex(0.,0.), # Aborts execution
# numpy.arange(0, 259, dtype=int) # GDL Error: unknown return array type"
numpy.arange(0, 259, dtype=numpy.uint8),
numpy.arange(-255, 259, dtype=numpy.int16),
# numpy.arange(-255, 255, dtype=numpy.int8), # GDL Error: unknown return array type
# numpy.arange(0, 259, dtype=numpy.uint16), # GDL Error: unknown return array type
numpy.arange(-255, 259, dtype=numpy.int32),
numpy.arange(0, 259, dtype=numpy.uint32),
numpy.arange(-1, 1., 0.1, dtype=float),
numpy.arange(-1, 1., 0.1, dtype=numpy.float32),
numpy.arange(-1, 1., 0.1, dtype=numpy.float64),
numpy.arange(-1, 1., 0.1, dtype=numpy.complex64),
numpy.arange(-1, 1., 0.1, dtype=numpy.complex128),
# [1,2,3,4,5], # GDL Error: Cannot convert python scalar"
# (1,2,3,4,5), # Returns only the first number
# {'1':2}, # GDL Error: Cannot convert python scalar
])
def test_function_arg_pass_return(arg):
'''Call a function that just returns its argument, with different data types'''
code = '''
function ARG_PASS_RETURN, arg
return, arg
end
'''
with GDLFile(code) as name:
assert numpy.all(GDL.function(name, arg) == arg)
def test_pro_internal():
'''Call the internal setenv procedure'''
s = 'blabla'
GDL.pro('setenv', 'T1=' + s)
def test_pro_user():
'''Call the setenv procedure via a user defined procedure'''
code = '''
pro USER_SETENV, key, val
SETENV, key + '=' + val
print, GETENV(key)
end
'''
s = 'blabla'
with GDLFile(code) as name:
GDL.pro(name, 'T1', s)
@pytest.mark.parametrize('arg', [
'Hello, world', '',
u'Hello, world',
-1.2, 1e-39, 0.0, 0.05, 1.0, 1e128, float('inf'), float('-inf'),
-1, 0, 1, 1000, 2**31-1, -2**31,
# 2**45-1, # GDL returns int
# complex(1.,1.), # Aborts execution
])
def test_pro_arg_pass(arg):
'''Call a user defined procedure that stores the value for different
data types in a file'''
code = '''
PRO pro_arg_pass, fname, arg
openw, 5, fname
printf, 5, arg
close, 5
end
'''
with GDLFile(code) as name:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fname = tempfile.mkstemp()[1]
GDL.pro(name, fname, arg)
ret = open(fname).read().strip()
os.unlink(fname)
assert arg == arg.__class__(ret)
@pytest.mark.xfail
@pytest.mark.parametrize('dtype', [
numpy.uint8, numpy.int16, numpy.int32, numpy.float32, numpy.float64,
numpy.complex64, numpy.complex128, numpy.uint32, float])
def test_pro_setvalue(dtype):
'''Call a user defined procedure that replaces the values of an array with
its sinus.
Note that GDL does not support changes in the arguments, so this test is
expected to fail'''
code = '''pro SETVALUE, arg
arg[*] = sin(arg)
end
'''
with GDLFile(code) as name:
arg = numpy.arange(-10, 10., 1.1).astype(dtype)
ret = numpy.copy(arg)
GDL.pro(name, ret)
for a, r in zip(numpy.sin(arg), ret):
assert a == r
def test_script():
'''Call a simple script that writes a short file'''
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fname = tempfile.mkstemp()[1]
scriptname = tempfile.mkstemp()[1]
arg = 'Hello, world!'
code = '''openw, 5, '{0}'
printf, 5, '{1}'
close, 5
'''
with GDLFile(code.format(fname, arg)) as script:
GDL.script(script)
ret = open(fname).read().strip()
os.unlink(fname)
assert arg == ret
@pytest.mark.xfail(reason='Does not raise a GDL.error')
def test_invalid_code():
'''Call a function with some invalid GDL code'''
code = '''
PRO invalid_code
this_is_invalid
end
'''
with GDLFile(code) as name:
with pytest.raises(GDL.error):
GDL.pro(name,ret)
|