cli: add recoverable endian APIs and qualified std enums
This commit is contained in:
45 files changed
+1205
-89
No files matched your search
@@ -0,0 +1,20 @@
|
||||
// expect: run
|
||||
// result: 305419896
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.3
|
||||
module demo.endian
|
||||
import std.bytes;
|
||||
|
||||
fn main() -> u32
|
||||
effects []
|
||||
{
|
||||
var wire: [u8; 6] = zero;
|
||||
match std.bytes.write_u32_be(mut wire[1..5], 0x12345678) {
|
||||
std.bytes.WriteResult::ok => {}
|
||||
std.bytes.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
match std.bytes.read_u32_be(ref wire[1..5]) {
|
||||
std.bytes.ReadU32::ok(value) => { return value; }
|
||||
std.bytes.ReadU32::short_input => { return 0; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// expect: run
|
||||
// result: {"variant":"ok","payload":[4660]}
|
||||
// equivalent: std/endian_read.air
|
||||
// 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 main() -> std.bytes.ReadU16
|
||||
effects []
|
||||
{ return std.bytes.read_u16_le(b"\x34\x12"); }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E2101 E2101
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ var b: [u8; 2] = zero; ref saved = b; let r = std.bytes.write_u16_le(mut b, 1); return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7006
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ match std.bytes.read_u16_le(b"xx") { ok(v) => { return 0; } short_input => { return 1; } } }
|
||||
@@ -0,0 +1,25 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §13; docs/spec/16-standard-library.md §2.3
|
||||
module test.endian
|
||||
import std.bytes;
|
||||
|
||||
struct Packet { word: std.bytes.ReadU32 }
|
||||
|
||||
fn read_first(words: slice<std.bytes.ReadU32>) -> u32
|
||||
effects []
|
||||
{
|
||||
match words[0] {
|
||||
std.bytes.ReadU32::ok(value) => { return value; }
|
||||
std.bytes.ReadU32::short_input => { return 0; }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> u32
|
||||
effects []
|
||||
{
|
||||
let packet = Packet { word: std.bytes.ReadU32::ok(42) };
|
||||
let words: [std.bytes.ReadU32; 2] = [packet.word; 2];
|
||||
return read_first(ref words);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7006
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ match std.bytes.read_u16_le(b"xx") { std.bytes.ReadU16::ok(v) => { return 0; } std.bytes.ReadU16::ok(w) => { return 1; } std.bytes.ReadU16::short_input => { return 2; } } }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7004
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ match std.bytes.read_u16_le(b"xx") { std.bytes.ReadU16::ok(v) => { return 0; } } }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7006
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ match std.bytes.read_u16_le(b"xx") { std.bytes.ReadU16::ok(v) => { return 0; } _ => { return 1; } } }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.3; docs/spec/15-source-language.md §13
|
||||
module test.endian
|
||||
|
||||
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{ let r = std.bytes.ReadU16::short_input; return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ let r = std.bytes.ReadU16::ok; return 0; }
|
||||
@@ -0,0 +1,12 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.3; docs/spec/15-source-language.md §13
|
||||
module test.endian
|
||||
|
||||
|
||||
fn take(r: std.bytes.ReadU16) -> void
|
||||
effects []
|
||||
{}
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{ return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ let r = std.bytes.ReadU16::ok(true); return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ let r: u16 = std.bytes.read_u16_le(b"xx"); return 0; }
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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;
|
||||
|
||||
enum ReadU16 { Local(u8) }
|
||||
struct __aji_std_bytes_ReadU16 { marker: u8 }
|
||||
fn __aji_std_bytes_ReadU16_1() -> u8
|
||||
effects []
|
||||
{ return 7; }
|
||||
|
||||
fn relay(r: std.bytes.ReadU16) -> std.bytes.ReadU16
|
||||
effects []
|
||||
{ return r; }
|
||||
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{
|
||||
let std: u8 = 1;
|
||||
let result: std.bytes.ReadU16 = relay(std.bytes.ReadU16::ok(0x1234));
|
||||
match result {
|
||||
std.bytes.ReadU16::ok(value) => { assert(value == 0x1234); }
|
||||
std.bytes.ReadU16::short_input => { trap; }
|
||||
}
|
||||
let local = ReadU16::Local(std);
|
||||
match local { ReadU16::Local(value) => { assert(value == 1); } }
|
||||
return __aji_std_bytes_ReadU16_1() - 7;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// 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.ReadU16, expected: u16) -> void
|
||||
effects []
|
||||
{
|
||||
match result {
|
||||
std.bytes.ReadU16::ok(value) => { assert(value == expected); }
|
||||
std.bytes.ReadU16::short_input => { trap; }
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_short_read(result: std.bytes.ReadU16) -> void
|
||||
effects []
|
||||
{
|
||||
match result {
|
||||
std.bytes.ReadU16::ok(value) => { trap; }
|
||||
std.bytes.ReadU16::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: u16, le: slice<u8>, be: slice<u8>) -> void
|
||||
effects []
|
||||
{
|
||||
expect_read(std.bytes.read_u16_le(ref le), value);
|
||||
expect_read(std.bytes.read_u16_be(ref be), value);
|
||||
var buf: [u8; 10] = [165; 10];
|
||||
expect_write(std.bytes.write_u16_le(mut buf[1..3], value));
|
||||
assert(std.bytes.equal(ref buf[1..3], ref le));
|
||||
assert(buf[0] == 165);
|
||||
for i in 3usize..buf.len { assert(buf[i] == 165); }
|
||||
expect_read(std.bytes.read_u16_le(ref buf[1..10]), value);
|
||||
expect_write(std.bytes.write_u16_be(mut buf[1..10], value));
|
||||
assert(std.bytes.equal(ref buf[1..3], ref be));
|
||||
assert(buf[0] == 165);
|
||||
for i in 3usize..buf.len { assert(buf[i] == 165); }
|
||||
expect_read(std.bytes.read_u16_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..2usize {
|
||||
expect_short_read(std.bytes.read_u16_le(ref buf[1..1 + size]));
|
||||
expect_short_read(std.bytes.read_u16_be(ref buf[1..1 + size]));
|
||||
expect_short_write(std.bytes.write_u16_le(mut buf[1..1 + size], 65535));
|
||||
expect_short_write(std.bytes.write_u16_be(mut buf[1..1 + size], 65535));
|
||||
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", b"\x00\x00");
|
||||
fixture(1, b"\x01\x00", b"\x00\x01");
|
||||
fixture(65535, b"\xff\xff", b"\xff\xff");
|
||||
fixture(32768, b"\x00\x80", b"\x80\x00");
|
||||
fixture(21845, b"\x55\x55", b"\x55\x55");
|
||||
fixture(258, b"\x02\x01", b"\x01\x02");
|
||||
fixture(65197, b"\xad\xfe", b"\xfe\xad");
|
||||
fixture(30078, b"\x7e\x75", b"\x75\x7e");
|
||||
fixture(3740, b"\x9c\x0e", b"\x0e\x9c");
|
||||
fixture(31293, b"\x3d\x7a", b"\x7a\x3d");
|
||||
fixture(58065, b"\xd1\xe2", b"\xe2\xd1");
|
||||
fixture(26864, b"\xf0\x68", b"\x68\xf0");
|
||||
fixture(39631, b"\xcf\x9a", b"\x9a\xcf");
|
||||
fixture(22646, b"\x76\x58", b"\x58\x76");
|
||||
fixture(34206, b"\x9e\x85", b"\x85\x9e");
|
||||
fixture(47885, b"\x0d\xbb", b"\xbb\x0d");
|
||||
fixture(47204, b"\x64\xb8", b"\xb8\x64");
|
||||
fixture(58243, b"\x83\xe3", b"\xe3\x83");
|
||||
fixture(55416, b"\x78\xd8", b"\xd8\x78");
|
||||
fixture(4283, b"\xbb\x10", b"\x10\xbb");
|
||||
fixture(13816, b"\xf8\x35", b"\x35\xf8");
|
||||
fixture(43928, b"\x98\xab", b"\xab\x98");
|
||||
fixture(5670, b"\x26\x16", b"\x16\x26");
|
||||
fixture(50429, b"\xfd\xc4", b"\xc4\xfd");
|
||||
fixture(6951, b"\x27\x1b", b"\x1b\x27");
|
||||
fixture(1626, b"\x5a\x06", b"\x06\x5a");
|
||||
fixture(42424, b"\xb8\xa5", b"\xa5\xb8");
|
||||
fixture(62732, b"\x0c\xf5", b"\xf5\x0c");
|
||||
fixture(19262, b"\x3e\x4b", b"\x4b\x3e");
|
||||
fixture(12581, b"\x25\x31", b"\x31\x25");
|
||||
// Exercise every bit position as well as the fixed encoding fixtures.
|
||||
for bit in 0u16..16u16 {
|
||||
let value = 1u16 << bit;
|
||||
expect_write(std.bytes.write_u16_le(mut buf, value));
|
||||
expect_read(std.bytes.read_u16_le(ref buf), value);
|
||||
expect_write(std.bytes.write_u16_be(mut buf, value));
|
||||
expect_read(std.bytes.read_u16_be(ref buf), value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// 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.ReadU32, expected: u32) -> void
|
||||
effects []
|
||||
{
|
||||
match result {
|
||||
std.bytes.ReadU32::ok(value) => { assert(value == expected); }
|
||||
std.bytes.ReadU32::short_input => { trap; }
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_short_read(result: std.bytes.ReadU32) -> void
|
||||
effects []
|
||||
{
|
||||
match result {
|
||||
std.bytes.ReadU32::ok(value) => { trap; }
|
||||
std.bytes.ReadU32::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: u32, le: slice<u8>, be: slice<u8>) -> void
|
||||
effects []
|
||||
{
|
||||
expect_read(std.bytes.read_u32_le(ref le), value);
|
||||
expect_read(std.bytes.read_u32_be(ref be), value);
|
||||
var buf: [u8; 10] = [165; 10];
|
||||
expect_write(std.bytes.write_u32_le(mut buf[1..5], value));
|
||||
assert(std.bytes.equal(ref buf[1..5], ref le));
|
||||
assert(buf[0] == 165);
|
||||
for i in 5usize..buf.len { assert(buf[i] == 165); }
|
||||
expect_read(std.bytes.read_u32_le(ref buf[1..10]), value);
|
||||
expect_write(std.bytes.write_u32_be(mut buf[1..10], value));
|
||||
assert(std.bytes.equal(ref buf[1..5], ref be));
|
||||
assert(buf[0] == 165);
|
||||
for i in 5usize..buf.len { assert(buf[i] == 165); }
|
||||
expect_read(std.bytes.read_u32_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..4usize {
|
||||
expect_short_read(std.bytes.read_u32_le(ref buf[1..1 + size]));
|
||||
expect_short_read(std.bytes.read_u32_be(ref buf[1..1 + size]));
|
||||
expect_short_write(std.bytes.write_u32_le(mut buf[1..1 + size], 4294967295));
|
||||
expect_short_write(std.bytes.write_u32_be(mut buf[1..1 + size], 4294967295));
|
||||
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", b"\x00\x00\x00\x00");
|
||||
fixture(1, b"\x01\x00\x00\x00", b"\x00\x00\x00\x01");
|
||||
fixture(4294967295, b"\xff\xff\xff\xff", b"\xff\xff\xff\xff");
|
||||
fixture(2147483648, b"\x00\x00\x00\x80", b"\x80\x00\x00\x00");
|
||||
fixture(1431655765, b"\x55\x55\x55\x55", b"\x55\x55\x55\x55");
|
||||
fixture(16909060, b"\x04\x03\x02\x01", b"\x01\x02\x03\x04");
|
||||
fixture(185903060, b"\xd4\xa7\x14\x0b", b"\x0b\x14\xa7\xd4");
|
||||
fixture(2037551487, b"\x7f\x91\x72\x79", b"\x79\x72\x91\x7f");
|
||||
fixture(2312196622, b"\x0e\x52\xd1\x89", b"\x89\xd1\x52\x0e");
|
||||
fixture(3350121379, b"\xa3\xcb\xae\xc7", b"\xc7\xae\xcb\xa3");
|
||||
fixture(1919861753, b"\xf9\xc3\x6e\x72", b"\x72\x6e\xc3\xf9");
|
||||
fixture(2075794910, b"\xde\x1d\xba\x7b", b"\x7b\xba\x1d\xde");
|
||||
fixture(2934997640, b"\x88\x82\xf0\xae", b"\xae\xf0\x82\x88");
|
||||
fixture(3004148163, b"\xc3\xa9\x0f\xb3", b"\xb3\x0f\xa9\xc3");
|
||||
fixture(2571791918, b"\x2e\x6e\x4a\x99", b"\x99\x4a\x6e\x2e");
|
||||
fixture(3086068765, b"\x1d\xac\xf1\xb7", b"\xb7\xf1\xac\x1d");
|
||||
fixture(3288209780, b"\x74\x19\xfe\xc3", b"\xc3\xfe\x19\x74");
|
||||
fixture(595502178, b"\x62\xa4\x7e\x23", b"\x23\x7e\xa4\x62");
|
||||
fixture(246423927, b"\x77\x21\xb0\x0e", b"\x0e\xb0\x21\x77");
|
||||
fixture(4061270332, b"\x3c\x11\x12\xf2", b"\xf2\x12\x11\x3c");
|
||||
fixture(2716554228, b"\xf4\x53\xeb\xa1", b"\xa1\xeb\x53\xf4");
|
||||
fixture(19619250, b"\xb2\x5d\x2b\x01", b"\x01\x2b\x5d\xb2");
|
||||
fixture(908296138, b"\xca\x7f\x23\x36", b"\x36\x23\x7f\xca");
|
||||
fixture(1856085160, b"\xa8\x9c\xa1\x6e", b"\x6e\xa1\x9c\xa8");
|
||||
fixture(2157080593, b"\x11\x70\x92\x80", b"\x80\x92\x70\x11");
|
||||
fixture(3019974009, b"\x79\x25\x01\xb4", b"\xb4\x01\x25\x79");
|
||||
fixture(877787239, b"\x67\xf8\x51\x34", b"\x34\x51\xf8\x67");
|
||||
fixture(4107084175, b"\x8f\x21\xcd\xf4", b"\xf4\xcd\x21\x8f");
|
||||
fixture(2223566135, b"\x37\xed\x88\x84", b"\x84\x88\xed\x37");
|
||||
fixture(1062036093, b"\x7d\x62\x4d\x3f", b"\x3f\x4d\x62\x7d");
|
||||
// Exercise every bit position as well as the fixed encoding fixtures.
|
||||
for bit in 0u32..32u32 {
|
||||
let value = 1u32 << bit;
|
||||
expect_write(std.bytes.write_u32_le(mut buf, value));
|
||||
expect_read(std.bytes.read_u32_le(ref buf), value);
|
||||
expect_write(std.bytes.write_u32_be(mut buf, value));
|
||||
expect_read(std.bytes.read_u32_be(ref buf), value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7002
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ match std.bytes.read_u16_le(b"xx") { std.bytes.Missing::ok(v) => { return 0; } std.bytes.Missing::short_input => { return 1; } } }
|
||||
@@ -0,0 +1,12 @@
|
||||
// expect: reject E7002
|
||||
// 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 take(r: std.bytes.Missing) -> void
|
||||
effects []
|
||||
{}
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{ return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7002
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ let r = std.bytes.ReadU16::missing; return 0; }
|
||||
@@ -0,0 +1,12 @@
|
||||
// expect: reject E7002
|
||||
// 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 take(r: ReadU16) -> void
|
||||
effects []
|
||||
{}
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{ return 0; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ var b: [u8; 2] = zero; let r = std.bytes.write_u16_le(mut b, 1u32); return 0; }
|
||||
@@ -0,0 +1,15 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.3; docs/spec/15-source-language.md §13
|
||||
module test.endian
|
||||
import std.bytes;
|
||||
|
||||
enum ReadU16 { ok(u16), short_input }
|
||||
fn main() -> u8
|
||||
effects []
|
||||
{
|
||||
match std.bytes.read_u16_le(b"xx") {
|
||||
ReadU16::ok(value) => { return 0; }
|
||||
ReadU16::short_input => { return 1; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// 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 main() -> u8
|
||||
effects []
|
||||
{ var b: [u8; 2] = zero; let r = std.bytes.write_u16_le(ref b, 1); return 0; }
|
||||
@@ -0,0 +1,16 @@
|
||||
; expect: reject E1001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md; docs/spec/16-standard-library.md §2.3
|
||||
module test.endian_payload_type
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
type @ReadU16 = enum { ok(u16), short_input }
|
||||
|
||||
fn @main() -> @ReadU16
|
||||
effects []
|
||||
entry:
|
||||
%wrong = const.u32 4660
|
||||
%result = enum.make @ReadU16.ok(%wrong)
|
||||
ret %result
|
||||
@@ -0,0 +1,63 @@
|
||||
; expect: run
|
||||
; result: {"variant":"ok","payload":[4660]}
|
||||
; phase: 1
|
||||
; spec: docs/spec/16-standard-library.md §2.3; docs/spec/07a-air-instructions.md
|
||||
module test.endian_read
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, ownership]
|
||||
|
||||
type @ReadU16 = enum { ok(u16), short_input }
|
||||
|
||||
fn @main() -> @ReadU16
|
||||
effects []
|
||||
entry:
|
||||
%storage = local.alloc array<u8, 2>
|
||||
local.zero %storage
|
||||
%m = borrow.mut %storage
|
||||
%dst = slice.make %m
|
||||
%zero = const.usize 0
|
||||
%one = const.usize 1
|
||||
%lo = const.u8 52
|
||||
%hi = const.u8 18
|
||||
%d0 = borrow.index %dst, %zero
|
||||
store %d0, %lo
|
||||
borrow.end %d0
|
||||
%d1 = borrow.index %dst, %one
|
||||
store %d1, %hi
|
||||
borrow.end %d1
|
||||
borrow.end %dst
|
||||
borrow.end %m
|
||||
%r = borrow.ref %storage
|
||||
%src = slice.make %r
|
||||
%result = call @read_u16_le(%src)
|
||||
borrow.end %src
|
||||
borrow.end %r
|
||||
ret %result
|
||||
|
||||
fn @read_u16_le(%src: slice<u8>) -> @ReadU16
|
||||
effects []
|
||||
entry:
|
||||
%n = slice.len %src
|
||||
%two = const.usize 2
|
||||
%short = lt.usize %n, %two
|
||||
cbr %short, @short_input(), @decode()
|
||||
short_input:
|
||||
%failure = enum.make @ReadU16.short_input()
|
||||
ret %failure
|
||||
decode:
|
||||
%zero = const.usize 0
|
||||
%one = const.usize 1
|
||||
%e0 = borrow.index %src, %zero
|
||||
%b0 = load %e0
|
||||
borrow.end %e0
|
||||
%e1 = borrow.index %src, %one
|
||||
%b1 = load %e1
|
||||
borrow.end %e1
|
||||
%lo = cast.checked.u16 %b0
|
||||
%hi = cast.checked.u16 %b1
|
||||
%eight = const.u16 8
|
||||
%shifted = shl.u16 %hi, %eight
|
||||
%value = or.u16 %lo, %shifted
|
||||
%success = enum.make @ReadU16.ok(%value)
|
||||
ret %success
|
||||
Reference in new issue
Block a user