blob: 140cb7c0e5ba1b27badc47f7b2858b35c47721f0 [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:"
62:" Changing var type should fail
63:try
64: let d = []
65:catch
66: $put =v:exception[:14] . v:exception[-1:-1]
67:endtry
68:try
69: let l = {}
70:catch
71: $put =v:exception[:14] . v:exception[-1:-1]
72:endtry
73:"
74:" removing items with :unlet
75:unlet l[2]
76:$put =string(l)
77:let l = range(8)
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000078:try
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000079:unlet l[:3]
80:unlet l[1:]
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000081:catch
82:$put =v:exception
83:endtry
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000084:$put =string(l)
85:"
86:unlet d.c
87:unlet d[-1]
88:$put =string(d)
89:"
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000090:" removing items out of range: silently skip items that don't exist
91let l = [0, 1, 2, 3]
92:unlet l[2:1]
93:$put =string(l)
94let l = [0, 1, 2, 3]
95:unlet l[2:2]
96:$put =string(l)
97let l = [0, 1, 2, 3]
98:unlet l[2:3]
99:$put =string(l)
100let l = [0, 1, 2, 3]
101:unlet l[2:4]
102:$put =string(l)
103let l = [0, 1, 2, 3]
104:unlet l[2:5]
105:$put =string(l)
106let l = [0, 1, 2, 3]
107:unlet l[-1:2]
108:$put =string(l)
109let l = [0, 1, 2, 3]
110:unlet l[-2:2]
111:$put =string(l)
112let l = [0, 1, 2, 3]
113:unlet l[-3:2]
114:$put =string(l)
115let l = [0, 1, 2, 3]
116:unlet l[-4:2]
117:$put =string(l)
118let l = [0, 1, 2, 3]
119:unlet l[-5:2]
120:$put =string(l)
121let l = [0, 1, 2, 3]
122:unlet l[-6:2]
123:$put =string(l)
124:"
125:" assignment to a list
126:let l = [0, 1, 2, 3]
127:let [va, vb] = l[2:3]
128:$put =va
129:$put =vb
130:try
131: let [va, vb] = l
132:catch
133: $put =v:exception[:14]
134:endtry
135:try
136: let [va, vb] = l[1:1]
137:catch
138: $put =v:exception[:14]
139:endtry
140:"
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000141:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000142:let d = {}
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000143:for i in range(1500)
144: let d[i] = 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000145:endfor
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000146:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000147:try
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000148: let n = d[1500]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000149:catch
Bram Moolenaarc87841c2008-02-20 09:58:18 +0000150: $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000151:endtry
152:" lookup each items
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000153:for i in range(1500)
154: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000155: $put =d[i]
156: endif
157:endfor
158: let i += 1
159:" delete even items
160:while i >= 2
161: let i -= 2
162: unlet d[i]
163:endwhile
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000164:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000165:" delete odd items, checking value, one intentionally wrong
166:let d[33] = 999
167:let i = 1
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000168:while i < 1500
169: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000170: $put =i . '=' . d[i]
171: else
172: unlet d[i]
173: endif
174: let i += 2
175:endwhile
176:$put =string(d) " must be almost empty now
177:unlet d
178:"
179:" Dictionary function
180:let dict = {}
181:func dict.func(a) dict
182: $put =a:a . len(self.data)
183:endfunc
184:let dict.data = [1,2,3]
185:call dict.func("len: ")
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000186:let x = dict.func("again: ")
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000187:try
188: let Fn = dict.func
189: call Fn('xxx')
190:catch
191: $put =v:exception[:15]
192:endtry
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000193:"
194:" Function in script-local List or Dict
195:let g:dict = {}
196:function g:dict.func() dict
197: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
198:endfunc
199:let g:dict.foo = ['-', 2, 3]
200:call insert(g:dict.foo, function('strlen'))
201:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000202:"
203:" Nasty: remove func from Dict that's being called (works)
204:let d = {1:1}
205:func d.func(a)
206: return "a:". a:a
207:endfunc
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000208:$put =d.func(string(remove(d, 'func')))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000209:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000210:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000211:let d = {1:1, 2:2}
212:let l = [4, d, 6]
213:let d[3] = l
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000214:let dc = deepcopy(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000215:try
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000216: let dc = deepcopy(d, 1)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000217:catch
218: $put =v:exception[:14]
219:endtry
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000220:let l2 = [0, l, l, 3]
221:let l[1] = l2
222:let l3 = deepcopy(l2)
223:$put ='same list: ' . (l3[1] is l3[2])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000224:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000225:" Locked variables
226:for depth in range(5)
227: $put ='depth is ' . depth
228: for u in range(3)
229: unlet l
230: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
231: exe "lockvar " . depth . " l"
232: if u == 1
233: exe "unlockvar l"
234: elseif u == 2
235: exe "unlockvar " . depth . " l"
236: endif
237: 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]")
238: $put =ps
239: let ps = ''
240: try
241: let l[1][1][0] = 99
242: let ps .= 'p'
243: catch
244: let ps .= 'F'
245: endtry
246: try
247: let l[1][1] = [99]
248: let ps .= 'p'
249: catch
250: let ps .= 'F'
251: endtry
252: try
253: let l[1] = [99]
254: let ps .= 'p'
255: catch
256: let ps .= 'F'
257: endtry
258: try
259: let l[2]['6'][7] = 99
260: let ps .= 'p'
261: catch
262: let ps .= 'F'
263: endtry
264: try
265: let l[2][6] = {99: 99}
266: let ps .= 'p'
267: catch
268: let ps .= 'F'
269: endtry
270: try
271: let l[2] = {99: 99}
272: let ps .= 'p'
273: catch
274: let ps .= 'F'
275: endtry
276: try
277: let l = [99]
278: let ps .= 'p'
279: catch
280: let ps .= 'F'
281: endtry
282: $put =ps
283: endfor
284:endfor
Bram Moolenaarf2d912e2014-08-29 09:46:10 +0200285:unlet l
286:let l = [1, 2, 3, 4]
287:lockvar! l
288:$put =string(l)
289:unlockvar l[1]
290:unlet l[0:1]
291:$put =string(l)
292:unlet l[1:2]
293:$put =string(l)
294:unlockvar l[1]
295:let l[0:1] = [0, 1]
296:$put =string(l)
297:let l[1:2] = [0, 1]
298:$put =string(l)
299:unlet l
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100300:" :lockvar/islocked() triggering script autoloading
301:set rtp+=./sautest
302:lockvar g:footest#x
303:unlockvar g:footest#x
304:$put ='locked g:footest#x:'.islocked('g:footest#x')
305:$put ='exists g:footest#x:'.exists('g:footest#x')
306:$put ='g:footest#x: '.g:footest#x
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000307:"
308:" a:000 function argument
309:" first the tests that should fail
310:try
311: let a:000 = [1, 2]
312:catch
313: $put ='caught a:000'
314:endtry
315:try
316: let a:000[0] = 9
317:catch
318: $put ='caught a:000[0]'
319:endtry
320:try
321: let a:000[2] = [9, 10]
322:catch
323: $put ='caught a:000[2]'
324:endtry
325:try
326: let a:000[3] = {9: 10}
327:catch
328: $put ='caught a:000[3]'
329:endtry
330:" now the tests that should pass
331:try
332: let a:000[2][1] = 9
333: call extend(a:000[2], [5, 6])
334: let a:000[3][5] = 8
335: let a:000[3]['a'] = 12
336: $put =string(a:000)
337:catch
338: $put ='caught ' . v:exception
339:endtry
340:"
Bram Moolenaar327aa022014-03-25 18:24:23 +0100341:" reverse(), sort(), uniq()
342:let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
343:$put =string(uniq(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000344:$put =string(reverse(l))
345:$put =string(reverse(reverse(l)))
346:$put =string(sort(l))
347:$put =string(reverse(sort(l)))
348:$put =string(sort(reverse(sort(l))))
Bram Moolenaar327aa022014-03-25 18:24:23 +0100349:$put =string(uniq(sort(l)))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200350: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 +0200351:$put =string(sort(copy(l), 'n'))
Bram Moolenaarc35e3de2014-07-02 19:06:18 +0200352: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 +0200353:$put =string(sort(copy(l), 1))
354:$put =string(sort(copy(l), 'i'))
355:$put =string(sort(copy(l)))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000356:"
Bram Moolenaarde934d72005-05-22 22:09:40 +0000357:" splitting a string to a List
358:$put =string(split(' aa bb '))
359:$put =string(split(' aa bb ', '\W\+', 0))
360:$put =string(split(' aa bb ', '\W\+', 1))
361:$put =string(split(' aa bb ', '\W', 1))
362:$put =string(split(':aa::bb:', ':', 0))
363:$put =string(split(':aa::bb:', ':', 1))
364:$put =string(split('aa,,bb, cc,', ',\s*', 1))
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000365:$put =string(split('abc', '\zs'))
366:$put =string(split('abc', '\zs', 1))
Bram Moolenaarde934d72005-05-22 22:09:40 +0000367:"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000368:" compare recursively linked list and dict
369:let l = [1, 2, 3, 4]
370:let d = {'1': 1, '2': l, '3': 3}
371:let l[1] = d
372:$put =(l == l)
373:$put =(d == d)
374:$put =(l != deepcopy(l))
375:$put =(d != deepcopy(d))
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100376:"
377:" compare complex recursively linked list and dict
378:let l = []
379:call add(l, l)
380:let dict4 = {"l": l}
381:call add(dict4.l, dict4)
382:let lcopy = deepcopy(l)
383:let dict4copy = deepcopy(dict4)
384:$put =(l == lcopy)
385:$put =(dict4 == dict4copy)
Bram Moolenaar2fc88022012-05-18 12:07:05 +0200386:"
387:" Pass the same List to extend()
388:let l = [1, 2, 3, 4, 5]
389:call extend(l, l)
390:$put =string(l)
391:"
392:" Pass the same Dict to extend()
393:let d = { 'a': {'b': 'B'}}
394:call extend(d, d)
395:$put =string(d)
396:"
397:" Pass the same Dict to extend() with "error"
398:try
399: call extend(d, d, "error")
400:catch
401: $put =v:exception[:15] . v:exception[-1:-1]
402:endtry
403:$put =string(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000404:endfun
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100405:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000406:call Test(1, 2, [3, 4], {5: 6}) " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000407:"
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +0000408:delfunc Test
409:unlet dict
410:call garbagecollect(1)
411:"
Bram Moolenaardd7d8462012-08-29 16:55:13 +0200412:" test for patch 7.3.637
413:let a = 'No error caught'
414:try|foldopen|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
415o=a :"
416:lang C
417:redir => a
418:try|foobar|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
419:redir END
420o=a :"
421:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000422:/^start:/,$wq! test.out
423ENDTEST
424
425start: