support/testing: new bc runtime test

Signed-off-by: Julien Olivain <ju.o@free.fr>
[yann.morin.1998@free.fr: tweak hex number]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
This commit is contained in:
Julien Olivain 2024-01-16 19:20:26 +01:00 committed by Yann E. MORIN
parent ad40b23f2b
commit fb007c98ea
2 changed files with 63 additions and 0 deletions

View File

@ -1760,6 +1760,7 @@ 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_apache.py
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

View File

@ -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 = "DEADBEEF0000ABADC0DE00008BADF00D"
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)