Files
aji/tests/conformance/source/std/endian_u64.aji
T

115 lines
5.9 KiB
Rust

// expect: run
// result: 0
// phase: 6
// spec: docs/spec/16-standard-library.md §2.3; docs/spec/15-source-language.md §13
module test.endian
import std.bytes;
fn expect_read(result: std.bytes.ReadU64, expected: u64) -> void
effects []
{
match result {
std.bytes.ReadU64::ok(value) => { assert(value == expected); }
std.bytes.ReadU64::short_input => { trap; }
}
}
fn expect_short_read(result: std.bytes.ReadU64) -> void
effects []
{
match result {
std.bytes.ReadU64::ok(value) => { trap; }
std.bytes.ReadU64::short_input => {}
}
}
fn expect_write(result: std.bytes.WriteResult) -> void
effects []
{
match result {
std.bytes.WriteResult::ok => {}
std.bytes.WriteResult::short_output => { trap; }
}
}
fn expect_short_write(result: std.bytes.WriteResult) -> void
effects []
{
match result {
std.bytes.WriteResult::ok => { trap; }
std.bytes.WriteResult::short_output => {}
}
}
fn fixture(value: u64, le: slice<u8>, be: slice<u8>) -> void
effects []
{
expect_read(std.bytes.read_u64_le(ref le), value);
expect_read(std.bytes.read_u64_be(ref be), value);
var buf: [u8; 10] = [165; 10];
expect_write(std.bytes.write_u64_le(mut buf[1..9], value));
assert(std.bytes.equal(ref buf[1..9], ref le));
assert(buf[0] == 165);
for i in 9usize..buf.len { assert(buf[i] == 165); }
expect_read(std.bytes.read_u64_le(ref buf[1..10]), value);
expect_write(std.bytes.write_u64_be(mut buf[1..10], value));
assert(std.bytes.equal(ref buf[1..9], ref be));
assert(buf[0] == 165);
for i in 9usize..buf.len { assert(buf[i] == 165); }
expect_read(std.bytes.read_u64_be(ref buf[1..10]), value);
}
fn main() -> u8
effects []
{
// Every short length, including zero; error writes preserve the whole array.
var buf: [u8; 10] = [165; 10];
for size in 0usize..8usize {
expect_short_read(std.bytes.read_u64_le(ref buf[1..1 + size]));
expect_short_read(std.bytes.read_u64_be(ref buf[1..1 + size]));
expect_short_write(std.bytes.write_u64_le(mut buf[1..1 + size], 18446744073709551615));
expect_short_write(std.bytes.write_u64_be(mut buf[1..1 + size], 18446744073709551615));
for i in 0..buf.len { assert(buf[i] == 165); }
}
// Expected bytes are fixed by Python int.to_bytes, independently of std's algorithm.
fixture(0, b"\x00\x00\x00\x00\x00\x00\x00\x00", b"\x00\x00\x00\x00\x00\x00\x00\x00");
fixture(1, b"\x01\x00\x00\x00\x00\x00\x00\x00", b"\x00\x00\x00\x00\x00\x00\x00\x01");
fixture(18446744073709551615, b"\xff\xff\xff\xff\xff\xff\xff\xff", b"\xff\xff\xff\xff\xff\xff\xff\xff");
fixture(9223372036854775808, b"\x00\x00\x00\x00\x00\x00\x00\x80", b"\x80\x00\x00\x00\x00\x00\x00\x00");
fixture(6148914691236517205, b"\x55\x55\x55\x55\x55\x55\x55\x55", b"\x55\x55\x55\x55\x55\x55\x55\x55");
fixture(72623859790382856, b"\x08\x07\x06\x05\x04\x03\x02\x01", b"\x01\x02\x03\x04\x05\x06\x07\x08");
fixture(15292073328584422715, b"\x3b\xf5\x7f\xe5\xab\x5b\x38\xd4", b"\xd4\x38\x5b\xab\xe5\x7f\xf5\x3b");
fixture(16250661963492436153, b"\xb9\x28\xeb\xc2\xf4\xf2\x85\xe1", b"\xe1\x85\xf2\xf4\xc2\xeb\x28\xb9");
fixture(10437458558198301272, b"\x58\x42\x45\x37\x42\x4d\xd9\x90", b"\x90\xd9\x4d\x42\x37\x45\x42\x58");
fixture(17041513473160135133, b"\xdd\x41\xbe\xca\x36\x9e\x7f\xec", b"\xec\x7f\x9e\x36\xca\xbe\x41\xdd");
fixture(228321258174005107, b"\x73\xbf\x21\x80\xf9\x28\x2b\x03", b"\x03\x2b\x28\xf9\x80\x21\xbf\x73");
fixture(3870920093340187, b"\x1b\x12\x1e\xd1\x94\xc0\x0d\x00", b"\x00\x0d\xc0\x94\xd1\x1e\x12\x1b");
fixture(10610674309359647252, b"\x14\xba\xbe\x0c\x11\xb0\x40\x93", b"\x93\x40\xb0\x11\x0c\xbe\xba\x14");
fixture(10430557097931907062, b"\xf6\x1f\xa9\xc7\x6a\xc8\xc0\x90", b"\x90\xc0\xc8\x6a\xc7\xa9\x1f\xf6");
fixture(3219893636986986552, b"\x38\x60\x26\xff\x33\x5c\xaf\x2c", b"\x2c\xaf\x5c\x33\xff\x26\x60\x38");
fixture(8545323553216493439, b"\x7f\x77\x3f\x20\x7f\x16\x97\x76", b"\x76\x97\x16\x7f\x20\x3f\x77\x7f");
fixture(8990680145927224152, b"\x58\x3b\x26\x43\xf5\x4f\xc5\x7c", b"\x7c\xc5\x4f\xf5\x43\x26\x3b\x58");
fixture(12294589648129633388, b"\x6c\x2c\xf6\x3b\x25\x28\x9f\xaa", b"\xaa\x9f\x28\x25\x3b\xf6\x2c\x6c");
fixture(7802525658554814316, b"\x6c\xcf\x07\x35\xbf\x23\x48\x6c", b"\x6c\x48\x23\xbf\x35\x07\xcf\x6c");
fixture(3321086216808458160, b"\xb0\x67\x50\x7d\x51\xde\x16\x2e", b"\x2e\x16\xde\x51\x7d\x50\x67\xb0");
fixture(6819651081902464023, b"\x17\x9c\xe7\xe4\x86\x44\xa4\x5e", b"\x5e\xa4\x44\x86\xe4\xe7\x9c\x17");
fixture(2183293185793142392, b"\x78\x2e\xbb\xbb\x95\x9d\x4c\x1e", b"\x1e\x4c\x9d\x95\xbb\xbb\x2e\x78");
fixture(5544438551056829737, b"\x29\x4d\xb9\x8a\x7c\xcd\xf1\x4c", b"\x4c\xf1\xcd\x7c\x8a\xb9\x4d\x29");
fixture(7373447788311746792, b"\xe8\x18\x36\x80\xb2\xbf\x53\x66", b"\x66\x53\xbf\xb2\x80\x36\x18\xe8");
fixture(4804431457320197428, b"\x34\x5d\x48\x99\xf4\xc4\xac\x42", b"\x42\xac\xc4\xf4\x99\x48\x5d\x34");
fixture(13174703505296887866, b"\x3a\x90\x50\x16\x09\xf3\xd5\xb6", b"\xb6\xd5\xf3\x09\x16\x50\x90\x3a");
fixture(12286828819404160673, b"\xa1\xfe\x9d\x7c\xb6\x95\x83\xaa", b"\xaa\x83\x95\xb6\x7c\x9d\xfe\xa1");
fixture(8427543257253372388, b"\xe4\x61\xd3\x01\xf1\xa5\xf4\x74", b"\x74\xf4\xa5\xf1\x01\xd3\x61\xe4");
fixture(6325739954515741802, b"\x6a\x0c\x68\x32\xf9\x8a\xc9\x57", b"\x57\xc9\x8a\xf9\x32\x68\x0c\x6a");
fixture(3169332480603093103, b"\x6f\x78\x74\x69\x19\xbb\xfb\x2b", b"\x2b\xfb\xbb\x19\x69\x74\x78\x6f");
// Exercise every bit position as well as the fixed encoding fixtures.
for bit in 0u64..64u64 {
let value = 1u64 << bit;
expect_write(std.bytes.write_u64_le(mut buf, value));
expect_read(std.bytes.read_u64_le(ref buf), value);
expect_write(std.bytes.write_u64_be(mut buf, value));
expect_read(std.bytes.read_u64_be(ref buf), value);
}
return 0;
}