unittests: Add tests for tv_clear()

This commit is contained in:
ZyX 2017-03-13 19:35:12 +03:00
parent e43de6bb3e
commit f0bbd1e825
2 changed files with 75 additions and 3 deletions

View File

@ -1840,10 +1840,11 @@ static inline void _nothing_conv_dict_end(typval_T *const tv,
/// Free memory for a variable value and set the value to NULL or 0 /// Free memory for a variable value and set the value to NULL or 0
/// ///
/// @param[in,out] tv Value to free. /// @param[in,out] tv Value to free.
void tv_clear(typval_T *tv) void tv_clear(typval_T *const tv)
{ {
if (tv != NULL && tv->v_type != VAR_UNKNOWN) { if (tv != NULL && tv->v_type != VAR_UNKNOWN) {
const int evn_ret = encode_vim_to_nothing(NULL, tv, "tv_clear argument"); const int evn_ret = encode_vim_to_nothing(NULL, tv,
_("tv_clear() argument"));
(void)evn_ret; (void)evn_ret;
assert(evn_ret == OK); assert(evn_ret == OK);
} }

View File

@ -55,12 +55,16 @@ local function list_watch(l, li)
end end
local function get_alloc_rets(exp_log, res) local function get_alloc_rets(exp_log, res)
setmetatable(res, {
__index={
freed=function(r, n) return {func='free', args={r[n]}} end
}
})
for i = 1,#exp_log do for i = 1,#exp_log do
if ({malloc=true, calloc=true})[exp_log[i].func] then if ({malloc=true, calloc=true})[exp_log[i].func] then
res[#res + 1] = exp_log[i].ret res[#res + 1] = exp_log[i].ret
end end
end end
res.freed = function(r, n) return {func='free', args={r[n]}} end
return exp_log return exp_log
end end
@ -2201,4 +2205,71 @@ describe('typval.c', function()
end) end)
end) end)
end) end)
describe('tv', function()
describe('alloc', function()
describe('list ret()', function()
itp('works', function()
local rettv = typvalt(lib.VAR_UNKNOWN)
local l = lib.tv_list_alloc_ret(rettv)
eq(empty_list, typvalt2lua(rettv))
eq(rettv.vval.v_list, l)
end)
end)
describe('dict ret()', function()
itp('works', function()
local rettv = typvalt(lib.VAR_UNKNOWN)
lib.tv_dict_alloc_ret(rettv)
eq({}, typvalt2lua(rettv))
end)
end)
end)
describe('clear()', function()
itp('works', function()
local function deffrees(alloc_rets)
local ret = {}
for i = #alloc_rets, 1, -1 do
ret[#alloc_rets - i + 1] = alloc_rets:freed(i)
end
return ret
end
local function defalloc()
return {}
end
alloc_log:check({})
lib.tv_clear(nil)
alloc_log:check({})
local ll = {}
local ll_l = nil
ll[1] = ll
local dd = {}
dd.dd = dd
for _, v in ipairs({
{nil},
{null_string, nil, function() return {a.freed(alloc_log.null)} end},
{0},
{int(0)},
{true},
{false},
{{}, function(tv) return {a.dict(tv.vval.v_dict)} end},
{empty_list, function(tv) return {a.list(tv.vval.v_list)} end},
{ll, function(tv)
ll_l = tv.vval.v_list
return {a.list(tv.vval.v_list), a.li(tv.vval.v_list.lv_first)}
end, defalloc},
{dd, function(tv)
dd_d = tv.vval.v_dict
return {a.dict(tv.vval.v_dict), a.di(first_di(tv.vval.v_dict))}
end, defalloc},
}) do
local tv = lua2typvalt(v[1])
local alloc_rets = {}
alloc_log:check(get_alloc_rets((v[2] or defalloc)(tv), alloc_rets))
lib.tv_clear(tv)
alloc_log:check((v[3] or deffrees)(alloc_rets))
end
eq(1, ll_l.lv_refcount)
eq(1, dd_d.dv_refcount)
end)
end)
end)
end) end)