annotate src/message_test.c @ 19736:4174c4da6ff7 v8.2.0424

patch 8.2.0424: checking for wrong return value Commit: https://github.com/vim/vim/commit/97acfc781bdb7fa2838dc6e0e7f9952ea61bb2fd Author: Bram Moolenaar <Bram@vim.org> Date: Sun Mar 22 13:44:28 2020 +0100 patch 8.2.0424: checking for wrong return value Problem: Checking for wrong return value. (Tom) Solution: Invert the check and fix the test.
author Bram Moolenaar <Bram@vim.org>
date Sun, 22 Mar 2020 13:45:08 +0100
parents c617ba6413bb
children aadd1cae2ff5
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";
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
42
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 /*
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 * Test trunc_string().
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 */
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 static void
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 test_trunc_string(void)
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 {
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
49 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
50 char_u *s;
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
52 // in place
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
53 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 STRCPY(buf, "text");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 assert(STRCMP(buf, "text") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
57 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
59 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 STRCPY(buf, "a short text");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 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
63 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
65 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66 STRCPY(buf, "a text tha just fits");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 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
69 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
71 buf = alloc(40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72 STRCPY(buf, "a text that nott fits");
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 trunc_string(buf, buf, 20, 40);
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 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
75 vim_free(buf);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
76
18931
80b40bd5ec1a patch 8.2.0026: still some /* */ comments
Bram Moolenaar <Bram@vim.org>
parents: 11309
diff changeset
77 // copy from string to buf
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
78 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
79 s = vim_strsave((char_u *)"text");
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
80 trunc_string(s, buf, 20, 40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 assert(STRCMP(buf, "text") == 0);
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
82 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
83 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
84
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
85 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
86 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
87 trunc_string(s, buf, 34, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
88 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
89 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
90 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
91
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
92 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
93 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
94 trunc_string(s, buf, 20, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
95 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
96 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
97 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
98
9581
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
99 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
100 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
101 trunc_string(s, buf, 20, 40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
102 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
103 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
104 vim_free(s);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
105
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
106 buf = alloc(40);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
107 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
108 trunc_string(s, buf, 20, 40);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
109 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
110 vim_free(buf);
716382aaa0c0 commit https://github.com/vim/vim/commit/b9644433d2728e99fab874e5e33147ad95d23a31
Christian Brabandt <cb@256bit.org>
parents: 9542
diff changeset
111 vim_free(s);
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
112 }
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
113
19011
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
114 /*
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
115 * 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
116 * correct when buffer is small, since it cannot be tested from
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
117 * vim scrip tests. Check that:
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
118 * - 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
119 * - 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
120 *
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
121 * 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
122 * 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
123 */
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
124 static void
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
125 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
126 {
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
127 int n;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
128 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
129 int bsize_int;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
130 char *ptr = (char *)0x87654321;
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
131
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
132 // 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
133 // 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
134 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
135 {
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
136 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
137
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
138 // 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
139 // 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
140 // 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
141 // 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
142 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
143
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
144 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
145 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
146 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
147 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
148
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
149 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
150 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
151 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
152 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
153
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
154 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
155 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
156 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
157 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
158
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
159 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
160 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
161 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
162 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
163
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
164 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
165 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
166 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
167 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
168
19119
7816aa44c145 patch 8.2.0119: message test fails on some platforms
Bram Moolenaar <Bram@vim.org>
parents: 19011
diff changeset
169 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
170 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
171 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
172 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
173
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
174 #ifdef FEAT_FLOAT
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
175 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
176 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
177 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
178 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
179
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
180 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
181 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
182 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
183 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
184
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
185 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
186 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
187 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
188 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
189
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
190 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
191 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
192 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
193 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
194
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
195 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
196 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
197 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
198 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
199
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
200 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
201 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
202 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
203 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
204 #endif
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
205
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
206 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
207 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
208 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
209 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
210
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
211 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
212 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
213 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
214 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
215
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
216 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
217 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
218 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
219 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
220
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
221 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
222 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
223 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
224 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
225
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
226 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
227 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
228 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
229 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
230
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
231 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
232 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
233 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
234 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
235
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
236 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
237 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
238 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
239 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
240
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
241 // %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
242 // 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
243 // 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
244 #ifdef SUN_SYSTEM
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
245 # define PREFIX_LEN 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
246 # define PREFIX_STR1 ""
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
247 # define PREFIX_STR2 "00"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
248 #else
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
249 # define PREFIX_LEN 2
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
250 # define PREFIX_STR1 "0x"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
251 # define PREFIX_STR2 "0x"
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
252 #endif
19011
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, "%p", ptr);
19352
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
254 assert(n == 8 + PREFIX_LEN);
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
255 assert(bsize == 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
256 || 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
257 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
258
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
259 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
260 assert(n == 12);
19352
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
261 assert(bsize == 0
c617ba6413bb patch 8.2.0234: message test fails on SunOS
Bram Moolenaar <Bram@vim.org>
parents: 19119
diff changeset
262 || 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
263 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
264
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
265 free(buf);
61d94accf16f patch 8.2.0066: some corners of vim_snprintf() are not tested
Bram Moolenaar <Bram@vim.org>
parents: 18999
diff changeset
266 }
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
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
269 int
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
270 main(int argc, char **argv)
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
271 {
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
272 vim_memset(&params, 0, sizeof(params));
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
273 params.argc = argc;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
274 params.argv = argv;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
275 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
276
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
277 set_option_value((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
278 init_chartab();
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
279 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
280 test_vim_snprintf();
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
281
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
282 set_option_value((char_u *)"encoding", 0, (char_u *)"latin1", 0);
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
283 init_chartab();
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
284 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
285 test_vim_snprintf();
18999
6fb11e7fb9cd patch 8.2.0060: message test only runs with one encoding
Bram Moolenaar <Bram@vim.org>
parents: 18931
diff changeset
286
9542
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
287 return 0;
00ee8d8c2e0c commit https://github.com/vim/vim/commit/502ae4ba63561c98ac69af26cd9883bfd18d225f
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
288 }