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