2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-18 07:56:30 +08:00

test: added test case for nested exceptions and try

This commit is contained in:
pca006132 2022-01-23 21:20:20 +08:00 committed by Sébastien Bourdeauducq
parent ba34700798
commit 536b3e0c26

View File

@ -125,6 +125,57 @@ class _Pulses(EnvExperiment):
class _MyException(Exception): class _MyException(Exception):
pass pass
class _NestedFinally(EnvExperiment):
def build(self, trace):
self.setattr_device("core")
self.trace = trace
def _trace(self, i):
self.trace.append(i)
@kernel
def run(self):
try:
try:
raise ValueError
finally:
try:
raise IndexError()
except ValueError:
self._trace(0)
except:
self._trace(1)
finally:
self._trace(2)
class _NestedExceptions(EnvExperiment):
def build(self, trace):
self.setattr_device("core")
self.trace = trace
def _trace(self, i):
self.trace.append(i)
@kernel
def run(self):
try:
try:
raise ValueError
except _MyException:
self._trace(0)
raise
finally:
try:
raise IndexError()
except ValueError:
self._trace(1)
raise
except IndexError:
self._trace(2)
except:
self._trace(3)
finally:
self._trace(4)
class _Exceptions(EnvExperiment): class _Exceptions(EnvExperiment):
def build(self, trace): def build(self, trace):
@ -253,6 +304,18 @@ class HostVsDeviceCase(ExperimentCase):
_run_on_host(_Exceptions, trace=t_host) _run_on_host(_Exceptions, trace=t_host)
self.assertEqual(t_device, t_host) self.assertEqual(t_device, t_host)
def test_nested_finally(self):
t_device, t_host = [], []
self.execute(_NestedFinally, trace=t_device)
_run_on_host(_NestedFinally, trace=t_host)
self.assertEqual(t_device, t_host)
def test_nested_exceptions(self):
t_device, t_host = [], []
self.execute(_NestedExceptions, trace=t_device)
_run_on_host(_NestedExceptions, trace=t_host)
self.assertEqual(t_device, t_host)
def test_rpc_exceptions(self): def test_rpc_exceptions(self):
for f in self.execute, _run_on_host: for f in self.execute, _run_on_host:
with self.assertRaises(_MyException): with self.assertRaises(_MyException):