test_bc.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import math
  2. import os
  3. import infra.basetest
  4. class TestBc(infra.basetest.BRTest):
  5. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  6. """
  7. BR2_PACKAGE_BC=y
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def test_run(self):
  12. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  13. self.emulator.boot(arch="armv5",
  14. kernel="builtin",
  15. options=["-initrd", cpio_file])
  16. self.emulator.login()
  17. # Check the program executes.
  18. self.assertRunOk("bc --version")
  19. # We check a square root function of a number slightly larger
  20. # than 32 bits.
  21. value = 123456
  22. squared_value = value ** 2
  23. bc_expr = f"sqrt({squared_value})"
  24. cmd = f"echo '{bc_expr}' | bc"
  25. output, exit_code = self.emulator.run(cmd)
  26. self.assertEqual(exit_code, 0)
  27. self.assertEqual(int(output[0]), value)
  28. # Perform an integer exponentiation producing a large number.
  29. base = 3
  30. exponent = 4567
  31. expected_value = base ** exponent
  32. bc_expr = f"{base} ^ {exponent}"
  33. cmd = f"echo '{bc_expr}' | BC_LINE_LENGTH=0 bc"
  34. output, exit_code = self.emulator.run(cmd)
  35. self.assertEqual(exit_code, 0)
  36. self.assertEqual(int(output[0]), expected_value)
  37. # Test a basic output base conversion of a large number.
  38. hex_str = "DEADBEEF0000ABADC0DE00008BADF00D"
  39. hex_base = 16
  40. value = int(hex_str, base=hex_base)
  41. bc_expr = f"obase={hex_base} ; {value}"
  42. cmd = f"echo '{bc_expr}' | bc"
  43. output, exit_code = self.emulator.run(cmd)
  44. self.assertEqual(exit_code, 0)
  45. self.assertEqual(output[0], hex_str)
  46. # Test a floating point computation by estimating Pi. Since we
  47. # use the bc arc-tangent a() function, we need the '-l'
  48. # option.
  49. bc_expr = "4 * a(1)"
  50. cmd = f"echo '{bc_expr}' | bc -l"
  51. output, exit_code = self.emulator.run(cmd)
  52. self.assertEqual(exit_code, 0)
  53. self.assertAlmostEqual(float(output[0]), math.pi, places=16)