1import os
2
3import infra.basetest
4
5
6class TestOctave(infra.basetest.BRTest):
7    # infra.basetest.BASIC_TOOLCHAIN_CONFIG cannot be used as it does
8    # not include gfortran.
9    config = \
10        """
11        BR2_aarch64=y
12        BR2_TOOLCHAIN_EXTERNAL=y
13        BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
14        BR2_LINUX_KERNEL=y
15        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
16        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="5.15.26"
17        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
18        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
19        BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
20        BR2_TARGET_ROOTFS_CPIO=y
21        BR2_TARGET_ROOTFS_CPIO_GZIP=y
22        # BR2_TARGET_ROOTFS_TAR is not set
23        BR2_PACKAGE_OCTAVE=y
24        """
25    timeout = 60
26
27    def octave_cmd(self, octave_expr):
28        return "octave --quiet --eval '{}'".format(octave_expr)
29
30    def test_run(self):
31        img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
32        kern = os.path.join(self.builddir, "images", "Image")
33        self.emulator.boot(arch="aarch64",
34                           kernel=kern,
35                           kernel_cmdline=["console=ttyAMA0"],
36                           options=["-M", "virt", "-cpu", "cortex-a57", "-m", "512M", "-initrd", img])
37        self.emulator.login()
38
39        # Check Euler identity
40        cmd = self.octave_cmd("assert (exp(i*pi)+1, 0, 1e-10)")
41        self.assertRunOk(cmd)
42
43        # Solve equation system example from Octave homepage
44        octave_expr = "b = [4; 9; 2]; "
45        octave_expr += "A = [ 3 4 5; 1 3 1; 3 5 9 ]; "
46        octave_expr += "x = A \\ b; "
47        octave_expr += "assert(x, [-1.5; 4; -1.5], 1e-10)"
48        cmd = self.octave_cmd(octave_expr)
49        self.assertRunOk(cmd)
50
51        # Check octave can fail
52        cmd = self.octave_cmd("assert(false)")
53        _, exit_code = self.emulator.run(cmd)
54        self.assertNotEqual(exit_code, 0)
55
56        # Check string output
57        string = "Hello World"
58        cmd = self.octave_cmd("printf(\"{}\\n\")".format(string))
59        output, exit_code = self.emulator.run(cmd)
60        self.assertEqual(exit_code, 0)
61        self.assertEqual(output, [string])
62
63        # Run some octave self tests
64        octave_modules = [
65            "elfun/atan2d",
66            "elfun/sind",
67            "general/gradient",
68            "general/num2str",
69            "polynomial/poly",
70            "signal/fftconv",
71            "special-matrix/magic",
72            "specfun/isprime",
73            "statistics/corr",
74            "strings/str2num"
75        ]
76
77        for mod in octave_modules:
78            cmd = self.octave_cmd('assert(test(\"{}\"),true)'.format(mod))
79            self.assertRunOk(cmd, timeout=10)
80