2021-03-04 14:33:54 +03:00
|
|
|
import os
|
|
|
|
|
2022-07-28 16:21:23 +03:00
|
|
|
const tfolder = os.join_path(os.temp_dir(), 'v', 'tests', 'os_file_test')
|
|
|
|
|
|
|
|
const tfile = os.join_path_single(tfolder, 'test_file')
|
|
|
|
|
|
|
|
fn testsuite_begin() ? {
|
|
|
|
os.rmdir_all(tfolder) or {}
|
|
|
|
assert !os.is_dir(tfolder)
|
|
|
|
os.mkdir_all(tfolder)?
|
|
|
|
os.chdir(tfolder)?
|
|
|
|
assert os.is_dir(tfolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() ? {
|
|
|
|
os.rmdir_all(tfolder) or {}
|
|
|
|
}
|
|
|
|
|
2021-03-04 14:33:54 +03:00
|
|
|
struct Point {
|
|
|
|
x f64
|
|
|
|
y f64
|
|
|
|
z f64
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:44:53 +03:00
|
|
|
struct Extended_Point {
|
|
|
|
a f64
|
|
|
|
b f64
|
|
|
|
c f64
|
|
|
|
d f64
|
|
|
|
e f64
|
|
|
|
f f64
|
|
|
|
g f64
|
|
|
|
h f64
|
|
|
|
i f64
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
enum Color {
|
|
|
|
red
|
|
|
|
green
|
|
|
|
blue
|
|
|
|
}
|
2021-03-04 14:33:54 +03:00
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
[flag]
|
|
|
|
enum Permissions {
|
|
|
|
read
|
|
|
|
write
|
|
|
|
execute
|
|
|
|
}
|
2021-03-06 21:44:53 +03:00
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
const (
|
|
|
|
unit_point = Point{1.0, 1.0, 1.0}
|
|
|
|
another_point = Point{0.25, 2.25, 6.25}
|
|
|
|
extended_point = Extended_Point{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}
|
2022-04-15 14:58:56 +03:00
|
|
|
another_byte = u8(123)
|
2021-03-10 19:45:12 +03:00
|
|
|
another_color = Color.red
|
|
|
|
another_permission = Permissions.read | .write
|
|
|
|
)
|
|
|
|
|
2021-05-21 13:18:08 +03:00
|
|
|
// test_read_bytes_into_newline_text tests reading text from a file with newlines.
|
|
|
|
// This test simulates reading a larger text file step by step into a buffer and
|
|
|
|
// returning on each newline, even before the buffer is full, and reaching EOF before
|
|
|
|
// the buffer is completely filled.
|
|
|
|
fn test_read_bytes_into_newline_text() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_string('Hello World!\nGood\r morning.')?
|
2021-05-21 13:18:08 +03:00
|
|
|
f.close()
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 8}
|
2021-05-21 13:18:08 +03:00
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n0 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n0 == 8
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n1 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n1 == 5
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n2 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n2 == 8
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n3 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n3 == 6
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test_read_bytes_into_newline_binary tests reading a binary file with NUL bytes.
|
|
|
|
// This test simulates the scenario when a byte stream is read and a newline byte
|
|
|
|
// appears in that stream and an EOF occurs before the buffer is full.
|
|
|
|
fn test_read_bytes_into_newline_binary() ? {
|
|
|
|
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
2022-04-15 15:35:35 +03:00
|
|
|
mut bw := []u8{len: 15}
|
2021-05-21 13:18:08 +03:00
|
|
|
bw[9] = 0xff
|
|
|
|
bw[12] = 10 // newline
|
|
|
|
|
|
|
|
n0_bytes := bw[0..10]
|
|
|
|
n1_bytes := bw[10..13]
|
|
|
|
n2_bytes := bw[13..]
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write(bw)?
|
2021-05-21 13:18:08 +03:00
|
|
|
f.close()
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 10}
|
2021-05-21 13:18:08 +03:00
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n0 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n0 == 10
|
|
|
|
assert buf[..n0] == n0_bytes
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n1 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n1 == 3
|
|
|
|
assert buf[..n1] == n1_bytes
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
n2 := f.read_bytes_into_newline(mut buf)?
|
2021-05-21 13:18:08 +03:00
|
|
|
assert n2 == 2
|
|
|
|
assert buf[..n2] == n2_bytes
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:48:55 +03:00
|
|
|
// test_read_eof_last_read_partial_buffer_fill tests that when reading a file
|
|
|
|
// the end-of-file is detected and results in a none error being returned. This
|
|
|
|
// test simulates file reading where the end-of-file is reached inside an fread
|
|
|
|
// containing data.
|
|
|
|
fn test_read_eof_last_read_partial_buffer_fill() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
2022-04-15 15:35:35 +03:00
|
|
|
bw := []u8{len: 199, init: 5}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.write(bw)?
|
2021-05-13 09:48:55 +03:00
|
|
|
f.close()
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2022-04-15 15:35:35 +03:00
|
|
|
mut br := []u8{len: 100}
|
2021-05-13 09:48:55 +03:00
|
|
|
// Read first 100 bytes of 199 byte file, should fill buffer with no error.
|
2022-08-08 02:33:25 +03:00
|
|
|
n0 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
2021-05-13 09:48:55 +03:00
|
|
|
assert n0 == 100
|
|
|
|
// Read remaining 99 bytes of 199 byte file, should fill buffer with no
|
|
|
|
// error, even though end-of-file was reached.
|
2022-08-08 02:33:25 +03:00
|
|
|
n1 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
2021-05-13 09:48:55 +03:00
|
|
|
assert n1 == 99
|
|
|
|
// Read again, end-of-file was previously reached so should return none
|
|
|
|
// error.
|
|
|
|
if _ := f.read(mut br) {
|
|
|
|
// This is not intended behavior because the read function should
|
|
|
|
// not return a number of bytes read when end-of-file is reached.
|
|
|
|
assert false
|
|
|
|
} else {
|
2022-08-08 02:33:25 +03:00
|
|
|
// Expected an error when received end-of-file.
|
|
|
|
assert err !is none
|
2021-05-13 09:48:55 +03:00
|
|
|
}
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test_read_eof_last_read_full_buffer_fill tests that when reading a file the
|
|
|
|
// end-of-file is detected and results in a none error being returned. This test
|
|
|
|
// simulates file reading where the end-of-file is reached at the beinning of an
|
|
|
|
// fread that returns no data.
|
|
|
|
fn test_read_eof_last_read_full_buffer_fill() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
2022-04-15 15:35:35 +03:00
|
|
|
bw := []u8{len: 200, init: 5}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.write(bw)?
|
2021-05-13 09:48:55 +03:00
|
|
|
f.close()
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2022-04-15 15:35:35 +03:00
|
|
|
mut br := []u8{len: 100}
|
2021-05-13 09:48:55 +03:00
|
|
|
// Read first 100 bytes of 200 byte file, should fill buffer with no error.
|
2022-08-08 02:33:25 +03:00
|
|
|
n0 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
2021-05-13 09:48:55 +03:00
|
|
|
assert n0 == 100
|
|
|
|
// Read remaining 100 bytes of 200 byte file, should fill buffer with no
|
|
|
|
// error. The end-of-file isn't reached yet, but there is no more data.
|
2022-08-08 02:33:25 +03:00
|
|
|
n1 := f.read(mut br) or { return error('failed to read 100 bytes') }
|
2021-05-13 09:48:55 +03:00
|
|
|
assert n1 == 100
|
|
|
|
// Read again, end-of-file was previously reached so should return none
|
|
|
|
// error.
|
|
|
|
if _ := f.read(mut br) {
|
|
|
|
// This is not intended behavior because the read function should
|
|
|
|
// not return a number of bytes read when end-of-file is reached.
|
|
|
|
assert false
|
|
|
|
} else {
|
2022-08-08 02:33:25 +03:00
|
|
|
// Expect an error at EOF.
|
|
|
|
assert err !is none
|
2021-05-13 09:48:55 +03:00
|
|
|
}
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
fn test_write_struct() ? {
|
|
|
|
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
2021-03-04 14:33:54 +03:00
|
|
|
size_of_point := int(sizeof(Point))
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_struct(another_point)?
|
2021-03-04 14:33:54 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
x := os.read_file(tfile)?
|
2022-04-15 14:58:56 +03:00
|
|
|
pcopy := unsafe { &u8(memdup(&another_point, size_of_point)) }
|
2021-04-05 20:47:32 +03:00
|
|
|
y := unsafe { pcopy.vstring_with_len(size_of_point) }
|
2021-03-04 14:33:54 +03:00
|
|
|
assert x == y
|
|
|
|
$if debug {
|
|
|
|
eprintln(x.bytes())
|
|
|
|
eprintln(y.bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
fn test_write_struct_at() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_struct(extended_point)?
|
|
|
|
f.write_struct_at(another_point, 3)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-03-10 19:45:12 +03:00
|
|
|
mut p := Point{}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.read_struct_at(mut p, 3)?
|
2021-03-04 14:33:54 +03:00
|
|
|
f.close()
|
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
assert p == another_point
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_struct() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_struct(another_point)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-03-04 14:33:54 +03:00
|
|
|
mut p := Point{}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.read_struct(mut p)?
|
2021-03-04 14:33:54 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
assert p == another_point
|
|
|
|
}
|
2021-03-06 21:44:53 +03:00
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
fn test_read_struct_at() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write([u8(1), 2, 3])?
|
|
|
|
f.write_struct(another_point)?
|
2021-03-06 21:44:53 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-03-06 21:44:53 +03:00
|
|
|
mut p := Point{}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.read_struct_at(mut p, 3)?
|
2021-03-06 21:44:53 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
assert p == another_point
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:45:12 +03:00
|
|
|
fn test_write_raw() ? {
|
|
|
|
os.rm(tfile) or {} // FIXME This is a workaround for macos, because the file isn't truncated when open with 'w'
|
|
|
|
size_of_point := int(sizeof(Point))
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_raw(another_point)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
x := os.read_file(tfile)?
|
2022-04-15 14:58:56 +03:00
|
|
|
pcopy := unsafe { &u8(memdup(&another_point, size_of_point)) }
|
2021-04-05 20:47:32 +03:00
|
|
|
y := unsafe { pcopy.vstring_with_len(size_of_point) }
|
2021-03-10 19:45:12 +03:00
|
|
|
assert x == y
|
|
|
|
$if debug {
|
|
|
|
eprintln(x.bytes())
|
|
|
|
eprintln(y.bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_raw_at() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_raw(extended_point)?
|
|
|
|
f.write_raw_at(another_point, 3)?
|
2021-03-06 21:44:53 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-03-06 21:44:53 +03:00
|
|
|
mut p := Point{}
|
2022-05-13 06:56:21 +03:00
|
|
|
f.read_struct_at(mut p, 3)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
assert p == another_point
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_raw_at_negative_pos() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
2021-03-10 19:45:12 +03:00
|
|
|
if _ := f.write_raw_at(another_point, -1) {
|
|
|
|
assert false
|
|
|
|
}
|
2022-02-11 16:52:33 +03:00
|
|
|
f.write_raw_at(another_point, -234) or { assert err.msg() == 'Invalid argument' }
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_raw() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_raw(another_point)?
|
|
|
|
f.write_raw(another_byte)?
|
|
|
|
f.write_raw(another_color)?
|
|
|
|
f.write_raw(another_permission)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
|
|
|
p := f.read_raw<Point>()?
|
|
|
|
b := f.read_raw<u8>()?
|
|
|
|
c := f.read_raw<Color>()?
|
|
|
|
x := f.read_raw<Permissions>()?
|
2021-03-06 21:44:53 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
assert p == another_point
|
2021-03-10 19:45:12 +03:00
|
|
|
assert b == another_byte
|
|
|
|
assert c == another_color
|
|
|
|
assert x == another_permission
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_raw_at() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write([u8(1), 2, 3])?
|
|
|
|
f.write_raw(another_point)?
|
|
|
|
f.write_raw(another_byte)?
|
|
|
|
f.write_raw(another_color)?
|
|
|
|
f.write_raw(another_permission)?
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-07-05 10:14:00 +03:00
|
|
|
mut at := u64(3)
|
2022-05-13 06:56:21 +03:00
|
|
|
p := f.read_raw_at<Point>(at)?
|
2021-07-05 10:14:00 +03:00
|
|
|
at += sizeof(Point)
|
2022-05-13 06:56:21 +03:00
|
|
|
b := f.read_raw_at<u8>(at)?
|
2022-04-15 18:25:45 +03:00
|
|
|
at += sizeof(u8)
|
2022-05-13 06:56:21 +03:00
|
|
|
c := f.read_raw_at<Color>(at)?
|
2021-07-05 10:14:00 +03:00
|
|
|
at += sizeof(Color)
|
2022-05-13 06:56:21 +03:00
|
|
|
x := f.read_raw_at<Permissions>(at)?
|
2021-07-05 10:14:00 +03:00
|
|
|
at += sizeof(Permissions)
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
assert p == another_point
|
|
|
|
assert b == another_byte
|
|
|
|
assert c == another_color
|
|
|
|
assert x == another_permission
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_raw_at_negative_pos() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'r')?
|
2021-03-10 19:45:12 +03:00
|
|
|
if _ := f.read_raw_at<Point>(-1) {
|
|
|
|
assert false
|
|
|
|
}
|
2022-02-11 16:52:33 +03:00
|
|
|
f.read_raw_at<Point>(-234) or { assert err.msg() == 'Invalid argument' }
|
2021-03-10 19:45:12 +03:00
|
|
|
f.close()
|
2021-03-06 21:44:53 +03:00
|
|
|
}
|
2021-07-22 07:46:21 +03:00
|
|
|
|
|
|
|
fn test_seek() ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'w')?
|
|
|
|
f.write_raw(another_point)?
|
|
|
|
f.write_raw(another_byte)?
|
|
|
|
f.write_raw(another_color)?
|
|
|
|
f.write_raw(another_permission)?
|
2021-07-22 07:46:21 +03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
// println('> ${sizeof(Point)} ${sizeof(byte)} ${sizeof(Color)} ${sizeof(Permissions)}')
|
2022-05-13 06:56:21 +03:00
|
|
|
f = os.open_file(tfile, 'r')?
|
2021-07-22 16:25:23 +03:00
|
|
|
//
|
2022-05-13 06:56:21 +03:00
|
|
|
f.seek(i64(sizeof(Point)), .start)?
|
|
|
|
assert f.tell()? == sizeof(Point)
|
|
|
|
b := f.read_raw<u8>()?
|
2021-07-22 16:25:23 +03:00
|
|
|
assert b == another_byte
|
2021-07-22 07:46:21 +03:00
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
f.seek(i64(sizeof(Color)), .current)?
|
|
|
|
x := f.read_raw<Permissions>()?
|
2021-07-22 16:25:23 +03:00
|
|
|
assert x == another_permission
|
|
|
|
//
|
|
|
|
f.close()
|
|
|
|
}
|
2021-07-22 07:46:21 +03:00
|
|
|
|
2021-07-22 16:25:23 +03:00
|
|
|
fn test_tell() ? {
|
|
|
|
for size in 10 .. 30 {
|
|
|
|
s := 'x'.repeat(size)
|
2022-05-13 06:56:21 +03:00
|
|
|
os.write_file(tfile, s)?
|
2021-07-22 16:25:23 +03:00
|
|
|
fs := os.file_size(tfile)
|
2021-07-22 18:47:23 +03:00
|
|
|
assert int(fs) == size
|
2021-07-22 16:25:23 +03:00
|
|
|
//
|
2022-05-13 06:56:21 +03:00
|
|
|
mut f := os.open_file(tfile, 'r')?
|
|
|
|
f.seek(-5, .end)?
|
|
|
|
pos := f.tell()?
|
2021-07-22 16:25:23 +03:00
|
|
|
f.close()
|
|
|
|
// dump(pos)
|
|
|
|
assert pos == size - 5
|
2021-07-22 13:28:00 +03:00
|
|
|
}
|
2021-07-22 07:46:21 +03:00
|
|
|
}
|
2022-07-28 16:21:23 +03:00
|
|
|
|
|
|
|
fn test_reopen() ? {
|
|
|
|
tfile1 := os.join_path_single(tfolder, 'tfile1')
|
|
|
|
tfile2 := os.join_path_single(tfolder, 'tfile2')
|
|
|
|
os.write_file(tfile1, 'Hello World!\nGood\r morning.\nBye 1.')?
|
|
|
|
os.write_file(tfile2, 'Another file\nAnother line.\nBye 2.')?
|
|
|
|
assert os.file_size(tfile1) > 0
|
|
|
|
assert os.file_size(tfile2) > 0
|
|
|
|
|
|
|
|
mut line_buffer := []u8{len: 1024}
|
|
|
|
|
|
|
|
mut f2 := os.open(tfile2)?
|
|
|
|
x := f2.read_bytes_into_newline(mut line_buffer)?
|
|
|
|
assert !f2.eof()
|
|
|
|
assert x > 0
|
|
|
|
assert line_buffer#[..x].bytestr() == 'Another file\n'
|
|
|
|
|
|
|
|
// Note: after this call, f2 should be using the file `tfile1`:
|
|
|
|
f2.reopen(tfile1, 'r')?
|
|
|
|
assert !f2.eof()
|
|
|
|
|
2022-08-14 09:50:05 +03:00
|
|
|
z := f2.read(mut line_buffer) or { panic(err) }
|
2022-07-28 16:21:23 +03:00
|
|
|
assert f2.eof()
|
|
|
|
assert z > 0
|
|
|
|
content := line_buffer#[..z].bytestr()
|
|
|
|
// dump(content)
|
|
|
|
assert content.starts_with('Hello World')
|
|
|
|
assert content.ends_with('Bye 1.')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_eof() ? {
|
|
|
|
os.write_file(tfile, 'Hello World!\n')?
|
|
|
|
|
|
|
|
mut f := os.open(tfile)?
|
|
|
|
f.read_bytes(10)
|
|
|
|
assert !f.eof()
|
|
|
|
f.read_bytes(100)
|
|
|
|
assert f.eof()
|
|
|
|
}
|
2022-08-14 09:50:05 +03:00
|
|
|
|
|
|
|
fn test_open_file_wb_ab() ? {
|
|
|
|
os.rm(tfile) or {}
|
|
|
|
mut wfile := os.open_file('text.txt', 'wb', 0o666)?
|
|
|
|
wfile.write_string('hello')?
|
|
|
|
wfile.close()
|
|
|
|
assert os.read_file('text.txt')? == 'hello'
|
|
|
|
//
|
|
|
|
mut afile := os.open_file('text.txt', 'ab', 0o666)?
|
|
|
|
afile.write_string('hello')?
|
|
|
|
afile.close()
|
|
|
|
assert os.read_file('text.txt')? == 'hellohello'
|
|
|
|
}
|