typval_spec.lua 118 KB

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