mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: add pub fn read_bytes and pub fn read_bytes_at
This commit is contained in:

committed by
Alexander Medvednikov

parent
81b1b882fd
commit
c23155790a
@ -33,6 +33,42 @@ fn test_write_and_read_string_to_file() {
|
||||
os.rm(filename)
|
||||
}
|
||||
|
||||
// test_write_and_read_bytes checks for regressions made in the functions
|
||||
// read_bytes, read_bytes_at and write_bytes.
|
||||
fn test_write_and_read_bytes() {
|
||||
file_name := './byte_reader_writer.tst'
|
||||
payload := [`I`, `D`, `D`, `Q`, `D`]
|
||||
|
||||
file_write := os.create(os.realpath(file_name)) or {
|
||||
eprintln('failed to create file $file_name')
|
||||
return
|
||||
}
|
||||
|
||||
// We use the standard write_bytes function to write the payload and
|
||||
// compare the length of the array with the file size (have to match).
|
||||
file_write.write_bytes(payload.data, 5)
|
||||
|
||||
file_write.close()
|
||||
|
||||
assert payload.len == os.file_size(file_name)
|
||||
|
||||
file_read := os.open(os.realpath(file_name)) or {
|
||||
eprintln('failed to open file $file_name')
|
||||
return
|
||||
}
|
||||
|
||||
// We only need to test read_bytes because this function calls
|
||||
// read_bytes_at with second parameter zeroed (size, 0).
|
||||
red_bytes := file_read.read_bytes(5)
|
||||
|
||||
file_read.close()
|
||||
|
||||
assert red_bytes.str() == payload.str()
|
||||
|
||||
// We finally delete the test file.
|
||||
os.rm(file_name)
|
||||
}
|
||||
|
||||
fn test_create_and_delete_folder() {
|
||||
folder := './test1'
|
||||
os.mkdir(folder)
|
||||
|
Reference in New Issue
Block a user