typval_spec.lua 116 KB

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