From 9861fd06ac55370b21f1157c74bbb9ded2c1a25f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 22 Mar 2021 21:09:33 -0500 Subject: [PATCH 1/3] add some tests --- tests/testpioasm.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/testpioasm.py diff --git a/tests/testpioasm.py b/tests/testpioasm.py new file mode 100644 index 0000000..1e90923 --- /dev/null +++ b/tests/testpioasm.py @@ -0,0 +1,69 @@ +# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +# pylint: disable=missing-module-docstring,invalid-name,missing-function-docstring,missing-class-docstring + +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).absolute().parent.parent)) + +import adafruit_pioasm # pylint: disable=wrong-import-position + + +def nice_opcode(o): + o = f"{o:016b}" + return o[:3] + "_" + o[3:8] + "_" + o[8:] + + +class TestNop(unittest.TestCase): + def assertAssemblesTo(self, source, expected): + actual = adafruit_pioasm.assemble(source) + expected_bin = [nice_opcode(x) for x in expected] + actual_bin = [nice_opcode(x) for x in actual] + self.assertEqual( + expected_bin, + actual_bin, + f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}", + ) + + def assertAssemblyFails(self, source): + self.assertRaises(RuntimeError, adafruit_pioasm.assemble, source) + + def testNonsense(self): + self.assertAssemblyFails("nope") + + def testNop(self): + self.assertAssemblesTo("nop", [0b101_00000_010_00_010]) + self.assertAssemblesTo( + "nop\nnop", [0b101_00000_010_00_010, 0b101_00000_010_00_010] + ) + self.assertAssemblesTo("nop [1]", [0b101_00001_010_00_010]) + self.assertAssemblesTo(".side_set 1\nnop side 1", [0b101_10000_010_00_010]) + self.assertAssemblesTo(".side_set 1\nnop side 1 [1]", [0b101_10001_010_00_010]) + + def testJmp(self): + self.assertAssemblesTo("l:\njmp l", [0b000_00000_000_00000]) + self.assertAssemblesTo("l:\njmp 7", [0b000_00000_000_00111]) + self.assertAssemblesTo("jmp l\nl:", [0b000_00000_000_00001]) + self.assertAssemblesTo("jmp !x, l\nl:", [0b000_00000_001_00001]) + self.assertAssemblesTo("jmp x--, l\nl:", [0b000_00000_010_00001]) + self.assertAssemblesTo("jmp !y, l\nl:", [0b000_00000_011_00001]) + self.assertAssemblesTo("jmp y--, l\nl:", [0b000_00000_100_00001]) + self.assertAssemblesTo("jmp x!=y, l\nl:", [0b000_00000_101_00001]) + self.assertAssemblesTo("jmp pin, l\nl:", [0b000_00000_110_00001]) + self.assertAssemblesTo("jmp !osre, l\nl:", [0b000_00000_111_00001]) + + def testWait(self): + self.assertAssemblesTo("wait 0 gpio 0", [0b001_00000_0_00_00000]) + self.assertAssemblesTo("wait 0 gpio 1", [0b001_00000_0_00_00001]) + self.assertAssemblesTo("wait 1 gpio 2", [0b001_00000_1_00_00010]) + self.assertAssemblesTo("wait 0 pin 0", [0b001_00000_0_01_00000]) + self.assertAssemblesTo("wait 0 pin 1", [0b001_00000_0_01_00001]) + self.assertAssemblesTo("wait 1 pin 2", [0b001_00000_1_01_00010]) + self.assertAssemblesTo("wait 0 irq 0", [0b001_00000_0_10_00000]) + self.assertAssemblesTo("wait 0 irq 0 rel", [0b001_00000_0_10_10000]) + self.assertAssemblesTo("wait 1 irq 0", [0b001_00000_1_10_00000]) + self.assertAssemblesTo("wait 0 irq 1 rel", [0b001_00000_0_10_10001]) From 55bee3f66cd6e3e84941e5a69e492d928be63ee0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 22 Mar 2021 21:09:54 -0500 Subject: [PATCH 2/3] fix an error while raising an invalid instruction error --- adafruit_pioasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index e225a7d..3bae992 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -170,7 +170,7 @@ def assemble(text_program): raise RuntimeError("Set value out of range") assembled[-1] |= value else: - raise RuntimeError("Unknown instruction:" + instruction) + raise RuntimeError("Unknown instruction:" + instruction[0]) assembled[-1] |= delay << 8 # print(hex(assembled[-1])) From bf9ae4ee28e8a832892325a71048433d92c2eecf Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 22 Mar 2021 21:11:11 -0500 Subject: [PATCH 3/3] run tests during ci --- .github/workflows/build.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3baf502..cbac825 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,6 +50,10 @@ jobs: - name: Pre-commit hooks run: | pre-commit run --all-files + - name: Run tests + run: | + cd tests/ && python -m unittest discover + cd .. - name: Build assets run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location . - name: Archive bundles