robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.9 KiB314 linesraw
1local t = require('test.unit.testutil')
2local describe = t.describe
3local itp = t.gen_itp(t.it)
4
5local child_call_once = t.child_call_once
6local cimport = t.cimport
7local eq = t.eq
8local ffi = t.ffi
9local to_cstr = t.to_cstr
10
11local strings = cimport('stdlib.h', './src/nvim/strings.h', './src/nvim/memory.h')
12
13local UVARNUM_TYPE
14
15child_call_once(function()
16  UVARNUM_TYPE = ffi.typeof('uvarnumber_T')
17end)
18
19describe('vim_strsave_escaped()', function()
20  local vim_strsave_escaped = function(s, chars)
21    local res = strings.vim_strsave_escaped(to_cstr(s), to_cstr(chars))
22    local ret = ffi.string(res)
23
24    -- Explicitly free memory so we are sure it is allocated: if it was not it
25    -- will crash.
26    strings.xfree(res)
27    return ret
28  end
29
30  itp('precedes by a backslash all chars from second argument', function()
31    eq([[\a\b\c\d]], vim_strsave_escaped('abcd', 'abcd'))
32  end)
33
34  itp('precedes by a backslash chars only from second argument', function()
35    eq([[\a\bcd]], vim_strsave_escaped('abcd', 'ab'))
36  end)
37
38  itp('returns a copy of passed string if second argument is empty', function()
39    eq('text \n text', vim_strsave_escaped('text \n text', ''))
40  end)
41
42  itp('returns an empty string if first argument is empty string', function()
43    eq('', vim_strsave_escaped('', '\r'))
44  end)
45
46  itp('returns a copy of passed string if it does not contain chars from 2nd argument', function()
47    eq('some text', vim_strsave_escaped('some text', 'a'))
48  end)
49end)
50
51describe('vim_strnsave_unquoted()', function()
52  local vim_strnsave_unquoted = function(s, len)
53    local res = strings.vim_strnsave_unquoted(to_cstr(s), len or #s)
54    local ret = ffi.string(res)
55    -- Explicitly free memory so we are sure it is allocated: if it was not it
56    -- will crash.
57    strings.xfree(res)
58    return ret
59  end
60
61  itp('copies unquoted strings as-is', function()
62    eq('-c', vim_strnsave_unquoted('-c'))
63    eq('', vim_strnsave_unquoted(''))
64  end)
65
66  itp('respects length argument', function()
67    eq('', vim_strnsave_unquoted('-c', 0))
68    eq('-', vim_strnsave_unquoted('-c', 1))
69    eq('-', vim_strnsave_unquoted('"-c', 2))
70  end)
71
72  itp('unquotes fully quoted word', function()
73    eq('/bin/sh', vim_strnsave_unquoted('"/bin/sh"'))
74  end)
75
76  itp('unquotes partially quoted word', function()
77    eq('/Program Files/sh', vim_strnsave_unquoted('/Program" "Files/sh'))
78  end)
79
80  itp('removes ""', function()
81    eq('/Program Files/sh', vim_strnsave_unquoted('/""Program" "Files/sh'))
82  end)
83
84  itp('performs unescaping of "', function()
85    eq('/"Program Files"/sh', vim_strnsave_unquoted('/"\\""Program Files"\\""/sh'))
86  end)
87
88  itp('performs unescaping of \\', function()
89    eq('/\\Program Files\\foo/sh', vim_strnsave_unquoted('/"\\\\"Program Files"\\\\foo"/sh'))
90  end)
91
92  itp('strips quote when there is no pair to it', function()
93    eq('/Program Files/sh', vim_strnsave_unquoted('/Program" Files/sh'))
94    eq('', vim_strnsave_unquoted('"'))
95  end)
96
97  itp('allows string to end with one backslash unescaped', function()
98    eq('/Program Files/sh\\', vim_strnsave_unquoted('/Program" Files/sh\\'))
99  end)
100
101  itp('does not perform unescaping out of quotes', function()
102    eq('/Program\\ Files/sh\\', vim_strnsave_unquoted('/Program\\ Files/sh\\'))
103  end)
104
105  itp('does not unescape \\n', function()
106    eq('/Program\\nFiles/sh', vim_strnsave_unquoted('/Program"\\n"Files/sh'))
107  end)
108end)
109
110describe('vim_strchr()', function()
111  local vim_strchr = function(s, c)
112    local str = to_cstr(s)
113    local res = strings.vim_strchr(str, c)
114    if res == nil then
115      return nil
116    else
117      return res - str
118    end
119  end
120  itp('handles NUL and <0 correctly', function()
121    eq(nil, vim_strchr('abc', 0))
122    eq(nil, vim_strchr('abc', -1))
123  end)
124  itp('works', function()
125    eq(0, vim_strchr('abc', ('a'):byte()))
126    eq(1, vim_strchr('abc', ('b'):byte()))
127    eq(2, vim_strchr('abc', ('c'):byte()))
128    eq(0, vim_strchr('a«b»c', ('a'):byte()))
129    eq(3, vim_strchr('a«b»c', ('b'):byte()))
130    eq(6, vim_strchr('a«b»c', ('c'):byte()))
131
132    eq(nil, vim_strchr('«»', ('«'):byte()))
133    -- 0xAB == 171 == '«'
134    eq(nil, vim_strchr('\171', 0xAB))
135    eq(0, vim_strchr('«»', 0xAB))
136    eq(3, vim_strchr('„«»“', 0xAB))
137
138    eq(7, vim_strchr('„«»“', 0x201C))
139    eq(nil, vim_strchr('„«»“', 0x201D))
140    eq(0, vim_strchr('„«»“', 0x201E))
141
142    eq(0, vim_strchr('\244\143\188\128', 0x10FF00))
143    eq(2, vim_strchr('«\244\143\188\128»', 0x10FF00))
144    --                   |0xDBFF     |0xDF00  - surrogate pair for 0x10FF00
145    eq(nil, vim_strchr('«\237\175\191\237\188\128»', 0x10FF00))
146  end)
147end)
148
149describe('vim_snprintf()', function()
150  local function a(expected, buf, bsize, fmt, ...)
151    local args = { ... }
152    local ctx = string.format('snprintf(buf, %d, "%s"', bsize, fmt)
153    for _, x in ipairs(args) do
154      ctx = ctx .. ', ' .. tostring(x)
155    end
156    ctx = ctx .. string.format(') = %s', expected)
157    eq(#expected, strings.vim_snprintf(buf, bsize, fmt, ...), ctx)
158    if bsize > 0 then
159      local actual = ffi.string(buf, math.min(#expected + 1, bsize))
160      eq(expected:sub(1, bsize - 1) .. '\0', actual, ctx)
161    end
162  end
163
164  local function uv(n)
165    return ffi.cast(UVARNUM_TYPE, n)
166  end
167  local function i(n)
168    return ffi.cast('int', n)
169  end
170  local function l(n)
171    return ffi.cast('long', n)
172  end
173  local function ll(n)
174    return ffi.cast('long long', n)
175  end
176  local function z(n)
177    return ffi.cast('ptrdiff_t', n)
178  end
179  local function u(n)
180    return ffi.cast('unsigned', n)
181  end
182  local function ul(n)
183    return ffi.cast('unsigned long', n)
184  end
185  local function ull(n)
186    return ffi.cast('unsigned long long', n)
187  end
188  local function uz(n)
189    return ffi.cast('size_t', n)
190  end
191
192  itp('truncation', function()
193    for bsize = 0, 14 do
194      local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
195      a('1.00000001e7', buf, bsize, '%.8g', 10000000.1)
196      a('1234567', buf, bsize, '%d', i(1234567))
197      a('1234567', buf, bsize, '%ld', l(1234567))
198      a('  1234567', buf, bsize, '%9ld', l(1234567))
199      a('1234567  ', buf, bsize, '%-9ld', l(1234567))
200      a('deadbeef', buf, bsize, '%x', u(0xdeadbeef))
201      a('001100', buf, bsize, '%06b', uv(12))
202      a('one two', buf, bsize, '%s %s', 'one', 'two')
203      a('1.234000', buf, bsize, '%f', 1.234)
204      a('1.234000e+00', buf, bsize, '%e', 1.234)
205      a('nan', buf, bsize, '%f', 0.0 / 0.0)
206      a('inf', buf, bsize, '%f', 1.0 / 0.0)
207      a('-inf', buf, bsize, '%f', -1.0 / 0.0)
208      a('-0.000000', buf, bsize, '%f', tonumber('-0.0'))
209      a('漢語', buf, bsize, '%s', '漢語')
210      a('  漢語', buf, bsize, '%8s', '漢語')
211      a('漢語  ', buf, bsize, '%-8s', '漢語')
212      a('漢', buf, bsize, '%.3s', '漢語')
213      a('  foo', buf, bsize, '%5S', 'foo')
214      a('%%%', buf, bsize, '%%%%%%')
215      a('0x87654321', buf, bsize, '%p', ffi.cast('char *', 0x87654321))
216      a('0x0087654321', buf, bsize, '%012p', ffi.cast('char *', 0x87654321))
217    end
218  end)
219
220  itp('positional arguments', function()
221    for bsize = 0, 24 do
222      local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
223      a('1234567  ', buf, bsize, '%1$*2$ld', l(1234567), i(-9))
224      a('1234567  ', buf, bsize, '%1$*2$.*3$ld', l(1234567), i(-9), i(5))
225      a('1234567  ', buf, bsize, '%1$*3$.*2$ld', l(1234567), i(5), i(-9))
226      a('1234567  ', buf, bsize, '%3$*1$.*2$ld', i(-9), i(5), l(1234567))
227      a('1234567', buf, bsize, '%1$ld', l(1234567))
228      a('  1234567', buf, bsize, '%1$*2$ld', l(1234567), i(9))
229      a('9 12345 7654321', buf, bsize, '%2$ld %1$d %3$lu', i(12345), l(9), ul(7654321))
230      a('9 1234567 7654321', buf, bsize, '%2$d %1$ld %3$lu', l(1234567), i(9), ul(7654321))
231      a('9 1234567 7654321', buf, bsize, '%2$d %1$lld %3$lu', ll(1234567), i(9), ul(7654321))
232      a('9 12345 7654321', buf, bsize, '%2$ld %1$u %3$lu', u(12345), l(9), ul(7654321))
233      a('9 1234567 7654321', buf, bsize, '%2$d %1$lu %3$lu', ul(1234567), i(9), ul(7654321))
234      a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321))
235      a('9 deadbeef 7654321', buf, bsize, '%2$d %1$x %3$lu', u(0xdeadbeef), i(9), ul(7654321))
236      a('9 c 7654321', buf, bsize, '%2$ld %1$c %3$lu', i(('c'):byte()), l(9), ul(7654321))
237      a('9 hi 7654321', buf, bsize, '%2$ld %1$s %3$lu', 'hi', l(9), ul(7654321))
238      a('9 0.000000e+00 7654321', buf, bsize, '%2$ld %1$e %3$lu', 0.0, l(9), ul(7654321))
239      a('two one two', buf, bsize, '%2$s %1$s %2$s', 'one', 'two', 'three')
240      a('three one two', buf, bsize, '%3$s %1$s %2$s', 'one', 'two', 'three')
241      a('1234567', buf, bsize, '%1$d', i(1234567))
242      a('deadbeef', buf, bsize, '%1$x', u(0xdeadbeef))
243      a('001100', buf, bsize, '%2$0*1$b', i(6), uv(12))
244      a('001100', buf, bsize, '%1$0.*2$b', uv(12), i(6))
245      a('one two', buf, bsize, '%1$s %2$s', 'one', 'two')
246      a('001100', buf, bsize, '%06b', uv(12))
247      a('two one', buf, bsize, '%2$s %1$s', 'one', 'two')
248      a('1.234000', buf, bsize, '%1$f', 1.234)
249      a('1.234000e+00', buf, bsize, '%1$e', 1.234)
250      a('nan', buf, bsize, '%1$f', 0.0 / 0.0)
251      a('inf', buf, bsize, '%1$f', 1.0 / 0.0)
252      a('-inf', buf, bsize, '%1$f', -1.0 / 0.0)
253      a('-0.000000', buf, bsize, '%1$f', tonumber('-0.0'))
254    end
255  end)
256
257  itp('%zd and %zu', function()
258    local bsize = 20
259    local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
260    a('-1234567 -7654321', buf, bsize, '%zd %zd', z(-1234567), z(-7654321))
261    a('-7654321 -1234567', buf, bsize, '%2$zd %1$zd', z(-1234567), z(-7654321))
262    a('1234567 7654321', buf, bsize, '%zu %zu', uz(1234567), uz(7654321))
263    a('7654321 1234567', buf, bsize, '%2$zu %1$zu', uz(1234567), uz(7654321))
264  end)
265end)
266
267describe('strcase_save()', function()
268  local strcase_save = function(input_string, upper)
269    local res = strings.strcase_save(to_cstr(input_string), upper)
270    return ffi.string(res)
271  end
272
273  itp('decodes overlong encoded characters.', function()
274    eq('A', strcase_save('\xc1\x81', true))
275    eq('a', strcase_save('\xc1\x81', false))
276  end)
277end)
278
279describe('reverse_text', function()
280  local reverse_text = function(str)
281    return t.internalize(strings.reverse_text(to_cstr(str)))
282  end
283
284  itp('handles empty string', function()
285    eq('', reverse_text(''))
286  end)
287
288  itp('handles simple cases', function()
289    eq('a', reverse_text('a'))
290    eq('ba', reverse_text('ab'))
291  end)
292
293  itp('handles multibyte characters', function()
294    eq('bα', reverse_text('αb'))
295    eq('Yötön yö', reverse_text('öy nötöY'))
296  end)
297
298  itp('handles combining chars', function()
299    local utf8_COMBINING_RING_ABOVE = '\204\138'
300    local utf8_COMBINING_RING_BELOW = '\204\165'
301    eq(
302      'bba' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'aa',
303      reverse_text('aaa' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'bb')
304    )
305  end)
306
307  itp('treats invalid utf as separate characters', function()
308    eq('\192ba', reverse_text('ab\192'))
309  end)
310
311  itp('treats an incomplete utf continuation sequence as valid', function()
312    eq('\194ba', reverse_text('ab\194'))
313  end)
314end)