file io
This commit is contained in:
@@ -2,15 +2,15 @@ arraylist :: import "@std/arraylist"
|
||||
mem :: import "@std/mem"
|
||||
std :: import "@std"
|
||||
|
||||
hide fail_alloc func(_ ?*mut anyopaque, _ usize, _ usize) ?*mut u8 {
|
||||
hide fail_alloc func(_ ?@mut anyopaque, _ usize, _ usize) ?*mut u8 {
|
||||
return none
|
||||
}
|
||||
|
||||
hide fail_realloc func(_ ?*mut anyopaque, _ ?*mut u8, _ usize, _ usize, _ usize) ?*mut u8 {
|
||||
hide fail_realloc func(_ ?@mut anyopaque, _ ?*mut u8, _ usize, _ usize, _ usize) ?*mut u8 {
|
||||
return none
|
||||
}
|
||||
|
||||
hide fail_free func(_ ?*mut anyopaque, _ ?*mut u8, _ usize, _ usize) void {}
|
||||
hide fail_free func(_ ?@mut anyopaque, _ ?*mut u8, _ usize, _ usize) void {}
|
||||
|
||||
hide fail_vtable mem.AllocatorVTable :: mem.AllocatorVTable {
|
||||
alloc = fail_alloc,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
io :: import "@std/io"
|
||||
process :: import "@std/process"
|
||||
|
||||
hide read_ok func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
|
||||
hide read_ok func(_ ?@mut anyopaque, _ io.Handle, buffer []mut u8) usize ! io.ReadError {
|
||||
if buffer.len == 0 {
|
||||
return 0
|
||||
}
|
||||
@@ -13,85 +13,94 @@ hide read_ok func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! i
|
||||
return 2
|
||||
}
|
||||
|
||||
hide read_too_much func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
|
||||
hide read_too_much func(_ ?@mut anyopaque, _ io.Handle, buffer []mut u8) usize ! io.ReadError {
|
||||
return buffer.len + 1
|
||||
}
|
||||
|
||||
hide read_eof func(_ ?*mut anyopaque, _ io.ReadStream, _ []mut u8) usize ! io.ReadError {
|
||||
hide read_eof func(_ ?@mut anyopaque, _ io.Handle, _ []mut u8) usize ! io.ReadError {
|
||||
return 0
|
||||
}
|
||||
|
||||
hide write_short func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
|
||||
hide write_short func(_ ?@mut anyopaque, _ io.Handle, bytes []u8) usize ! io.WriteError {
|
||||
if bytes.len > 2 {
|
||||
return 2
|
||||
}
|
||||
return bytes.len
|
||||
}
|
||||
|
||||
hide write_none func(_ ?*mut anyopaque, _ io.WriteStream, _ []u8) usize ! io.WriteError {
|
||||
hide write_none func(_ ?@mut anyopaque, _ io.Handle, _ []u8) usize ! io.WriteError {
|
||||
return 0
|
||||
}
|
||||
|
||||
hide write_too_much func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
|
||||
hide write_too_much func(_ ?@mut anyopaque, _ io.Handle, bytes []u8) usize ! io.WriteError {
|
||||
return bytes.len + 1
|
||||
}
|
||||
|
||||
hide ok_vtable io.IoVTable :: io.IoVTable {
|
||||
read = read_ok,
|
||||
write = write_short,
|
||||
}
|
||||
|
||||
hide read_bad_vtable io.IoVTable :: io.IoVTable {
|
||||
read = read_too_much,
|
||||
write = write_short,
|
||||
}
|
||||
|
||||
hide eof_vtable io.IoVTable :: io.IoVTable {
|
||||
read = read_eof,
|
||||
write = write_short,
|
||||
}
|
||||
|
||||
hide write_none_vtable io.IoVTable :: io.IoVTable {
|
||||
read = read_ok,
|
||||
write = write_none,
|
||||
}
|
||||
|
||||
hide write_bad_vtable io.IoVTable :: io.IoVTable {
|
||||
read = read_ok,
|
||||
write = write_too_much,
|
||||
}
|
||||
|
||||
reader_for func(vtable @io.IoVTable) io.Reader {
|
||||
ok_reader func() io.Reader {
|
||||
return io.Reader {
|
||||
impl = io.Io {context = none, vtable = vtable},
|
||||
stream = .stdin,
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
read = read_ok,
|
||||
}
|
||||
}
|
||||
|
||||
writer_for func(vtable @io.IoVTable) io.Writer {
|
||||
bad_reader func() io.Reader {
|
||||
return io.Reader {
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
read = read_too_much,
|
||||
}
|
||||
}
|
||||
|
||||
eof_reader func() io.Reader {
|
||||
return io.Reader {
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
read = read_eof,
|
||||
}
|
||||
}
|
||||
|
||||
short_writer func() io.Writer {
|
||||
return io.Writer {
|
||||
impl = io.Io {context = none, vtable = vtable},
|
||||
stream = .stdout,
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
write = write_short,
|
||||
}
|
||||
}
|
||||
|
||||
none_writer func() io.Writer {
|
||||
return io.Writer {
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
write = write_none,
|
||||
}
|
||||
}
|
||||
|
||||
bad_writer func() io.Writer {
|
||||
return io.Writer {
|
||||
context = none,
|
||||
handle = io.Handle {file_desc = 0},
|
||||
write = write_too_much,
|
||||
}
|
||||
}
|
||||
|
||||
rejects_bad_read func() bool {
|
||||
buffer [1]mut u8 = [0]
|
||||
_ = io.read(reader_for(&read_bad_vtable), buffer[..]) catch |err| {
|
||||
_ = io.read(bad_reader(), buffer[..]) catch |err| {
|
||||
return err == .read_failed
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
rejects_no_progress func() bool {
|
||||
io.print(writer_for(&write_none_vtable), "{s}", {"x",}) catch |err| {
|
||||
io.print(none_writer(), "{s}", {"x",}) catch |err| {
|
||||
return err == .no_progress
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
rejects_bad_write func() bool {
|
||||
_ = io.write(writer_for(&write_bad_vtable), "x") catch |err| {
|
||||
_ = io.write(bad_writer(), "x") catch |err| {
|
||||
return err == .write_failed
|
||||
}
|
||||
return false
|
||||
@@ -100,32 +109,26 @@ rejects_bad_write func() bool {
|
||||
main func(init process.Init) i32 {
|
||||
system io.Io :: init.io
|
||||
buffer [2]mut u8 = [0, 0]
|
||||
count usize :: io.read(reader_for(&ok_vtable), buffer[..]) catch 0
|
||||
count usize :: io.read(ok_reader(), buffer[..]) catch 0
|
||||
if count != 2 or buffer[0] != 'o' or buffer[1] != 'k' {
|
||||
return 1
|
||||
}
|
||||
eof usize :: io.read(reader_for(&eof_vtable), buffer[..]) catch 1
|
||||
empty_read usize :: io.read(reader_for(&read_bad_vtable), buffer[0..0]) catch 1
|
||||
empty_write usize :: io.write(writer_for(&write_bad_vtable), "") catch 1
|
||||
eof usize :: io.read(eof_reader(), buffer[..]) catch 1
|
||||
empty_read usize :: io.read(bad_reader(), buffer[0..0]) catch 1
|
||||
empty_write usize :: io.write(bad_writer(), "") catch 1
|
||||
if eof != 0 or empty_read != 0 or empty_write != 0 {
|
||||
return 5
|
||||
}
|
||||
io.print(writer_for(&ok_vtable), "{s}{d}", {"partial", 37}) catch |_| {
|
||||
io.print(short_writer(), "{s}{d}", {"partial", 37}) catch |_| {
|
||||
return 2
|
||||
}
|
||||
if !rejects_bad_read() or !rejects_no_progress() or !rejects_bad_write() {
|
||||
return 3
|
||||
}
|
||||
io.print(io.Writer {
|
||||
impl = system,
|
||||
stream = .stdout,
|
||||
}, "io-ok {d} {{bro}}\n", {37,}) catch |_| {
|
||||
io.print(io.stdout(system), "io-ok {d} {{bro}}\n", {37,}) catch |_| {
|
||||
return 4
|
||||
}
|
||||
io.print(io.Writer {
|
||||
impl = system,
|
||||
stream = .stdout,
|
||||
}, "bounds={d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d}\n", {
|
||||
io.print(io.stdout(system), "bounds={d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d} {d}/{d}\n", {
|
||||
minval!(i8), maxval!(u8),
|
||||
minval!(i16), maxval!(u16),
|
||||
minval!(i32), maxval!(u32),
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
mem :: import "@std/mem"
|
||||
|
||||
hide probe_count func(context ?*mut anyopaque) void {
|
||||
hide probe_count func(context ?@mut anyopaque) void {
|
||||
if context |raw| {
|
||||
counts *mut usize :: ptrcast!(usize, raw)
|
||||
counts[0] += 1
|
||||
count @mut usize :: ptrcast!(usize, raw)
|
||||
count^ += 1
|
||||
}
|
||||
}
|
||||
|
||||
hide probe_alloc func(context ?*mut anyopaque, _ usize, _ usize) ?*mut u8 {
|
||||
hide probe_alloc func(context ?@mut anyopaque, _ usize, _ usize) ?*mut u8 {
|
||||
probe_count(context)
|
||||
return none
|
||||
}
|
||||
|
||||
hide probe_realloc func(context ?*mut anyopaque, _ ?*mut u8, _ usize, _ usize, _ usize) ?*mut u8 {
|
||||
hide probe_realloc func(context ?@mut anyopaque, _ ?*mut u8, _ usize, _ usize, _ usize) ?*mut u8 {
|
||||
probe_count(context)
|
||||
return none
|
||||
}
|
||||
|
||||
hide probe_free func(context ?*mut anyopaque, _ ?*mut u8, _ usize, _ usize) void {
|
||||
hide probe_free func(context ?@mut anyopaque, _ ?*mut u8, _ usize, _ usize) void {
|
||||
probe_count(context)
|
||||
}
|
||||
|
||||
@@ -33,11 +33,11 @@ typed_allocator_test func() i32 {
|
||||
first_calls [1]mut usize = [0]
|
||||
second_calls [1]mut usize = [0]
|
||||
first_allocator mem.Allocator :: mem.Allocator {
|
||||
context = (&first_calls).ptr,
|
||||
context = &first_calls,
|
||||
vtable = &probe_vtable,
|
||||
}
|
||||
second_allocator mem.Allocator :: mem.Allocator {
|
||||
context = (&second_calls).ptr,
|
||||
context = &second_calls,
|
||||
vtable = &probe_vtable,
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ main func(init process.Init) i32 {
|
||||
return 2
|
||||
}
|
||||
|
||||
writer io.Writer :: io.Writer {impl = init.io, stream = .stdout}
|
||||
writer io.Writer :: io.stdout(init.io)
|
||||
positive f64 = 1.0
|
||||
zero f64 = 0.0
|
||||
infinity f64 :: positive / zero
|
||||
|
||||
Reference in New Issue
Block a user