Bram Moolenaar | 383f9bc | 2005-01-19 22:18:32 +0000 | [diff] [blame^] | 1 | Tests for List and Dictionary types. vim: set ft=vim : |
| 2 | |
| 3 | STARTTEST |
| 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 | :" |
| 79 | :" manipulating a big Dictionary |
| 80 | :let d = {} |
| 81 | :for i in range(15000) |
| 82 | : let d[i] = 30000 - i |
| 83 | :endfor |
| 84 | :$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[14000] . ' ' . d[14999] |
| 85 | :try |
| 86 | : let n = d[15000] |
| 87 | :catch |
| 88 | : $put =v:exception[:14] . v:exception[-5:-1] |
| 89 | :endtry |
| 90 | :" lookup each items |
| 91 | :for i in range(15000) |
| 92 | : if d[i] != 30000 - i |
| 93 | : $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 |
| 102 | :$put =get(d, 15000 - 100, 'NONE') . ' ' . d[1] |
| 103 | :" delete odd items, checking value, one intentionally wrong |
| 104 | :let d[33] = 999 |
| 105 | :let i = 1 |
| 106 | :while i < 15000 |
| 107 | : if d[i] != 30000 - i |
| 108 | : $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: ") |
| 124 | :echo dict.func("again: ") |
| 125 | :try |
| 126 | : let Fn = dict.func |
| 127 | : call Fn('xxx') |
| 128 | :catch |
| 129 | : $put =v:exception[:15] |
| 130 | :endtry |
| 131 | :sleep 5 |
| 132 | :" |
| 133 | :" Nasty: remove func from Dict that's being called (works) |
| 134 | :let d = {1:1} |
| 135 | :func d.func(a) |
| 136 | : return "a:". a:a |
| 137 | :endfunc |
| 138 | :$put = d.func(string(remove(d, 'func'))) |
| 139 | :" |
| 140 | :" Nasty: deepcopy() dict that refers to itself (fails) |
| 141 | :let d = {1:1, 2:2} |
| 142 | :let l = [4, d, 6] |
| 143 | :let d[3] = l |
| 144 | :try |
| 145 | : let x = deepcopy(d) |
| 146 | :catch |
| 147 | : $put =v:exception[:14] |
| 148 | :endtry |
| 149 | :" |
| 150 | :endfun |
| 151 | :call Test() |
| 152 | :" |
| 153 | :/^start:/,$wq! test.out |
| 154 | ENDTEST |
| 155 | |
| 156 | start: |