robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
1.7 KiB51 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local cimport = t.cimport
6local cstr = t.cstr
7local eq = t.eq
8local ffi = t.ffi
9local to_cstr = t.to_cstr
10
11local cimp = cimport('stdlib.h', './src/nvim/memory.h')
12
13describe('xstrlcat()', function()
14  local function test_xstrlcat(dst, src, dsize)
15    t.ok(dsize >= 1 + string.len(dst)) -- sanity check for tests
16    local dst_cstr = cstr(dsize, dst)
17    local src_cstr = to_cstr(src)
18    eq(string.len(dst .. src), cimp.xstrlcat(dst_cstr, src_cstr, dsize))
19    return ffi.string(dst_cstr)
20  end
21
22  local function test_xstrlcat_overlap(dst, src_idx, dsize)
23    t.ok(dsize >= 1 + string.len(dst)) -- sanity check for tests
24    local dst_cstr = cstr(dsize, dst)
25    local src_cstr = dst_cstr + src_idx -- pointer into `dst` (overlaps)
26    eq(string.len(dst) + string.len(dst) - src_idx, cimp.xstrlcat(dst_cstr, src_cstr, dsize))
27    return ffi.string(dst_cstr)
28  end
29
30  itp('concatenates strings', function()
31    eq('ab', test_xstrlcat('a', 'b', 3))
32    eq('ab', test_xstrlcat('a', 'b', 4096))
33    eq('ABCיהZdefgiיהZ', test_xstrlcat('ABCיהZ', 'defgiיהZ', 4096))
34    eq('b', test_xstrlcat('', 'b', 4096))
35    eq('a', test_xstrlcat('a', '', 4096))
36  end)
37
38  itp('concatenates overlapping strings', function()
39    eq('abcabc', test_xstrlcat_overlap('abc', 0, 7))
40    eq('abca', test_xstrlcat_overlap('abc', 0, 5))
41    eq('abcb', test_xstrlcat_overlap('abc', 1, 5))
42    eq('abcc', test_xstrlcat_overlap('abc', 2, 10))
43    eq('abcabc', test_xstrlcat_overlap('abc', 0, 2343))
44  end)
45
46  itp('truncates if `dsize` is too small', function()
47    eq('a', test_xstrlcat('a', 'b', 2))
48    eq('', test_xstrlcat('', 'b', 1))
49    eq('ABCיהZd', test_xstrlcat('ABCיהZ', 'defgiיהZ', 10))
50  end)
51end)