Hi
Attached patch improves coverage tests of function printf().
While adding tests, I noticed a few things possibly wrong:
:echo printf("%f", sqrt(-1.0))
-nan
I would have expected it to print "nan" instead of "-nan"
as in the example given in :help sqrt(). nan should not
be signed. I'm curious what it prints on other systems.
Another bug: The following 2 commands correctly
prints signed infinity:
:echo 1.0/0.0
inf
:echo -1.0/0.0
-inf
But when using printf(), the sign is gone. The sign
is significant for infinity, so the output of printf()
looks wrong:
:echo printf('%f', 1.0/0.0)
inf
:echo printf('%f', -1.0/0.0)
inf
I expected instead inf and -inf.
I also noticed that a few things are not documented
in :help printf() but which appear to do something or
are ignored. I did not test those as they are undocumented:
Is there any used for %p in vim function printf()?
:echo printf('%p', 'abc')
0x22c96e0
modifiers "%hd", "%ld", "%lld" are also undocumented:
:echo printf('%hx', 0x12345678)
5678
:echo printf('%x', 0x12345678)
12345678
:echo printf('%lx', 0x12345678)
12345678
:echo printf('%llx', 0x12345678)
12345678
Regards
Dominique
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index b23b449..49d6c3a 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -130,6 +130,99 @@ func Test_option_value()
set cpo&vim
endfunc
+function Test_printf_misc()
+ call assert_equal('123', printf('%d', 123))
+ call assert_equal('123', printf('%i', 123))
+ call assert_equal('123', printf('%D', 123))
+ call assert_equal('123', printf('%U', 123))
+ call assert_equal('173', printf('%o', 123))
+ call assert_equal('173', printf('%O', 123))
+ call assert_equal('7b', printf('%x', 123))
+ call assert_equal('7B', printf('%X', 123))
+ if has('ebcdic')
+ call assert_equal('#', printf('%c', 123))
+ else
+ call assert_equal('{', printf('%c', 123))
+ endif
+ call assert_equal('abc', printf('%s', 'abc'))
+ call assert_equal('abc', printf('%S', 'abc'))
+
+ call assert_equal('+123', printf('%+d', 123))
+ call assert_equal('-123', printf('%+d', -123))
+ call assert_equal('+123', printf('%+ d', 123))
+ call assert_equal(' 123', printf('% d', 123))
+ call assert_equal(' 123', printf('% d', 123))
+ call assert_equal('-123', printf('% d', -123))
+
+ call assert_equal('00123', printf('%.*d', 5, 123))
+ call assert_equal(' 123', printf('% *d', 5, 123))
+ call assert_equal(' +123', printf('%+ *d', 5, 123))
+
+ call assert_equal('123', printf('%2d', 123))
+ call assert_equal(' 123', printf('%5d', 123))
+ call assert_equal('00123', printf('%05d', 123))
+ call assert_equal('123 ', printf('%-5d', 123))
+ call assert_equal('0x7b', printf('%#x', 123))
+ call assert_equal('0X7B', printf('%#X', 123))
+ call assert_equal('0173', printf('%#o', 123))
+ call assert_equal('0173', printf('%#O', 123))
+ call assert_equal('abc', printf('%#s', 'abc'))
+ call assert_equal('abc', printf('%#S', 'abc'))
+
+ call assert_equal(' 00123', printf('%6.5d', 123))
+ call assert_equal(' 0007b', printf('%6.5x', 123))
+
+ call assert_equal('abc', printf('%2s', 'abc'))
+ call assert_equal('abc', printf('%2S', 'abc'))
+ call assert_equal('abc', printf('%.4s', 'abc'))
+ call assert_equal('abc', printf('%.4S', 'abc'))
+ call assert_equal('ab', printf('%.2s', 'abc'))
+ call assert_equal('ab', printf('%.2S', 'abc'))
+ call assert_equal('', printf('%.0s', 'abc'))
+ call assert_equal('', printf('%.s', 'abc'))
+ call assert_equal(' abc', printf('%4s', 'abc'))
+ call assert_equal(' abc', printf('%4S', 'abc'))
+ call assert_equal('0abc', printf('%04s', 'abc'))
+ call assert_equal('0abc', printf('%04S', 'abc'))
+ call assert_equal('abc ', printf('%-4s', 'abc'))
+ call assert_equal('abc ', printf('%-4S', 'abc'))
+
+ call assert_equal('1%', printf('%d%%', 1))
+endfunc
+
+function Test_printf_float()
+ if has('float')
+ call assert_equal('1.230000', printf('%f', 1.23))
+ call assert_equal('1.230000', printf('%F', 1.23))
+ call assert_equal('1.23', printf('%g', 1.23))
+ call assert_equal('1.23', printf('%G', 1.23))
+ call assert_equal('1.230000e+00', printf('%e', 1.23))
+ call assert_equal('1.230000E+00', printf('%E', 1.23))
+ call assert_equal('1.200000e-02', printf('%e', 0.012))
+ call assert_equal('-1.200000e-02', printf('%e', -0.012))
+ call assert_equal('1.2', printf('%.1f', 1.23))
+
+ call assert_equal('inf', printf('%f', 1.0/0.0))
+
+ " This prints inf but shouldn't it print -inf instead?
+ call assert_match('^-\?inf$', printf('%f', -1.0/0.0))
+
+ " This prints -nan but shouldn't it print nan instead?
+ call assert_match('^-\?nan$', printf('%f', sqrt(-1.0)))
+ call assert_match('^-\?nan$', printf('%f', 0.0/0.0))
+
+ call assert_fails('echo printf("%f", "a")', 'E807:')
+ endif
+endfunc
+
+function Test_printf_errors()
+ call assert_fails('echo printf("%d", {})', 'E728:')
+ call assert_fails('echo printf("%d", [])', 'E745:')
+ call assert_fails('echo printf("%d", 1, 2)', 'E767:')
+ call assert_fails('echo printf("%*d", 1)', 'E766:')
+ call assert_fails('echo printf("%d", 1.2)', 'E805:')
+endfunc
+
function Test_printf_64bit()
if has('num64')
call assert_equal("123456789012345", printf('%d', 123456789012345))