robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4 5local cimport =t . cimport 6local ffi =t . ffi 7local eq =t . eq 8local neq =t . neq 9 10local prof =cimport ( './src/nvim/profile.h' ) 11 12local function split ( inputstr , sep ) 13if sep == nil then 14sep ='%s' 15end 16 17local q , i ={} , 1 18for str in string . gmatch ( inputstr , '([^' .. sep .. ']+)' ) do 19q [ i ] =str 20i =i + 1 21end 22 23return q 24end 25 26local function trim ( s ) 27local from =s : match '^%s*()' 28return from > # s and '' or s : match ( '.*%S' , from ) 29end 30 31local function starts ( str , start ) 32return string . sub ( str , 1 , string . len ( start )) == start 33end 34 35local function cmp_assert ( v1 , v2 , op , opstr ) 36local res =op ( v1 , v2 ) 37if res == false then 38( string . format ( 'expected: %f %s %f' , v1 , opstr , v2 )) 39end 40t . ok ( res ) 41end 42 43local function lt ( a , b ) -- luacheck: ignore 44cmp_assert ( a , b , function ( x , y ) 45return x < y 46end , '<' ) 47end 48 49local function lte ( a , b ) -- luacheck: ignore 50cmp_assert ( a , b , function ( x , y ) 51return x <= y 52end , '<=' ) 53end 54 55local function gt ( a , b ) -- luacheck: ignore 56cmp_assert ( a , b , function ( x , y ) 57return x > y 58end , '>' ) 59end 60 61local function gte ( a , b ) 62cmp_assert ( a , b , function ( x , y ) 63return x >= y 64end , '>=' ) 65end 66 67-- missing functions: 68-- profile_self 69-- profile_get_wait 70-- profile_set_wait 71-- profile_sub_wait 72describe ( 'profiling related functions' , function () 73local function profile_start () 74return prof . profile_start () 75end 76local function profile_end ( q ) 77return prof . profile_end ( q ) 78end 79local function profile_zero () 80return prof . profile_zero () 81end 82local function profile_setlimit ( ms ) 83return prof . profile_setlimit ( ms ) 84end 85local function profile_passed_limit ( q ) 86return prof . profile_passed_limit ( q ) 87end 88local function profile_add ( t1 , t2 ) 89return prof . profile_add ( t1 , t2 ) 90end 91local function profile_sub ( t1 , t2 ) 92return prof . profile_sub ( t1 , t2 ) 93end 94local function profile_divide ( q , cnt ) 95return prof . profile_divide ( q , cnt ) 96end 97local function profile_cmp ( t1 , t2 ) 98return prof . profile_cmp ( t1 , t2 ) 99end 100local function profile_msg ( q ) 101return ffi . string ( prof . profile_msg ( q )) 102end 103 104local function toseconds ( q ) -- luacheck: ignore 105local str =trim ( profile_msg ( q )) 106local spl =split ( str , '.' ) 107local s , us =spl [ 1 ], spl [ 2 ] 108return tonumber ( s ) + tonumber ( us ) / 1000000 109end 110 111-- this is quite difficult to test, as it would rely on other functions in 112-- the profiling package. Those functions in turn will probably be tested 113-- using profile_cmp... circular reasoning. 114describe ( 'profile_cmp' , function () 115itp ( 'can compare subsequent starts' , function () 116local s1 , s2 =profile_start (), profile_start () 117t . ok ( profile_cmp ( s1 , s2 ) > 0 ) 118t . ok ( profile_cmp ( s2 , s1 ) < 0 ) 119end ) 120 121itp ( 'can compare the zero element' , function () 122t . ok ( profile_cmp ( profile_zero (), profile_zero ()) == 0 ) 123end ) 124 125itp ( 'correctly orders divisions' , function () 126local start =profile_start () 127t . ok ( profile_cmp ( start , profile_divide ( start , 10 )) <= 0 ) 128end ) 129end ) 130 131describe ( 'profile_divide' , function () 132itp ( 'actually performs division' , function () 133-- note: the routine actually performs floating-point division to get 134-- better rounding behaviour, we have to take that into account when 135-- checking. (check range, not exact number). 136local divisor =10 137 138local start =profile_start () 139local divided =profile_divide ( start , divisor ) 140 141local res =divided 142for _ =1 , divisor - 1 do 143res =profile_add ( res , divided ) 144end 145 146-- res should be in the range [start - divisor, start + divisor] 147local start_min , start_max =profile_sub ( start , divisor ), profile_add ( start , divisor ) 148t . ok ( profile_cmp ( start_min , res ) >= 0 ) 149t . ok ( profile_cmp ( start_max , res ) <= 0 ) 150end ) 151end ) 152 153describe ( 'profile_zero' , function () 154itp ( 'returns the same value on each call' , function () 155eq ( 0 , profile_zero ()) 156end ) 157end ) 158 159describe ( 'profile_start' , function () 160itp ( 'increases' , function () 161local last =profile_start () 162for _ =1 , 100 do 163local curr =profile_start () 164gte ( curr , last ) 165last =curr 166end 167end ) 168end ) 169 170describe ( 'profile_end' , function () 171itp ( 'the elapsed time cannot be zero' , function () 172neq ( profile_zero (), profile_end ( profile_start ())) 173end ) 174 175itp ( 'outer elapsed >= inner elapsed' , function () 176for _ =1 , 100 do 177local start_outer =profile_start () 178local start_inner =profile_start () 179local elapsed_inner =profile_end ( start_inner ) 180local elapsed_outer =profile_end ( start_outer ) 181 182gte ( elapsed_outer , elapsed_inner ) 183end 184end ) 185end ) 186 187describe ( 'profile_setlimit' , function () 188itp ( 'sets no limit when 0 is passed' , function () 189eq ( profile_setlimit ( 0 ), profile_zero ()) 190end ) 191 192itp ( 'sets a limit in the future otherwise' , function () 193local future =profile_setlimit ( 1000 ) 194local now =profile_start () 195t . ok ( profile_cmp ( future , now ) < 0 ) 196end ) 197end ) 198 199describe ( 'profile_passed_limit' , function () 200itp ( 'start is in the past' , function () 201local start =profile_start () 202eq ( true , profile_passed_limit ( start )) 203end ) 204 205itp ( 'start + start is in the future' , function () 206local start =profile_start () 207local future =profile_add ( start , start ) 208eq ( false , profile_passed_limit ( future )) 209end ) 210end ) 211 212describe ( 'profile_msg' , function () 213itp ( 'prints the zero time as 0.00000' , function () 214local str =trim ( profile_msg ( profile_zero ())) 215eq ( '0.000000' , str ) 216end ) 217 218itp ( 'prints the time passed, in seconds.microsends' , function () 219local start =profile_start () 220local endt =profile_end ( start ) 221local str =trim ( profile_msg ( endt )) 222local spl =split ( str , '.' ) 223 224-- string has two parts (before dot and after dot) 225eq ( 2 , # spl ) 226 227local s , us =spl [ 1 ], spl [ 2 ] 228 229-- zero seconds have passed (if this is not true, either LuaJIT is too 230-- slow or the profiling functions are too slow and need to be fixed) 231eq ( '0' , s ) 232 233-- more or less the same goes for the microsecond part, if it doesn't 234-- start with 0, it's too slow. 235t . ok ( starts ( us , '0' )) 236end ) 237end ) 238 239describe ( 'profile_add' , function () 240itp ( 'adds profiling times' , function () 241local start =profile_start () 242t . eq ( start , profile_add ( profile_zero (), start )) 243end ) 244end ) 245 246describe ( 'profile_sub' , function () 247itp ( 'subtracts profiling times' , function () 248-- subtracting zero does nothing 249local start =profile_start () 250t . eq ( start , profile_sub ( start , profile_zero ())) 251 252local start1 , start2 , start3 =profile_start (), profile_start (), profile_start () 253local cmp =profile_cmp ( profile_sub ( start2 , start1 ), profile_sub ( start3 , start1 )) 254-- t2 >= t1 => profile_cmp(t1, t2) >= 0 255t . ok ( cmp >= 0 ) 256 257cmp =profile_cmp ( profile_sub ( start3 , start1 ), profile_sub ( start2 , start1 )) 258-- t2 <= t1 => profile_cmp(t1, t2) <= 0 259t . ok ( cmp <= 0 ) 260end ) 261end ) 262end )