2024-04-08 02:03:20 -07:00
|
|
|
|
local t = require('test.unit.testutil')(after_each)
|
|
|
|
|
local itp = t.gen_itp(it)
|
2017-01-31 09:42:22 -07:00
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
|
local cimport = t.cimport
|
|
|
|
|
local cstr = t.cstr
|
|
|
|
|
local eq = t.eq
|
|
|
|
|
local ffi = t.ffi
|
|
|
|
|
local to_cstr = t.to_cstr
|
2017-01-31 09:42:22 -07:00
|
|
|
|
|
|
|
|
|
local cimp = cimport('stdlib.h', './src/nvim/memory.h')
|
|
|
|
|
|
|
|
|
|
describe('xstrlcat()', function()
|
|
|
|
|
local function test_xstrlcat(dst, src, dsize)
|
2023-12-04 15:32:39 -07:00
|
|
|
|
assert.is_true(dsize >= 1 + string.len(dst)) -- sanity check for tests
|
2017-01-31 09:42:22 -07:00
|
|
|
|
local dst_cstr = cstr(dsize, dst)
|
|
|
|
|
local src_cstr = to_cstr(src)
|
|
|
|
|
eq(string.len(dst .. src), cimp.xstrlcat(dst_cstr, src_cstr, dsize))
|
|
|
|
|
return ffi.string(dst_cstr)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function test_xstrlcat_overlap(dst, src_idx, dsize)
|
2023-12-04 15:32:39 -07:00
|
|
|
|
assert.is_true(dsize >= 1 + string.len(dst)) -- sanity check for tests
|
2017-01-31 09:42:22 -07:00
|
|
|
|
local dst_cstr = cstr(dsize, dst)
|
2023-12-04 15:32:39 -07:00
|
|
|
|
local src_cstr = dst_cstr + src_idx -- pointer into `dst` (overlaps)
|
|
|
|
|
eq(string.len(dst) + string.len(dst) - src_idx, cimp.xstrlcat(dst_cstr, src_cstr, dsize))
|
2017-01-31 09:42:22 -07:00
|
|
|
|
return ffi.string(dst_cstr)
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
|
itp('concatenates strings', function()
|
2017-01-31 09:42:22 -07:00
|
|
|
|
eq('ab', test_xstrlcat('a', 'b', 3))
|
|
|
|
|
eq('ab', test_xstrlcat('a', 'b', 4096))
|
2023-12-04 15:32:39 -07:00
|
|
|
|
eq('ABCיהZdefgiיהZ', test_xstrlcat('ABCיהZ', 'defgiיהZ', 4096))
|
|
|
|
|
eq('b', test_xstrlcat('', 'b', 4096))
|
|
|
|
|
eq('a', test_xstrlcat('a', '', 4096))
|
2017-01-31 09:42:22 -07:00
|
|
|
|
end)
|
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
|
itp('concatenates overlapping strings', function()
|
2023-12-04 15:32:39 -07:00
|
|
|
|
eq('abcabc', test_xstrlcat_overlap('abc', 0, 7))
|
|
|
|
|
eq('abca', test_xstrlcat_overlap('abc', 0, 5))
|
|
|
|
|
eq('abcb', test_xstrlcat_overlap('abc', 1, 5))
|
|
|
|
|
eq('abcc', test_xstrlcat_overlap('abc', 2, 10))
|
|
|
|
|
eq('abcabc', test_xstrlcat_overlap('abc', 0, 2343))
|
2017-01-31 09:42:22 -07:00
|
|
|
|
end)
|
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
|
itp('truncates if `dsize` is too small', function()
|
2017-01-31 09:42:22 -07:00
|
|
|
|
eq('a', test_xstrlcat('a', 'b', 2))
|
|
|
|
|
eq('', test_xstrlcat('', 'b', 1))
|
2023-12-04 15:32:39 -07:00
|
|
|
|
eq('ABCיהZd', test_xstrlcat('ABCיהZ', 'defgiיהZ', 10))
|
2017-01-31 09:42:22 -07:00
|
|
|
|
end)
|
|
|
|
|
end)
|