blob: 5570c596e9f6e23382b165df0121a74f589d1cd9 [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
16:"
17:" List identity
18:let ll = l
19:let lx = copy(l)
20:try
21: $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
22:catch
23: $put =v:exception
24:endtry
25:"
26:" Creating Dictionary directly with different types
27:let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
28:$put =string(d) . d.1
29:$put =string(sort(keys(d)))
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000030:$put =string (values(d))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000031:for [key, val] in items(d)
32: $put =key . ':' . string(val)
33: unlet key val
34:endfor
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000035:call extend (d, {3:33, 1:99})
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000036:call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
37:try
38: call extend(d, {3:333,4:444}, "error")
39:catch
40: $put =v:exception[:15] . v:exception[-1:-1]
41:endtry
42:$put =string(d)
43:call filter(d, 'v:key =~ ''[ac391]''')
44:$put =string(d)
45:"
46:" Dictionary identity
47:let dd = d
48:let dx = copy(d)
49:try
50: $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
51:catch
52: $put =v:exception
53:endtry
54:"
55:" Changing var type should fail
56:try
57: let d = []
58:catch
59: $put =v:exception[:14] . v:exception[-1:-1]
60:endtry
61:try
62: let l = {}
63:catch
64: $put =v:exception[:14] . v:exception[-1:-1]
65:endtry
66:"
67:" removing items with :unlet
68:unlet l[2]
69:$put =string(l)
70:let l = range(8)
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000071:try
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000072:unlet l[:3]
73:unlet l[1:]
Bram Moolenaar2ce06f62005-01-31 19:19:04 +000074:catch
75:$put =v:exception
76:endtry
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000077:$put =string(l)
78:"
79:unlet d.c
80:unlet d[-1]
81:$put =string(d)
82:"
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000083:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000084:let d = {}
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000085:for i in range(1500)
86: let d[i] = 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000087:endfor
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000088:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000089:try
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000090: let n = d[1500]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000091:catch
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000092: $put =v:exception[:14] . v:exception[-4:-1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000093:endtry
94:" lookup each items
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000095:for i in range(1500)
96: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000097: $put =d[i]
98: endif
99:endfor
100: let i += 1
101:" delete even items
102:while i >= 2
103: let i -= 2
104: unlet d[i]
105:endwhile
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000106:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000107:" delete odd items, checking value, one intentionally wrong
108:let d[33] = 999
109:let i = 1
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000110:while i < 1500
111: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000112: $put =i . '=' . d[i]
113: else
114: unlet d[i]
115: endif
116: let i += 2
117:endwhile
118:$put =string(d) " must be almost empty now
119:unlet d
120:"
121:" Dictionary function
122:let dict = {}
123:func dict.func(a) dict
124: $put =a:a . len(self.data)
125:endfunc
126:let dict.data = [1,2,3]
127:call dict.func("len: ")
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000128:let x = dict.func("again: ")
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000129:try
130: let Fn = dict.func
131: call Fn('xxx')
132:catch
133: $put =v:exception[:15]
134:endtry
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000135:"
136:" Function in script-local List or Dict
137:let g:dict = {}
138:function g:dict.func() dict
139: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
140:endfunc
141:let g:dict.foo = ['-', 2, 3]
142:call insert(g:dict.foo, function('strlen'))
143:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000144:"
145:" Nasty: remove func from Dict that's being called (works)
146:let d = {1:1}
147:func d.func(a)
148: return "a:". a:a
149:endfunc
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000150:$put =d.func(string(remove(d, 'func')))
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000151:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000152:" Nasty: deepcopy() dict that refers to itself (fails when noref used)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000153:let d = {1:1, 2:2}
154:let l = [4, d, 6]
155:let d[3] = l
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000156:let dc = deepcopy(d)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000157:try
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000158: let dc = deepcopy(d, 1)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000159:catch
160: $put =v:exception[:14]
161:endtry
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000162:let l2 = [0, l, l, 3]
163:let l[1] = l2
164:let l3 = deepcopy(l2)
165:$put ='same list: ' . (l3[1] is l3[2])
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000166:"
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000167:" Locked variables
168:for depth in range(5)
169: $put ='depth is ' . depth
170: for u in range(3)
171: unlet l
172: let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
173: exe "lockvar " . depth . " l"
174: if u == 1
175: exe "unlockvar l"
176: elseif u == 2
177: exe "unlockvar " . depth . " l"
178: endif
179: 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]")
180: $put =ps
181: let ps = ''
182: try
183: let l[1][1][0] = 99
184: let ps .= 'p'
185: catch
186: let ps .= 'F'
187: endtry
188: try
189: let l[1][1] = [99]
190: let ps .= 'p'
191: catch
192: let ps .= 'F'
193: endtry
194: try
195: let l[1] = [99]
196: let ps .= 'p'
197: catch
198: let ps .= 'F'
199: endtry
200: try
201: let l[2]['6'][7] = 99
202: let ps .= 'p'
203: catch
204: let ps .= 'F'
205: endtry
206: try
207: let l[2][6] = {99: 99}
208: let ps .= 'p'
209: catch
210: let ps .= 'F'
211: endtry
212: try
213: let l[2] = {99: 99}
214: let ps .= 'p'
215: catch
216: let ps .= 'F'
217: endtry
218: try
219: let l = [99]
220: let ps .= 'p'
221: catch
222: let ps .= 'F'
223: endtry
224: $put =ps
225: endfor
226:endfor
227:"
228:" a:000 function argument
229:" first the tests that should fail
230:try
231: let a:000 = [1, 2]
232:catch
233: $put ='caught a:000'
234:endtry
235:try
236: let a:000[0] = 9
237:catch
238: $put ='caught a:000[0]'
239:endtry
240:try
241: let a:000[2] = [9, 10]
242:catch
243: $put ='caught a:000[2]'
244:endtry
245:try
246: let a:000[3] = {9: 10}
247:catch
248: $put ='caught a:000[3]'
249:endtry
250:" now the tests that should pass
251:try
252: let a:000[2][1] = 9
253: call extend(a:000[2], [5, 6])
254: let a:000[3][5] = 8
255: let a:000[3]['a'] = 12
256: $put =string(a:000)
257:catch
258: $put ='caught ' . v:exception
259:endtry
260:"
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000261:" reverse() and sort()
262:let l = ['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', [0, 1, 2], 'x8']
263:$put =string(reverse(l))
264:$put =string(reverse(reverse(l)))
265:$put =string(sort(l))
266:$put =string(reverse(sort(l)))
267:$put =string(sort(reverse(sort(l))))
268:"
Bram Moolenaarde934d72005-05-22 22:09:40 +0000269:" splitting a string to a List
270:$put =string(split(' aa bb '))
271:$put =string(split(' aa bb ', '\W\+', 0))
272:$put =string(split(' aa bb ', '\W\+', 1))
273:$put =string(split(' aa bb ', '\W', 1))
274:$put =string(split(':aa::bb:', ':', 0))
275:$put =string(split(':aa::bb:', ':', 1))
276:$put =string(split('aa,,bb, cc,', ',\s*', 1))
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000277:$put =string(split('abc', '\zs'))
278:$put =string(split('abc', '\zs', 1))
Bram Moolenaarde934d72005-05-22 22:09:40 +0000279:"
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000280:endfun
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000281:call Test(1, 2, [3, 4], {5: 6}) " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000282:"
283:/^start:/,$wq! test.out
284ENDTEST
285
286start: