blob: 2fa26bf903ac0a2defe1afd3c664ca138bf20115 [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
5:fun Test()
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
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)))
30:$put =string(values(d))
31:for [key, val] in items(d)
32: $put =key . ':' . string(val)
33: unlet key val
34:endfor
35:call extend(d, {3:33, 1:99})
36: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)
71:unlet l[:3]
72:unlet l[1:]
73:$put =string(l)
74:"
75:unlet d.c
76:unlet d[-1]
77:$put =string(d)
78:"
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000079:" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000080:let d = {}
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000081:for i in range(1500)
82: let d[i] = 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000083:endfor
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000084:$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000085:try
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000086: let n = d[1500]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000087:catch
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000088: $put =v:exception[:14] . v:exception[-4:-1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000089:endtry
90:" lookup each items
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000091:for i in range(1500)
92: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +000093: $put =d[i]
94: endif
95:endfor
96: let i += 1
97:" delete even items
98:while i >= 2
99: let i -= 2
100: unlet d[i]
101:endwhile
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000102:$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000103:" delete odd items, checking value, one intentionally wrong
104:let d[33] = 999
105:let i = 1
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000106:while i < 1500
107: if d[i] != 3000 - i
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000108: $put =i . '=' . d[i]
109: else
110: unlet d[i]
111: endif
112: let i += 2
113:endwhile
114:$put =string(d) " must be almost empty now
115:unlet d
116:"
117:" Dictionary function
118:let dict = {}
119:func dict.func(a) dict
120: $put =a:a . len(self.data)
121:endfunc
122:let dict.data = [1,2,3]
123:call dict.func("len: ")
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000124:let x = dict.func("again: ")
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000125:try
126: let Fn = dict.func
127: call Fn('xxx')
128:catch
129: $put =v:exception[:15]
130:endtry
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000131:"
132:" Function in script-local List or Dict
133:let g:dict = {}
134:function g:dict.func() dict
135: $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
136:endfunc
137:let g:dict.foo = ['-', 2, 3]
138:call insert(g:dict.foo, function('strlen'))
139:call g:dict.func()
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000140:"
141:" Nasty: remove func from Dict that's being called (works)
142:let d = {1:1}
143:func d.func(a)
144: return "a:". a:a
145:endfunc
146:$put = d.func(string(remove(d, 'func')))
147:"
148:" Nasty: deepcopy() dict that refers to itself (fails)
149:let d = {1:1, 2:2}
150:let l = [4, d, 6]
151:let d[3] = l
152:try
153: let x = deepcopy(d)
154:catch
155: $put =v:exception[:14]
156:endtry
157:"
158:endfun
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000159:call Test() " This may take a while
Bram Moolenaar383f9bc2005-01-19 22:18:32 +0000160:"
161:/^start:/,$wq! test.out
162ENDTEST
163
164start: