diff mbox series

[1/1] support/testing: new bc runtime test

Message ID 20240116182026.284510-1-ju.o@free.fr
State Accepted
Headers show
Series [1/1] support/testing: new bc runtime test | expand

Commit Message

Julien Olivain Jan. 16, 2024, 6:20 p.m. UTC
Signed-off-by: Julien Olivain <ju.o@free.fr>
---
Tested on branch master at commit e07402a with commands:

    make check-package
    ...
    0 warnings generated

    support/testing/run-tests \
        -d dl -o output_folder \
        tests.package.test_bc
    ...
    OK
---
 DEVELOPERS                               |  1 +
 support/testing/tests/package/test_bc.py | 62 ++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 support/testing/tests/package/test_bc.py

Comments

Yann E. MORIN Jan. 21, 2024, 10:22 a.m. UTC | #1
Julien, All,

On 2024-01-16 19:20 +0100, Julien Olivain spake thusly:
> Signed-off-by: Julien Olivain <ju.o@free.fr>
> ---
[--SNIP--]
> +        # Test a basic output base conversion of a large number.
> +        hex_str = "DEADBEEF0000ABADC0DE0000CAFEBABE"
                                              ^^^^^^^^
I've changed that to 8BADF00D.

Applied to master, thanks.

Regards,
Yann E. MORIN.

> +        hex_base = 16
> +        value = int(hex_str, base=hex_base)
> +        bc_expr = f"obase={hex_base} ; {value}"
> +        cmd = f"echo '{bc_expr}' | bc"
> +        output, exit_code = self.emulator.run(cmd)
> +        self.assertEqual(exit_code, 0)
> +        self.assertEqual(output[0], hex_str)
> +
> +        # Test a floating point computation by estimating Pi. Since we
> +        # use the bc arc-tangent a() function, we need the '-l'
> +        # option.
> +        bc_expr = "4 * a(1)"
> +        cmd = f"echo '{bc_expr}' | bc -l"
> +        output, exit_code = self.emulator.run(cmd)
> +        self.assertEqual(exit_code, 0)
> +        self.assertAlmostEqual(float(output[0]), math.pi, places=16)
> -- 
> 2.43.0
> 
> _______________________________________________
> buildroot mailing list
> buildroot@buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
diff mbox series

Patch

diff --git a/DEVELOPERS b/DEVELOPERS
index f5b04937b6..b30923f3ba 100644
--- a/DEVELOPERS
+++ b/DEVELOPERS
@@ -1759,6 +1759,7 @@  F:	support/testing/tests/package/sample_python_pyalsa.py
 F:	support/testing/tests/package/sample_python_spake2.py
 F:	support/testing/tests/package/test_acpica.py
 F:	support/testing/tests/package/test_acpica/
+F:	support/testing/tests/package/test_bc.py
 F:	support/testing/tests/package/test_brotli.py
 F:	support/testing/tests/package/test_bzip2.py
 F:	support/testing/tests/package/test_compressor_base.py
diff --git a/support/testing/tests/package/test_bc.py b/support/testing/tests/package/test_bc.py
new file mode 100644
index 0000000000..c31e871339
--- /dev/null
+++ b/support/testing/tests/package/test_bc.py
@@ -0,0 +1,62 @@ 
+import math
+import os
+
+import infra.basetest
+
+
+class TestBc(infra.basetest.BRTest):
+    config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
+        """
+        BR2_PACKAGE_BC=y
+        BR2_TARGET_ROOTFS_CPIO=y
+        # BR2_TARGET_ROOTFS_TAR is not set
+        """
+
+    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()
+
+        # Check the program executes.
+        self.assertRunOk("bc --version")
+
+        # We check a square root function of a number slightly larger
+        # than 32 bits.
+        value = 123456
+        squared_value = value ** 2
+        bc_expr = f"sqrt({squared_value})"
+        cmd = f"echo '{bc_expr}' | bc"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(int(output[0]), value)
+
+        # Perform an integer exponentiation producing a large number.
+        base = 3
+        exponent = 4567
+        expected_value = base ** exponent
+        bc_expr = f"{base} ^ {exponent}"
+        cmd = f"echo '{bc_expr}' | BC_LINE_LENGTH=0 bc"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(int(output[0]), expected_value)
+
+        # Test a basic output base conversion of a large number.
+        hex_str = "DEADBEEF0000ABADC0DE0000CAFEBABE"
+        hex_base = 16
+        value = int(hex_str, base=hex_base)
+        bc_expr = f"obase={hex_base} ; {value}"
+        cmd = f"echo '{bc_expr}' | bc"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(output[0], hex_str)
+
+        # Test a floating point computation by estimating Pi. Since we
+        # use the bc arc-tangent a() function, we need the '-l'
+        # option.
+        bc_expr = "4 * a(1)"
+        cmd = f"echo '{bc_expr}' | bc -l"
+        output, exit_code = self.emulator.run(cmd)
+        self.assertEqual(exit_code, 0)
+        self.assertAlmostEqual(float(output[0]), math.pi, places=16)