robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
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 () 14local function test_xstrlcat ( dst , src , dsize ) 15t . ok ( dsize >= 1 + string . len ( dst )) -- sanity check for tests 16local dst_cstr =cstr ( dsize , dst ) 17local src_cstr =to_cstr ( src ) 18eq ( string . len ( dst .. src ), cimp . xstrlcat ( dst_cstr , src_cstr , dsize )) 19return ffi . string ( dst_cstr ) 20end 21 22local function test_xstrlcat_overlap ( dst , src_idx , dsize ) 23t . ok ( dsize >= 1 + string . len ( dst )) -- sanity check for tests 24local dst_cstr =cstr ( dsize , dst ) 25local src_cstr =dst_cstr + src_idx -- pointer into `dst` (overlaps) 26eq ( string . len ( dst ) + string . len ( dst ) - src_idx , cimp . xstrlcat ( dst_cstr , src_cstr , dsize )) 27return ffi . string ( dst_cstr ) 28end 29 30itp ( 'concatenates strings' , function () 31eq ( 'ab' , test_xstrlcat ( 'a' , 'b' , 3 )) 32eq ( 'ab' , test_xstrlcat ( 'a' , 'b' , 4096 )) 33eq ( 'ABCיהZdefgiיהZ' , test_xstrlcat ( 'ABCיהZ' , 'defgiיהZ' , 4096 )) 34eq ( 'b' , test_xstrlcat ( '' , 'b' , 4096 )) 35eq ( 'a' , test_xstrlcat ( 'a' , '' , 4096 )) 36end ) 37 38itp ( 'concatenates overlapping strings' , function () 39eq ( 'abcabc' , test_xstrlcat_overlap ( 'abc' , 0 , 7 )) 40eq ( 'abca' , test_xstrlcat_overlap ( 'abc' , 0 , 5 )) 41eq ( 'abcb' , test_xstrlcat_overlap ( 'abc' , 1 , 5 )) 42eq ( 'abcc' , test_xstrlcat_overlap ( 'abc' , 2 , 10 )) 43eq ( 'abcabc' , test_xstrlcat_overlap ( 'abc' , 0 , 2343 )) 44end ) 45 46itp ( 'truncates if `dsize` is too small' , function () 47eq ( 'a' , test_xstrlcat ( 'a' , 'b' , 2 )) 48eq ( '' , test_xstrlcat ( '' , 'b' , 1 )) 49eq ( 'ABCיהZd' , test_xstrlcat ( 'ABCיהZ' , 'defgiיהZ' , 10 )) 50end ) 51end )