blob: 635b35fc605f5223151b6c6c27db9130707a519e [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 Moolenaar383f9bc2005-01-19 22:18:32 +0000176:try
177: let Fn = dict.func
178: call Fn('xxx')
179:catch
180: $put =v:exception[:15]
181:endtry
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000182:"
183:" Function in script-local List or Dict
184:let g:dict = {}
185:function g:dict.func() dict
186: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
187:endfunc
188:let g:dict.foo = ['-', 2, 3]
189:call insert(g:dict.foo, function('strlen'))
190:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000191:"
192:" Nasty: remove func from Dict that's being called (works)
193:let d = {1:1}
194:func d.func(a)
195: return "a:". a:a
196:endfunc
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000197:$put =d.func(string(remove(d, 'func')))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000198:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000199:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000200:let d = {1:1, 2:2}
201:let l = [4, d, 6]
202:let d[3] = l
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000203:let dc = deepcopy(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000204:try
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000205: let dc = deepcopy(d, 1)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000206:catch
207: $put =v:exception[:14]
208:endtry
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000209:let l2 = [0, l, l, 3]
210:let l[1] = l2
211:let l3 = deepcopy(l2)
212:$put ='same list: ' . (l3[1] is l3[2])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000213:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000214:" Locked variables
215:for depth in range(5)
216: $put ='depth is ' . depth
217: for u in range(3)
218: unlet l
219: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
220: exe "lockvar " . depth . " l"
221: if u == 1
222: exe "unlockvar l"
223: elseif u == 2
224: exe "unlockvar " . depth . " l"
225: endif
226: 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]")
227: $put =ps
228: let ps = ''
229: try
230: let l[1][1][0] = 99
231: let ps .= 'p'
232: catch
233: let ps .= 'F'
234: endtry
235: try
236: let l[1][1] = [99]
237: let ps .= 'p'
238: catch
239: let ps .= 'F'
240: endtry
241: try
242: let l[1] = [99]
243: let ps .= 'p'
244: catch
245: let ps .= 'F'
246: endtry
247: try
248: let l[2]['6'][7] = 99
249: let ps .= 'p'
250: catch
251: let ps .= 'F'
252: endtry
253: try
254: let l[2][6] = {99: 99}
255: let ps .= 'p'
256: catch
257: let ps .= 'F'
258: endtry
259: try
260: let l[2] = {99: 99}
261: let ps .= 'p'
262: catch
263: let ps .= 'F'
264: endtry
265: try
266: let l = [99]
267: let ps .= 'p'
268: catch
269: let ps .= 'F'
270: endtry
271: $put =ps
272: endfor
273:endfor
Bram Moolenaar9bc174b2015-04-13 16:16:38 +0200274:"
275:" Unletting locked variables
276:$put ='Unletting:'
277:for depth in range(5)
278: $put ='depth is ' . depth
279: for u in range(3)
280: unlet l
281: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
282: exe "lockvar " . depth . " l"
283: if u == 1
284: exe "unlockvar l"
285: elseif u == 2
286: exe "unlockvar " . depth . " l"
287: endif
288: 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]")
289: $put =ps
290: let ps = ''
291: try
292: unlet l[2]['6'][7]
293: let ps .= 'p'
294: catch
295: let ps .= 'F'
296: endtry
297: try
298: unlet l[2][6]
299: let ps .= 'p'
300: catch
301: let ps .= 'F'
302: endtry
303: try
304: unlet l[2]
305: let ps .= 'p'
306: catch
307: let ps .= 'F'
308: endtry
309: try
310: unlet l[1][1][0]
311: let ps .= 'p'
312: catch
313: let ps .= 'F'
314: endtry
315: try
316: unlet l[1][1]
317: let ps .= 'p'
318: catch
319: let ps .= 'F'
320: endtry
321: try
322: unlet l[1]
323: let ps .= 'p'
324: catch
325: let ps .= 'F'
326: endtry
327: try
328: unlet l
329: let ps .= 'p'
330: catch
331: let ps .= 'F'
332: endtry
333: $put =ps
334: endfor
335:endfor
336:"
337:" Locked variables and :unlet or list / dict functions
338:$put ='Locks and commands or functions:'
339:"
340:$put ='No :unlet after lock on dict:'
341:unlet! d
342:let d = {'a': 99, 'b': 100}
343:lockvar 1 d
344:try
345: unlet d.a
346: $put ='did :unlet'
347:catch
348: $put =v:exception[:16]
349:endtry
350:$put =string(d)
351:"
352:$put =':unlet after lock on dict item:'
353:unlet! d
354:let d = {'a': 99, 'b': 100}
355:lockvar d.a
356:try
357: unlet d.a
358: $put ='did :unlet'
359:catch
360: $put =v:exception[:16]
361:endtry
362:$put =string(d)
363:"
364:$put ='filter() after lock on dict item:'
365:unlet! d
366:let d = {'a': 99, 'b': 100}
367:lockvar d.a
368:try
369: call filter(d, 'v:key != "a"')
370: $put ='did filter()'
371:catch
372: $put =v:exception[:16]
373:endtry
374:$put =string(d)
375:"
376:$put ='map() after lock on dict:'
377:unlet! d
378:let d = {'a': 99, 'b': 100}
379:lockvar 1 d
380:try
381: call map(d, 'v:val + 200')
382: $put ='did map()'
383:catch
384: $put =v:exception[:16]
385:endtry
386:$put =string(d)
387:"
388:$put ='No extend() after lock on dict item:'
389:unlet! d
390:let d = {'a': 99, 'b': 100}
391:lockvar d.a
392:try
393: $put =string(extend(d, {'a': 123}))
394: $put ='did extend()'
395:catch
396: $put =v:exception[:14]
397:endtry
398:$put =string(d)
399:"
400:$put ='No remove() of write-protected scope-level variable:'
401:fun! Tfunc(this_is_a_loooooooooong_parameter_name)
402: try
403: $put =string(remove(a:, 'this_is_a_loooooooooong_parameter_name'))
404: $put ='did remove()'
405: catch
406: $put =v:exception[:14]
407: endtry
408:endfun
409:call Tfunc('testval')
410:"
411:$put ='No extend() of write-protected scope-level variable:'
412:fun! Tfunc(this_is_a_loooooooooong_parameter_name)
413: try
414: $put =string(extend(a:, {'this_is_a_loooooooooong_parameter_name': 1234}))
415: $put ='did extend()'
416: catch
417: $put =v:exception[:14]
418: endtry
419:endfun
420:call Tfunc('testval')
421:"
422:$put ='No :unlet of variable in locked scope:'
423:let b:testvar = 123
424:lockvar 1 b:
425:try
426: unlet b:testvar
427: $put ='b:testvar was :unlet: '. (!exists('b:testvar'))
428:catch
429: $put =v:exception[:16]
430:endtry
431:unlockvar 1 b:
432:unlet! b:testvar
433:"
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200434:$put ='No :let += of locked list variable:'
435:let l = ['a', 'b', 3]
436:lockvar 1 l
437:try
438: let l += ['x']
439: $put ='did :let +='
440:catch
441: $put =v:exception[:14]
442:endtry
443:$put =string(l)
444:"
Bram Moolenaarf2d912e2014-08-29 09:46:10 +0200445:unlet l
446:let l = [1, 2, 3, 4]
447:lockvar! l
448:$put =string(l)
449:unlockvar l[1]
450:unlet l[0:1]
451:$put =string(l)
452:unlet l[1:2]
453:$put =string(l)
454:unlockvar l[1]
455:let l[0:1] = [0, 1]
456:$put =string(l)
457:let l[1:2] = [0, 1]
458:$put =string(l)
459:unlet l
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100460:" :lockvar/islocked() triggering script autoloading
461:set rtp+=./sautest
462:lockvar g:footest#x
463:unlockvar g:footest#x
464:$put ='locked g:footest#x:'.islocked('g:footest#x')
465:$put ='exists g:footest#x:'.exists('g:footest#x')
466:$put ='g:footest#x: '.g:footest#x
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000467:"
468:" a:000 function argument
469:" first the tests that should fail
470:try
471: let a:000 = [1, 2]
472:catch
473: $put ='caught a:000'
474:endtry
475:try
476: let a:000[0] = 9
477:catch
478: $put ='caught a:000[0]'
479:endtry
480:try
481: let a:000[2] = [9, 10]
482:catch
483: $put ='caught a:000[2]'
484:endtry
485:try
486: let a:000[3] = {9: 10}
487:catch
488: $put ='caught a:000[3]'
489:endtry
490:" now the tests that should pass
491:try
492: let a:000[2][1] = 9
493: call extend(a:000[2], [5, 6])
494: let a:000[3][5] = 8
495: let a:000[3]['a'] = 12
496: $put =string(a:000)
497:catch
498: $put ='caught ' . v:exception
499:endtry
500:"
Bram Moolenaar327aa022014-03-25 18:24:23 +0100501:" reverse(), sort(), uniq()
502:let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
503:$put =string(uniq(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000504:$put =string(reverse(l))
505:$put =string(reverse(reverse(l)))
506:$put =string(sort(l))
507:$put =string(reverse(sort(l)))
508:$put =string(sort(reverse(sort(l))))
Bram Moolenaar327aa022014-03-25 18:24:23 +0100509:$put =string(uniq(sort(l)))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200510: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 +0200511:$put =string(sort(copy(l), 'n'))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200512: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 +0200513:$put =string(sort(copy(l), 1))
514:$put =string(sort(copy(l), 'i'))
515:$put =string(sort(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000516:"
Bram Moolenaarde934d72005-05-22 22:09:40 +0000517:" splitting a string to a List
518:$put =string(split(' aa bb '))
519:$put =string(split(' aa bb ', '\W\+', 0))
520:$put =string(split(' aa bb ', '\W\+', 1))
521:$put =string(split(' aa bb ', '\W', 1))
522:$put =string(split(':aa::bb:', ':', 0))
523:$put =string(split(':aa::bb:', ':', 1))
524:$put =string(split('aa,,bb, cc,', ',\s*', 1))
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000525:$put =string(split('abc', '\zs'))
526:$put =string(split('abc', '\zs', 1))
Bram Moolenaarde934d72005-05-22 22:09:40 +0000527:"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000528:" compare recursively linked list and dict
529:let l = [1, 2, 3, 4]
530:let d = {'1': 1, '2': l, '3': 3}
531:let l[1] = d
532:$put =(l == l)
533:$put =(d == d)
534:$put =(l != deepcopy(l))
535:$put =(d != deepcopy(d))
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100536:"
537:" compare complex recursively linked list and dict
538:let l = []
539:call add(l, l)
540:let dict4 = {"l": l}
541:call add(dict4.l, dict4)
542:let lcopy = deepcopy(l)
543:let dict4copy = deepcopy(dict4)
544:$put =(l == lcopy)
545:$put =(dict4 == dict4copy)
Bram Moolenaar2fc88022012-05-18 12:07:05 +0200546:"
547:" Pass the same List to extend()
548:let l = [1, 2, 3, 4, 5]
549:call extend(l, l)
550:$put =string(l)
551:"
552:" Pass the same Dict to extend()
553:let d = { 'a': {'b': 'B'}}
554:call extend(d, d)
555:$put =string(d)
556:"
557:" Pass the same Dict to extend() with "error"
558:try
559: call extend(d, d, "error")
560:catch
561: $put =v:exception[:15] . v:exception[-1:-1]
562:endtry
563:$put =string(d)
Bram Moolenaarb2a851f2014-12-07 00:18:33 +0100564:"
565:" test for range assign
566:let l = [0]
567:let l[:] = [1, 2]
568:$put =string(l)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000569:endfun
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100570:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000571:call Test(1, 2, [3, 4], {5: 6}) " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000572:"
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +0000573:delfunc Test
574:unlet dict
575:call garbagecollect(1)
576:"
Bram Moolenaardd7d8462012-08-29 16:55:13 +0200577:" test for patch 7.3.637
578:let a = 'No error caught'
579:try|foldopen|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
580o=a :"
581:lang C
582:redir => a
583:try|foobar|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
584:redir END
585o=a :"
586:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000587:/^start:/,$wq! test.out
588ENDTEST
589
590start: