unittests: Free everything and check for memory leaks

Also improves error reporting.
This commit is contained in:
ZyX 2017-10-16 03:06:34 +03:00
parent c03dc13bb7
commit 252a76db80
2 changed files with 45 additions and 22 deletions

View File

@ -315,7 +315,7 @@ local function alloc_log_new()
eq(exp, self.log)
self:clear()
end
function log:clear_tmp_allocs()
function log:clear_tmp_allocs(clear_null_frees)
local toremove = {}
local allocs = {}
for i, v in ipairs(self.log) do
@ -327,6 +327,8 @@ local function alloc_log_new()
if v.func == 'free' then
toremove[#toremove + 1] = i
end
elseif clear_null_frees and v.args[1] == self.null then
toremove[#toremove + 1] = i
end
if v.func == 'realloc' then
allocs[tostring(v.ret)] = i
@ -779,6 +781,12 @@ local function kvi_init(kvi)
return kvi
end
local function kvi_destroy(kvi)
if kvi.items ~= kvi.init_array then
lib.xfree(kvi.items)
end
end
local function kvi_new(ct)
return kvi_init(ffi.new(ct))
end
@ -830,6 +838,7 @@ local module = {
sc = sc,
conv_enum = conv_enum,
array_size = array_sive,
kvi_destroy = kvi_destroy,
kvi_size = kvi_size,
kvi_init = kvi_init,
kvi_new = kvi_new,

View File

@ -5,6 +5,8 @@ local viml_helpers = require('test.unit.viml.helpers')
local make_enum_conv_tab = helpers.make_enum_conv_tab
local child_call_once = helpers.child_call_once
local alloc_log_new = helpers.alloc_log_new
local kvi_destroy = helpers.kvi_destroy
local conv_enum = helpers.conv_enum
local ptr2key = helpers.ptr2key
local cimport = helpers.cimport
@ -23,6 +25,8 @@ local format_luav = global_helpers.format_luav
local lib = cimport('./src/nvim/viml/parser/expressions.h')
local alloc_log = alloc_log_new()
local function format_check(expr, flags, ast, hls)
-- That forces specific order.
print( format_string('\ncheck_parsing(%r, %u, {', expr, flags))
@ -230,30 +234,40 @@ end)
describe('Expressions parser', function()
local function check_parsing(str, flags, exp_ast, exp_highlighting_fs)
flags = flags or 0
local err, msg = pcall(function()
flags = flags or 0
if os.getenv('NVIM_TEST_PARSER_SPEC_PRINT_TEST_CASE') == '1' then
print(str, flags)
end
local pstate = new_pstate({str})
local east = lib.viml_pexpr_parse(pstate, flags)
local ast = east2lua(pstate, east)
local hls = phl2lua(pstate)
if exp_ast == nil then
format_check(str, flags, ast, hls)
return
end
eq(exp_ast, ast)
if exp_highlighting_fs then
local exp_highlighting = {}
local next_col = 0
for i, h in ipairs(exp_highlighting_fs) do
exp_highlighting[i], next_col = h(next_col)
if os.getenv('NVIM_TEST_PARSER_SPEC_PRINT_TEST_CASE') == '1' then
print(str, flags)
end
eq(exp_highlighting, hls)
alloc_log:check({})
local pstate = new_pstate({str})
local east = lib.viml_pexpr_parse(pstate, flags)
local ast = east2lua(pstate, east)
local hls = phl2lua(pstate)
if exp_ast == nil then
format_check(str, flags, ast, hls)
return
end
eq(exp_ast, ast)
if exp_highlighting_fs then
local exp_highlighting = {}
local next_col = 0
for i, h in ipairs(exp_highlighting_fs) do
exp_highlighting[i], next_col = h(next_col)
end
eq(exp_highlighting, hls)
end
lib.viml_pexpr_free_ast(east)
kvi_destroy(pstate.colors)
alloc_log:clear_tmp_allocs(true)
alloc_log:check({})
end)
if not err then
msg = format_string('Error while processing test (%r, %u):\n%s', str, flags, msg)
error(msg)
end
lib.viml_pexpr_free_ast(east)
end
local function hl(group, str, shift)
return function(next_col)