123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423 |
- local bit = require('bit')
- local t = require('test.unit.testutil')
- local t_eval = require('test.unit.eval.testutil')
- local itp = t.gen_itp(it)
- local OK = t.OK
- local eq = t.eq
- local neq = t.neq
- local ffi = t.ffi
- local FAIL = t.FAIL
- local NULL = t.NULL
- local cimport = t.cimport
- local to_cstr = t.to_cstr
- local alloc_log_new = t.alloc_log_new
- local concat_tables = t.concat_tables
- local map = vim.tbl_map
- local a = t_eval.alloc_logging_t
- local int = t_eval.int
- local list = t_eval.list
- local dict = t_eval.dict
- local eval0 = t_eval.eval0
- local lst2tbl = t_eval.lst2tbl
- local dct2tbl = t_eval.dct2tbl
- local typvalt = t_eval.typvalt
- local type_key = t_eval.type_key
- local li_alloc = t_eval.li_alloc
- local first_di = t_eval.first_di
- local nil_value = t_eval.nil_value
- local func_type = t_eval.func_type
- local null_list = t_eval.null_list
- local null_dict = t_eval.null_dict
- local dict_items = t_eval.dict_items
- local list_items = t_eval.list_items
- local empty_list = t_eval.empty_list
- local lua2typvalt = t_eval.lua2typvalt
- local typvalt2lua = t_eval.typvalt2lua
- local null_string = t_eval.null_string
- local callback2tbl = t_eval.callback2tbl
- local tbl2callback = t_eval.tbl2callback
- local dict_watchers = t_eval.dict_watchers
- local lib = cimport(
- './src/nvim/eval/typval.h',
- './src/nvim/memory.h',
- './src/nvim/mbyte.h',
- './src/nvim/garray.h',
- './src/nvim/eval.h',
- './src/nvim/vim_defs.h',
- './src/nvim/globals.h'
- )
- local function vimconv_alloc()
- return ffi.gc(ffi.cast('vimconv_T*', lib.xcalloc(1, ffi.sizeof('vimconv_T'))), function(vc)
- lib.convert_setup(vc, nil, nil)
- lib.xfree(vc)
- end)
- end
- local function list_watch_alloc(li)
- return ffi.cast('listwatch_T*', ffi.new('listwatch_T[1]', { { lw_item = li } }))
- end
- local function list_watch(l, li)
- local lw = list_watch_alloc(li or l.lv_first)
- lib.tv_list_watch_add(l, lw)
- return lw
- end
- local function get_alloc_rets(exp_log, res)
- setmetatable(res, {
- __index = {
- freed = function(r, n)
- return { func = 'free', args = { r[n] } }
- end,
- },
- })
- for i = 1, #exp_log do
- if ({ malloc = true, calloc = true })[exp_log[i].func] then
- res[#res + 1] = exp_log[i].ret
- end
- end
- return exp_log
- end
- local alloc_log = alloc_log_new()
- before_each(function()
- alloc_log:before_each()
- end)
- after_each(function()
- alloc_log:after_each()
- end)
- local function ga_alloc(itemsize, growsize)
- local ga = ffi.gc(ffi.cast('garray_T*', ffi.new('garray_T[1]', {})), lib.ga_clear)
- lib.ga_init(ga, itemsize or 1, growsize or 80)
- return ga
- end
- local function check_emsg(f, msg)
- local saved_last_msg_hist = lib.last_msg_hist
- if saved_last_msg_hist == nil then
- saved_last_msg_hist = nil
- end
- local ret = { f() }
- local last_msg = lib.last_msg_hist ~= nil and ffi.string(lib.last_msg_hist.msg) or nil
- if msg ~= nil then
- eq(msg, last_msg)
- neq(saved_last_msg_hist, lib.last_msg_hist)
- else
- if saved_last_msg_hist ~= lib.last_msg_hist then
- eq(nil, last_msg)
- else
- eq(saved_last_msg_hist, lib.last_msg_hist)
- end
- end
- return unpack(ret)
- end
- describe('typval.c', function()
- describe('list', function()
- describe('item', function()
- describe('remove()', function()
- itp('works', function()
- local l = list(1, 2, 3, 4, 5, 6, 7)
- neq(nil, l)
- local lis = list_items(l)
- alloc_log:check({
- a.list(l),
- a.li(lis[1]),
- a.li(lis[2]),
- a.li(lis[3]),
- a.li(lis[4]),
- a.li(lis[5]),
- a.li(lis[6]),
- a.li(lis[7]),
- })
- eq(lis[2], lib.tv_list_item_remove(l, lis[1]))
- alloc_log:check({
- a.freed(table.remove(lis, 1)),
- })
- eq(lis, list_items(l))
- eq(lis[7], lib.tv_list_item_remove(l, lis[6]))
- alloc_log:check({
- a.freed(table.remove(lis)),
- })
- eq(lis, list_items(l))
- eq(lis[4], lib.tv_list_item_remove(l, lis[3]))
- alloc_log:check({
- a.freed(table.remove(lis, 3)),
- })
- eq(lis, list_items(l))
- end)
- itp('also frees the value', function()
- local l = list('a', 'b', 'c', 'd')
- neq(nil, l)
- local lis = list_items(l)
- alloc_log:check({
- a.list(l),
- a.str(lis[1].li_tv.vval.v_string, 1),
- a.li(lis[1]),
- a.str(lis[2].li_tv.vval.v_string, 1),
- a.li(lis[2]),
- a.str(lis[3].li_tv.vval.v_string, 1),
- a.li(lis[3]),
- a.str(lis[4].li_tv.vval.v_string, 1),
- a.li(lis[4]),
- })
- local strings = map(function(li)
- return li.li_tv.vval.v_string
- end, lis)
- eq(lis[2], lib.tv_list_item_remove(l, lis[1]))
- alloc_log:check({
- a.freed(table.remove(strings, 1)),
- a.freed(table.remove(lis, 1)),
- })
- eq(lis, list_items(l))
- eq(lis[3], lib.tv_list_item_remove(l, lis[2]))
- alloc_log:check({
- a.freed(table.remove(strings, 2)),
- a.freed(table.remove(lis, 2)),
- })
- eq(lis, list_items(l))
- eq(nil, lib.tv_list_item_remove(l, lis[2]))
- alloc_log:check({
- a.freed(table.remove(strings, 2)),
- a.freed(table.remove(lis, 2)),
- })
- eq(lis, list_items(l))
- end)
- itp('works and adjusts watchers correctly', function()
- local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
- neq(nil, l)
- local lis = list_items(l)
- -- Three watchers: pointing to first, middle and last elements.
- local lws = {
- list_watch(l, lis[1]),
- list_watch(l, lis[4]),
- list_watch(l, lis[7]),
- }
- alloc_log:check({
- a.list(l),
- a.li(lis[1]),
- a.li(lis[2]),
- a.li(lis[3]),
- a.li(lis[4]),
- a.li(lis[5]),
- a.li(lis[6]),
- a.li(lis[7]),
- })
- eq(lis[5], lib.tv_list_item_remove(l, lis[4]))
- alloc_log:check({ a.freed(lis[4]) })
- eq({ lis[1], lis[5], lis[7] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
- eq(lis[3], lib.tv_list_item_remove(l, lis[2]))
- alloc_log:check({ a.freed(lis[2]) })
- eq({ lis[1], lis[5], lis[7] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
- eq(nil, lib.tv_list_item_remove(l, lis[7]))
- alloc_log:check({ a.freed(lis[7]) })
- eq(
- { lis[1], lis[5], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- eq(lis[3], lib.tv_list_item_remove(l, lis[1]))
- alloc_log:check({ a.freed(lis[1]) })
- eq(
- { lis[3], lis[5], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- lib.tv_list_watch_remove(l, lws[2])
- lib.tv_list_watch_remove(l, lws[3])
- lib.tv_list_watch_remove(l, lws[1])
- lib.tv_list_free(l)
- alloc_log:check({
- a.freed(lis[3]),
- a.freed(lis[5]),
- a.freed(lis[6]),
- a.freed(l),
- })
- end)
- end)
- end)
- describe('watch', function()
- describe('remove()', function()
- itp('works', function()
- local l = ffi.gc(list(1, 2, 3, 4, 5, 6, 7), nil)
- eq(nil, l.lv_watch)
- local lw = list_watch(l)
- neq(nil, l.lv_watch)
- alloc_log:clear()
- lib.tv_list_watch_remove(l, lw)
- eq(nil, l.lv_watch)
- alloc_log:check({
- -- Does not free anything.
- })
- local lws = { list_watch(l), list_watch(l), list_watch(l) }
- alloc_log:clear()
- lib.tv_list_watch_remove(l, lws[2])
- eq(lws[3], l.lv_watch)
- eq(lws[1], l.lv_watch.lw_next)
- lib.tv_list_watch_remove(l, lws[1])
- eq(lws[3], l.lv_watch)
- eq(nil, l.lv_watch.lw_next)
- lib.tv_list_watch_remove(l, lws[3])
- eq(nil, l.lv_watch)
- alloc_log:check({
- -- Does not free anything.
- })
- end)
- itp('ignores not found watchers', function()
- local l = list(1, 2, 3, 4, 5, 6, 7)
- local lw = list_watch_alloc()
- lib.tv_list_watch_remove(l, lw)
- end)
- end)
- end)
- -- add() and fix() were tested when testing tv_list_item_remove()
- describe('free()', function()
- itp('recursively frees list', function()
- local l1 = ffi.gc(list(1, 'abc'), nil)
- local l2 = ffi.gc(list({}), nil)
- local l3 = ffi.gc(list(empty_list), nil)
- local alloc_rets = {}
- alloc_log:check(get_alloc_rets({
- a.list(l1),
- a.li(l1.lv_first),
- a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
- a.li(l1.lv_last),
- a.list(l2),
- a.dict(l2.lv_first.li_tv.vval.v_dict),
- a.li(l2.lv_first),
- a.list(l3),
- a.list(l3.lv_first.li_tv.vval.v_list),
- a.li(l3.lv_first),
- }, alloc_rets))
- lib.tv_list_free(l1)
- alloc_log:check({
- alloc_rets:freed(2),
- alloc_rets:freed(3),
- alloc_rets:freed(4),
- alloc_rets:freed(1),
- })
- lib.tv_list_free(l2)
- alloc_log:check({
- alloc_rets:freed(6),
- alloc_rets:freed(7),
- alloc_rets:freed(5),
- })
- lib.tv_list_free(l3)
- alloc_log:check({
- alloc_rets:freed(9),
- alloc_rets:freed(10),
- alloc_rets:freed(8),
- })
- end)
- end)
- describe('free_list()', function()
- itp('does not free list contents', function()
- local l1 = ffi.gc(list(1, 'abc'), nil)
- local l2 = ffi.gc(list({}), nil)
- local l3 = ffi.gc(list(empty_list), nil)
- local alloc_rets = {}
- alloc_log:check(get_alloc_rets({
- a.list(l1),
- a.li(l1.lv_first),
- a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
- a.li(l1.lv_last),
- a.list(l2),
- a.dict(l2.lv_first.li_tv.vval.v_dict),
- a.li(l2.lv_first),
- a.list(l3),
- a.list(l3.lv_first.li_tv.vval.v_list),
- a.li(l3.lv_first),
- }, alloc_rets))
- lib.tv_list_free_list(l1)
- alloc_log:check({
- alloc_rets:freed(1),
- })
- lib.tv_list_free_list(l2)
- alloc_log:check({
- alloc_rets:freed(5),
- })
- lib.tv_list_free_list(l3)
- alloc_log:check({
- alloc_rets:freed(8),
- })
- end)
- end)
- describe('free_contents()', function()
- itp('recursively frees list, except for the list structure itself', function()
- local l1 = ffi.gc(list(1, 'abc'), nil)
- local l2 = ffi.gc(list({}), nil)
- local l3 = ffi.gc(list(empty_list), nil)
- local alloc_rets = {}
- alloc_log:check(get_alloc_rets({
- a.list(l1),
- a.li(l1.lv_first),
- a.str(l1.lv_last.li_tv.vval.v_string, #'abc'),
- a.li(l1.lv_last),
- a.list(l2),
- a.dict(l2.lv_first.li_tv.vval.v_dict),
- a.li(l2.lv_first),
- a.list(l3),
- a.list(l3.lv_first.li_tv.vval.v_list),
- a.li(l3.lv_first),
- }, alloc_rets))
- lib.tv_list_free_contents(l1)
- alloc_log:check({
- alloc_rets:freed(2),
- alloc_rets:freed(3),
- alloc_rets:freed(4),
- })
- lib.tv_list_free_contents(l2)
- alloc_log:check({
- alloc_rets:freed(6),
- alloc_rets:freed(7),
- })
- lib.tv_list_free_contents(l3)
- alloc_log:check({
- alloc_rets:freed(9),
- alloc_rets:freed(10),
- })
- end)
- end)
- describe('unref()', function()
- itp('recursively frees list when reference count goes to 0', function()
- local l = ffi.gc(list(empty_list), nil)
- local alloc_rets = {}
- alloc_log:check(get_alloc_rets({
- a.list(l),
- a.list(l.lv_first.li_tv.vval.v_list),
- a.li(l.lv_first),
- }, alloc_rets))
- l.lv_refcount = 2
- lib.tv_list_unref(l)
- alloc_log:check({})
- lib.tv_list_unref(l)
- alloc_log:check({
- alloc_rets:freed(2),
- alloc_rets:freed(3),
- alloc_rets:freed(1),
- })
- end)
- end)
- describe('drop_items()', function()
- itp('works', function()
- local l_tv = lua2typvalt({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 })
- local l = l_tv.vval.v_list
- local lis = list_items(l)
- -- Three watchers: pointing to first, middle and last elements.
- local lws = {
- list_watch(l, lis[1]),
- list_watch(l, lis[7]),
- list_watch(l, lis[13]),
- }
- alloc_log:clear()
- lib.tv_list_drop_items(l, lis[1], lis[3])
- eq({ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, typvalt2lua(l_tv))
- eq({ lis[4], lis[7], lis[13] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
- lib.tv_list_drop_items(l, lis[11], lis[13])
- eq({ 4, 5, 6, 7, 8, 9, 10 }, typvalt2lua(l_tv))
- eq(
- { lis[4], lis[7], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- lib.tv_list_drop_items(l, lis[6], lis[8])
- eq({ 4, 5, 9, 10 }, typvalt2lua(l_tv))
- eq(
- { lis[4], lis[9], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- lib.tv_list_drop_items(l, lis[4], lis[10])
- eq(empty_list, typvalt2lua(l_tv))
- eq(
- { true, true, true },
- { lws[1].lw_item == nil, lws[2].lw_item == nil, lws[3].lw_item == nil }
- )
- lib.tv_list_watch_remove(l, lws[1])
- lib.tv_list_watch_remove(l, lws[2])
- lib.tv_list_watch_remove(l, lws[3])
- alloc_log:check({})
- end)
- end)
- describe('remove_items()', function()
- itp('works', function()
- local l_tv =
- lua2typvalt({ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' })
- local l = l_tv.vval.v_list
- local lis = list_items(l)
- local strings = map(function(li)
- return li.li_tv.vval.v_string
- end, lis)
- -- Three watchers: pointing to first, middle and last elements.
- local lws = {
- list_watch(l, lis[1]),
- list_watch(l, lis[7]),
- list_watch(l, lis[13]),
- }
- alloc_log:clear()
- lib.tv_list_remove_items(l, lis[1], lis[3])
- eq({ '4', '5', '6', '7', '8', '9', '10', '11', '12', '13' }, typvalt2lua(l_tv))
- eq({ lis[4], lis[7], lis[13] }, { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item })
- alloc_log:check({
- a.freed(strings[1]),
- a.freed(lis[1]),
- a.freed(strings[2]),
- a.freed(lis[2]),
- a.freed(strings[3]),
- a.freed(lis[3]),
- })
- lib.tv_list_remove_items(l, lis[11], lis[13])
- eq({ '4', '5', '6', '7', '8', '9', '10' }, typvalt2lua(l_tv))
- eq(
- { lis[4], lis[7], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- alloc_log:check({
- a.freed(strings[11]),
- a.freed(lis[11]),
- a.freed(strings[12]),
- a.freed(lis[12]),
- a.freed(strings[13]),
- a.freed(lis[13]),
- })
- lib.tv_list_remove_items(l, lis[6], lis[8])
- eq({ '4', '5', '9', '10' }, typvalt2lua(l_tv))
- eq(
- { lis[4], lis[9], nil },
- { lws[1].lw_item, lws[2].lw_item, lws[3].lw_item == nil and nil }
- )
- alloc_log:check({
- a.freed(strings[6]),
- a.freed(lis[6]),
- a.freed(strings[7]),
- a.freed(lis[7]),
- a.freed(strings[8]),
- a.freed(lis[8]),
- })
- lib.tv_list_remove_items(l, lis[4], lis[10])
- eq(empty_list, typvalt2lua(l_tv))
- eq(
- { true, true, true },
- { lws[1].lw_item == nil, lws[2].lw_item == nil, lws[3].lw_item == nil }
- )
- alloc_log:check({
- a.freed(strings[4]),
- a.freed(lis[4]),
- a.freed(strings[5]),
- a.freed(lis[5]),
- a.freed(strings[9]),
- a.freed(lis[9]),
- a.freed(strings[10]),
- a.freed(lis[10]),
- })
- lib.tv_list_watch_remove(l, lws[1])
- lib.tv_list_watch_remove(l, lws[2])
- lib.tv_list_watch_remove(l, lws[3])
- alloc_log:check({})
- end)
- end)
- describe('insert', function()
- describe('()', function()
- itp('works', function()
- local l_tv = lua2typvalt({ 1, 2, 3, 4, 5, 6, 7 })
- local l = l_tv.vval.v_list
- local lis = list_items(l)
- local li
- li = li_alloc(true)
- li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 100500 } }
- lib.tv_list_insert(l, li, nil)
- eq(l.lv_last, li)
- eq({ 1, 2, 3, 4, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
- li = li_alloc(true)
- li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 0 } }
- lib.tv_list_insert(l, li, lis[1])
- eq(l.lv_first, li)
- eq({ 0, 1, 2, 3, 4, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
- li = li_alloc(true)
- li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 4.5 } }
- lib.tv_list_insert(l, li, lis[5])
- eq(list_items(l)[6], li)
- eq({ 0, 1, 2, 3, 4, 4.5, 5, 6, 7, 100500 }, typvalt2lua(l_tv))
- end)
- itp('works with an empty list', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- eq(nil, l.lv_first)
- eq(nil, l.lv_last)
- local li = li_alloc(true)
- li.li_tv = { v_type = lib.VAR_FLOAT, vval = { v_float = 100500 } }
- lib.tv_list_insert(l, li, nil)
- eq(l.lv_last, li)
- eq({ 100500 }, typvalt2lua(l_tv))
- end)
- end)
- describe('tv()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local l_l_tv = lua2typvalt(empty_list)
- alloc_log:clear()
- local l_l = l_l_tv.vval.v_list
- eq(1, l_l.lv_refcount)
- lib.tv_list_insert_tv(l, l_l_tv, nil)
- eq(2, l_l.lv_refcount)
- eq(l_l, l.lv_first.li_tv.vval.v_list)
- alloc_log:check({
- a.li(l.lv_first),
- })
- local l_s_tv = lua2typvalt('test')
- alloc_log:check({
- a.str(l_s_tv.vval.v_string, 'test'),
- })
- lib.tv_list_insert_tv(l, l_s_tv, l.lv_first)
- alloc_log:check({
- a.li(l.lv_first),
- a.str(l.lv_first.li_tv.vval.v_string, 'test'),
- })
- eq({ 'test', empty_list }, typvalt2lua(l_tv))
- end)
- end)
- end)
- describe('append', function()
- describe('list()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local l_l = list(1)
- alloc_log:clear()
- eq(1, l_l.lv_refcount)
- lib.tv_list_append_list(l, l_l)
- eq(2, l_l.lv_refcount)
- eq(l_l, l.lv_first.li_tv.vval.v_list)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_list(l, nil)
- alloc_log:check({
- a.li(l.lv_last),
- })
- eq({ { 1 }, null_list }, typvalt2lua(l_tv))
- end)
- end)
- describe('dict()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local l_d_tv = lua2typvalt({ test = 1 })
- local l_d = l_d_tv.vval.v_dict
- alloc_log:clear()
- eq(1, l_d.dv_refcount)
- lib.tv_list_append_dict(l, l_d)
- eq(2, l_d.dv_refcount)
- eq(l_d, l.lv_first.li_tv.vval.v_list)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_dict(l, nil)
- alloc_log:check({
- a.li(l.lv_last),
- })
- eq({ { test = 1 }, null_dict }, typvalt2lua(l_tv))
- end)
- end)
- describe('string()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- alloc_log:clear()
- lib.tv_list_append_string(l, 'test', 3)
- alloc_log:check({
- a.str(l.lv_last.li_tv.vval.v_string, 'tes'),
- a.li(l.lv_last),
- })
- lib.tv_list_append_string(l, nil, 0)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_string(l, nil, -1)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_string(l, 'test', -1)
- alloc_log:check({
- a.str(l.lv_last.li_tv.vval.v_string, 'test'),
- a.li(l.lv_last),
- })
- eq({ 'tes', null_string, null_string, 'test' }, typvalt2lua(l_tv))
- end)
- end)
- describe('allocated string()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local s = lib.xstrdup('test')
- alloc_log:clear()
- lib.tv_list_append_allocated_string(l, s)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_allocated_string(l, nil)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_allocated_string(l, nil)
- alloc_log:check({
- a.li(l.lv_last),
- })
- eq({ 'test', null_string, null_string }, typvalt2lua(l_tv))
- end)
- end)
- describe('number()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- alloc_log:clear()
- lib.tv_list_append_number(l, -100500)
- alloc_log:check({
- a.li(l.lv_last),
- })
- lib.tv_list_append_number(l, 100500)
- alloc_log:check({
- a.li(l.lv_last),
- })
- eq({ int(-100500), int(100500) }, typvalt2lua(l_tv))
- end)
- end)
- describe('tv()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local l_l_tv = lua2typvalt(empty_list)
- alloc_log:clear()
- local l_l = l_l_tv.vval.v_list
- eq(1, l_l.lv_refcount)
- lib.tv_list_append_tv(l, l_l_tv)
- eq(2, l_l.lv_refcount)
- eq(l_l, l.lv_first.li_tv.vval.v_list)
- alloc_log:check({
- a.li(l.lv_first),
- })
- local l_s_tv = lua2typvalt('test')
- alloc_log:check({
- a.str(l_s_tv.vval.v_string, 'test'),
- })
- lib.tv_list_append_tv(l, l_s_tv)
- alloc_log:check({
- a.li(l.lv_last),
- a.str(l.lv_last.li_tv.vval.v_string, 'test'),
- })
- eq({ empty_list, 'test' }, typvalt2lua(l_tv))
- end)
- end)
- describe('owned tv()', function()
- itp('works', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- local l_l_tv = lua2typvalt(empty_list)
- alloc_log:clear()
- local l_l = l_l_tv.vval.v_list
- eq(1, l_l.lv_refcount)
- lib.tv_list_append_owned_tv(l, l_l_tv)
- eq(1, l_l.lv_refcount)
- l_l.lv_refcount = l_l.lv_refcount + 1
- eq(l_l, l.lv_first.li_tv.vval.v_list)
- alloc_log:check({
- a.li(l.lv_first),
- })
- local l_s_tv = ffi.gc(lua2typvalt('test'), nil)
- alloc_log:check({
- a.str(l_s_tv.vval.v_string, 'test'),
- })
- lib.tv_list_append_owned_tv(l, l_s_tv)
- eq(l_s_tv.vval.v_string, l.lv_last.li_tv.vval.v_string)
- l_s_tv.vval.v_string = nil
- alloc_log:check({
- a.li(l.lv_last),
- })
- eq({ empty_list, 'test' }, typvalt2lua(l_tv))
- end)
- end)
- end)
- describe('copy()', function()
- local function tv_list_copy(...)
- return ffi.gc(lib.tv_list_copy(...), lib.tv_list_unref)
- end
- itp('copies NULL correctly', function()
- eq(nil, lib.tv_list_copy(nil, nil, true, 0))
- eq(nil, lib.tv_list_copy(nil, nil, false, 0))
- eq(nil, lib.tv_list_copy(nil, nil, true, 1))
- eq(nil, lib.tv_list_copy(nil, nil, false, 1))
- end)
- itp('copies list correctly without converting items', function()
- do
- local v = { { ['«'] = '»' }, { '„' }, 1, '“', null_string, null_list, null_dict }
- local l_tv = lua2typvalt(v)
- local l = l_tv.vval.v_list
- local lis = list_items(l)
- alloc_log:clear()
- eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
- local l_copy1 = tv_list_copy(nil, l, false, 0)
- eq(2, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(2, lis[2].li_tv.vval.v_list.lv_refcount)
- local lis_copy1 = list_items(l_copy1)
- eq(lis[1].li_tv.vval.v_dict, lis_copy1[1].li_tv.vval.v_dict)
- eq(lis[2].li_tv.vval.v_list, lis_copy1[2].li_tv.vval.v_list)
- eq(v, lst2tbl(l_copy1))
- alloc_log:check({
- a.list(l_copy1),
- a.li(lis_copy1[1]),
- a.li(lis_copy1[2]),
- a.li(lis_copy1[3]),
- a.li(lis_copy1[4]),
- a.str(lis_copy1[4].li_tv.vval.v_string, #v[4]),
- a.li(lis_copy1[5]),
- a.li(lis_copy1[6]),
- a.li(lis_copy1[7]),
- })
- lib.tv_list_free(ffi.gc(l_copy1, nil))
- alloc_log:clear()
- eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
- local l_deepcopy1 = tv_list_copy(nil, l, true, 0)
- neq(nil, l_deepcopy1)
- eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
- local lis_deepcopy1 = list_items(l_deepcopy1)
- neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
- neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
- eq(v, lst2tbl(l_deepcopy1))
- local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
- alloc_log:check({
- a.list(l_deepcopy1),
- a.li(lis_deepcopy1[1]),
- a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
- a.di(di_deepcopy1, #'«'),
- a.str(di_deepcopy1.di_tv.vval.v_string, #v[1]['«']),
- a.li(lis_deepcopy1[2]),
- a.list(lis_deepcopy1[2].li_tv.vval.v_list),
- a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
- a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
- a.li(lis_deepcopy1[3]),
- a.li(lis_deepcopy1[4]),
- a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
- a.li(lis_deepcopy1[5]),
- a.li(lis_deepcopy1[6]),
- a.li(lis_deepcopy1[7]),
- })
- end
- collectgarbage()
- end)
- itp('copies list correctly and converts items', function()
- local vc = vimconv_alloc()
- -- UTF-8 ↔ latin1 conversions needs no iconv
- eq(OK, lib.convert_setup(vc, to_cstr('utf-8'), to_cstr('latin1')))
- local v = { { ['«'] = '»' }, { '„' }, 1, '“', null_string, null_list, null_dict }
- local l_tv = lua2typvalt(v)
- local l = l_tv.vval.v_list
- local lis = list_items(l)
- alloc_log:clear()
- eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
- local l_deepcopy1 = tv_list_copy(vc, l, true, 0)
- neq(nil, l_deepcopy1)
- eq(1, lis[1].li_tv.vval.v_dict.dv_refcount)
- eq(1, lis[2].li_tv.vval.v_list.lv_refcount)
- local lis_deepcopy1 = list_items(l_deepcopy1)
- neq(lis[1].li_tv.vval.v_dict, lis_deepcopy1[1].li_tv.vval.v_dict)
- neq(lis[2].li_tv.vval.v_list, lis_deepcopy1[2].li_tv.vval.v_list)
- eq(
- { { ['\171'] = '\187' }, { '\191' }, 1, '\191', null_string, null_list, null_dict },
- lst2tbl(l_deepcopy1)
- )
- local di_deepcopy1 = first_di(lis_deepcopy1[1].li_tv.vval.v_dict)
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.list(l_deepcopy1),
- a.li(lis_deepcopy1[1]),
- a.dict(lis_deepcopy1[1].li_tv.vval.v_dict),
- a.di(di_deepcopy1, 1),
- a.str(di_deepcopy1.di_tv.vval.v_string, 2),
- a.li(lis_deepcopy1[2]),
- a.list(lis_deepcopy1[2].li_tv.vval.v_list),
- a.li(lis_deepcopy1[2].li_tv.vval.v_list.lv_first),
- a.str(lis_deepcopy1[2].li_tv.vval.v_list.lv_first.li_tv.vval.v_string, #v[2][1]),
- a.li(lis_deepcopy1[3]),
- a.li(lis_deepcopy1[4]),
- a.str(lis_deepcopy1[4].li_tv.vval.v_string, #v[4]),
- a.li(lis_deepcopy1[5]),
- a.li(lis_deepcopy1[6]),
- a.li(lis_deepcopy1[7]),
- })
- end)
- itp('returns different/same containers with(out) copyID', function()
- local l_inner_tv = lua2typvalt(empty_list)
- local l_tv = lua2typvalt({ l_inner_tv, l_inner_tv })
- eq(3, l_inner_tv.vval.v_list.lv_refcount)
- local l = l_tv.vval.v_list
- eq(l.lv_first.li_tv.vval.v_list, l.lv_last.li_tv.vval.v_list)
- local l_copy1 = tv_list_copy(nil, l, true, 0)
- neq(l_copy1.lv_first.li_tv.vval.v_list, l_copy1.lv_last.li_tv.vval.v_list)
- eq({ empty_list, empty_list }, lst2tbl(l_copy1))
- local l_copy2 = tv_list_copy(nil, l, true, 2)
- eq(l_copy2.lv_first.li_tv.vval.v_list, l_copy2.lv_last.li_tv.vval.v_list)
- eq({ empty_list, empty_list }, lst2tbl(l_copy2))
- eq(3, l_inner_tv.vval.v_list.lv_refcount)
- end)
- itp('works with self-referencing list with copyID', function()
- local l_tv = lua2typvalt(empty_list)
- local l = l_tv.vval.v_list
- eq(1, l.lv_refcount)
- lib.tv_list_append_list(l, l)
- eq(2, l.lv_refcount)
- local l_copy1 = tv_list_copy(nil, l, true, 2)
- eq(2, l_copy1.lv_refcount)
- local v = {}
- v[1] = v
- eq(v, lst2tbl(l_copy1))
- local lis = list_items(l)
- lib.tv_list_item_remove(l, lis[1])
- eq(1, l.lv_refcount)
- local lis_copy1 = list_items(l_copy1)
- lib.tv_list_item_remove(l_copy1, lis_copy1[1])
- eq(1, l_copy1.lv_refcount)
- end)
- end)
- describe('extend()', function()
- itp('can extend list with itself', function()
- local l
- l = list(1, {})
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l, nil)
- alloc_log:check({
- a.li(l.lv_last.li_prev),
- a.li(l.lv_last),
- })
- eq(1, l.lv_refcount)
- eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq({ 1, {}, 1, {} }, lst2tbl(l))
- l = list(1, {})
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l, l.lv_last)
- alloc_log:check({
- a.li(l.lv_last.li_prev.li_prev),
- a.li(l.lv_last.li_prev),
- })
- eq({ 1, 1, {}, {} }, lst2tbl(l))
- eq(1, l.lv_refcount)
- eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- l = list(1, {})
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l, l.lv_first)
- alloc_log:check({
- a.li(l.lv_first),
- a.li(l.lv_first.li_next),
- })
- eq({ 1, {}, 1, {} }, lst2tbl(l))
- eq(1, l.lv_refcount)
- eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- end)
- itp('can extend list with an empty list', function()
- local l = list(1, {})
- local el = list()
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, el.lv_refcount)
- lib.tv_list_extend(l, el, nil)
- alloc_log:check({})
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, el.lv_refcount)
- eq({ 1, {} }, lst2tbl(l))
- lib.tv_list_extend(l, el, l.lv_first)
- alloc_log:check({})
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, el.lv_refcount)
- eq({ 1, {} }, lst2tbl(l))
- lib.tv_list_extend(l, el, l.lv_last)
- alloc_log:check({})
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, el.lv_refcount)
- eq({ 1, {} }, lst2tbl(l))
- end)
- itp('can extend list with another non-empty list', function()
- local l
- local l2 = list(42, empty_list)
- eq(1, l2.lv_refcount)
- eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- l = ffi.gc(list(1, {}), nil)
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l2, nil)
- alloc_log:check({
- a.li(l.lv_last.li_prev),
- a.li(l.lv_last),
- })
- eq(1, l2.lv_refcount)
- eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- eq({ 1, {}, 42, empty_list }, lst2tbl(l))
- lib.tv_list_free(l)
- eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- l = ffi.gc(list(1, {}), nil)
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l2, l.lv_first)
- alloc_log:check({
- a.li(l.lv_first),
- a.li(l.lv_first.li_next),
- })
- eq(1, l2.lv_refcount)
- eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- eq({ 42, empty_list, 1, {} }, lst2tbl(l))
- lib.tv_list_free(l)
- eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- l = ffi.gc(list(1, {}), nil)
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- lib.tv_list_extend(l, l2, l.lv_last)
- alloc_log:check({
- a.li(l.lv_first.li_next),
- a.li(l.lv_first.li_next.li_next),
- })
- eq(1, l2.lv_refcount)
- eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- eq({ 1, 42, empty_list, {} }, lst2tbl(l))
- lib.tv_list_free(l)
- eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- end)
- end)
- describe('concat()', function()
- itp('works with NULL lists', function()
- local l = list(1, {})
- alloc_log:clear()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- local rettv1 = typvalt()
- eq(OK, lib.tv_list_concat(nil, l, rettv1))
- eq(1, l.lv_refcount)
- eq(tonumber(lib.VAR_LIST), tonumber(rettv1.v_type))
- eq({ 1, {} }, typvalt2lua(rettv1))
- eq(1, rettv1.vval.v_list.lv_refcount)
- alloc_log:check({
- a.list(rettv1.vval.v_list),
- a.li(rettv1.vval.v_list.lv_first),
- a.li(rettv1.vval.v_list.lv_last),
- })
- eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- local rettv2 = typvalt()
- eq(OK, lib.tv_list_concat(l, nil, rettv2))
- eq(1, l.lv_refcount)
- eq(tonumber(lib.VAR_LIST), tonumber(rettv2.v_type))
- eq({ 1, {} }, typvalt2lua(rettv2))
- eq(1, rettv2.vval.v_list.lv_refcount)
- alloc_log:check({
- a.list(rettv2.vval.v_list),
- a.li(rettv2.vval.v_list.lv_first),
- a.li(rettv2.vval.v_list.lv_last),
- })
- eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- local rettv3 = typvalt()
- eq(OK, lib.tv_list_concat(nil, nil, rettv3))
- eq(tonumber(lib.VAR_LIST), tonumber(rettv3.v_type))
- eq(null_list, typvalt2lua(rettv3))
- alloc_log:check({})
- end)
- itp('works with two different lists', function()
- local l1 = list(1, {})
- local l2 = list(3, empty_list)
- eq(1, l1.lv_refcount)
- eq(1, l1.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, l2.lv_refcount)
- eq(1, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- alloc_log:clear()
- local rettv = typvalt()
- eq(OK, lib.tv_list_concat(l1, l2, rettv))
- eq(1, l1.lv_refcount)
- eq(2, l1.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, l2.lv_refcount)
- eq(2, l2.lv_last.li_tv.vval.v_list.lv_refcount)
- alloc_log:check({
- a.list(rettv.vval.v_list),
- a.li(rettv.vval.v_list.lv_first),
- a.li(rettv.vval.v_list.lv_first.li_next),
- a.li(rettv.vval.v_list.lv_last.li_prev),
- a.li(rettv.vval.v_list.lv_last),
- })
- eq({ 1, {}, 3, empty_list }, typvalt2lua(rettv))
- end)
- itp('can concatenate list with itself', function()
- local l = list(1, {})
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- alloc_log:clear()
- local rettv = typvalt()
- eq(OK, lib.tv_list_concat(l, l, rettv))
- eq(1, l.lv_refcount)
- eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- alloc_log:check({
- a.list(rettv.vval.v_list),
- a.li(rettv.vval.v_list.lv_first),
- a.li(rettv.vval.v_list.lv_first.li_next),
- a.li(rettv.vval.v_list.lv_last.li_prev),
- a.li(rettv.vval.v_list.lv_last),
- })
- eq({ 1, {}, 1, {} }, typvalt2lua(rettv))
- end)
- itp('can concatenate empty non-NULL lists', function()
- local l = list(1, {})
- local le = list()
- local le2 = list()
- eq(1, l.lv_refcount)
- eq(1, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, le.lv_refcount)
- eq(1, le2.lv_refcount)
- alloc_log:clear()
- local rettv1 = typvalt()
- eq(OK, lib.tv_list_concat(l, le, rettv1))
- eq(1, l.lv_refcount)
- eq(2, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, le.lv_refcount)
- eq(1, le2.lv_refcount)
- alloc_log:check({
- a.list(rettv1.vval.v_list),
- a.li(rettv1.vval.v_list.lv_first),
- a.li(rettv1.vval.v_list.lv_last),
- })
- eq({ 1, {} }, typvalt2lua(rettv1))
- local rettv2 = typvalt()
- eq(OK, lib.tv_list_concat(le, l, rettv2))
- eq(1, l.lv_refcount)
- eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, le.lv_refcount)
- eq(1, le2.lv_refcount)
- alloc_log:check({
- a.list(rettv2.vval.v_list),
- a.li(rettv2.vval.v_list.lv_first),
- a.li(rettv2.vval.v_list.lv_last),
- })
- eq({ 1, {} }, typvalt2lua(rettv2))
- local rettv3 = typvalt()
- eq(OK, lib.tv_list_concat(le, le, rettv3))
- eq(1, l.lv_refcount)
- eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, le.lv_refcount)
- eq(1, le2.lv_refcount)
- alloc_log:check({
- a.list(rettv3.vval.v_list),
- })
- eq(empty_list, typvalt2lua(rettv3))
- local rettv4 = typvalt()
- eq(OK, lib.tv_list_concat(le, le2, rettv4))
- eq(1, l.lv_refcount)
- eq(3, l.lv_last.li_tv.vval.v_dict.dv_refcount)
- eq(1, le.lv_refcount)
- eq(1, le2.lv_refcount)
- alloc_log:check({
- a.list(rettv4.vval.v_list),
- })
- eq(empty_list, typvalt2lua(rettv4))
- end)
- end)
- describe('join()', function()
- local function list_join(l, sep, join_ret)
- local ga = ga_alloc()
- eq(join_ret or OK, lib.tv_list_join(ga, l, sep))
- local ret = ''
- if ga.ga_data ~= nil then
- ret = ffi.string(ga.ga_data)
- end
- -- For some reason this is not working well in GC
- lib.ga_clear(ffi.gc(ga, nil))
- return ret
- end
- itp('works', function()
- local l
- l = list('boo', 'far')
- eq('boo far', list_join(l, ' '))
- eq('boofar', list_join(l, ''))
- l = list('boo')
- eq('boo', list_join(l, ' '))
- l = list()
- eq('', list_join(l, ' '))
- l = list({}, 'far')
- eq('{} far', list_join(l, ' '))
- local recursive_list = {}
- recursive_list[1] = recursive_list
- l = ffi.gc(list(recursive_list, 'far'), nil)
- eq('[[...@0]] far', list_join(l, ' '))
- local recursive_l = l.lv_first.li_tv.vval.v_list
- local recursive_li = recursive_l.lv_first
- lib.tv_list_item_remove(recursive_l, recursive_li)
- lib.tv_list_free(l)
- end)
- end)
- describe('equal()', function()
- itp('compares empty and NULL lists correctly', function()
- local l = list()
- local l2 = list()
- -- NULL lists are equal to empty lists
- eq(true, lib.tv_list_equal(l, nil, true))
- eq(true, lib.tv_list_equal(nil, l, false))
- -- NULL lists are equal themselves
- eq(true, lib.tv_list_equal(nil, nil, true))
- eq(true, lib.tv_list_equal(nil, nil, false))
- -- As well as empty lists
- eq(true, lib.tv_list_equal(l, l, true))
- eq(true, lib.tv_list_equal(l, l2, false))
- eq(true, lib.tv_list_equal(l2, l, false))
- eq(true, lib.tv_list_equal(l2, l2, true))
- end)
- itp('compares lists correctly when case is not ignored', function()
- local l1 = list('abc', { 1, 2, 'Abc' }, 'def')
- local l2 = list('abc', { 1, 2, 'Abc' })
- local l3 = list('abc', { 1, 2, 'Abc' }, 'Def')
- local l4 = list('abc', { 1, 2, 'Abc', 4 }, 'def')
- local l5 = list('Abc', { 1, 2, 'Abc' }, 'def')
- local l6 = list('abc', { 1, 2, 'Abc' }, 'def')
- local l7 = list('abc', { 1, 2, 'abc' }, 'def')
- local l8 = list('abc', nil, 'def')
- local l9 = list('abc', { 1, 2, nil }, 'def')
- eq(true, lib.tv_list_equal(l1, l1, false))
- eq(false, lib.tv_list_equal(l1, l2, false))
- eq(false, lib.tv_list_equal(l1, l3, false))
- eq(false, lib.tv_list_equal(l1, l4, false))
- eq(false, lib.tv_list_equal(l1, l5, false))
- eq(true, lib.tv_list_equal(l1, l6, false))
- eq(false, lib.tv_list_equal(l1, l7, false))
- eq(false, lib.tv_list_equal(l1, l8, false))
- eq(false, lib.tv_list_equal(l1, l9, false))
- end)
- itp('compares lists correctly when case is ignored', function()
- local l1 = list('abc', { 1, 2, 'Abc' }, 'def')
- local l2 = list('abc', { 1, 2, 'Abc' })
- local l3 = list('abc', { 1, 2, 'Abc' }, 'Def')
- local l4 = list('abc', { 1, 2, 'Abc', 4 }, 'def')
- local l5 = list('Abc', { 1, 2, 'Abc' }, 'def')
- local l6 = list('abc', { 1, 2, 'Abc' }, 'def')
- local l7 = list('abc', { 1, 2, 'abc' }, 'def')
- local l8 = list('abc', nil, 'def')
- local l9 = list('abc', { 1, 2, nil }, 'def')
- eq(true, lib.tv_list_equal(l1, l1, true))
- eq(false, lib.tv_list_equal(l1, l2, true))
- eq(true, lib.tv_list_equal(l1, l3, true))
- eq(false, lib.tv_list_equal(l1, l4, true))
- eq(true, lib.tv_list_equal(l1, l5, true))
- eq(true, lib.tv_list_equal(l1, l6, true))
- eq(true, lib.tv_list_equal(l1, l7, true))
- eq(false, lib.tv_list_equal(l1, l8, true))
- eq(false, lib.tv_list_equal(l1, l9, true))
- end)
- end)
- describe('find', function()
- describe('()', function()
- itp('correctly indexes list', function()
- local l = list(1, 2, 3, 4, 5)
- local lis = list_items(l)
- alloc_log:clear()
- eq(nil, lib.tv_list_find(nil, -1))
- eq(nil, lib.tv_list_find(nil, 0))
- eq(nil, lib.tv_list_find(nil, 1))
- eq(nil, lib.tv_list_find(l, 5))
- eq(nil, lib.tv_list_find(l, -6))
- eq(lis[1], lib.tv_list_find(l, -5))
- eq(lis[5], lib.tv_list_find(l, 4))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, -3))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, -3))
- l.lv_idx_item = nil
- eq(lis[1], lib.tv_list_find(l, -5))
- l.lv_idx_item = nil
- eq(lis[5], lib.tv_list_find(l, 4))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, 2))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, -3))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, 2))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, 2))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, -3))
- l.lv_idx_item = nil
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[1], lib.tv_list_find(l, -5))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[5], lib.tv_list_find(l, 4))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, -3))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, 2))
- eq(lis[3], lib.tv_list_find(l, -3))
- alloc_log:check({})
- end)
- end)
- describe('nr()', function()
- local function tv_list_find_nr(l, n, msg)
- return check_emsg(function()
- local err = ffi.new('bool[1]', { false })
- local ret = lib.tv_list_find_nr(l, n, err)
- return (err[0] == true), ret
- end, msg)
- end
- itp('returns correct number', function()
- local l = list(int(1), int(2), int(3), int(4), int(5))
- alloc_log:clear()
- eq({ false, 1 }, { tv_list_find_nr(l, -5) })
- eq({ false, 5 }, { tv_list_find_nr(l, 4) })
- eq({ false, 3 }, { tv_list_find_nr(l, 2) })
- eq({ false, 3 }, { tv_list_find_nr(l, -3) })
- alloc_log:check({})
- end)
- itp('returns correct number when given a string', function()
- local l = list('1', '2', '3', '4', '5')
- alloc_log:clear()
- eq({ false, 1 }, { tv_list_find_nr(l, -5) })
- eq({ false, 5 }, { tv_list_find_nr(l, 4) })
- eq({ false, 3 }, { tv_list_find_nr(l, 2) })
- eq({ false, 3 }, { tv_list_find_nr(l, -3) })
- alloc_log:check({})
- end)
- itp('returns zero when given a NULL string', function()
- local l = list(null_string)
- alloc_log:clear()
- eq({ false, 0 }, { tv_list_find_nr(l, 0) })
- alloc_log:check({})
- end)
- itp('errors out on NULL lists', function()
- eq({ true, -1 }, { tv_list_find_nr(nil, -5) })
- eq({ true, -1 }, { tv_list_find_nr(nil, 4) })
- eq({ true, -1 }, { tv_list_find_nr(nil, 2) })
- eq({ true, -1 }, { tv_list_find_nr(nil, -3) })
- alloc_log:check({})
- end)
- itp('errors out on out-of-range indexes', function()
- local l = list(int(1), int(2), int(3), int(4), int(5))
- alloc_log:clear()
- eq({ true, -1 }, { tv_list_find_nr(l, -6) })
- eq({ true, -1 }, { tv_list_find_nr(l, 5) })
- alloc_log:check({})
- end)
- itp('errors out on invalid types', function()
- local l = list(1, empty_list, {})
- eq({ true, 0 }, { tv_list_find_nr(l, 0, 'E805: Using a Float as a Number') })
- eq({ true, 0 }, { tv_list_find_nr(l, 1, 'E745: Using a List as a Number') })
- eq({ true, 0 }, { tv_list_find_nr(l, 2, 'E728: Using a Dictionary as a Number') })
- eq({ true, 0 }, { tv_list_find_nr(l, -1, 'E728: Using a Dictionary as a Number') })
- eq({ true, 0 }, { tv_list_find_nr(l, -2, 'E745: Using a List as a Number') })
- eq({ true, 0 }, { tv_list_find_nr(l, -3, 'E805: Using a Float as a Number') })
- end)
- end)
- local function tv_list_find_str(l, n, msg)
- return check_emsg(function()
- local ret = lib.tv_list_find_str(l, n)
- local s = nil
- if ret ~= nil then
- s = ffi.string(ret)
- end
- return s
- end, msg)
- end
- describe('str()', function()
- itp('returns correct string', function()
- local l = list(int(1), 2.5, int(3), int(4), int(5))
- alloc_log:clear()
- eq('1', tv_list_find_str(l, -5))
- eq('2.5', tv_list_find_str(l, 1))
- eq('5', tv_list_find_str(l, 4))
- eq('3', tv_list_find_str(l, 2))
- eq('3', tv_list_find_str(l, -3))
- alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
- end)
- itp('returns string when used with VAR_STRING items', function()
- local l = list('1', '2', '3', '4', '5')
- alloc_log:clear()
- eq('1', tv_list_find_str(l, -5))
- eq('5', tv_list_find_str(l, 4))
- eq('3', tv_list_find_str(l, 2))
- eq('3', tv_list_find_str(l, -3))
- alloc_log:check({})
- end)
- itp('returns empty when used with NULL string', function()
- local l = list(null_string)
- alloc_log:clear()
- eq('', tv_list_find_str(l, 0))
- alloc_log:check({})
- end)
- itp('fails with error message when index is out of range', function()
- local l = list(int(1), int(2), int(3), int(4), int(5))
- eq(nil, tv_list_find_str(l, -6, 'E684: List index out of range: -6'))
- eq(nil, tv_list_find_str(l, 5, 'E684: List index out of range: 5'))
- end)
- itp('fails with error message on invalid types', function()
- local l = list(empty_list, {})
- eq('', tv_list_find_str(l, 0, 'E730: Using a List as a String'))
- eq('', tv_list_find_str(l, 1, 'E731: Using a Dictionary as a String'))
- eq('', tv_list_find_str(l, -1, 'E731: Using a Dictionary as a String'))
- eq('', tv_list_find_str(l, -2, 'E730: Using a List as a String'))
- end)
- end)
- end)
- describe('idx_of_item()', function()
- itp('works', function()
- local l = list(1, 2, 3, 4, 5)
- local l2 = list(42, empty_list)
- local lis = list_items(l)
- local lis2 = list_items(l2)
- for i, li in ipairs(lis) do
- eq(i - 1, lib.tv_list_idx_of_item(l, li))
- end
- eq(-1, lib.tv_list_idx_of_item(l, lis2[1]))
- eq(-1, lib.tv_list_idx_of_item(l, nil))
- eq(-1, lib.tv_list_idx_of_item(nil, nil))
- eq(-1, lib.tv_list_idx_of_item(nil, lis[1]))
- end)
- end)
- end)
- describe('dict', function()
- describe('watcher', function()
- describe('add()/remove()', function()
- itp('works with an empty key', function()
- local d = dict({})
- eq({}, dict_watchers(d))
- local cb = ffi.gc(tbl2callback({ type = 'none' }), nil)
- alloc_log:clear()
- lib.tv_dict_watcher_add(d, '*', 0, cb[0])
- local ws, qs = dict_watchers(d)
- local key_p = qs[1].key_pattern
- alloc_log:check({
- a.dwatcher(qs[1]),
- a.str(key_p, 0),
- })
- eq({ { busy = false, cb = { type = 'none' }, pat = '' } }, ws)
- eq(true, lib.tv_dict_watcher_remove(d, 'x', 0, cb[0]))
- alloc_log:check({
- a.freed(key_p),
- a.freed(qs[1]),
- })
- eq({}, dict_watchers(d))
- end)
- itp('works with multiple callbacks', function()
- local d = dict({})
- eq({}, dict_watchers(d))
- alloc_log:check({ a.dict(d) })
- local cbs = {}
- cbs[1] = { 'te', ffi.gc(tbl2callback({ type = 'none' }), nil) }
- alloc_log:check({})
- cbs[2] = { 'foo', ffi.gc(tbl2callback({ type = 'fref', fref = 'tr' }), nil) }
- alloc_log:check({
- a.str(cbs[2][2].data.funcref, #'tr'),
- })
- cbs[3] = {
- 'te',
- ffi.gc(
- tbl2callback({
- type = 'pt',
- fref = 'tr',
- pt = {
- value = 'tr',
- args = { 'test' },
- dict = {},
- },
- }),
- nil
- ),
- }
- local pt3 = cbs[3][2].data.partial
- local pt3_argv = pt3.pt_argv
- local pt3_dict = pt3.pt_dict
- local pt3_name = pt3.pt_name
- local pt3_str_arg = pt3.pt_argv[0].vval.v_string
- alloc_log:check({
- a.lua_pt(pt3),
- a.lua_tvs(pt3_argv, pt3.pt_argc),
- a.str(pt3_str_arg, #'test'),
- a.dict(pt3_dict),
- a.str(pt3_name, #'tr'),
- })
- for _, v in ipairs(cbs) do
- lib.tv_dict_watcher_add(d, v[1], #v[1], v[2][0])
- end
- local ws, qs, kps = dict_watchers(d)
- eq({
- { busy = false, pat = cbs[1][1], cb = { type = 'none' } },
- { busy = false, pat = cbs[2][1], cb = { type = 'fref', fref = 'tr' } },
- {
- busy = false,
- pat = cbs[3][1],
- cb = {
- type = 'pt',
- fref = 'tr',
- pt = {
- [type_key] = func_type,
- value = 'tr',
- args = { 'test' },
- dict = {},
- },
- },
- },
- }, ws)
- alloc_log:check({
- a.dwatcher(qs[1]),
- a.str(kps[1][1], kps[1][2]),
- a.dwatcher(qs[2]),
- a.str(kps[2][1], kps[2][2]),
- a.dwatcher(qs[3]),
- a.str(kps[3][1], kps[3][2]),
- })
- eq(true, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
- alloc_log:check({
- a.freed(cbs[2][2].data.funcref),
- a.freed(kps[2][1]),
- a.freed(qs[2]),
- })
- eq(false, lib.tv_dict_watcher_remove(d, cbs[2][1], #cbs[2][1], cbs[2][2][0]))
- eq({
- { busy = false, pat = cbs[1][1], cb = { type = 'none' } },
- {
- busy = false,
- pat = cbs[3][1],
- cb = {
- type = 'pt',
- fref = 'tr',
- pt = {
- [type_key] = func_type,
- value = 'tr',
- args = { 'test' },
- dict = {},
- },
- },
- },
- }, dict_watchers(d))
- eq(true, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
- alloc_log:check({
- a.freed(pt3_str_arg),
- a.freed(pt3_argv),
- a.freed(pt3_dict),
- a.freed(pt3_name),
- a.freed(pt3),
- a.freed(kps[3][1]),
- a.freed(qs[3]),
- })
- eq(false, lib.tv_dict_watcher_remove(d, cbs[3][1], #cbs[3][1], cbs[3][2][0]))
- eq({ { busy = false, pat = cbs[1][1], cb = { type = 'none' } } }, dict_watchers(d))
- eq(true, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
- alloc_log:check({
- a.freed(kps[1][1]),
- a.freed(qs[1]),
- })
- eq(false, lib.tv_dict_watcher_remove(d, cbs[1][1], #cbs[1][1], cbs[1][2][0]))
- eq({}, dict_watchers(d))
- end)
- end)
- describe('notify', function()
- -- Way too hard to test it here, functional tests in
- -- dict_notifications_spec.lua.
- end)
- end)
- describe('item', function()
- describe('alloc()/free()', function()
- local function check_tv_dict_item_alloc_len(s, len, tv, more_frees)
- local di
- if len == nil then
- di = ffi.gc(lib.tv_dict_item_alloc(s), nil)
- len = #s
- else
- di = ffi.gc(lib.tv_dict_item_alloc_len(s, len or #s), nil)
- end
- eq(s:sub(1, len), ffi.string(di.di_key))
- alloc_log:check({ a.di(di, len) })
- if tv then
- di.di_tv = ffi.gc(tv, nil)
- else
- di.di_tv.v_type = lib.VAR_UNKNOWN
- end
- lib.tv_dict_item_free(di)
- alloc_log:check(concat_tables(more_frees, { a.freed(di) }))
- end
- local function check_tv_dict_item_alloc(s, tv, more_frees)
- return check_tv_dict_item_alloc_len(s, nil, tv, more_frees)
- end
- itp('works', function()
- check_tv_dict_item_alloc('')
- check_tv_dict_item_alloc('t')
- check_tv_dict_item_alloc('TEST')
- check_tv_dict_item_alloc_len('', 0)
- check_tv_dict_item_alloc_len('TEST', 2)
- local tv = lua2typvalt('test')
- alloc_log:check({ a.str(tv.vval.v_string, #'test') })
- check_tv_dict_item_alloc('', tv, { a.freed(tv.vval.v_string) })
- tv = lua2typvalt('test')
- alloc_log:check({ a.str(tv.vval.v_string, #'test') })
- check_tv_dict_item_alloc_len('', 0, tv, { a.freed(tv.vval.v_string) })
- end)
- end)
- describe('add()/remove()', function()
- itp('works', function()
- local d = dict()
- eq({}, dct2tbl(d))
- alloc_log:check({ a.dict(d) })
- local di = ffi.gc(lib.tv_dict_item_alloc(''), nil)
- local tv = lua2typvalt('test')
- di.di_tv = ffi.gc(tv, nil)
- alloc_log:check({ a.di(di, ''), a.str(tv.vval.v_string, 'test') })
- eq(OK, lib.tv_dict_add(d, di))
- alloc_log:check({})
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add(d, di)
- end, 'E685: Internal error: hash_add(): duplicate key ""')
- )
- alloc_log:clear()
- lib.tv_dict_item_remove(d, di)
- alloc_log:check({
- a.freed(tv.vval.v_string),
- a.freed(di),
- })
- end)
- end)
- end)
- describe('indexing', function()
- describe('find()', function()
- local function tv_dict_find(d, key, key_len)
- local di = lib.tv_dict_find(d, key, key_len or #key)
- if di == nil then
- return nil, nil, nil
- end
- return typvalt2lua(di.di_tv), ffi.string(di.di_key), di
- end
- itp('works with NULL dict', function()
- eq(nil, lib.tv_dict_find(nil, '', 0))
- eq(nil, lib.tv_dict_find(nil, 'test', -1))
- eq(nil, lib.tv_dict_find(nil, nil, 0))
- end)
- itp('works with empty key', function()
- local lua_d = {
- [''] = 0,
- t = 1,
- te = 2,
- tes = 3,
- test = 4,
- testt = 5,
- }
- local d = dict(lua_d)
- alloc_log:clear()
- eq(lua_d, dct2tbl(d))
- alloc_log:check({})
- local dis = dict_items(d)
- eq({ 0, '', dis[''] }, { tv_dict_find(d, '', 0) })
- end)
- itp('works with len properly', function()
- local lua_d = {
- [''] = 0,
- t = 1,
- te = 2,
- tes = 3,
- test = 4,
- testt = 5,
- }
- local d = dict(lua_d)
- alloc_log:clear()
- eq(lua_d, dct2tbl(d))
- alloc_log:check({})
- for i = 0, 5 do
- local v, k = tv_dict_find(d, 'testt', i)
- eq({ i, ('testt'):sub(1, i) }, { v, k })
- end
- eq(nil, tv_dict_find(d, 'testt', 6)) -- Should take NUL byte
- eq(5, tv_dict_find(d, 'testt', -1))
- alloc_log:check({})
- end)
- end)
- describe('get_number()', function()
- itp('works with NULL dict', function()
- eq(
- 0,
- check_emsg(function()
- return lib.tv_dict_get_number(nil, 'test')
- end, nil)
- )
- end)
- itp('works', function()
- local d = ffi.gc(dict({ test = {} }), nil)
- eq(
- 0,
- check_emsg(function()
- return lib.tv_dict_get_number(d, 'test')
- end, 'E728: Using a Dictionary as a Number')
- )
- d = ffi.gc(dict({ tes = int(42), t = 44, te = '43' }), nil)
- alloc_log:clear()
- eq(
- 0,
- check_emsg(function()
- return lib.tv_dict_get_number(d, 'test')
- end, nil)
- )
- eq(
- 42,
- check_emsg(function()
- return lib.tv_dict_get_number(d, 'tes')
- end, nil)
- )
- eq(
- 43,
- check_emsg(function()
- return lib.tv_dict_get_number(d, 'te')
- end, nil)
- )
- alloc_log:check({})
- eq(
- 0,
- check_emsg(function()
- return lib.tv_dict_get_number(d, 't')
- end, 'E805: Using a Float as a Number')
- )
- end)
- end)
- describe('get_string()', function()
- itp('works with NULL dict', function()
- eq(
- nil,
- check_emsg(function()
- return lib.tv_dict_get_string(nil, 'test', false)
- end, nil)
- )
- end)
- itp('works', function()
- local d = ffi.gc(dict({ test = {} }), nil)
- eq(
- '',
- ffi.string(check_emsg(function()
- return lib.tv_dict_get_string(d, 'test', false)
- end, 'E731: Using a Dictionary as a String'))
- )
- d = ffi.gc(dict({ tes = int(42), t = 44, te = '43', xx = int(45) }), nil)
- alloc_log:clear()
- local dis = dict_items(d)
- eq(
- nil,
- check_emsg(function()
- return lib.tv_dict_get_string(d, 'test', false)
- end, nil)
- )
- local s42 = check_emsg(function()
- return lib.tv_dict_get_string(d, 'tes', false)
- end, nil)
- eq('42', ffi.string(s42))
- local s45 = check_emsg(function()
- return lib.tv_dict_get_string(d, 'xx', false)
- end, nil)
- eq(s42, s45)
- eq('45', ffi.string(s45))
- eq('45', ffi.string(s42))
- local s43 = check_emsg(function()
- return lib.tv_dict_get_string(d, 'te', false)
- end, nil)
- eq('43', ffi.string(s43))
- neq(s42, s43)
- eq(s43, dis.te.di_tv.vval.v_string)
- alloc_log:check({})
- local s44 = check_emsg(function()
- return lib.tv_dict_get_string(d, 't', false)
- end, nil)
- eq('44.0', ffi.string(s44))
- alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
- end)
- itp('allocates a string copy when requested', function()
- local function tv_dict_get_string_alloc(d, key, emsg, is_float)
- alloc_log:clear()
- local ret = check_emsg(function()
- return lib.tv_dict_get_string(d, key, true)
- end, emsg)
- local s_ret = (ret ~= nil) and ffi.string(ret) or nil
- if not emsg then
- if s_ret then
- if is_float then
- alloc_log:check({
- a.freed(alloc_log.null),
- a.freed(alloc_log.null),
- a.str(ret, s_ret),
- })
- else
- alloc_log:check({ a.str(ret, s_ret) })
- end
- else
- alloc_log:check({})
- end
- end
- lib.xfree(ret)
- return s_ret
- end
- local d = ffi.gc(dict({ test = {} }), nil)
- eq('', tv_dict_get_string_alloc(d, 'test', 'E731: Using a Dictionary as a String'))
- d = ffi.gc(dict({ tes = int(42), t = 44, te = '43', xx = int(45) }), nil)
- alloc_log:clear()
- eq(nil, tv_dict_get_string_alloc(d, 'test'))
- eq('42', tv_dict_get_string_alloc(d, 'tes'))
- eq('45', tv_dict_get_string_alloc(d, 'xx'))
- eq('43', tv_dict_get_string_alloc(d, 'te'))
- eq('44.0', tv_dict_get_string_alloc(d, 't', nil, true))
- end)
- end)
- describe('get_string_buf()', function()
- local function tv_dict_get_string_buf(d, key, is_float, buf, emsg)
- buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
- alloc_log:clear()
- local ret = check_emsg(function()
- return lib.tv_dict_get_string_buf(d, key, buf)
- end, emsg)
- local s_ret = (ret ~= nil) and ffi.string(ret) or nil
- if not emsg then
- if is_float then
- alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
- else
- alloc_log:check({})
- end
- end
- return s_ret, ret, buf
- end
- itp('works with NULL dict', function()
- eq(nil, tv_dict_get_string_buf(nil, 'test'))
- end)
- itp('works', function()
- local lua_d = {
- [''] = {},
- t = 1,
- te = int(2),
- tes = empty_list,
- test = 'tset',
- testt = 5,
- }
- local d = dict(lua_d)
- alloc_log:clear()
- eq(lua_d, dct2tbl(d))
- alloc_log:check({})
- local s, r, b
- s, r, b = tv_dict_get_string_buf(d, 'test')
- neq(r, b)
- eq('tset', s)
- s, r, b = tv_dict_get_string_buf(d, 't', true)
- eq(r, b)
- eq('1.0', s)
- s, r, b = tv_dict_get_string_buf(d, 'te')
- eq(r, b)
- eq('2', s)
- end)
- end)
- describe('get_string_buf_chk()', function()
- local function tv_dict_get_string_buf_chk(d, key, is_float, len, buf, def, emsg)
- buf = buf or ffi.gc(lib.xmalloc(lib.NUMBUFLEN), lib.xfree)
- def = def or ffi.gc(lib.xstrdup('DEFAULT'), lib.xfree)
- len = len or #key
- alloc_log:clear()
- local ret = check_emsg(function()
- return lib.tv_dict_get_string_buf_chk(d, key, len, buf, def)
- end, emsg)
- local s_ret = (ret ~= nil) and ffi.string(ret) or nil
- if not emsg then
- if is_float then
- alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
- else
- alloc_log:check({})
- end
- end
- return s_ret, ret, buf, def
- end
- itp('works with NULL dict', function()
- eq('DEFAULT', tv_dict_get_string_buf_chk(nil, 'test'))
- end)
- itp('works', function()
- local lua_d = {
- [''] = {},
- t = 1,
- te = int(2),
- tes = empty_list,
- test = 'tset',
- testt = 5,
- }
- local d = dict(lua_d)
- alloc_log:clear()
- eq(lua_d, dct2tbl(d))
- alloc_log:check({})
- local s, r, b, def
- s, r, b, def = tv_dict_get_string_buf_chk(d, 'test')
- neq(r, b)
- neq(r, def)
- eq('tset', s)
- s, r, b, def = tv_dict_get_string_buf_chk(d, 'test', true, 1)
- eq(r, b)
- neq(r, def)
- eq('1.0', s)
- s, r, b, def = tv_dict_get_string_buf_chk(d, 'te')
- eq(r, b)
- neq(r, def)
- eq('2', s)
- s, r, b, def = tv_dict_get_string_buf_chk(d, 'TEST')
- eq(r, def)
- neq(r, b)
- eq('DEFAULT', s)
- end)
- end)
- describe('get_callback()', function()
- local function tv_dict_get_callback(d, key, key_len, emsg)
- key_len = key_len or #key
- local cb =
- ffi.gc(ffi.cast('Callback*', lib.xmalloc(ffi.sizeof('Callback'))), lib.callback_free)
- alloc_log:clear()
- local ret = check_emsg(function()
- return lib.tv_dict_get_callback(d, key, key_len, cb)
- end, emsg)
- local cb_lua = callback2tbl(cb[0])
- return cb_lua, ret
- end
- itp('works with NULL dict', function()
- eq({ { type = 'none' }, true }, { tv_dict_get_callback(nil, '') })
- end)
- itp('works', function()
- local lua_d = {
- [''] = 'tr',
- t = int(1),
- te = { [type_key] = func_type, value = 'tr' },
- tes = { [type_key] = func_type, value = 'tr', args = { 'a', 'b' } },
- test = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = {} },
- testt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = { 1 } },
- }
- local d = dict(lua_d)
- eq(lua_d, dct2tbl(d))
- eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, '', -1) })
- eq({ { type = 'none' }, true }, { tv_dict_get_callback(d, 'x', -1) })
- eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, 'testt', 0) })
- eq({ { type = 'none' }, false }, {
- tv_dict_get_callback(
- d,
- 'test',
- 1,
- 'E6000: Argument is not a function or function name'
- ),
- })
- eq({ { type = 'fref', fref = 'tr' }, true }, { tv_dict_get_callback(d, 'testt', 2) })
- eq({
- {
- type = 'pt',
- fref = 'tr',
- pt = {
- [type_key] = func_type,
- value = 'tr',
- args = { 'a', 'b' },
- },
- },
- true,
- }, { tv_dict_get_callback(d, 'testt', 3) })
- eq({
- {
- type = 'pt',
- fref = 'Test',
- pt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = {} },
- },
- true,
- }, { tv_dict_get_callback(d, 'testt', 4) })
- eq({
- {
- type = 'pt',
- fref = 'Test',
- pt = { [type_key] = func_type, value = 'Test', dict = { test = 1 }, args = { 1 } },
- },
- true,
- }, { tv_dict_get_callback(d, 'testt', 5) })
- end)
- end)
- end)
- describe('add', function()
- describe('()', function()
- itp('works', function()
- local di = lib.tv_dict_item_alloc_len('t-est', 5)
- alloc_log:check({ a.di(di, 't-est') })
- di.di_tv.v_type = lib.VAR_NUMBER
- di.di_tv.vval.v_number = 42
- local d = dict({ test = 10 })
- local dis = dict_items(d)
- alloc_log:check({
- a.dict(d),
- a.di(dis.test, 'test'),
- })
- eq({ test = 10 }, dct2tbl(d))
- alloc_log:clear()
- eq(OK, lib.tv_dict_add(d, di))
- alloc_log:check({})
- eq({ test = 10, ['t-est'] = int(42) }, dct2tbl(d))
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add(d, di)
- end, 'E685: Internal error: hash_add(): duplicate key "t-est"')
- )
- end)
- end)
- describe('list()', function()
- itp('works', function()
- local l = list(1, 2, 3)
- alloc_log:clear()
- eq(1, l.lv_refcount)
- local d = dict({ test = 10 })
- alloc_log:clear()
- eq({ test = 10 }, dct2tbl(d))
- eq(OK, lib.tv_dict_add_list(d, 'testt', 3, l))
- local dis = dict_items(d)
- alloc_log:check({ a.di(dis.tes, 'tes') })
- eq({ test = 10, tes = { 1, 2, 3 } }, dct2tbl(d))
- eq(2, l.lv_refcount)
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_list(d, 'testt', 3, l)
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- eq(2, l.lv_refcount)
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_list(d, 'testt', 3, l)
- end, nil)
- )
- eq(2, l.lv_refcount)
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({})
- end)
- end)
- describe('dict()', function()
- itp('works', function()
- local d2 = dict({ foo = 42 })
- alloc_log:clear()
- eq(1, d2.dv_refcount)
- local d = dict({ test = 10 })
- alloc_log:clear()
- eq({ test = 10 }, dct2tbl(d))
- eq(OK, lib.tv_dict_add_dict(d, 'testt', 3, d2))
- local dis = dict_items(d)
- alloc_log:check({ a.di(dis.tes, 'tes') })
- eq({ test = 10, tes = { foo = 42 } }, dct2tbl(d))
- eq(2, d2.dv_refcount)
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_dict(d, 'testt', 3, d2)
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- eq(2, d2.dv_refcount)
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_dict(d, 'testt', 3, d2)
- end, nil)
- )
- eq(2, d2.dv_refcount)
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({})
- end)
- end)
- describe('nr()', function()
- itp('works', function()
- local d = dict({ test = 10 })
- alloc_log:clear()
- eq({ test = 10 }, dct2tbl(d))
- eq(OK, lib.tv_dict_add_nr(d, 'testt', 3, 2))
- local dis = dict_items(d)
- alloc_log:check({ a.di(dis.tes, 'tes') })
- eq({ test = 10, tes = int(2) }, dct2tbl(d))
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_nr(d, 'testt', 3, 2)
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_nr(d, 'testt', 3, 2)
- end, nil)
- )
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({})
- end)
- end)
- describe('float()', function()
- itp('works', function()
- local d = dict({ test = 10 })
- alloc_log:clear()
- eq({ test = 10 }, dct2tbl(d))
- eq(OK, lib.tv_dict_add_float(d, 'testt', 3, 1.5))
- local dis = dict_items(d)
- alloc_log:check({ a.di(dis.tes, 'tes') })
- eq({ test = 10, tes = 1.5 }, dct2tbl(d))
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_float(d, 'testt', 3, 1.5)
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_float(d, 'testt', 3, 1.5)
- end, nil)
- )
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({})
- end)
- end)
- describe('str()', function()
- itp('works', function()
- local d = dict({ test = 10 })
- alloc_log:clear()
- eq({ test = 10 }, dct2tbl(d))
- eq(OK, lib.tv_dict_add_str(d, 'testt', 3, 'TEST'))
- local dis = dict_items(d)
- alloc_log:check({
- a.str(dis.tes.di_tv.vval.v_string, 'TEST'),
- a.di(dis.tes, 'tes'),
- })
- eq({ test = 10, tes = 'TEST' }, dct2tbl(d))
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_str(d, 'testt', 3, 'TEST')
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_str(d, 'testt', 3, 'TEST')
- end, nil)
- )
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({})
- end)
- end)
- describe('allocated_str()', function()
- itp('works', function()
- local d = dict({ test = 10 })
- eq({ test = 10 }, dct2tbl(d))
- alloc_log:clear()
- local s1 = lib.xstrdup('TEST')
- local s2 = lib.xstrdup('TEST')
- local s3 = lib.xstrdup('TEST')
- alloc_log:check({
- a.str(s1, 'TEST'),
- a.str(s2, 'TEST'),
- a.str(s3, 'TEST'),
- })
- eq(OK, lib.tv_dict_add_allocated_str(d, 'testt', 3, s1))
- local dis = dict_items(d)
- alloc_log:check({
- a.di(dis.tes, 'tes'),
- })
- eq({ test = 10, tes = 'TEST' }, dct2tbl(d))
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_allocated_str(d, 'testt', 3, s2)
- end, 'E685: Internal error: hash_add(): duplicate key "tes"')
- )
- alloc_log:clear()
- lib.emsg_skip = lib.emsg_skip + 1
- eq(
- FAIL,
- check_emsg(function()
- return lib.tv_dict_add_allocated_str(d, 'testt', 3, s3)
- end, nil)
- )
- lib.emsg_skip = lib.emsg_skip - 1
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.freed(s3),
- })
- end)
- end)
- end)
- describe('clear()', function()
- itp('works', function()
- local d = dict()
- alloc_log:check({ a.dict(d) })
- eq({}, dct2tbl(d))
- lib.tv_dict_clear(d)
- eq({}, dct2tbl(d))
- lib.tv_dict_add_str(d, 'TEST', 3, 'tEsT')
- local dis = dict_items(d)
- local di = dis.TES
- local di_s = di.di_tv.vval.v_string
- alloc_log:check({ a.str(di_s), a.di(di) })
- eq({ TES = 'tEsT' }, dct2tbl(d))
- lib.tv_dict_clear(d)
- alloc_log:check({ a.freed(di_s), a.freed(di) })
- eq({}, dct2tbl(d))
- end)
- end)
- describe('extend()', function()
- local function tv_dict_extend(d1, d2, action, emsg)
- action = action or 'force'
- check_emsg(function()
- return lib.tv_dict_extend(d1, d2, action)
- end, emsg)
- end
- pending('works (skip due to flakiness)', function()
- local d1 = dict()
- alloc_log:check({ a.dict(d1) })
- eq({}, dct2tbl(d1))
- local d2 = dict()
- alloc_log:check({ a.dict(d2) })
- eq({}, dct2tbl(d2))
- tv_dict_extend(d1, d2, 'error')
- tv_dict_extend(d1, d2, 'keep')
- tv_dict_extend(d1, d2, 'force')
- alloc_log:check({})
- d1 = dict({ a = 'TEST' })
- eq({ a = 'TEST' }, dct2tbl(d1))
- local dis1 = dict_items(d1)
- local a1_s = dis1.a.di_tv.vval.v_string
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.dict(d1),
- a.di(dis1.a),
- a.str(a1_s),
- })
- d2 = dict({ a = 'TSET' })
- eq({ a = 'TSET' }, dct2tbl(d2))
- local dis2 = dict_items(d2)
- local a2_s = dis2.a.di_tv.vval.v_string
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.dict(d2),
- a.di(dis2.a),
- a.str(a2_s),
- })
- tv_dict_extend(d1, d2, 'error', 'E737: Key already exists: a')
- eq({ a = 'TEST' }, dct2tbl(d1))
- eq({ a = 'TSET' }, dct2tbl(d2))
- alloc_log:clear()
- tv_dict_extend(d1, d2, 'keep')
- alloc_log:check({})
- eq({ a = 'TEST' }, dct2tbl(d1))
- eq({ a = 'TSET' }, dct2tbl(d2))
- tv_dict_extend(d1, d2, 'force')
- alloc_log:check({
- a.freed(a1_s),
- a.str(dis1.a.di_tv.vval.v_string),
- })
- eq({ a = 'TSET' }, dct2tbl(d1))
- eq({ a = 'TSET' }, dct2tbl(d2))
- end)
- pending('disallows overriding builtin or user functions: here be the dragons', function()
- -- pending: see TODO below
- local d = dict()
- d.dv_scope = lib.VAR_DEF_SCOPE
- local f_lua = {
- [type_key] = func_type,
- value = 'tr',
- }
- local f_tv = lua2typvalt(f_lua)
- local p_lua = {
- [type_key] = func_type,
- value = 'tr',
- args = { 1 },
- }
- local p_tv = lua2typvalt(p_lua)
- eq(lib.VAR_PARTIAL, p_tv.v_type)
- local d2 = dict({ tr = f_tv })
- local d3 = dict({ tr = p_tv })
- local d4 = dict({ ['TEST:THIS'] = p_tv })
- local d5 = dict({ Test = f_tv })
- local d6 = dict({ Test = p_tv })
- eval0([[execute("function Test()\nendfunction")]])
- -- TODO: test breaks at this point
- tv_dict_extend(d, d2, 'force', 'E704: Funcref variable name must start with a capital: tr')
- tv_dict_extend(d, d3, 'force', 'E704: Funcref variable name must start with a capital: tr')
- tv_dict_extend(d, d4, 'force', 'E461: Illegal variable name: TEST:THIS')
- tv_dict_extend(d, d5, 'force', 'E705: Variable name conflicts with existing function: Test')
- tv_dict_extend(d, d6, 'force', 'E705: Variable name conflicts with existing function: Test')
- eq({}, dct2tbl(d))
- d.dv_scope = lib.VAR_SCOPE
- tv_dict_extend(d, d4, 'force', 'E461: Illegal variable name: TEST:THIS')
- eq({}, dct2tbl(d))
- tv_dict_extend(d, d2, 'force')
- eq({ tr = f_lua }, dct2tbl(d))
- tv_dict_extend(d, d3, 'force')
- eq({ tr = p_lua }, dct2tbl(d))
- tv_dict_extend(d, d5, 'force')
- eq({ tr = p_lua, Test = f_lua }, dct2tbl(d))
- tv_dict_extend(d, d6, 'force')
- eq({ tr = p_lua, Test = p_lua }, dct2tbl(d))
- end)
- itp('cares about locks and read-only items', function()
- local d_lua = { tv_locked = 1, tv_fixed = 2, di_ro = 3, di_ro_sbx = 4 }
- local d = dict(d_lua)
- local dis = dict_items(d)
- dis.tv_locked.di_tv.v_lock = lib.VAR_LOCKED
- dis.tv_fixed.di_tv.v_lock = lib.VAR_FIXED
- dis.di_ro.di_flags = bit.bor(dis.di_ro.di_flags, lib.DI_FLAGS_RO)
- dis.di_ro_sbx.di_flags = bit.bor(dis.di_ro_sbx.di_flags, lib.DI_FLAGS_RO_SBX)
- lib.sandbox = true
- local d1 = dict({ tv_locked = 41 })
- local d2 = dict({ tv_fixed = 42 })
- local d3 = dict({ di_ro = 43 })
- local d4 = dict({ di_ro_sbx = 44 })
- tv_dict_extend(d, d1, 'force', 'E741: Value is locked: extend() argument')
- tv_dict_extend(d, d2, 'force', 'E742: Cannot change value of extend() argument')
- tv_dict_extend(d, d3, 'force', 'E46: Cannot change read-only variable "extend() argument"')
- tv_dict_extend(
- d,
- d4,
- 'force',
- 'E794: Cannot set variable in the sandbox: "extend() argument"'
- )
- eq(d_lua, dct2tbl(d))
- lib.sandbox = false
- tv_dict_extend(d, d4, 'force')
- d_lua.di_ro_sbx = 44
- eq(d_lua, dct2tbl(d))
- end)
- end)
- describe('equal()', function()
- local function tv_dict_equal(d1, d2, ic)
- return lib.tv_dict_equal(d1, d2, ic or false)
- end
- itp('works', function()
- eq(true, tv_dict_equal(nil, nil))
- local d1 = dict()
- alloc_log:check({ a.dict(d1) })
- eq(1, d1.dv_refcount)
- eq(true, tv_dict_equal(nil, d1))
- eq(true, tv_dict_equal(d1, nil))
- eq(true, tv_dict_equal(d1, d1))
- eq(1, d1.dv_refcount)
- alloc_log:check({})
- local d_upper = dict({ a = 'TEST' })
- local dis_upper = dict_items(d_upper)
- local d_lower = dict({ a = 'test' })
- local dis_lower = dict_items(d_lower)
- local d_kupper_upper = dict({ A = 'TEST' })
- local dis_kupper_upper = dict_items(d_kupper_upper)
- local d_kupper_lower = dict({ A = 'test' })
- local dis_kupper_lower = dict_items(d_kupper_lower)
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.dict(d_upper),
- a.di(dis_upper.a),
- a.str(dis_upper.a.di_tv.vval.v_string),
- a.dict(d_lower),
- a.di(dis_lower.a),
- a.str(dis_lower.a.di_tv.vval.v_string),
- a.dict(d_kupper_upper),
- a.di(dis_kupper_upper.A),
- a.str(dis_kupper_upper.A.di_tv.vval.v_string),
- a.dict(d_kupper_lower),
- a.di(dis_kupper_lower.A),
- a.str(dis_kupper_lower.A.di_tv.vval.v_string),
- })
- eq(true, tv_dict_equal(d_upper, d_upper))
- eq(true, tv_dict_equal(d_upper, d_upper, true))
- eq(false, tv_dict_equal(d_upper, d_lower, false))
- eq(true, tv_dict_equal(d_upper, d_lower, true))
- eq(true, tv_dict_equal(d_kupper_upper, d_kupper_lower, true))
- eq(false, tv_dict_equal(d_kupper_upper, d_lower, true))
- eq(false, tv_dict_equal(d_kupper_upper, d_upper, true))
- alloc_log:check({})
- end)
- end)
- describe('copy()', function()
- local function tv_dict_copy(...)
- return ffi.gc(lib.tv_dict_copy(...), lib.tv_dict_unref)
- end
- itp('copies NULL correctly', function()
- eq(nil, lib.tv_dict_copy(nil, nil, true, 0))
- eq(nil, lib.tv_dict_copy(nil, nil, false, 0))
- eq(nil, lib.tv_dict_copy(nil, nil, true, 1))
- eq(nil, lib.tv_dict_copy(nil, nil, false, 1))
- end)
- itp('copies dict correctly without converting items', function()
- do
- local v = {
- a = { ['«'] = '»' },
- b = { '„' },
- ['1'] = 1,
- ['«»'] = '“',
- ns = null_string,
- nl = null_list,
- nd = null_dict,
- }
- local d_tv = lua2typvalt(v)
- local d = d_tv.vval.v_dict
- local dis = dict_items(d)
- alloc_log:clear()
- eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
- local d_copy1 = tv_dict_copy(nil, d, false, 0)
- eq(2, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(2, dis.b.di_tv.vval.v_list.lv_refcount)
- local dis_copy1 = dict_items(d_copy1)
- eq(dis.a.di_tv.vval.v_dict, dis_copy1.a.di_tv.vval.v_dict)
- eq(dis.b.di_tv.vval.v_list, dis_copy1.b.di_tv.vval.v_list)
- eq(v, dct2tbl(d_copy1))
- alloc_log:clear()
- lib.tv_dict_free(ffi.gc(d_copy1, nil))
- alloc_log:clear()
- eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
- local d_deepcopy1 = tv_dict_copy(nil, d, true, 0)
- neq(nil, d_deepcopy1)
- eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
- local dis_deepcopy1 = dict_items(d_deepcopy1)
- neq(dis.a.di_tv.vval.v_dict, dis_deepcopy1.a.di_tv.vval.v_dict)
- neq(dis.b.di_tv.vval.v_list, dis_deepcopy1.b.di_tv.vval.v_list)
- eq(v, dct2tbl(d_deepcopy1))
- alloc_log:clear()
- end
- collectgarbage()
- end)
- itp('copies dict correctly and converts items', function()
- local vc = vimconv_alloc()
- -- UTF-8 ↔ latin1 conversions need no iconv
- eq(OK, lib.convert_setup(vc, to_cstr('utf-8'), to_cstr('latin1')))
- local v = {
- a = { ['«'] = '»' },
- b = { '„' },
- ['1'] = 1,
- ['«»'] = '“',
- ns = null_string,
- nl = null_list,
- nd = null_dict,
- }
- local d_tv = lua2typvalt(v)
- local d = d_tv.vval.v_dict
- local dis = dict_items(d)
- alloc_log:clear()
- eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
- local d_deepcopy1 = tv_dict_copy(vc, d, true, 0)
- neq(nil, d_deepcopy1)
- eq(1, dis.a.di_tv.vval.v_dict.dv_refcount)
- eq(1, dis.b.di_tv.vval.v_list.lv_refcount)
- local dis_deepcopy1 = dict_items(d_deepcopy1)
- neq(dis.a.di_tv.vval.v_dict, dis_deepcopy1.a.di_tv.vval.v_dict)
- neq(dis.b.di_tv.vval.v_list, dis_deepcopy1.b.di_tv.vval.v_list)
- eq({
- a = { ['\171'] = '\187' },
- b = { '\191' },
- ['1'] = 1,
- ['\171\187'] = '\191',
- ns = null_string,
- nl = null_list,
- nd = null_dict,
- }, dct2tbl(d_deepcopy1))
- alloc_log:clear_tmp_allocs()
- alloc_log:clear()
- end)
- itp('returns different/same containers with(out) copyID', function()
- local d_inner_tv = lua2typvalt({})
- local d_tv = lua2typvalt({ a = d_inner_tv, b = d_inner_tv })
- eq(3, d_inner_tv.vval.v_dict.dv_refcount)
- local d = d_tv.vval.v_dict
- local dis = dict_items(d)
- eq(dis.a.di_tv.vval.v_dict, dis.b.di_tv.vval.v_dict)
- local d_copy1 = tv_dict_copy(nil, d, true, 0)
- local dis_copy1 = dict_items(d_copy1)
- neq(dis_copy1.a.di_tv.vval.v_dict, dis_copy1.b.di_tv.vval.v_dict)
- eq({ a = {}, b = {} }, dct2tbl(d_copy1))
- local d_copy2 = tv_dict_copy(nil, d, true, 2)
- local dis_copy2 = dict_items(d_copy2)
- eq(dis_copy2.a.di_tv.vval.v_dict, dis_copy2.b.di_tv.vval.v_dict)
- eq({ a = {}, b = {} }, dct2tbl(d_copy2))
- eq(3, d_inner_tv.vval.v_dict.dv_refcount)
- end)
- itp('works with self-referencing dict with copyID', function()
- local d_tv = lua2typvalt({})
- local d = d_tv.vval.v_dict
- eq(1, d.dv_refcount)
- lib.tv_dict_add_dict(d, 'test', 4, d)
- eq(2, d.dv_refcount)
- local d_copy1 = tv_dict_copy(nil, d, true, 2)
- eq(2, d_copy1.dv_refcount)
- local v = {}
- v.test = v
- eq(v, dct2tbl(d_copy1))
- lib.tv_dict_clear(d)
- eq(1, d.dv_refcount)
- lib.tv_dict_clear(d_copy1)
- eq(1, d_copy1.dv_refcount)
- end)
- end)
- describe('set_keys_readonly()', function()
- itp('works', function()
- local d = dict({ a = true })
- local dis = dict_items(d)
- alloc_log:check({ a.dict(d), a.di(dis.a) })
- eq(0, bit.band(dis.a.di_flags, lib.DI_FLAGS_RO))
- eq(0, bit.band(dis.a.di_flags, lib.DI_FLAGS_FIX))
- lib.tv_dict_set_keys_readonly(d)
- alloc_log:check({})
- eq(lib.DI_FLAGS_RO, bit.band(dis.a.di_flags, lib.DI_FLAGS_RO))
- eq(lib.DI_FLAGS_FIX, bit.band(dis.a.di_flags, lib.DI_FLAGS_FIX))
- end)
- end)
- end)
- describe('tv', function()
- describe('alloc', function()
- describe('list ret()', function()
- itp('works', function()
- local rettv = typvalt(lib.VAR_UNKNOWN)
- local l = lib.tv_list_alloc_ret(rettv, 0)
- eq(empty_list, typvalt2lua(rettv))
- eq(rettv.vval.v_list, l)
- end)
- end)
- describe('dict ret()', function()
- itp('works', function()
- local rettv = typvalt(lib.VAR_UNKNOWN)
- lib.tv_dict_alloc_ret(rettv)
- eq({}, typvalt2lua(rettv))
- end)
- end)
- end)
- local function defalloc()
- return {}
- end
- describe('clear()', function()
- itp('works', function()
- local function deffrees(alloc_rets)
- local ret = {}
- for i = #alloc_rets, 1, -1 do
- ret[#alloc_rets - i + 1] = alloc_rets:freed(i)
- end
- return ret
- end
- alloc_log:check({})
- lib.tv_clear(nil)
- alloc_log:check({})
- local ll = {}
- local ll_l = nil
- ll[1] = ll
- local dd = {}
- local dd_d = nil
- dd.dd = dd
- for _, v in ipairs({
- { nil_value },
- {
- null_string,
- nil,
- function()
- return { a.freed(alloc_log.null) }
- end,
- },
- { 0 },
- { int(0) },
- { true },
- { false },
- {
- 'true',
- function(tv)
- return { a.str(tv.vval.v_string) }
- end,
- },
- {
- {},
- function(tv)
- return { a.dict(tv.vval.v_dict) }
- end,
- },
- {
- empty_list,
- function(tv)
- return { a.list(tv.vval.v_list) }
- end,
- },
- {
- ll,
- function(tv)
- ll_l = tv.vval.v_list
- return { a.list(tv.vval.v_list), a.li(tv.vval.v_list.lv_first) }
- end,
- defalloc,
- },
- {
- dd,
- function(tv)
- dd_d = tv.vval.v_dict
- return { a.dict(tv.vval.v_dict), a.di(first_di(tv.vval.v_dict)) }
- end,
- defalloc,
- },
- }) do
- local tv = lua2typvalt(v[1])
- local alloc_rets = {}
- alloc_log:check(get_alloc_rets((v[2] or defalloc)(tv), alloc_rets))
- lib.tv_clear(tv)
- alloc_log:check((v[3] or deffrees)(alloc_rets))
- end
- eq(1, ll_l.lv_refcount)
- eq(1, dd_d.dv_refcount)
- end)
- end)
- describe('copy()', function()
- itp('works', function()
- local function strallocs(tv)
- return { a.str(tv.vval.v_string) }
- end
- for _, v in ipairs({
- { nil_value },
- { null_string },
- { 0 },
- { int(0) },
- { true },
- { false },
- {
- {},
- function(tv)
- return { a.dict(tv.vval.v_dict) }
- end,
- nil,
- function(from, to)
- eq(2, to.vval.v_dict.dv_refcount)
- eq(to.vval.v_dict, from.vval.v_dict)
- end,
- },
- {
- empty_list,
- function(tv)
- return { a.list(tv.vval.v_list) }
- end,
- nil,
- function(from, to)
- eq(2, to.vval.v_list.lv_refcount)
- eq(to.vval.v_list, from.vval.v_list)
- end,
- },
- {
- 'test',
- strallocs,
- strallocs,
- function(from, to)
- neq(to.vval.v_string, from.vval.v_string)
- end,
- },
- }) do
- local from = lua2typvalt(v[1])
- alloc_log:check((v[2] or defalloc)(from))
- local to = typvalt(lib.VAR_UNKNOWN)
- lib.tv_copy(from, to)
- local res = v[1]
- eq(res, typvalt2lua(to))
- alloc_log:check((v[3] or defalloc)(to))
- if v[4] then
- v[4](from, to)
- end
- end
- end)
- end)
- describe('item_lock()', function()
- itp('does not alter VAR_PARTIAL', function()
- local p_tv = lua2typvalt({
- [type_key] = func_type,
- value = 'tr',
- dict = {},
- })
- lib.tv_item_lock(p_tv, -1, true, false)
- eq(lib.VAR_UNLOCKED, p_tv.vval.v_partial.pt_dict.dv_lock)
- end)
- itp('does not change VAR_FIXED values', function()
- local d_tv = lua2typvalt({})
- local l_tv = lua2typvalt(empty_list)
- alloc_log:clear()
- d_tv.v_lock = lib.VAR_FIXED
- d_tv.vval.v_dict.dv_lock = lib.VAR_FIXED
- l_tv.v_lock = lib.VAR_FIXED
- l_tv.vval.v_list.lv_lock = lib.VAR_FIXED
- lib.tv_item_lock(d_tv, 1, true, false)
- lib.tv_item_lock(l_tv, 1, true, false)
- eq(lib.VAR_FIXED, d_tv.v_lock)
- eq(lib.VAR_FIXED, l_tv.v_lock)
- eq(lib.VAR_FIXED, d_tv.vval.v_dict.dv_lock)
- eq(lib.VAR_FIXED, l_tv.vval.v_list.lv_lock)
- lib.tv_item_lock(d_tv, 1, false, false)
- lib.tv_item_lock(l_tv, 1, false, false)
- eq(lib.VAR_FIXED, d_tv.v_lock)
- eq(lib.VAR_FIXED, l_tv.v_lock)
- eq(lib.VAR_FIXED, d_tv.vval.v_dict.dv_lock)
- eq(lib.VAR_FIXED, l_tv.vval.v_list.lv_lock)
- alloc_log:check({})
- end)
- itp('works with NULL values', function()
- local l_tv = lua2typvalt(null_list)
- local d_tv = lua2typvalt(null_dict)
- local s_tv = lua2typvalt(null_string)
- alloc_log:clear()
- lib.tv_item_lock(l_tv, 1, true, false)
- lib.tv_item_lock(d_tv, 1, true, false)
- lib.tv_item_lock(s_tv, 1, true, false)
- eq(null_list, typvalt2lua(l_tv))
- eq(null_dict, typvalt2lua(d_tv))
- eq(null_string, typvalt2lua(s_tv))
- eq(lib.VAR_LOCKED, d_tv.v_lock)
- eq(lib.VAR_LOCKED, l_tv.v_lock)
- eq(lib.VAR_LOCKED, s_tv.v_lock)
- alloc_log:check({})
- end)
- end)
- describe('islocked()', function()
- itp('works with NULL values', function()
- local l_tv = lua2typvalt(null_list)
- local d_tv = lua2typvalt(null_dict)
- eq(false, lib.tv_islocked(l_tv))
- eq(false, lib.tv_islocked(d_tv))
- end)
- itp('works', function()
- local tv = lua2typvalt()
- local d_tv = lua2typvalt({})
- local l_tv = lua2typvalt(empty_list)
- alloc_log:clear()
- eq(false, lib.tv_islocked(tv))
- eq(false, lib.tv_islocked(l_tv))
- eq(false, lib.tv_islocked(d_tv))
- d_tv.vval.v_dict.dv_lock = lib.VAR_LOCKED
- l_tv.vval.v_list.lv_lock = lib.VAR_LOCKED
- eq(true, lib.tv_islocked(l_tv))
- eq(true, lib.tv_islocked(d_tv))
- tv.v_lock = lib.VAR_LOCKED
- d_tv.v_lock = lib.VAR_LOCKED
- l_tv.v_lock = lib.VAR_LOCKED
- eq(true, lib.tv_islocked(tv))
- eq(true, lib.tv_islocked(l_tv))
- eq(true, lib.tv_islocked(d_tv))
- d_tv.vval.v_dict.dv_lock = lib.VAR_UNLOCKED
- l_tv.vval.v_list.lv_lock = lib.VAR_UNLOCKED
- eq(true, lib.tv_islocked(tv))
- eq(true, lib.tv_islocked(l_tv))
- eq(true, lib.tv_islocked(d_tv))
- tv.v_lock = lib.VAR_FIXED
- d_tv.v_lock = lib.VAR_FIXED
- l_tv.v_lock = lib.VAR_FIXED
- eq(false, lib.tv_islocked(tv))
- eq(false, lib.tv_islocked(l_tv))
- eq(false, lib.tv_islocked(d_tv))
- d_tv.vval.v_dict.dv_lock = lib.VAR_LOCKED
- l_tv.vval.v_list.lv_lock = lib.VAR_LOCKED
- eq(true, lib.tv_islocked(l_tv))
- eq(true, lib.tv_islocked(d_tv))
- d_tv.vval.v_dict.dv_lock = lib.VAR_FIXED
- l_tv.vval.v_list.lv_lock = lib.VAR_FIXED
- eq(false, lib.tv_islocked(l_tv))
- eq(false, lib.tv_islocked(d_tv))
- alloc_log:check({})
- end)
- end)
- describe('check_lock()', function()
- local function tv_check_lock(lock, name, name_len, emsg)
- return check_emsg(function()
- return lib.value_check_lock(lock, name, name_len)
- end, emsg)
- end
- itp('works', function()
- eq(false, tv_check_lock(lib.VAR_UNLOCKED, 'test', 3))
- eq(true, tv_check_lock(lib.VAR_LOCKED, 'test', 3, 'E741: Value is locked: tes'))
- eq(true, tv_check_lock(lib.VAR_FIXED, 'test', 3, 'E742: Cannot change value of tes'))
- eq(true, tv_check_lock(lib.VAR_LOCKED, nil, 0, 'E741: Value is locked: Unknown'))
- eq(true, tv_check_lock(lib.VAR_FIXED, nil, 0, 'E742: Cannot change value of Unknown'))
- eq(
- true,
- tv_check_lock(lib.VAR_LOCKED, nil, lib.kTVCstring, 'E741: Value is locked: Unknown')
- )
- eq(
- true,
- tv_check_lock(lib.VAR_FIXED, 'test', lib.kTVCstring, 'E742: Cannot change value of test')
- )
- end)
- end)
- describe('equal()', function()
- itp('compares empty and NULL lists correctly', function()
- local l = lua2typvalt(empty_list)
- local l2 = lua2typvalt(empty_list)
- local nl = lua2typvalt(null_list)
- -- NULL lists are equal to empty lists
- eq(true, lib.tv_equal(l, nl, true))
- eq(true, lib.tv_equal(nl, l, false))
- -- NULL lists are equal themselves
- eq(true, lib.tv_equal(nl, nl, true))
- eq(true, lib.tv_equal(nl, nl, false))
- -- As well as empty lists
- eq(true, lib.tv_equal(l, l, true))
- eq(true, lib.tv_equal(l, l2, false))
- eq(true, lib.tv_equal(l2, l, false))
- eq(true, lib.tv_equal(l2, l2, true))
- end)
- itp('compares lists correctly when case is not ignored', function()
- local l1 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
- local l2 = lua2typvalt({ 'abc', { 1, 2, 'Abc' } })
- local l3 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'Def' })
- local l4 = lua2typvalt({ 'abc', { 1, 2, 'Abc', 4 }, 'def' })
- local l5 = lua2typvalt({ 'Abc', { 1, 2, 'Abc' }, 'def' })
- local l6 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
- local l7 = lua2typvalt({ 'abc', { 1, 2, 'abc' }, 'def' })
- local l8 = lua2typvalt({ 'abc', nil, 'def' })
- local l9 = lua2typvalt({ 'abc', { 1, 2, nil }, 'def' })
- eq(true, lib.tv_equal(l1, l1, false))
- eq(false, lib.tv_equal(l1, l2, false))
- eq(false, lib.tv_equal(l1, l3, false))
- eq(false, lib.tv_equal(l1, l4, false))
- eq(false, lib.tv_equal(l1, l5, false))
- eq(true, lib.tv_equal(l1, l6, false))
- eq(false, lib.tv_equal(l1, l7, false))
- eq(false, lib.tv_equal(l1, l8, false))
- eq(false, lib.tv_equal(l1, l9, false))
- end)
- itp('compares lists correctly when case is ignored', function()
- local l1 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
- local l2 = lua2typvalt({ 'abc', { 1, 2, 'Abc' } })
- local l3 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'Def' })
- local l4 = lua2typvalt({ 'abc', { 1, 2, 'Abc', 4 }, 'def' })
- local l5 = lua2typvalt({ 'Abc', { 1, 2, 'Abc' }, 'def' })
- local l6 = lua2typvalt({ 'abc', { 1, 2, 'Abc' }, 'def' })
- local l7 = lua2typvalt({ 'abc', { 1, 2, 'abc' }, 'def' })
- local l8 = lua2typvalt({ 'abc', nil, 'def' })
- local l9 = lua2typvalt({ 'abc', { 1, 2, nil }, 'def' })
- eq(true, lib.tv_equal(l1, l1, true))
- eq(false, lib.tv_equal(l1, l2, true))
- eq(true, lib.tv_equal(l1, l3, true))
- eq(false, lib.tv_equal(l1, l4, true))
- eq(true, lib.tv_equal(l1, l5, true))
- eq(true, lib.tv_equal(l1, l6, true))
- eq(true, lib.tv_equal(l1, l7, true))
- eq(false, lib.tv_equal(l1, l8, true))
- eq(false, lib.tv_equal(l1, l9, true))
- end)
- local function tv_equal(d1, d2, ic)
- return lib.tv_equal(d1, d2, ic or false)
- end
- itp('works with dictionaries', function()
- local nd = lua2typvalt(null_dict)
- eq(true, tv_equal(nd, nd))
- alloc_log:check({})
- local d1 = lua2typvalt({})
- alloc_log:check({ a.dict(d1.vval.v_dict) })
- eq(1, d1.vval.v_dict.dv_refcount)
- eq(true, tv_equal(nd, d1))
- eq(true, tv_equal(d1, nd))
- eq(true, tv_equal(d1, d1))
- eq(1, d1.vval.v_dict.dv_refcount)
- alloc_log:check({})
- local d_upper = lua2typvalt({ a = 'TEST' })
- local dis_upper = dict_items(d_upper.vval.v_dict)
- local d_lower = lua2typvalt({ a = 'test' })
- local dis_lower = dict_items(d_lower.vval.v_dict)
- local d_kupper_upper = lua2typvalt({ A = 'TEST' })
- local dis_kupper_upper = dict_items(d_kupper_upper.vval.v_dict)
- local d_kupper_lower = lua2typvalt({ A = 'test' })
- local dis_kupper_lower = dict_items(d_kupper_lower.vval.v_dict)
- alloc_log:clear_tmp_allocs()
- alloc_log:check({
- a.dict(d_upper.vval.v_dict),
- a.di(dis_upper.a),
- a.str(dis_upper.a.di_tv.vval.v_string),
- a.dict(d_lower.vval.v_dict),
- a.di(dis_lower.a),
- a.str(dis_lower.a.di_tv.vval.v_string),
- a.dict(d_kupper_upper.vval.v_dict),
- a.di(dis_kupper_upper.A),
- a.str(dis_kupper_upper.A.di_tv.vval.v_string),
- a.dict(d_kupper_lower.vval.v_dict),
- a.di(dis_kupper_lower.A),
- a.str(dis_kupper_lower.A.di_tv.vval.v_string),
- })
- eq(true, tv_equal(d_upper, d_upper))
- eq(true, tv_equal(d_upper, d_upper, true))
- eq(false, tv_equal(d_upper, d_lower, false))
- eq(true, tv_equal(d_upper, d_lower, true))
- eq(true, tv_equal(d_kupper_upper, d_kupper_lower, true))
- eq(false, tv_equal(d_kupper_upper, d_lower, true))
- eq(false, tv_equal(d_kupper_upper, d_upper, true))
- alloc_log:check({})
- end)
- end)
- describe('check', function()
- describe('str_or_nr()', function()
- itp('works', function()
- local tv = typvalt()
- local mem = lib.xmalloc(1)
- tv.vval.v_list = mem -- Should crash when actually accessed
- alloc_log:clear()
- for _, v in ipairs({
- { lib.VAR_NUMBER, nil },
- { lib.VAR_FLOAT, 'E805: Expected a Number or a String, Float found' },
- { lib.VAR_PARTIAL, 'E703: Expected a Number or a String, Funcref found' },
- { lib.VAR_FUNC, 'E703: Expected a Number or a String, Funcref found' },
- { lib.VAR_LIST, 'E745: Expected a Number or a String, List found' },
- { lib.VAR_DICT, 'E728: Expected a Number or a String, Dictionary found' },
- { lib.VAR_SPECIAL, 'E5300: Expected a Number or a String' },
- { lib.VAR_UNKNOWN, 'E685: Internal error: tv_check_str_or_nr(UNKNOWN)' },
- }) do
- local typ = v[1]
- local emsg = v[2]
- local ret = true
- if emsg then
- ret = false
- end
- tv.v_type = typ
- eq(
- ret,
- check_emsg(function()
- return lib.tv_check_str_or_nr(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- describe('num()', function()
- itp('works', function()
- local tv = typvalt()
- local mem = lib.xmalloc(1)
- tv.vval.v_list = mem -- Should crash when actually accessed
- alloc_log:clear()
- for _, v in ipairs({
- { lib.VAR_NUMBER, nil },
- { lib.VAR_FLOAT, 'E805: Using a Float as a Number' },
- { lib.VAR_PARTIAL, 'E703: Using a Funcref as a Number' },
- { lib.VAR_FUNC, 'E703: Using a Funcref as a Number' },
- { lib.VAR_LIST, 'E745: Using a List as a Number' },
- { lib.VAR_DICT, 'E728: Using a Dictionary as a Number' },
- { lib.VAR_SPECIAL, nil },
- { lib.VAR_UNKNOWN, 'E685: using an invalid value as a Number' },
- }) do
- local typ = v[1]
- local emsg = v[2]
- local ret = true
- if emsg then
- ret = false
- end
- tv.v_type = typ
- eq(
- ret,
- check_emsg(function()
- return lib.tv_check_num(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- describe('str()', function()
- itp('works', function()
- local tv = typvalt()
- local mem = lib.xmalloc(1)
- tv.vval.v_list = mem -- Should crash when actually accessed
- alloc_log:clear()
- for _, v in ipairs({
- { lib.VAR_NUMBER, nil },
- { lib.VAR_FLOAT, nil },
- { lib.VAR_PARTIAL, 'E729: Using a Funcref as a String' },
- { lib.VAR_FUNC, 'E729: Using a Funcref as a String' },
- { lib.VAR_LIST, 'E730: Using a List as a String' },
- { lib.VAR_DICT, 'E731: Using a Dictionary as a String' },
- { lib.VAR_BOOL, nil },
- { lib.VAR_SPECIAL, nil },
- { lib.VAR_UNKNOWN, 'E908: Using an invalid value as a String' },
- }) do
- local typ = v[1]
- local emsg = v[2]
- local ret = true
- if emsg then
- ret = false
- end
- tv.v_type = typ
- eq(
- ret,
- check_emsg(function()
- return lib.tv_check_str(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- end)
- describe('get', function()
- describe('number()', function()
- itp('works', function()
- for _, v in ipairs({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
- { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', 0 },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', 0 },
- { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', 0 },
- { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', 0 },
- { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', 0 },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
- { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', 0 },
- }) do
- -- Using to_cstr, cannot free with tv_clear
- local tv = ffi.gc(typvalt(v[1], v[2]), nil)
- alloc_log:check({})
- local emsg = v[3]
- local ret = v[4]
- eq(
- ret,
- check_emsg(function()
- return lib.tv_get_number(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- describe('number_chk()', function()
- itp('works', function()
- for _, v in ipairs({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
- { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', 0 },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', 0 },
- { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', 0 },
- { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', 0 },
- { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', 0 },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
- { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', 0 },
- }) do
- -- Using to_cstr, cannot free with tv_clear
- local tv = ffi.gc(typvalt(v[1], v[2]), nil)
- alloc_log:check({})
- local emsg = v[3]
- local ret = { v[4], not not emsg }
- eq(
- ret,
- check_emsg(function()
- local err = ffi.new('bool[1]', { false })
- local res = lib.tv_get_number_chk(tv, err)
- return { res, err[0] }
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- describe('lnum()', function()
- pending('works (skip due to flakiness)', function()
- for _, v in ipairs({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, 100500 },
- { lib.VAR_STRING, { v_string = to_cstr('.') }, nil, 46 },
- { lib.VAR_FLOAT, { v_float = 42.53 }, 'E805: Using a Float as a Number', -1 },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E703: Using a Funcref as a Number', -1 },
- { lib.VAR_FUNC, { v_string = NULL }, 'E703: Using a Funcref as a Number', -1 },
- { lib.VAR_LIST, { v_list = NULL }, 'E745: Using a List as a Number', -1 },
- { lib.VAR_DICT, { v_dict = NULL }, 'E728: Using a Dictionary as a Number', -1 },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 0 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 1 },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 0 },
- { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_number(UNKNOWN)', -1 },
- }) do
- lib.curwin.w_cursor.lnum = 46
- -- Using to_cstr, cannot free with tv_clear
- local tv = ffi.gc(typvalt(v[1], v[2]), nil)
- alloc_log:check({})
- local emsg = v[3]
- local ret = v[4]
- eq(
- ret,
- check_emsg(function()
- return lib.tv_get_lnum(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- describe('float()', function()
- itp('works', function()
- for _, v in ipairs({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, 42 },
- {
- lib.VAR_STRING,
- { v_string = to_cstr('100500') },
- 'E892: Using a String as a Float',
- 0,
- },
- { lib.VAR_FLOAT, { v_float = 42.53 }, nil, 42.53 },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E891: Using a Funcref as a Float', 0 },
- { lib.VAR_FUNC, { v_string = NULL }, 'E891: Using a Funcref as a Float', 0 },
- { lib.VAR_LIST, { v_list = NULL }, 'E893: Using a List as a Float', 0 },
- { lib.VAR_DICT, { v_dict = NULL }, 'E894: Using a Dictionary as a Float', 0 },
- {
- lib.VAR_SPECIAL,
- { v_special = lib.kSpecialVarNull },
- 'E907: Using a special value as a Float',
- 0,
- },
- {
- lib.VAR_BOOL,
- { v_bool = lib.kBoolVarTrue },
- 'E362: Using a boolean value as a Float',
- 0,
- },
- {
- lib.VAR_BOOL,
- { v_bool = lib.kBoolVarFalse },
- 'E362: Using a boolean value as a Float',
- 0,
- },
- { lib.VAR_UNKNOWN, nil, 'E685: Internal error: tv_get_float(UNKNOWN)', 0 },
- }) do
- -- Using to_cstr, cannot free with tv_clear
- local tv = ffi.gc(typvalt(v[1], v[2]), nil)
- alloc_log:check({})
- local emsg = v[3]
- local ret = v[4]
- eq(
- ret,
- check_emsg(function()
- return lib.tv_get_float(tv)
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- else
- alloc_log:check({})
- end
- end
- end)
- end)
- local function test_string_fn(input, fn)
- for _, v in ipairs(input) do
- -- Using to_cstr in place of Neovim allocated string, cannot
- -- tv_clear() that.
- local tv = ffi.gc(typvalt(v[1], v[2]), nil)
- alloc_log:check({})
- local emsg = v[3]
- local ret = v[4]
- eq(
- ret,
- check_emsg(function()
- local res, buf = fn(tv)
- if
- tv.v_type == lib.VAR_NUMBER
- or tv.v_type == lib.VAR_FLOAT
- or tv.v_type == lib.VAR_SPECIAL
- or tv.v_type == lib.VAR_BOOL
- then
- eq(buf, res)
- else
- neq(buf, res)
- end
- if res ~= nil then
- return ffi.string(res)
- else
- return nil
- end
- end, emsg)
- )
- if emsg then
- alloc_log:clear()
- elseif tv.v_type == lib.VAR_FLOAT then
- alloc_log:check({ a.freed(alloc_log.null), a.freed(alloc_log.null) })
- else
- alloc_log:check({})
- end
- end
- end
- describe('string()', function()
- pending('works (skip due to flakiness)', function()
- local buf = lib.tv_get_string(lua2typvalt(int(1)))
- local buf_chk = lib.tv_get_string_chk(lua2typvalt(int(1)))
- neq(buf, buf_chk)
- test_string_fn({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
- { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', '' },
- { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', '' },
- { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', '' },
- { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', '' },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
- { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', '' },
- }, function(tv)
- return lib.tv_get_string(tv), buf
- end)
- end)
- end)
- describe('string_chk()', function()
- itp('works', function()
- local buf = lib.tv_get_string_chk(lua2typvalt(int(1)))
- test_string_fn({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
- { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', nil },
- { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', nil },
- { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', nil },
- { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', nil },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
- { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil },
- }, function(tv)
- return lib.tv_get_string_chk(tv), buf
- end)
- end)
- end)
- describe('string_buf()', function()
- itp('works', function()
- test_string_fn({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
- { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', '' },
- { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', '' },
- { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', '' },
- { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', '' },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
- { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', '' },
- }, function(tv)
- local buf = ffi.new('char[?]', lib.NUMBUFLEN, { 0 })
- return lib.tv_get_string_buf(tv, buf), buf
- end)
- end)
- end)
- describe('string_buf_chk()', function()
- itp('works', function()
- test_string_fn({
- { lib.VAR_NUMBER, { v_number = 42 }, nil, '42' },
- { lib.VAR_STRING, { v_string = to_cstr('100500') }, nil, '100500' },
- { lib.VAR_FLOAT, { v_float = 42.53 }, nil, '42.53' },
- { lib.VAR_PARTIAL, { v_partial = NULL }, 'E729: Using a Funcref as a String', nil },
- { lib.VAR_FUNC, { v_string = NULL }, 'E729: Using a Funcref as a String', nil },
- { lib.VAR_LIST, { v_list = NULL }, 'E730: Using a List as a String', nil },
- { lib.VAR_DICT, { v_dict = NULL }, 'E731: Using a Dictionary as a String', nil },
- { lib.VAR_SPECIAL, { v_special = lib.kSpecialVarNull }, nil, 'v:null' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarTrue }, nil, 'v:true' },
- { lib.VAR_BOOL, { v_bool = lib.kBoolVarFalse }, nil, 'v:false' },
- { lib.VAR_UNKNOWN, nil, 'E908: Using an invalid value as a String', nil },
- }, function(tv)
- local buf = ffi.new('char[?]', lib.NUMBUFLEN, { 0 })
- return lib.tv_get_string_buf_chk(tv, buf), buf
- end)
- end)
- end)
- end)
- end)
- end)
|