annotate src/message_test.c @ 33353:b59205d0567e v9.0.1939

patch 9.0.1939: still a problem when processing LSP RPC requests Commit: https://github.com/vim/vim/commit/b80ae6cec34639abfb1a7080fb633346a81a5770 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Sep 24 23:38:46 2023 +0200 patch 9.0.1939: still a problem when processing LSP RPC requests Problem: still a problem when processing LSP RPC requests Solution: When processing async LSP RPC requests, compare sequence numbers only in response messages A LSP request message can be sent to the language server either synchronously (ch_evalexpr) or asynchronously (ch_sendexpr). In both cases, when looking for response messages by using the sequence number, LSP requests messages from the language server with the same sequence number should not be used. Patch 9.0.1927 fixed this issue for synchronous requests. This PR fixes the issue for asynchronous requests and adds additional tests. closes: #13158 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 24 Sep 2023 23:45:08 +0200
parents 05c320c640de
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 9581
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 *
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 *
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 */
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 /*
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 * message_test.c: Unittests for message.c
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 */
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 #undef NDEBUG
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 #include <assert.h>
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
17 // Must include main.c because it contains much more than just main()
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 #define NO_VIM_MAIN
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 #include "main.c"
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
21 // This file has to be included because some of the tested functions are
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
22 // static.
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 #include "message.c"
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
25 #ifndef MIN
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
26 # define MIN(x,y) ((x) < (y) ? (x) : (y))
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
27 #endif
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
28
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
29 // These formats are not standard in C printf() function.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
30 // Use a global variable rather than a literal format to disable
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
31 // -Wformat compiler warnings:
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
32 //
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
33 // - warning: '0' flag used with ‘%p’ gnu_printf format
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
34 // - warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
35 // - warning: unknown conversion type character ‘b’ in format
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
36 //
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
37 // These formats are in practise only used from vim script printf()
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
38 // function and never as literals in C code.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
39 char *fmt_012p = "%012p";
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
40 char *fmt_5S = "%5S";
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
41 char *fmt_06b = "%06b";
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
42 char *fmt_06pb = "%1$0.*2$b";
33148
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
43 char *fmt_06pb2 = "%2$0*1$b";
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
44 char *fmt_212s = "%2$s %1$s %2$s";
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
45 char *fmt_21s = "%2$s %1$s";
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
46
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 /*
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 * Test trunc_string().
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 */
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 static void
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 test_trunc_string(void)
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 {
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
53 char_u *buf; /*allocated every time to find uninit errors */
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
54 char_u *s;
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55
23946
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
56 // Should not write anything to destination if buflen is 0.
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
57 trunc_string((char_u *)"", NULL, 1, 0);
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
58
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
59 // Truncating an empty string does nothing.
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
60 buf = alloc(1);
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
61 trunc_string((char_u *)"", buf, 1, 1);
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
62 assert(buf[0] == NUL);
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
63 vim_free(buf);
0b1f5717dc4d patch 8.2.2515: memory access error when truncating an empty message
Bram Moolenaar <Bram@vim.org>
parents: 20105
diff changeset
64
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
65 // in place
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
66 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 STRCPY(buf, "text");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 assert(STRCMP(buf, "text") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
70 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
72 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 STRCPY(buf, "a short text");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 assert(STRCMP(buf, "a short text") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
76 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
78 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 STRCPY(buf, "a text tha just fits");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
80 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 assert(STRCMP(buf, "a text tha just fits") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
82 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
83
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
84 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
85 STRCPY(buf, "a text that nott fits");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 assert(STRCMP(buf, "a text t...nott fits") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
88 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
90 // copy from string to buf
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
91 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
92 s = vim_strsave((char_u *)"text");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
93 trunc_string(s, buf, 20, 40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
94 assert(STRCMP(buf, "text") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
95 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
96 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
97
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
98 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
99 s = vim_strsave((char_u *)"a text that fits");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
100 trunc_string(s, buf, 34, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
101 assert(STRCMP(buf, "a text that fits") == 0);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
102 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
103 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
104
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
105 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
106 s = vim_strsave((char_u *)"a short text");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
107 trunc_string(s, buf, 20, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
108 assert(STRCMP(buf, "a short text") == 0);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
109 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
110 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
112 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
113 s = vim_strsave((char_u *)"a text tha just fits");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
114 trunc_string(s, buf, 20, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
115 assert(STRCMP(buf, "a text tha just fits") == 0);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
116 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
117 vim_free(s);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
118
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
119 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
120 s = vim_strsave((char_u *)"a text that nott fits");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
121 trunc_string(s, buf, 20, 40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
122 assert(STRCMP(buf, "a text t...nott fits") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
123 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
124 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
125 }
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
126
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
127 /*
24709
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
128 * Test trunc_string() with mbyte chars.
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
129 */
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
130 static void
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
131 test_trunc_string_mbyte(void)
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
132 {
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
133 char_u *buf; // allocated every time to find uninit errors
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
134 char_u *s;
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
135
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
136 buf = alloc(40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
137 s = vim_strsave((char_u *)"Ä text tha just fits");
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
138 trunc_string(s, buf, 20, 40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
139 assert(STRCMP(buf, "Ä text tha just fits") == 0);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
140 vim_free(buf);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
141 vim_free(s);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
142
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
143 buf = alloc(40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
144 s = vim_strsave((char_u *)"a text ÄÖÜä nott fits");
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
145 trunc_string(s, buf, 20, 40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
146 assert(STRCMP(buf, "a text Ä...nott fits") == 0);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
147 vim_free(buf);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
148 vim_free(s);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
149
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
150 buf = alloc(40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
151 s = vim_strsave((char_u *)"a text that not fitsÄ");
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
152 trunc_string(s, buf, 20, 40);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
153 assert(STRCMP(buf, "a text t...not fitsÄ") == 0);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
154 vim_free(buf);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
155 vim_free(s);
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
156 }
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
157
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
158 /*
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
159 * Test vim_snprintf() with a focus on checking that truncation is
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
160 * correct when buffer is small, since it cannot be tested from
30986
360f286b5869 patch 9.0.0828: various typos
Bram Moolenaar <Bram@vim.org>
parents: 30310
diff changeset
161 * vim script tests. Check that:
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
162 * - no buffer overflows happens (with valgrind or asan)
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
163 * - output string is always NUL terminated.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
164 *
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
165 * Not all formats of vim_snprintf() are checked here. They are
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
166 * checked more exhaustively in Test_printf*() vim script tests.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
167 */
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
168 static void
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
169 test_vim_snprintf(void)
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
170 {
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
171 int n;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
172 size_t bsize;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
173 int bsize_int;
20105
785e2f0095a9 patch 8.2.0608: warning from clang when building message test
Bram Moolenaar <Bram@vim.org>
parents: 20007
diff changeset
174 void *ptr = (void *)0x87654321;
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
175
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
176 // Loop on various buffer sizes to make sure that truncation of
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
177 // vim_snprintf() is correct.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
178 for (bsize = 0; bsize < 15; ++bsize)
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
179 {
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
180 bsize_int = (int)bsize - 1;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
181
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
182 // buf is the heap rather than in the stack
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
183 // so valgrind can detect buffer overflows if any.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
184 // Use malloc() rather than alloc() as test checks with 0-size
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
185 // buffer and its content should then never be used.
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
186 char *buf = malloc(bsize);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
187
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
188 n = vim_snprintf(buf, bsize, "%.8g", 10000000.1);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
189 assert(n == 12);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
190 assert(bsize == 0 || STRNCMP(buf, "1.00000001e7", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
191 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
192
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
193 n = vim_snprintf(buf, bsize, "%d", 1234567);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
194 assert(n == 7);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
195 assert(bsize == 0 || STRNCMP(buf, "1234567", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
196 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
197
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
198 n = vim_snprintf(buf, bsize, "%ld", 1234567L);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
199 assert(n == 7);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
200 assert(bsize == 0 || STRNCMP(buf, "1234567", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
201 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
202
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
203 n = vim_snprintf(buf, bsize, "%9ld", 1234567L);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
204 assert(n == 9);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
205 assert(bsize == 0 || STRNCMP(buf, " 1234567", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
206 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
207
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
208 n = vim_snprintf(buf, bsize, "%-9ld", 1234567L);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
209 assert(n == 9);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
210 assert(bsize == 0 || STRNCMP(buf, "1234567 ", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
211 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
212
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
213 n = vim_snprintf(buf, bsize, "%x", 0xdeadbeef);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
214 assert(n == 8);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
215 assert(bsize == 0 || STRNCMP(buf, "deadbeef", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
216 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
217
19119
7816aa44c145 patch 8.2.0119: message test fails on some platforms
Bram Moolenaar <Bram@vim.org>
parents: 19011
diff changeset
218 n = vim_snprintf(buf, bsize, fmt_06b, (uvarnumber_T)12);
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
219 assert(n == 6);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
220 assert(bsize == 0 || STRNCMP(buf, "001100", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
221 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
222
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
223 n = vim_snprintf(buf, bsize, "%s %s", "one", "two");
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
224 assert(n == 7);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
225 assert(bsize == 0 || STRNCMP(buf, "one two", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
226 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
227
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
228 n = vim_snprintf(buf, bsize, "%f", 1.234);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
229 assert(n == 8);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
230 assert(bsize == 0 || STRNCMP(buf, "1.234000", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
231 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
232
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
233 n = vim_snprintf(buf, bsize, "%e", 1.234);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
234 assert(n == 12);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
235 assert(bsize == 0 || STRNCMP(buf, "1.234000e+00", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
236 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
237
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
238 n = vim_snprintf(buf, bsize, "%f", 0.0/0.0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
239 assert(n == 3);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
240 assert(bsize == 0 || STRNCMP(buf, "nan", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
241 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
242
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
243 n = vim_snprintf(buf, bsize, "%f", 1.0/0.0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
244 assert(n == 3);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
245 assert(bsize == 0 || STRNCMP(buf, "inf", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
246 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
247
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
248 n = vim_snprintf(buf, bsize, "%f", -1.0/0.0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
249 assert(n == 4);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
250 assert(bsize == 0 || STRNCMP(buf, "-inf", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
251 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
252
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
253 n = vim_snprintf(buf, bsize, "%f", -0.0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
254 assert(n == 9);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
255 assert(bsize == 0 || STRNCMP(buf, "-0.000000", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
256 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
257
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
258 n = vim_snprintf(buf, bsize, "%s", "漢語");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
259 assert(n == 6);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
260 assert(bsize == 0 || STRNCMP(buf, "漢語", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
261 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
262
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
263 n = vim_snprintf(buf, bsize, "%8s", "漢語");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
264 assert(n == 8);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
265 assert(bsize == 0 || STRNCMP(buf, " 漢語", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
266 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
267
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
268 n = vim_snprintf(buf, bsize, "%-8s", "漢語");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
269 assert(n == 8);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
270 assert(bsize == 0 || STRNCMP(buf, "漢語 ", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
271 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
272
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
273 n = vim_snprintf(buf, bsize, "%.3s", "漢語");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
274 assert(n == 3);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
275 assert(bsize == 0 || STRNCMP(buf, "漢", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
276 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
277
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
278 n = vim_snprintf(buf, bsize, fmt_5S, "foo");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
279 assert(n == 5);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
280 assert(bsize == 0 || STRNCMP(buf, " foo", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
281 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
282
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
283 n = vim_snprintf(buf, bsize, "%%%%%%");
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
284 assert(n == 3);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
285 assert(bsize == 0 || STRNCMP(buf, "%%%", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
286 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
287
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
288 n = vim_snprintf(buf, bsize, "%c%c", 1, 2);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
289 assert(n == 2);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
290 assert(bsize == 0 || STRNCMP(buf, "\x01\x02", bsize_int) == 0);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
291 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
292
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
293 // %p format is not tested in vim script tests Test_printf*()
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
294 // as it only makes sense in C code.
19352
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
295 // NOTE: SunOS libc doesn't use the prefix "0x" on %p.
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
296 #ifdef SUN_SYSTEM
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
297 # define PREFIX_LEN 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
298 # define PREFIX_STR1 ""
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
299 # define PREFIX_STR2 "00"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
300 #else
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
301 # define PREFIX_LEN 2
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
302 # define PREFIX_STR1 "0x"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
303 # define PREFIX_STR2 "0x"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
304 #endif
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
305 n = vim_snprintf(buf, bsize, "%p", ptr);
19352
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
306 assert(n == 8 + PREFIX_LEN);
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
307 assert(bsize == 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
308 || STRNCMP(buf, PREFIX_STR1 "87654321", bsize_int) == 0);
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
309 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
310
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
311 n = vim_snprintf(buf, bsize, fmt_012p, ptr);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
312 assert(n == 12);
19352
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
313 assert(bsize == 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
314 || STRNCMP(buf, PREFIX_STR2 "0087654321", bsize_int) == 0);
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
315 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
316
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
317 free(buf);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
318 }
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
319 }
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
320
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
321 /*
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
322 * Test vim_snprintf() with a focus on checking that positional
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
323 * arguments are correctly applied and skipped
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
324 */
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
325 static void
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
326 test_vim_snprintf_positional(void)
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
327 {
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
328 int n;
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
329 size_t bsize;
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
330 int bsize_int;
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
331
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
332 // Loop on various buffer sizes to make sure that truncation of
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
333 // vim_snprintf() is correct.
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
334 for (bsize = 0; bsize < 25; ++bsize)
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
335 {
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
336 bsize_int = (int)bsize - 1;
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
337
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
338 // buf is the heap rather than in the stack
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
339 // so valgrind can detect buffer overflows if any.
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
340 // Use malloc() rather than alloc() as test checks with 0-size
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
341 // buffer and its content should then never be used.
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
342 char *buf = malloc(bsize);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
343
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
344 n = vim_snprintf(buf, bsize, "%1$*2$ld", 1234567L, -9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
345 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
346 assert(bsize == 0 || STRNCMP(buf, "1234567 ", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
347 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
348
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
349 n = vim_snprintf(buf, bsize, "%1$*2$.*3$ld", 1234567L, -9, 5);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
350 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
351 assert(bsize == 0 || STRNCMP(buf, "1234567 ", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
352 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
353
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
354 n = vim_snprintf(buf, bsize, "%1$*3$.*2$ld", 1234567L, 5, -9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
355 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
356 assert(bsize == 0 || STRNCMP(buf, "1234567 ", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
357 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
358
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
359 n = vim_snprintf(buf, bsize, "%3$*1$.*2$ld", -9, 5, 1234567L);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
360 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
361 assert(bsize == 0 || STRNCMP(buf, "1234567 ", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
362 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
363
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
364 n = vim_snprintf(buf, bsize, "%1$ld", 1234567L);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
365 assert(n == 7);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
366 assert(bsize == 0 || STRNCMP(buf, "1234567", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
367 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
368
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
369 n = vim_snprintf(buf, bsize, "%1$*2$ld", 1234567L, 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
370 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
371 assert(bsize == 0 || STRNCMP(buf, " 1234567", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
372 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
373
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
374 n = vim_snprintf(buf, bsize, "%2$ld %1$d %3$lu", 12345, 9L, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
375 assert(n == 15);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
376 assert(bsize == 0 || STRNCMP(buf, "9 12345 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
377 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
378
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
379 n = vim_snprintf(buf, bsize, "%2$d %1$ld %3$lu", 1234567L, 9, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
380 assert(n == 17);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
381 assert(bsize == 0 || STRNCMP(buf, "9 1234567 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
382 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
383
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
384 n = vim_snprintf(buf, bsize, "%2$d %1$lld %3$lu", 1234567LL, 9, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
385 assert(n == 17);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
386 assert(bsize == 0 || STRNCMP(buf, "9 1234567 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
387 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
388
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
389 n = vim_snprintf(buf, bsize, "%2$ld %1$u %3$lu", 12345U, 9L, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
390 assert(n == 15);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
391 assert(bsize == 0 || STRNCMP(buf, "9 12345 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
392 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
393
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
394 n = vim_snprintf(buf, bsize, "%2$d %1$lu %3$lu", 1234567UL, 9, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
395 assert(n == 17);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
396 assert(bsize == 0 || STRNCMP(buf, "9 1234567 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
397 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
398
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
399 n = vim_snprintf(buf, bsize, "%2$d %1$llu %3$lu", 1234567LLU, 9, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
400 assert(n == 17);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
401 assert(bsize == 0 || STRNCMP(buf, "9 1234567 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
402 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
403
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
404 n = vim_snprintf(buf, bsize, "%2$d %1$x %3$lu", 0xdeadbeef, 9, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
405 assert(n == 18);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
406 assert(bsize == 0 || STRNCMP(buf, "9 deadbeef 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
407 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
408
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
409 n = vim_snprintf(buf, bsize, "%2$ld %1$c %3$lu", 'c', 9L, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
410 assert(n == 11);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
411 assert(bsize == 0 || STRNCMP(buf, "9 c 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
412 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
413
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
414 n = vim_snprintf(buf, bsize, "%2$ld %1$s %3$lu", "hi", 9L, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
415 assert(n == 12);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
416 assert(bsize == 0 || STRNCMP(buf, "9 hi 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
417 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
418
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
419 n = vim_snprintf(buf, bsize, "%2$ld %1$e %3$lu", 0.0, 9L, 7654321UL);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
420 assert(n == 22);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
421 assert(bsize == 0 || STRNCMP(buf, "9 0.000000e+00 7654321", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
422 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
423
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
424 n = vim_snprintf(buf, bsize, fmt_212s, "one", "two", "three");
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
425 assert(n == 11);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
426 assert(bsize == 0 || STRNCMP(buf, "two one two", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
427 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
428
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
429 n = vim_snprintf(buf, bsize, "%3$s %1$s %2$s", "one", "two", "three");
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
430 assert(n == 13);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
431 assert(bsize == 0 || STRNCMP(buf, "three one two", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
432 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
433
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
434 n = vim_snprintf(buf, bsize, "%1$d", 1234567);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
435 assert(n == 7);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
436 assert(bsize == 0 || STRNCMP(buf, "1234567", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
437 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
438
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
439 n = vim_snprintf(buf, bsize, "%1$x", 0xdeadbeef);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
440 assert(n == 8);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
441 assert(bsize == 0 || STRNCMP(buf, "deadbeef", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
442 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
443
33148
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
444 n = vim_snprintf(buf, bsize, fmt_06pb2, 6, (uvarnumber_T)12);
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
445 assert(n == 6);
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
446 assert(bsize == 0 || STRNCMP(buf, "001100", bsize_int) == 0);
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
447 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
7bc10151ce81 patch 9.0.1856: issues with formatting positional arguments
Christian Brabandt <cb@256bit.org>
parents: 32798
diff changeset
448
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
449 n = vim_snprintf(buf, bsize, fmt_06pb, (uvarnumber_T)12, 6);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
450 assert(n == 6);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
451 assert(bsize == 0 || STRNCMP(buf, "001100", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
452 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
453
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
454 n = vim_snprintf(buf, bsize, "%1$s %2$s", "one", "two");
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
455 assert(n == 7);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
456 assert(bsize == 0 || STRNCMP(buf, "one two", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
457 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
458
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
459 n = vim_snprintf(buf, bsize, fmt_06b, (uvarnumber_T)12);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
460 assert(n == 6);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
461 assert(bsize == 0 || STRNCMP(buf, "001100", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
462 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
463
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
464 n = vim_snprintf(buf, bsize, fmt_21s, "one", "two", "three");
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
465 assert(n == 7);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
466 assert(bsize == 0 || STRNCMP(buf, "two one", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
467 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
468
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
469 #ifdef FEAT_FLOAT
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
470 n = vim_snprintf(buf, bsize, "%1$f", 1.234);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
471 assert(n == 8);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
472 assert(bsize == 0 || STRNCMP(buf, "1.234000", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
473 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
474
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
475 n = vim_snprintf(buf, bsize, "%1$e", 1.234);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
476 assert(n == 12);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
477 assert(bsize == 0 || STRNCMP(buf, "1.234000e+00", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
478 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
479
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
480 n = vim_snprintf(buf, bsize, "%1$f", 0.0/0.0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
481 assert(n == 3);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
482 assert(bsize == 0 || STRNCMP(buf, "nan", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
483 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
484
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
485 n = vim_snprintf(buf, bsize, "%1$f", 1.0/0.0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
486 assert(n == 3);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
487 assert(bsize == 0 || STRNCMP(buf, "inf", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
488 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
489
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
490 n = vim_snprintf(buf, bsize, "%1$f", -1.0/0.0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
491 assert(n == 4);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
492 assert(bsize == 0 || STRNCMP(buf, "-inf", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
493 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
494
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
495 n = vim_snprintf(buf, bsize, "%1$f", -0.0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
496 assert(n == 9);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
497 assert(bsize == 0 || STRNCMP(buf, "-0.000000", bsize_int) == 0);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
498 assert(bsize == 0 || buf[MIN(n, bsize_int)] == '\0');
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
499 #endif
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
500
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
501 free(buf);
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
502 }
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
503 }
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
504
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
505 int
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
506 main(int argc, char **argv)
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
507 {
20007
aadd1cae2ff5 patch 8.2.0559: clearing a struct is verbose
Bram Moolenaar <Bram@vim.org>
parents: 19352
diff changeset
508 CLEAR_FIELD(params);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
509 params.argc = argc;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
510 params.argv = argv;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
511 common_init(&params);
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
512
28457
4dcccb2673fe patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 24709
diff changeset
513 set_option_value_give_err((char_u *)"encoding", 0, (char_u *)"utf-8", 0);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
514 init_chartab();
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
515 test_trunc_string();
24709
9d304d363ab6 patch 8.2.2893: multi-byte text in popup title shows up wrong
Bram Moolenaar <Bram@vim.org>
parents: 23946
diff changeset
516 test_trunc_string_mbyte();
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
517 test_vim_snprintf();
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
518 test_vim_snprintf_positional();
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
519
28457
4dcccb2673fe patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 24709
diff changeset
520 set_option_value_give_err((char_u *)"encoding", 0, (char_u *)"latin1", 0);
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
521 init_chartab();
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
522 test_trunc_string();
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
523 test_vim_snprintf();
32775
2d5e8c46508b patch 9.0.1704: Cannot use positional arguments for printf()
Christian Brabandt <cb@256bit.org>
parents: 30986
diff changeset
524 test_vim_snprintf_positional();
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
525
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
526 return 0;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
527 }