2017-03-12 03:41:11 -07:00
|
|
|
local helpers = require('test.unit.helpers')(after_each)
|
|
|
|
local eval_helpers = require('test.unit.eval.helpers')
|
|
|
|
|
|
|
|
local itp = helpers.gen_itp(it)
|
|
|
|
|
2016-09-17 12:06:11 -07:00
|
|
|
local OK = helpers.OK
|
2017-03-12 03:41:11 -07:00
|
|
|
local eq = helpers.eq
|
|
|
|
local neq = helpers.neq
|
|
|
|
local ffi = helpers.ffi
|
|
|
|
local cimport = helpers.cimport
|
2016-09-17 12:06:11 -07:00
|
|
|
local to_cstr = helpers.to_cstr
|
2017-03-12 03:41:11 -07:00
|
|
|
local alloc_log_new = helpers.alloc_log_new
|
|
|
|
|
|
|
|
local a = eval_helpers.alloc_logging_helpers
|
|
|
|
local list = eval_helpers.list
|
2016-09-17 12:06:11 -07:00
|
|
|
local lst2tbl = eval_helpers.lst2tbl
|
2017-03-12 03:41:11 -07:00
|
|
|
local type_key = eval_helpers.type_key
|
2016-09-14 09:39:19 -07:00
|
|
|
local li_alloc = eval_helpers.li_alloc
|
2016-09-16 18:25:49 -07:00
|
|
|
local int_type = eval_helpers.int_type
|
2016-09-17 12:06:11 -07:00
|
|
|
local first_di = eval_helpers.first_di
|
2017-03-12 03:41:11 -07:00
|
|
|
local dict_type = eval_helpers.dict_type
|
2016-09-14 09:39:19 -07:00
|
|
|
local list_type = eval_helpers.list_type
|
2016-09-16 18:25:49 -07:00
|
|
|
local null_list = eval_helpers.null_list
|
|
|
|
local null_dict = eval_helpers.null_dict
|
2017-03-12 03:41:11 -07:00
|
|
|
local lua2typvalt = eval_helpers.lua2typvalt
|
2016-09-14 10:32:07 -07:00
|
|
|
local typvalt2lua = eval_helpers.typvalt2lua
|
2016-09-16 18:25:49 -07:00
|
|
|
local null_string = eval_helpers.null_string
|
2017-03-12 03:41:11 -07:00
|
|
|
|
2016-09-17 12:06:11 -07:00
|
|
|
local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h',
|
|
|
|
'./src/nvim/mbyte.h')
|
2017-03-12 03:41:11 -07:00
|
|
|
|
|
|
|
local function list_items(l)
|
|
|
|
local lis = {}
|
|
|
|
local li = l.lv_first
|
|
|
|
for i = 1, l.lv_len do
|
2016-09-14 09:39:19 -07:00
|
|
|
lis[i] = ffi.gc(li, nil)
|
2017-03-12 03:41:11 -07:00
|
|
|
li = li.li_next
|
|
|
|
end
|
|
|
|
return lis
|
|
|
|
end
|
|
|
|
|
2016-09-14 09:39:19 -07:00
|
|
|
local function list_watch_alloc(li)
|
|
|
|
return ffi.cast('listwatch_T*', ffi.new('listwatch_T[1]', {{lw_item=li}}))
|
2017-03-12 03:41:11 -07:00
|
|
|
end
|
|
|
|
|
2016-09-14 09:39:19 -07:00
|
|
|
local function list_watch(l, li)
|
|
|
|
local lw = list_watch_alloc(li or l.lv_first)
|
|
|
|
lib.tv_list_watch_add(l, lw)
|
|
|
|
return lw
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_alloc_rets(exp_log, res)
|
|
|
|
for i = 1,#exp_log do
|
|
|
|
if ({malloc=true, calloc=true})[exp_log[i].func] then
|
|
|
|
res[#res + 1] = exp_log[i].ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res.freed = function(r, n) return {func='free', args={r[n]}} end
|
|
|
|
return exp_log
|
|
|
|
end
|
2017-03-12 03:41:11 -07:00
|
|
|
|
|
|
|
local to_cstr_nofree = function(v) return lib.xstrdup(v) end
|
|
|
|
|
|
|
|
local alloc_log = alloc_log_new()
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
alloc_log:before_each()
|
|
|
|
end)
|
|
|
|
|
2016-09-17 12:06:11 -07:00
|
|
|
local function clear_tmp_allocs()
|
|
|
|
local toremove = {}
|
|
|
|
local allocs = {}
|
|
|
|
for i, v in ipairs(alloc_log.log) do
|
|
|
|
if v.func == 'malloc' or v.func == 'calloc' then
|
|
|
|
allocs[tostring(v.ret)] = i
|
|
|
|
elseif v.func == 'realloc' or v.func == 'free' then
|
|
|
|
if allocs[tostring(v.args[1])] then
|
|
|
|
toremove[#toremove + 1] = allocs[tostring(v.args[1])]
|
|
|
|
if v.func == 'free' then
|
|
|
|
toremove[#toremove + 1] = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if v.func == 'realloc' then
|
|
|
|
allocs[tostring(v.ret)] = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(toremove)
|
|
|
|
for i = #toremove,1,-1 do
|
|
|
|
table.remove(alloc_log.log, toremove[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-12 03:41:11 -07:00
|
|
|
after_each(function()
|
|
|
|
alloc_log:after_each()
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('typval.c', function()
|
|
|
|
describe('list', function()
|
|
|
|
describe('item', function()
|
|
|
|
describe('alloc()/free()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local li = li_alloc(true)
|
|
|
|
neq(nil, li)
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
itp('also frees the value', function()
|
|
|
|
local li
|
|
|
|
local s
|
|
|
|
local l
|
|
|
|
local tv
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv.v_type = lib.VAR_NUMBER
|
|
|
|
li.li_tv.vval.v_number = 10
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv.v_type = lib.VAR_FLOAT
|
|
|
|
li.li_tv.vval.v_float = 10.5
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv.v_type = lib.VAR_STRING
|
|
|
|
li.li_tv.vval.v_string = nil
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.freed(alloc_log.null),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv.v_type = lib.VAR_STRING
|
|
|
|
s = to_cstr_nofree('test')
|
|
|
|
li.li_tv.vval.v_string = s
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.str(s, #('test')),
|
|
|
|
a.freed(s),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv.v_type = lib.VAR_LIST
|
2016-09-14 09:39:19 -07:00
|
|
|
l = ffi.gc(list(), nil)
|
2017-03-12 03:41:11 -07:00
|
|
|
l.lv_refcount = 2
|
|
|
|
li.li_tv.vval.v_list = l
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.list(l),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
eq(1, l.lv_refcount)
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
tv = lua2typvalt({[type_key]=dict_type})
|
|
|
|
tv.vval.v_dict.dv_refcount = 2
|
|
|
|
li.li_tv = tv
|
|
|
|
lib.tv_list_item_free(li)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(li),
|
|
|
|
a.dict(tv.vval.v_dict),
|
|
|
|
a.freed(li),
|
|
|
|
})
|
|
|
|
eq(1, tv.vval.v_dict.dv_refcount)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('remove()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l = list(1, 2, 3, 4, 5, 6, 7)
|
|
|
|
neq(nil, l)
|
|
|
|
local lis = list_items(l)
|
|
|
|
alloc_log:check({
|
|
|
|
a.list(l),
|
|
|
|
a.li(lis[1]),
|
|
|
|
a.li(lis[2]),
|
|
|
|
a.li(lis[3]),
|
|
|
|
a.li(lis[4]),
|
|
|
|
a.li(lis[5]),
|
|
|
|
a.li(lis[6]),
|
|
|
|
a.li(lis[7]),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[1])
|
|
|
|
alloc_log:check({
|
|
|
|
a.freed(table.remove(lis, 1)),
|
|
|
|
})
|
|
|
|
eq(lis, list_items(l))
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[6])
|
|
|
|
alloc_log:check({
|
|
|
|
a.freed(table.remove(lis)),
|
|
|
|
})
|
|
|
|
eq(lis, list_items(l))
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[3])
|
|
|
|
alloc_log:check({
|
|
|
|
a.freed(table.remove(lis, 3)),
|
|
|
|
})
|
|
|
|
eq(lis, list_items(l))
|
|
|
|
end)
|
|
|
|
itp('works and adjusts watchers correctly', function()
|
|
|
|
local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
|
|
|
|
neq(nil, l)
|
|
|
|
local lis = list_items(l)
|
|
|
|
-- Three watchers: pointing to first, middle and last elements.
|
2016-09-14 09:39:19 -07:00
|
|
|
local lws = {
|
|
|
|
list_watch(l, lis[1]),
|
|
|
|
list_watch(l, lis[4]),
|
|
|
|
list_watch(l, lis[7]),
|
|
|
|
}
|
2017-03-12 03:41:11 -07:00
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[4])
|
2016-09-14 09:39:19 -07:00
|
|
|
ffi.gc(lis[4], lib.tv_list_item_free)
|
2017-03-12 03:41:11 -07:00
|
|
|
eq({lis[1], lis[5], lis[7]}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item})
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[2])
|
2016-09-14 09:39:19 -07:00
|
|
|
ffi.gc(lis[2], lib.tv_list_item_free)
|
2017-03-12 03:41:11 -07:00
|
|
|
eq({lis[1], lis[5], lis[7]}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item})
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[7])
|
2016-09-14 09:39:19 -07:00
|
|
|
ffi.gc(lis[7], lib.tv_list_item_free)
|
2017-03-12 03:41:11 -07:00
|
|
|
eq({lis[1], lis[5], nil}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil})
|
|
|
|
|
|
|
|
lib.tv_list_item_remove(l, lis[1])
|
2016-09-14 09:39:19 -07:00
|
|
|
ffi.gc(lis[1], lib.tv_list_item_free)
|
2017-03-12 03:41:11 -07:00
|
|
|
eq({lis[3], lis[5], nil}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil})
|
|
|
|
|
2016-09-14 09:39:19 -07:00
|
|
|
alloc_log:clear()
|
2017-03-12 03:41:11 -07:00
|
|
|
lib.tv_list_free(l)
|
2016-09-14 09:39:19 -07:00
|
|
|
alloc_log:check({
|
|
|
|
a.freed(lis[3]),
|
|
|
|
a.freed(lis[5]),
|
|
|
|
a.freed(lis[6]),
|
|
|
|
a.freed(l),
|
|
|
|
})
|
2017-03-12 03:41:11 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
2016-09-14 09:39:19 -07:00
|
|
|
describe('watch', function()
|
|
|
|
describe('remove()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
|
|
|
|
eq(nil, l.lv_watch)
|
|
|
|
local lw = list_watch(l)
|
|
|
|
neq(nil, l.lv_watch)
|
|
|
|
alloc_log:clear()
|
|
|
|
lib.tv_list_watch_remove(l, lw)
|
|
|
|
eq(nil, l.lv_watch)
|
|
|
|
alloc_log:check({
|
|
|
|
-- Does not free anything.
|
|
|
|
})
|
|
|
|
local lws = { list_watch(l), list_watch(l), list_watch(l) }
|
|
|
|
alloc_log:clear()
|
|
|
|
lib.tv_list_watch_remove(l, lws[2])
|
|
|
|
eq(lws[3], l.lv_watch)
|
|
|
|
eq(lws[1], l.lv_watch.lw_next)
|
|
|
|
lib.tv_list_watch_remove(l, lws[1])
|
|
|
|
eq(lws[3], l.lv_watch)
|
|
|
|
eq(nil, l.lv_watch.lw_next)
|
|
|
|
lib.tv_list_watch_remove(l, lws[3])
|
|
|
|
eq(nil, l.lv_watch)
|
|
|
|
alloc_log:check({
|
|
|
|
-- Does not free anything.
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
itp('ignores not found watchers', function()
|
|
|
|
local l = list(1, 2, 3, 4, 5, 6, 7)
|
|
|
|
local lw = list_watch_alloc()
|
|
|
|
lib.tv_list_watch_remove(l, lw)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
-- add() and fix() were tested when testing tv_list_item_remove()
|
|
|
|
describe('alloc()/free()', function()
|
|
|
|
itp('recursively frees list with', function()
|
|
|
|
local l1 = ffi.gc(list(1, 'abc'), nil)
|
|
|
|
local l2 = ffi.gc(list({[type_key]=dict_type}), nil)
|
|
|
|
local l3 = ffi.gc(list({[type_key]=list_type}), nil)
|
|
|
|
local alloc_rets = {}
|
|
|
|
alloc_log:check(get_alloc_rets({
|
|
|
|
a.list(l1),
|
|
|
|
a.li(l1.lv_first),
|
|
|
|
a.str(l1.lv_last.li_tv.vval.v_string, #('abc')),
|
|
|
|
a.li(l1.lv_last),
|
|
|
|
a.list(l2),
|
|
|
|
a.dict(l2.lv_first.li_tv.vval.v_dict),
|
|
|
|
a.li(l2.lv_first),
|
|
|
|
a.list(l3),
|
|
|
|
a.list(l3.lv_first.li_tv.vval.v_list),
|
|
|
|
a.li(l3.lv_first),
|
|
|
|
}, alloc_rets))
|
|
|
|
lib.tv_list_free(l1)
|
|
|
|
alloc_log:check({
|
|
|
|
alloc_rets:freed(2),
|
|
|
|
alloc_rets:freed(3),
|
|
|
|
alloc_rets:freed(4),
|
|
|
|
alloc_rets:freed(1),
|
|
|
|
})
|
|
|
|
lib.tv_list_free(l2)
|
|
|
|
alloc_log:check({
|
|
|
|
alloc_rets:freed(6),
|
|
|
|
alloc_rets:freed(7),
|
|
|
|
alloc_rets:freed(5),
|
|
|
|
})
|
|
|
|
lib.tv_list_free(l3)
|
|
|
|
alloc_log:check({
|
|
|
|
alloc_rets:freed(9),
|
|
|
|
alloc_rets:freed(10),
|
|
|
|
alloc_rets:freed(8),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
itp('frees all containers inside a list', function()
|
|
|
|
local l1 = ffi.gc(list('abc', {[type_key]=dict_type}, {[type_key]=list_type}), nil)
|
|
|
|
local alloc_rets = {}
|
|
|
|
alloc_log:check(get_alloc_rets({
|
|
|
|
a.list(l1),
|
|
|
|
a.str(l1.lv_first.li_tv.vval.v_string, #('abc')),
|
|
|
|
a.li(l1.lv_first),
|
|
|
|
a.dict(l1.lv_first.li_next.li_tv.vval.v_dict),
|
|
|
|
a.li(l1.lv_first.li_next),
|
|
|
|
a.list(l1.lv_last.li_tv.vval.v_list),
|
|
|
|
a.li(l1.lv_last),
|
|
|
|
}, alloc_rets))
|
|
|
|
lib.tv_list_free(l1)
|
|
|
|
alloc_log:check({
|
|
|
|
alloc_rets:freed(2),
|
|
|
|
alloc_rets:freed(3),
|
|
|
|
alloc_rets:freed(4),
|
|
|
|
alloc_rets:freed(5),
|
|
|
|
alloc_rets:freed(6),
|
|
|
|
alloc_rets:freed(7),
|
|
|
|
alloc_rets:freed(1),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('unref()', function()
|
|
|
|
itp('recursively frees list when reference count goes to 0', function()
|
|
|
|
local l = ffi.gc(list({[type_key]=list_type}), nil)
|
|
|
|
local alloc_rets = {}
|
|
|
|
alloc_log:check(get_alloc_rets({
|
|
|
|
a.list(l),
|
|
|
|
a.list(l.lv_first.li_tv.vval.v_list),
|
|
|
|
a.li(l.lv_first),
|
|
|
|
}, alloc_rets))
|
|
|
|
l.lv_refcount = 2
|
|
|
|
lib.tv_list_unref(l)
|
|
|
|
alloc_log:check({})
|
|
|
|
lib.tv_list_unref(l)
|
|
|
|
alloc_log:check({
|
|
|
|
alloc_rets:freed(2),
|
|
|
|
alloc_rets:freed(3),
|
|
|
|
alloc_rets:freed(1),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
end)
|
2016-09-14 10:32:07 -07:00
|
|
|
describe('remove_items()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
local lis = list_items(l)
|
|
|
|
-- Three watchers: pointing to first, middle and last elements.
|
|
|
|
local lws = {
|
|
|
|
list_watch(l, lis[1]),
|
|
|
|
list_watch(l, lis[7]),
|
|
|
|
list_watch(l, lis[13]),
|
|
|
|
}
|
|
|
|
alloc_log:clear()
|
|
|
|
|
|
|
|
lib.tv_list_remove_items(l, lis[1], lis[3])
|
|
|
|
eq({4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, typvalt2lua(l_tv))
|
|
|
|
eq({lis[4], lis[7], lis[13]}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item})
|
|
|
|
|
|
|
|
lib.tv_list_remove_items(l, lis[11], lis[13])
|
|
|
|
eq({4, 5, 6, 7, 8, 9, 10}, typvalt2lua(l_tv))
|
|
|
|
eq({lis[4], lis[7], nil}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil})
|
|
|
|
|
|
|
|
lib.tv_list_remove_items(l, lis[6], lis[8])
|
|
|
|
eq({4, 5, 9, 10}, typvalt2lua(l_tv))
|
|
|
|
eq({lis[4], lis[9], nil}, {lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil})
|
|
|
|
|
|
|
|
lib.tv_list_remove_items(l, lis[4], lis[10])
|
|
|
|
eq({[type_key]=list_type}, typvalt2lua(l_tv))
|
|
|
|
eq({true, true, true}, {lws[1].lw_item == nil, lws[2].lw_item == nil, lws[3].lw_item == nil})
|
|
|
|
|
|
|
|
alloc_log:check({})
|
|
|
|
end)
|
|
|
|
end)
|
2016-09-16 18:25:49 -07:00
|
|
|
describe('insert', function()
|
|
|
|
describe('()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({1, 2, 3, 4, 5, 6, 7})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
local lis = list_items(l)
|
|
|
|
local li
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv = {v_type=lib.VAR_FLOAT, vval={v_float=100500}}
|
|
|
|
lib.tv_list_insert(l, li, nil)
|
|
|
|
eq(l.lv_last, li)
|
|
|
|
eq({1, 2, 3, 4, 5, 6, 7, 100500}, typvalt2lua(l_tv))
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv = {v_type=lib.VAR_FLOAT, vval={v_float=0}}
|
|
|
|
lib.tv_list_insert(l, li, lis[1])
|
|
|
|
eq(l.lv_first, li)
|
|
|
|
eq({0, 1, 2, 3, 4, 5, 6, 7, 100500}, typvalt2lua(l_tv))
|
|
|
|
|
|
|
|
li = li_alloc(true)
|
|
|
|
li.li_tv = {v_type=lib.VAR_FLOAT, vval={v_float=4.5}}
|
|
|
|
lib.tv_list_insert(l, li, lis[5])
|
|
|
|
eq(list_items(l)[6], li)
|
|
|
|
eq({0, 1, 2, 3, 4, 4.5, 5, 6, 7, 100500}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
itp('works with an empty list', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
eq(nil, l.lv_first)
|
|
|
|
eq(nil, l.lv_last)
|
|
|
|
|
|
|
|
local li = li_alloc(true)
|
|
|
|
li.li_tv = {v_type=lib.VAR_FLOAT, vval={v_float=100500}}
|
|
|
|
lib.tv_list_insert(l, li, nil)
|
|
|
|
eq(l.lv_last, li)
|
|
|
|
eq({100500}, typvalt2lua(l_tv))
|
|
|
|
end)
|
2016-09-14 10:32:07 -07:00
|
|
|
end)
|
2016-09-16 18:25:49 -07:00
|
|
|
describe('tv()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
local l_l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
alloc_log:clear()
|
|
|
|
local l_l = l_l_tv.vval.v_list
|
|
|
|
eq(1, l_l.lv_refcount)
|
|
|
|
lib.tv_list_insert_tv(l, l_l_tv, nil)
|
|
|
|
eq(2, l_l.lv_refcount)
|
|
|
|
eq(l_l, l.lv_first.li_tv.vval.v_list)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_first),
|
|
|
|
})
|
2016-09-14 10:32:07 -07:00
|
|
|
|
2016-09-16 18:25:49 -07:00
|
|
|
local l_s_tv = lua2typvalt('test')
|
|
|
|
alloc_log:check({
|
|
|
|
a.str(l_s_tv.vval.v_string, 'test'),
|
|
|
|
})
|
|
|
|
lib.tv_list_insert_tv(l, l_s_tv, l.lv_first)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_first),
|
|
|
|
a.str(l.lv_first.li_tv.vval.v_string, 'test'),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({'test', {[type_key]=list_type}}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('append', function()
|
|
|
|
describe('list()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
local l_l = list(1)
|
|
|
|
alloc_log:clear()
|
|
|
|
eq(1, l_l.lv_refcount)
|
|
|
|
lib.tv_list_append_list(l, l_l)
|
|
|
|
eq(2, l_l.lv_refcount)
|
|
|
|
eq(l_l, l.lv_first.li_tv.vval.v_list)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_list(l, nil)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({{1}, null_list}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('dict()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
local l_d_tv = lua2typvalt({test=1})
|
|
|
|
local l_d = l_d_tv.vval.v_dict
|
|
|
|
alloc_log:clear()
|
|
|
|
eq(1, l_d.dv_refcount)
|
|
|
|
lib.tv_list_append_dict(l, l_d)
|
|
|
|
eq(2, l_d.dv_refcount)
|
|
|
|
eq(l_d, l.lv_first.li_tv.vval.v_list)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
2016-09-14 10:32:07 -07:00
|
|
|
|
2016-09-16 18:25:49 -07:00
|
|
|
lib.tv_list_append_dict(l, nil)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({{test=1}, null_dict}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('string()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
alloc_log:clear()
|
|
|
|
lib.tv_list_append_string(l, 'test', 3)
|
|
|
|
alloc_log:check({
|
|
|
|
a.str(l.lv_last.li_tv.vval.v_string, 'tes'),
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_string(l, nil, 0)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_string(l, nil, -1)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_string(l, 'test', -1)
|
|
|
|
alloc_log:check({
|
|
|
|
a.str(l.lv_last.li_tv.vval.v_string, 'test'),
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({'tes', null_string, null_string, 'test'}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('allocated string()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
local s = lib.xstrdup('test')
|
|
|
|
alloc_log:clear()
|
|
|
|
lib.tv_list_append_allocated_string(l, s)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_allocated_string(l, nil)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_allocated_string(l, nil)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({'test', null_string, null_string}, typvalt2lua(l_tv))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
describe('number()', function()
|
|
|
|
itp('works', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
|
|
|
|
alloc_log:clear()
|
|
|
|
lib.tv_list_append_number(l, -100500)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
lib.tv_list_append_number(l, 100500)
|
|
|
|
alloc_log:check({
|
|
|
|
a.li(l.lv_last),
|
|
|
|
})
|
|
|
|
|
|
|
|
eq({{[type_key]=int_type, value=-100500},
|
|
|
|
{[type_key]=int_type, value=100500}}, typvalt2lua(l_tv))
|
|
|
|
end)
|
2016-09-14 10:32:07 -07:00
|
|
|
end)
|
|
|
|
end)
|
2016-09-17 12:06:11 -07:00
|
|
|
describe('copy()', function()
|
|
|
|
local function tv_list_copy(...)
|
|
|
|
return ffi.gc(lib.tv_list_copy(...), lib.tv_list_unref)
|
|
|
|
end
|
|
|
|
itp('copies NULL correctly', function()
|
|
|
|
eq(nil, lib.tv_list_copy(nil, nil, true, 0))
|
|
|
|
eq(nil, lib.tv_list_copy(nil, nil, false, 0))
|
|
|
|
eq(nil, lib.tv_list_copy(nil, nil, true, 1))
|
|
|
|
eq(nil, lib.tv_list_copy(nil, nil, false, 1))
|
|
|
|
end)
|
|
|
|
itp('copies list correctly without converting items', function()
|
|
|
|
local v = {{['«']='»'}, {'„'}, 1, '“', null_string, null_list, null_dict}
|
|
|
|
local l_tv = lua2typvalt(v)
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
local lis = list_items(l)
|
|
|
|
alloc_log:clear()
|
|
|
|
|
|
|
|
eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local l_copy1 = tv_list_copy(nil, l, false, 0)
|
|
|
|
eq(2, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(2, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local lis_copy1 = list_items(l_copy1)
|
|
|
|
eq(lis[1].li_tv.vval.v_dict, lis_copy1[1].li_tv.vval.v_dict)
|
|
|
|
eq(lis[2].li_tv.vval.v_list, lis_copy1[2].li_tv.vval.v_list)
|
|
|
|
eq(v, lst2tbl(l_copy1))
|
|
|
|
alloc_log:check({
|
|
|
|
a.list(l_copy1),
|
|
|
|
a.li(lis_copy1[1]),
|
|
|
|
a.li(lis_copy1[2]),
|
|
|
|
a.li(lis_copy1[3]),
|
|
|
|
a.li(lis_copy1[4]),
|
|
|
|
a.str(lis_copy1[4].li_tv.vval.v_string, #v[4]),
|
|
|
|
a.li(lis_copy1[5]),
|
|
|
|
a.li(lis_copy1[6]),
|
|
|
|
a.li(lis_copy1[7]),
|
|
|
|
})
|
|
|
|
lib.tv_list_free(ffi.gc(l_copy1, nil))
|
|
|
|
alloc_log:clear()
|
|
|
|
|
|
|
|
eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local l_deepcopy1 = tv_list_copy(nil, l, true, 0)
|
|
|
|
neq(nil, l_deepcopy1)
|
|
|
|
eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local lis_deepcopy1 = list_items(l_deepcopy1)
|
|
|
|
neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
|
|
|
|
neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
|
|
|
|
eq(v, lst2tbl(l_deepcopy1))
|
|
|
|
local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
|
|
|
|
alloc_log:check({
|
|
|
|
a.list(l_deepcopy1),
|
|
|
|
a.li(lis_deepcopy1[1]),
|
|
|
|
a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
|
|
|
|
a.di(di_deepcopy1, #('«')),
|
|
|
|
a.str(di_deepcopy1.di_tv.vval.v_string, #v[1]['«']),
|
|
|
|
a.li(lis_deepcopy1[2]),
|
|
|
|
a.list(lis_deepcopy1[2].li_tv.vval.v_list),
|
|
|
|
a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
|
|
|
|
a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
|
|
|
|
a.li(lis_deepcopy1[3]),
|
|
|
|
a.li(lis_deepcopy1[4]),
|
|
|
|
a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
|
|
|
|
a.li(lis_deepcopy1[5]),
|
|
|
|
a.li(lis_deepcopy1[6]),
|
|
|
|
a.li(lis_deepcopy1[7]),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
itp('copies list correctly and converts items', function()
|
|
|
|
local vc = ffi.gc(ffi.new('vimconv_T[1]'), function(vc)
|
|
|
|
lib.convert_setup(vc, nil, nil)
|
|
|
|
end)
|
|
|
|
-- UTF-8 ↔ latin1 conversions need no iconv
|
|
|
|
eq(OK, lib.convert_setup(vc, to_cstr('utf-8'), to_cstr('latin1')))
|
|
|
|
|
|
|
|
local v = {{['«']='»'}, {'„'}, 1, '“', null_string, null_list, null_dict}
|
|
|
|
local l_tv = lua2typvalt(v)
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
local lis = list_items(l)
|
|
|
|
alloc_log:clear()
|
|
|
|
|
|
|
|
eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local l_deepcopy1 = tv_list_copy(vc, l, true, 0)
|
|
|
|
neq(nil, l_deepcopy1)
|
|
|
|
eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
|
|
|
|
eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
|
|
|
|
local lis_deepcopy1 = list_items(l_deepcopy1)
|
|
|
|
neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
|
|
|
|
neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
|
|
|
|
eq({{['\171']='\187'}, {'\191'}, 1, '\191', null_string, null_list, null_dict},
|
|
|
|
lst2tbl(l_deepcopy1))
|
|
|
|
local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
|
|
|
|
clear_tmp_allocs()
|
|
|
|
alloc_log:check({
|
|
|
|
a.list(l_deepcopy1),
|
|
|
|
a.li(lis_deepcopy1[1]),
|
|
|
|
a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
|
|
|
|
a.di(di_deepcopy1, 1),
|
|
|
|
a.str(di_deepcopy1.di_tv.vval.v_string, 2),
|
|
|
|
a.li(lis_deepcopy1[2]),
|
|
|
|
a.list(lis_deepcopy1[2].li_tv.vval.v_list),
|
|
|
|
a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
|
|
|
|
a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
|
|
|
|
a.li(lis_deepcopy1[3]),
|
|
|
|
a.li(lis_deepcopy1[4]),
|
|
|
|
a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
|
|
|
|
a.li(lis_deepcopy1[5]),
|
|
|
|
a.li(lis_deepcopy1[6]),
|
|
|
|
a.li(lis_deepcopy1[7]),
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
itp('returns different/same containers with(out) copyID', function()
|
|
|
|
local l_inner_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l_tv = lua2typvalt({l_inner_tv, l_inner_tv})
|
|
|
|
eq(3, l_inner_tv.vval.v_list.lv_refcount)
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
eq(l.lv_first.li_tv.vval.v_list, l.lv_last.li_tv.vval.v_list)
|
|
|
|
|
|
|
|
local l_copy1 = tv_list_copy(nil, l, true, 0)
|
|
|
|
neq(l_copy1.lv_first.li_tv.vval.v_list, l_copy1.lv_last.li_tv.vval.v_list)
|
|
|
|
eq({{[type_key]=list_type}, {[type_key]=list_type}}, lst2tbl(l_copy1))
|
|
|
|
|
|
|
|
local l_copy2 = tv_list_copy(nil, l, true, 2)
|
|
|
|
eq(l_copy2.lv_first.li_tv.vval.v_list, l_copy2.lv_last.li_tv.vval.v_list)
|
|
|
|
eq({{[type_key]=list_type}, {[type_key]=list_type}}, lst2tbl(l_copy2))
|
|
|
|
|
|
|
|
eq(3, l_inner_tv.vval.v_list.lv_refcount)
|
|
|
|
end)
|
|
|
|
itp('works with self-referencing list with copyID', function()
|
|
|
|
local l_tv = lua2typvalt({[type_key]=list_type})
|
|
|
|
local l = l_tv.vval.v_list
|
|
|
|
eq(1, l.lv_refcount)
|
|
|
|
lib.tv_list_append_list(l, l)
|
|
|
|
eq(2, l.lv_refcount)
|
|
|
|
|
|
|
|
local l_copy1 = tv_list_copy(nil, l, true, 2)
|
|
|
|
eq(2, l_copy1.lv_refcount)
|
|
|
|
local v = {}
|
|
|
|
v[1] = v
|
|
|
|
eq(v, lst2tbl(l_copy1))
|
|
|
|
|
|
|
|
local lis = list_items(l)
|
|
|
|
lib.tv_list_item_remove(l, lis[1])
|
|
|
|
eq(1, l.lv_refcount)
|
|
|
|
|
|
|
|
local lis_copy1 = list_items(l_copy1)
|
|
|
|
lib.tv_list_item_remove(l_copy1, lis_copy1[1])
|
|
|
|
eq(1, l_copy1.lv_refcount)
|
|
|
|
end)
|
|
|
|
end)
|
2017-03-12 03:41:11 -07:00
|
|
|
end)
|
|
|
|
end)
|