1 /* Unit test suite for FormatMessageA
3 * Copyright 2002 Mike McCormack for Codeweavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "wine/test.h"
23 /* #define ok(cond,failstr) if(!(cond)) {printf("line %d : %s\n",__LINE__,failstr);exit(1);} */
25 DWORD doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
26 LPSTR out, DWORD outsize, ... )
31 va_start(list, outsize);
32 r = FormatMessageA(flags, src, msg_id,
33 lang_id, out, outsize, &list);
38 void test_message_from_string(void)
40 CHAR out[0x100] = {0};
42 WCHAR szwTest[] = { 't','e','s','t',0};
45 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0,
46 0, out, sizeof out/sizeof (CHAR),NULL);
47 ok(!strcmp("test", out),"failed out=[%s]",out);
48 ok(r==4,"failed: r=%ld",r);
50 /* using the format feature */
51 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0,
52 0, out, sizeof out/sizeof (CHAR), "test");
53 ok(!strcmp("test", out),"failed out=[%s]",out);
54 ok(r==4,"failed: r=%ld",r);
57 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
58 0, out, sizeof out/sizeof (CHAR), "test");
59 ok(!strcmp("test", out),"failed out=[%s]",out);
60 ok(r==4,"failed: r=%ld",r);
63 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%2", 0,
64 0, out, sizeof out/sizeof (CHAR), "te","st");
65 ok(!strcmp("test", out),"failed out=[%s]",out);
66 ok(r==4,"failed: r=%ld",r);
69 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%3%2%1", 0,
70 0, out, sizeof out/sizeof (CHAR), "t","s","e");
71 ok(!strcmp("test", out),"failed out=[%s]",out);
72 ok(r==4,"failed: r=%ld",r);
74 /* s doesn't seem to work in format strings */
75 r = doit(FORMAT_MESSAGE_FROM_STRING, "%!s!", 0,
76 0, out, sizeof out/sizeof (CHAR), "test");
77 ok(!strcmp("!s!", out),"failed out=[%s]",out);
78 ok(r==3,"failed: r=%ld",r);
81 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0,
82 0, out, sizeof out/sizeof (CHAR), szwTest);
83 ok(!strcmp("test", out),"failed out=[%s]",out);
84 ok(r==4,"failed: r=%ld",r);
87 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!c!%2!c!%3!c!%1!c!", 0,
88 0, out, sizeof out/sizeof (CHAR), 't','e','s');
89 ok(!strcmp("test", out),"failed out=[%s]",out);
90 ok(r==4,"failed: r=%ld",r);
93 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!d!%2!d!%3!d!", 0,
94 0, out, sizeof out/sizeof (CHAR), 1,2,3);
95 ok(!strcmp("123", out),"failed out=[%s]",out);
96 ok(r==3,"failed: r=%ld",r);
98 /* a single digit with some spacing */
99 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
100 0, out, sizeof out/sizeof (CHAR), 1);
101 ok(!strcmp(" 1", out),"failed out=[%s]",out);
102 ok(r==4,"failed: r=%ld",r);
104 /* a single digit, left justified */
105 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4d!", 0,
106 0, out, sizeof out/sizeof (CHAR), 1);
107 ok(!strcmp("1 ", out),"failed out=[%s]",out);
108 ok(r==4,"failed: r=%ld",r);
110 /* two digit decimal number */
111 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
112 0, out, sizeof out/sizeof (CHAR), 11);
113 ok(!strcmp(" 11", out),"failed out=[%s]",out);
114 ok(r==4,"failed: r=%ld",r);
117 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0,
118 0, out, sizeof out/sizeof (CHAR), 11);
119 ok(!strcmp(" b", out),"failed out=[%s]",out);
120 ok(r==4,"failed: r=%ld",r);
122 /* a hex number, upper case */
123 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
124 0, out, sizeof out/sizeof (CHAR), 11);
125 ok(!strcmp(" B", out),"failed out=[%s]",out);
126 ok(r==4,"failed: r=%ld",r);
128 /* a hex number, upper case, left justified */
129 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4X!", 0,
130 0, out, sizeof out/sizeof (CHAR), 11);
131 ok(!strcmp("B ", out),"failed out=[%s]",out);
132 ok(r==4,"failed: r=%ld",r);
134 /* a long hex number, upper case */
135 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
136 0, out, sizeof out/sizeof (CHAR), 0x1ab);
137 ok(!strcmp(" 1AB", out),"failed out=[%s]",out);
138 ok(r==4,"failed: r=%ld",r);
141 r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0,
142 0, out, sizeof out/sizeof (CHAR));
143 ok(!strcmp(" %% ", out),"failed out=[%s]",out);
144 ok(r==4,"failed: r=%ld",r);
146 /* periods are special cases */
147 r = doit(FORMAT_MESSAGE_FROM_STRING, " %.%. %1!d!", 0,
148 0, out, sizeof out/sizeof (CHAR), 0x1ab);
149 ok(!strcmp(" .. 427", out),"failed out=[%s]",out);
150 ok(r==7,"failed: r=%ld",r);
152 /* %0 ends the line */
153 r = doit(FORMAT_MESSAGE_FROM_STRING, "test%0test", 0,
154 0, out, sizeof out/sizeof (CHAR));
155 ok(!strcmp("test", out),"failed out=[%s]",out);
156 ok(r==4,"failed: r=%ld",r);
158 /* %! prints an exclaimation */
159 r = doit(FORMAT_MESSAGE_FROM_STRING, "yah%!%0 ", 0,
160 0, out, sizeof out/sizeof (CHAR));
161 ok(!strcmp("yah!", out),"failed out=[%s]",out);
162 ok(r==4,"failed: r=%ld",r);
165 r = doit(FORMAT_MESSAGE_FROM_STRING, "% % ", 0,
166 0, out, sizeof out/sizeof (CHAR));
167 ok(!strcmp(" ", out),"failed out=[%s]",out);
168 ok(r==4,"failed: r=%ld",r);
171 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\n", 0,
172 0, out, sizeof out/sizeof (CHAR));
173 ok(!strcmp("hi\r\n", out),"failed out=[%s]",out);
174 ok(r==4,"failed: r=%ld",r);
176 /* carriage return line feed */
177 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\r\n", 0,
178 0, out, sizeof out/sizeof (CHAR));
179 ok(!strcmp("hi\r\n", out),"failed out=[%s]",out);
180 ok(r==4,"failed: r=%ld",r);
182 /* carriage return line feed */
183 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r", 0,
184 0, out, sizeof out/sizeof (CHAR));
185 ok(!strcmp("\r\n", out),"failed out=[%s]",out);
186 ok(r==2,"failed: r=%ld",r);
188 /* carriage return line feed */
189 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r\r\n", 0,
190 0, out, sizeof out/sizeof (CHAR));
191 ok(!strcmp("\r\n\r\n", out),"failed out=[%s]",out);
192 ok(r==4,"failed: r=%ld",r);
194 /* change of pace... test the low byte of dwflags */
196 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0,
197 0, out, sizeof out/sizeof (CHAR));
198 ok(!strcmp("hi ", out) || !strcmp("hi\r\n", out),"failed out=[%s]",out);
199 ok(r==3 || r==4,"failed: r=%ld",r);
201 /* carriage return line feed */
202 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0,
203 0, out, sizeof out/sizeof (CHAR));
204 ok(!strcmp("hi ", out),"failed out=[%s]",out);
205 ok(r==3,"failed: r=%ld",r);
207 /* carriage return line feed */
208 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0,
209 0, out, sizeof out/sizeof (CHAR));
210 ok(!strcmp(" ", out),"failed out=[%s]",out);
211 ok(r==1,"failed: r=%ld",r);
213 /* carriage return line feed */
214 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0,
215 0, out, sizeof out/sizeof (CHAR));
216 ok(!strcmp(" ", out),"failed out=[%s]",out);
217 ok(r==2,"failed: r=%ld",r);
220 START_TEST(format_msg)
222 test_message_from_string();