neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

memory_spec.lua (1778B)


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