blob: cab9c532738ff55919ccafc4bc9ef19ec46c7a25 [file] [log] [blame]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001Tests for List and Dictionary types. vim: set ft=vim :
2
3STARTTEST
4:so small.vim
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005:fun Test(...)
Bram Moolenaar4e3c70d2013-03-07 14:50:34 +01006:lang C
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00007:" Creating List directly with different types
8:let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
9:$put =string(l)
10:$put =string(l[-1])
11:$put =string(l[-4])
12:try
13: $put =string(l[-5])
14:catch
15: $put =v:exception[:14]
16:endtry
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000017:" List slices
18:$put =string(l[:])
19:$put =string(l[1:])
20:$put =string(l[:-2])
21:$put =string(l[0:8])
22:$put =string(l[8:-1])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000023:"
24:" List identity
25:let ll = l
26:let lx = copy(l)
27:try
28: $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
29:catch
30: $put =v:exception
31:endtry
32:"
33:" Creating Dictionary directly with different types
34:let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
35:$put =string(d) . d.1
36:$put =string(sort(keys(d)))
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000037:$put =string (values(d))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000038:for [key, val] in items(d)
39: $put =key . ':' . string(val)
40: unlet key val
41:endfor
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000042:call extend (d, {3:33, 1:99})
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000043:call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
44:try
45: call extend(d, {3:333,4:444}, "error")
46:catch
47: $put =v:exception[:15] . v:exception[-1:-1]
48:endtry
49:$put =string(d)
50:call filter(d, 'v:key =~ ''[ac391]''')
51:$put =string(d)
52:"
53:" Dictionary identity
54:let dd = d
55:let dx = copy(d)
56:try
57: $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
58:catch
59: $put =v:exception
60:endtry
61:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000062:"
63:" removing items with :unlet
64:unlet l[2]
65:$put =string(l)
66:let l = range(8)
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000067:try
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000068:unlet l[:3]
69:unlet l[1:]
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000070:catch
71:$put =v:exception
72:endtry
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000073:$put =string(l)
74:"
75:unlet d.c
76:unlet d[-1]
77:$put =string(d)
78:"
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000079:" removing items out of range: silently skip items that don't exist
80let l = [0, 1, 2, 3]
81:unlet l[2:1]
82:$put =string(l)
83let l = [0, 1, 2, 3]
84:unlet l[2:2]
85:$put =string(l)
86let l = [0, 1, 2, 3]
87:unlet l[2:3]
88:$put =string(l)
89let l = [0, 1, 2, 3]
90:unlet l[2:4]
91:$put =string(l)
92let l = [0, 1, 2, 3]
93:unlet l[2:5]
94:$put =string(l)
95let l = [0, 1, 2, 3]
96:unlet l[-1:2]
97:$put =string(l)
98let l = [0, 1, 2, 3]
99:unlet l[-2:2]
100:$put =string(l)
101let l = [0, 1, 2, 3]
102:unlet l[-3:2]
103:$put =string(l)
104let l = [0, 1, 2, 3]
105:unlet l[-4:2]
106:$put =string(l)
107let l = [0, 1, 2, 3]
108:unlet l[-5:2]
109:$put =string(l)
110let l = [0, 1, 2, 3]
111:unlet l[-6:2]
112:$put =string(l)
113:"
114:" assignment to a list
115:let l = [0, 1, 2, 3]
116:let [va, vb] = l[2:3]
117:$put =va
118:$put =vb
119:try
120: let [va, vb] = l
121:catch
122: $put =v:exception[:14]
123:endtry
124:try
125: let [va, vb] = l[1:1]
126:catch
127: $put =v:exception[:14]
128:endtry
129:"
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000130:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000131:let d = {}
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000132:for i in range(1500)
133: let d[i] = 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000134:endfor
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000135:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000136:try
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000137: let n = d[1500]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000138:catch
Bram Moolenaarc87841c2008-02-20 09:58:18 +0000139: $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000140:endtry
141:" lookup each items
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000142:for i in range(1500)
143: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000144: $put =d[i]
145: endif
146:endfor
147: let i += 1
148:" delete even items
149:while i >= 2
150: let i -= 2
151: unlet d[i]
152:endwhile
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000153:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000154:" delete odd items, checking value, one intentionally wrong
155:let d[33] = 999
156:let i = 1
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000157:while i < 1500
158: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000159: $put =i . '=' . d[i]
160: else
161: unlet d[i]
162: endif
163: let i += 2
164:endwhile
165:$put =string(d) " must be almost empty now
166:unlet d
167:"
168:" Dictionary function
169:let dict = {}
170:func dict.func(a) dict
171: $put =a:a . len(self.data)
172:endfunc
173:let dict.data = [1,2,3]
174:call dict.func("len: ")
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000175:let x = dict.func("again: ")
Bram Moolenaar3905e292016-03-19 18:44:08 +0100176:let Fn = dict.func
177:call Fn('xxx')
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000178:"
179:" Function in script-local List or Dict
180:let g:dict = {}
181:function g:dict.func() dict
182: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
183:endfunc
184:let g:dict.foo = ['-', 2, 3]
185:call insert(g:dict.foo, function('strlen'))
186:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000187:"
188:" Nasty: remove func from Dict that's being called (works)
189:let d = {1:1}
190:func d.func(a)
191: return "a:". a:a
192:endfunc
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000193:$put =d.func(string(remove(d, 'func')))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000194:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000195:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000196:let d = {1:1, 2:2}
197:let l = [4, d, 6]
198:let d[3] = l
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000199:let dc = deepcopy(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000200:try
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000201: let dc = deepcopy(d, 1)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000202:catch
203: $put =v:exception[:14]
204:endtry
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000205:let l2 = [0, l, l, 3]
206:let l[1] = l2
207:let l3 = deepcopy(l2)
208:$put ='same list: ' . (l3[1] is l3[2])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000209:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000210:" Locked variables
211:for depth in range(5)
212: $put ='depth is ' . depth
213: for u in range(3)
214: unlet l
215: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
216: exe "lockvar " . depth . " l"
217: if u == 1
218: exe "unlockvar l"
219: elseif u == 2
220: exe "unlockvar " . depth . " l"
221: endif
222: let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
223: $put =ps
224: let ps = ''
225: try
226: let l[1][1][0] = 99
227: let ps .= 'p'
228: catch
229: let ps .= 'F'
230: endtry
231: try
232: let l[1][1] = [99]
233: let ps .= 'p'
234: catch
235: let ps .= 'F'
236: endtry
237: try
238: let l[1] = [99]
239: let ps .= 'p'
240: catch
241: let ps .= 'F'
242: endtry
243: try
244: let l[2]['6'][7] = 99
245: let ps .= 'p'
246: catch
247: let ps .= 'F'
248: endtry
249: try
250: let l[2][6] = {99: 99}
251: let ps .= 'p'
252: catch
253: let ps .= 'F'
254: endtry
255: try
256: let l[2] = {99: 99}
257: let ps .= 'p'
258: catch
259: let ps .= 'F'
260: endtry
261: try
262: let l = [99]
263: let ps .= 'p'
264: catch
265: let ps .= 'F'
266: endtry
267: $put =ps
268: endfor
269:endfor
Bram Moolenaar9bc174b2015-04-13 16:16:38 +0200270:"
271:" Unletting locked variables
272:$put ='Unletting:'
273:for depth in range(5)
274: $put ='depth is ' . depth
275: for u in range(3)
276: unlet l
277: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
278: exe "lockvar " . depth . " l"
279: if u == 1
280: exe "unlockvar l"
281: elseif u == 2
282: exe "unlockvar " . depth . " l"
283: endif
284: let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
285: $put =ps
286: let ps = ''
287: try
288: unlet l[2]['6'][7]
289: let ps .= 'p'
290: catch
291: let ps .= 'F'
292: endtry
293: try
294: unlet l[2][6]
295: let ps .= 'p'
296: catch
297: let ps .= 'F'
298: endtry
299: try
300: unlet l[2]
301: let ps .= 'p'
302: catch
303: let ps .= 'F'
304: endtry
305: try
306: unlet l[1][1][0]
307: let ps .= 'p'
308: catch
309: let ps .= 'F'
310: endtry
311: try
312: unlet l[1][1]
313: let ps .= 'p'
314: catch
315: let ps .= 'F'
316: endtry
317: try
318: unlet l[1]
319: let ps .= 'p'
320: catch
321: let ps .= 'F'
322: endtry
323: try
324: unlet l
325: let ps .= 'p'
326: catch
327: let ps .= 'F'
328: endtry
329: $put =ps
330: endfor
331:endfor
332:"
333:" Locked variables and :unlet or list / dict functions
334:$put ='Locks and commands or functions:'
335:"
336:$put ='No :unlet after lock on dict:'
337:unlet! d
338:let d = {'a': 99, 'b': 100}
339:lockvar 1 d
340:try
341: unlet d.a
342: $put ='did :unlet'
343:catch
344: $put =v:exception[:16]
345:endtry
346:$put =string(d)
347:"
348:$put =':unlet after lock on dict item:'
349:unlet! d
350:let d = {'a': 99, 'b': 100}
351:lockvar d.a
352:try
353: unlet d.a
354: $put ='did :unlet'
355:catch
356: $put =v:exception[:16]
357:endtry
358:$put =string(d)
359:"
360:$put ='filter() after lock on dict item:'
361:unlet! d
362:let d = {'a': 99, 'b': 100}
363:lockvar d.a
364:try
365: call filter(d, 'v:key != "a"')
366: $put ='did filter()'
367:catch
368: $put =v:exception[:16]
369:endtry
370:$put =string(d)
371:"
372:$put ='map() after lock on dict:'
373:unlet! d
374:let d = {'a': 99, 'b': 100}
375:lockvar 1 d
376:try
377: call map(d, 'v:val + 200')
378: $put ='did map()'
379:catch
380: $put =v:exception[:16]
381:endtry
382:$put =string(d)
383:"
384:$put ='No extend() after lock on dict item:'
385:unlet! d
386:let d = {'a': 99, 'b': 100}
387:lockvar d.a
388:try
389: $put =string(extend(d, {'a': 123}))
390: $put ='did extend()'
391:catch
392: $put =v:exception[:14]
393:endtry
394:$put =string(d)
395:"
396:$put ='No remove() of write-protected scope-level variable:'
397:fun! Tfunc(this_is_a_loooooooooong_parameter_name)
398: try
399: $put =string(remove(a:, 'this_is_a_loooooooooong_parameter_name'))
400: $put ='did remove()'
401: catch
402: $put =v:exception[:14]
403: endtry
404:endfun
405:call Tfunc('testval')
406:"
407:$put ='No extend() of write-protected scope-level variable:'
408:fun! Tfunc(this_is_a_loooooooooong_parameter_name)
409: try
410: $put =string(extend(a:, {'this_is_a_loooooooooong_parameter_name': 1234}))
411: $put ='did extend()'
412: catch
413: $put =v:exception[:14]
414: endtry
415:endfun
416:call Tfunc('testval')
417:"
418:$put ='No :unlet of variable in locked scope:'
419:let b:testvar = 123
420:lockvar 1 b:
421:try
422: unlet b:testvar
423: $put ='b:testvar was :unlet: '. (!exists('b:testvar'))
424:catch
425: $put =v:exception[:16]
426:endtry
427:unlockvar 1 b:
428:unlet! b:testvar
429:"
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200430:$put ='No :let += of locked list variable:'
431:let l = ['a', 'b', 3]
432:lockvar 1 l
433:try
434: let l += ['x']
435: $put ='did :let +='
436:catch
437: $put =v:exception[:14]
438:endtry
439:$put =string(l)
440:"
Bram Moolenaarf2d912e2014-08-29 09:46:10 +0200441:unlet l
442:let l = [1, 2, 3, 4]
443:lockvar! l
444:$put =string(l)
445:unlockvar l[1]
446:unlet l[0:1]
447:$put =string(l)
448:unlet l[1:2]
449:$put =string(l)
450:unlockvar l[1]
451:let l[0:1] = [0, 1]
452:$put =string(l)
453:let l[1:2] = [0, 1]
454:$put =string(l)
455:unlet l
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100456:" :lockvar/islocked() triggering script autoloading
457:set rtp+=./sautest
458:lockvar g:footest#x
459:unlockvar g:footest#x
460:$put ='locked g:footest#x:'.islocked('g:footest#x')
461:$put ='exists g:footest#x:'.exists('g:footest#x')
462:$put ='g:footest#x: '.g:footest#x
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000463:"
464:" a:000 function argument
465:" first the tests that should fail
466:try
467: let a:000 = [1, 2]
468:catch
469: $put ='caught a:000'
470:endtry
471:try
472: let a:000[0] = 9
473:catch
474: $put ='caught a:000[0]'
475:endtry
476:try
477: let a:000[2] = [9, 10]
478:catch
479: $put ='caught a:000[2]'
480:endtry
481:try
482: let a:000[3] = {9: 10}
483:catch
484: $put ='caught a:000[3]'
485:endtry
486:" now the tests that should pass
487:try
488: let a:000[2][1] = 9
489: call extend(a:000[2], [5, 6])
490: let a:000[3][5] = 8
491: let a:000[3]['a'] = 12
492: $put =string(a:000)
493:catch
494: $put ='caught ' . v:exception
495:endtry
496:"
Bram Moolenaar327aa022014-03-25 18:24:23 +0100497:" reverse(), sort(), uniq()
498:let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
499:$put =string(uniq(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000500:$put =string(reverse(l))
501:$put =string(reverse(reverse(l)))
502:$put =string(sort(l))
503:$put =string(reverse(sort(l)))
504:$put =string(sort(reverse(sort(l))))
Bram Moolenaar327aa022014-03-25 18:24:23 +0100505:$put =string(uniq(sort(l)))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200506:let l=[7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
Bram Moolenaare8a34922014-06-25 17:31:09 +0200507:$put =string(sort(copy(l), 'n'))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200508:let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
Bram Moolenaare8a34922014-06-25 17:31:09 +0200509:$put =string(sort(copy(l), 1))
510:$put =string(sort(copy(l), 'i'))
511:$put =string(sort(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000512:"
Bram Moolenaarde934d72005-05-22 22:09:40 +0000513:" splitting a string to a List
514:$put =string(split(' aa bb '))
515:$put =string(split(' aa bb ', '\W\+', 0))
516:$put =string(split(' aa bb ', '\W\+', 1))
517:$put =string(split(' aa bb ', '\W', 1))
518:$put =string(split(':aa::bb:', ':', 0))
519:$put =string(split(':aa::bb:', ':', 1))
520:$put =string(split('aa,,bb, cc,', ',\s*', 1))
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000521:$put =string(split('abc', '\zs'))
522:$put =string(split('abc', '\zs', 1))
Bram Moolenaarde934d72005-05-22 22:09:40 +0000523:"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000524:" compare recursively linked list and dict
525:let l = [1, 2, 3, 4]
526:let d = {'1': 1, '2': l, '3': 3}
527:let l[1] = d
528:$put =(l == l)
529:$put =(d == d)
530:$put =(l != deepcopy(l))
531:$put =(d != deepcopy(d))
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100532:"
533:" compare complex recursively linked list and dict
534:let l = []
535:call add(l, l)
536:let dict4 = {"l": l}
537:call add(dict4.l, dict4)
538:let lcopy = deepcopy(l)
539:let dict4copy = deepcopy(dict4)
540:$put =(l == lcopy)
541:$put =(dict4 == dict4copy)
Bram Moolenaar2fc88022012-05-18 12:07:05 +0200542:"
543:" Pass the same List to extend()
544:let l = [1, 2, 3, 4, 5]
545:call extend(l, l)
546:$put =string(l)
547:"
548:" Pass the same Dict to extend()
549:let d = { 'a': {'b': 'B'}}
550:call extend(d, d)
551:$put =string(d)
552:"
553:" Pass the same Dict to extend() with "error"
554:try
555: call extend(d, d, "error")
556:catch
557: $put =v:exception[:15] . v:exception[-1:-1]
558:endtry
559:$put =string(d)
Bram Moolenaarb2a851f2014-12-07 00:18:33 +0100560:"
561:" test for range assign
562:let l = [0]
563:let l[:] = [1, 2]
564:$put =string(l)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000565:endfun
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100566:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000567:call Test(1, 2, [3, 4], {5: 6}) " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000568:"
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +0000569:delfunc Test
570:unlet dict
571:call garbagecollect(1)
572:"
Bram Moolenaardd7d8462012-08-29 16:55:13 +0200573:" test for patch 7.3.637
574:let a = 'No error caught'
575:try|foldopen|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
576o=a :"
577:lang C
578:redir => a
579:try|foobar|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
580:redir END
581o=a :"
582:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000583:/^start:/,$wq! test.out
584ENDTEST
585
586start: