From 389165cac1596bf602c50904a789722d65ceaac7 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 15 Aug 2023 14:46:05 +0800 Subject: 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 --- test/unit/strings_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/unit/strings_spec.lua') 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) -- cgit From 842a47d6a4103a75e33c2c0023dbae5ad2c0f534 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 15 Aug 2023 19:16:19 +0800 Subject: vim-patch:9.0.1704: Cannot use positional arguments for printf() (#24719) Problem: Cannot use positional arguments for printf() Solution: Support positional arguments in string formatting closes: vim/vim#12140 https://github.com/vim/vim/commit/0c6181fec4c362eb9682d5af583341eb20cb1af5 Co-authored-by: Christ van Willegen --- test/unit/strings_spec.lua | 100 +++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 27 deletions(-) (limited to 'test/unit/strings_spec.lua') diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua index bb5ea12edc..68790ea026 100644 --- a/test/unit/strings_spec.lua +++ b/test/unit/strings_spec.lua @@ -140,37 +140,83 @@ describe('vim_strchr()', function() 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 + local function a(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 + local function i(n) return ffi.cast('int', n) end + local function l(n) return ffi.cast('long', n) end + local function u(n) return ffi.cast('unsigned', n) end + local function ll(n) return ffi.cast('long long', n) end + local function ul(n) return ffi.cast('unsigned long', n) end + local function ull(n) return ffi.cast('unsigned long long', n) end + + itp('truncation', function() 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)) + a('1.00000001e7', buf, bsize, '%.8g', 10000000.1) + a('1234567', buf, bsize, '%d', i(1234567)) + a('1234567', buf, bsize, '%ld', l(1234567)) + a(' 1234567', buf, bsize, '%9ld', l(1234567)) + a('1234567 ', buf, bsize, '%-9ld', l(1234567)) + a('deadbeef', buf, bsize, '%x', u(0xdeadbeef)) + a('001100', buf, bsize, '%06b', u(12)) + a('one two', buf, bsize, '%s %s', 'one', 'two') + a('1.234000', buf, bsize, '%f', 1.234) + a('1.234000e+00', buf, bsize, '%e', 1.234) + a('nan', buf, bsize, '%f', 0.0 / 0.0) + a('inf', buf, bsize, '%f', 1.0 / 0.0) + a('-inf', buf, bsize, '%f', -1.0 / 0.0) + a('-0.000000', buf, bsize, '%f', -0.0) + a('漢語', buf, bsize, '%s', '漢語') + a(' 漢語', buf, bsize, '%8s', '漢語') + a('漢語 ', buf, bsize, '%-8s', '漢語') + a('漢', buf, bsize, '%.3s', '漢語') + a(' foo', buf, bsize, '%5S', 'foo') + a('%%%', buf, bsize, '%%%%%%') + a('0x87654321', buf, bsize, '%p', ffi.cast('char *', 0x87654321)) + a('0x0087654321', buf, bsize, '%012p', ffi.cast('char *', 0x87654321)) + end + end) + + itp('positional arguments', function() + for bsize = 0, 24 do + local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree) + a('1234567 ', buf, bsize, '%1$*2$ld', l(1234567), i(-9)) + a('1234567 ', buf, bsize, '%1$*2$.*3$ld', l(1234567), i(-9), i(5)) + a('1234567 ', buf, bsize, '%1$*3$.*2$ld', l(1234567), i(5), i(-9)) + a('1234567 ', buf, bsize, '%3$*1$.*2$ld', i(-9), i(5), l(1234567)) + a('1234567', buf, bsize, '%1$ld', l(1234567)) + a(' 1234567', buf, bsize, '%1$*2$ld', l(1234567), i(9)) + a('9 12345 7654321', buf, bsize, '%2$ld %1$d %3$lu', i(12345), l(9), ul(7654321)) + a('9 1234567 7654321', buf, bsize, '%2$d %1$ld %3$lu', l(1234567), i(9), ul(7654321)) + a('9 1234567 7654321', buf, bsize, '%2$d %1$lld %3$lu', ll(1234567), i(9), ul(7654321)) + a('9 12345 7654321', buf, bsize, '%2$ld %1$u %3$lu', u(12345), l(9), ul(7654321)) + a('9 1234567 7654321', buf, bsize, '%2$d %1$lu %3$lu', ul(1234567), i(9), ul(7654321)) + a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321)) + a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321)) + a('9 deadbeef 7654321', buf, bsize, '%2$d %1$x %3$lu', u(0xdeadbeef), i(9), ul(7654321)) + a('9 c 7654321', buf, bsize, '%2$ld %1$c %3$lu', i(('c'):byte()), l(9), ul(7654321)) + a('9 hi 7654321', buf, bsize, '%2$ld %1$s %3$lu', 'hi', l(9), ul(7654321)) + a('9 0.000000e+00 7654321', buf, bsize, '%2$ld %1$e %3$lu', 0.0, l(9), ul(7654321)) + a('two one two', buf, bsize, '%2$s %1$s %2$s', 'one', 'two', 'three') + a('three one two', buf, bsize, '%3$s %1$s %2$s', 'one', 'two', 'three') + a('1234567', buf, bsize, '%1$d', i(1234567)) + a('deadbeef', buf, bsize, '%1$x', u(0xdeadbeef)) + a('001100', buf, bsize, '%1$0.*2$b', u(12), i(6)) + a('one two', buf, bsize, '%1$s %2$s', 'one', 'two') + a('001100', buf, bsize, '%06b', u(12)) + a('two one', buf, bsize, '%2$s %1$s', 'one', 'two') + a('1.234000', buf, bsize, '%1$f', 1.234) + a('1.234000e+00', buf, bsize, '%1$e', 1.234) + a('nan', buf, bsize, '%1$f', 0.0 / 0.0) + a('inf', buf, bsize, '%1$f', 1.0 / 0.0) + a('-inf', buf, bsize, '%1$f', -1.0 / 0.0) + a('-0.000000', buf, bsize, '%1$f', -0.0) end end) end) -- cgit From fc14928719df12826397b46b1765b82f1fc7d1d8 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 15 Aug 2023 20:54:28 +0800 Subject: fix(printf): make positional %zd and %zu work (#24722) --- test/unit/strings_spec.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'test/unit/strings_spec.lua') diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua index 68790ea026..d6c14e7a40 100644 --- a/test/unit/strings_spec.lua +++ b/test/unit/strings_spec.lua @@ -150,10 +150,12 @@ describe('vim_snprintf()', function() local function i(n) return ffi.cast('int', n) end local function l(n) return ffi.cast('long', n) end - local function u(n) return ffi.cast('unsigned', n) end local function ll(n) return ffi.cast('long long', n) end + local function z(n) return ffi.cast('ptrdiff_t', n) end + local function u(n) return ffi.cast('unsigned', n) end local function ul(n) return ffi.cast('unsigned long', n) end local function ull(n) return ffi.cast('unsigned long long', n) end + local function uz(n) return ffi.cast('size_t', n) end itp('truncation', function() for bsize = 0, 14 do @@ -219,6 +221,15 @@ describe('vim_snprintf()', function() a('-0.000000', buf, bsize, '%1$f', -0.0) end end) + + itp('%zd and %zu', function() + local bsize = 20 + local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree) + a('-1234567 -7654321', buf, bsize, '%zd %zd', z(-1234567), z(-7654321)) + a('-7654321 -1234567', buf, bsize, '%2$zd %1$zd', z(-1234567), z(-7654321)) + a('1234567 7654321', buf, bsize, '%zu %zu', uz(1234567), uz(7654321)) + a('7654321 1234567', buf, bsize, '%2$zu %1$zu', uz(1234567), uz(7654321)) + end) end) describe('strcase_save()' , function() -- cgit From 10c5f35a8ddf2683e0c849f71c02b81556ece902 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 16 Aug 2023 06:11:05 +0800 Subject: vim-patch:9.0.1715: duplicate test in message_test.c (#24728) Problem: duplicate test in message_test.c Solution: Remove duplicate test and make functions static closes: vim/vim#12803 https://github.com/vim/vim/commit/7772c93a3ec1ce60469e9aea9899687ae1141f34 --- test/unit/strings_spec.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'test/unit/strings_spec.lua') diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua index d6c14e7a40..04d11d8f04 100644 --- a/test/unit/strings_spec.lua +++ b/test/unit/strings_spec.lua @@ -200,7 +200,6 @@ describe('vim_snprintf()', function() a('9 12345 7654321', buf, bsize, '%2$ld %1$u %3$lu', u(12345), l(9), ul(7654321)) a('9 1234567 7654321', buf, bsize, '%2$d %1$lu %3$lu', ul(1234567), i(9), ul(7654321)) a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321)) - a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321)) a('9 deadbeef 7654321', buf, bsize, '%2$d %1$x %3$lu', u(0xdeadbeef), i(9), ul(7654321)) a('9 c 7654321', buf, bsize, '%2$ld %1$c %3$lu', i(('c'):byte()), l(9), ul(7654321)) a('9 hi 7654321', buf, bsize, '%2$ld %1$s %3$lu', 'hi', l(9), ul(7654321)) -- cgit From c431d820e7be1c511d3d16e89cdffaa21b7909fa Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 4 Sep 2023 08:49:50 +0800 Subject: vim-patch:9.0.1856: issues with formatting positional arguments (#25013) Problem: issues with formatting positional arguments Solution: fix them, add tests and documentation closes: vim/vim#12140 closes: vim/vim#12985 Tentatively fix message_test. Check NULL ptr. https://github.com/vim/vim/commit/aa90d4f031f73a34aaef5746931ea746849a2231 Co-authored-by: Christ van Willegen --- test/unit/strings_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test/unit/strings_spec.lua') diff --git a/test/unit/strings_spec.lua b/test/unit/strings_spec.lua index 04d11d8f04..6d7aceb4b2 100644 --- a/test/unit/strings_spec.lua +++ b/test/unit/strings_spec.lua @@ -208,6 +208,7 @@ describe('vim_snprintf()', function() a('three one two', buf, bsize, '%3$s %1$s %2$s', 'one', 'two', 'three') a('1234567', buf, bsize, '%1$d', i(1234567)) a('deadbeef', buf, bsize, '%1$x', u(0xdeadbeef)) + a('001100', buf, bsize, '%2$0*1$b', i(6), u(12)) a('001100', buf, bsize, '%1$0.*2$b', u(12), i(6)) a('one two', buf, bsize, '%1$s %2$s', 'one', 'two') a('001100', buf, bsize, '%06b', u(12)) -- cgit