memory_spec.lua 1.8 KB

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