aboutsummaryrefslogtreecommitdiff
path: root/test/unit/os/fileio_spec.lua
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2024-02-24 10:17:20 +0100
committerbfredl <bjorn.linse@gmail.com>2024-02-25 11:20:06 +0100
commit77e928fd3e92f35182237b663437d7ebde7ebde7 (patch)
treed516508d050cbd7f1d25682e1764fe214e9282e9 /test/unit/os/fileio_spec.lua
parent0fcbda59871ebc5fc91cac7fa430a4a93f6698c2 (diff)
downloadrneovim-77e928fd3e92f35182237b663437d7ebde7ebde7.tar.gz
rneovim-77e928fd3e92f35182237b663437d7ebde7ebde7.tar.bz2
rneovim-77e928fd3e92f35182237b663437d7ebde7ebde7.zip
refactor(fileio): remove API shell layer encouraging unnecessary allocations
Functions like file_open_new() and file_open_fd_new() which just is a wrapper around the real functions but with an extra xmalloc/xfree around is an anti-pattern. If the caller really needs to allocate a FileDescriptor as a heap object, it can do that directly. FileDescriptor by itself is pretty much a pointer, or rather two: the OS fd index and a pointer to a buffer. So most of the time an extra pointer layer is just wasteful. In the case of scriptin[curscript] in getchar.c, curscript used to mean in practice: N+1 open scripts when curscript>0 zero or one open scripts when curscript==0 Which means scriptin[0] had to be compared to NULL to disambiguate the curscript=0 case. Instead, use curscript==-1 to mean that are no script, then all pointer comparisons dissappear and we can just use an array of structs without extra pointers.
Diffstat (limited to 'test/unit/os/fileio_spec.lua')
-rw-r--r--test/unit/os/fileio_spec.lua59
1 files changed, 0 insertions, 59 deletions
diff --git a/test/unit/os/fileio_spec.lua b/test/unit/os/fileio_spec.lua
index 385f4fa4b0..cc5a0d84c3 100644
--- a/test/unit/os/fileio_spec.lua
+++ b/test/unit/os/fileio_spec.lua
@@ -55,24 +55,12 @@ local function file_open(fname, flags, mode)
return ret1, ret2
end
-local function file_open_new(fname, flags, mode)
- local ret1 = ffi.new('int[?]', 1, { 0 })
- local ret2 = ffi.gc(m.file_open_new(ret1, fname, flags, mode), nil)
- return ret1[0], ret2
-end
-
local function file_open_fd(fd, flags)
local ret2 = ffi.new('FileDescriptor')
local ret1 = m.file_open_fd(ret2, fd, flags)
return ret1, ret2
end
-local function file_open_fd_new(fd, flags)
- local ret1 = ffi.new('int[?]', 1, { 0 })
- local ret2 = ffi.gc(m.file_open_fd_new(ret1, fd, flags), nil)
- return ret1[0], ret2
-end
-
local function file_write(fp, buf)
return m.file_write(fp, buf, #buf)
end
@@ -131,26 +119,6 @@ describe('file_open_fd', function()
end)
end)
-describe('file_open_fd_new', function()
- itp('can use file descriptor returned by os_open for reading', function()
- local fd = m.os_open(file1, m.kO_RDONLY, 0)
- local err, fp = file_open_fd_new(fd, m.kFileReadOnly)
- eq(0, err)
- eq({ #fcontents, fcontents }, { file_read(fp, #fcontents) })
- eq(0, m.file_free(fp, false))
- end)
- itp('can use file descriptor returned by os_open for writing', function()
- eq(nil, uv.fs_stat(filec))
- local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
- local err, fp = file_open_fd_new(fd, m.kFileWriteOnly)
- eq(0, err)
- eq(4, file_write(fp, 'test'))
- eq(0, m.file_free(fp, false))
- eq(4, uv.fs_stat(filec).size)
- eq('test', io.open(filec):read('*a'))
- end)
-end)
-
describe('file_open', function()
itp('can create a rwx------ file with kFileCreate', function()
local err, fp = file_open(filec, m.kFileCreate, 448)
@@ -276,21 +244,6 @@ describe('file_open', function()
end)
end)
-describe('file_open_new', function()
- itp('can open a file read-only', function()
- local err, fp = file_open_new(file1, 0, 384)
- eq(0, err)
- eq(false, fp.wr)
- eq(0, m.file_free(fp, false))
- end)
-
- itp('fails to open an existing file with kFileCreateOnly', function()
- local err, fp = file_open_new(file1, m.kFileCreateOnly, 384)
- eq(m.UV_EEXIST, err)
- eq(nil, fp)
- end)
-end)
-
describe('file_close', function()
itp('can flush writes to disk also with true argument', function()
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
@@ -303,18 +256,6 @@ describe('file_close', function()
end)
end)
-describe('file_free', function()
- itp('can flush writes to disk also with true argument', function()
- local err, fp = file_open_new(filec, m.kFileCreateOnly, 384)
- eq(0, err)
- local wsize = file_write(fp, 'test')
- eq(4, wsize)
- eq(0, uv.fs_stat(filec).size)
- eq(0, m.file_free(fp, true))
- eq(wsize, uv.fs_stat(filec).size)
- end)
-end)
-
describe('file_fsync', function()
itp('can flush writes to disk', function()
local err, fp = file_open(filec, m.kFileCreateOnly, 384)