blob: d7cd4d91ad84cd236c8a6e5b511081cdc0d45903 [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 Moolenaar383f9bc2005-01-19 22:18:32 +00006:" Creating List directly with different types
7:let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
8:$put =string(l)
9:$put =string(l[-1])
10:$put =string(l[-4])
11:try
12: $put =string(l[-5])
13:catch
14: $put =v:exception[:14]
15:endtry
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016:" List slices
17:$put =string(l[:])
18:$put =string(l[1:])
19:$put =string(l[:-2])
20:$put =string(l[0:8])
21:$put =string(l[8:-1])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000022:"
23:" List identity
24:let ll = l
25:let lx = copy(l)
26:try
27: $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
28:catch
29: $put =v:exception
30:endtry
31:"
32:" Creating Dictionary directly with different types
33:let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
34:$put =string(d) . d.1
35:$put =string(sort(keys(d)))
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000036:$put =string (values(d))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000037:for [key, val] in items(d)
38: $put =key . ':' . string(val)
39: unlet key val
40:endfor
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000041:call extend (d, {3:33, 1:99})
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000042:call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
43:try
44: call extend(d, {3:333,4:444}, "error")
45:catch
46: $put =v:exception[:15] . v:exception[-1:-1]
47:endtry
48:$put =string(d)
49:call filter(d, 'v:key =~ ''[ac391]''')
50:$put =string(d)
51:"
52:" Dictionary identity
53:let dd = d
54:let dx = copy(d)
55:try
56: $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
57:catch
58: $put =v:exception
59:endtry
60:"
61:" Changing var type should fail
62:try
63: let d = []
64:catch
65: $put =v:exception[:14] . v:exception[-1:-1]
66:endtry
67:try
68: let l = {}
69:catch
70: $put =v:exception[:14] . v:exception[-1:-1]
71:endtry
72:"
73:" removing items with :unlet
74:unlet l[2]
75:$put =string(l)
76:let l = range(8)
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000077:try
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000078:unlet l[:3]
79:unlet l[1:]
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000080:catch
81:$put =v:exception
82:endtry
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000083:$put =string(l)
84:"
85:unlet d.c
86:unlet d[-1]
87:$put =string(d)
88:"
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000089:" removing items out of range: silently skip items that don't exist
90let l = [0, 1, 2, 3]
91:unlet l[2:1]
92:$put =string(l)
93let l = [0, 1, 2, 3]
94:unlet l[2:2]
95:$put =string(l)
96let l = [0, 1, 2, 3]
97:unlet l[2:3]
98:$put =string(l)
99let l = [0, 1, 2, 3]
100:unlet l[2:4]
101:$put =string(l)
102let l = [0, 1, 2, 3]
103:unlet l[2:5]
104:$put =string(l)
105let l = [0, 1, 2, 3]
106:unlet l[-1:2]
107:$put =string(l)
108let l = [0, 1, 2, 3]
109:unlet l[-2:2]
110:$put =string(l)
111let l = [0, 1, 2, 3]
112:unlet l[-3:2]
113:$put =string(l)
114let l = [0, 1, 2, 3]
115:unlet l[-4:2]
116:$put =string(l)
117let l = [0, 1, 2, 3]
118:unlet l[-5:2]
119:$put =string(l)
120let l = [0, 1, 2, 3]
121:unlet l[-6:2]
122:$put =string(l)
123:"
124:" assignment to a list
125:let l = [0, 1, 2, 3]
126:let [va, vb] = l[2:3]
127:$put =va
128:$put =vb
129:try
130: let [va, vb] = l
131:catch
132: $put =v:exception[:14]
133:endtry
134:try
135: let [va, vb] = l[1:1]
136:catch
137: $put =v:exception[:14]
138:endtry
139:"
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000140:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000141:let d = {}
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000142:for i in range(1500)
143: let d[i] = 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000144:endfor
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000145:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000146:try
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000147: let n = d[1500]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000148:catch
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000149: $put =v:exception[:14] . v:exception[-4:-1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000150:endtry
151:" lookup each items
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000152:for i in range(1500)
153: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000154: $put =d[i]
155: endif
156:endfor
157: let i += 1
158:" delete even items
159:while i >= 2
160: let i -= 2
161: unlet d[i]
162:endwhile
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000163:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000164:" delete odd items, checking value, one intentionally wrong
165:let d[33] = 999
166:let i = 1
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000167:while i < 1500
168: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000169: $put =i . '=' . d[i]
170: else
171: unlet d[i]
172: endif
173: let i += 2
174:endwhile
175:$put =string(d) " must be almost empty now
176:unlet d
177:"
178:" Dictionary function
179:let dict = {}
180:func dict.func(a) dict
181: $put =a:a . len(self.data)
182:endfunc
183:let dict.data = [1,2,3]
184:call dict.func("len: ")
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000185:let x = dict.func("again: ")
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000186:try
187: let Fn = dict.func
188: call Fn('xxx')
189:catch
190: $put =v:exception[:15]
191:endtry
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000192:"
193:" Function in script-local List or Dict
194:let g:dict = {}
195:function g:dict.func() dict
196: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
197:endfunc
198:let g:dict.foo = ['-', 2, 3]
199:call insert(g:dict.foo, function('strlen'))
200:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000201:"
202:" Nasty: remove func from Dict that's being called (works)
203:let d = {1:1}
204:func d.func(a)
205: return "a:". a:a
206:endfunc
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000207:$put =d.func(string(remove(d, 'func')))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000208:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000209:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000210:let d = {1:1, 2:2}
211:let l = [4, d, 6]
212:let d[3] = l
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000213:let dc = deepcopy(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000214:try
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000215: let dc = deepcopy(d, 1)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000216:catch
217: $put =v:exception[:14]
218:endtry
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000219:let l2 = [0, l, l, 3]
220:let l[1] = l2
221:let l3 = deepcopy(l2)
222:$put ='same list: ' . (l3[1] is l3[2])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000223:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000224:" Locked variables
225:for depth in range(5)
226: $put ='depth is ' . depth
227: for u in range(3)
228: unlet l
229: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
230: exe "lockvar " . depth . " l"
231: if u == 1
232: exe "unlockvar l"
233: elseif u == 2
234: exe "unlockvar " . depth . " l"
235: endif
236: 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]")
237: $put =ps
238: let ps = ''
239: try
240: let l[1][1][0] = 99
241: let ps .= 'p'
242: catch
243: let ps .= 'F'
244: endtry
245: try
246: let l[1][1] = [99]
247: let ps .= 'p'
248: catch
249: let ps .= 'F'
250: endtry
251: try
252: let l[1] = [99]
253: let ps .= 'p'
254: catch
255: let ps .= 'F'
256: endtry
257: try
258: let l[2]['6'][7] = 99
259: let ps .= 'p'
260: catch
261: let ps .= 'F'
262: endtry
263: try
264: let l[2][6] = {99: 99}
265: let ps .= 'p'
266: catch
267: let ps .= 'F'
268: endtry
269: try
270: let l[2] = {99: 99}
271: let ps .= 'p'
272: catch
273: let ps .= 'F'
274: endtry
275: try
276: let l = [99]
277: let ps .= 'p'
278: catch
279: let ps .= 'F'
280: endtry
281: $put =ps
282: endfor
283:endfor
284:"
285:" a:000 function argument
286:" first the tests that should fail
287:try
288: let a:000 = [1, 2]
289:catch
290: $put ='caught a:000'
291:endtry
292:try
293: let a:000[0] = 9
294:catch
295: $put ='caught a:000[0]'
296:endtry
297:try
298: let a:000[2] = [9, 10]
299:catch
300: $put ='caught a:000[2]'
301:endtry
302:try
303: let a:000[3] = {9: 10}
304:catch
305: $put ='caught a:000[3]'
306:endtry
307:" now the tests that should pass
308:try
309: let a:000[2][1] = 9
310: call extend(a:000[2], [5, 6])
311: let a:000[3][5] = 8
312: let a:000[3]['a'] = 12
313: $put =string(a:000)
314:catch
315: $put ='caught ' . v:exception
316:endtry
317:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000318:" reverse() and sort()
319:let l = ['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', [0, 1, 2], 'x8']
320:$put =string(reverse(l))
321:$put =string(reverse(reverse(l)))
322:$put =string(sort(l))
323:$put =string(reverse(sort(l)))
324:$put =string(sort(reverse(sort(l))))
325:"
Bram Moolenaarde934d72005-05-22 22:09:40 +0000326:" splitting a string to a List
327:$put =string(split(' aa bb '))
328:$put =string(split(' aa bb ', '\W\+', 0))
329:$put =string(split(' aa bb ', '\W\+', 1))
330:$put =string(split(' aa bb ', '\W', 1))
331:$put =string(split(':aa::bb:', ':', 0))
332:$put =string(split(':aa::bb:', ':', 1))
333:$put =string(split('aa,,bb, cc,', ',\s*', 1))
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000334:$put =string(split('abc', '\zs'))
335:$put =string(split('abc', '\zs', 1))
Bram Moolenaarde934d72005-05-22 22:09:40 +0000336:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000337:endfun
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000338:call Test(1, 2, [3, 4], {5: 6}) " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000339:"
340:/^start:/,$wq! test.out
341ENDTEST
342
343start: