Prechádzať zdrojové kódy

support/testing: add micropython runtime test

Signed-off-by: Julien Olivain <ju.o@free.fr>
[me: rework the test mostly for eye-candyness]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
Julien Olivain 1 rok pred
rodič
commit
0c6ffa5513

+ 2 - 0
DEVELOPERS

@@ -1791,6 +1791,8 @@ F:	support/testing/tests/package/test_lzip.py
 F:	support/testing/tests/package/test_lsof.py
 F:	support/testing/tests/package/test_lz4.py
 F:	support/testing/tests/package/test_lzop.py
+F:	support/testing/tests/package/test_micropython.py
+F:	support/testing/tests/package/test_micropython/
 F:	support/testing/tests/package/test_mtools.py
 F:	support/testing/tests/package/test_mtr.py
 F:	support/testing/tests/package/test_ncdu.py

+ 68 - 0
support/testing/tests/package/test_micropython.py

@@ -0,0 +1,68 @@
+import os
+
+import infra.basetest
+
+
+class TestMicroPython(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        f"""
+        BR2_PACKAGE_MICROPYTHON=y
+        BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_micropython/rootfs-overlay")}"
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    def run_upy_code(self, python_code, opts=""):
+        cmd = f'micropython {opts} -c "{python_code}"'
+        output, ret = self.emulator.run(cmd)
+        self.assertEqual(ret, 0, f"could not run '{cmd}', returnd {ret}: '{output}'")
+        return output
+
+    def test_run(self):
+        cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
+        self.emulator.boot(arch="armv5",
+                           kernel="builtin",
+                           options=["-initrd", cpio_file])
+        self.emulator.login()
+
+        # The micropython binary can execute.
+        self.assertRunOk("micropython -h")
+
+        # Query interpreter version and implementation.
+        py_code = "import sys ; "
+        py_code += "print('Version:', sys.version) ; "
+        py_code += "print('Implementation:', sys.implementation)"
+        self.run_upy_code(py_code)
+
+        # Check implementation is 'micropython'.
+        py_code = "import sys ; print(sys.implementation.name)"
+        output = self.run_upy_code(py_code)
+        self.assertEqual(output[0], "micropython")
+
+        # Check micropython optimization are correctly reported.
+        py_code = "import micropython ; print(micropython.opt_level())"
+        for opt_level in range(4):
+            output = self.run_upy_code(py_code, f"-O{opt_level}")
+            self.assertEqual(
+                int(output[0]),
+                opt_level,
+                f"Running '{py_code}' at -O{opt_level} returned '{output}'"
+            )
+
+        # Check micropython can return a non-zero exit code.
+        expected_code = 123
+        py_code = "import sys ; "
+        py_code += f"sys.exit({expected_code})"
+        cmd = f'micropython -c "{py_code}"'
+        _, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, expected_code)
+
+        # Check micropython computes correctly.
+        input_value = 1234
+        expected_output = str(sum(range(input_value)))
+        py_code = f"print(sum(range(({input_value}))))"
+        output = self.run_upy_code(py_code)
+        self.assertEqual(output[0], expected_output)
+
+        # Finally, Check a small script can execute.
+        self.assertRunOk("/root/mandel.py", timeout=10)

+ 25 - 0
support/testing/tests/package/test_micropython/rootfs-overlay/root/mandel.py

@@ -0,0 +1,25 @@
+#! /usr/bin/env micropython
+
+from micropython import mem_info
+
+POINTS = list(",.:-;!/>)|&IH%*Z")
+
+
+def mandel():
+    for y in range(-15, 16):
+        for x in range(1, 85):
+            i = 0
+            r = 0
+            for k in range(112):
+                j = (r*r) - (i*i) - 2 + (x/25)
+                i = 2 * r * i + (y/10)
+                if j*j + i*i >= 11:
+                    break
+                r = j
+            print(POINTS[k & 0xF], end='')
+        print()
+
+
+if __name__ == '__main__':
+    mandel()
+    mem_info()