typval_spec.lua 117 KB

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