Fix WriteBuffer.write_bytes off-by-one (#1625)

* fix WriteBuffer.write_bytes off-by-one

* test for WriteBuffer.write_bytes off-by-one
This commit is contained in:
Walther Chen
2024-11-14 08:58:09 -05:00
committed by GitHub
parent ea9a871d90
commit a233771433
2 changed files with 27 additions and 2 deletions

View File

@@ -66,4 +66,29 @@ fn void! writebuffer()
assert(n == DATA.len, "got %d; want %d", n, DATA.len);
String got = out.str_view();
assert(got == DATA, "got %s; want %s", got, DATA);
}
}
fn void! writebuffer_write_byte()
{
ByteWriter out;
out.temp_init();
char[2] buf;
WriteBuffer write_buf;
write_buf.init(&out, buf[..]);
write_buf.write_byte('a')!;
assert(write_buf.str_view() == "a");
assert(out.str_view() == "");
write_buf.write_byte('b')!;
assert(write_buf.str_view() == "ab");
assert(out.str_view() == "");
write_buf.write_byte('c')!;
assert(write_buf.str_view() == "c");
assert(out.str_view() == "ab");
write_buf.flush()!;
assert(write_buf.str_view() == "");
assert(out.str_view() == "abc");
}