robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
118.4 KiB3420 linesraw
1local bit = require('bit')
2local t = require('test.unit.testutil')
3local t_eval = require('test.unit.eval.testutil')
4
5local describe, before_each, after_each, pending =
6  t.describe, t.before_each, t.after_each, t.pending
7local itp = t.gen_itp(t.it)
8
9local OK = t.OK
10local eq = t.eq
11local neq = t.neq
12local ffi = t.ffi
13local FAIL = t.FAIL
14local NULL = t.NULL
15local cimport = t.cimport
16local to_cstr = t.to_cstr
17local alloc_log_new = t.alloc_log_new
18local concat_tables = t.concat_tables
19local map = vim.tbl_map
20
21local a = t_eval.alloc_logging_t
22local int = t_eval.int
23local list = t_eval.list
24local dict = t_eval.dict
25local eval0 = t_eval.eval0
26local lst2tbl = t_eval.lst2tbl
27local dct2tbl = t_eval.dct2tbl
28local typvalt = t_eval.typvalt
29local type_key = t_eval.type_key
30local li_alloc = t_eval.li_alloc
31local first_di = t_eval.first_di
32local nil_value = t_eval.nil_value
33local func_type = t_eval.func_type
34local null_list = t_eval.null_list
35local null_dict = t_eval.null_dict
36local dict_items = t_eval.dict_items
37local list_items = t_eval.list_items
38local empty_list = t_eval.empty_list
39local lua2typvalt = t_eval.lua2typvalt
40local typvalt2lua = t_eval.typvalt2lua
41local null_string = t_eval.null_string
42local callback2tbl = t_eval.callback2tbl
43local tbl2callback = t_eval.tbl2callback
44local dict_watchers = t_eval.dict_watchers
45
46local lib = cimport(
47  './src/nvim/eval/typval.h',
48  './src/nvim/memory.h',
49  './src/nvim/mbyte.h',
50  './src/nvim/garray.h',
51  './src/nvim/eval.h',
52  './src/nvim/vim_defs.h',
53  './src/nvim/globals.h'
54)
55
56local function vimconv_alloc()
57  return ffi.gc(ffi.cast('vimconv_T*', lib.xcalloc(1, ffi.sizeof('vimconv_T'))), function(vc)
58    lib.convert_setup(vc, nil, nil)
59    lib.xfree(vc)
60  end)
61end
62
63local function list_watch_alloc(li)
64  return ffi.cast('listwatch_T*', ffi.new('listwatch_T[1]', { { lw_item = li } }))
65end
66
67local function list_watch(l, li)
68  local lw = list_watch_alloc(li or l.lv_first)
69  lib.tv_list_watch_add(l, lw)
70  return lw
71end
72
73local function get_alloc_rets(exp_log, res)
74  setmetatable(res, {
75    __index = {
76      freed = function(r, n)
77        return { func = 'free', args = { r[n] } }
78      end,
79    },
80  })
81  for i = 1, #exp_log do
82    if ({ malloc = true, calloc = true })[exp_log[i].func] then
83      res[#res + 1] = exp_log[i].ret
84    end
85  end
86  return exp_log
87end
88
89local alloc_log = alloc_log_new()
90
91before_each(function()
92  alloc_log:before_each()
93end)
94
95after_each(function()
96  alloc_log:after_each()
97end)
98
99local function ga_alloc(itemsize, growsize)
100  local ga = ffi.gc(ffi.cast('garray_T*', ffi.new('garray_T[1]', {})), lib.ga_clear)
101  lib.ga_init(ga, itemsize or 1, growsize or 80)
102  return ga
103end
104
105local function check_emsg(f, msg)
106  local saved_msg_hist_last = lib.msg_hist_last
107  if saved_msg_hist_last == nil then
108    saved_msg_hist_last = nil
109  end
110  local ret = { f() }
111  if msg ~= nil then
112    eq(msg, ffi.string(lib.msg_hist_last.msg.items[0].text.data))
113    neq(saved_msg_hist_last, lib.msg_hist_last)
114  else
115    if saved_msg_hist_last ~= lib.msg_hist_last then
116      eq(nil, lib.msg_hist_last)
117    else
118      eq(saved_msg_hist_last, lib.msg_hist_last)
119    end
120  end
121  return unpack(ret)
122end
123
124describe('typval.c', function()
125  describe('list', function()
126    describe('item', function()
127      describe('remove()', function()
128        itp('works', function()
129          local l = list(1, 2, 3, 4, 5, 6, 7)
130          neq(nil, l)
131          local lis = list_items(l)
132          alloc_log:check({
133            a.list(l),
134            a.li(lis[1]),
135            a.li(lis[2]),
136            a.li(lis[3]),
137            a.li(lis[4]),
138            a.li(lis[5]),
139            a.li(lis[6]),
140            a.li(lis[7]),
141          })
142
143          eq(lis[2], lib.tv_list_item_remove(l, lis[1]))
144          alloc_log:check({
145            a.freed(table.remove(lis, 1)),
146          })
147          eq(lis, list_items(l))
148
149          eq(lis[7], lib.tv_list_item_remove(l, lis[6]))
150          alloc_log:check({
151            a.freed(table.remove(lis)),
152          })
153          eq(lis, list_items(l))
154
155          eq(lis[4], lib.tv_list_item_remove(l, lis[3]))
156          alloc_log:check({
157            a.freed(table.remove(lis, 3)),
158          })
159          eq(lis, list_items(l))
160        end)
161        itp('also frees the value', function()
162          local l = list('a', 'b', 'c', 'd')
163          neq(nil, l)
164          local lis = list_items(l)
165          alloc_log:check({
166            a.list(l),
167            a.str(lis[1].li_tv.vval.v_string, 1),
168            a.li(lis[1]),
169            a.str(lis[2].li_tv.vval.v_string, 1),
170            a.li(lis[2]),
171            a.str(lis[3].li_tv.vval.v_string, 1),
172            a.li(lis[3]),
173            a.str(lis[4].li_tv.vval.v_string, 1),
174            a.li(lis[4]),
175          })
176          local strings = map(function(li)
177            return li.li_tv.vval.v_string
178          end, lis)
179
180          eq(lis[2], lib.tv_list_item_remove(l, lis[1]))
181          alloc_log:check({
182            a.freed(table.remove(strings, 1)),
183            a.freed(table.remove(lis, 1)),
184          })
185          eq(lis, list_items(l))
186
187          eq(lis[3], lib.tv_list_item_remove(l, lis[2]))
188          alloc_log:check({
189            a.freed(table.remove(strings, 2)),
190            a.freed(table.remove(lis, 2)),
191          })
192          eq(lis, list_items(l))
193
194          eq(nil, lib.tv_list_item_remove(l, lis[2]))
195          alloc_log:check({
196            a.freed(table.remove(strings, 2)),
197            a.freed(table.remove(lis, 2)),
198          })
199          eq(lis, list_items(l))
200        end)
201        itp('works and adjusts watchers correctly', function()
202          local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
203          neq(nil, l)
204          local lis = list_items(l)
205          -- Three watchers: pointing to first, middle and last elements.
206          local lws = {
207            list_watch(l, lis[1]),
208            list_watch(l, lis[4]),
209            list_watch(l, lis[7]),
210          }
211          alloc_log:check({
212            a.list(l),
213            a.li(lis[1]),
214            a.li(lis[2]),
215            a.li(lis[3]),
216            a.li(lis[4]),
217            a.li(lis[5]),
218            a.li(lis[6]),
219            a.li(lis[7]),
220          })
221
222          eq(lis[5], lib.tv_list_item_remove(l, lis[4]))
223          alloc_log:check({ a.freed(lis[4]) })
224          eq({ lis[1], lis[5], lis[7] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
225
226          eq(lis[3], lib.tv_list_item_remove(l, lis[2]))
227          alloc_log:check({ a.freed(lis[2]) })
228          eq({ lis[1], lis[5], lis[7] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
229
230          eq(nil, lib.tv_list_item_remove(l, lis[7]))
231          alloc_log:check({ a.freed(lis[7]) })
232          eq(
233            { lis[1], lis[5], nil },
234            { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
235          )
236
237          eq(lis[3], lib.tv_list_item_remove(l, lis[1]))
238          alloc_log:check({ a.freed(lis[1]) })
239          eq(
240            { lis[3], lis[5], nil },
241            { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
242          )
243
244          lib.tv_list_watch_remove(l, lws[2])
245          lib.tv_list_watch_remove(l, lws[3])
246          lib.tv_list_watch_remove(l, lws[1])
247          lib.tv_list_free(l)
248          alloc_log:check({
249            a.freed(lis[3]),
250            a.freed(lis[5]),
251            a.freed(lis[6]),
252            a.freed(l),
253          })
254        end)
255      end)
256    end)
257    describe('watch', function()
258      describe('remove()', function()
259        itp('works', function()
260          local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
261          eq(nil, l.lv_watch)
262          local lw = list_watch(l)
263          neq(nil, l.lv_watch)
264          alloc_log:clear()
265          lib.tv_list_watch_remove(l, lw)
266          eq(nil, l.lv_watch)
267          alloc_log:check({
268            -- Does not free anything.
269          })
270          local lws = { list_watch(l), list_watch(l), list_watch(l) }
271          alloc_log:clear()
272          lib.tv_list_watch_remove(l, lws[2])
273          eq(lws[3], l.lv_watch)
274          eq(lws[1], l.lv_watch.lw_next)
275          lib.tv_list_watch_remove(l, lws[1])
276          eq(lws[3], l.lv_watch)
277          eq(nil, l.lv_watch.lw_next)
278          lib.tv_list_watch_remove(l, lws[3])
279          eq(nil, l.lv_watch)
280          alloc_log:check({
281            -- Does not free anything.
282          })
283        end)
284        itp('ignores not found watchers', function()
285          local l = list(1, 2, 3, 4, 5, 6, 7)
286          local lw = list_watch_alloc()
287          lib.tv_list_watch_remove(l, lw)
288        end)
289      end)
290    end)
291    -- add() and fix() were tested when testing tv_list_item_remove()
292    describe('free()', function()
293      itp('recursively frees list', function()
294        local l1 = ffi.gc(list(1, 'abc'), nil)
295        local l2 = ffi.gc(list({}), nil)
296        local l3 = ffi.gc(list(empty_list), nil)
297        local alloc_rets = {}
298        alloc_log:check(get_alloc_rets({
299          a.list(l1),
300          a.li(l1.lv_first),
301          a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
302          a.li(l1.lv_last),
303          a.list(l2),
304          a.dict(l2.lv_first.li_tv.vval.v_dict),
305          a.li(l2.lv_first),
306          a.list(l3),
307          a.list(l3.lv_first.li_tv.vval.v_list),
308          a.li(l3.lv_first),
309        }, alloc_rets))
310        lib.tv_list_free(l1)
311        alloc_log:check({
312          alloc_rets:freed(2),
313          alloc_rets:freed(3),
314          alloc_rets:freed(4),
315          alloc_rets:freed(1),
316        })
317        lib.tv_list_free(l2)
318        alloc_log:check({
319          alloc_rets:freed(6),
320          alloc_rets:freed(7),
321          alloc_rets:freed(5),
322        })
323        lib.tv_list_free(l3)
324        alloc_log:check({
325          alloc_rets:freed(9),
326          alloc_rets:freed(10),
327          alloc_rets:freed(8),
328        })
329      end)
330    end)
331    describe('free_list()', function()
332      itp('does not free list contents', function()
333        local l1 = ffi.gc(list(1, 'abc'), nil)
334        local l2 = ffi.gc(list({}), nil)
335        local l3 = ffi.gc(list(empty_list), nil)
336        local alloc_rets = {}
337        alloc_log:check(get_alloc_rets({
338          a.list(l1),
339          a.li(l1.lv_first),
340          a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
341          a.li(l1.lv_last),
342          a.list(l2),
343          a.dict(l2.lv_first.li_tv.vval.v_dict),
344          a.li(l2.lv_first),
345          a.list(l3),
346          a.list(l3.lv_first.li_tv.vval.v_list),
347          a.li(l3.lv_first),
348        }, alloc_rets))
349        lib.tv_list_free_list(l1)
350        alloc_log:check({
351          alloc_rets:freed(1),
352        })
353        lib.tv_list_free_list(l2)
354        alloc_log:check({
355          alloc_rets:freed(5),
356        })
357        lib.tv_list_free_list(l3)
358        alloc_log:check({
359          alloc_rets:freed(8),
360        })
361      end)
362    end)
363    describe('free_contents()', function()
364      itp('recursively frees list, except for the list structure itself', function()
365        local l1 = ffi.gc(list(1, 'abc'), nil)
366        local l2 = ffi.gc(list({}), nil)
367        local l3 = ffi.gc(list(empty_list), nil)
368        local alloc_rets = {}
369        alloc_log:check(get_alloc_rets({
370          a.list(l1),
371          a.li(l1.lv_first),
372          a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
373          a.li(l1.lv_last),
374          a.list(l2),
375          a.dict(l2.lv_first.li_tv.vval.v_dict),
376          a.li(l2.lv_first),
377          a.list(l3),
378          a.list(l3.lv_first.li_tv.vval.v_list),
379          a.li(l3.lv_first),
380        }, alloc_rets))
381        lib.tv_list_free_contents(l1)
382        alloc_log:check({
383          alloc_rets:freed(2),
384          alloc_rets:freed(3),
385          alloc_rets:freed(4),
386        })
387        lib.tv_list_free_contents(l2)
388        alloc_log:check({
389          alloc_rets:freed(6),
390          alloc_rets:freed(7),
391        })
392        lib.tv_list_free_contents(l3)
393        alloc_log:check({
394          alloc_rets:freed(9),
395          alloc_rets:freed(10),
396        })
397      end)
398    end)
399    describe('unref()', function()
400      itp('recursively frees list when reference count goes to 0', function()
401        local l = ffi.gc(list(empty_list), nil)
402        local alloc_rets = {}
403        alloc_log:check(get_alloc_rets({
404          a.list(l),
405          a.list(l.lv_first.li_tv.vval.v_list),
406          a.li(l.lv_first),
407        }, alloc_rets))
408        l.lv_refcount = 2
409        lib.tv_list_unref(l)
410        alloc_log:check({})
411        lib.tv_list_unref(l)
412        alloc_log:check({
413          alloc_rets:freed(2),
414          alloc_rets:freed(3),
415          alloc_rets:freed(1),
416        })
417      end)
418    end)
419    describe('drop_items()', function()
420      itp('works', function()
421        local l_tv = lua2typvalt({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 })
422        local l = l_tv.vval.v_list
423        local lis = list_items(l)
424        -- Three watchers: pointing to first, middle and last elements.
425        local lws = {
426          list_watch(l, lis[1]),
427          list_watch(l, lis[7]),
428          list_watch(l, lis[13]),
429        }
430        alloc_log:clear()
431
432        lib.tv_list_drop_items(l, lis[1], lis[3])
433        eq({ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, typvalt2lua(l_tv))
434        eq({ lis[4], lis[7], lis[13] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
435
436        lib.tv_list_drop_items(l, lis[11], lis[13])
437        eq({ 4, 5, 6, 7, 8, 9, 10 }, typvalt2lua(l_tv))
438        eq(
439          { lis[4], lis[7], nil },
440          { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
441        )
442
443        lib.tv_list_drop_items(l, lis[6], lis[8])
444        eq({ 4, 5, 9, 10 }, typvalt2lua(l_tv))
445        eq(
446          { lis[4], lis[9], nil },
447          { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
448        )
449
450        lib.tv_list_drop_items(l, lis[4], lis[10])
451        eq(empty_list, typvalt2lua(l_tv))
452        eq(
453          { true, true, true },
454          { lws[1].lw_item == nil, lws[2].lw_item == nil, lws[3].lw_item == nil }
455        )
456
457        lib.tv_list_watch_remove(l, lws[1])
458        lib.tv_list_watch_remove(l, lws[2])
459        lib.tv_list_watch_remove(l, lws[3])
460
461        alloc_log:check({})
462      end)
463    end)
464    describe('remove_items()', function()
465      itp('works', function()
466        local l_tv =
467          lua2typvalt({ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' })
468        local l = l_tv.vval.v_list
469        local lis = list_items(l)
470        local strings = map(function(li)
471          return li.li_tv.vval.v_string
472        end, lis)
473        -- Three watchers: pointing to first, middle and last elements.
474        local lws = {
475          list_watch(l, lis[1]),
476          list_watch(l, lis[7]),
477          list_watch(l, lis[13]),
478        }
479        alloc_log:clear()
480
481        lib.tv_list_remove_items(l, lis[1], lis[3])
482        eq({ '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' }, typvalt2lua(l_tv))
483        eq({ lis[4], lis[7], lis[13] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
484        alloc_log:check({
485          a.freed(strings[1]),
486          a.freed(lis[1]),
487          a.freed(strings[2]),
488          a.freed(lis[2]),
489          a.freed(strings[3]),
490          a.freed(lis[3]),
491        })
492
493        lib.tv_list_remove_items(l, lis[11], lis[13])
494        eq({ '4', '5', '6', '7', '8', '9', '10' }, typvalt2lua(l_tv))
495        eq(
496          { lis[4], lis[7], nil },
497          { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
498        )
499        alloc_log:check({
500          a.freed(strings[11]),
501          a.freed(lis[11]),
502          a.freed(strings[12]),
503          a.freed(lis[12]),
504          a.freed(strings[13]),
505          a.freed(lis[13]),
506        })
507
508        lib.tv_list_remove_items(l, lis[6], lis[8])
509        eq({ '4', '5', '9', '10' }, typvalt2lua(l_tv))
510        eq(
511          { lis[4], lis[9], nil },
512          { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
513        )
514        alloc_log:check({
515          a.freed(strings[6]),
516          a.freed(lis[6]),
517          a.freed(strings[7]),
518          a.freed(lis[7]),
519          a.freed(strings[8]),
520          a.freed(lis[8]),
521        })
522
523        lib.tv_list_remove_items(l, lis[4], lis[10])
524        eq(empty_list, typvalt2lua(l_tv))
525        eq(
526          { true, true, true },
527          { lws[1].lw_item == nil, lws[2].lw_item == nil, lws[3].lw_item == nil }
528        )
529        alloc_log:check({
530          a.freed(strings[4]),
531          a.freed(lis[4]),
532          a.freed(strings[5]),
533          a.freed(lis[5]),
534          a.freed(strings[9]),
535          a.freed(lis[9]),
536          a.freed(strings[10]),
537          a.freed(lis[10]),
538        })
539
540        lib.tv_list_watch_remove(l, lws[1])
541        lib.tv_list_watch_remove(l, lws[2])
542        lib.tv_list_watch_remove(l, lws[3])
543
544        alloc_log:check({})
545      end)
546    end)
547    describe('insert', function()
548      describe('()', function()
549        itp('works', function()
550          local l_tv = lua2typvalt({ 1, 2, 3, 4, 5, 6, 7 })
551          local l = l_tv.vval.v_list
552          local lis = list_items(l)
553          local li
554
555          li = li_alloc(true)
556          li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 100500 } }
557          lib.tv_list_insert(l, li, nil)
558          eq(l.lv_last, li)
559          eq({ 1, 2, 3, 4, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
560
561          li = li_alloc(true)
562          li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 0 } }
563          lib.tv_list_insert(l, li, lis[1])
564          eq(l.lv_first, li)
565          eq({ 0, 1, 2, 3, 4, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
566
567          li = li_alloc(true)
568          li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 4.5 } }
569          lib.tv_list_insert(l, li, lis[5])
570          eq(list_items(l)[6], li)
571          eq({ 0, 1, 2, 3, 4, 4.5, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
572        end)
573        itp('works with an empty list', function()
574          local l_tv = lua2typvalt(empty_list)
575          local l = l_tv.vval.v_list
576
577          eq(nil, l.lv_first)
578          eq(nil, l.lv_last)
579
580          local li = li_alloc(true)
581          li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 100500 } }
582          lib.tv_list_insert(l, li, nil)
583          eq(l.lv_last, li)
584          eq({ 100500 }, typvalt2lua(l_tv))
585        end)
586      end)
587      describe('tv()', function()
588        itp('works', function()
589          local l_tv = lua2typvalt(empty_list)
590          local l = l_tv.vval.v_list
591
592          local l_l_tv = lua2typvalt(empty_list)
593          alloc_log:clear()
594          local l_l = l_l_tv.vval.v_list
595          eq(1, l_l.lv_refcount)
596          lib.tv_list_insert_tv(l, l_l_tv, nil)
597          eq(2, l_l.lv_refcount)
598          eq(l_l, l.lv_first.li_tv.vval.v_list)
599          alloc_log:check({
600            a.li(l.lv_first),
601          })
602
603          local l_s_tv = lua2typvalt('test')
604          alloc_log:check({
605            a.str(l_s_tv.vval.v_string, 'test'),
606          })
607          lib.tv_list_insert_tv(l, l_s_tv, l.lv_first)
608          alloc_log:check({
609            a.li(l.lv_first),
610            a.str(l.lv_first.li_tv.vval.v_string, 'test'),
611          })
612
613          eq({ 'test', empty_list }, typvalt2lua(l_tv))
614        end)
615      end)
616    end)
617    describe('append', function()
618      describe('list()', function()
619        itp('works', function()
620          local l_tv = lua2typvalt(empty_list)
621          local l = l_tv.vval.v_list
622
623          local l_l = list(1)
624          alloc_log:clear()
625          eq(1, l_l.lv_refcount)
626          lib.tv_list_append_list(l, l_l)
627          eq(2, l_l.lv_refcount)
628          eq(l_l, l.lv_first.li_tv.vval.v_list)
629          alloc_log:check({
630            a.li(l.lv_last),
631          })
632
633          lib.tv_list_append_list(l, nil)
634          alloc_log:check({
635            a.li(l.lv_last),
636          })
637
638          eq({ { 1 }, null_list }, typvalt2lua(l_tv))
639        end)
640      end)
641      describe('dict()', function()
642        itp('works', function()
643          local l_tv = lua2typvalt(empty_list)
644          local l = l_tv.vval.v_list
645
646          local l_d_tv = lua2typvalt({ test = 1 })
647          local l_d = l_d_tv.vval.v_dict
648          alloc_log:clear()
649          eq(1, l_d.dv_refcount)
650          lib.tv_list_append_dict(l, l_d)
651          eq(2, l_d.dv_refcount)
652          eq(l_d, l.lv_first.li_tv.vval.v_list)
653          alloc_log:check({
654            a.li(l.lv_last),
655          })
656
657          lib.tv_list_append_dict(l, nil)
658          alloc_log:check({
659            a.li(l.lv_last),
660          })
661
662          eq({ { test = 1 }, null_dict }, typvalt2lua(l_tv))
663        end)
664      end)
665      describe('string()', function()
666        itp('works', function()
667          local l_tv = lua2typvalt(empty_list)
668          local l = l_tv.vval.v_list
669
670          alloc_log:clear()
671          lib.tv_list_append_string(l, 'test', 3)
672          alloc_log:check({
673            a.str(l.lv_last.li_tv.vval.v_string, 'tes'),
674            a.li(l.lv_last),
675          })
676
677          lib.tv_list_append_string(l, nil, 0)
678          alloc_log:check({
679            a.li(l.lv_last),
680          })
681
682          lib.tv_list_append_string(l, nil, -1)
683          alloc_log:check({
684            a.li(l.lv_last),
685          })
686
687          lib.tv_list_append_string(l, 'test', -1)
688          alloc_log:check({
689            a.str(l.lv_last.li_tv.vval.v_string, 'test'),
690            a.li(l.lv_last),
691          })
692
693          eq({ 'tes', null_string, null_string, 'test' }, typvalt2lua(l_tv))
694        end)
695      end)
696      describe('allocated string()', function()
697        itp('works', function()
698          local l_tv = lua2typvalt(empty_list)
699          local l = l_tv.vval.v_list
700
701          local s = lib.xstrdup('test')
702          alloc_log:clear()
703          lib.tv_list_append_allocated_string(l, s)
704          alloc_log:check({
705            a.li(l.lv_last),
706          })
707
708          lib.tv_list_append_allocated_string(l, nil)
709          alloc_log:check({
710            a.li(l.lv_last),
711          })
712
713          lib.tv_list_append_allocated_string(l, nil)
714          alloc_log:check({
715            a.li(l.lv_last),
716          })
717
718          eq({ 'test', null_string, null_string }, typvalt2lua(l_tv))
719        end)
720      end)
721      describe('number()', function()
722        itp('works', function()
723          local l_tv = lua2typvalt(empty_list)
724          local l = l_tv.vval.v_list
725
726          alloc_log:clear()
727          lib.tv_list_append_number(l, -100500)
728          alloc_log:check({
729            a.li(l.lv_last),
730          })
731
732          lib.tv_list_append_number(l, 100500)
733          alloc_log:check({
734            a.li(l.lv_last),
735          })
736
737          eq({ int(-100500), int(100500) }, typvalt2lua(l_tv))
738        end)
739      end)
740      describe('tv()', function()
741        itp('works', function()
742          local l_tv = lua2typvalt(empty_list)
743          local l = l_tv.vval.v_list
744
745          local l_l_tv = lua2typvalt(empty_list)
746          alloc_log:clear()
747          local l_l = l_l_tv.vval.v_list
748          eq(1, l_l.lv_refcount)
749          lib.tv_list_append_tv(l, l_l_tv)
750          eq(2, l_l.lv_refcount)
751          eq(l_l, l.lv_first.li_tv.vval.v_list)
752          alloc_log:check({
753            a.li(l.lv_first),
754          })
755
756          local l_s_tv = lua2typvalt('test')
757          alloc_log:check({
758            a.str(l_s_tv.vval.v_string, 'test'),
759          })
760          lib.tv_list_append_tv(l, l_s_tv)
761          alloc_log:check({
762            a.li(l.lv_last),
763            a.str(l.lv_last.li_tv.vval.v_string, 'test'),
764          })
765
766          eq({ empty_list, 'test' }, typvalt2lua(l_tv))
767        end)
768      end)
769      describe('owned tv()', function()
770        itp('works', function()
771          local l_tv = lua2typvalt(empty_list)
772          local l = l_tv.vval.v_list
773
774          local l_l_tv = lua2typvalt(empty_list)
775          alloc_log:clear()
776          local l_l = l_l_tv.vval.v_list
777          eq(1, l_l.lv_refcount)
778          lib.tv_list_append_owned_tv(l, l_l_tv)
779          eq(1, l_l.lv_refcount)
780          l_l.lv_refcount = l_l.lv_refcount + 1
781          eq(l_l, l.lv_first.li_tv.vval.v_list)
782          alloc_log:check({
783            a.li(l.lv_first),
784          })
785
786          local l_s_tv = ffi.gc(lua2typvalt('test'), nil)
787          alloc_log:check({
788            a.str(l_s_tv.vval.v_string, 'test'),
789          })
790          lib.tv_list_append_owned_tv(l, l_s_tv)
791          eq(l_s_tv.vval.v_string, l.lv_last.li_tv.vval.v_string)
792          l_s_tv.vval.v_string = nil
793          alloc_log:check({
794            a.li(l.lv_last),
795          })
796
797          eq({ empty_list, 'test' }, typvalt2lua(l_tv))
798        end)
799      end)
800    end)
801    describe('copy()', function()
802      local function tv_list_copy(...)
803        return ffi.gc(lib.tv_list_copy(...), lib.tv_list_unref)
804      end
805      itp('copies NULL correctly', function()
806        eq(nil, lib.tv_list_copy(nil, nil, true, 0))
807        eq(nil, lib.tv_list_copy(nil, nil, false, 0))
808        eq(nil, lib.tv_list_copy(nil, nil, true, 1))
809        eq(nil, lib.tv_list_copy(nil, nil, false, 1))
810      end)
811      itp('copies list correctly without converting items', function()
812        do
813          local v = { { ['«'] = '»' }, { '„' }, 1, '“', null_string, null_list, null_dict }
814          local l_tv = lua2typvalt(v)
815          local l = l_tv.vval.v_list
816          local lis = list_items(l)
817          alloc_log:clear()
818
819          eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
820          eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
821          local l_copy1 = tv_list_copy(nil, l, false, 0)
822          eq(2, lis[1].li_tv.vval.v_dict.dv_refcount)
823          eq(2, lis[2].li_tv.vval.v_list.lv_refcount)
824          local lis_copy1 = list_items(l_copy1)
825          eq(lis[1].li_tv.vval.v_dict, lis_copy1[1].li_tv.vval.v_dict)
826          eq(lis[2].li_tv.vval.v_list, lis_copy1[2].li_tv.vval.v_list)
827          eq(v, lst2tbl(l_copy1))
828          alloc_log:check({
829            a.list(l_copy1),
830            a.li(lis_copy1[1]),
831            a.li(lis_copy1[2]),
832            a.li(lis_copy1[3]),
833            a.li(lis_copy1[4]),
834            a.str(lis_copy1[4].li_tv.vval.v_string, #v[4]),
835            a.li(lis_copy1[5]),
836            a.li(lis_copy1[6]),
837            a.li(lis_copy1[7]),
838          })
839          lib.tv_list_free(ffi.gc(l_copy1, nil))
840          alloc_log:clear()
841
842          eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
843          eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
844          local l_deepcopy1 = tv_list_copy(nil, l, true, 0)
845          neq(nil, l_deepcopy1)
846          eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
847          eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
848          local lis_deepcopy1 = list_items(l_deepcopy1)
849          neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
850          neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
851          eq(v, lst2tbl(l_deepcopy1))
852          local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
853          alloc_log:check({
854            a.list(l_deepcopy1),
855            a.li(lis_deepcopy1[1]),
856            a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
857            a.di(di_deepcopy1, #'«'),
858            a.str(di_deepcopy1.di_tv.vval.v_string, #v[1]['«']),
859            a.li(lis_deepcopy1[2]),
860            a.list(lis_deepcopy1[2].li_tv.vval.v_list),
861            a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
862            a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
863            a.li(lis_deepcopy1[3]),
864            a.li(lis_deepcopy1[4]),
865            a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
866            a.li(lis_deepcopy1[5]),
867            a.li(lis_deepcopy1[6]),
868            a.li(lis_deepcopy1[7]),
869          })
870        end
871        collectgarbage()
872      end)
873      itp('copies list correctly and converts items', function()
874        local vc = vimconv_alloc()
875        -- UTF-8 ↔ latin1 conversions needs no iconv
876        eq(OK, lib.convert_setup(vc, to_cstr('utf-8'), to_cstr('latin1')))
877
878        local v = { { ['«'] = '»' }, { '„' }, 1, '“', null_string, null_list, null_dict }
879        local l_tv = lua2typvalt(v)
880        local l = l_tv.vval.v_list
881        local lis = list_items(l)
882        alloc_log:clear()
883
884        eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
885        eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
886        local l_deepcopy1 = tv_list_copy(vc, l, true, 0)
887        neq(nil, l_deepcopy1)
888        eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
889        eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
890        local lis_deepcopy1 = list_items(l_deepcopy1)
891        neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
892        neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
893        eq(
894          { { ['\171'] = '\187' }, { '\191' }, 1, '\191', null_string, null_list, null_dict },
895          lst2tbl(l_deepcopy1)
896        )
897        local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
898        alloc_log:clear_tmp_allocs()
899        alloc_log:check({
900          a.list(l_deepcopy1),
901          a.li(lis_deepcopy1[1]),
902          a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
903          a.di(di_deepcopy1, 1),
904          a.str(di_deepcopy1.di_tv.vval.v_string, 2),
905          a.li(lis_deepcopy1[2]),
906          a.list(lis_deepcopy1[2].li_tv.vval.v_list),
907          a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
908          a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
909          a.li(lis_deepcopy1[3]),
910          a.li(lis_deepcopy1[4]),
911          a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
912          a.li(lis_deepcopy1[5]),
913          a.li(lis_deepcopy1[6]),
914          a.li(lis_deepcopy1[7]),
915        })
916      end)
917      itp('returns different/same containers with(out) copyID', function()
918        local l_inner_tv = lua2typvalt(empty_list)
919        local l_tv = lua2typvalt({ l_inner_tv, l_inner_tv })
920        eq(3, l_inner_tv.vval.v_list.lv_refcount)
921        local l = l_tv.vval.v_list
922        eq(l.lv_first.li_tv.vval.v_list, l.lv_last.li_tv.vval.v_list)
923
924        local l_copy1 = tv_list_copy(nil, l, true, 0)
925        neq(l_copy1.lv_first.li_tv.vval.v_list, l_copy1.lv_last.li_tv.vval.v_list)
926        eq({ empty_list, empty_list }, lst2tbl(l_copy1))
927
928        local l_copy2 = tv_list_copy(nil, l, true, 2)
929        eq(l_copy2.lv_first.li_tv.vval.v_list, l_copy2.lv_last.li_tv.vval.v_list)
930        eq({ empty_list, empty_list }, lst2tbl(l_copy2))
931
932        eq(3, l_inner_tv.vval.v_list.lv_refcount)
933      end)
934      itp('works with self-referencing list with copyID', function()
935        local l_tv = lua2typvalt(empty_list)
936        local l = l_tv.vval.v_list
937        eq(1, l.lv_refcount)
938        lib.tv_list_append_list(l, l)
939        eq(2, l.lv_refcount)
940
941        local l_copy1 = tv_list_copy(nil, l, true, 2)
942        eq(2, l_copy1.lv_refcount)
943        local v = {}
944        v[1] = v
945        eq(v, lst2tbl(l_copy1))
946
947        local lis = list_items(l)
948        lib.tv_list_item_remove(l, lis[1])
949        eq(1, l.lv_refcount)
950
951        local lis_copy1 = list_items(l_copy1)
952        lib.tv_list_item_remove(l_copy1, lis_copy1[1])
953        eq(1, l_copy1.lv_refcount)
954      end)
955    end)
956    describe('extend()', function()
957      itp('can extend list with itself', function()
958        local l
959
960        l = list(1, {})
961        alloc_log:clear()
962        eq(1, l.lv_refcount)
963        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
964
965        lib.tv_list_extend(l, l, nil)
966        alloc_log:check({
967          a.li(l.lv_last.li_prev),
968          a.li(l.lv_last),
969        })
970        eq(1, l.lv_refcount)
971        eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
972        eq({ 1, {}, 1, {} }, lst2tbl(l))
973
974        l = list(1, {})
975        alloc_log:clear()
976        eq(1, l.lv_refcount)
977        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
978
979        lib.tv_list_extend(l, l, l.lv_last)
980        alloc_log:check({
981          a.li(l.lv_last.li_prev.li_prev),
982          a.li(l.lv_last.li_prev),
983        })
984        eq({ 1, 1, {}, {} }, lst2tbl(l))
985        eq(1, l.lv_refcount)
986        eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
987
988        l = list(1, {})
989        alloc_log:clear()
990        eq(1, l.lv_refcount)
991        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
992
993        lib.tv_list_extend(l, l, l.lv_first)
994        alloc_log:check({
995          a.li(l.lv_first),
996          a.li(l.lv_first.li_next),
997        })
998        eq({ 1, {}, 1, {} }, lst2tbl(l))
999        eq(1, l.lv_refcount)
1000        eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1001      end)
1002      itp('can extend list with an empty list', function()
1003        local l = list(1, {})
1004        local el = list()
1005        alloc_log:clear()
1006        eq(1, l.lv_refcount)
1007        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1008        eq(1, el.lv_refcount)
1009
1010        lib.tv_list_extend(l, el, nil)
1011        alloc_log:check({})
1012        eq(1, l.lv_refcount)
1013        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1014        eq(1, el.lv_refcount)
1015        eq({ 1, {} }, lst2tbl(l))
1016
1017        lib.tv_list_extend(l, el, l.lv_first)
1018        alloc_log:check({})
1019        eq(1, l.lv_refcount)
1020        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1021        eq(1, el.lv_refcount)
1022        eq({ 1, {} }, lst2tbl(l))
1023
1024        lib.tv_list_extend(l, el, l.lv_last)
1025        alloc_log:check({})
1026        eq(1, l.lv_refcount)
1027        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1028        eq(1, el.lv_refcount)
1029        eq({ 1, {} }, lst2tbl(l))
1030      end)
1031      itp('can extend list with another non-empty list', function()
1032        local l
1033        local l2 = list(42, empty_list)
1034        eq(1, l2.lv_refcount)
1035        eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1036
1037        l = ffi.gc(list(1, {}), nil)
1038        alloc_log:clear()
1039        eq(1, l.lv_refcount)
1040        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1041
1042        lib.tv_list_extend(l, l2, nil)
1043        alloc_log:check({
1044          a.li(l.lv_last.li_prev),
1045          a.li(l.lv_last),
1046        })
1047        eq(1, l2.lv_refcount)
1048        eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1049        eq({ 1, {}, 42, empty_list }, lst2tbl(l))
1050        lib.tv_list_free(l)
1051        eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1052
1053        l = ffi.gc(list(1, {}), nil)
1054        alloc_log:clear()
1055        eq(1, l.lv_refcount)
1056        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1057
1058        lib.tv_list_extend(l, l2, l.lv_first)
1059        alloc_log:check({
1060          a.li(l.lv_first),
1061          a.li(l.lv_first.li_next),
1062        })
1063        eq(1, l2.lv_refcount)
1064        eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1065        eq({ 42, empty_list, 1, {} }, lst2tbl(l))
1066        lib.tv_list_free(l)
1067        eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1068
1069        l = ffi.gc(list(1, {}), nil)
1070        alloc_log:clear()
1071        eq(1, l.lv_refcount)
1072        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1073
1074        lib.tv_list_extend(l, l2, l.lv_last)
1075        alloc_log:check({
1076          a.li(l.lv_first.li_next),
1077          a.li(l.lv_first.li_next.li_next),
1078        })
1079        eq(1, l2.lv_refcount)
1080        eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1081        eq({ 1, 42, empty_list, {} }, lst2tbl(l))
1082        lib.tv_list_free(l)
1083        eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1084      end)
1085    end)
1086    describe('concat()', function()
1087      itp('works with NULL lists', function()
1088        local l = list(1, {})
1089        alloc_log:clear()
1090        eq(1, l.lv_refcount)
1091        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1092
1093        local rettv1 = typvalt()
1094        eq(OK, lib.tv_list_concat(nil, l, rettv1))
1095        eq(1, l.lv_refcount)
1096        eq(tonumber(lib.VAR_LIST), tonumber(rettv1.v_type))
1097        eq({ 1, {} }, typvalt2lua(rettv1))
1098        eq(1, rettv1.vval.v_list.lv_refcount)
1099        alloc_log:check({
1100          a.list(rettv1.vval.v_list),
1101          a.li(rettv1.vval.v_list.lv_first),
1102          a.li(rettv1.vval.v_list.lv_last),
1103        })
1104        eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1105
1106        local rettv2 = typvalt()
1107        eq(OK, lib.tv_list_concat(l, nil, rettv2))
1108        eq(1, l.lv_refcount)
1109        eq(tonumber(lib.VAR_LIST), tonumber(rettv2.v_type))
1110        eq({ 1, {} }, typvalt2lua(rettv2))
1111        eq(1, rettv2.vval.v_list.lv_refcount)
1112        alloc_log:check({
1113          a.list(rettv2.vval.v_list),
1114          a.li(rettv2.vval.v_list.lv_first),
1115          a.li(rettv2.vval.v_list.lv_last),
1116        })
1117        eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1118
1119        local rettv3 = typvalt()
1120        eq(OK, lib.tv_list_concat(nil, nil, rettv3))
1121        eq(tonumber(lib.VAR_LIST), tonumber(rettv3.v_type))
1122        eq(null_list, typvalt2lua(rettv3))
1123        alloc_log:check({})
1124      end)
1125      itp('works with two different lists', function()
1126        local l1 = list(1, {})
1127        local l2 = list(3, empty_list)
1128        eq(1, l1.lv_refcount)
1129        eq(1, l1.lv_last.li_tv.vval.v_dict.dv_refcount)
1130        eq(1, l2.lv_refcount)
1131        eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1132        alloc_log:clear()
1133
1134        local rettv = typvalt()
1135        eq(OK, lib.tv_list_concat(l1, l2, rettv))
1136        eq(1, l1.lv_refcount)
1137        eq(2, l1.lv_last.li_tv.vval.v_dict.dv_refcount)
1138        eq(1, l2.lv_refcount)
1139        eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
1140        alloc_log:check({
1141          a.list(rettv.vval.v_list),
1142          a.li(rettv.vval.v_list.lv_first),
1143          a.li(rettv.vval.v_list.lv_first.li_next),
1144          a.li(rettv.vval.v_list.lv_last.li_prev),
1145          a.li(rettv.vval.v_list.lv_last),
1146        })
1147        eq({ 1, {}, 3, empty_list }, typvalt2lua(rettv))
1148      end)
1149      itp('can concatenate list with itself', function()
1150        local l = list(1, {})
1151        eq(1, l.lv_refcount)
1152        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1153        alloc_log:clear()
1154
1155        local rettv = typvalt()
1156        eq(OK, lib.tv_list_concat(l, l, rettv))
1157        eq(1, l.lv_refcount)
1158        eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1159        alloc_log:check({
1160          a.list(rettv.vval.v_list),
1161          a.li(rettv.vval.v_list.lv_first),
1162          a.li(rettv.vval.v_list.lv_first.li_next),
1163          a.li(rettv.vval.v_list.lv_last.li_prev),
1164          a.li(rettv.vval.v_list.lv_last),
1165        })
1166        eq({ 1, {}, 1, {} }, typvalt2lua(rettv))
1167      end)
1168      itp('can concatenate empty non-NULL lists', function()
1169        local l = list(1, {})
1170        local le = list()
1171        local le2 = list()
1172        eq(1, l.lv_refcount)
1173        eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1174        eq(1, le.lv_refcount)
1175        eq(1, le2.lv_refcount)
1176        alloc_log:clear()
1177
1178        local rettv1 = typvalt()
1179        eq(OK, lib.tv_list_concat(l, le, rettv1))
1180        eq(1, l.lv_refcount)
1181        eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1182        eq(1, le.lv_refcount)
1183        eq(1, le2.lv_refcount)
1184        alloc_log:check({
1185          a.list(rettv1.vval.v_list),
1186          a.li(rettv1.vval.v_list.lv_first),
1187          a.li(rettv1.vval.v_list.lv_last),
1188        })
1189        eq({ 1, {} }, typvalt2lua(rettv1))
1190
1191        local rettv2 = typvalt()
1192        eq(OK, lib.tv_list_concat(le, l, rettv2))
1193        eq(1, l.lv_refcount)
1194        eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1195        eq(1, le.lv_refcount)
1196        eq(1, le2.lv_refcount)
1197        alloc_log:check({
1198          a.list(rettv2.vval.v_list),
1199          a.li(rettv2.vval.v_list.lv_first),
1200          a.li(rettv2.vval.v_list.lv_last),
1201        })
1202        eq({ 1, {} }, typvalt2lua(rettv2))
1203
1204        local rettv3 = typvalt()
1205        eq(OK, lib.tv_list_concat(le, le, rettv3))
1206        eq(1, l.lv_refcount)
1207        eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1208        eq(1, le.lv_refcount)
1209        eq(1, le2.lv_refcount)
1210        alloc_log:check({
1211          a.list(rettv3.vval.v_list),
1212        })
1213        eq(empty_list, typvalt2lua(rettv3))
1214
1215        local rettv4 = typvalt()
1216        eq(OK, lib.tv_list_concat(le, le2, rettv4))
1217        eq(1, l.lv_refcount)
1218        eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
1219        eq(1, le.lv_refcount)
1220        eq(1, le2.lv_refcount)
1221        alloc_log:check({
1222          a.list(rettv4.vval.v_list),
1223        })
1224        eq(empty_list, typvalt2lua(rettv4))
1225      end)
1226    end)
1227    describe('join()', function()
1228      local function list_join(l, sep, join_ret)
1229        local ga = ga_alloc()
1230        eq(join_ret or OK, lib.tv_list_join(ga, l, sep))
1231        local ret = ''
1232        if ga.ga_data ~= nil then
1233          ret = ffi.string(ga.ga_data)
1234        end
1235        -- For some reason this is not working well in GC
1236        lib.ga_clear(ffi.gc(ga, nil))
1237        return ret
1238      end
1239      itp('works', function()
1240        local l
1241        l = list('boo', 'far')
1242        eq('boo far', list_join(l, ' '))
1243        eq('boofar', list_join(l, ''))
1244
1245        l = list('boo')
1246        eq('boo', list_join(l, ' '))
1247
1248        l = list()
1249        eq('', list_join(l, ' '))
1250
1251        l = list({}, 'far')
1252        eq('{} far', list_join(l, ' '))
1253
1254        local recursive_list = {}
1255        recursive_list[1] = recursive_list
1256        l = ffi.gc(list(recursive_list, 'far'), nil)
1257        eq('[[...@0]] far', list_join(l, ' '))
1258
1259        local recursive_l = l.lv_first.li_tv.vval.v_list
1260        local recursive_li = recursive_l.lv_first
1261        lib.tv_list_item_remove(recursive_l, recursive_li)
1262        lib.tv_list_free(l)
1263      end)
1264    end)
1265    describe('equal()', function()
1266      itp('compares empty and NULL lists correctly', function()
1267        local l = list()
1268        local l2 = list()
1269
1270        -- NULL lists are equal to empty lists
1271        eq(true, lib.tv_list_equal(l, nil, true))
1272        eq(true, lib.tv_list_equal(nil, l, false))
1273
1274        -- NULL lists are equal themselves
1275        eq(true, lib.tv_list_equal(nil, nil, true))
1276        eq(true, lib.tv_list_equal(nil, nil, false))
1277
1278        -- As well as empty lists
1279        eq(true, lib.tv_list_equal(l, l, true))
1280        eq(true, lib.tv_list_equal(l, l2, false))
1281        eq(true, lib.tv_list_equal(l2, l, false))
1282        eq(true, lib.tv_list_equal(l2, l2, true))
1283      end)
1284      itp('compares lists correctly when case is not ignored', function()
1285        local l1 = list('abc', { 1, 2, 'Abc' }, 'def')
1286        local l2 = list('abc', { 1, 2, 'Abc' })
1287        local l3 = list('abc', { 1, 2, 'Abc' }, 'Def')
1288        local l4 = list('abc', { 1, 2, 'Abc', 4 }, 'def')
1289        local l5 = list('Abc', { 1, 2, 'Abc' }, 'def')
1290        local l6 = list('abc', { 1, 2, 'Abc' }, 'def')
1291        local l7 = list('abc', { 1, 2, 'abc' }, 'def')
1292        local l8 = list('abc', nil, 'def')
1293        local l9 = list('abc', { 1, 2, nil }, 'def')
1294
1295        eq(true, lib.tv_list_equal(l1, l1, false))
1296        eq(false, lib.tv_list_equal(l1, l2, false))
1297        eq(false, lib.tv_list_equal(l1, l3, false))
1298        eq(false, lib.tv_list_equal(l1, l4, false))
1299        eq(false, lib.tv_list_equal(l1, l5, false))
1300        eq(true, lib.tv_list_equal(l1, l6, false))
1301        eq(false, lib.tv_list_equal(l1, l7, false))
1302        eq(false, lib.tv_list_equal(l1, l8, false))
1303        eq(false, lib.tv_list_equal(l1, l9, false))
1304      end)
1305      itp('compares lists correctly when case is ignored', function()
1306        local l1 = list('abc', { 1, 2, 'Abc' }, 'def')
1307        local l2 = list('abc', { 1, 2, 'Abc' })
1308        local l3 = list('abc', { 1, 2, 'Abc' }, 'Def')
1309        local l4 = list('abc', { 1, 2, 'Abc', 4 }, 'def')
1310        local l5 = list('Abc', { 1, 2, 'Abc' }, 'def')
1311        local l6 = list('abc', { 1, 2, 'Abc' }, 'def')
1312        local l7 = list('abc', { 1, 2, 'abc' }, 'def')
1313        local l8 = list('abc', nil, 'def')
1314        local l9 = list('abc', { 1, 2, nil }, 'def')
1315
1316        eq(true, lib.tv_list_equal(l1, l1, true))
1317        eq(false, lib.tv_list_equal(l1, l2, true))
1318        eq(true, lib.tv_list_equal(l1, l3, true))
1319        eq(false, lib.tv_list_equal(l1, l4, true))
1320        eq(true, lib.tv_list_equal(l1, l5, true))
1321        eq(true, lib.tv_list_equal(l1, l6, true))
1322        eq(true, lib.tv_list_equal(l1, l7, true))
1323        eq(false, lib.tv_list_equal(l1, l8, true))
1324        eq(false, lib.tv_list_equal(l1, l9, true))
1325      end)
1326    end)
1327    describe('find', function()
1328      describe('()', function()
1329        itp('correctly indexes list', function()
1330          local l = list(1, 2, 3, 4, 5)
1331          local lis = list_items(l)
1332          alloc_log:clear()
1333
1334          eq(nil, lib.tv_list_find(nil, -1))
1335          eq(nil, lib.tv_list_find(nil, 0))
1336          eq(nil, lib.tv_list_find(nil, 1))
1337
1338          eq(nil, lib.tv_list_find(l, 5))
1339          eq(nil, lib.tv_list_find(l, -6))
1340          eq(lis[1], lib.tv_list_find(l, -5))
1341          eq(lis[5], lib.tv_list_find(l, 4))
1342          eq(lis[3], lib.tv_list_find(l, 2))
1343          eq(lis[3], lib.tv_list_find(l, -3))
1344          eq(lis[3], lib.tv_list_find(l, 2))
1345          eq(lis[3], lib.tv_list_find(l, 2))
1346          eq(lis[3], lib.tv_list_find(l, -3))
1347
1348          l.lv_idx_item = nil
1349          eq(lis[1], lib.tv_list_find(l, -5))
1350          l.lv_idx_item = nil
1351          eq(lis[5], lib.tv_list_find(l, 4))
1352          l.lv_idx_item = nil
1353          eq(lis[3], lib.tv_list_find(l, 2))
1354          l.lv_idx_item = nil
1355          eq(lis[3], lib.tv_list_find(l, -3))
1356          l.lv_idx_item = nil
1357          eq(lis[3], lib.tv_list_find(l, 2))
1358          l.lv_idx_item = nil
1359          eq(lis[3], lib.tv_list_find(l, 2))
1360          l.lv_idx_item = nil
1361          eq(lis[3], lib.tv_list_find(l, -3))
1362
1363          l.lv_idx_item = nil
1364          eq(lis[3], lib.tv_list_find(l, 2))
1365          eq(lis[1], lib.tv_list_find(l, -5))
1366          eq(lis[3], lib.tv_list_find(l, 2))
1367          eq(lis[5], lib.tv_list_find(l, 4))
1368          eq(lis[3], lib.tv_list_find(l, 2))
1369          eq(lis[3], lib.tv_list_find(l, 2))
1370          eq(lis[3], lib.tv_list_find(l, 2))
1371          eq(lis[3], lib.tv_list_find(l, -3))
1372          eq(lis[3], lib.tv_list_find(l, 2))
1373          eq(lis[3], lib.tv_list_find(l, 2))
1374          eq(lis[3], lib.tv_list_find(l, 2))
1375          eq(lis[3], lib.tv_list_find(l, -3))
1376
1377          alloc_log:check({})
1378        end)
1379      end)
1380      describe('nr()', function()
1381        local function tv_list_find_nr(l, n, msg)
1382          return check_emsg(function()
1383            local err = ffi.new('bool[1]', { false })
1384            local ret = lib.tv_list_find_nr(l, n, err)
1385            return (err[0] == true), ret
1386          end, msg)
1387        end
1388        itp('returns correct number', function()
1389          local l = list(int(1), int(2), int(3), int(4), int(5))
1390          alloc_log:clear()
1391
1392          eq({ false, 1 }, { tv_list_find_nr(l, -5) })
1393          eq({ false, 5 }, { tv_list_find_nr(l, 4) })
1394          eq({ false, 3 }, { tv_list_find_nr(l, 2) })
1395          eq({ false, 3 }, { tv_list_find_nr(l, -3) })
1396
1397          alloc_log:check({})
1398        end)
1399        itp('returns correct number when given a string', function()
1400          local l = list('1', '2', '3', '4', '5')
1401          alloc_log:clear()
1402
1403          eq({ false, 1 }, { tv_list_find_nr(l, -5) })
1404          eq({ false, 5 }, { tv_list_find_nr(l, 4) })
1405          eq({ false, 3 }, { tv_list_find_nr(l, 2) })
1406          eq({ false, 3 }, { tv_list_find_nr(l, -3) })
1407
1408          alloc_log:check({})
1409        end)
1410        itp('returns zero when given a NULL string', function()
1411          local l = list(null_string)
1412          alloc_log:clear()
1413
1414          eq({ false, 0 }, { tv_list_find_nr(l, 0) })
1415
1416          alloc_log:check({})
1417        end)
1418        itp('errors out on NULL lists', function()
1419          eq({ true, -1 }, { tv_list_find_nr(nil, -5) })
1420          eq({ true, -1 }, { tv_list_find_nr(nil, 4) })
1421          eq({ true, -1 }, { tv_list_find_nr(nil, 2) })
1422          eq({ true, -1 }, { tv_list_find_nr(nil, -3) })
1423
1424          alloc_log:check({})
1425        end)
1426        itp('errors out on out-of-range indexes', function()
1427          local l = list(int(1), int(2), int(3), int(4), int(5))
1428          alloc_log:clear()
1429
1430          eq({ true, -1 }, { tv_list_find_nr(l, -6) })
1431          eq({ true, -1 }, { tv_list_find_nr(l, 5) })
1432
1433          alloc_log:check({})
1434        end)
1435        itp('errors out on invalid types', function()
1436          local l = list(1, empty_list, {})
1437
1438          eq({ true, 0 }, { tv_list_find_nr(l, 0, 'E805: Using a Float as a Number') })
1439          eq({ true, 0 }, { tv_list_find_nr(l, 1, 'E745: Using a List as a Number') })
1440          eq({ true, 0 }, { tv_list_find_nr(l, 2, 'E728: Using a Dictionary as a Number') })
1441          eq({ true, 0 }, { tv_list_find_nr(l, -1, 'E728: Using a Dictionary as a Number') })
1442          eq({ true, 0 }, { tv_list_find_nr(l, -2, 'E745: Using a List as a Number') })
1443          eq({ true, 0 }, { tv_list_find_nr(l, -3, 'E805: Using a Float as a Number') })
1444        end)
1445      end)
1446      local function tv_list_find_str(l, n, msg)
1447        return check_emsg(function()
1448          local ret = lib.tv_list_find_str(l, n)
1449          local s = nil
1450          if ret ~= nil then
1451            s = ffi.string(ret)
1452          end
1453          return s
1454        end, msg)
1455      end
1456      describe('str()', function()
1457        itp('returns correct string', function()
1458          local l = list(int(1), 2.5, int(3), int(4), int(5))
1459          alloc_log:clear()
1460
1461          eq('1', tv_list_find_str(l, -5))
1462          eq('2.5', tv_list_find_str(l, 1))
1463          eq('5', tv_list_find_str(l, 4))
1464          eq('3', tv_list_find_str(l, 2))
1465          eq('3', tv_list_find_str(l, -3))
1466
1467          alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
1468        end)
1469        itp('returns string when used with VAR_STRING items', function()
1470          local l = list('1', '2', '3', '4', '5')
1471          alloc_log:clear()
1472
1473          eq('1', tv_list_find_str(l, -5))
1474          eq('5', tv_list_find_str(l, 4))
1475          eq('3', tv_list_find_str(l, 2))
1476          eq('3', tv_list_find_str(l, -3))
1477
1478          alloc_log:check({})
1479        end)
1480        itp('returns empty when used with NULL string', function()
1481          local l = list(null_string)
1482          alloc_log:clear()
1483
1484          eq('', tv_list_find_str(l, 0))
1485
1486          alloc_log:check({})
1487        end)
1488        itp('fails with error message when index is out of range', function()
1489          local l = list(int(1), int(2), int(3), int(4), int(5))
1490
1491          eq(nil, tv_list_find_str(l, -6, 'E684: List index out of range: -6'))
1492          eq(nil, tv_list_find_str(l, 5, 'E684: List index out of range: 5'))
1493        end)
1494        itp('fails with error message on invalid types', function()
1495          local l = list(empty_list, {})
1496
1497          eq('', tv_list_find_str(l, 0, 'E730: Using a List as a String'))
1498          eq('', tv_list_find_str(l, 1, 'E731: Using a Dictionary as a String'))
1499          eq('', tv_list_find_str(l, -1, 'E731: Using a Dictionary as a String'))
1500          eq('', tv_list_find_str(l, -2, 'E730: Using a List as a String'))
1501        end)
1502      end)
1503    end)
1504    describe('idx_of_item()', function()
1505      itp('works', function()
1506        local l = list(1, 2, 3, 4, 5)
1507        local l2 = list(42, empty_list)
1508        local lis = list_items(l)
1509        local lis2 = list_items(l2)
1510
1511        for i, li in ipairs(lis) do
1512          eq(i - 1, lib.tv_list_idx_of_item(l, li))
1513        end
1514        eq(-1, lib.tv_list_idx_of_item(l, lis2[1]))
1515        eq(-1, lib.tv_list_idx_of_item(l, nil))
1516        eq(-1, lib.tv_list_idx_of_item(nil, nil))
1517        eq(-1, lib.tv_list_idx_of_item(nil, lis[1]))
1518      end)
1519    end)
1520  end)
1521  describe('dict', function()
1522    describe('watcher', function()
1523      describe('add()/remove()', function()
1524        itp('works with an empty key', function()
1525          local d = dict({})
1526          eq({}, dict_watchers(d))
1527          local cb = ffi.gc(tbl2callback({ type = 'none' }), nil)
1528          alloc_log:clear()
1529          lib.tv_dict_watcher_add(d, '*', 0, cb[0])
1530          local ws, qs = dict_watchers(d)
1531          local key_p = qs[1].key_pattern
1532          alloc_log:check({
1533            a.dwatcher(qs[1]),
1534            a.str(key_p, 0),
1535          })
1536          eq({ { busy = false, cb = { type = 'none' }, pat = '' } }, ws)
1537          eq(true, lib.tv_dict_watcher_remove(d, 'x', 0, cb[0]))
1538          alloc_log:check({
1539            a.freed(key_p),
1540            a.freed(qs[1]),
1541          })
1542          eq({}, dict_watchers(d))
1543        end)
1544        itp('works with multiple callbacks', function()
1545          local d = dict({})
1546          eq({}, dict_watchers(d))
1547          alloc_log:check({ a.dict(d) })
1548          local cbs = {}
1549          cbs[1] = { 'te', ffi.gc(tbl2callback({ type = 'none' }), nil) }
1550          alloc_log:check({})
1551          cbs[2] = { 'foo', ffi.gc(tbl2callback({ type = 'fref', fref = 'tr' }), nil) }
1552          alloc_log:check({
1553            a.str(cbs[2][2].data.funcref, #'tr'),
1554          })
1555          cbs[3] = {
1556            'te',
1557            ffi.gc(
1558              tbl2callback({
1559                type = 'pt',
1560                fref = 'tr',
1561                pt = {
1562                  value = 'tr',
1563                  args = { 'test' },
1564                  dict = {},
1565                },
1566              }),
1567              nil
1568            ),
1569          }
1570          local pt3 = cbs[3][2].data.partial
1571          local pt3_argv = pt3.pt_argv
1572          local pt3_dict = pt3.pt_dict
1573          local pt3_name = pt3.pt_name
1574          local pt3_str_arg = pt3.pt_argv[0].vval.v_string
1575          alloc_log:check({
1576            a.lua_pt(pt3),
1577            a.lua_tvs(pt3_argv, pt3.pt_argc),
1578            a.str(pt3_str_arg, #'test'),
1579            a.dict(pt3_dict),
1580            a.str(pt3_name, #'tr'),
1581          })
1582          for _, v in ipairs(cbs) do
1583            lib.tv_dict_watcher_add(d, v[1], #v[1], v[2][0])
1584          end
1585          local ws, qs, kps = dict_watchers(d)
1586          eq({
1587            { busy = false, pat = cbs[1][1], cb = { type = 'none' } },
1588            { busy = false, pat = cbs[2][1], cb = { type = 'fref', fref = 'tr' } },
1589            {
1590              busy = false,
1591              pat = cbs[3][1],
1592              cb = {
1593                type = 'pt',
1594                fref = 'tr',
1595                pt = {
1596                  [type_key] = func_type,
1597                  value = 'tr',
1598                  args = { 'test' },
1599                  dict = {},
1600                },
1601              },
1602            },
1603          }, ws)
1604          alloc_log:check({
1605            a.dwatcher(qs[1]),
1606            a.str(kps[1][1], kps[1][2]),
1607            a.dwatcher(qs[2]),
1608            a.str(kps[2][1], kps[2][2]),
1609            a.dwatcher(qs[3]),
1610            a.str(kps[3][1], kps[3][2]),
1611          })
1612          eq(true, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
1613          alloc_log:check({
1614            a.freed(cbs[2][2].data.funcref),
1615            a.freed(kps[2][1]),
1616            a.freed(qs[2]),
1617          })
1618          eq(false, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
1619          eq({
1620            { busy = false, pat = cbs[1][1], cb = { type = 'none' } },
1621            {
1622              busy = false,
1623              pat = cbs[3][1],
1624              cb = {
1625                type = 'pt',
1626                fref = 'tr',
1627                pt = {
1628                  [type_key] = func_type,
1629                  value = 'tr',
1630                  args = { 'test' },
1631                  dict = {},
1632                },
1633              },
1634            },
1635          }, dict_watchers(d))
1636          eq(true, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
1637          alloc_log:check({
1638            a.freed(pt3_str_arg),
1639            a.freed(pt3_argv),
1640            a.freed(pt3_dict),
1641            a.freed(pt3_name),
1642            a.freed(pt3),
1643            a.freed(kps[3][1]),
1644            a.freed(qs[3]),
1645          })
1646          eq(false, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
1647          eq({ { busy = false, pat = cbs[1][1], cb = { type = 'none' } } }, dict_watchers(d))
1648          eq(true, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
1649          alloc_log:check({
1650            a.freed(kps[1][1]),
1651            a.freed(qs[1]),
1652          })
1653          eq(false, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
1654          eq({}, dict_watchers(d))
1655        end)
1656      end)
1657      describe('notify', function()
1658        -- Way too hard to test it here, functional tests in
1659        -- dict_notifications_spec.lua.
1660      end)
1661    end)
1662    describe('item', function()
1663      describe('alloc()/free()', function()
1664        local function check_tv_dict_item_alloc_len(s, len, tv, more_frees)
1665          local di
1666          if len == nil then
1667            di = ffi.gc(lib.tv_dict_item_alloc(s), nil)
1668            len = #s
1669          else
1670            di = ffi.gc(lib.tv_dict_item_alloc_len(s, len or #s), nil)
1671          end
1672          eq(s:sub(1, len), ffi.string(di.di_key))
1673          alloc_log:check({ a.di(di, len) })
1674          if tv then
1675            di.di_tv = ffi.gc(tv, nil)
1676          else
1677            di.di_tv.v_type = lib.VAR_UNKNOWN
1678          end
1679          lib.tv_dict_item_free(di)
1680          alloc_log:check(concat_tables(more_frees, { a.freed(di) }))
1681        end
1682        local function check_tv_dict_item_alloc(s, tv, more_frees)
1683          return check_tv_dict_item_alloc_len(s, nil, tv, more_frees)
1684        end
1685        itp('works', function()
1686          check_tv_dict_item_alloc('')
1687          check_tv_dict_item_alloc('t')
1688          check_tv_dict_item_alloc('TEST')
1689          check_tv_dict_item_alloc_len('', 0)
1690          check_tv_dict_item_alloc_len('TEST', 2)
1691          local tv = lua2typvalt('test')
1692          alloc_log:check({ a.str(tv.vval.v_string, #'test') })
1693          check_tv_dict_item_alloc('', tv, { a.freed(tv.vval.v_string) })
1694          tv = lua2typvalt('test')
1695          alloc_log:check({ a.str(tv.vval.v_string, #'test') })
1696          check_tv_dict_item_alloc_len('', 0, tv, { a.freed(tv.vval.v_string) })
1697        end)
1698      end)
1699      describe('add()/remove()', function()
1700        itp('works', function()
1701          local d = dict()
1702          eq({}, dct2tbl(d))
1703          alloc_log:check({ a.dict(d) })
1704          local di = ffi.gc(lib.tv_dict_item_alloc(''), nil)
1705          local tv = lua2typvalt('test')
1706          di.di_tv = ffi.gc(tv, nil)
1707          alloc_log:check({ a.di(di, ''), a.str(tv.vval.v_string, 'test') })
1708          eq(OK, lib.tv_dict_add(d, di))
1709          alloc_log:check({})
1710          eq(
1711            FAIL,
1712            check_emsg(function()
1713              return lib.tv_dict_add(d, di)
1714            end, 'E685: Internal error: hash_add(): duplicate key ""')
1715          )
1716          alloc_log:clear()
1717          lib.tv_dict_item_remove(d, di)
1718          alloc_log:check({
1719            a.freed(tv.vval.v_string),
1720            a.freed(di),
1721          })
1722        end)
1723      end)
1724    end)
1725    describe('indexing', function()
1726      describe('find()', function()
1727        local function tv_dict_find(d, key, key_len)
1728          local di = lib.tv_dict_find(d, key, key_len or #key)
1729          if di == nil then
1730            return nil, nil, nil
1731          end
1732          return typvalt2lua(di.di_tv), ffi.string(di.di_key), di
1733        end
1734        itp('works with NULL dict', function()
1735          eq(nil, lib.tv_dict_find(nil, '', 0))
1736          eq(nil, lib.tv_dict_find(nil, 'test', -1))
1737          eq(nil, lib.tv_dict_find(nil, nil, 0))
1738        end)
1739        itp('works with empty key', function()
1740          local lua_d = {
1741            [''] = 0,
1742            t = 1,
1743            te = 2,
1744            tes = 3,
1745            test = 4,
1746            testt = 5,
1747          }
1748          local d = dict(lua_d)
1749          alloc_log:clear()
1750          eq(lua_d, dct2tbl(d))
1751          alloc_log:check({})
1752          local dis = dict_items(d)
1753          eq({ 0, '', dis[''] }, { tv_dict_find(d, '', 0) })
1754        end)
1755        itp('works with len properly', function()
1756          local lua_d = {
1757            [''] = 0,
1758            t = 1,
1759            te = 2,
1760            tes = 3,
1761            test = 4,
1762            testt = 5,
1763          }
1764          local d = dict(lua_d)
1765          alloc_log:clear()
1766          eq(lua_d, dct2tbl(d))
1767          alloc_log:check({})
1768          for i = 0, 5 do
1769            local v, k = tv_dict_find(d, 'testt', i)
1770            eq({ i, ('testt'):sub(1, i) }, { v, k })
1771          end
1772          eq(nil, tv_dict_find(d, 'testt', 6)) -- Should take NUL byte
1773          eq(5, tv_dict_find(d, 'testt', -1))
1774          alloc_log:check({})
1775        end)
1776      end)
1777      describe('get_number()', function()
1778        itp('works with NULL dict', function()
1779          eq(
1780            0,
1781            check_emsg(function()
1782              return lib.tv_dict_get_number(nil, 'test')
1783            end, nil)
1784          )
1785        end)
1786        itp('works', function()
1787          local d = ffi.gc(dict({ test = {} }), nil)
1788          eq(
1789            0,
1790            check_emsg(function()
1791              return lib.tv_dict_get_number(d, 'test')
1792            end, 'E728: Using a Dictionary as a Number')
1793          )
1794          d = ffi.gc(dict({ tes = int(42), t = 44, te = '43' }), nil)
1795          alloc_log:clear()
1796          eq(
1797            0,
1798            check_emsg(function()
1799              return lib.tv_dict_get_number(d, 'test')
1800            end, nil)
1801          )
1802          eq(
1803            42,
1804            check_emsg(function()
1805              return lib.tv_dict_get_number(d, 'tes')
1806            end, nil)
1807          )
1808          eq(
1809            43,
1810            check_emsg(function()
1811              return lib.tv_dict_get_number(d, 'te')
1812            end, nil)
1813          )
1814          alloc_log:check({})
1815          eq(
1816            0,
1817            check_emsg(function()
1818              return lib.tv_dict_get_number(d, 't')
1819            end, 'E805: Using a Float as a Number')
1820          )
1821        end)
1822      end)
1823      describe('get_string()', function()
1824        itp('works with NULL dict', function()
1825          eq(
1826            nil,
1827            check_emsg(function()
1828              return lib.tv_dict_get_string(nil, 'test', false)
1829            end, nil)
1830          )
1831        end)
1832        itp('works', function()
1833          local d = ffi.gc(dict({ test = {} }), nil)
1834          eq(
1835            '',
1836            ffi.string(check_emsg(function()
1837              return lib.tv_dict_get_string(d, 'test', false)
1838            end, 'E731: Using a Dictionary as a String'))
1839          )
1840          d = ffi.gc(dict({ tes = int(42), t = 44, te = '43', xx = int(45) }), nil)
1841          alloc_log:clear()
1842          local dis = dict_items(d)
1843          eq(
1844            nil,
1845            check_emsg(function()
1846              return lib.tv_dict_get_string(d, 'test', false)
1847            end, nil)
1848          )
1849          local s42 = check_emsg(function()
1850            return lib.tv_dict_get_string(d, 'tes', false)
1851          end, nil)
1852          eq('42', ffi.string(s42))
1853          local s45 = check_emsg(function()
1854            return lib.tv_dict_get_string(d, 'xx', false)
1855          end, nil)
1856          eq(s42, s45)
1857          eq('45', ffi.string(s45))
1858          eq('45', ffi.string(s42))
1859          local s43 = check_emsg(function()
1860            return lib.tv_dict_get_string(d, 'te', false)
1861          end, nil)
1862          eq('43', ffi.string(s43))
1863          neq(s42, s43)
1864          eq(s43, dis.te.di_tv.vval.v_string)
1865          alloc_log:check({})
1866          local s44 = check_emsg(function()
1867            return lib.tv_dict_get_string(d, 't', false)
1868          end, nil)
1869          eq('44.0', ffi.string(s44))
1870          alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
1871        end)
1872        itp('allocates a string copy when requested', function()
1873          local function tv_dict_get_string_alloc(d, key, emsg, is_float)
1874            alloc_log:clear()
1875            local ret = check_emsg(function()
1876              return lib.tv_dict_get_string(d, key, true)
1877            end, emsg)
1878            local s_ret = (ret ~= nil) and ffi.string(ret) or nil
1879            if not emsg then
1880              if s_ret then
1881                if is_float then
1882                  alloc_log:check({
1883                    a.freed(alloc_log.null),
1884                    a.freed(alloc_log.null),
1885                    a.str(ret, s_ret),
1886                  })
1887                else
1888                  alloc_log:check({ a.str(ret, s_ret) })
1889                end
1890              else
1891                alloc_log:check({})
1892              end
1893            end
1894            lib.xfree(ret)
1895            return s_ret
1896          end
1897          local d = ffi.gc(dict({ test = {} }), nil)
1898          eq('', tv_dict_get_string_alloc(d, 'test', 'E731: Using a Dictionary as a String'))
1899          d = ffi.gc(dict({ tes = int(42), t = 44, te = '43', xx = int(45) }), nil)
1900          alloc_log:clear()
1901          eq(nil, tv_dict_get_string_alloc(d, 'test'))
1902          eq('42', tv_dict_get_string_alloc(d, 'tes'))
1903          eq('45', tv_dict_get_string_alloc(d, 'xx'))
1904          eq('43', tv_dict_get_string_alloc(d, 'te'))
1905          eq('44.0', tv_dict_get_string_alloc(d, 't', nil, true))
1906        end)
1907      end)
1908      describe('get_string_buf()', function()
1909        local function tv_dict_get_string_buf(d, key, is_float, buf, emsg)
1910          buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
1911          alloc_log:clear()
1912          local ret = check_emsg(function()
1913            return lib.tv_dict_get_string_buf(d, key, buf)
1914          end, emsg)
1915          local s_ret = (ret ~= nil) and ffi.string(ret) or nil
1916          if not emsg then
1917            if is_float then
1918              alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
1919            else
1920              alloc_log:check({})
1921            end
1922          end
1923          return s_ret, ret, buf
1924        end
1925        itp('works with NULL dict', function()
1926          eq(nil, tv_dict_get_string_buf(nil, 'test'))
1927        end)
1928        itp('works', function()
1929          local lua_d = {
1930            [''] = {},
1931            t = 1,
1932            te = int(2),
1933            tes = empty_list,
1934            test = 'tset',
1935            testt = 5,
1936          }
1937          local d = dict(lua_d)
1938          alloc_log:clear()
1939          eq(lua_d, dct2tbl(d))
1940          alloc_log:check({})
1941          local s, r, b
1942          s, r, b = tv_dict_get_string_buf(d, 'test')
1943          neq(r, b)
1944          eq('tset', s)
1945          s, r, b = tv_dict_get_string_buf(d, 't', true)
1946          eq(r, b)
1947          eq('1.0', s)
1948          s, r, b = tv_dict_get_string_buf(d, 'te')
1949          eq(r, b)
1950          eq('2', s)
1951        end)
1952      end)
1953      describe('get_string_buf_chk()', function()
1954        local function tv_dict_get_string_buf_chk(d, key, is_float, len, buf, def, emsg)
1955          buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
1956          def = def or ffi.gc(lib.xstrdup('DEFAULT'), lib.xfree)
1957          len = len or #key
1958          alloc_log:clear()
1959          local ret = check_emsg(function()
1960            return lib.tv_dict_get_string_buf_chk(d, key, len, buf, def)
1961          end, emsg)
1962          local s_ret = (ret ~= nil) and ffi.string(ret) or nil
1963          if not emsg then
1964            if is_float then
1965              alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
1966            else
1967              alloc_log:check({})
1968            end
1969          end
1970          return s_ret, ret, buf, def
1971        end
1972        itp('works with NULL dict', function()
1973          eq('DEFAULT', tv_dict_get_string_buf_chk(nil, 'test'))
1974        end)
1975        itp('works', function()
1976          local lua_d = {
1977            [''] = {},
1978            t = 1,
1979            te = int(2),
1980            tes = empty_list,
1981            test = 'tset',
1982            testt = 5,
1983          }
1984          local d = dict(lua_d)
1985          alloc_log:clear()
1986          eq(lua_d, dct2tbl(d))
1987          alloc_log:check({})
1988          local s, r, b, def
1989          s, r, b, def = tv_dict_get_string_buf_chk(d, 'test')
1990          neq(r, b)
1991          neq(r, def)
1992          eq('tset', s)
1993          s, r, b, def = tv_dict_get_string_buf_chk(d, 'test', true, 1)
1994          eq(r, b)
1995          neq(r, def)
1996          eq('1.0', s)
1997          s, r, b, def = tv_dict_get_string_buf_chk(d, 'te')
1998          eq(r, b)
1999          neq(r, def)
2000          eq('2', s)
2001          s, r, b, def = tv_dict_get_string_buf_chk(d, 'TEST')
2002          eq(r, def)
2003          neq(r, b)
2004          eq('DEFAULT', s)
2005        end)
2006      end)
2007      describe('get_callback()', function()
2008        local function tv_dict_get_callback(d, key, key_len, emsg)
2009          key_len = key_len or #key
2010          local cb =
2011            ffi.gc(ffi.cast('Callback*', lib.xmalloc(ffi.sizeof('Callback'))), lib.callback_free)
2012          alloc_log:clear()
2013          local ret = check_emsg(function()
2014            return lib.tv_dict_get_callback(d, key, key_len, cb)
2015          end, emsg)
2016          local cb_lua = callback2tbl(cb[0])
2017          return cb_lua, ret
2018        end
2019        itp('works with NULL dict', function()
2020          eq({ { type = 'none' }, true }, { tv_dict_get_callback(nil, '') })
2021        end)
2022        itp('works', function()
2023          local lua_d = {
2024            [''] = 'tr',
2025            t = int(1),
2026            te = { [type_key] = func_type, value = 'tr' },
2027            tes = { [type_key] = func_type, value = 'tr', args = { 'a', 'b' } },
2028            test = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = {} },
2029            testt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = { 1 } },
2030          }
2031          local d = dict(lua_d)
2032          eq(lua_d, dct2tbl(d))
2033          eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, '', -1) })
2034          eq({ { type = 'none' }, true }, { tv_dict_get_callback(d, 'x', -1) })
2035          eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, 'testt', 0) })
2036          eq({ { type = 'none' }, false }, {
2037            tv_dict_get_callback(
2038              d,
2039              'test',
2040              1,
2041              'E6000: Argument is not a function or function name'
2042            ),
2043          })
2044          eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, 'testt', 2) })
2045          eq({
2046            {
2047              type = 'pt',
2048              fref = 'tr',
2049              pt = {
2050                [type_key] = func_type,
2051                value = 'tr',
2052                args = { 'a', 'b' },
2053              },
2054            },
2055            true,
2056          }, { tv_dict_get_callback(d, 'testt', 3) })
2057          eq({
2058            {
2059              type = 'pt',
2060              fref = 'Test',
2061              pt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = {} },
2062            },
2063            true,
2064          }, { tv_dict_get_callback(d, 'testt', 4) })
2065          eq({
2066            {
2067              type = 'pt',
2068              fref = 'Test',
2069              pt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = { 1 } },
2070            },
2071            true,
2072          }, { tv_dict_get_callback(d, 'testt', 5) })
2073        end)
2074      end)
2075    end)
2076    describe('add', function()
2077      describe('()', function()
2078        itp('works', function()
2079          local di = lib.tv_dict_item_alloc_len('t-est', 5)
2080          alloc_log:check({ a.di(di, 't-est') })
2081          di.di_tv.v_type = lib.VAR_NUMBER
2082          di.di_tv.vval.v_number = 42
2083          local d = dict({ test = 10 })
2084          local dis = dict_items(d)
2085          alloc_log:check({
2086            a.dict(d),
2087            a.di(dis.test, 'test'),
2088          })
2089          eq({ test = 10 }, dct2tbl(d))
2090          alloc_log:clear()
2091          eq(OK, lib.tv_dict_add(d, di))
2092          alloc_log:check({})
2093          eq({ test = 10, ['t-est'] = int(42) }, dct2tbl(d))
2094          eq(
2095            FAIL,
2096            check_emsg(function()
2097              return lib.tv_dict_add(d, di)
2098            end, 'E685: Internal error: hash_add(): duplicate key "t-est"')
2099          )
2100        end)
2101      end)
2102      describe('list()', function()
2103        itp('works', function()
2104          local l = list(1, 2, 3)
2105          alloc_log:clear()
2106          eq(1, l.lv_refcount)
2107          local d = dict({ test = 10 })
2108          alloc_log:clear()
2109          eq({ test = 10 }, dct2tbl(d))
2110          eq(OK, lib.tv_dict_add_list(d, 'testt', 3, l))
2111          local dis = dict_items(d)
2112          alloc_log:check({ a.di(dis.tes, 'tes') })
2113          eq({ test = 10, tes = { 1, 2, 3 } }, dct2tbl(d))
2114          eq(2, l.lv_refcount)
2115          eq(
2116            FAIL,
2117            check_emsg(function()
2118              return lib.tv_dict_add_list(d, 'testt', 3, l)
2119            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2120          )
2121          eq(2, l.lv_refcount)
2122          alloc_log:clear()
2123          lib.emsg_skip = lib.emsg_skip + 1
2124          eq(
2125            FAIL,
2126            check_emsg(function()
2127              return lib.tv_dict_add_list(d, 'testt', 3, l)
2128            end, nil)
2129          )
2130          eq(2, l.lv_refcount)
2131          lib.emsg_skip = lib.emsg_skip - 1
2132          alloc_log:clear_tmp_allocs()
2133          alloc_log:check({})
2134        end)
2135      end)
2136      describe('dict()', function()
2137        itp('works', function()
2138          local d2 = dict({ foo = 42 })
2139          alloc_log:clear()
2140          eq(1, d2.dv_refcount)
2141          local d = dict({ test = 10 })
2142          alloc_log:clear()
2143          eq({ test = 10 }, dct2tbl(d))
2144          eq(OK, lib.tv_dict_add_dict(d, 'testt', 3, d2))
2145          local dis = dict_items(d)
2146          alloc_log:check({ a.di(dis.tes, 'tes') })
2147          eq({ test = 10, tes = { foo = 42 } }, dct2tbl(d))
2148          eq(2, d2.dv_refcount)
2149          eq(
2150            FAIL,
2151            check_emsg(function()
2152              return lib.tv_dict_add_dict(d, 'testt', 3, d2)
2153            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2154          )
2155          eq(2, d2.dv_refcount)
2156          alloc_log:clear()
2157          lib.emsg_skip = lib.emsg_skip + 1
2158          eq(
2159            FAIL,
2160            check_emsg(function()
2161              return lib.tv_dict_add_dict(d, 'testt', 3, d2)
2162            end, nil)
2163          )
2164          eq(2, d2.dv_refcount)
2165          lib.emsg_skip = lib.emsg_skip - 1
2166          alloc_log:clear_tmp_allocs()
2167          alloc_log:check({})
2168        end)
2169      end)
2170      describe('nr()', function()
2171        itp('works', function()
2172          local d = dict({ test = 10 })
2173          alloc_log:clear()
2174          eq({ test = 10 }, dct2tbl(d))
2175          eq(OK, lib.tv_dict_add_nr(d, 'testt', 3, 2))
2176          local dis = dict_items(d)
2177          alloc_log:check({ a.di(dis.tes, 'tes') })
2178          eq({ test = 10, tes = int(2) }, dct2tbl(d))
2179          eq(
2180            FAIL,
2181            check_emsg(function()
2182              return lib.tv_dict_add_nr(d, 'testt', 3, 2)
2183            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2184          )
2185          alloc_log:clear()
2186          lib.emsg_skip = lib.emsg_skip + 1
2187          eq(
2188            FAIL,
2189            check_emsg(function()
2190              return lib.tv_dict_add_nr(d, 'testt', 3, 2)
2191            end, nil)
2192          )
2193          lib.emsg_skip = lib.emsg_skip - 1
2194          alloc_log:clear_tmp_allocs()
2195          alloc_log:check({})
2196        end)
2197      end)
2198      describe('float()', function()
2199        itp('works', function()
2200          local d = dict({ test = 10 })
2201          alloc_log:clear()
2202          eq({ test = 10 }, dct2tbl(d))
2203          eq(OK, lib.tv_dict_add_float(d, 'testt', 3, 1.5))
2204          local dis = dict_items(d)
2205          alloc_log:check({ a.di(dis.tes, 'tes') })
2206          eq({ test = 10, tes = 1.5 }, dct2tbl(d))
2207          eq(
2208            FAIL,
2209            check_emsg(function()
2210              return lib.tv_dict_add_float(d, 'testt', 3, 1.5)
2211            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2212          )
2213          alloc_log:clear()
2214          lib.emsg_skip = lib.emsg_skip + 1
2215          eq(
2216            FAIL,
2217            check_emsg(function()
2218              return lib.tv_dict_add_float(d, 'testt', 3, 1.5)
2219            end, nil)
2220          )
2221          lib.emsg_skip = lib.emsg_skip - 1
2222          alloc_log:clear_tmp_allocs()
2223          alloc_log:check({})
2224        end)
2225      end)
2226      describe('str()', function()
2227        itp('works', function()
2228          local d = dict({ test = 10 })
2229          alloc_log:clear()
2230          eq({ test = 10 }, dct2tbl(d))
2231          eq(OK, lib.tv_dict_add_str(d, 'testt', 3, 'TEST'))
2232          local dis = dict_items(d)
2233          alloc_log:check({
2234            a.str(dis.tes.di_tv.vval.v_string, 'TEST'),
2235            a.di(dis.tes, 'tes'),
2236          })
2237          eq({ test = 10, tes = 'TEST' }, dct2tbl(d))
2238          eq(
2239            FAIL,
2240            check_emsg(function()
2241              return lib.tv_dict_add_str(d, 'testt', 3, 'TEST')
2242            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2243          )
2244          alloc_log:clear()
2245          lib.emsg_skip = lib.emsg_skip + 1
2246          eq(
2247            FAIL,
2248            check_emsg(function()
2249              return lib.tv_dict_add_str(d, 'testt', 3, 'TEST')
2250            end, nil)
2251          )
2252          lib.emsg_skip = lib.emsg_skip - 1
2253          alloc_log:clear_tmp_allocs()
2254          alloc_log:check({})
2255        end)
2256      end)
2257      describe('allocated_str()', function()
2258        itp('works', function()
2259          local d = dict({ test = 10 })
2260          eq({ test = 10 }, dct2tbl(d))
2261          alloc_log:clear()
2262          local s1 = lib.xstrdup('TEST')
2263          local s2 = lib.xstrdup('TEST')
2264          local s3 = lib.xstrdup('TEST')
2265          alloc_log:check({
2266            a.str(s1, 'TEST'),
2267            a.str(s2, 'TEST'),
2268            a.str(s3, 'TEST'),
2269          })
2270          eq(OK, lib.tv_dict_add_allocated_str(d, 'testt', 3, s1))
2271          local dis = dict_items(d)
2272          alloc_log:check({
2273            a.di(dis.tes, 'tes'),
2274          })
2275          eq({ test = 10, tes = 'TEST' }, dct2tbl(d))
2276          eq(
2277            FAIL,
2278            check_emsg(function()
2279              return lib.tv_dict_add_allocated_str(d, 'testt', 3, s2)
2280            end, 'E685: Internal error: hash_add(): duplicate key "tes"')
2281          )
2282          alloc_log:clear()
2283          lib.emsg_skip = lib.emsg_skip + 1
2284          eq(
2285            FAIL,
2286            check_emsg(function()
2287              return lib.tv_dict_add_allocated_str(d, 'testt', 3, s3)
2288            end, nil)
2289          )
2290          lib.emsg_skip = lib.emsg_skip - 1
2291          alloc_log:clear_tmp_allocs()
2292          alloc_log:check({
2293            a.freed(s3),
2294          })
2295        end)
2296      end)
2297    end)
2298    describe('clear()', function()
2299      itp('works', function()
2300        local d = dict()
2301        alloc_log:check({ a.dict(d) })
2302        eq({}, dct2tbl(d))
2303        lib.tv_dict_clear(d)
2304        eq({}, dct2tbl(d))
2305        lib.tv_dict_add_str(d, 'TEST', 3, 'tEsT')
2306        local dis = dict_items(d)
2307        local di = dis.TES
2308        local di_s = di.di_tv.vval.v_string
2309        alloc_log:check({ a.str(di_s), a.di(di) })
2310        eq({ TES = 'tEsT' }, dct2tbl(d))
2311        lib.tv_dict_clear(d)
2312        alloc_log:check({ a.freed(di_s), a.freed(di) })
2313        eq({}, dct2tbl(d))
2314      end)
2315    end)
2316    describe('extend()', function()
2317      local function tv_dict_extend(d1, d2, action, emsg)
2318        action = action or 'force'
2319        check_emsg(function()
2320          return lib.tv_dict_extend(d1, d2, action)
2321        end, emsg)
2322      end
2323      itp('works', function()
2324        local d1 = dict()
2325        alloc_log:check({ a.dict(d1) })
2326        eq({}, dct2tbl(d1))
2327        local d2 = dict()
2328        alloc_log:check({ a.dict(d2) })
2329        eq({}, dct2tbl(d2))
2330        tv_dict_extend(d1, d2, 'error')
2331        tv_dict_extend(d1, d2, 'keep')
2332        tv_dict_extend(d1, d2, 'force')
2333        alloc_log:check({})
2334
2335        d1 = dict({ a = 'TEST' })
2336        eq({ a = 'TEST' }, dct2tbl(d1))
2337        local dis1 = dict_items(d1)
2338        local a1_s = dis1.a.di_tv.vval.v_string
2339        alloc_log:clear_tmp_allocs()
2340        alloc_log:check({
2341          a.dict(d1),
2342          a.di(dis1.a),
2343          a.str(a1_s),
2344        })
2345        d2 = dict({ a = 'TSET' })
2346        eq({ a = 'TSET' }, dct2tbl(d2))
2347        local dis2 = dict_items(d2)
2348        local a2_s = dis2.a.di_tv.vval.v_string
2349        alloc_log:clear_tmp_allocs()
2350        alloc_log:check({
2351          a.dict(d2),
2352          a.di(dis2.a),
2353          a.str(a2_s),
2354        })
2355
2356        tv_dict_extend(d1, d2, 'error', 'E737: Key already exists: a')
2357        eq({ a = 'TEST' }, dct2tbl(d1))
2358        eq({ a = 'TSET' }, dct2tbl(d2))
2359        alloc_log:clear()
2360
2361        tv_dict_extend(d1, d2, 'keep')
2362        alloc_log:check({})
2363        eq({ a = 'TEST' }, dct2tbl(d1))
2364        eq({ a = 'TSET' }, dct2tbl(d2))
2365
2366        tv_dict_extend(d1, d2, 'force')
2367        alloc_log:check({
2368          a.freed(a1_s),
2369          a.str(dis1.a.di_tv.vval.v_string),
2370        })
2371        eq({ a = 'TSET' }, dct2tbl(d1))
2372        eq({ a = 'TSET' }, dct2tbl(d2))
2373      end)
2374      pending('disallows overriding builtin or user functions: here be the dragons', function()
2375        -- pending: see TODO below
2376        local d = dict()
2377        d.dv_scope = lib.VAR_DEF_SCOPE
2378        local f_lua = {
2379          [type_key] = func_type,
2380          value = 'tr',
2381        }
2382        local f_tv = lua2typvalt(f_lua)
2383        local p_lua = {
2384          [type_key] = func_type,
2385          value = 'tr',
2386          args = { 1 },
2387        }
2388        local p_tv = lua2typvalt(p_lua)
2389        eq(lib.VAR_PARTIAL, p_tv.v_type)
2390        local d2 = dict({ tr = f_tv })
2391        local d3 = dict({ tr = p_tv })
2392        local d4 = dict({ ['TEST:THIS'] = p_tv })
2393        local d5 = dict({ Test = f_tv })
2394        local d6 = dict({ Test = p_tv })
2395        eval0([[execute("function Test()\nendfunction")]])
2396        -- TODO: test breaks at this point
2397        tv_dict_extend(d, d2, 'force', 'E704: Funcref variable name must start with a capital: tr')
2398        tv_dict_extend(d, d3, 'force', 'E704: Funcref variable name must start with a capital: tr')
2399        tv_dict_extend(d, d4, 'force', 'E461: Illegal variable name: TEST:THIS')
2400        tv_dict_extend(d, d5, 'force', 'E705: Variable name conflicts with existing function: Test')
2401        tv_dict_extend(d, d6, 'force', 'E705: Variable name conflicts with existing function: Test')
2402        eq({}, dct2tbl(d))
2403        d.dv_scope = lib.VAR_SCOPE
2404        tv_dict_extend(d, d4, 'force', 'E461: Illegal variable name: TEST:THIS')
2405        eq({}, dct2tbl(d))
2406        tv_dict_extend(d, d2, 'force')
2407        eq({ tr = f_lua }, dct2tbl(d))
2408        tv_dict_extend(d, d3, 'force')
2409        eq({ tr = p_lua }, dct2tbl(d))
2410        tv_dict_extend(d, d5, 'force')
2411        eq({ tr = p_lua, Test = f_lua }, dct2tbl(d))
2412        tv_dict_extend(d, d6, 'force')
2413        eq({ tr = p_lua, Test = p_lua }, dct2tbl(d))
2414      end)
2415      itp('cares about locks and read-only items', function()
2416        local d_lua = { tv_locked = 1, tv_fixed = 2, di_ro = 3, di_ro_sbx = 4 }
2417        local d = dict(d_lua)
2418        local dis = dict_items(d)
2419        dis.tv_locked.di_tv.v_lock = lib.VAR_LOCKED
2420        dis.tv_fixed.di_tv.v_lock = lib.VAR_FIXED
2421        dis.di_ro.di_flags = bit.bor(dis.di_ro.di_flags, lib.DI_FLAGS_RO)
2422        dis.di_ro_sbx.di_flags = bit.bor(dis.di_ro_sbx.di_flags, lib.DI_FLAGS_RO_SBX)
2423        lib.sandbox = true
2424        local d1 = dict({ tv_locked = 41 })
2425        local d2 = dict({ tv_fixed = 42 })
2426        local d3 = dict({ di_ro = 43 })
2427        local d4 = dict({ di_ro_sbx = 44 })
2428        tv_dict_extend(d, d1, 'force', 'E741: Value is locked: extend() argument')
2429        tv_dict_extend(d, d2, 'force', 'E742: Cannot change value of extend() argument')
2430        tv_dict_extend(d, d3, 'force', 'E46: Cannot change read-only variable "extend() argument"')
2431        tv_dict_extend(
2432          d,
2433          d4,
2434          'force',
2435          'E794: Cannot set variable in the sandbox: "extend() argument"'
2436        )
2437        eq(d_lua, dct2tbl(d))
2438        lib.sandbox = false
2439        tv_dict_extend(d, d4, 'force')
2440        d_lua.di_ro_sbx = 44
2441        eq(d_lua, dct2tbl(d))
2442      end)
2443    end)
2444    describe('equal()', function()
2445      local function tv_dict_equal(d1, d2, ic)
2446        return lib.tv_dict_equal(d1, d2, ic or false)
2447      end
2448      itp('works', function()
2449        eq(true, tv_dict_equal(nil, nil))
2450        local d1 = dict()
2451        alloc_log:check({ a.dict(d1) })
2452        eq(1, d1.dv_refcount)
2453        eq(true, tv_dict_equal(nil, d1))
2454        eq(true, tv_dict_equal(d1, nil))
2455        eq(true, tv_dict_equal(d1, d1))
2456        eq(1, d1.dv_refcount)
2457        alloc_log:check({})
2458        local d_upper = dict({ a = 'TEST' })
2459        local dis_upper = dict_items(d_upper)
2460        local d_lower = dict({ a = 'test' })
2461        local dis_lower = dict_items(d_lower)
2462        local d_kupper_upper = dict({ A = 'TEST' })
2463        local dis_kupper_upper = dict_items(d_kupper_upper)
2464        local d_kupper_lower = dict({ A = 'test' })
2465        local dis_kupper_lower = dict_items(d_kupper_lower)
2466        alloc_log:clear_tmp_allocs()
2467        alloc_log:check({
2468          a.dict(d_upper),
2469          a.di(dis_upper.a),
2470          a.str(dis_upper.a.di_tv.vval.v_string),
2471
2472          a.dict(d_lower),
2473          a.di(dis_lower.a),
2474          a.str(dis_lower.a.di_tv.vval.v_string),
2475
2476          a.dict(d_kupper_upper),
2477          a.di(dis_kupper_upper.A),
2478          a.str(dis_kupper_upper.A.di_tv.vval.v_string),
2479
2480          a.dict(d_kupper_lower),
2481          a.di(dis_kupper_lower.A),
2482          a.str(dis_kupper_lower.A.di_tv.vval.v_string),
2483        })
2484        eq(true, tv_dict_equal(d_upper, d_upper))
2485        eq(true, tv_dict_equal(d_upper, d_upper, true))
2486        eq(false, tv_dict_equal(d_upper, d_lower, false))
2487        eq(true, tv_dict_equal(d_upper, d_lower, true))
2488        eq(true, tv_dict_equal(d_kupper_upper, d_kupper_lower, true))
2489        eq(false, tv_dict_equal(d_kupper_upper, d_lower, true))
2490        eq(false, tv_dict_equal(d_kupper_upper, d_upper, true))
2491        alloc_log:check({})
2492      end)
2493    end)
2494    describe('copy()', function()
2495      local function tv_dict_copy(...)
2496        return ffi.gc(lib.tv_dict_copy(...), lib.tv_dict_unref)
2497      end
2498      itp('copies NULL correctly', function()
2499        eq(nil, lib.tv_dict_copy(nil, nil, true, 0))
2500        eq(nil, lib.tv_dict_copy(nil, nil, false, 0))
2501        eq(nil, lib.tv_dict_copy(nil, nil, true, 1))
2502        eq(nil, lib.tv_dict_copy(nil, nil, false, 1))
2503      end)
2504      itp('copies dict correctly without converting items', function()
2505        do
2506          local v = {
2507            a = { ['«'] = '»' },
2508            b = { '„' },
2509            ['1'] = 1,
2510            ['«»'] = '“',
2511            ns = null_string,
2512            nl = null_list,
2513            nd = null_dict,
2514          }
2515          local d_tv = lua2typvalt(v)
2516          local d = d_tv.vval.v_dict
2517          local dis = dict_items(d)
2518          alloc_log:clear()
2519
2520          eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
2521          eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
2522          local d_copy1 = tv_dict_copy(nil, d, false, 0)
2523          eq(2, dis.a.di_tv.vval.v_dict.dv_refcount)
2524          eq(2, dis.b.di_tv.vval.v_list.lv_refcount)
2525          local dis_copy1 = dict_items(d_copy1)
2526          eq(dis.a.di_tv.vval.v_dict, dis_copy1.a.di_tv.vval.v_dict)
2527          eq(dis.b.di_tv.vval.v_list, dis_copy1.b.di_tv.vval.v_list)
2528          eq(v, dct2tbl(d_copy1))
2529          alloc_log:clear()
2530          lib.tv_dict_free(ffi.gc(d_copy1, nil))
2531          alloc_log:clear()
2532
2533          eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
2534          eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
2535          local d_deepcopy1 = tv_dict_copy(nil, d, true, 0)
2536          neq(nil, d_deepcopy1)
2537          eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
2538          eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
2539          local dis_deepcopy1 = dict_items(d_deepcopy1)
2540          neq(dis.a.di_tv.vval.v_dict, dis_deepcopy1.a.di_tv.vval.v_dict)
2541          neq(dis.b.di_tv.vval.v_list, dis_deepcopy1.b.di_tv.vval.v_list)
2542          eq(v, dct2tbl(d_deepcopy1))
2543          alloc_log:clear()
2544        end
2545        collectgarbage()
2546      end)
2547      itp('copies dict correctly and converts items', function()
2548        local vc = vimconv_alloc()
2549        -- UTF-8 ↔ latin1 conversions need no iconv
2550        eq(OK, lib.convert_setup(vc, to_cstr('utf-8'), to_cstr('latin1')))
2551
2552        local v = {
2553          a = { ['«'] = '»' },
2554          b = { '„' },
2555          ['1'] = 1,
2556          ['«»'] = '“',
2557          ns = null_string,
2558          nl = null_list,
2559          nd = null_dict,
2560        }
2561        local d_tv = lua2typvalt(v)
2562        local d = d_tv.vval.v_dict
2563        local dis = dict_items(d)
2564        alloc_log:clear()
2565
2566        eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
2567        eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
2568        local d_deepcopy1 = tv_dict_copy(vc, d, true, 0)
2569        neq(nil, d_deepcopy1)
2570        eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
2571        eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
2572        local dis_deepcopy1 = dict_items(d_deepcopy1)
2573        neq(dis.a.di_tv.vval.v_dict, dis_deepcopy1.a.di_tv.vval.v_dict)
2574        neq(dis.b.di_tv.vval.v_list, dis_deepcopy1.b.di_tv.vval.v_list)
2575        eq({
2576          a = { ['\171'] = '\187' },
2577          b = { '\191' },
2578          ['1'] = 1,
2579          ['\171\187'] = '\191',
2580          ns = null_string,
2581          nl = null_list,
2582          nd = null_dict,
2583        }, dct2tbl(d_deepcopy1))
2584        alloc_log:clear_tmp_allocs()
2585        alloc_log:clear()
2586      end)
2587      itp('returns different/same containers with(out) copyID', function()
2588        local d_inner_tv = lua2typvalt({})
2589        local d_tv = lua2typvalt({ a = d_inner_tv, b = d_inner_tv })
2590        eq(3, d_inner_tv.vval.v_dict.dv_refcount)
2591        local d = d_tv.vval.v_dict
2592        local dis = dict_items(d)
2593        eq(dis.a.di_tv.vval.v_dict, dis.b.di_tv.vval.v_dict)
2594
2595        local d_copy1 = tv_dict_copy(nil, d, true, 0)
2596        local dis_copy1 = dict_items(d_copy1)
2597        neq(dis_copy1.a.di_tv.vval.v_dict, dis_copy1.b.di_tv.vval.v_dict)
2598        eq({ a = {}, b = {} }, dct2tbl(d_copy1))
2599
2600        local d_copy2 = tv_dict_copy(nil, d, true, 2)
2601        local dis_copy2 = dict_items(d_copy2)
2602        eq(dis_copy2.a.di_tv.vval.v_dict, dis_copy2.b.di_tv.vval.v_dict)
2603        eq({ a = {}, b = {} }, dct2tbl(d_copy2))
2604
2605        eq(3, d_inner_tv.vval.v_dict.dv_refcount)
2606      end)
2607      itp('works with self-referencing dict with copyID', function()
2608        local d_tv = lua2typvalt({})
2609        local d = d_tv.vval.v_dict
2610        eq(1, d.dv_refcount)
2611        lib.tv_dict_add_dict(d, 'test', 4, d)
2612        eq(2, d.dv_refcount)
2613
2614        local d_copy1 = tv_dict_copy(nil, d, true, 2)
2615        eq(2, d_copy1.dv_refcount)
2616        local v = {}
2617        v.test = v
2618        eq(v, dct2tbl(d_copy1))
2619
2620        lib.tv_dict_clear(d)
2621        eq(1, d.dv_refcount)
2622
2623        lib.tv_dict_clear(d_copy1)
2624        eq(1, d_copy1.dv_refcount)
2625      end)
2626    end)
2627    describe('set_keys_readonly()', function()
2628      itp('works', function()
2629        local d = dict({ a = true })
2630        local dis = dict_items(d)
2631        alloc_log:check({ a.dict(d), a.di(dis.a) })
2632        eq(0, bit.band(dis.a.di_flags, lib.DI_FLAGS_RO))
2633        eq(0, bit.band(dis.a.di_flags, lib.DI_FLAGS_FIX))
2634        lib.tv_dict_set_keys_readonly(d)
2635        alloc_log:check({})
2636        eq(lib.DI_FLAGS_RO, bit.band(dis.a.di_flags, lib.DI_FLAGS_RO))
2637        eq(lib.DI_FLAGS_FIX, bit.band(dis.a.di_flags, lib.DI_FLAGS_FIX))
2638      end)
2639    end)
2640  end)
2641  describe('tv', function()
2642    describe('alloc', function()
2643      describe('list ret()', function()
2644        itp('works', function()
2645          local rettv = typvalt(lib.VAR_UNKNOWN)
2646          local l = lib.tv_list_alloc_ret(rettv, 0)
2647          eq(empty_list, typvalt2lua(rettv))
2648          eq(rettv.vval.v_list, l)
2649        end)
2650      end)
2651      describe('dict ret()', function()
2652        itp('works', function()
2653          local rettv = typvalt(lib.VAR_UNKNOWN)
2654          lib.tv_dict_alloc_ret(rettv)
2655          eq({}, typvalt2lua(rettv))
2656        end)
2657      end)
2658    end)
2659    local function defalloc()
2660      return {}
2661    end
2662    describe('clear()', function()
2663      itp('works', function()
2664        local function deffrees(alloc_rets)
2665          local ret = {}
2666          for i = #alloc_rets, 1, -1 do
2667            ret[#alloc_rets - i + 1] = alloc_rets:freed(i)
2668          end
2669          return ret
2670        end
2671        alloc_log:check({})
2672        lib.tv_clear(nil)
2673        alloc_log:check({})
2674        local ll = {}
2675        local ll_l = nil
2676        ll[1] = ll
2677        local dd = {}
2678        local dd_d = nil
2679        dd.dd = dd
2680        for _, v in ipairs({
2681          { nil_value },
2682          {
2683            null_string,
2684            nil,
2685            function()
2686              return { a.freed(alloc_log.null) }
2687            end,
2688          },
2689          { 0 },
2690          { int(0) },
2691          { true },
2692          { false },
2693          {
2694            'true',
2695            function(tv)
2696              return { a.str(tv.vval.v_string) }
2697            end,
2698          },
2699          {
2700            {},
2701            function(tv)
2702              return { a.dict(tv.vval.v_dict) }
2703            end,
2704          },
2705          {
2706            empty_list,
2707            function(tv)
2708              return { a.list(tv.vval.v_list) }
2709            end,
2710          },
2711          {
2712            ll,
2713            function(tv)
2714              ll_l = tv.vval.v_list
2715              return { a.list(tv.vval.v_list), a.li(tv.vval.v_list.lv_first) }
2716            end,
2717            defalloc,
2718          },
2719          {
2720            dd,
2721            function(tv)
2722              dd_d = tv.vval.v_dict
2723              return { a.dict(tv.vval.v_dict), a.di(first_di(tv.vval.v_dict)) }
2724            end,
2725            defalloc,
2726          },
2727        }) do
2728          local tv = lua2typvalt(v[1])
2729          local alloc_rets = {}
2730          alloc_log:check(get_alloc_rets((v[2] or defalloc)(tv), alloc_rets))
2731          lib.tv_clear(tv)
2732          alloc_log:check((v[3] or deffrees)(alloc_rets))
2733        end
2734        eq(1, ll_l.lv_refcount)
2735        eq(1, dd_d.dv_refcount)
2736      end)
2737    end)
2738    describe('copy()', function()
2739      itp('works', function()
2740        local function strallocs(tv)
2741          return { a.str(tv.vval.v_string) }
2742        end
2743        for _, v in ipairs({
2744          { nil_value },
2745          { null_string },
2746          { 0 },
2747          { int(0) },
2748          { true },
2749          { false },
2750          {
2751            {},
2752            function(tv)
2753              return { a.dict(tv.vval.v_dict) }
2754            end,
2755            nil,
2756            function(from, to)
2757              eq(2, to.vval.v_dict.dv_refcount)
2758              eq(to.vval.v_dict, from.vval.v_dict)
2759            end,
2760          },
2761          {
2762            empty_list,
2763            function(tv)
2764              return { a.list(tv.vval.v_list) }
2765            end,
2766            nil,
2767            function(from, to)
2768              eq(2, to.vval.v_list.lv_refcount)
2769              eq(to.vval.v_list, from.vval.v_list)
2770            end,
2771          },
2772          {
2773            'test',
2774            strallocs,
2775            strallocs,
2776            function(from, to)
2777              neq(to.vval.v_string, from.vval.v_string)
2778            end,
2779          },
2780        }) do
2781          local from = lua2typvalt(v[1])
2782          alloc_log:check((v[2] or defalloc)(from))
2783          local to = typvalt(lib.VAR_UNKNOWN)
2784          lib.tv_copy(from, to)
2785          local res = v[1]
2786          eq(res, typvalt2lua(to))
2787          alloc_log:check((v[3] or defalloc)(to))
2788          if v[4] then
2789            v[4](from, to)
2790          end
2791        end
2792      end)
2793    end)
2794    describe('item_lock()', function()
2795      itp('does not alter VAR_PARTIAL', function()
2796        local p_tv = lua2typvalt({
2797          [type_key] = func_type,
2798          value = 'tr',
2799          dict = {},
2800        })
2801        lib.tv_item_lock(p_tv, -1, true, false)
2802        eq(lib.VAR_UNLOCKED, p_tv.vval.v_partial.pt_dict.dv_lock)
2803      end)
2804      itp('does not change VAR_FIXED values', function()
2805        local d_tv = lua2typvalt({})
2806        local l_tv = lua2typvalt(empty_list)
2807        alloc_log:clear()
2808        d_tv.v_lock = lib.VAR_FIXED
2809        d_tv.vval.v_dict.dv_lock = lib.VAR_FIXED
2810        l_tv.v_lock = lib.VAR_FIXED
2811        l_tv.vval.v_list.lv_lock = lib.VAR_FIXED
2812        lib.tv_item_lock(d_tv, 1, true, false)
2813        lib.tv_item_lock(l_tv, 1, true, false)
2814        eq(lib.VAR_FIXED, d_tv.v_lock)
2815        eq(lib.VAR_FIXED, l_tv.v_lock)
2816        eq(lib.VAR_FIXED, d_tv.vval.v_dict.dv_lock)
2817        eq(lib.VAR_FIXED, l_tv.vval.v_list.lv_lock)
2818        lib.tv_item_lock(d_tv, 1, false, false)
2819        lib.tv_item_lock(l_tv, 1, false, false)
2820        eq(lib.VAR_FIXED, d_tv.v_lock)
2821        eq(lib.VAR_FIXED, l_tv.v_lock)
2822        eq(lib.VAR_FIXED, d_tv.vval.v_dict.dv_lock)
2823        eq(lib.VAR_FIXED, l_tv.vval.v_list.lv_lock)
2824        alloc_log:check({})
2825      end)
2826      itp('works with NULL values', function()
2827        local l_tv = lua2typvalt(null_list)
2828        local d_tv = lua2typvalt(null_dict)
2829        local s_tv = lua2typvalt(null_string)
2830        alloc_log:clear()
2831        lib.tv_item_lock(l_tv, 1, true, false)
2832        lib.tv_item_lock(d_tv, 1, true, false)
2833        lib.tv_item_lock(s_tv, 1, true, false)
2834        eq(null_list, typvalt2lua(l_tv))
2835        eq(null_dict, typvalt2lua(d_tv))
2836        eq(null_string, typvalt2lua(s_tv))
2837        eq(lib.VAR_LOCKED, d_tv.v_lock)
2838        eq(lib.VAR_LOCKED, l_tv.v_lock)
2839        eq(lib.VAR_LOCKED, s_tv.v_lock)
2840        alloc_log:check({})
2841      end)
2842    end)
2843    describe('islocked()', function()
2844      itp('works with NULL values', function()
2845        local l_tv = lua2typvalt(null_list)
2846        local d_tv = lua2typvalt(null_dict)
2847        eq(false, lib.tv_islocked(l_tv))
2848        eq(false, lib.tv_islocked(d_tv))
2849      end)
2850      itp('works', function()
2851        local tv = lua2typvalt()
2852        local d_tv = lua2typvalt({})
2853        local l_tv = lua2typvalt(empty_list)
2854        alloc_log:clear()
2855        eq(false, lib.tv_islocked(tv))
2856        eq(false, lib.tv_islocked(l_tv))
2857        eq(false, lib.tv_islocked(d_tv))
2858        d_tv.vval.v_dict.dv_lock = lib.VAR_LOCKED
2859        l_tv.vval.v_list.lv_lock = lib.VAR_LOCKED
2860        eq(true, lib.tv_islocked(l_tv))
2861        eq(true, lib.tv_islocked(d_tv))
2862        tv.v_lock = lib.VAR_LOCKED
2863        d_tv.v_lock = lib.VAR_LOCKED
2864        l_tv.v_lock = lib.VAR_LOCKED
2865        eq(true, lib.tv_islocked(tv))
2866        eq(true, lib.tv_islocked(l_tv))
2867        eq(true, lib.tv_islocked(d_tv))
2868        d_tv.vval.v_dict.dv_lock = lib.VAR_UNLOCKED
2869        l_tv.vval.v_list.lv_lock = lib.VAR_UNLOCKED
2870        eq(true, lib.tv_islocked(tv))
2871        eq(true, lib.tv_islocked(l_tv))
2872        eq(true, lib.tv_islocked(d_tv))
2873        tv.v_lock = lib.VAR_FIXED
2874        d_tv.v_lock = lib.VAR_FIXED
2875        l_tv.v_lock = lib.VAR_FIXED
2876        eq(false, lib.tv_islocked(tv))
2877        eq(false, lib.tv_islocked(l_tv))
2878        eq(false, lib.tv_islocked(d_tv))
2879        d_tv.vval.v_dict.dv_lock = lib.VAR_LOCKED
2880        l_tv.vval.v_list.lv_lock = lib.VAR_LOCKED
2881        eq(true, lib.tv_islocked(l_tv))
2882        eq(true, lib.tv_islocked(d_tv))
2883        d_tv.vval.v_dict.dv_lock = lib.VAR_FIXED
2884        l_tv.vval.v_list.lv_lock = lib.VAR_FIXED
2885        eq(false, lib.tv_islocked(l_tv))
2886        eq(false, lib.tv_islocked(d_tv))
2887        alloc_log:check({})
2888      end)
2889    end)
2890    describe('check_lock()', function()
2891      local function tv_check_lock(lock, name, name_len, emsg)
2892        return check_emsg(function()
2893          return lib.value_check_lock(lock, name, name_len)
2894        end, emsg)
2895      end
2896      itp('works', function()
2897        eq(false, tv_check_lock(lib.VAR_UNLOCKED, 'test', 3))
2898        eq(true, tv_check_lock(lib.VAR_LOCKED, 'test', 3, 'E741: Value is locked: tes'))
2899        eq(true, tv_check_lock(lib.VAR_FIXED, 'test', 3, 'E742: Cannot change value of tes'))
2900        eq(true, tv_check_lock(lib.VAR_LOCKED, nil, 0, 'E741: Value is locked'))
2901        eq(true, tv_check_lock(lib.VAR_FIXED, nil, 0, 'E742: Cannot change value'))
2902        eq(true, tv_check_lock(lib.VAR_LOCKED, nil, lib.kTVCstring, 'E741: Value is locked'))
2903        eq(
2904          true,
2905          tv_check_lock(lib.VAR_FIXED, 'test', lib.kTVCstring, 'E742: Cannot change value of test')
2906        )
2907      end)
2908    end)
2909    describe('equal()', function()
2910      itp('compares empty and NULL lists correctly', function()
2911        local l = lua2typvalt(empty_list)
2912        local l2 = lua2typvalt(empty_list)
2913        local nl = lua2typvalt(null_list)
2914
2915        -- NULL lists are equal to empty lists
2916        eq(true, lib.tv_equal(l, nl, true))
2917        eq(true, lib.tv_equal(nl, l, false))
2918
2919        -- NULL lists are equal themselves
2920        eq(true, lib.tv_equal(nl, nl, true))
2921        eq(true, lib.tv_equal(nl, nl, false))
2922
2923        -- As well as empty lists
2924        eq(true, lib.tv_equal(l, l, true))
2925        eq(true, lib.tv_equal(l, l2, false))
2926        eq(true, lib.tv_equal(l2, l, false))
2927        eq(true, lib.tv_equal(l2, l2, true))
2928      end)
2929      itp('compares lists correctly when case is not ignored', function()
2930        local l1 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
2931        local l2 = lua2typvalt({ 'abc', { 1, 2, 'Abc' } })
2932        local l3 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'Def' })
2933        local l4 = lua2typvalt({ 'abc', { 1, 2, 'Abc', 4 }, 'def' })
2934        local l5 = lua2typvalt({ 'Abc', { 1, 2, 'Abc' }, 'def' })
2935        local l6 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
2936        local l7 = lua2typvalt({ 'abc', { 1, 2, 'abc' }, 'def' })
2937        local l8 = lua2typvalt({ 'abc', nil, 'def' })
2938        local l9 = lua2typvalt({ 'abc', { 1, 2, nil }, 'def' })
2939
2940        eq(true, lib.tv_equal(l1, l1, false))
2941        eq(false, lib.tv_equal(l1, l2, false))
2942        eq(false, lib.tv_equal(l1, l3, false))
2943        eq(false, lib.tv_equal(l1, l4, false))
2944        eq(false, lib.tv_equal(l1, l5, false))
2945        eq(true, lib.tv_equal(l1, l6, false))
2946        eq(false, lib.tv_equal(l1, l7, false))
2947        eq(false, lib.tv_equal(l1, l8, false))
2948        eq(false, lib.tv_equal(l1, l9, false))
2949      end)
2950      itp('compares lists correctly when case is ignored', function()
2951        local l1 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
2952        local l2 = lua2typvalt({ 'abc', { 1, 2, 'Abc' } })
2953        local l3 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'Def' })
2954        local l4 = lua2typvalt({ 'abc', { 1, 2, 'Abc', 4 }, 'def' })
2955        local l5 = lua2typvalt({ 'Abc', { 1, 2, 'Abc' }, 'def' })
2956        local l6 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
2957        local l7 = lua2typvalt({ 'abc', { 1, 2, 'abc' }, 'def' })
2958        local l8 = lua2typvalt({ 'abc', nil, 'def' })
2959        local l9 = lua2typvalt({ 'abc', { 1, 2, nil }, 'def' })
2960
2961        eq(true, lib.tv_equal(l1, l1, true))
2962        eq(false, lib.tv_equal(l1, l2, true))
2963        eq(true, lib.tv_equal(l1, l3, true))
2964        eq(false, lib.tv_equal(l1, l4, true))
2965        eq(true, lib.tv_equal(l1, l5, true))
2966        eq(true, lib.tv_equal(l1, l6, true))
2967        eq(true, lib.tv_equal(l1, l7, true))
2968        eq(false, lib.tv_equal(l1, l8, true))
2969        eq(false, lib.tv_equal(l1, l9, true))
2970      end)
2971      local function tv_equal(d1, d2, ic)
2972        return lib.tv_equal(d1, d2, ic or false)
2973      end
2974      itp('works with dictionaries', function()
2975        local nd = lua2typvalt(null_dict)
2976        eq(true, tv_equal(nd, nd))
2977        alloc_log:check({})
2978        local d1 = lua2typvalt({})
2979        alloc_log:check({ a.dict(d1.vval.v_dict) })
2980        eq(1, d1.vval.v_dict.dv_refcount)
2981        eq(true, tv_equal(nd, d1))
2982        eq(true, tv_equal(d1, nd))
2983        eq(true, tv_equal(d1, d1))
2984        eq(1, d1.vval.v_dict.dv_refcount)
2985        alloc_log:check({})
2986        local d_upper = lua2typvalt({ a = 'TEST' })
2987        local dis_upper = dict_items(d_upper.vval.v_dict)
2988        local d_lower = lua2typvalt({ a = 'test' })
2989        local dis_lower = dict_items(d_lower.vval.v_dict)
2990        local d_kupper_upper = lua2typvalt({ A = 'TEST' })
2991        local dis_kupper_upper = dict_items(d_kupper_upper.vval.v_dict)
2992        local d_kupper_lower = lua2typvalt({ A = 'test' })
2993        local dis_kupper_lower = dict_items(d_kupper_lower.vval.v_dict)
2994        alloc_log:clear_tmp_allocs()
2995        alloc_log:check({
2996          a.dict(d_upper.vval.v_dict),
2997          a.di(dis_upper.a),
2998          a.str(dis_upper.a.di_tv.vval.v_string),
2999
3000          a.dict(d_lower.vval.v_dict),
3001          a.di(dis_lower.a),
3002          a.str(dis_lower.a.di_tv.vval.v_string),
3003
3004          a.dict(d_kupper_upper.vval.v_dict),
3005          a.di(dis_kupper_upper.A),
3006          a.str(dis_kupper_upper.A.di_tv.vval.v_string),
3007
3008          a.dict(d_kupper_lower.vval.v_dict),
3009          a.di(dis_kupper_lower.A),
3010          a.str(dis_kupper_lower.A.di_tv.vval.v_string),
3011        })
3012        eq(true, tv_equal(d_upper, d_upper))
3013        eq(true, tv_equal(d_upper, d_upper, true))
3014        eq(false, tv_equal(d_upper, d_lower, false))
3015        eq(true, tv_equal(d_upper, d_lower, true))
3016        eq(true, tv_equal(d_kupper_upper, d_kupper_lower, true))
3017        eq(false, tv_equal(d_kupper_upper, d_lower, true))
3018        eq(false, tv_equal(d_kupper_upper, d_upper, true))
3019        alloc_log:check({})
3020      end)
3021    end)
3022    describe('check', function()
3023      describe('str_or_nr()', function()
3024        itp('works', function()
3025          local tv = typvalt()
3026          local mem = lib.xmalloc(1)
3027          tv.vval.v_list = mem -- Should crash when actually accessed
3028          alloc_log:clear()
3029          for _, v in ipairs({
3030            { lib.VAR_NUMBER, nil },
3031            { lib.VAR_FLOAT, 'E805: Expected a Number or a String, Float found' },
3032            { lib.VAR_PARTIAL, 'E703: Expected a Number or a String, Funcref found' },
3033            { lib.VAR_FUNC, 'E703: Expected a Number or a String, Funcref found' },
3034            { lib.VAR_LIST, 'E745: Expected a Number or a String, List found' },
3035            { lib.VAR_DICT, 'E728: Expected a Number or a String, Dictionary found' },
3036            { lib.VAR_SPECIAL, 'E5300: Expected a Number or a String' },
3037            { lib.VAR_UNKNOWN, 'E685: Internal error: tv_check_str_or_nr(UNKNOWN)' },
3038          }) do
3039            local typ = v[1]
3040            local emsg = v[2]
3041            local ret = true
3042            if emsg then
3043              ret = false
3044            end
3045            tv.v_type = typ
3046            eq(
3047              ret,
3048              check_emsg(function()
3049                return lib.tv_check_str_or_nr(tv)
3050              end, emsg)
3051            )
3052            if emsg then
3053              alloc_log:clear()
3054            else
3055              alloc_log:check({})
3056            end
3057          end
3058        end)
3059      end)
3060      describe('num()', function()
3061        itp('works', function()
3062          local tv = typvalt()
3063          local mem = lib.xmalloc(1)
3064          tv.vval.v_list = mem -- Should crash when actually accessed
3065          alloc_log:clear()
3066          for _, v in ipairs({
3067            { lib.VAR_NUMBER, nil },
3068            { lib.VAR_FLOAT, 'E805: Using a Float as a Number' },
3069            { lib.VAR_PARTIAL, 'E703: Using a Funcref as a Number' },
3070            { lib.VAR_FUNC, 'E703: Using a Funcref as a Number' },
3071            { lib.VAR_LIST, 'E745: Using a List as a Number' },
3072            { lib.VAR_DICT, 'E728: Using a Dictionary as a Number' },
3073            { lib.VAR_SPECIAL, nil },
3074            { lib.VAR_UNKNOWN, 'E685: using an invalid value as a Number' },
3075          }) do
3076            local typ = v[1]
3077            local emsg = v[2]
3078            local ret = true
3079            if emsg then
3080              ret = false
3081            end
3082            tv.v_type = typ
3083            eq(
3084              ret,
3085              check_emsg(function()
3086                return lib.tv_check_num(tv)
3087              end, emsg)
3088            )
3089            if emsg then
3090              alloc_log:clear()
3091            else
3092              alloc_log:check({})
3093            end
3094          end
3095        end)
3096      end)
3097      describe('str()', function()
3098        itp('works', function()
3099          local tv = typvalt()
3100          local mem = lib.xmalloc(1)
3101          tv.vval.v_list = mem -- Should crash when actually accessed
3102          alloc_log:clear()
3103          for _, v in ipairs({
3104            { lib.VAR_NUMBER, nil },
3105            { lib.VAR_FLOAT, nil },
3106            { lib.VAR_PARTIAL, 'E729: Using a Funcref as a String' },
3107            { lib.VAR_FUNC, 'E729: Using a Funcref as a String' },
3108            { lib.VAR_LIST, 'E730: Using a List as a String' },
3109            { lib.VAR_DICT, 'E731: Using a Dictionary as a String' },
3110            { lib.VAR_BOOL, nil },
3111            { lib.VAR_SPECIAL, nil },
3112            { lib.VAR_UNKNOWN, 'E908: Using an invalid value as a String' },
3113          }) do
3114            local typ = v[1]
3115            local emsg = v[2]
3116            local ret = true
3117            if emsg then
3118              ret = false
3119            end
3120            tv.v_type = typ
3121            eq(
3122              ret,
3123              check_emsg(function()
3124                return lib.tv_check_str(tv)
3125              end, emsg)
3126            )
3127            if emsg then
3128              alloc_log:clear()
3129            else
3130              alloc_log:check({})
3131            end
3132          end
3133        end)
3134      end)
3135    end)
3136    describe('get', function()
3137      describe('number()', function()
3138        itp('works', function()
3139          for _, v in ipairs({
3140            { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
3141            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
3142            { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', 0 },
3143            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', 0 },
3144            { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', 0 },
3145            { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', 0 },
3146            { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', 0 },
3147            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
3148            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
3149            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
3150            { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', 0 },
3151          }) do
3152            -- Using to_cstr, cannot free with tv_clear
3153            local tv = ffi.gc(typvalt(v[1], v[2]), nil)
3154            alloc_log:check({})
3155            local emsg = v[3]
3156            local ret = v[4]
3157            eq(
3158              ret,
3159              check_emsg(function()
3160                return lib.tv_get_number(tv)
3161              end, emsg)
3162            )
3163            if emsg then
3164              alloc_log:clear()
3165            else
3166              alloc_log:check({})
3167            end
3168          end
3169        end)
3170      end)
3171      describe('number_chk()', function()
3172        itp('works', function()
3173          for _, v in ipairs({
3174            { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
3175            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
3176            { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', 0 },
3177            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', 0 },
3178            { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', 0 },
3179            { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', 0 },
3180            { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', 0 },
3181            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
3182            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
3183            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
3184            { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', 0 },
3185          }) do
3186            -- Using to_cstr, cannot free with tv_clear
3187            local tv = ffi.gc(typvalt(v[1], v[2]), nil)
3188            alloc_log:check({})
3189            local emsg = v[3]
3190            local ret = { v[4], not not emsg }
3191            eq(
3192              ret,
3193              check_emsg(function()
3194                local err = ffi.new('bool[1]', { false })
3195                local res = lib.tv_get_number_chk(tv, err)
3196                return { res, err[0] }
3197              end, emsg)
3198            )
3199            if emsg then
3200              alloc_log:clear()
3201            else
3202              alloc_log:check({})
3203            end
3204          end
3205        end)
3206      end)
3207      describe('lnum()', function()
3208        itp('works', function()
3209          for _, v in ipairs({
3210            { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
3211            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
3212            { lib.VAR_STRING, { v_string = to_cstr('.') }, nil, 46 },
3213            { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', -1 },
3214            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', -1 },
3215            { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', -1 },
3216            { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', -1 },
3217            { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', -1 },
3218            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
3219            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
3220            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
3221            { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', -1 },
3222          }) do
3223            lib.curwin.w_cursor.lnum = 46
3224            -- Using to_cstr, cannot free with tv_clear
3225            local tv = ffi.gc(typvalt(v[1], v[2]), nil)
3226            alloc_log:check({})
3227            local emsg = v[3]
3228            local ret = v[4]
3229            eq(
3230              ret,
3231              check_emsg(function()
3232                return lib.tv_get_lnum(tv)
3233              end, emsg)
3234            )
3235            if emsg then
3236              alloc_log:clear()
3237            else
3238              alloc_log:check({})
3239            end
3240          end
3241        end)
3242      end)
3243      describe('float()', function()
3244        itp('works', function()
3245          for _, v in ipairs({
3246            { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
3247            {
3248              lib.VAR_STRING,
3249              { v_string = to_cstr('100500') },
3250              'E892: Using a String as a Float',
3251              0,
3252            },
3253            { lib.VAR_FLOAT, { v_float = 42.53 }, nil, 42.53 },
3254            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E891: Using a Funcref as a Float', 0 },
3255            { lib.VAR_FUNC, { v_string = NULL }, 'E891: Using a Funcref as a Float', 0 },
3256            { lib.VAR_LIST, { v_list = NULL }, 'E893: Using a List as a Float', 0 },
3257            { lib.VAR_DICT, { v_dict = NULL }, 'E894: Using a Dictionary as a Float', 0 },
3258            {
3259              lib.VAR_SPECIAL,
3260              { v_special = lib.kSpecialVarNull },
3261              'E907: Using a special value as a Float',
3262              0,
3263            },
3264            {
3265              lib.VAR_BOOL,
3266              { v_bool = lib.kBoolVarTrue },
3267              'E362: Using a boolean value as a Float',
3268              0,
3269            },
3270            {
3271              lib.VAR_BOOL,
3272              { v_bool = lib.kBoolVarFalse },
3273              'E362: Using a boolean value as a Float',
3274              0,
3275            },
3276            { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_float(UNKNOWN)', 0 },
3277          }) do
3278            -- Using to_cstr, cannot free with tv_clear
3279            local tv = ffi.gc(typvalt(v[1], v[2]), nil)
3280            alloc_log:check({})
3281            local emsg = v[3]
3282            local ret = v[4]
3283            eq(
3284              ret,
3285              check_emsg(function()
3286                return lib.tv_get_float(tv)
3287              end, emsg)
3288            )
3289            if emsg then
3290              alloc_log:clear()
3291            else
3292              alloc_log:check({})
3293            end
3294          end
3295        end)
3296      end)
3297
3298      local function test_string_fn(input, fn)
3299        for _, v in ipairs(input) do
3300          -- Using to_cstr in place of Neovim allocated string, cannot
3301          -- tv_clear() that.
3302          local tv = ffi.gc(typvalt(v[1], v[2]), nil)
3303          alloc_log:check({})
3304          local emsg = v[3]
3305          local ret = v[4]
3306          eq(
3307            ret,
3308            check_emsg(function()
3309              local res, buf = fn(tv)
3310              if
3311                tv.v_type == lib.VAR_NUMBER
3312                or tv.v_type == lib.VAR_FLOAT
3313                or tv.v_type == lib.VAR_SPECIAL
3314                or tv.v_type == lib.VAR_BOOL
3315              then
3316                eq(buf, res)
3317              else
3318                neq(buf, res)
3319              end
3320              if res ~= nil then
3321                return ffi.string(res)
3322              else
3323                return nil
3324              end
3325            end, emsg)
3326          )
3327          if emsg then
3328            alloc_log:clear()
3329          elseif tv.v_type == lib.VAR_FLOAT then
3330            alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
3331          else
3332            alloc_log:check({})
3333          end
3334        end
3335      end
3336      describe('string()', function()
3337        itp('works', function()
3338          local buf = lib.tv_get_string(lua2typvalt(int(1)))
3339          local buf_chk = lib.tv_get_string_chk(lua2typvalt(int(1)))
3340          neq(buf, buf_chk)
3341          test_string_fn({
3342            { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
3343            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
3344            { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
3345            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', '' },
3346            { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', '' },
3347            { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', '' },
3348            { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', '' },
3349            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
3350            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
3351            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
3352            { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', '' },
3353          }, function(tv)
3354            return lib.tv_get_string(tv), buf
3355          end)
3356        end)
3357      end)
3358      describe('string_chk()', function()
3359        itp('works', function()
3360          local buf = lib.tv_get_string_chk(lua2typvalt(int(1)))
3361          test_string_fn({
3362            { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
3363            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
3364            { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
3365            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', nil },
3366            { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', nil },
3367            { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', nil },
3368            { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', nil },
3369            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
3370            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
3371            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
3372            { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil },
3373          }, function(tv)
3374            return lib.tv_get_string_chk(tv), buf
3375          end)
3376        end)
3377      end)
3378      describe('string_buf()', function()
3379        itp('works', function()
3380          test_string_fn({
3381            { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
3382            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
3383            { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
3384            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', '' },
3385            { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', '' },
3386            { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', '' },
3387            { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', '' },
3388            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
3389            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
3390            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
3391            { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', '' },
3392          }, function(tv)
3393            local buf = ffi.new('char[?]', lib.NUMBUFLEN, { 0 })
3394            return lib.tv_get_string_buf(tv, buf), buf
3395          end)
3396        end)
3397      end)
3398      describe('string_buf_chk()', function()
3399        itp('works', function()
3400          test_string_fn({
3401            { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
3402            { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
3403            { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
3404            { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', nil },
3405            { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', nil },
3406            { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', nil },
3407            { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', nil },
3408            { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
3409            { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
3410            { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
3411            { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil },
3412          }, function(tv)
3413            local buf = ffi.new('char[?]', lib.NUMBUFLEN, { 0 })
3414            return lib.tv_get_string_buf_chk(tv, buf), buf
3415          end)
3416        end)
3417      end)
3418    end)
3419  end)
3420end)