robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local bit =require ( 'bit' ) 3 4local describe =t . describe 5local itp =t . gen_itp ( t . it ) 6 7local child_call_once =t . child_call_once 8local cimport =t . cimport 9local ffi =t . ffi 10 11local lib =cimport ( './src/nvim/charset.h' ) 12 13local ARGTYPES 14 15child_call_once ( function () 16ARGTYPES ={ 17num =ffi . typeof ( 'varnumber_T[1]' ), 18unum =ffi . typeof ( 'uvarnumber_T[1]' ), 19pre =ffi . typeof ( 'int[1]' ), 20len =ffi . typeof ( 'int[1]' ), 21} 22end ) 23 24local icnt =- 42 25local ucnt =4242 26 27local function arginit ( arg ) 28if arg == 'unum' then 29ucnt =ucnt + 1 30return ARGTYPES [ arg ]( { ucnt } ) 31else 32icnt =icnt - 1 33return ARGTYPES [ arg ]( { icnt } ) 34end 35end 36 37local function argreset ( arg , args ) 38if arg == 'unum' then 39ucnt =ucnt + 1 40args [ arg ][ 0 ] =ucnt 41else 42icnt =icnt - 1 43args [ arg ][ 0 ] =icnt 44end 45end 46 47local function test_vim_str2nr ( s , what , exp , maxlen , strict ) 48if strict == nil then 49strict =true 50end 51local bits ={} 52for k , _ in pairs ( exp ) do 53bits [ # bits + 1 ] =k 54end 55maxlen =maxlen or # s 56local args ={} 57for k , _ in pairs ( ARGTYPES ) do 58args [ k ] =arginit ( k ) 59end 60for case =0 , (( 2 ^ # bits ) - 1 ) do 61local cv ={} 62for b =0 , ( # bits - 1 ) do 63if bit . band ( case , ( 2 ^ b )) == 0 then 64local k =bits [ b + 1 ] 65argreset ( k , args ) 66cv [ k ] =args [ k ] 67end 68end 69lib . vim_str2nr ( s , cv . pre , cv . len , what , cv . num , cv . unum , maxlen , strict , nil ) 70for cck , ccv in pairs ( cv ) do 71if exp [ cck ] ~= tonumber ( ccv [ 0 ]) then 72error ( 73( 'Failed check (%s = %d) in test (s=%s, w=%u, m=%d, strict=%s): %d' ): format ( 74cck , 75exp [ cck ], 76s , 77tonumber ( what ), 78maxlen , 79tostring ( strict ), 80tonumber ( ccv [ 0 ]) 81) 82) 83end 84end 85end 86end 87 88local _itp =itp 89itp =function ( ... ) 90collectgarbage ( 'restart' ) 91_itp ( ... ) 92end 93 94describe ( 'vim_str2nr()' , function () 95itp ( 'works fine when it has nothing to do' , function () 96test_vim_str2nr ( '' , 0 , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 97test_vim_str2nr ( '' , lib . STR2NR_ALL , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 98test_vim_str2nr ( '' , lib . STR2NR_BIN , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 99test_vim_str2nr ( '' , lib . STR2NR_OCT , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 100test_vim_str2nr ( '' , lib . STR2NR_OOCT , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 101test_vim_str2nr ( '' , lib . STR2NR_HEX , { len =0 , num =0 , unum =0 , pre =0 } , 0 ) 102test_vim_str2nr ( 103'' , 104lib . STR2NR_FORCE + lib . STR2NR_DEC , 105{ len =0 , num =0 , unum =0 , pre =0 } , 1060 107) 108test_vim_str2nr ( 109'' , 110lib . STR2NR_FORCE + lib . STR2NR_BIN , 111{ len =0 , num =0 , unum =0 , pre =0 } , 1120 113) 114test_vim_str2nr ( 115'' , 116lib . STR2NR_FORCE + lib . STR2NR_OCT , 117{ len =0 , num =0 , unum =0 , pre =0 } , 1180 119) 120test_vim_str2nr ( 121'' , 122lib . STR2NR_FORCE + lib . STR2NR_OOCT , 123{ len =0 , num =0 , unum =0 , pre =0 } , 1240 125) 126test_vim_str2nr ( 127'' , 128lib . STR2NR_FORCE + lib . STR2NR_OCT + lib . STR2NR_OOCT , 129{ len =0 , num =0 , unum =0 , pre =0 } , 1300 131) 132test_vim_str2nr ( 133'' , 134lib . STR2NR_FORCE + lib . STR2NR_HEX , 135{ len =0 , num =0 , unum =0 , pre =0 } , 1360 137) 138end ) 139itp ( 'works with decimal numbers' , function () 140for _ , flags in ipairs ( { 1410 , 142lib . STR2NR_BIN , 143lib . STR2NR_OCT , 144lib . STR2NR_HEX , 145lib . STR2NR_OOCT , 146lib . STR2NR_BIN + lib . STR2NR_OCT , 147lib . STR2NR_BIN + lib . STR2NR_HEX , 148lib . STR2NR_OCT + lib . STR2NR_HEX , 149lib . STR2NR_OOCT + lib . STR2NR_HEX , 150lib . STR2NR_ALL , 151lib . STR2NR_FORCE + lib . STR2NR_DEC , 152} ) do 153-- Check that all digits are recognized 154test_vim_str2nr ( '12345' , flags , { len =5 , num =12345 , unum =12345 , pre =0 } , 0 ) 155test_vim_str2nr ( '67890' , flags , { len =5 , num =67890 , unum =67890 , pre =0 } , 0 ) 156test_vim_str2nr ( '12345A' , flags , { len =0 } , 0 ) 157test_vim_str2nr ( '67890A' , flags , { len =0 } , 0 ) 158test_vim_str2nr ( '12345A' , flags , { len =5 , num =12345 , unum =12345 , pre =0 } , 0 , false ) 159test_vim_str2nr ( '67890A' , flags , { len =5 , num =67890 , unum =67890 , pre =0 } , 0 , false ) 160 161test_vim_str2nr ( '42' , flags , { len =2 , num =42 , unum =42 , pre =0 } , 0 ) 162test_vim_str2nr ( '42' , flags , { len =1 , num =4 , unum =4 , pre =0 } , 1 ) 163test_vim_str2nr ( '42' , flags , { len =2 , num =42 , unum =42 , pre =0 } , 2 ) 164test_vim_str2nr ( '42' , flags , { len =2 , num =42 , unum =42 , pre =0 } , 3 ) -- includes NUL byte in maxlen 165 166test_vim_str2nr ( '42x' , flags , { len =0 } , 0 ) 167test_vim_str2nr ( '42x' , flags , { len =0 } , 3 ) 168test_vim_str2nr ( '42x' , flags , { len =2 , num =42 , unum =42 , pre =0 } , 0 , false ) 169test_vim_str2nr ( '42x' , flags , { len =2 , num =42 , unum =42 , pre =0 } , 3 , false ) 170 171test_vim_str2nr ( '-42' , flags , { len =3 , num =- 42 , unum =42 , pre =0 } , 3 ) 172test_vim_str2nr ( '-42' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 173 174test_vim_str2nr ( '-42x' , flags , { len =0 } , 0 ) 175test_vim_str2nr ( '-42x' , flags , { len =0 } , 4 ) 176test_vim_str2nr ( '-42x' , flags , { len =3 , num =- 42 , unum =42 , pre =0 } , 0 , false ) 177test_vim_str2nr ( '-42x' , flags , { len =3 , num =- 42 , unum =42 , pre =0 } , 4 , false ) 178end 179end ) 180itp ( 'works with binary numbers' , function () 181for _ , flags in ipairs ( { 182lib . STR2NR_BIN , 183lib . STR2NR_BIN + lib . STR2NR_OCT , 184lib . STR2NR_BIN + lib . STR2NR_HEX , 185lib . STR2NR_ALL , 186lib . STR2NR_FORCE + lib . STR2NR_BIN , 187} ) do 188local bin 189local BIN 190if flags > lib . STR2NR_FORCE then 191bin =0 192BIN =0 193else 194bin =( 'b' ): byte () 195BIN =( 'B' ): byte () 196end 197 198test_vim_str2nr ( '0b101' , flags , { len =5 , num =5 , unum =5 , pre =bin } , 0 ) 199test_vim_str2nr ( '0b101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 200test_vim_str2nr ( '0b101' , flags , { len =0 } , 2 ) 201test_vim_str2nr ( '0b101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 2 , false ) 202test_vim_str2nr ( '0b101' , flags , { len =3 , num =1 , unum =1 , pre =bin } , 3 ) 203test_vim_str2nr ( '0b101' , flags , { len =4 , num =2 , unum =2 , pre =bin } , 4 ) 204test_vim_str2nr ( '0b101' , flags , { len =5 , num =5 , unum =5 , pre =bin } , 5 ) 205test_vim_str2nr ( '0b101' , flags , { len =5 , num =5 , unum =5 , pre =bin } , 6 ) 206 207test_vim_str2nr ( '0b1012' , flags , { len =0 } , 0 ) 208test_vim_str2nr ( '0b1012' , flags , { len =0 } , 6 ) 209test_vim_str2nr ( '0b1012' , flags , { len =5 , num =5 , unum =5 , pre =bin } , 0 , false ) 210test_vim_str2nr ( '0b1012' , flags , { len =5 , num =5 , unum =5 , pre =bin } , 6 , false ) 211 212test_vim_str2nr ( '-0b101' , flags , { len =6 , num =- 5 , unum =5 , pre =bin } , 0 ) 213test_vim_str2nr ( '-0b101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 214test_vim_str2nr ( '-0b101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 215test_vim_str2nr ( '-0b101' , flags , { len =0 } , 3 ) 216test_vim_str2nr ( '-0b101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 3 , false ) 217test_vim_str2nr ( '-0b101' , flags , { len =4 , num =- 1 , unum =1 , pre =bin } , 4 ) 218test_vim_str2nr ( '-0b101' , flags , { len =5 , num =- 2 , unum =2 , pre =bin } , 5 ) 219test_vim_str2nr ( '-0b101' , flags , { len =6 , num =- 5 , unum =5 , pre =bin } , 6 ) 220test_vim_str2nr ( '-0b101' , flags , { len =6 , num =- 5 , unum =5 , pre =bin } , 7 ) 221 222test_vim_str2nr ( '-0b1012' , flags , { len =0 } , 0 ) 223test_vim_str2nr ( '-0b1012' , flags , { len =0 } , 7 ) 224test_vim_str2nr ( '-0b1012' , flags , { len =6 , num =- 5 , unum =5 , pre =bin } , 0 , false ) 225test_vim_str2nr ( '-0b1012' , flags , { len =6 , num =- 5 , unum =5 , pre =bin } , 7 , false ) 226 227test_vim_str2nr ( '0B101' , flags , { len =5 , num =5 , unum =5 , pre =BIN } , 0 ) 228test_vim_str2nr ( '0B101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 229test_vim_str2nr ( '0B101' , flags , { len =0 } , 2 ) 230test_vim_str2nr ( '0B101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 2 , false ) 231test_vim_str2nr ( '0B101' , flags , { len =3 , num =1 , unum =1 , pre =BIN } , 3 ) 232test_vim_str2nr ( '0B101' , flags , { len =4 , num =2 , unum =2 , pre =BIN } , 4 ) 233test_vim_str2nr ( '0B101' , flags , { len =5 , num =5 , unum =5 , pre =BIN } , 5 ) 234test_vim_str2nr ( '0B101' , flags , { len =5 , num =5 , unum =5 , pre =BIN } , 6 ) 235 236test_vim_str2nr ( '0B1012' , flags , { len =0 } , 0 ) 237test_vim_str2nr ( '0B1012' , flags , { len =0 } , 6 ) 238test_vim_str2nr ( '0B1012' , flags , { len =5 , num =5 , unum =5 , pre =BIN } , 0 , false ) 239test_vim_str2nr ( '0B1012' , flags , { len =5 , num =5 , unum =5 , pre =BIN } , 6 , false ) 240 241test_vim_str2nr ( '-0B101' , flags , { len =6 , num =- 5 , unum =5 , pre =BIN } , 0 ) 242test_vim_str2nr ( '-0B101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 243test_vim_str2nr ( '-0B101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 244test_vim_str2nr ( '-0B101' , flags , { len =0 } , 3 ) 245test_vim_str2nr ( '-0B101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 3 , false ) 246test_vim_str2nr ( '-0B101' , flags , { len =4 , num =- 1 , unum =1 , pre =BIN } , 4 ) 247test_vim_str2nr ( '-0B101' , flags , { len =5 , num =- 2 , unum =2 , pre =BIN } , 5 ) 248test_vim_str2nr ( '-0B101' , flags , { len =6 , num =- 5 , unum =5 , pre =BIN } , 6 ) 249test_vim_str2nr ( '-0B101' , flags , { len =6 , num =- 5 , unum =5 , pre =BIN } , 7 ) 250 251test_vim_str2nr ( '-0B1012' , flags , { len =0 } , 0 ) 252test_vim_str2nr ( '-0B1012' , flags , { len =0 } , 7 ) 253test_vim_str2nr ( '-0B1012' , flags , { len =6 , num =- 5 , unum =5 , pre =BIN } , 0 , false ) 254test_vim_str2nr ( '-0B1012' , flags , { len =6 , num =- 5 , unum =5 , pre =BIN } , 7 , false ) 255 256if flags > lib . STR2NR_FORCE then 257test_vim_str2nr ( '-101' , flags , { len =4 , num =- 5 , unum =5 , pre =0 } , 0 ) 258end 259end 260end ) 261itp ( 'works with octal numbers (0 prefix)' , function () 262for _ , flags in ipairs ( { 263lib . STR2NR_OCT , 264lib . STR2NR_OCT + lib . STR2NR_BIN , 265lib . STR2NR_OCT + lib . STR2NR_HEX , 266lib . STR2NR_OCT + lib . STR2NR_OOCT , 267lib . STR2NR_ALL , 268lib . STR2NR_FORCE + lib . STR2NR_OCT , 269lib . STR2NR_FORCE + lib . STR2NR_OOCT , 270lib . STR2NR_FORCE + lib . STR2NR_OCT + lib . STR2NR_OOCT , 271} ) do 272local oct 273if flags > lib . STR2NR_FORCE then 274oct =0 275else 276oct =( '0' ): byte () 277end 278 279-- Check that all digits are recognized 280test_vim_str2nr ( '012345670' , flags , { len =9 , num =2739128 , unum =2739128 , pre =oct } , 0 ) 281 282test_vim_str2nr ( '054' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 0 ) 283test_vim_str2nr ( '054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 284test_vim_str2nr ( '054' , flags , { len =2 , num =5 , unum =5 , pre =oct } , 2 ) 285test_vim_str2nr ( '054' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 3 ) 286test_vim_str2nr ( '0548' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 3 ) 287test_vim_str2nr ( '054' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 4 ) 288 289test_vim_str2nr ( '054x' , flags , { len =0 } , 4 ) 290test_vim_str2nr ( '054x' , flags , { len =0 } , 0 ) 291test_vim_str2nr ( '054x' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 4 , false ) 292test_vim_str2nr ( '054x' , flags , { len =3 , num =44 , unum =44 , pre =oct } , 0 , false ) 293 294test_vim_str2nr ( '-054' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 0 ) 295test_vim_str2nr ( '-054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 296test_vim_str2nr ( '-054' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 297test_vim_str2nr ( '-054' , flags , { len =3 , num =- 5 , unum =5 , pre =oct } , 3 ) 298test_vim_str2nr ( '-054' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 4 ) 299test_vim_str2nr ( '-0548' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 4 ) 300test_vim_str2nr ( '-054' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 5 ) 301 302test_vim_str2nr ( '-054x' , flags , { len =0 } , 5 ) 303test_vim_str2nr ( '-054x' , flags , { len =0 } , 0 ) 304test_vim_str2nr ( '-054x' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 5 , false ) 305test_vim_str2nr ( '-054x' , flags , { len =4 , num =- 44 , unum =44 , pre =oct } , 0 , false ) 306 307if flags > lib . STR2NR_FORCE then 308test_vim_str2nr ( '-54' , flags , { len =3 , num =- 44 , unum =44 , pre =0 } , 0 ) 309test_vim_str2nr ( '-0548' , flags , { len =0 } , 5 ) 310test_vim_str2nr ( '-0548' , flags , { len =0 } , 0 ) 311test_vim_str2nr ( '-0548' , flags , { len =4 , num =- 44 , unum =44 , pre =0 } , 5 , false ) 312test_vim_str2nr ( '-0548' , flags , { len =4 , num =- 44 , unum =44 , pre =0 } , 0 , false ) 313else 314test_vim_str2nr ( '-0548' , flags , { len =5 , num =- 548 , unum =548 , pre =0 } , 5 ) 315test_vim_str2nr ( '-0548' , flags , { len =5 , num =- 548 , unum =548 , pre =0 } , 0 ) 316end 317end 318end ) 319itp ( 'works with octal numbers (0o or 0O prefix)' , function () 320for _ , flags in ipairs ( { 321lib . STR2NR_OOCT , 322lib . STR2NR_OOCT + lib . STR2NR_BIN , 323lib . STR2NR_OOCT + lib . STR2NR_HEX , 324lib . STR2NR_OCT + lib . STR2NR_OOCT , 325lib . STR2NR_OCT + lib . STR2NR_OOCT + lib . STR2NR_BIN , 326lib . STR2NR_OCT + lib . STR2NR_OOCT + lib . STR2NR_HEX , 327lib . STR2NR_ALL , 328lib . STR2NR_FORCE + lib . STR2NR_OCT , 329lib . STR2NR_FORCE + lib . STR2NR_OOCT , 330lib . STR2NR_FORCE + lib . STR2NR_OCT + lib . STR2NR_OOCT , 331} ) do 332local oct 333local OCT 334if flags > lib . STR2NR_FORCE then 335oct =0 336OCT =0 337else 338oct =( 'o' ): byte () 339OCT =( 'O' ): byte () 340end 341 342test_vim_str2nr ( '0o054' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 0 ) 343test_vim_str2nr ( '0o054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 344test_vim_str2nr ( '0o054' , flags , { len =0 } , 2 ) 345test_vim_str2nr ( '0o054' , flags , { len =3 , num =0 , unum =0 , pre =oct } , 3 ) 346test_vim_str2nr ( '0o054' , flags , { len =4 , num =5 , unum =5 , pre =oct } , 4 ) 347test_vim_str2nr ( '0o054' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 5 ) 348test_vim_str2nr ( '0o0548' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 5 ) 349test_vim_str2nr ( '0o054' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 6 ) 350 351test_vim_str2nr ( '0o054x' , flags , { len =0 } , 6 ) 352test_vim_str2nr ( '0o054x' , flags , { len =0 } , 0 ) 353test_vim_str2nr ( '0o054x' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 6 , false ) 354test_vim_str2nr ( '0o054x' , flags , { len =5 , num =44 , unum =44 , pre =oct } , 0 , false ) 355 356test_vim_str2nr ( '-0o054' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 0 ) 357test_vim_str2nr ( '-0o054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 358test_vim_str2nr ( '-0o054' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 359test_vim_str2nr ( '-0o054' , flags , { len =0 } , 3 ) 360test_vim_str2nr ( '-0o054' , flags , { len =4 , num =0 , unum =0 , pre =oct } , 4 ) 361test_vim_str2nr ( '-0o054' , flags , { len =5 , num =- 5 , unum =5 , pre =oct } , 5 ) 362test_vim_str2nr ( '-0o054' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 6 ) 363test_vim_str2nr ( '-0o0548' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 6 ) 364test_vim_str2nr ( '-0o054' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 7 ) 365 366test_vim_str2nr ( '-0o054x' , flags , { len =0 } , 7 ) 367test_vim_str2nr ( '-0o054x' , flags , { len =0 } , 0 ) 368test_vim_str2nr ( '-0o054x' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 7 , false ) 369test_vim_str2nr ( '-0o054x' , flags , { len =6 , num =- 44 , unum =44 , pre =oct } , 0 , false ) 370 371test_vim_str2nr ( '0O054' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 0 ) 372test_vim_str2nr ( '0O054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 373test_vim_str2nr ( '0O054' , flags , { len =0 } , 2 ) 374test_vim_str2nr ( '0O054' , flags , { len =3 , num =0 , unum =0 , pre =OCT } , 3 ) 375test_vim_str2nr ( '0O054' , flags , { len =4 , num =5 , unum =5 , pre =OCT } , 4 ) 376test_vim_str2nr ( '0O054' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 5 ) 377test_vim_str2nr ( '0O0548' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 5 ) 378test_vim_str2nr ( '0O054' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 6 ) 379 380test_vim_str2nr ( '0O054x' , flags , { len =0 } , 6 ) 381test_vim_str2nr ( '0O054x' , flags , { len =0 } , 0 ) 382test_vim_str2nr ( '0O054x' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 6 , false ) 383test_vim_str2nr ( '0O054x' , flags , { len =5 , num =44 , unum =44 , pre =OCT } , 0 , false ) 384 385test_vim_str2nr ( '-0O054' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 0 ) 386test_vim_str2nr ( '-0O054' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 387test_vim_str2nr ( '-0O054' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 388test_vim_str2nr ( '-0O054' , flags , { len =0 } , 3 ) 389test_vim_str2nr ( '-0O054' , flags , { len =4 , num =0 , unum =0 , pre =OCT } , 4 ) 390test_vim_str2nr ( '-0O054' , flags , { len =5 , num =- 5 , unum =5 , pre =OCT } , 5 ) 391test_vim_str2nr ( '-0O054' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 6 ) 392test_vim_str2nr ( '-0O0548' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 6 ) 393test_vim_str2nr ( '-0O054' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 7 ) 394 395test_vim_str2nr ( '-0O054x' , flags , { len =0 } , 7 ) 396test_vim_str2nr ( '-0O054x' , flags , { len =0 } , 0 ) 397test_vim_str2nr ( '-0O054x' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 7 , false ) 398test_vim_str2nr ( '-0O054x' , flags , { len =6 , num =- 44 , unum =44 , pre =OCT } , 0 , false ) 399 400if flags > lib . STR2NR_FORCE then 401test_vim_str2nr ( '-0548' , flags , { len =0 } , 5 ) 402test_vim_str2nr ( '-0548' , flags , { len =0 } , 0 ) 403test_vim_str2nr ( '-0548' , flags , { len =4 , num =- 44 , unum =44 , pre =0 } , 5 , false ) 404test_vim_str2nr ( '-0548' , flags , { len =4 , num =- 44 , unum =44 , pre =0 } , 0 , false ) 405test_vim_str2nr ( '-055' , flags , { len =4 , num =- 45 , unum =45 , pre =0 } , 0 ) 406else 407test_vim_str2nr ( '-0548' , flags , { len =5 , num =- 548 , unum =548 , pre =0 } , 5 ) 408test_vim_str2nr ( '-0548' , flags , { len =5 , num =- 548 , unum =548 , pre =0 } , 0 ) 409end 410end 411end ) 412itp ( 'works with hexadecimal numbers' , function () 413for _ , flags in ipairs ( { 414lib . STR2NR_HEX , 415lib . STR2NR_HEX + lib . STR2NR_BIN , 416lib . STR2NR_HEX + lib . STR2NR_OCT , 417lib . STR2NR_ALL , 418lib . STR2NR_FORCE + lib . STR2NR_HEX , 419} ) do 420local hex 421local HEX 422if flags > lib . STR2NR_FORCE then 423hex =0 424HEX =0 425else 426hex =( 'x' ): byte () 427HEX =( 'X' ): byte () 428end 429 430-- Check that all digits are recognized 431test_vim_str2nr ( '0x12345' , flags , { len =7 , num =74565 , unum =74565 , pre =hex } , 0 ) 432test_vim_str2nr ( '0x67890' , flags , { len =7 , num =424080 , unum =424080 , pre =hex } , 0 ) 433test_vim_str2nr ( '0xABCDEF' , flags , { len =8 , num =11259375 , unum =11259375 , pre =hex } , 0 ) 434test_vim_str2nr ( '0xabcdef' , flags , { len =8 , num =11259375 , unum =11259375 , pre =hex } , 0 ) 435 436test_vim_str2nr ( '0x101' , flags , { len =5 , num =257 , unum =257 , pre =hex } , 0 ) 437test_vim_str2nr ( '0x101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 438test_vim_str2nr ( '0x101' , flags , { len =0 } , 2 ) 439test_vim_str2nr ( '0x101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 2 , false ) 440test_vim_str2nr ( '0x101' , flags , { len =3 , num =1 , unum =1 , pre =hex } , 3 ) 441test_vim_str2nr ( '0x101' , flags , { len =4 , num =16 , unum =16 , pre =hex } , 4 ) 442test_vim_str2nr ( '0x101' , flags , { len =5 , num =257 , unum =257 , pre =hex } , 5 ) 443test_vim_str2nr ( '0x101' , flags , { len =5 , num =257 , unum =257 , pre =hex } , 6 ) 444 445test_vim_str2nr ( '0x101G' , flags , { len =0 } , 0 ) 446test_vim_str2nr ( '0x101G' , flags , { len =0 } , 6 ) 447test_vim_str2nr ( '0x101G' , flags , { len =5 , num =257 , unum =257 , pre =hex } , 0 , false ) 448test_vim_str2nr ( '0x101G' , flags , { len =5 , num =257 , unum =257 , pre =hex } , 6 , false ) 449 450test_vim_str2nr ( '-0x101' , flags , { len =6 , num =- 257 , unum =257 , pre =hex } , 0 ) 451test_vim_str2nr ( '-0x101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 452test_vim_str2nr ( '-0x101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 453test_vim_str2nr ( '-0x101' , flags , { len =0 } , 3 ) 454test_vim_str2nr ( '-0x101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 3 , false ) 455test_vim_str2nr ( '-0x101' , flags , { len =4 , num =- 1 , unum =1 , pre =hex } , 4 ) 456test_vim_str2nr ( '-0x101' , flags , { len =5 , num =- 16 , unum =16 , pre =hex } , 5 ) 457test_vim_str2nr ( '-0x101' , flags , { len =6 , num =- 257 , unum =257 , pre =hex } , 6 ) 458test_vim_str2nr ( '-0x101' , flags , { len =6 , num =- 257 , unum =257 , pre =hex } , 7 ) 459 460test_vim_str2nr ( '-0x101G' , flags , { len =0 } , 0 ) 461test_vim_str2nr ( '-0x101G' , flags , { len =0 } , 7 ) 462test_vim_str2nr ( '-0x101G' , flags , { len =6 , num =- 257 , unum =257 , pre =hex } , 0 , false ) 463test_vim_str2nr ( '-0x101G' , flags , { len =6 , num =- 257 , unum =257 , pre =hex } , 7 , false ) 464 465test_vim_str2nr ( '0X101' , flags , { len =5 , num =257 , unum =257 , pre =HEX } , 0 ) 466test_vim_str2nr ( '0X101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 467test_vim_str2nr ( '0X101' , flags , { len =0 } , 2 ) 468test_vim_str2nr ( '0X101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 2 , false ) 469test_vim_str2nr ( '0X101' , flags , { len =3 , num =1 , unum =1 , pre =HEX } , 3 ) 470test_vim_str2nr ( '0X101' , flags , { len =4 , num =16 , unum =16 , pre =HEX } , 4 ) 471test_vim_str2nr ( '0X101' , flags , { len =5 , num =257 , unum =257 , pre =HEX } , 5 ) 472test_vim_str2nr ( '0X101' , flags , { len =5 , num =257 , unum =257 , pre =HEX } , 6 ) 473 474test_vim_str2nr ( '0X101G' , flags , { len =0 } , 0 ) 475test_vim_str2nr ( '0X101G' , flags , { len =0 } , 6 ) 476test_vim_str2nr ( '0X101G' , flags , { len =5 , num =257 , unum =257 , pre =HEX } , 0 , false ) 477test_vim_str2nr ( '0X101G' , flags , { len =5 , num =257 , unum =257 , pre =HEX } , 6 , false ) 478 479test_vim_str2nr ( '-0X101' , flags , { len =6 , num =- 257 , unum =257 , pre =HEX } , 0 ) 480test_vim_str2nr ( '-0X101' , flags , { len =1 , num =0 , unum =0 , pre =0 } , 1 ) 481test_vim_str2nr ( '-0X101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 2 ) 482test_vim_str2nr ( '-0X101' , flags , { len =0 } , 3 ) 483test_vim_str2nr ( '-0X101' , flags , { len =2 , num =0 , unum =0 , pre =0 } , 3 , false ) 484test_vim_str2nr ( '-0X101' , flags , { len =4 , num =- 1 , unum =1 , pre =HEX } , 4 ) 485test_vim_str2nr ( '-0X101' , flags , { len =5 , num =- 16 , unum =16 , pre =HEX } , 5 ) 486test_vim_str2nr ( '-0X101' , flags , { len =6 , num =- 257 , unum =257 , pre =HEX } , 6 ) 487test_vim_str2nr ( '-0X101' , flags , { len =6 , num =- 257 , unum =257 , pre =HEX } , 7 ) 488 489test_vim_str2nr ( '-0X101G' , flags , { len =0 } , 0 ) 490test_vim_str2nr ( '-0X101G' , flags , { len =0 } , 7 ) 491test_vim_str2nr ( '-0X101G' , flags , { len =6 , num =- 257 , unum =257 , pre =HEX } , 0 , false ) 492test_vim_str2nr ( '-0X101G' , flags , { len =6 , num =- 257 , unum =257 , pre =HEX } , 7 , false ) 493 494if flags > lib . STR2NR_FORCE then 495test_vim_str2nr ( '-101' , flags , { len =4 , num =- 257 , unum =257 , pre =0 } , 0 ) 496end 497end 498end ) 499-- Test_str2nr() in test_functions.vim already tests normal usage 500itp ( 'works with weirdly quoted numbers' , function () 501local flags =lib . STR2NR_DEC + lib . STR2NR_QUOTE 502test_vim_str2nr ( "'027" , flags , { len =0 } , 0 ) 503test_vim_str2nr ( "'027" , flags , { len =0 } , 0 , false ) 504test_vim_str2nr ( "1'2'3'4" , flags , { len =7 , num =1234 , unum =1234 , pre =0 } , 0 ) 505 506-- counter-intuitive, but like Vim, strict=true should partially accept 507-- these: (' and - are not alphanumeric) 508test_vim_str2nr ( "7''331" , flags , { len =1 , num =7 , unum =7 , pre =0 } , 0 ) 509test_vim_str2nr ( "123'x4" , flags , { len =3 , num =123 , unum =123 , pre =0 } , 0 ) 510test_vim_str2nr ( "1337'" , flags , { len =4 , num =1337 , unum =1337 , pre =0 } , 0 ) 511test_vim_str2nr ( "-'" , flags , { len =1 , num =0 , unum =0 , pre =0 } , 0 ) 512 513flags =lib . STR2NR_HEX + lib . STR2NR_QUOTE 514local hex =( 'x' ): byte () 515test_vim_str2nr ( "0x'abcd" , flags , { len =0 } , 0 ) 516test_vim_str2nr ( "0x'abcd" , flags , { len =1 , num =0 , unum =0 , pre =0 } , 0 , false ) 517test_vim_str2nr ( "0xab''cd" , flags , { len =4 , num =171 , unum =171 , pre =hex } , 0 ) 518end ) 519end )