memory_spec.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local t = require('test.unit.testutil')
  2. local itp = t.gen_itp(it)
  3. local cimport = t.cimport
  4. local cstr = t.cstr
  5. local eq = t.eq
  6. local ffi = t.ffi
  7. local to_cstr = t.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, cimp.xstrlcat(dst_cstr, src_cstr, dsize))
  22. return ffi.string(dst_cstr)
  23. end
  24. itp('concatenates strings', function()
  25. eq('ab', test_xstrlcat('a', 'b', 3))
  26. eq('ab', test_xstrlcat('a', 'b', 4096))
  27. eq('ABCיהZdefgiיהZ', test_xstrlcat('ABCיהZ', 'defgiיהZ', 4096))
  28. eq('b', test_xstrlcat('', 'b', 4096))
  29. eq('a', test_xstrlcat('a', '', 4096))
  30. end)
  31. itp('concatenates overlapping strings', function()
  32. eq('abcabc', test_xstrlcat_overlap('abc', 0, 7))
  33. eq('abca', test_xstrlcat_overlap('abc', 0, 5))
  34. eq('abcb', test_xstrlcat_overlap('abc', 1, 5))
  35. eq('abcc', test_xstrlcat_overlap('abc', 2, 10))
  36. eq('abcabc', test_xstrlcat_overlap('abc', 0, 2343))
  37. end)
  38. itp('truncates if `dsize` is too small', function()
  39. eq('a', test_xstrlcat('a', 'b', 2))
  40. eq('', test_xstrlcat('', 'b', 1))
  41. eq('ABCיהZd', test_xstrlcat('ABCיהZ', 'defgiיהZ', 10))
  42. end)
  43. end)