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