summaryrefslogtreecommitdiff
path: root/src/testdir/test55.in
blob: cf85f57a6c4434f51fe9232e92736db0990627a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
Tests for List and Dictionary types.     vim: set ft=vim :

STARTTEST
:so small.vim
:fun Test(...)
:" Creating List directly with different types
:let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
:$put =string(l)
:$put =string(l[-1])
:$put =string(l[-4])
:try
:  $put =string(l[-5])
:catch
:  $put =v:exception[:14]
:endtry
:" List slices
:$put =string(l[:])
:$put =string(l[1:])
:$put =string(l[:-2])
:$put =string(l[0:8])
:$put =string(l[8:-1])
:"
:" List identity
:let ll = l
:let lx = copy(l)
:try
:  $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
:catch
:  $put =v:exception
:endtry
:"
:" Creating Dictionary directly with different types
:let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
:$put =string(d) . d.1
:$put =string(sort(keys(d)))
:$put =string (values(d))
:for [key, val] in items(d)
:  $put =key . ':' . string(val)
:  unlet key val
:endfor
:call extend  (d, {3:33, 1:99})
:call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
:try
:  call extend(d, {3:333,4:444}, "error")
:catch
:  $put =v:exception[:15] . v:exception[-1:-1]
:endtry
:$put =string(d)
:call filter(d, 'v:key =~ ''[ac391]''')
:$put =string(d)
:"
:" Dictionary identity
:let dd = d
:let dx = copy(d)
:try
:  $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
:catch
:  $put =v:exception
:endtry
:"
:" Changing var type should fail
:try
:  let d = []
:catch
:  $put =v:exception[:14] . v:exception[-1:-1]
:endtry
:try
:  let l = {}
:catch
:  $put =v:exception[:14] . v:exception[-1:-1]
:endtry
:"
:" removing items with :unlet
:unlet l[2]
:$put =string(l)
:let l = range(8)
:try
:unlet l[:3]
:unlet l[1:]
:catch
:$put =v:exception
:endtry
:$put =string(l)
:"
:unlet d.c
:unlet d[-1]
:$put =string(d)
:"
:" removing items out of range: silently skip items that don't exist
let l = [0, 1, 2, 3]
:unlet l[2:1]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[2:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[2:3]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[2:4]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[2:5]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-1:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-2:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-3:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-4:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-5:2]
:$put =string(l)
let l = [0, 1, 2, 3]
:unlet l[-6:2]
:$put =string(l)
:"
:" assignment to a list
:let l = [0, 1, 2, 3]
:let [va, vb] = l[2:3]
:$put =va
:$put =vb
:try
:  let [va, vb] = l
:catch
:  $put =v:exception[:14]
:endtry
:try
:  let [va, vb] = l[1:1]
:catch
:  $put =v:exception[:14]
:endtry
:"
:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
:let d = {}
:for i in range(1500)
: let d[i] = 3000 - i
:endfor
:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
:try
:  let n = d[1500]
:catch
:  $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
:endtry
:" lookup each items
:for i in range(1500)
: if d[i] != 3000 - i
:  $put =d[i]
: endif
:endfor
: let i += 1
:" delete even items
:while i >= 2
: let i -= 2
: unlet d[i]
:endwhile
:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
:" delete odd items, checking value, one intentionally wrong
:let d[33] = 999
:let i = 1
:while i < 1500
: if d[i] != 3000 - i
:  $put =i . '=' . d[i]
: else
:  unlet d[i]
: endif
: let i += 2
:endwhile
:$put =string(d)  " must be almost empty now
:unlet d
:"
:" Dictionary function
:let dict = {}
:func dict.func(a) dict
:  $put =a:a . len(self.data)
:endfunc
:let dict.data = [1,2,3]
:call dict.func("len: ")
:let x = dict.func("again: ")
:try
:  let Fn = dict.func
:  call Fn('xxx')
:catch
:  $put =v:exception[:15]
:endtry
:" 
:" Function in script-local List or Dict
:let g:dict = {}
:function g:dict.func() dict
:  $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
:endfunc
:let g:dict.foo = ['-', 2, 3]
:call insert(g:dict.foo, function('strlen'))
:call g:dict.func()
:" 
:" Nasty: remove func from Dict that's being called (works)
:let d = {1:1}
:func d.func(a)
:  return "a:". a:a
:endfunc
:$put =d.func(string(remove(d, 'func')))
:"
:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
:let d = {1:1, 2:2}
:let l = [4, d, 6]
:let d[3] = l
:let dc = deepcopy(d)
:try
:  let dc = deepcopy(d, 1)
:catch
:  $put =v:exception[:14]
:endtry
:let l2 = [0, l, l, 3]
:let l[1] = l2
:let l3 = deepcopy(l2)
:$put ='same list: ' . (l3[1] is l3[2])
:"
:" Locked variables
:for depth in range(5)
:  $put ='depth is ' . depth
:  for u in range(3)
:    unlet l
:    let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
:    exe "lockvar " . depth . " l"
:    if u == 1
:      exe "unlockvar l"
:    elseif u == 2
:      exe "unlockvar " . depth . " l"
:    endif
:    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]")
:    $put =ps
:    let ps = ''
:    try
:      let l[1][1][0] = 99
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l[1][1] = [99]
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l[1] = [99]
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l[2]['6'][7] = 99
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l[2][6] = {99: 99}
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l[2] = {99: 99}
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    try
:      let l = [99]
:      let ps .= 'p'
:    catch
:      let ps .= 'F'
:    endtry
:    $put =ps
:  endfor
:endfor
:"
:" a:000 function argument
:" first the tests that should fail
:try
:  let a:000 = [1, 2]
:catch
:  $put ='caught a:000'
:endtry
:try
:  let a:000[0] = 9
:catch
:  $put ='caught a:000[0]'
:endtry
:try
:  let a:000[2] = [9, 10]
:catch
:  $put ='caught a:000[2]'
:endtry
:try
:  let a:000[3] = {9: 10}
:catch
:  $put ='caught a:000[3]'
:endtry
:" now the tests that should pass
:try
:  let a:000[2][1] = 9
:  call extend(a:000[2], [5, 6])
:  let a:000[3][5] = 8
:  let a:000[3]['a'] = 12
:  $put =string(a:000)
:catch
:  $put ='caught ' . v:exception
:endtry
:"
:" reverse() and sort()
:let l = ['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', [0, 1, 2], 'x8']
:$put =string(reverse(l))
:$put =string(reverse(reverse(l)))
:$put =string(sort(l))
:$put =string(reverse(sort(l)))
:$put =string(sort(reverse(sort(l))))
:"
:" splitting a string to a List
:$put =string(split('  aa  bb '))
:$put =string(split('  aa  bb  ', '\W\+', 0))
:$put =string(split('  aa  bb  ', '\W\+', 1))
:$put =string(split('  aa  bb  ', '\W', 1))
:$put =string(split(':aa::bb:', ':', 0))
:$put =string(split(':aa::bb:', ':', 1))
:$put =string(split('aa,,bb, cc,', ',\s*', 1))
:$put =string(split('abc', '\zs'))
:$put =string(split('abc', '\zs', 1))
:"
:" compare recursively linked list and dict
:let l = [1, 2, 3, 4]
:let d = {'1': 1, '2': l, '3': 3}
:let l[1] = d
:$put =(l == l)
:$put =(d == d)
:$put =(l != deepcopy(l))
:$put =(d != deepcopy(d))
:"
:" compare complex recursively linked list and dict
:let l = []
:call add(l, l)
:let dict4 = {"l": l}
:call add(dict4.l, dict4)
:let lcopy = deepcopy(l)
:let dict4copy = deepcopy(dict4)
:$put =(l == lcopy)
:$put =(dict4 == dict4copy)
:"
:" Pass the same List to extend()
:let l = [1, 2, 3, 4, 5]
:call extend(l, l)
:$put =string(l)
:"
:" Pass the same Dict to extend()
:let d = { 'a': {'b': 'B'}}
:call extend(d, d)
:$put =string(d)
:"
:" Pass the same Dict to extend() with "error"
:try
:  call extend(d, d, "error")
:catch
:  $put =v:exception[:15] . v:exception[-1:-1]
:endtry
:$put =string(d)
:endfun
:"
:call Test(1, 2, [3, 4], {5: 6})  " This may take a while
:"
:delfunc Test
:unlet dict
:call garbagecollect(1)
:"
:/^start:/,$wq! test.out
ENDTEST

start: