blob: 55ca814107ae602f2399ed4bb577946e21e1c68a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message_test.c: Unittests for message.c
12 */
13
14#undef NDEBUG
15#include <assert.h>
16
17/* Must include main.c because it contains much more than just main() */
18#define NO_VIM_MAIN
19#include "main.c"
20
21/* This file has to be included because some of the tested functions are
22 * static. */
23#include "message.c"
24
25/*
26 * Test trunc_string().
27 */
28 static void
29test_trunc_string(void)
30{
Bram Moolenaarb9644432016-07-19 12:33:44 +020031 char_u *buf; /*allocated every time to find uninit errors */
32 char_u *s;
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020033
34 /* in place */
Bram Moolenaarb9644432016-07-19 12:33:44 +020035 buf = alloc(40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020036 STRCPY(buf, "text");
37 trunc_string(buf, buf, 20, 40);
38 assert(STRCMP(buf, "text") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020039 vim_free(buf);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020040
Bram Moolenaarb9644432016-07-19 12:33:44 +020041 buf = alloc(40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020042 STRCPY(buf, "a short text");
43 trunc_string(buf, buf, 20, 40);
44 assert(STRCMP(buf, "a short text") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020045 vim_free(buf);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020046
Bram Moolenaarb9644432016-07-19 12:33:44 +020047 buf = alloc(40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020048 STRCPY(buf, "a text tha just fits");
49 trunc_string(buf, buf, 20, 40);
50 assert(STRCMP(buf, "a text tha just fits") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020051 vim_free(buf);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020052
Bram Moolenaarb9644432016-07-19 12:33:44 +020053 buf = alloc(40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020054 STRCPY(buf, "a text that nott fits");
55 trunc_string(buf, buf, 20, 40);
56 assert(STRCMP(buf, "a text t...nott fits") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020057 vim_free(buf);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020058
59 /* copy from string to buf */
Bram Moolenaarb9644432016-07-19 12:33:44 +020060 buf = alloc(40);
61 s = vim_strsave((char_u *)"text");
62 trunc_string(s, buf, 20, 40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020063 assert(STRCMP(buf, "text") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020064 vim_free(buf);
65 vim_free(s);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020066
Bram Moolenaarb9644432016-07-19 12:33:44 +020067 buf = alloc(40);
68 s = vim_strsave((char_u *)"a text that fits");
69 trunc_string(s, buf, 34, 40);
70 assert(STRCMP(buf, "a text that fits") == 0);
71 vim_free(buf);
72 vim_free(s);
73
74 buf = alloc(40);
75 s = vim_strsave((char_u *)"a short text");
76 trunc_string(s, buf, 20, 40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020077 assert(STRCMP(buf, "a short text") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020078 vim_free(buf);
79 vim_free(s);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020080
Bram Moolenaarb9644432016-07-19 12:33:44 +020081 buf = alloc(40);
82 s = vim_strsave((char_u *)"a text tha just fits");
83 trunc_string(s, buf, 20, 40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020084 assert(STRCMP(buf, "a text tha just fits") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020085 vim_free(buf);
86 vim_free(s);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020087
Bram Moolenaarb9644432016-07-19 12:33:44 +020088 buf = alloc(40);
89 s = vim_strsave((char_u *)"a text that nott fits");
90 trunc_string(s, buf, 20, 40);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020091 assert(STRCMP(buf, "a text t...nott fits") == 0);
Bram Moolenaarb9644432016-07-19 12:33:44 +020092 vim_free(buf);
93 vim_free(s);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020094}
95
96 int
97main(int argc, char **argv)
98{
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020099 vim_memset(&params, 0, sizeof(params));
100 params.argc = argc;
101 params.argv = argv;
102 common_init(&params);
103 init_chartab();
104
105 test_trunc_string();
106 return 0;
107}