119
|
1 Tests for List and Dictionary types. vim: set ft=vim :
|
|
2
|
|
3 STARTTEST
|
|
4 :so small.vim
|
148
|
5 :fun Test(...)
|
119
|
6 :" 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
|
842
|
16 :" 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])
|
119
|
22 :"
|
|
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)))
|
148
|
36 :$put =string (values(d))
|
119
|
37 :for [key, val] in items(d)
|
|
38 : $put =key . ':' . string(val)
|
|
39 : unlet key val
|
|
40 :endfor
|
148
|
41 :call extend (d, {3:33, 1:99})
|
119
|
42 :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)
|
148
|
77 :try
|
119
|
78 :unlet l[:3]
|
|
79 :unlet l[1:]
|
148
|
80 :catch
|
|
81 :$put =v:exception
|
|
82 :endtry
|
119
|
83 :$put =string(l)
|
|
84 :"
|
|
85 :unlet d.c
|
|
86 :unlet d[-1]
|
|
87 :$put =string(d)
|
|
88 :"
|
842
|
89 :" removing items out of range: silently skip items that don't exist
|
|
90 let l = [0, 1, 2, 3]
|
|
91 :unlet l[2:1]
|
|
92 :$put =string(l)
|
|
93 let l = [0, 1, 2, 3]
|
|
94 :unlet l[2:2]
|
|
95 :$put =string(l)
|
|
96 let l = [0, 1, 2, 3]
|
|
97 :unlet l[2:3]
|
|
98 :$put =string(l)
|
|
99 let l = [0, 1, 2, 3]
|
|
100 :unlet l[2:4]
|
|
101 :$put =string(l)
|
|
102 let l = [0, 1, 2, 3]
|
|
103 :unlet l[2:5]
|
|
104 :$put =string(l)
|
|
105 let l = [0, 1, 2, 3]
|
|
106 :unlet l[-1:2]
|
|
107 :$put =string(l)
|
|
108 let l = [0, 1, 2, 3]
|
|
109 :unlet l[-2:2]
|
|
110 :$put =string(l)
|
|
111 let l = [0, 1, 2, 3]
|
|
112 :unlet l[-3:2]
|
|
113 :$put =string(l)
|
|
114 let l = [0, 1, 2, 3]
|
|
115 :unlet l[-4:2]
|
|
116 :$put =string(l)
|
|
117 let l = [0, 1, 2, 3]
|
|
118 :unlet l[-5:2]
|
|
119 :$put =string(l)
|
|
120 let 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 :"
|
123
|
140 :" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
|
119
|
141 :let d = {}
|
123
|
142 :for i in range(1500)
|
|
143 : let d[i] = 3000 - i
|
119
|
144 :endfor
|
123
|
145 :$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
|
119
|
146 :try
|
123
|
147 : let n = d[1500]
|
119
|
148 :catch
|
1539
|
149 : $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
|
119
|
150 :endtry
|
|
151 :" lookup each items
|
123
|
152 :for i in range(1500)
|
|
153 : if d[i] != 3000 - i
|
119
|
154 : $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
|
123
|
163 :$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
|
119
|
164 :" delete odd items, checking value, one intentionally wrong
|
|
165 :let d[33] = 999
|
|
166 :let i = 1
|
123
|
167 :while i < 1500
|
|
168 : if d[i] != 3000 - i
|
119
|
169 : $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: ")
|
123
|
185 :let x = dict.func("again: ")
|
119
|
186 :try
|
|
187 : let Fn = dict.func
|
|
188 : call Fn('xxx')
|
|
189 :catch
|
|
190 : $put =v:exception[:15]
|
|
191 :endtry
|
123
|
192 :"
|
|
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()
|
119
|
201 :"
|
|
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
|
148
|
207 :$put =d.func(string(remove(d, 'func')))
|
119
|
208 :"
|
164
|
209 :" Nasty: deepcopy() dict that refers to itself (fails when noref used)
|
119
|
210 :let d = {1:1, 2:2}
|
|
211 :let l = [4, d, 6]
|
|
212 :let d[3] = l
|
164
|
213 :let dc = deepcopy(d)
|
119
|
214 :try
|
164
|
215 : let dc = deepcopy(d, 1)
|
119
|
216 :catch
|
|
217 : $put =v:exception[:14]
|
|
218 :endtry
|
164
|
219 :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])
|
119
|
223 :"
|
148
|
224 :" 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 :"
|
164
|
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))))
|
|
325 :"
|
283
|
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))
|
294
|
334 :$put =string(split('abc', '\zs'))
|
|
335 :$put =string(split('abc', '\zs', 1))
|
283
|
336 :"
|
1125
|
337 :" compare recursively linked list and dict
|
|
338 :let l = [1, 2, 3, 4]
|
|
339 :let d = {'1': 1, '2': l, '3': 3}
|
|
340 :let l[1] = d
|
|
341 :$put =(l == l)
|
|
342 :$put =(d == d)
|
|
343 :$put =(l != deepcopy(l))
|
|
344 :$put =(d != deepcopy(d))
|
119
|
345 :endfun
|
148
|
346 :call Test(1, 2, [3, 4], {5: 6}) " This may take a while
|
119
|
347 :"
|
1405
|
348 :delfunc Test
|
|
349 :unlet dict
|
|
350 :call garbagecollect(1)
|
|
351 :"
|
119
|
352 :/^start:/,$wq! test.out
|
|
353 ENDTEST
|
|
354
|
|
355 start:
|