test_format.vim 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. " Tests for expressions.
  2. source check.vim
  3. source vim9.vim
  4. func Test_printf_pos_misc()
  5. let lines =<< trim END
  6. call assert_equal('123', printf('%1$d', 123))
  7. call assert_equal('', printf('%1$.0d', 0))
  8. call assert_equal('00005', printf('%1$5.5d', 5))
  9. call assert_equal('00005', printf('%1$*1$.5d', 5))
  10. call assert_equal('00005', printf('%1$5.*1$d', 5))
  11. call assert_equal('00005', printf('%1$*1$.*1$d', 5))
  12. call assert_equal('00005', printf('%1$*10$.5d%2$.0d%3$.0d%4$.0d%5$.0d%6$.0d%7$.0d%8$.0d%9$.0d', 5, 0, 0, 0, 0, 0, 0, 0, 0, 5))
  13. call assert_equal('00005', printf('%1$5.*10$d%2$.0d%3$.0d%4$.0d%5$.0d%6$.0d%7$.0d%8$.0d%9$.0d', 5, 0, 0, 0, 0, 0, 0, 0, 0, 5))
  14. call assert_equal('123', printf('%1$i', 123))
  15. call assert_equal('123', printf('%1$D', 123))
  16. call assert_equal('123', printf('%1$U', 123))
  17. call assert_equal('173', printf('%1$o', 123))
  18. call assert_equal('173', printf('%1$O', 123))
  19. call assert_equal('7b', printf('%1$x', 123))
  20. call assert_equal('7B', printf('%1$X', 123))
  21. call assert_equal('Printing 1 at width 1 gives: 1', 1->printf("Printing %1$d at width %1$d gives: %1$*1$d"))
  22. call assert_equal('Printing 2 at width 2 gives: 2', 2->printf("Printing %1$d at width %1$d gives: %1$*1$d"))
  23. call assert_equal('Printing 3 at width 3 gives: 3', 3->printf("Printing %1$d at width %1$d gives: %1$*1$d"))
  24. call assert_equal('Printing 1 at width/precision 1.1 gives: 1', 1->printf("Printing %1$d at width/precision %1$d.%1$d gives: %1$*1$.*1$d"))
  25. call assert_equal('Printing 2 at width/precision 2.2 gives: 02', 2->printf("Printing %1$d at width/precision %1$d.%1$d gives: %1$*1$.*1$d"))
  26. call assert_equal('Printing 3 at width/precision 3.3 gives: 003', 3->printf("Printing %1$d at width/precision %1$d.%1$d gives: %1$*1$.*1$d"))
  27. call assert_equal('123', printf('%1$hd', 123))
  28. call assert_equal('-123', printf('%1$hd', -123))
  29. call assert_equal('-1', printf('%1$hd', 0xFFFF))
  30. call assert_equal('-1', printf('%1$hd', 0x1FFFFF))
  31. call assert_equal('123', printf('%1$hu', 123))
  32. call assert_equal('65413', printf('%1$hu', -123))
  33. call assert_equal('65535', printf('%1$hu', 0xFFFF))
  34. call assert_equal('65535', printf('%1$hu', 0x1FFFFF))
  35. call assert_equal('123', printf('%1$ld', 123))
  36. call assert_equal('-123', printf('%1$ld', -123))
  37. call assert_equal('65535', printf('%1$ld', 0xFFFF))
  38. call assert_equal('131071', printf('%1$ld', 0x1FFFF))
  39. call assert_equal('{', printf('%1$c', 123))
  40. call assert_equal('abc', printf('%1$s', 'abc'))
  41. call assert_equal('abc', printf('%1$S', 'abc'))
  42. call assert_equal('+123', printf('%1$+d', 123))
  43. call assert_equal('-123', printf('%1$+d', -123))
  44. call assert_equal('+123', printf('%1$+ d', 123))
  45. call assert_equal(' 123', printf('%1$ d', 123))
  46. call assert_equal(' 123', printf('%1$ d', 123))
  47. call assert_equal('-123', printf('%1$ d', -123))
  48. call assert_equal(' 123', printf('%2$*1$d', 5, 123))
  49. call assert_equal('123 ', printf('%2$*1$d', -5, 123))
  50. call assert_equal('00123', printf('%2$.*1$d', 5, 123))
  51. call assert_equal(' 123', printf('%2$ *1$d', 5, 123))
  52. call assert_equal(' +123', printf('%2$+ *1$d', 5, 123))
  53. call assert_equal(' 123', printf('%1$*2$d', 123, 5))
  54. call assert_equal('123 ', printf('%1$*2$d', 123, -5))
  55. call assert_equal('00123', printf('%1$.*2$d', 123, 5))
  56. call assert_equal(' 123', printf('%1$ *2$d', 123, 5))
  57. call assert_equal(' +123', printf('%1$+ *2$d', 123, 5))
  58. call assert_equal('foobar', printf('%2$.*1$s', 9, 'foobar'))
  59. call assert_equal('foo', printf('%2$.*1$s', 3, 'foobar'))
  60. call assert_equal('', printf('%2$.*1$s', 0, 'foobar'))
  61. call assert_equal('foobar', printf('%2$.*1$s', -1, 'foobar'))
  62. #" Unrecognized format specifier kept as-is.
  63. call assert_equal('_123', printf("%_%1$d", 123))
  64. #" Test alternate forms.
  65. call assert_equal('0x7b', printf('%1$#x', 123))
  66. call assert_equal('0X7B', printf('%1$#X', 123))
  67. call assert_equal('0173', printf('%1$#o', 123))
  68. call assert_equal('0173', printf('%1$#O', 123))
  69. call assert_equal('abc', printf('%1$#s', 'abc'))
  70. call assert_equal('abc', printf('%1$#S', 'abc'))
  71. call assert_equal('1%', printf('%1$d%%', 1))
  72. call assert_notequal('', printf('%1$p', "abc"))
  73. call assert_notequal('', printf('%2$d %1$p %3$s', "abc", 2, "abc"))
  74. #" Try argument re-use and argument swapping
  75. call assert_equal('one two one', printf('%1$s %2$s %1$s', "one", "two"))
  76. call assert_equal('Screen height: 400', printf('%1$s height: %2$d', "Screen", 400))
  77. call assert_equal('400 is: Screen height', printf('%2$d is: %1$s height', "Screen", 400))
  78. #" Try out lots of combinations of argument types to skip
  79. call assert_equal('9 12345 7654321', printf('%2$ld %1$d %3$lu', 12345, 9, 7654321))
  80. call assert_equal('9 1234567 7654321', printf('%2$d %1$ld %3$lu', 1234567, 9, 7654321))
  81. call assert_equal('9 1234567 7654321', printf('%2$d %1$lld %3$lu', 1234567, 9, 7654321))
  82. call assert_equal('9 12345 7654321', printf('%2$ld %1$u %3$lu', 12345, 9, 7654321))
  83. call assert_equal('9 1234567 7654321', printf('%2$d %1$lu %3$lu', 1234567, 9, 7654321))
  84. call assert_equal('9 1234567 7654321', printf('%2$d %1$llu %3$lu', 1234567, 9, 7654321))
  85. call assert_equal('9 1234567 7654321', printf('%2$d %1$llu %3$lu', 1234567, 9, 7654321))
  86. call assert_equal('9 deadbeef 7654321', printf('%2$d %1$x %3$lu', 0xdeadbeef, 9, 7654321))
  87. call assert_equal('9 c 7654321', printf('%2$ld %1$c %3$lu', 99, 9, 7654321))
  88. call assert_equal('9 hi 7654321', printf('%2$ld %1$s %3$lu', "hi", 9, 7654321))
  89. call assert_equal('9 0.000000e+00 7654321', printf('%2$ld %1$e %3$lu', 0.0, 9, 7654321))
  90. END
  91. call CheckLegacyAndVim9Success(lines)
  92. endfunc
  93. func Test_printf_pos_float()
  94. let lines =<< trim END
  95. call assert_equal('1.000000', printf('%1$f', 1))
  96. call assert_equal('1.230000', printf('%1$f', 1.23))
  97. call assert_equal('1.230000', printf('%1$F', 1.23))
  98. call assert_equal('9999999.9', printf('%1$g', 9999999.9))
  99. call assert_equal('9999999.9', printf('%1$G', 9999999.9))
  100. call assert_equal('1.230000e+00', printf('%1$e', 1.23))
  101. call assert_equal('1.230000E+00', printf('%1$E', 1.23))
  102. call assert_equal('1.200000e-02', printf('%1$e', 0.012))
  103. call assert_equal('-1.200000e-02', printf('%1$e', -0.012))
  104. call assert_equal('0.33', printf('%1$.2f', 1.0 / 3.0))
  105. #" When precision is 0, the dot should be omitted.
  106. call assert_equal(' 2', printf('%1$*2$.f', 7.0 / 3.0, 3))
  107. call assert_equal(' 2', printf('%2$*1$.f', 3, 7.0 / 3.0))
  108. call assert_equal(' 2', printf('%1$*2$.g', 7.0 / 3.0, 3))
  109. call assert_equal(' 2', printf('%2$*1$.g', 3, 7.0 / 3.0))
  110. call assert_equal(' 2e+00', printf('%1$*2$.e', 7.0 / 3.0, 7))
  111. call assert_equal(' 2e+00', printf('%2$*1$.e', 7, 7.0 / 3.0))
  112. #" Float zero can be signed.
  113. call assert_equal('+0.000000', printf('%1$+f', 0.0))
  114. call assert_equal('0.000000', printf('%1$f', 1.0 / (1.0 / 0.0)))
  115. call assert_equal('-0.000000', printf('%1$f', 1.0 / (-1.0 / 0.0)))
  116. call assert_equal('0.0', printf('%1$s', 1.0 / (1.0 / 0.0)))
  117. call assert_equal('-0.0', printf('%1$s', 1.0 / (-1.0 / 0.0)))
  118. call assert_equal('0.0', printf('%1$S', 1.0 / (1.0 / 0.0)))
  119. call assert_equal('-0.0', printf('%1$S', 1.0 / (-1.0 / 0.0)))
  120. #" Float infinity can be signed.
  121. call assert_equal('inf', printf('%1$f', 1.0 / 0.0))
  122. call assert_equal('-inf', printf('%1$f', -1.0 / 0.0))
  123. call assert_equal('inf', printf('%1$g', 1.0 / 0.0))
  124. call assert_equal('-inf', printf('%1$g', -1.0 / 0.0))
  125. call assert_equal('inf', printf('%1$e', 1.0 / 0.0))
  126. call assert_equal('-inf', printf('%1$e', -1.0 / 0.0))
  127. call assert_equal('INF', printf('%1$F', 1.0 / 0.0))
  128. call assert_equal('-INF', printf('%1$F', -1.0 / 0.0))
  129. call assert_equal('INF', printf('%1$E', 1.0 / 0.0))
  130. call assert_equal('-INF', printf('%1$E', -1.0 / 0.0))
  131. call assert_equal('INF', printf('%1$E', 1.0 / 0.0))
  132. call assert_equal('-INF', printf('%1$G', -1.0 / 0.0))
  133. call assert_equal('+inf', printf('%1$+f', 1.0 / 0.0))
  134. call assert_equal('-inf', printf('%1$+f', -1.0 / 0.0))
  135. call assert_equal(' inf', printf('%1$ f', 1.0 / 0.0))
  136. call assert_equal(' inf', printf('%1$*2$f', 1.0 / 0.0, 6))
  137. call assert_equal(' -inf', printf('%1$*2$f', -1.0 / 0.0, 6))
  138. call assert_equal(' inf', printf('%1$*2$g', 1.0 / 0.0, 6))
  139. call assert_equal(' -inf', printf('%1$*2$g', -1.0 / 0.0, 6))
  140. call assert_equal(' +inf', printf('%1$+*2$f', 1.0 / 0.0, 6))
  141. call assert_equal(' inf', printf('%1$ *2$f', 1.0 / 0.0, 6))
  142. call assert_equal(' +inf', printf('%1$+0*2$f', 1.0 / 0.0, 6))
  143. call assert_equal('inf ', printf('%1$-*2$f', 1.0 / 0.0, 6))
  144. call assert_equal('-inf ', printf('%1$-*2$f', -1.0 / 0.0, 6))
  145. call assert_equal('+inf ', printf('%1$-+*2$f', 1.0 / 0.0, 6))
  146. call assert_equal(' inf ', printf('%1$- *2$f', 1.0 / 0.0, 6))
  147. call assert_equal('-INF ', printf('%1$-*2$F', -1.0 / 0.0, 6))
  148. call assert_equal('+INF ', printf('%1$-+*2$F', 1.0 / 0.0, 6))
  149. call assert_equal(' INF ', printf('%1$- *2$F', 1.0 / 0.0, 6))
  150. call assert_equal('INF ', printf('%1$-*2$G', 1.0 / 0.0, 6))
  151. call assert_equal('-INF ', printf('%1$-*2$G', -1.0 / 0.0, 6))
  152. call assert_equal('INF ', printf('%1$-*2$E', 1.0 / 0.0, 6))
  153. call assert_equal('-INF ', printf('%1$-*2$E', -1.0 / 0.0, 6))
  154. call assert_equal(' inf', printf('%2$*1$f', 6, 1.0 / 0.0))
  155. call assert_equal(' -inf', printf('%2$*1$f', 6, -1.0 / 0.0))
  156. call assert_equal(' inf', printf('%2$*1$g', 6, 1.0 / 0.0))
  157. call assert_equal(' -inf', printf('%2$*1$g', 6, -1.0 / 0.0))
  158. call assert_equal(' +inf', printf('%2$+*1$f', 6, 1.0 / 0.0))
  159. call assert_equal(' inf', printf('%2$ *1$f', 6, 1.0 / 0.0))
  160. call assert_equal(' +inf', printf('%2$+0*1$f', 6, 1.0 / 0.0))
  161. call assert_equal('inf ', printf('%2$-*1$f', 6, 1.0 / 0.0))
  162. call assert_equal('-inf ', printf('%2$-*1$f', 6, -1.0 / 0.0))
  163. call assert_equal('+inf ', printf('%2$-+*1$f', 6, 1.0 / 0.0))
  164. call assert_equal(' inf ', printf('%2$- *1$f', 6, 1.0 / 0.0))
  165. call assert_equal('-INF ', printf('%2$-*1$F', 6, -1.0 / 0.0))
  166. call assert_equal('+INF ', printf('%2$-+*1$F', 6, 1.0 / 0.0))
  167. call assert_equal(' INF ', printf('%2$- *1$F', 6, 1.0 / 0.0))
  168. call assert_equal('INF ', printf('%2$-*1$G', 6, 1.0 / 0.0))
  169. call assert_equal('-INF ', printf('%2$-*1$G', 6, -1.0 / 0.0))
  170. call assert_equal('INF ', printf('%2$-*1$E', 6, 1.0 / 0.0))
  171. call assert_equal('-INF ', printf('%2$-*1$E', 6, -1.0 / 0.0))
  172. call assert_equal("str2float('inf')", printf('%1$s', 1.0 / 0.0))
  173. call assert_equal("-str2float('inf')", printf('%1$s', -1.0 / 0.0))
  174. #" Test special case where max precision is truncated at 340.
  175. call assert_equal('1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%1$.*2$f', 1.0, 330))
  176. call assert_equal('1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%2$.*1$f', 330, 1.0))
  177. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%1$.*2$f', 1.0, 340))
  178. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%2$.*1$f', 340, 1.0))
  179. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%1$.*2$f', 1.0, 350))
  180. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%2$.*1$f', 350, 1.0))
  181. #" Float nan (not a number) has no sign.
  182. call assert_equal('nan', printf('%1$f', sqrt(-1.0)))
  183. call assert_equal('nan', printf('%1$f', 0.0 / 0.0))
  184. call assert_equal('nan', printf('%1$f', -0.0 / 0.0))
  185. call assert_equal('nan', printf('%1$g', 0.0 / 0.0))
  186. call assert_equal('nan', printf('%1$e', 0.0 / 0.0))
  187. call assert_equal('NAN', printf('%1$F', 0.0 / 0.0))
  188. call assert_equal('NAN', printf('%1$G', 0.0 / 0.0))
  189. call assert_equal('NAN', printf('%1$E', 0.0 / 0.0))
  190. call assert_equal('NAN', printf('%1$F', -0.0 / 0.0))
  191. call assert_equal('NAN', printf('%1$G', -0.0 / 0.0))
  192. call assert_equal('NAN', printf('%1$E', -0.0 / 0.0))
  193. call assert_equal(' nan', printf('%1$*2$f', 0.0 / 0.0, 6))
  194. call assert_equal(' nan', printf('%1$0*2$f', 0.0 / 0.0, 6))
  195. call assert_equal('nan ', printf('%1$-*2$f', 0.0 / 0.0, 6))
  196. call assert_equal('nan ', printf('%1$- *2$f', 0.0 / 0.0, 6))
  197. call assert_equal(' nan', printf('%2$*1$f', 6, 0.0 / 0.0))
  198. call assert_equal(' nan', printf('%2$0*1$f', 6, 0.0 / 0.0))
  199. call assert_equal('nan ', printf('%2$-*1$f', 6, 0.0 / 0.0))
  200. call assert_equal('nan ', printf('%2$- *1$f', 6, 0.0 / 0.0))
  201. call assert_equal("str2float('nan')", printf('%1$s', 0.0 / 0.0))
  202. call assert_equal("str2float('nan')", printf('%1$s', -0.0 / 0.0))
  203. call assert_equal("str2float('nan')", printf('%1$S', 0.0 / 0.0))
  204. call assert_equal("str2float('nan')", printf('%1$S', -0.0 / 0.0))
  205. END
  206. call CheckLegacyAndVim9Success(lines)
  207. endfunc
  208. func Test_printf_pos_errors()
  209. call CheckLegacyAndVim9Failure(['echo printf("%1$d", {})'], 'E728:')
  210. call CheckLegacyAndVim9Failure(['echo printf("%1$d", [])'], 'E745:')
  211. call CheckLegacyAndVim9Failure(['echo printf("%1$d", 1, 2)'], 'E767:')
  212. call CheckLegacyAndVim9Failure(['echo printf("%*d", 1)'], 'E766:')
  213. call CheckLegacyAndVim9Failure(['echo printf("%1$s")'], 'E1503:')
  214. call CheckLegacyAndVim9Failure(['echo printf("%1$d", 1.2)'], 'E805:')
  215. call CheckLegacyAndVim9Failure(['echo printf("%1$f")'], 'E1503:')
  216. call CheckLegacyAndVim9Failure(['echo printf("%f", "a")'], 'E807:')
  217. call CheckLegacyAndVim9Failure(["call printf('%1$d%2$d', 1, 3, 4)"], "E767:")
  218. call CheckLegacyAndVim9Failure(["call printf('%2$d%d', 1, 3)"], "E1500:")
  219. call CheckLegacyAndVim9Failure(["call printf('%d%2$d', 1, 3)"], "E1500:")
  220. call CheckLegacyAndVim9Failure(["call printf('%2$*1$d%d', 1, 3)"], "E1500:")
  221. call CheckLegacyAndVim9Failure(["call printf('%d%2$*1$d', 1, 3)"], "E1500:")
  222. call CheckLegacyAndVim9Failure(["call printf('%2$.*1$d%d', 1, 3)"], "E1500:")
  223. call CheckLegacyAndVim9Failure(["call printf('%d%2$.*1$d', 1, 3)"], "E1500:")
  224. call CheckLegacyAndVim9Failure(["call printf('%1$%')"], "E1500:")
  225. call CheckLegacyAndVim9Failure(["call printf('%1$')"], "E1500:")
  226. call CheckLegacyAndVim9Failure(["call printf('%1$_')"], "E1500:")
  227. call CheckLegacyAndVim9Failure(["call printf('%1$*3$.*d', 3)"], "E1500:")
  228. call CheckLegacyAndVim9Failure(["call printf('%1$*.*2$d', 3)"], "E1500:")
  229. call CheckLegacyAndVim9Failure(["call printf('%1$*.*d', 3)"], "E1500:")
  230. call CheckLegacyAndVim9Failure(["call printf('%*.*1$d', 3)"], "E1500:")
  231. call CheckLegacyAndVim9Failure(["call printf('%*1$.*d', 3)"], "E1500:")
  232. call CheckLegacyAndVim9Failure(["call printf('%*1$.*1$d', 3)"], "E1500:")
  233. call CheckLegacyAndVim9Failure(["call printf('%2$d', 3, 3)"], "E1501:")
  234. call CheckLegacyAndVim9Failure(["call printf('%2$*1$d %1$ld', 3, 3)"], "E1502:")
  235. call CheckLegacyAndVim9Failure(["call printf('%1$s %1$*1$d', 3)"], "E1502:")
  236. call CheckLegacyAndVim9Failure(["call printf('%1$p %1$*1$d', 3)"], "E1502:")
  237. call CheckLegacyAndVim9Failure(["call printf('%1$f %1$*1$d', 3)"], "E1502:")
  238. call CheckLegacyAndVim9Failure(["call printf('%1$lud %1$*1$d', 3)"], "E1502:")
  239. call CheckLegacyAndVim9Failure(["call printf('%1$llud %1$*1$d', 3)"], "E1502:")
  240. call CheckLegacyAndVim9Failure(["call printf('%1$lld %1$*1$d', 3)"], "E1502:")
  241. call CheckLegacyAndVim9Failure(["call printf('%1$s %1$*1$d', 3)"], "E1502:")
  242. call CheckLegacyAndVim9Failure(["call printf('%1$c %1$*1$d', 3)"], "E1502:")
  243. call CheckLegacyAndVim9Failure(["call printf('%1$ld %1$*1$d', 3)"], "E1502:")
  244. call CheckLegacyAndVim9Failure(["call printf('%1$ld %2$*1$d', 3, 3)"], "E1502:")
  245. call CheckLegacyAndVim9Failure(["call printf('%1$*1$ld', 3)"], "E1502:")
  246. call CheckLegacyAndVim9Failure(["call printf('%1$*1$.*1$ld', 3)"], "E1502:")
  247. call CheckLegacyAndVim9Failure(["call printf('%1$d%2$d', 3)"], "E1503:")
  248. call CheckLegacyAndVim9Failure(["call printf('%1$d %1$s', 3)"], "E1504:")
  249. call CheckLegacyAndVim9Failure(["call printf('%1$ld %1$s', 3)"], "E1504:")
  250. call CheckLegacyAndVim9Failure(["call printf('%1$ud %1$d', 3)"], "E1504:")
  251. call CheckLegacyAndVim9Failure(["call printf('%1$s %1$f', 3.0)"], "E1504:")
  252. call CheckLegacyAndVim9Failure(["call printf('%1$*1$d %1$ld', 3)"], "E1504:")
  253. call CheckLegacyAndVim9Failure(["call printf('%1$s %1$d', 3)"], "E1504:")
  254. call CheckLegacyAndVim9Failure(["call printf('%1$p %1$d', 3)"], "E1504:")
  255. call CheckLegacyAndVim9Failure(["call printf('%1$f %1$d', 3)"], "E1504:")
  256. call CheckLegacyAndVim9Failure(["call printf('%1$lud %1$d', 3)"], "E1504:")
  257. call CheckLegacyAndVim9Failure(["call printf('%1$llud %1$d', 3)"], "E1504:")
  258. call CheckLegacyAndVim9Failure(["call printf('%1$lld %1$d', 3)"], "E1504:")
  259. call CheckLegacyAndVim9Failure(["call printf('%1$s %1$d', 3)"], "E1504:")
  260. call CheckLegacyAndVim9Failure(["call printf('%1$c %1$d', 3)"], "E1504:")
  261. call CheckLegacyAndVim9Failure(["call printf('%1$ld %1$d', 3)"], "E1504:")
  262. call CheckLegacyAndVim9Failure(["call printf('%1$.2$d', 3)"], "E1505:")
  263. call CheckLegacyAndVim9Failure(["call printf('%01$d', 3)"], "E1505:")
  264. call CheckLegacyAndVim9Failure(["call printf('%01$0d', 3)"], "E1505:")
  265. call CheckLegacyAndVim9Failure(["call printf('%1$*2d', 3)"], "E1505:")
  266. call CheckLegacyAndVim9Failure(["call printf('%1$*3.*2$d', 3)"], "E1505:")
  267. call CheckLegacyAndVim9Failure(["call printf('%1$*3$.2$d', 3)"], "E1505:")
  268. call CheckLegacyAndVim9Failure(["call printf('%1$*3$.*2d', 3)"], "E1505:")
  269. call CheckLegacyAndVim9Failure(["call printf('%1$1$.5d', 5)"], "E1505:")
  270. call CheckLegacyAndVim9Failure(["call printf('%1$5.1$d', 5)"], "E1505:")
  271. call CheckLegacyAndVim9Failure(["call printf('%1$1$.1$d', 5)"], "E1505:")
  272. call CheckLegacyAndVim9Failure(["call printf('%.123456789$d', 5)"], "E1510:")
  273. call CheckLegacyAndVim9Failure(["call printf('%.123456789d', 5)"], "E1510:")
  274. call CheckLegacyAndVim9Failure(["call printf('%123456789$d', 5)"], "E1510:")
  275. call CheckLegacyAndVim9Failure(["call printf('%123456789d', 5)"], "E1510:")
  276. call CheckLegacyAndVim9Failure(["call printf('%123456789$5.5d', 5)"], "E1510:")
  277. call CheckLegacyAndVim9Failure(["call printf('%1$123456789.5d', 5)"], "E1510:")
  278. call CheckLegacyAndVim9Failure(["call printf('%1$5.123456789d', 5)"], "E1510:")
  279. call CheckLegacyAndVim9Failure(["call printf('%123456789$987654321.5d', 5)"], "E1510:")
  280. call CheckLegacyAndVim9Failure(["call printf('%1$123456789.987654321d', 5)"], "E1510:")
  281. call CheckLegacyAndVim9Failure(["call printf('%123456789$5.987654321d', 5)"], "E1510:")
  282. call CheckLegacyAndVim9Failure(["call printf('%123456789$*1$.5d', 5)"], "E1510:")
  283. call CheckLegacyAndVim9Failure(["call printf('%1$*123456789$.5d', 5)"], "E1510:")
  284. call CheckLegacyAndVim9Failure(["call printf('%1$*1$.123456789d', 5)"], "E1510:")
  285. call CheckLegacyAndVim9Failure(["call printf('%123456789$*987654321$.5d', 5)"], "E1510:")
  286. call CheckLegacyAndVim9Failure(["call printf('%1$*123456789$.987654321d', 5)"], "E1510:")
  287. call CheckLegacyAndVim9Failure(["call printf('%123456789$*1$.987654321d', 5)"], "E1510:")
  288. call CheckLegacyAndVim9Failure(["call printf('%123456789$5.*1$d', 5)"], "E1510:")
  289. call CheckLegacyAndVim9Failure(["call printf('%1$123456789.*1$d', 5)"], "E1510:")
  290. call CheckLegacyAndVim9Failure(["call printf('%1$5.*123456789$d', 5)"], "E1510:")
  291. call CheckLegacyAndVim9Failure(["call printf('%123456789$987654321.*1$d', 5)"], "E1510:")
  292. call CheckLegacyAndVim9Failure(["call printf('%1$123456789.*987654321$d', 5)"], "E1510:")
  293. call CheckLegacyAndVim9Failure(["call printf('%123456789$5.*987654321$d', 5)"], "E1510:")
  294. call CheckLegacyAndVim9Failure(["call printf('%123456789$*1$.*1$d', 5)"], "E1510:")
  295. call CheckLegacyAndVim9Failure(["call printf('%1$*123456789$.*1$d', 5)"], "E1510:")
  296. call CheckLegacyAndVim9Failure(["call printf('%1$*1$.*123456789d', 5)"], "E1510:")
  297. call CheckLegacyAndVim9Failure(["call printf('%123456789$*987654321$.*1$d', 5)"], "E1510:")
  298. call CheckLegacyAndVim9Failure(["call printf('%1$*123456789$.*987654321$d', 5)"], "E1510:")
  299. call CheckLegacyAndVim9Failure(["call printf('%123456789$*1$.*987654321$d', 5)"], "E1510:")
  300. call CheckLegacyAndVim9Failure(["call printf('%1$*2$.*1$d', 5, 9999)"], "E1510:")
  301. call CheckLegacyAndVim9Failure(["call printf('%1$*1$.*2$d', 5, 9999)"], "E1510:")
  302. call CheckLegacyAndVim9Failure(["call printf('%2$*3$.*1$d', 5, 9123, 9321)"], "E1510:")
  303. call CheckLegacyAndVim9Failure(["call printf('%1$*2$.*3$d', 5, 9123, 9321)"], "E1510:")
  304. call CheckLegacyAndVim9Failure(["call printf('%2$*1$.*3$d', 5, 9123, 9312)"], "E1510:")
  305. call CheckLegacyAndVim9Failure(["call printf('%1$*2$d', 5, 9999)"], "E1510:")
  306. endfunc
  307. func Test_printf_pos_64bit()
  308. let lines =<< trim END
  309. call assert_equal("123456789012345", printf('%1$d', 123456789012345))
  310. END
  311. call CheckLegacyAndVim9Success(lines)
  312. endfunc
  313. func Test_printf_pos_spec_s()
  314. let lines =<< trim END
  315. #" number
  316. call assert_equal("1234567890", printf('%1$s', 1234567890))
  317. #" string
  318. call assert_equal("abcdefgi", printf('%1$s', "abcdefgi"))
  319. #" float
  320. call assert_equal("1.23", printf('%1$s', 1.23))
  321. #" list
  322. VAR lvalue = [1, 'two', ['three', 4]]
  323. call assert_equal(string(lvalue), printf('%1$s', lvalue))
  324. #" dict
  325. VAR dvalue = {'key1': 'value1', 'key2': ['list', 'lvalue'], 'key3': {'dict': 'lvalue'}}
  326. call assert_equal(string(dvalue), printf('%1$s', dvalue))
  327. #" funcref
  328. call assert_equal('printf', printf('%1$s', 'printf'->function()))
  329. #" partial
  330. call assert_equal(string(function('printf', ['%1$s'])), printf('%1$s', function('printf', ['%1$s'])))
  331. END
  332. call CheckLegacyAndVim9Success(lines)
  333. endfunc
  334. func Test_printf_pos_spec_b()
  335. let lines =<< trim END
  336. call assert_equal("0", printf('%1$b', 0))
  337. call assert_equal("00001100", printf('%1$0*2$b', 12, 8))
  338. call assert_equal("11111111", printf('%1$0*2$b', 0xff, 8))
  339. call assert_equal(" 1111011", printf('%1$*2$b', 123, 10))
  340. call assert_equal("0001111011", printf('%1$0*2$b', 123, 10))
  341. call assert_equal(" 0b1111011", printf('%1$#*2$b', 123, 10))
  342. call assert_equal("0B01111011", printf('%1$#0*2$B', 123, 10))
  343. call assert_equal("00001100", printf('%2$0*1$b', 8, 12))
  344. call assert_equal("11111111", printf('%2$0*1$b', 8, 0xff))
  345. call assert_equal(" 1111011", printf('%2$*1$b', 10, 123))
  346. call assert_equal("0001111011", printf('%2$0*1$b', 10, 123))
  347. call assert_equal(" 0b1111011", printf('%2$#*1$b', 10, 123))
  348. call assert_equal("0B01111011", printf('%2$#0*1$B', 10, 123))
  349. call assert_equal("1001001100101100000001011010010", printf('%1$b', 1234567890))
  350. call assert_equal("11100000100100010000110000011011101111101111001", printf('%1$b', 123456789012345))
  351. call assert_equal("1111111111111111111111111111111111111111111111111111111111111111", printf('%1$b', -1))
  352. END
  353. call CheckLegacyAndVim9Success(lines)
  354. endfunc
  355. " vim: shiftwidth=2 sts=2 expandtab