comparison src/testdir/test_filter_map.vim @ 26585:0d2a709e2ff0 v8.2.3822

patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9 Commit: https://github.com/vim/vim/commit/2d877599ee1cede063ef4abe3a2272e67c116238 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Dec 16 08:21:09 2021 +0000 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9 Problem: Leaking memory in map() and filter(), cannot use a string argument in Vim9 script. Solution: Fix the leak, adjust the argument check, also run the tests as Vim9 script. (Yegappan Lakshmanan, closes #9354)
author Bram Moolenaar <Bram@vim.org>
date Thu, 16 Dec 2021 09:30:04 +0100
parents 06693d1afc48
children 2126feddeda6
comparison
equal deleted inserted replaced
26584:b1299fc6445b 26585:0d2a709e2ff0
1 " Test filter() and map() 1 " Test filter() and map()
2
3 source vim9.vim
2 4
3 " list with expression string 5 " list with expression string
4 func Test_filter_map_list_expr_string() 6 func Test_filter_map_list_expr_string()
5 " filter() 7 " filter()
6 call assert_equal([2, 3, 4], filter([1, 2, 3, 4], 'v:val > 1')) 8 call assert_equal([2, 3, 4], filter([1, 2, 3, 4], 'v:val > 1'))
141 let bout = mapnew(bin, {k, v -> k == 1 ? 0x99 : v}) 143 let bout = mapnew(bin, {k, v -> k == 1 ? 0x99 : v})
142 call assert_equal(0z123456, bin) 144 call assert_equal(0z123456, bin)
143 call assert_equal(0z129956, bout) 145 call assert_equal(0z129956, bout)
144 endfunc 146 endfunc
145 147
148 " Test for using map(), filter() and mapnew() with a string
146 func Test_filter_map_string() 149 func Test_filter_map_string()
147 let s = "abc" 150 " filter()
148 151 let lines =<< trim END
149 " filter() 152 VAR s = "abc"
150 call filter(s, '"b" != v:val') 153 call filter(s, '"b" != v:val')
151 call assert_equal(s, s) 154 call assert_equal(s, s)
152 call assert_equal('ac', filter('abc', '"b" != v:val')) 155 call assert_equal('ac', filter('abc', '"b" != v:val'))
153 call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val')) 156 call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val'))
154 call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val')) 157 call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val'))
155 call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val')) 158 call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val'))
156 let t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}" 159 VAR t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}"
157 let u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}" 160 VAR u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}"
158 call assert_equal(u, filter(t, '"," != v:val')) 161 call assert_equal(u, filter(t, '"," != v:val'))
159 call assert_equal('', filter('abc', '0')) 162 call assert_equal('', filter('abc', '0'))
160 call assert_equal('ac', filter('abc', { i, x -> "b" != x })) 163 call assert_equal('ac', filter('abc', LSTART i, x LMIDDLE "b" != x LEND))
161 call assert_equal('あいうえお', filter('あxいxうxえxお', { i, x -> "x" != x })) 164 call assert_equal('あいうえお', filter('あxいxうxえxお', LSTART i, x LMIDDLE "x" != x LEND))
162 call assert_equal('', filter('abc', { i, x -> v:false })) 165 call assert_equal('', filter('abc', LSTART i, x LMIDDLE v:false LEND))
163 166 call assert_equal('', filter('', "v:val == 'a'"))
164 " map() 167 call assert_equal('', filter(test_null_string(), "v:val == 'a'"))
165 call map(s, 'nr2char(char2nr(v:val) + 2)') 168 END
166 call assert_equal(s, s) 169 call CheckLegacyAndVim9Success(lines)
167 call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)')) 170
168 call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"')) 171 " map()
169 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) 172 let lines =<< trim END
170 call assert_equal('', map('abc', '""')) 173 VAR s = "abc"
171 call assert_equal('cde', map('abc', { i, x -> nr2char(char2nr(x) + 2) })) 174 call map(s, 'nr2char(char2nr(v:val) + 2)')
172 call assert_equal('[あ][i][う][え][お]', map('あiうえお', { i, x -> '[' .. x .. ']' })) 175 call assert_equal(s, s)
173 call assert_equal('', map('abc', { i, x -> '' })) 176 call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)'))
177 call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"'))
178 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
179 call assert_equal('', map('abc', '""'))
180 call assert_equal('cde', map('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
181 call assert_equal('[あ][i][う][え][お]', map('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
182 call assert_equal('', map('abc', LSTART i, x LMIDDLE '' LEND))
183 call assert_equal('', map('', "v:val == 'a'"))
184 call assert_equal('', map(test_null_string(), "v:val == 'a'"))
185 END
186 call CheckLegacyAndVim9Success(lines)
174 187
175 " mapnew() 188 " mapnew()
176 call mapnew(s, 'nr2char(char2nr(v:val) + 2)') 189 let lines =<< trim END
177 call assert_equal(s, s) 190 VAR s = "abc"
178 call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)')) 191 call mapnew(s, 'nr2char(char2nr(v:val) + 2)')
179 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"')) 192 call assert_equal(s, s)
180 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) 193 call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)'))
181 call assert_equal('', mapnew('abc', '""')) 194 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"'))
182 call assert_equal('cde', mapnew('abc', { i, x -> nr2char(char2nr(x) + 2) })) 195 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
183 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', { i, x -> '[' .. x .. ']' })) 196 call assert_equal('', mapnew('abc', '""'))
184 call assert_equal('', mapnew('abc', { i, x -> '' })) 197 call assert_equal('cde', mapnew('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
185 198 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
186 " map() and filter() 199 call assert_equal('', mapnew('abc', LSTART i, x LMIDDLE '' LEND))
187 call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"')) 200 call assert_equal('', mapnew('', "v:val == 'a'"))
188 201 call assert_equal('', mapnew(test_null_string(), "v:val == 'a'"))
189 " patterns-composing(\Z) 202 END
190 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) })) 203 call CheckLegacyAndVim9Success(lines)
191 call assert_equal('àà', filter('càt,càt', {i,x -> x =~ '\Za' })) 204
192 call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) })) 205 let lines =<< trim END
193 call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) })) 206 #" map() and filter()
194 call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) ? '@' : x })) 207 call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"'))
195 call assert_equal('c@t', map('càt', {i,x -> x =~ '\Za' ? '@' : x })) 208
196 call assert_equal('@ström', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) ? '@' : x })) 209 #" patterns-composing(\Z)
197 call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) ? '@' : x })) 210 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) LEND))
198 211 call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ '\Za' LEND))
199 " patterns-composing(\%C) 212 call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) LEND))
200 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' })) 213 call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) LEND))
201 call assert_equal('àà', filter('càt,càt', {i,x -> x =~ 'a' .. '\%C' })) 214 call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) ? '@' : x LEND))
202 call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' })) 215 call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ '\Za' ? '@' : x LEND))
203 call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' })) 216 call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) ? '@' : x LEND))
204 call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' ? '@' : x })) 217 call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) ? '@' : x LEND))
205 call assert_equal('c@t', map('càt', {i,x -> x =~ 'a' .. '\%C' ? '@' : x })) 218
206 call assert_equal('@ström', map('Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' ? '@' : x })) 219 #" patterns-composing(\%C)
207 call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' ? '@' : x })) 220 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' LEND))
221 call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' LEND))
222 call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' LEND))
223 call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' LEND))
224 call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' ? '@' : x LEND))
225 call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' ? '@' : x LEND))
226 call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' ? '@' : x LEND))
227 call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' ? '@' : x LEND))
228 END
229 call CheckLegacyAndVim9Success(lines)
208 endfunc 230 endfunc
209 231
210 " vim: shiftwidth=2 sts=2 expandtab 232 " vim: shiftwidth=2 sts=2 expandtab