robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test suite for vim.list 2local t =require ( 'test.testutil' ) 3local describe , it =t . describe , t . it 4local eq =t . eq 5 6describe ( 'vim.list' , function () 7it ( 'vim.list.unique()' , function () 8eq ( { 1 , 2 , 3 , 4 , 5 } , vim . list . unique ( { 1 , 2 , 2 , 3 , 4 , 4 , 5 } )) 9eq ( { 1 , 2 , 3 , 4 , 5 } , vim . list . unique ( { 1 , 2 , 3 , 4 , 4 , 5 , 1 , 2 , 3 , 2 , 1 , 2 , 3 , 4 , 5 } )) 10eq ( { 1 , 2 , 3 , 4 , 5 , field =1 } , vim . list . unique ( { 1 , 2 , 2 , 3 , 4 , 4 , 5 , field =1 } )) 11 12-- Not properly defined, but test anyway 13-- luajit evaluates #t as 7, whereas Lua 5.1 evaluates it as 12 14local r =vim . list . unique ( { 1 , 2 , 2 , 3 , 4 , 4 , 5 , nil , 6 , 6 , 7 , 7 } ) 15if jit then 16eq ( { 1 , 2 , 3 , 4 , 5 , nil , nil , nil , 6 , 6 , 7 , 7 } , r ) 17else 18eq ( { 1 , 2 , 3 , 4 , 5 , nil , 6 , 7 } , r ) 19end 20 21eq ( 22{ { 1 } , { 2 } , { 3 } } , 23vim . list . unique ( { { 1 } , { 1 } , { 2 } , { 2 } , { 3 } , { 3 } } , function ( x ) 24return x [ 1 ] 25end ) 26) 27 28eq ( { { id =1 } , { id =2 } } , vim . list . unique ( { { id =1 } , { id =2 } , { id =1 } } , 'id' )) 29end ) 30 31--- Generate a list like { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ...}. 32---@param num integer 33local function gen_list ( num ) 34---@type integer[] 35local list ={} 36for i =1 , num do 37for _ =1 , i do 38list [ # list + 1 ] =i 39end 40end 41return list 42end 43 44--- Index of the last {num}. 45--- Mathematically, a triangular number. 46---@param num integer 47local function index ( num ) 48return math . floor (( math . pow ( num , 2 ) + num ) / 2 ) 49end 50 51it ( "vim.list.bisect(..., { bound = 'lower' })" , function () 52local num =math . random ( 100 ) 53local list =gen_list ( num ) 54 55local target =math . random ( num ) 56eq ( vim . list . bisect ( list , target , { bound ='lower' } ), index ( target - 1 ) + 1 ) 57eq ( vim . list . bisect ( list , num + 1 , { bound ='lower' } ), index ( num ) + 1 ) 58 59local dict_list ={ { value =1 } , { value =2 } , { value =2 } , { value =3 } } 60eq ( 2 , vim . list . bisect ( dict_list , { value =2 } , { key ='value' , bound ='lower' } )) 61end ) 62 63it ( "vim.list.bisect(..., bound = { 'upper' })" , function () 64local num =math . random ( 100 ) 65local list =gen_list ( num ) 66 67local target =math . random ( num ) 68eq ( vim . list . bisect ( list , target , { bound ='upper' } ), index ( target ) + 1 ) 69eq ( vim . list . bisect ( list , num + 1 , { bound ='upper' } ), index ( num ) + 1 ) 70 71local dict_list ={ { value =1 } , { value =2 } , { value =2 } , { value =3 } } 72eq ( 4 , vim . list . bisect ( dict_list , { value =2 } , { key ='value' , bound ='upper' } )) 73end ) 74end )