test_blob.vim 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. " Tests for the Blob types
  2. source check.vim
  3. source vim9.vim
  4. func TearDown()
  5. " Run garbage collection after every test
  6. call test_garbagecollect_now()
  7. endfunc
  8. " Tests for Blob type
  9. " Blob creation from constant
  10. func Test_blob_create()
  11. let lines =<< trim END
  12. VAR b = 0zDEADBEEF
  13. call assert_equal(v:t_blob, type(b))
  14. call assert_equal(4, len(b))
  15. call assert_equal(0xDE, b[0])
  16. call assert_equal(0xAD, b[1])
  17. call assert_equal(0xBE, b[2])
  18. call assert_equal(0xEF, b[3])
  19. call assert_fails('VAR x = b[4]')
  20. call assert_equal(0xDE, get(b, 0))
  21. call assert_equal(0xEF, get(b, 3))
  22. call assert_fails('VAR b = 0z1', 'E973:')
  23. call assert_fails('VAR b = 0z1x', 'E973:')
  24. call assert_fails('VAR b = 0z12345', 'E973:')
  25. call assert_equal(0z, v:_null_blob)
  26. LET b = 0z001122.33445566.778899.aabbcc.dd
  27. call assert_equal(0z00112233445566778899aabbccdd, b)
  28. call assert_fails('VAR b = 0z1.1')
  29. call assert_fails('VAR b = 0z.')
  30. call assert_fails('VAR b = 0z001122.')
  31. call assert_fails('call get("", 1)', 'E896:')
  32. call assert_equal(0, len(v:_null_blob))
  33. call assert_equal(0z, copy(v:_null_blob))
  34. END
  35. call CheckLegacyAndVim9Success(lines)
  36. endfunc
  37. " assignment to a blob
  38. func Test_blob_assign()
  39. let lines =<< trim END
  40. VAR b = 0zDEADBEEF
  41. VAR b2 = b[1 : 2]
  42. call assert_equal(0zADBE, b2)
  43. VAR bcopy = b[:]
  44. call assert_equal(b, bcopy)
  45. call assert_false(b is bcopy)
  46. LET b = 0zDEADBEEF
  47. LET b2 = b
  48. call assert_true(b is b2)
  49. LET b[:] = 0z11223344
  50. call assert_equal(0z11223344, b)
  51. call assert_equal(0z11223344, b2)
  52. call assert_true(b is b2)
  53. LET b = 0zDEADBEEF
  54. LET b[3 :] = 0z66
  55. call assert_equal(0zDEADBE66, b)
  56. LET b[: 1] = 0z8899
  57. call assert_equal(0z8899BE66, b)
  58. LET b = 0zDEADBEEF
  59. LET b += 0z99
  60. call assert_equal(0zDEADBEEF99, b)
  61. VAR l = [0z12]
  62. VAR m = deepcopy(l)
  63. LET m[0] = 0z34 #" E742 or E741 should not occur.
  64. VAR blob1 = 0z10
  65. LET blob1 += v:_null_blob
  66. call assert_equal(0z10, blob1)
  67. LET blob1 = v:_null_blob
  68. LET blob1 += 0z20
  69. call assert_equal(0z20, blob1)
  70. END
  71. call CheckLegacyAndVim9Success(lines)
  72. let lines =<< trim END
  73. VAR b = 0zDEADBEEF
  74. LET b[2 : 3] = 0z112233
  75. END
  76. call CheckLegacyAndVim9Failure(lines, 'E972:')
  77. let lines =<< trim END
  78. VAR b = 0zDEADBEEF
  79. LET b[2 : 3] = 0z11
  80. END
  81. call CheckLegacyAndVim9Failure(lines, 'E972:')
  82. let lines =<< trim END
  83. VAR b = 0zDEADBEEF
  84. LET b[3 : 2] = 0z
  85. END
  86. call CheckLegacyAndVim9Failure(lines, 'E979:')
  87. let lines =<< trim END
  88. VAR b = 0zDEADBEEF
  89. LET b ..= 0z33
  90. END
  91. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1019:', 'E734:'])
  92. let lines =<< trim END
  93. VAR b = 0zDEADBEEF
  94. LET b ..= "xx"
  95. END
  96. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1019:', 'E734:'])
  97. let lines =<< trim END
  98. VAR b = 0zDEADBEEF
  99. LET b += "xx"
  100. END
  101. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1012:', 'E734:'])
  102. let lines =<< trim END
  103. VAR b = 0zDEADBEEF
  104. LET b[1 : 1] ..= 0z55
  105. END
  106. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1183:', 'E734:'])
  107. call assert_fails('let b = readblob("a1b2c3")', 'E484:')
  108. endfunc
  109. func Test_blob_get_range()
  110. let lines =<< trim END
  111. VAR b = 0z0011223344
  112. call assert_equal(0z2233, b[2 : 3])
  113. call assert_equal(0z223344, b[2 : -1])
  114. call assert_equal(0z00, b[0 : -5])
  115. call assert_equal(0z, b[0 : -11])
  116. call assert_equal(0z44, b[-1 :])
  117. call assert_equal(0z0011223344, b[:])
  118. call assert_equal(0z0011223344, b[: -1])
  119. call assert_equal(0z, b[5 : 6])
  120. call assert_equal(0z0011, b[-10 : 1])
  121. END
  122. call CheckLegacyAndVim9Success(lines)
  123. " legacy script white space
  124. let b = 0z0011223344
  125. call assert_equal(0z2233, b[2:3])
  126. endfunc
  127. func Test_blob_get()
  128. let lines =<< trim END
  129. VAR b = 0z0011223344
  130. call assert_equal(0x00, get(b, 0))
  131. call assert_equal(0x22, get(b, 2, 999))
  132. call assert_equal(0x44, get(b, 4))
  133. call assert_equal(0x44, get(b, -1))
  134. call assert_equal(-1, get(b, 5))
  135. call assert_equal(999, get(b, 5, 999))
  136. call assert_equal(-1, get(b, -8))
  137. call assert_equal(999, get(b, -8, 999))
  138. call assert_equal(10, get(v:_null_blob, 2, 10))
  139. call assert_equal(0x00, b[0])
  140. call assert_equal(0x22, b[2])
  141. call assert_equal(0x44, b[4])
  142. call assert_equal(0x44, b[-1])
  143. END
  144. call CheckLegacyAndVim9Success(lines)
  145. let lines =<< trim END
  146. VAR b = 0z0011223344
  147. echo b[5]
  148. END
  149. call CheckLegacyAndVim9Failure(lines, 'E979:')
  150. let lines =<< trim END
  151. VAR b = 0z0011223344
  152. echo b[-8]
  153. END
  154. call CheckLegacyAndVim9Failure(lines, 'E979:')
  155. endfunc
  156. func Test_blob_to_string()
  157. let lines =<< trim END
  158. VAR b = 0z00112233445566778899aabbccdd
  159. call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
  160. call assert_equal(b, eval(string(b)))
  161. call remove(b, 4, -1)
  162. call assert_equal('0z00112233', string(b))
  163. call remove(b, 0, 3)
  164. call assert_equal('0z', string(b))
  165. call assert_equal('0z', string(v:_null_blob))
  166. END
  167. call CheckLegacyAndVim9Success(lines)
  168. endfunc
  169. func Test_blob_compare()
  170. let lines =<< trim END
  171. VAR b1 = 0z0011
  172. VAR b2 = 0z1100
  173. VAR b3 = 0z001122
  174. call assert_true(b1 == b1)
  175. call assert_false(b1 == b2)
  176. call assert_false(b1 == b3)
  177. call assert_true(b1 != b2)
  178. call assert_true(b1 != b3)
  179. call assert_true(b1 == 0z0011)
  180. call assert_false(b1 is b2)
  181. LET b2 = b1
  182. call assert_true(b1 == b2)
  183. call assert_true(b1 is b2)
  184. LET b2 = copy(b1)
  185. call assert_true(b1 == b2)
  186. call assert_false(b1 is b2)
  187. LET b2 = b1[:]
  188. call assert_true(b1 == b2)
  189. call assert_false(b1 is b2)
  190. call assert_true(b1 isnot b2)
  191. call assert_true(0z != 0z10)
  192. call assert_true(0z10 != 0z)
  193. END
  194. call CheckLegacyAndVim9Success(lines)
  195. let lines =<< trim END
  196. VAR b1 = 0z0011
  197. echo b1 == 9
  198. END
  199. call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
  200. let lines =<< trim END
  201. VAR b1 = 0z0011
  202. echo b1 != 9
  203. END
  204. call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
  205. let lines =<< trim END
  206. VAR b1 = 0z0011
  207. VAR b2 = 0z1100
  208. VAR x = b1 > b2
  209. END
  210. call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
  211. let lines =<< trim END
  212. VAR b1 = 0z0011
  213. VAR b2 = 0z1100
  214. VAR x = b1 < b2
  215. END
  216. call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
  217. let lines =<< trim END
  218. VAR b1 = 0z0011
  219. VAR b2 = 0z1100
  220. VAR x = b1 - b2
  221. END
  222. call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
  223. let lines =<< trim END
  224. VAR b1 = 0z0011
  225. VAR b2 = 0z1100
  226. VAR x = b1 / b2
  227. END
  228. call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
  229. let lines =<< trim END
  230. VAR b1 = 0z0011
  231. VAR b2 = 0z1100
  232. VAR x = b1 * b2
  233. END
  234. call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
  235. endfunc
  236. func Test_blob_index_assign()
  237. let lines =<< trim END
  238. VAR b = 0z00
  239. LET b[1] = 0x11
  240. LET b[2] = 0x22
  241. LET b[0] = 0x33
  242. call assert_equal(0z331122, b)
  243. END
  244. call CheckLegacyAndVim9Success(lines)
  245. let lines =<< trim END
  246. VAR b = 0z00
  247. LET b[2] = 0x33
  248. END
  249. call CheckLegacyAndVim9Failure(lines, 'E979:')
  250. let lines =<< trim END
  251. VAR b = 0z00
  252. LET b[-2] = 0x33
  253. END
  254. call CheckLegacyAndVim9Failure(lines, 'E979:')
  255. let lines =<< trim END
  256. VAR b = 0z00010203
  257. LET b[0 : -1] = 0z33
  258. END
  259. call CheckLegacyAndVim9Failure(lines, 'E979:')
  260. let lines =<< trim END
  261. VAR b = 0z00010203
  262. LET b[3 : 4] = 0z3344
  263. END
  264. call CheckLegacyAndVim9Failure(lines, 'E979:')
  265. endfunc
  266. func Test_blob_for_loop()
  267. let lines =<< trim END
  268. VAR blob = 0z00010203
  269. VAR i = 0
  270. for byte in blob
  271. call assert_equal(i, byte)
  272. LET i += 1
  273. endfor
  274. call assert_equal(4, i)
  275. LET blob = 0z00
  276. call remove(blob, 0)
  277. call assert_equal(0, len(blob))
  278. for byte in blob
  279. call assert_report('loop over empty blob')
  280. endfor
  281. LET blob = 0z0001020304
  282. LET i = 0
  283. for byte in blob
  284. call assert_equal(i, byte)
  285. if i == 1
  286. call remove(blob, 0)
  287. elseif i == 3
  288. call remove(blob, 3)
  289. endif
  290. LET i += 1
  291. endfor
  292. call assert_equal(5, i)
  293. END
  294. call CheckLegacyAndVim9Success(lines)
  295. " Test for skipping the loop var assignment in a for loop
  296. let lines =<< trim END
  297. VAR blob = 0z998877
  298. VAR c = 0
  299. for _ in blob
  300. LET c += 1
  301. endfor
  302. call assert_equal(3, c)
  303. END
  304. call CheckLegacyAndVim9Success(lines)
  305. endfunc
  306. func Test_blob_concatenate()
  307. let lines =<< trim END
  308. VAR b = 0z0011
  309. LET b += 0z2233
  310. call assert_equal(0z00112233, b)
  311. LET b = 0zDEAD + 0zBEEF
  312. call assert_equal(0zDEADBEEF, b)
  313. END
  314. call CheckLegacyAndVim9Success(lines)
  315. let lines =<< trim END
  316. VAR b = 0z0011
  317. LET b += "a"
  318. END
  319. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1012:', 'E734:'])
  320. let lines =<< trim END
  321. VAR b = 0z0011
  322. LET b += 88
  323. END
  324. call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1012:', 'E734:'])
  325. endfunc
  326. func Test_blob_add()
  327. let lines =<< trim END
  328. VAR b = 0z0011
  329. call add(b, 0x22)
  330. call assert_equal(0z001122, b)
  331. END
  332. call CheckLegacyAndVim9Success(lines)
  333. " Only works in legacy script
  334. let b = 0z0011
  335. call add(b, '51')
  336. call assert_equal(0z001133, b)
  337. call assert_equal(1, add(v:_null_blob, 0x22))
  338. let lines =<< trim END
  339. VAR b = 0z0011
  340. call add(b, [9])
  341. END
  342. call CheckLegacyAndVim9Failure(lines, ['E745:', 'E1012:', 'E745:'])
  343. let lines =<< trim END
  344. VAR b = 0z0011
  345. call add("", 0x01)
  346. END
  347. call CheckLegacyAndVim9Failure(lines, 'E897:')
  348. let lines =<< trim END
  349. add(v:_null_blob, 0x22)
  350. END
  351. call CheckDefExecAndScriptFailure(lines, 'E1131:')
  352. let lines =<< trim END
  353. let b = 0zDEADBEEF
  354. lockvar b
  355. call add(b, 0)
  356. unlockvar b
  357. END
  358. call CheckScriptFailure(lines, 'E741:')
  359. endfunc
  360. func Test_blob_empty()
  361. call assert_false(empty(0z001122))
  362. call assert_true(empty(0z))
  363. call assert_true(empty(v:_null_blob))
  364. endfunc
  365. " Test removing items in blob
  366. func Test_blob_func_remove()
  367. let lines =<< trim END
  368. #" Test removing 1 element
  369. VAR b = 0zDEADBEEF
  370. call assert_equal(0xDE, remove(b, 0))
  371. call assert_equal(0zADBEEF, b)
  372. LET b = 0zDEADBEEF
  373. call assert_equal(0xEF, remove(b, -1))
  374. call assert_equal(0zDEADBE, b)
  375. LET b = 0zDEADBEEF
  376. call assert_equal(0xAD, remove(b, 1))
  377. call assert_equal(0zDEBEEF, b)
  378. #" Test removing range of element(s)
  379. LET b = 0zDEADBEEF
  380. call assert_equal(0zBE, remove(b, 2, 2))
  381. call assert_equal(0zDEADEF, b)
  382. LET b = 0zDEADBEEF
  383. call assert_equal(0zADBE, remove(b, 1, 2))
  384. call assert_equal(0zDEEF, b)
  385. END
  386. call CheckLegacyAndVim9Success(lines)
  387. " Test invalid cases
  388. let lines =<< trim END
  389. VAR b = 0zDEADBEEF
  390. call remove(b, 5)
  391. END
  392. call CheckLegacyAndVim9Failure(lines, 'E979:')
  393. let lines =<< trim END
  394. VAR b = 0zDEADBEEF
  395. call remove(b, 1, 5)
  396. END
  397. call CheckLegacyAndVim9Failure(lines, 'E979:')
  398. let lines =<< trim END
  399. VAR b = 0zDEADBEEF
  400. call remove(b, -10)
  401. END
  402. call CheckLegacyAndVim9Failure(lines, 'E979:')
  403. let lines =<< trim END
  404. VAR b = 0zDEADBEEF
  405. call remove(b, 3, 2)
  406. END
  407. call CheckLegacyAndVim9Failure(lines, 'E979:')
  408. let lines =<< trim END
  409. VAR b = 0zDEADBEEF
  410. call remove(1, 0)
  411. END
  412. call CheckLegacyAndVim9Failure(lines, 'E896:')
  413. let lines =<< trim END
  414. VAR b = 0zDEADBEEF
  415. call remove(b, b)
  416. END
  417. call CheckLegacyAndVim9Failure(lines, 'E974:')
  418. let lines =<< trim END
  419. VAR b = 0zDEADBEEF
  420. call remove(b, 1, [])
  421. END
  422. call CheckLegacyAndVim9Failure(lines, 'E745:')
  423. let lines =<< trim END
  424. VAR b = 0zDEADBEEF
  425. call remove(v:_null_blob, 1, 2)
  426. END
  427. call CheckLegacyAndVim9Failure(lines, 'E979:')
  428. let lines =<< trim END
  429. let b = 0zDEADBEEF
  430. lockvar b
  431. call remove(b, 0)
  432. unlockvar b
  433. END
  434. call CheckScriptFailure(lines, 'E741:')
  435. " can only check at script level, not in a :def function
  436. let lines =<< trim END
  437. vim9script
  438. var b = 0zDEADBEEF
  439. lockvar b
  440. remove(b, 0)
  441. END
  442. call CheckScriptFailure(lines, 'E741:')
  443. call assert_fails('echo remove(0z1020, [])', 'E745:')
  444. call assert_fails('echo remove(0z1020, 0, [])', 'E745:')
  445. endfunc
  446. func Test_blob_read_write()
  447. let lines =<< trim END
  448. VAR b = 0zDEADBEEF
  449. call writefile(b, 'Xblob')
  450. VAR br = readfile('Xblob', 'B')
  451. call assert_equal(b, br)
  452. VAR br2 = readblob('Xblob')
  453. call assert_equal(b, br2)
  454. VAR br3 = readblob('Xblob', 1)
  455. call assert_equal(b[1 :], br3)
  456. VAR br4 = readblob('Xblob', 1, 2)
  457. call assert_equal(b[1 : 2], br4)
  458. VAR br5 = readblob('Xblob', -3)
  459. call assert_equal(b[-3 :], br5)
  460. VAR br6 = readblob('Xblob', -3, 2)
  461. call assert_equal(b[-3 : -2], br6)
  462. #" reading past end of file, empty result
  463. VAR br1e = readblob('Xblob', 10000)
  464. call assert_equal(0z, br1e)
  465. #" reading too much, result is truncated
  466. VAR blong = readblob('Xblob', -1000)
  467. call assert_equal(b, blong)
  468. LET blong = readblob('Xblob', -10, 8)
  469. call assert_equal(b, blong)
  470. LET blong = readblob('Xblob', 0, 10)
  471. call assert_equal(b, blong)
  472. call delete('Xblob')
  473. END
  474. call CheckLegacyAndVim9Success(lines)
  475. if filereadable('/dev/random')
  476. let b = readblob('/dev/random', 0, 10)
  477. call assert_equal(10, len(b))
  478. endif
  479. call assert_fails("call readblob('notexist')", 'E484:')
  480. " TODO: How do we test for the E485 error?
  481. " This was crashing when calling readfile() with a directory.
  482. call assert_fails("call readfile('.', 'B')", 'E17: "." is a directory')
  483. endfunc
  484. " filter() item in blob
  485. func Test_blob_filter()
  486. let lines =<< trim END
  487. call assert_equal(v:_null_blob, filter(v:_null_blob, '0'))
  488. call assert_equal(0z, filter(0zDEADBEEF, '0'))
  489. call assert_equal(0zADBEEF, filter(0zDEADBEEF, 'v:val != 0xDE'))
  490. call assert_equal(0zDEADEF, filter(0zDEADBEEF, 'v:val != 0xBE'))
  491. call assert_equal(0zDEADBE, filter(0zDEADBEEF, 'v:val != 0xEF'))
  492. call assert_equal(0zDEADBEEF, filter(0zDEADBEEF, '1'))
  493. call assert_equal(0z01030103, filter(0z010203010203, 'v:val != 0x02'))
  494. call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2'))
  495. END
  496. call CheckLegacyAndVim9Success(lines)
  497. call assert_fails('echo filter(0z10, "a10")', 'E121:')
  498. endfunc
  499. " map() item in blob
  500. func Test_blob_map()
  501. let lines =<< trim END
  502. call assert_equal(0zDFAEBFF0, map(0zDEADBEEF, 'v:val + 1'))
  503. call assert_equal(0z00010203, map(0zDEADBEEF, 'v:key'))
  504. call assert_equal(0zDEAEC0F2, map(0zDEADBEEF, 'v:key + v:val'))
  505. END
  506. call CheckLegacyAndVim9Success(lines)
  507. let lines =<< trim END
  508. call map(0z00, '[9]')
  509. END
  510. call CheckLegacyAndVim9Failure(lines, 'E978:')
  511. call assert_fails('echo map(0z10, "a10")', 'E121:')
  512. endfunc
  513. func Test_blob_index()
  514. let lines =<< trim END
  515. call assert_equal(2, index(0zDEADBEEF, 0xBE))
  516. call assert_equal(-1, index(0zDEADBEEF, 0))
  517. call assert_equal(2, index(0z11111111, 0x11, 2))
  518. call assert_equal(3, 0z11110111->index(0x11, 2))
  519. call assert_equal(2, index(0z11111111, 0x11, -2))
  520. call assert_equal(3, index(0z11110111, 0x11, -2))
  521. call assert_equal(0, index(0z11110111, 0x11, -10))
  522. call assert_equal(-1, index(v:_null_blob, 1))
  523. END
  524. call CheckLegacyAndVim9Success(lines)
  525. let lines =<< trim END
  526. echo index(0z11110111, 0x11, [])
  527. END
  528. call CheckLegacyAndVim9Failure(lines, 'E745:')
  529. let lines =<< trim END
  530. call index("asdf", 0)
  531. END
  532. call CheckLegacyAndVim9Failure(lines, 'E897:')
  533. endfunc
  534. func Test_blob_insert()
  535. let lines =<< trim END
  536. VAR b = 0zDEADBEEF
  537. call insert(b, 0x33)
  538. call assert_equal(0z33DEADBEEF, b)
  539. LET b = 0zDEADBEEF
  540. call insert(b, 0x33, 2)
  541. call assert_equal(0zDEAD33BEEF, b)
  542. END
  543. call CheckLegacyAndVim9Success(lines)
  544. " only works in legacy script
  545. call assert_equal(0, insert(v:_null_blob, 0x33))
  546. let lines =<< trim END
  547. VAR b = 0zDEADBEEF
  548. call insert(b, -1)
  549. END
  550. call CheckLegacyAndVim9Failure(lines, 'E475:')
  551. let lines =<< trim END
  552. VAR b = 0zDEADBEEF
  553. call insert(b, 257)
  554. END
  555. call CheckLegacyAndVim9Failure(lines, 'E475:')
  556. let lines =<< trim END
  557. VAR b = 0zDEADBEEF
  558. call insert(b, 0, [9])
  559. END
  560. call CheckLegacyAndVim9Failure(lines, ['E745:', 'E1013:', 'E745:'])
  561. let lines =<< trim END
  562. VAR b = 0zDEADBEEF
  563. call insert(b, 0, -20)
  564. END
  565. call CheckLegacyAndVim9Failure(lines, 'E475:')
  566. let lines =<< trim END
  567. VAR b = 0zDEADBEEF
  568. call insert(b, 0, 20)
  569. END
  570. call CheckLegacyAndVim9Failure(lines, 'E475:')
  571. let lines =<< trim END
  572. VAR b = 0zDEADBEEF
  573. call insert(b, [])
  574. END
  575. call CheckLegacyAndVim9Failure(lines, ['E745:', 'E1013:', 'E745:'])
  576. let lines =<< trim END
  577. insert(v:_null_blob, 0x33)
  578. END
  579. call CheckDefExecAndScriptFailure(lines, 'E1131:')
  580. let lines =<< trim END
  581. let b = 0zDEADBEEF
  582. lockvar b
  583. call insert(b, 3)
  584. unlockvar b
  585. END
  586. call CheckScriptFailure(lines, 'E741:')
  587. let lines =<< trim END
  588. vim9script
  589. var b = 0zDEADBEEF
  590. lockvar b
  591. insert(b, 3)
  592. END
  593. call CheckScriptFailure(lines, 'E741:')
  594. endfunc
  595. func Test_blob_reverse()
  596. let lines =<< trim END
  597. call assert_equal(0zEFBEADDE, reverse(0zDEADBEEF))
  598. call assert_equal(0zBEADDE, reverse(0zDEADBE))
  599. call assert_equal(0zADDE, reverse(0zDEAD))
  600. call assert_equal(0zDE, reverse(0zDE))
  601. call assert_equal(0z, reverse(v:_null_blob))
  602. END
  603. call CheckLegacyAndVim9Success(lines)
  604. endfunc
  605. func Test_blob_json_encode()
  606. let lines =<< trim END
  607. #" call assert_equal('[222,173,190,239]', json_encode(0zDEADBEEF))
  608. call assert_equal('[222, 173, 190, 239]', json_encode(0zDEADBEEF))
  609. call assert_equal('[]', json_encode(0z))
  610. END
  611. call CheckLegacyAndVim9Success(lines)
  612. endfunc
  613. func Test_blob_lock()
  614. let lines =<< trim END
  615. let b = 0z112233
  616. lockvar b
  617. unlockvar b
  618. let b = 0z44
  619. END
  620. call CheckScriptSuccess(lines)
  621. let lines =<< trim END
  622. vim9script
  623. var b = 0z112233
  624. lockvar b
  625. unlockvar b
  626. b = 0z44
  627. END
  628. call CheckScriptSuccess(lines)
  629. let lines =<< trim END
  630. let b = 0z112233
  631. lockvar b
  632. let b = 0z44
  633. END
  634. call CheckScriptFailure(lines, 'E741:')
  635. let lines =<< trim END
  636. vim9script
  637. var b = 0z112233
  638. lockvar b
  639. b = 0z44
  640. END
  641. call CheckScriptFailure(lines, 'E741:')
  642. endfunc
  643. func Test_blob_sort()
  644. if has('float')
  645. call CheckLegacyAndVim9Failure(['call sort([1.0, 0z11], "f")'], 'E975:')
  646. endif
  647. call CheckLegacyAndVim9Failure(['call sort([11, 0z11], "N")'], 'E974:')
  648. endfunc
  649. " Tests for the blob2list() function
  650. func Test_blob2list()
  651. call assert_fails('let v = blob2list(10)', 'E1238: Blob required for argument 1')
  652. eval 0zFFFF->blob2list()->assert_equal([255, 255])
  653. let tests = [[0z0102, [1, 2]],
  654. \ [0z00, [0]],
  655. \ [0z, []],
  656. \ [0z00000000, [0, 0, 0, 0]],
  657. \ [0zAABB.CCDD, [170, 187, 204, 221]]]
  658. for t in tests
  659. call assert_equal(t[0]->blob2list(), t[1])
  660. endfor
  661. exe 'let v = 0z' .. repeat('000102030405060708090A0B0C0D0E0F', 64)
  662. call assert_equal(1024, blob2list(v)->len())
  663. call assert_equal([4, 8, 15], [v[100], v[1000], v[1023]])
  664. call assert_equal([], blob2list(v:_null_blob))
  665. endfunc
  666. " Tests for the list2blob() function
  667. func Test_list2blob()
  668. call assert_fails('let b = list2blob(0z10)', 'E1211: List required for argument 1')
  669. let tests = [[[1, 2], 0z0102],
  670. \ [[0], 0z00],
  671. \ [[], 0z],
  672. \ [[0, 0, 0, 0], 0z00000000],
  673. \ [[255, 255], 0zFFFF],
  674. \ [[170, 187, 204, 221], 0zAABB.CCDD],
  675. \ ]
  676. for t in tests
  677. call assert_equal(t[1], t[0]->list2blob())
  678. endfor
  679. call assert_fails('let b = list2blob([1, []])', 'E745:')
  680. call assert_fails('let b = list2blob([-1])', 'E1239:')
  681. call assert_fails('let b = list2blob([256])', 'E1239:')
  682. let b = range(16)->repeat(64)->list2blob()
  683. call assert_equal(1024, b->len())
  684. call assert_equal([4, 8, 15], [b[100], b[1000], b[1023]])
  685. call assert_equal(0z, list2blob(v:_null_list))
  686. endfunc
  687. " The following used to cause an out-of-bounds memory access
  688. func Test_blob2string()
  689. let v = '0z' .. repeat('01010101.', 444)
  690. let v ..= '01'
  691. exe 'let b = ' .. v
  692. call assert_equal(v, string(b))
  693. endfunc
  694. func Test_blob_repeat()
  695. call assert_equal(0z, repeat(0z00, 0))
  696. call assert_equal(0z00, repeat(0z00, 1))
  697. call assert_equal(0z0000, repeat(0z00, 2))
  698. call assert_equal(0z00000000, repeat(0z0000, 2))
  699. call assert_equal(0z, repeat(0z12, 0))
  700. call assert_equal(0z, repeat(0z1234, 0))
  701. call assert_equal(0z1234, repeat(0z1234, 1))
  702. call assert_equal(0z12341234, repeat(0z1234, 2))
  703. endfunc
  704. " Test for blob allocation failure
  705. func Test_blob_alloc_failure()
  706. CheckFunction test_alloc_fail
  707. " blob variable
  708. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  709. call assert_fails('let v = 0z10', 'E342:')
  710. " blob slice
  711. let v = 0z1020
  712. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  713. call assert_fails('let x = v[0:0]', 'E342:')
  714. call assert_equal(0z1020, x)
  715. " blob remove()
  716. let v = 0z10203040
  717. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  718. call assert_fails('let x = remove(v, 1, 2)', 'E342:')
  719. call assert_equal(0, x)
  720. " list2blob()
  721. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  722. call assert_fails('let a = list2blob([1, 2, 4])', 'E342:')
  723. call assert_equal(0, a)
  724. " mapnew()
  725. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  726. call assert_fails('let x = mapnew(0z1234, {_, v -> 1})', 'E342:')
  727. call assert_equal(0, x)
  728. " copy()
  729. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  730. call assert_fails('let x = copy(v)', 'E342:')
  731. call assert_equal(0z, x)
  732. " readblob()
  733. call test_alloc_fail(GetAllocId('blob_alloc'), 0, 0)
  734. call assert_fails('let x = readblob("test_blob.vim")', 'E342:')
  735. call assert_equal(0, x)
  736. endfunc
  737. " Test for the indexof() function
  738. func Test_indexof()
  739. let b = 0zdeadbeef
  740. call assert_equal(0, indexof(b, {i, v -> v == 0xde}))
  741. call assert_equal(3, indexof(b, {i, v -> v == 0xef}))
  742. call assert_equal(-1, indexof(b, {i, v -> v == 0x1}))
  743. call assert_equal(1, indexof(b, "v:val == 0xad"))
  744. call assert_equal(-1, indexof(b, "v:val == 0xff"))
  745. call assert_equal(-1, indexof(b, {_, v -> "v == 0xad"}))
  746. call assert_equal(-1, indexof(0z, "v:val == 0x0"))
  747. call assert_equal(-1, indexof(v:_null_blob, "v:val == 0xde"))
  748. call assert_equal(-1, indexof(b, v:_null_string))
  749. " Nvim doesn't have null functions
  750. " call assert_equal(-1, indexof(b, test_null_function()))
  751. call assert_equal(-1, indexof(b, ""))
  752. let b = 0z01020102
  753. call assert_equal(1, indexof(b, "v:val == 0x02", #{startidx: 0}))
  754. call assert_equal(2, indexof(b, "v:val == 0x01", #{startidx: -2}))
  755. call assert_equal(-1, indexof(b, "v:val == 0x01", #{startidx: 5}))
  756. call assert_equal(0, indexof(b, "v:val == 0x01", #{startidx: -5}))
  757. call assert_equal(0, indexof(b, "v:val == 0x01", v:_null_dict))
  758. " failure cases
  759. call assert_fails('let i = indexof(b, "val == 0xde")', 'E121:')
  760. call assert_fails('let i = indexof(b, {})', 'E1256:')
  761. call assert_fails('let i = indexof(b, " ")', 'E15:')
  762. endfunc
  763. " vim: shiftwidth=2 sts=2 expandtab