diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/eval/helpers.lua | 8 | ||||
-rw-r--r-- | test/unit/eval/typval_spec.lua | 115 |
2 files changed, 44 insertions, 79 deletions
diff --git a/test/unit/eval/helpers.lua b/test/unit/eval/helpers.lua index 1a3210e1fd..6babd4be77 100644 --- a/test/unit/eval/helpers.lua +++ b/test/unit/eval/helpers.lua @@ -28,8 +28,13 @@ local function tv_list_item_alloc() return ffi.cast('listitem_T*', eval.xmalloc(ffi.sizeof('listitem_T'))) end +local function tv_list_item_free(li) + eval.tv_clear(li.li_tv) + eval.xfree(li) +end + local function li_alloc(nogc) - local gcfunc = eval.tv_list_item_free + local gcfunc = tv_list_item_free if nogc then gcfunc = nil end local li = ffi.gc(tv_list_item_alloc(), gcfunc) li.li_next = nil @@ -537,6 +542,7 @@ return { typvalt=typvalt, li_alloc=li_alloc, + tv_list_item_free=tv_list_item_free, dict_iter=dict_iter, list_iter=list_iter, diff --git a/test/unit/eval/typval_spec.lua b/test/unit/eval/typval_spec.lua index 2ae69ec04b..0536839b71 100644 --- a/test/unit/eval/typval_spec.lua +++ b/test/unit/eval/typval_spec.lua @@ -41,6 +41,7 @@ local tbl2callback = eval_helpers.tbl2callback local dict_watchers = eval_helpers.dict_watchers local concat_tables = global_helpers.concat_tables +local map = global_helpers.map local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/memory.h', './src/nvim/mbyte.h', './src/nvim/garray.h', @@ -121,118 +122,76 @@ end describe('typval.c', function() describe('list', function() describe('item', function() - describe('alloc()/free()', function() + describe('remove()', 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) + local l = list(1, 2, 3, 4, 5, 6, 7) + neq(nil, l) + local lis = list_items(l) alloc_log:check({ - a.li(li), - a.freed(alloc_log.null), - a.freed(li), + 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]), }) - 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) + lib.tv_list_item_remove(l, lis[1]) alloc_log:check({ - a.li(li), - a.str(s, #('test')), - a.freed(s), - a.freed(li), + a.freed(table.remove(lis, 1)), }) + eq(lis, list_items(l)) - li = li_alloc(true) - li.li_tv.v_type = lib.VAR_LIST - l = ffi.gc(list(), nil) - l.lv_refcount = 2 - li.li_tv.vval.v_list = l - lib.tv_list_item_free(li) + lib.tv_list_item_remove(l, lis[6]) alloc_log:check({ - a.li(li), - a.list(l), - a.freed(li), + a.freed(table.remove(lis)), }) - eq(1, l.lv_refcount) + eq(lis, list_items(l)) - li = li_alloc(true) - tv = lua2typvalt({}) - tv.vval.v_dict.dv_refcount = 2 - li.li_tv = tv - lib.tv_list_item_free(li) + lib.tv_list_item_remove(l, lis[3]) alloc_log:check({ - a.li(li), - a.dict(tv.vval.v_dict), - a.freed(li), + a.freed(table.remove(lis, 3)), }) - eq(1, tv.vval.v_dict.dv_refcount) + eq(lis, list_items(l)) end) - end) - describe('remove()', function() - itp('works', function() - local l = list(1, 2, 3, 4, 5, 6, 7) + itp('also frees the value', function() + local l = list('a', 'b', 'c', 'd') neq(nil, l) local lis = list_items(l) alloc_log:check({ a.list(l), + a.str(lis[1].li_tv.vval.v_string, 1), a.li(lis[1]), + a.str(lis[2].li_tv.vval.v_string, 1), a.li(lis[2]), + a.str(lis[3].li_tv.vval.v_string, 1), a.li(lis[3]), + a.str(lis[4].li_tv.vval.v_string, 1), a.li(lis[4]), - a.li(lis[5]), - a.li(lis[6]), - a.li(lis[7]), }) + local strings = map(function(li) return li.li_tv.vval.v_string end, + lis) lib.tv_list_item_remove(l, lis[1]) alloc_log:check({ + a.freed(table.remove(strings, 1)), a.freed(table.remove(lis, 1)), }) eq(lis, list_items(l)) - lib.tv_list_item_remove(l, lis[6]) + lib.tv_list_item_remove(l, lis[2]) alloc_log:check({ - a.freed(table.remove(lis)), + a.freed(table.remove(strings, 2)), + a.freed(table.remove(lis, 2)), }) eq(lis, list_items(l)) - lib.tv_list_item_remove(l, lis[3]) + lib.tv_list_item_remove(l, lis[2]) alloc_log:check({ - a.freed(table.remove(lis, 3)), + a.freed(table.remove(strings, 2)), + a.freed(table.remove(lis, 2)), }) eq(lis, list_items(l)) end) |