From cbd903a9dcb4ef570832256460f1001e51624a30 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 28 Aug 2015 05:24:57 -0500 Subject: [PATCH] compiler.embedding: add tests for quote serialization. --- artiq/test/coredevice/embedding.py | 41 ++++++++++++++++++++++++++++++ artiq/test/hardware_testbench.py | 18 ++++++++----- 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 artiq/test/coredevice/embedding.py diff --git a/artiq/test/coredevice/embedding.py b/artiq/test/coredevice/embedding.py new file mode 100644 index 000000000..c5ce1a2d4 --- /dev/null +++ b/artiq/test/coredevice/embedding.py @@ -0,0 +1,41 @@ +from artiq.language import * +from artiq.test.hardware_testbench import ExperimentCase + +class Roundtrip(EnvExperiment): + def build(self): + self.attr_device("core") + + @kernel + def roundtrip(self, obj, fn): + fn(obj) + +class RoundtripTest(ExperimentCase): + def assertRoundtrip(self, obj): + exp = self.create(Roundtrip) + def callback(objcopy) -> TNone: + self.assertEqual(obj, objcopy) + exp.roundtrip(obj, callback) + + def test_None(self): + self.assertRoundtrip(None) + + def test_bool(self): + self.assertRoundtrip(True) + self.assertRoundtrip(False) + + def test_int(self): + self.assertRoundtrip(42) + self.assertRoundtrip(int(42, width=64)) + + def test_float(self): + self.assertRoundtrip(42.0) + + def test_str(self): + self.assertRoundtrip("foo") + + def test_list(self): + self.assertRoundtrip([10]) + + def test_object(self): + obj = object() + self.assertRoundtrip(obj) diff --git a/artiq/test/hardware_testbench.py b/artiq/test/hardware_testbench.py index 1734852cc..c135bb696 100644 --- a/artiq/test/hardware_testbench.py +++ b/artiq/test/hardware_testbench.py @@ -37,7 +37,16 @@ class ExperimentCase(unittest.TestCase): self.pdb = FlatFileDB(os.path.join(artiq_root, "pdb.pyon")) self.rdb = ResultDB() - def execute(self, cls, **kwargs): + def create(self, cls, **kwargs): + try: + exp = cls(self.dmgr, self.pdb, self.rdb, **kwargs) + exp.prepare() + return exp + except KeyError as e: + # skip if ddb does not match requirements + raise unittest.SkipTest(*e.args) + + def execute(self, cls, *args, **kwargs): expid = { "file": sys.modules[cls.__module__].__file__, "class_name": cls.__name__, @@ -45,12 +54,7 @@ class ExperimentCase(unittest.TestCase): } self.dmgr.virtual_devices["scheduler"].expid = expid try: - try: - exp = cls(self.dmgr, self.pdb, self.rdb, **kwargs) - except KeyError as e: - # skip if ddb does not match requirements - raise unittest.SkipTest(*e.args) - exp.prepare() + exp = self.create(cls, **kwargs) exp.run() exp.analyze() return exp