robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.5 KiB129 linesraw
1local t = require('test.unit.testutil')
2local describe, before_each, after_each = t.describe, t.before_each, t.after_each
3local itp = t.gen_itp(t.it)
4local t_eval = require('test.unit.eval.testutil')
5
6local alloc_log_new = t.alloc_log_new
7local cimport = t.cimport
8local ffi = t.ffi
9local eq = t.eq
10
11local a = t_eval.alloc_logging_t
12local type_key = t_eval.type_key
13local list_type = t_eval.list_type
14local list_items = t_eval.list_items
15local dict_items = t_eval.dict_items
16local lua2typvalt = t_eval.lua2typvalt
17
18local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/eval.h')
19
20local alloc_log = alloc_log_new()
21
22before_each(function()
23  alloc_log:before_each()
24end)
25
26after_each(function()
27  alloc_log:after_each()
28end)
29
30describe('tv_clear()', function()
31  itp('successfully frees all lists in [&l [1], *l, *l]', function()
32    local l_inner = { 1 }
33    local list = { l_inner, l_inner, l_inner }
34    local list_tv = ffi.gc(lua2typvalt(list), nil)
35    local list_p = list_tv.vval.v_list
36    local lis = list_items(list_p)
37    local list_inner_p = lis[1].li_tv.vval.v_list
38    local lis_inner = list_items(list_inner_p)
39    alloc_log:check({
40      a.list(list_p),
41      a.list(list_inner_p),
42      a.li(lis_inner[1]),
43      a.li(lis[1]),
44      a.li(lis[2]),
45      a.li(lis[3]),
46    })
47    eq(3, list_inner_p.lv_refcount)
48    lib.tv_clear(list_tv)
49    alloc_log:check({
50      a.freed(lis_inner[1]),
51      a.freed(list_inner_p),
52      a.freed(lis[1]),
53      a.freed(lis[2]),
54      a.freed(lis[3]),
55      a.freed(list_p),
56    })
57  end)
58  itp('successfully frees all lists in [&l [], *l, *l]', function()
59    local l_inner = { [type_key] = list_type }
60    local list = { l_inner, l_inner, l_inner }
61    local list_tv = ffi.gc(lua2typvalt(list), nil)
62    local list_p = list_tv.vval.v_list
63    local lis = list_items(list_p)
64    local list_inner_p = lis[1].li_tv.vval.v_list
65    alloc_log:check({
66      a.list(list_p),
67      a.list(list_inner_p),
68      a.li(lis[1]),
69      a.li(lis[2]),
70      a.li(lis[3]),
71    })
72    eq(3, list_inner_p.lv_refcount)
73    lib.tv_clear(list_tv)
74    alloc_log:check({
75      a.freed(list_inner_p),
76      a.freed(lis[1]),
77      a.freed(lis[2]),
78      a.freed(lis[3]),
79      a.freed(list_p),
80    })
81  end)
82  itp('successfully frees all dictionaries in [&d {}, *d]', function()
83    local d_inner = {}
84    local list = { d_inner, d_inner }
85    local list_tv = ffi.gc(lua2typvalt(list), nil)
86    local list_p = list_tv.vval.v_list
87    local lis = list_items(list_p)
88    local dict_inner_p = lis[1].li_tv.vval.v_dict
89    alloc_log:check({
90      a.list(list_p),
91      a.dict(dict_inner_p),
92      a.li(lis[1]),
93      a.li(lis[2]),
94    })
95    eq(2, dict_inner_p.dv_refcount)
96    lib.tv_clear(list_tv)
97    alloc_log:check({
98      a.freed(dict_inner_p),
99      a.freed(lis[1]),
100      a.freed(lis[2]),
101      a.freed(list_p),
102    })
103  end)
104  itp('successfully frees all dictionaries in [&d {a: 1}, *d]', function()
105    local d_inner = { a = 1 }
106    local list = { d_inner, d_inner }
107    local list_tv = ffi.gc(lua2typvalt(list), nil)
108    local list_p = list_tv.vval.v_list
109    local lis = list_items(list_p)
110    local dict_inner_p = lis[1].li_tv.vval.v_dict
111    local dis = dict_items(dict_inner_p)
112    alloc_log:check({
113      a.list(list_p),
114      a.dict(dict_inner_p),
115      a.di(dis.a, 1),
116      a.li(lis[1]),
117      a.li(lis[2]),
118    })
119    eq(2, dict_inner_p.dv_refcount)
120    lib.tv_clear(list_tv)
121    alloc_log:check({
122      a.freed(dis.a),
123      a.freed(dict_inner_p),
124      a.freed(lis[1]),
125      a.freed(lis[2]),
126      a.freed(list_p),
127    })
128  end)
129end)