os: add os.write_bytes/2 as a complement to os.read_bytes/1, add test

This commit is contained in:
Delyan Angelov 2025-06-28 09:27:09 +03:00
parent 81e84a6a01
commit 2cfeb6d07b
2 changed files with 15 additions and 0 deletions

View File

@ -62,6 +62,12 @@ pub fn read_bytes(path string) ![]u8 {
return res
}
// write_bytes writes all the `bytes` to `path`.
@[manualfree]
pub fn write_bytes(path string, bytes []u8) ! {
write_file_array(path, bytes)!
}
fn find_cfile_size(fp &C.FILE) !int {
// NB: Musl's fseek returns -1 for virtual files, while Glibc's fseek returns 0
cseek := C.fseek(fp, 0, C.SEEK_END)

View File

@ -784,6 +784,15 @@ fn test_write_file_array_bytes() {
// eprintln(rarr.str())
}
fn test_write_bytes() {
fpath := './wbytes.bin'
for arr in [[u8(65), 66, 67, 68, 69, 70], [u8(3), 2, 1], []u8{}] {
os.write_bytes(fpath, arr)!
rarr := os.read_bytes(fpath)!
assert arr == rarr
}
}
fn test_write_file_array_structs() {
fpath := './astructs.bin'
mut arr := []IntPoint{len: maxn}