1 Tests for List and Dictionary types. vim: set ft=vim :
6 :" Creating List directly with different types
7 :let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
14 : $put =v:exception[:14]
21 :$put =string(l[8:-1])
27 : $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
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)))
36 :$put =string (values(d))
37 :for [key, val] in items(d)
38 : $put =key . ':' . string(val)
41 :call extend (d, {3:33, 1:99})
42 :call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
44 : call extend(d, {3:333,4:444}, "error")
46 : $put =v:exception[:15] . v:exception[-1:-1]
49 :call filter(d, 'v:key =~ ''[ac391]''')
52 :" Dictionary identity
56 : $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
61 :" Changing var type should fail
65 : $put =v:exception[:14] . v:exception[-1:-1]
70 : $put =v:exception[:14] . v:exception[-1:-1]
73 :" removing items with :unlet
89 :" removing items out of range: silently skip items that don't exist
124 :" assignment to a list
125 :let l = [0, 1, 2, 3]
126 :let [va, vb] = l[2:3]
132 : $put =v:exception[:14]
135 : let [va, vb] = l[1:1]
137 : $put =v:exception[:14]
140 :" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
142 :for i in range(1500)
143 : let d[i] = 3000 - i
145 :$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
149 : $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
152 :for i in range(1500)
153 : if d[i] != 3000 - i
163 :$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
164 :" delete odd items, checking value, one intentionally wrong
168 : if d[i] != 3000 - i
169 : $put =i . '=' . d[i]
175 :$put =string(d) " must be almost empty now
178 :" Dictionary function
180 :func dict.func(a) dict
181 : $put =a:a . len(self.data)
183 :let dict.data = [1,2,3]
184 :call dict.func("len: ")
185 :let x = dict.func("again: ")
190 : $put =v:exception[:15]
193 :" Function in script-local List or Dict
195 :function g:dict.func() dict
196 : $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
198 :let g:dict.foo = ['-', 2, 3]
199 :call insert(g:dict.foo, function('strlen'))
202 :" Nasty: remove func from Dict that's being called (works)
207 :$put =d.func(string(remove(d, 'func')))
209 :" Nasty: deepcopy() dict that refers to itself (fails when noref used)
213 :let dc = deepcopy(d)
215 : let dc = deepcopy(d, 1)
217 : $put =v:exception[:14]
219 :let l2 = [0, l, l, 3]
221 :let l3 = deepcopy(l2)
222 :$put ='same list: ' . (l3[1] is l3[2])
225 :for depth in range(5)
226 : $put ='depth is ' . depth
229 : let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
230 : exe "lockvar " . depth . " l"
234 : exe "unlockvar " . depth . " l"
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]")
240 : let l[1][1][0] = 99
258 : let l[2]['6'][7] = 99
264 : let l[2][6] = {99: 99}
270 : let l[2] = {99: 99}
285 :" a:000 function argument
286 :" first the tests that should fail
290 : $put ='caught a:000'
295 : $put ='caught a:000[0]'
298 : let a:000[2] = [9, 10]
300 : $put ='caught a:000[2]'
303 : let a:000[3] = {9: 10}
305 : $put ='caught a:000[3]'
307 :" now the tests that should pass
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)
315 : $put ='caught ' . v:exception
318 :" 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))))
326 :" 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))
334 :$put =string(split('abc', '\zs'))
335 :$put =string(split('abc', '\zs', 1))
337 :" compare recursively linked list and dict
338 :let l = [1, 2, 3, 4]
339 :let d = {'1': 1, '2': l, '3': 3}
343 :$put =(l != deepcopy(l))
344 :$put =(d != deepcopy(d))
346 :call Test(1, 2, [3, 4], {5: 6}) " This may take a while
350 :call garbagecollect(1)
352 :/^start:/,$wq! test.out