Files
micropython/tests/float/string_format2.py
Angus Gratton 3ec8b9a77c
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
all: Replace legacy name with MicroPython and MPy as applicable.
With the aim of getting consistency, and removing the need to learn an
additional term, replace uses of uPy/uPython with MPy/MicroPython.

Rule of thumb was to use "MPy" abbreviation where "CPy" is used nearby, but
the full word MicroPython otherwise.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-10-04 00:19:40 +10:00

183 lines
4.5 KiB
Python

# Change the following to True to get a much more comprehensive set of tests
# to run, albeit, which take considerably longer.
full_tests = False
def test(fmt, *args):
print("{:8s}".format(fmt) + ">" + fmt.format(*args) + "<")
def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
fmt = "{"
if conv:
fmt += "!"
fmt += conv
fmt += ":"
if alignment:
fmt += fill
fmt += alignment
fmt += sign
fmt += prefix
fmt += width
if precision:
fmt += "."
fmt += precision
fmt += type
fmt += "}"
test(fmt, arg)
if fill == "0" and alignment == "=":
fmt = "{:"
fmt += sign
fmt += prefix
fmt += width
if precision:
fmt += "."
fmt += precision
fmt += type
fmt += "}"
test(fmt, arg)
eg_nums = (
0.0,
-0.0,
0.1,
1.234,
12.3459,
1.23456789,
123456789.0,
-0.0,
-0.1,
-1.234,
-12.3459,
1e4,
1e-4,
1e5,
1e-5,
1e6,
1e-6,
1e10,
1e37,
-1e37,
1e-37,
-1e-37,
1.23456e8,
1.23456e7,
1.23456e6,
1.23456e5,
1.23456e4,
1.23456e3,
1.23456e2,
1.23456e1,
1.23456e0,
1.23456e-1,
1.23456e-2,
1.23456e-3,
1.23456e-4,
1.23456e-5,
1.23456e-6,
1.23456e-7,
1.23456e-8,
-1.23456e8,
-1.23456e7,
-1.23456e6,
-1.23456e5,
-1.23456e4,
-1.23456e3,
-1.23456e2,
-1.23456e1,
-1.23456e0,
-1.23456e-1,
-1.23456e-2,
-1.23456e-3,
-1.23456e-4,
-1.23456e-5,
-1.23456e-6,
-1.23456e-7,
-1.23456e-8,
)
if full_tests:
for type in ("e", "E", "g", "G", "n"):
for width in ("", "4", "6", "8", "10"):
for alignment in ("", "<", ">", "=", "^"):
for fill in ("", "@", "0", " "):
for sign in ("", "+", "-", " "):
for prec in ("", "1", "3", "6"):
for num in eg_nums:
test_fmt("", fill, alignment, sign, "", width, prec, type, num)
# Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345)
# rounds differently than print("%.3f", 1.2345);
f_nums = (
0.0,
-0.0,
0.0001,
0.001,
0.01,
0.1,
1.0,
10.0,
0.0012,
0.0123,
0.1234,
1.23459,
12.3456,
-0.0001,
-0.001,
-0.01,
-0.1,
-1.0,
-10.0,
-0.0012,
-0.0123,
-0.1234,
-1.23459,
-12.3456,
)
if full_tests:
for type in ("f", "F"):
for width in ("", "4", "6", "8", "10"):
for alignment in ("", "<", ">", "=", "^"):
for fill in ("", " ", "0", "@"):
for sign in ("", "+", "-", " "):
# An empty precision defaults to 6, but when MicroPython is
# configured to use a float, we can only use a
# precision of 6 with numbers less than 10 and still
# get results that compare to CPython (which uses
# long doubles).
for prec in ("1", "2", "3"):
for num in f_nums:
test_fmt("", fill, alignment, sign, "", width, prec, type, num)
for num in int_nums2:
test_fmt("", fill, alignment, sign, "", width, "", type, num)
pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99)
pct_nums2 = (True, False, 1, 0, -1)
if full_tests:
type = "%"
for width in ("", "4", "6", "8", "10"):
for alignment in ("", "<", ">", "=", "^"):
for fill in ("", " ", "0", "@"):
for sign in ("", "+", "-", " "):
# An empty precision defaults to 6, but when MicroPython is
# configured to use a float, we can only use a
# precision of 6 with numbers less than 10 and still
# get results that compare to CPython (which uses
# long doubles).
for prec in ("1", "2", "3"):
for num in pct_nums1:
test_fmt("", fill, alignment, sign, "", width, prec, type, num)
for num in pct_nums2:
test_fmt("", fill, alignment, sign, "", width, "", type, num)
else:
for num in pct_nums1:
test_fmt("", "", "", "", "", "", "1", "%", num)
# We don't currently test a type of '' with floats (see the detailed comment
# in objstr.c)