diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-08-15 14:46:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-15 14:46:05 +0800 |
commit | 389165cac1596bf602c50904a789722d65ceaac7 (patch) | |
tree | 83c19a2e8c383f8babea402b7835e8dcf52efc7a /test/unit/strings_spec.lua | |
parent | 6d93bdd458a03bd07b1797e43bb11e3be560d340 (diff) | |
download | rneovim-389165cac1596bf602c50904a789722d65ceaac7.tar.gz rneovim-389165cac1596bf602c50904a789722d65ceaac7.tar.bz2 rneovim-389165cac1596bf602c50904a789722d65ceaac7.zip |
vim-patch:8.2.0066: some corners of vim_snprintf() are not tested (#24718)
Problem: Some corners of vim_snprintf() are not tested.
Solution: Add a test in C. (Dominique Pelle, closes vim/vim#5422)
https://github.com/vim/vim/commit/d2c946bacfedf4e506a4c6771758beddb87df3af
Diffstat (limited to 'test/unit/strings_spec.lua')
-rw-r--r-- | test/unit/strings_spec.lua | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua index b2c839f25c..bb5ea12edc 100644 --- a/test/unit/strings_spec.lua +++ b/test/unit/strings_spec.lua @@ -139,6 +139,42 @@ describe('vim_strchr()', function() end) end) +describe('vim_snprintf()', function() + itp('truncation', function() + local function check(expected, buf, bsize, fmt, ...) + eq(#expected, strings.vim_snprintf(buf, bsize, fmt, ...)) + if bsize > 0 then + local actual = ffi.string(buf, math.min(#expected + 1, bsize)) + eq(expected:sub(1, bsize - 1) .. '\0', actual) + end + end + + for bsize = 0, 14 do + local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree) + check('1234567', buf, bsize, '%d', ffi.cast('int', 1234567)) + check('1234567', buf, bsize, '%ld', ffi.cast('long', 1234567)) + check(' 1234567', buf, bsize, '%9ld', ffi.cast('long', 1234567)) + check('1234567 ', buf, bsize, '%-9ld', ffi.cast('long', 1234567)) + check('deadbeef', buf, bsize, '%x', ffi.cast('unsigned', 0xdeadbeef)) + check('001100', buf, bsize, '%06b', ffi.cast('int', 12)) + check('1.234000', buf, bsize, '%f', ffi.cast('double', 1.234)) + check('1.234000e+00', buf, bsize, '%e', ffi.cast('double', 1.234)) + check('nan', buf, bsize, '%f', ffi.cast('double', 0.0 / 0.0)) + check('inf', buf, bsize, '%f', ffi.cast('double', 1.0 / 0.0)) + check('-inf', buf, bsize, '%f', ffi.cast('double', -1.0 / 0.0)) + check('-0.000000', buf, bsize, '%f', ffi.cast('double', -0.0)) + check('漢語', buf, bsize, '%s', '漢語') + check(' 漢語', buf, bsize, '%8s', '漢語') + check('漢語 ', buf, bsize, '%-8s', '漢語') + check('漢', buf, bsize, '%.3s', '漢語') + check(' foo', buf, bsize, '%5S', 'foo') + check('%%%', buf, bsize, '%%%%%%') + check('0x87654321', buf, bsize, '%p', ffi.cast('char *', 0x87654321)) + check('0x0087654321', buf, bsize, '%012p', ffi.cast('char *', 0x87654321)) + end + end) +end) + describe('strcase_save()' , function() local strcase_save = function(input_string, upper) local res = strings.strcase_save(to_cstr(input_string), upper) |