From bd72b95d9e8d5c05f5a7392dab00d9db34534565 Mon Sep 17 00:00:00 2001 From: chico ferreira <36338391+chicoferreira@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:10:59 +0100 Subject: Conversion from int to bytes and back (#4490) Co-authored-by: Laurenz --- tests/suite/foundations/int.typ | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'tests') diff --git a/tests/suite/foundations/int.typ b/tests/suite/foundations/int.typ index 0c85dcab..1744ef88 100644 --- a/tests/suite/foundations/int.typ +++ b/tests/suite/foundations/int.typ @@ -38,6 +38,24 @@ #test(int(10.0).signum(), 1) #test(int(-10.0).signum(), -1) +--- int-from-and-to-bytes --- +// Test `int.from-bytes` and `int.to-bytes`. +#test(int.from-bytes(bytes(())), 0) +#test(int.from-bytes(bytes((1, 0, 0, 0, 0, 0, 0, 0)), endian: "little", signed: true), 1) +#test(int.from-bytes(bytes((1, 0, 0, 0, 0, 0, 0, 0)), endian: "big", signed: true), 72057594037927936) +#test(int.from-bytes(bytes((1, 0, 0, 0, 0, 0, 0, 0)), endian: "little", signed: false), 1) +#test(int.from-bytes(bytes((255,)), endian: "big", signed: true), -1) +#test(int.from-bytes(bytes((255,)), endian: "big", signed: false), 255) +#test(int.from-bytes((-1000).to-bytes(endian: "big", size: 5), endian: "big", signed: true), -1000) +#test(int.from-bytes((-1000).to-bytes(endian: "little", size: 5), endian: "little", signed: true), -1000) +#test(int.from-bytes(1000.to-bytes(endian: "big", size: 5), endian: "big", signed: true), 1000) +#test(int.from-bytes(1000.to-bytes(endian: "little", size: 5), endian: "little", signed: true), 1000) +#test(int.from-bytes(1000.to-bytes(endian: "little", size: 5), endian: "little", signed: false), 1000) + +--- int-from-and-to-bytes-too-many --- +// Error: 2-34 too many bytes to convert to a 64 bit number +#int.from-bytes(bytes((0,) * 16)) + --- int-repr --- // Test the `repr` function with integers. #repr(12) \ -- cgit v1.2.3