1 /* Unit test suite for FormatMessageA/W
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/test.h"
27 /* #define ok(cond,failstr) if(!(cond)) {printf("line %d : %s\n",__LINE__,failstr);exit(1);} */
29 static DWORD
doit(DWORD flags
, LPCVOID src
, DWORD msg_id
, DWORD lang_id
,
30 LPSTR out
, DWORD outsize
, ... )
35 va_start(list
, outsize
);
36 r
= FormatMessageA(flags
, src
, msg_id
,
37 lang_id
, out
, outsize
, &list
);
42 static DWORD
doitW(DWORD flags
, LPCVOID src
, DWORD msg_id
, DWORD lang_id
,
43 LPWSTR out
, DWORD outsize
, ... )
48 va_start(list
, outsize
);
49 r
= FormatMessageW(flags
, src
, msg_id
,
50 lang_id
, out
, outsize
, &list
);
55 static char buf
[1024];
56 static const char *debugstr_w(const WCHAR
*str
)
58 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, sizeof(buf
), NULL
, NULL
);
62 static void test_message_from_string_wide(void)
64 static const WCHAR test
[] = {'t','e','s','t',0};
65 static const WCHAR te
[] = {'t','e',0};
66 static const WCHAR st
[] = {'s','t',0};
67 static const WCHAR t
[] = {'t',0};
68 static const WCHAR e
[] = {'e',0};
69 static const WCHAR s
[] = {'s',0};
70 static const WCHAR fmt_1
[] = {'%','1',0};
71 static const WCHAR fmt_12
[] = {'%','1','%','2',0};
72 static const WCHAR fmt_123
[] = {'%','1','%','3','%','2','%','1',0};
73 static const WCHAR fmt_123c
[] = {'%','1','!','c','!','%','2','!','c','!','%','3','!','c','!','%','1','!','c','!',0};
74 static const WCHAR fmt_123lc
[] = {'%','1','!','l','c','!','%','2','!','l','c','!','%','3','!','l','c','!','%','1','!','l','c','!',0};
75 static const WCHAR fmt_123wc
[] = {'%','1','!','w','c','!','%','2','!','w','c','!','%','3','!','w','c','!','%','1','!','w','c','!',0};
76 static const WCHAR fmt_123C
[] = {'%','1','!','C','!','%','2','!','C','!','%','3','!','C','!','%','1','!','C','!',0};
77 static const WCHAR fmt_123d
[] = {'%','1','!','d','!','%','2','!','d','!','%','3','!','d','!',0};
78 static const WCHAR fmt_1s
[] = {'%','1','!','s','!',0};
79 static const WCHAR fmt_s
[] = {'%','!','s','!',0};
80 static const WCHAR fmt_ls
[] = {'%','!','l','s','!',0};
81 static const WCHAR fmt_ws
[] = {'%','!','w','s','!',0};
82 static const WCHAR fmt_S
[] = {'%','!','S','!',0};
83 static const WCHAR fmt_14d
[] = {'%','1','!','4','d','!',0};
84 static const WCHAR fmt_14x
[] = {'%','1','!','4','x','!',0};
85 static const WCHAR fmt_14X
[] = {'%','1','!','4','X','!',0};
86 static const WCHAR fmt_1_4X
[] = {'%','1','!','-','4','X','!',0};
87 static const WCHAR fmt_1_4d
[] = {'%','1','!','-','4','d','!',0};
88 static const WCHAR fmt_2pct
[] = {' ','%','%','%','%',' ',0};
89 static const WCHAR fmt_2dot1d
[] = {' ', '%','.','%','.',' ',' ','%','1','!','d','!',0};
90 static const WCHAR fmt_t0t
[] = {'t','e','s','t','%','0','t','e','s','t',0};
91 static const WCHAR fmt_yah
[] = {'y','a','h','%','!','%','0',' ',' ',' ',0};
92 static const WCHAR fmt_space
[] = {'%',' ','%',' ',' ',' ',0};
93 static const WCHAR fmt_hi_lf
[] = {'h','i','\n',0};
94 static const WCHAR fmt_hi_crlf
[] = {'h','i','\r','\n',0};
95 static const WCHAR fmt_cr
[] = {'\r',0};
96 static const WCHAR fmt_crcrlf
[] = {'\r','\r','\n',0};
97 static const WCHAR s_123d
[] = {'1','2','3',0};
98 static const WCHAR s_14d
[] = {' ',' ',' ','1',0};
99 static const WCHAR s_14x
[] = {' ',' ',' ','b',0};
100 static const WCHAR s_14X
[] = {' ',' ',' ','B',0};
101 static const WCHAR s_1_4X
[] = {'B',' ',' ',' ',0};
102 static const WCHAR s_14d2
[] = {' ',' ','1','1',0};
103 static const WCHAR s_1_4d
[] = {'1',' ',' ',' ',0};
104 static const WCHAR s_1AB
[] = {' ','1','A','B',0};
105 static const WCHAR s_2pct
[] = {' ','%','%',' ',0};
106 static const WCHAR s_2dot147
[] = {' ','.','.',' ',' ','4','2','7',0};
107 static const WCHAR s_yah
[] = {'y','a','h','!',0};
108 static const WCHAR s_space
[] = {' ',' ',' ',' ',0};
109 static const WCHAR s_hi_crlf
[] = {'h','i','\r','\n',0};
110 static const WCHAR s_crlf
[] = {'\r','\n',0};
111 static const WCHAR s_crlfcrlf
[] = {'\r','\n','\r','\n',0};
112 static const WCHAR s_hi_sp
[] = {'h','i',' ',0};
113 static const WCHAR s_sp
[] = {' ',0};
114 static const WCHAR s_2sp
[] = {' ',' ',0};
115 WCHAR out
[0x100] = {0};
118 SetLastError(0xdeadbeef);
119 r
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, NULL
, 0, 0, NULL
, 0, NULL
);
120 error
= GetLastError();
121 if (!r
&& error
== ERROR_CALL_NOT_IMPLEMENTED
)
123 win_skip("FormatMessageW is not implemented\n");
128 r
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, test
, 0,
129 0, out
, sizeof(out
)/sizeof(WCHAR
), NULL
);
130 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
131 ok(r
==4, "failed: r=%d\n", r
);
133 /* using the format feature */
134 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_1s
, 0,
135 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
136 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
137 ok(r
==4,"failed: r=%d\n", r
);
140 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_1
, 0,
141 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
142 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
143 ok(r
==4,"failed: r=%d\n", r
);
146 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_12
, 0,
147 0, out
, sizeof(out
)/sizeof(WCHAR
), te
, st
);
148 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
149 ok(r
==4,"failed: r=%d\n", r
);
152 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123
, 0,
153 0, out
, sizeof(out
)/sizeof(WCHAR
), t
, s
, e
);
154 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
155 ok(r
==4,"failed: r=%d\n", r
);
157 /* s doesn't seem to work in format strings */
158 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_s
, 0,
159 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
160 ok(!lstrcmpW(&fmt_s
[1], out
), "failed out=[%s]\n", debugstr_w(out
));
161 ok(r
==3, "failed: r=%d\n", r
);
164 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_ls
, 0,
165 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
166 ok(!lstrcmpW(&fmt_ls
[1], out
), "failed out=[%s]\n", debugstr_w(out
));
167 ok(r
==4, "failed: r=%d\n", r
);
170 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_S
, 0,
171 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
172 ok(!lstrcmpW(&fmt_S
[1], out
), "failed out=[%s]\n", debugstr_w(out
));
173 ok(r
==3, "failed: r=%d\n", r
);
176 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_ws
, 0,
177 0, out
, sizeof(out
)/sizeof(WCHAR
), test
);
178 ok(!lstrcmpW(&fmt_ws
[1], out
), "failed out=[%s]\n", debugstr_w(out
));
179 ok(r
==4, "failed: r=%d\n", r
);
182 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123c
, 0,
183 0, out
, sizeof(out
)/sizeof(WCHAR
), 't', 'e', 's');
184 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
185 ok(r
==4,"failed: r=%d\n", r
);
188 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123lc
, 0,
189 0, out
, sizeof(out
)/sizeof(WCHAR
), 't', 'e', 's');
190 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
191 ok(r
==4,"failed: r=%d\n", r
);
194 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123wc
, 0,
195 0, out
, sizeof(out
)/sizeof(WCHAR
), 't', 'e', 's');
196 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
197 ok(r
==4,"failed: r=%d\n", r
);
200 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123C
, 0,
201 0, out
, sizeof(out
)/sizeof(WCHAR
), 't', 'e', 's');
202 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
203 ok(r
==4,"failed: r=%d\n", r
);
206 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_123d
, 0,
207 0, out
, sizeof(out
)/sizeof(WCHAR
), 1, 2, 3);
208 ok(!lstrcmpW(s_123d
, out
), "failed out=[%s]\n", debugstr_w(out
));
209 ok(r
==3,"failed: r=%d\n", r
);
211 /* a single digit with some spacing */
212 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_14d
, 0,
213 0, out
, sizeof(out
)/sizeof(WCHAR
), 1);
214 ok(!lstrcmpW(s_14d
, out
), "failed out=[%s]\n", debugstr_w(out
));
216 /* a single digit, left justified */
217 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_1_4d
, 0,
218 0, out
, sizeof(out
)/sizeof(CHAR
), 1);
219 ok(!lstrcmpW(s_1_4d
, out
), "failed out=[%s]\n", debugstr_w(out
));
220 ok(r
==4,"failed: r=%d\n", r
);
222 /* two digit decimal number */
223 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_14d
, 0,
224 0, out
, sizeof(out
)/sizeof(WCHAR
), 11);
225 ok(!lstrcmpW(s_14d2
, out
), "failed out=[%s]\n", debugstr_w(out
));
226 ok(r
==4,"failed: r=%d\n", r
);
229 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_14x
, 0,
230 0, out
, sizeof(out
)/sizeof(WCHAR
), 11);
231 ok(!lstrcmpW(s_14x
, out
), "failed out=[%s]\n", debugstr_w(out
));
232 ok(r
==4,"failed: r=%d\n", r
);
234 /* a hex number, upper case */
235 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_14X
, 0,
236 0, out
, sizeof(out
)/sizeof(WCHAR
), 11);
237 ok(!lstrcmpW(s_14X
, out
), "failed out=[%s]\n", debugstr_w(out
));
238 ok(r
==4,"failed: r=%d\n", r
);
240 /* a hex number, upper case, left justified */
241 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_1_4X
, 0,
242 0, out
, sizeof(out
)/sizeof(WCHAR
), 11);
243 ok(!lstrcmpW(s_1_4X
, out
), "failed out=[%s]\n", debugstr_w(out
));
244 ok(r
==4,"failed: r=%d\n", r
);
246 /* a long hex number, upper case */
247 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_14X
, 0,
248 0, out
, sizeof(out
)/sizeof(WCHAR
), 0x1ab);
249 ok(!lstrcmpW(s_1AB
, out
), "failed out=[%s]\n", debugstr_w(out
));
250 ok(r
==4,"failed: r=%d\n", r
);
253 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_2pct
, 0,
254 0, out
, sizeof(out
)/sizeof(WCHAR
));
255 ok(!lstrcmpW(s_2pct
, out
), "failed out=[%s]\n", debugstr_w(out
));
256 ok(r
==4,"failed: r=%d\n", r
);
258 /* periods are special cases */
259 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_2dot1d
, 0,
260 0, out
, sizeof(out
)/sizeof(WCHAR
), 0x1ab);
261 ok(!lstrcmpW(s_2dot147
, out
), "failed out=[%s]\n", debugstr_w(out
));
262 ok(r
==8,"failed: r=%d\n", r
);
264 /* %0 ends the line */
265 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_t0t
, 0,
266 0, out
, sizeof(out
)/sizeof(WCHAR
));
267 ok(!lstrcmpW(test
, out
), "failed out=[%s]\n", debugstr_w(out
));
268 ok(r
==4,"failed: r=%d\n", r
);
270 /* %! prints an exclamation */
271 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_yah
, 0,
272 0, out
, sizeof(out
)/sizeof(WCHAR
));
273 ok(!lstrcmpW(s_yah
, out
), "failed out=[%s]\n", debugstr_w(out
));
274 ok(r
==4,"failed: r=%d\n", r
);
277 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_space
, 0,
278 0, out
, sizeof(out
)/sizeof(WCHAR
));
279 ok(!lstrcmpW(s_space
, out
), "failed out=[%s]\n", debugstr_w(out
));
280 ok(r
==4,"failed: r=%d\n", r
);
283 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_hi_lf
, 0,
284 0, out
, sizeof(out
)/sizeof(WCHAR
));
285 ok(!lstrcmpW(s_hi_crlf
, out
), "failed out=[%s]\n", debugstr_w(out
));
286 ok(r
==4,"failed: r=%d\n", r
);
288 /* carriage return line feed */
289 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_hi_crlf
, 0,
290 0, out
, sizeof(out
)/sizeof(WCHAR
));
291 ok(!lstrcmpW(s_hi_crlf
, out
), "failed out=[%s]\n", debugstr_w(out
));
292 ok(r
==4,"failed: r=%d\n", r
);
294 /* carriage return */
295 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_cr
, 0,
296 0, out
, sizeof(out
)/sizeof(WCHAR
));
297 ok(!lstrcmpW(s_crlf
, out
), "failed out=[%s]\n", debugstr_w(out
));
298 ok(r
==2,"failed: r=%d\n", r
);
300 /* double carriage return line feed */
301 r
= doitW(FORMAT_MESSAGE_FROM_STRING
, fmt_crcrlf
, 0,
302 0, out
, sizeof(out
)/sizeof(WCHAR
));
303 ok(!lstrcmpW(s_crlfcrlf
, out
), "failed out=[%s]\n", debugstr_w(out
));
304 ok(r
==4,"failed: r=%d\n", r
);
306 /* change of pace... test the low byte of dwflags */
309 r
= doitW(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, fmt_hi_lf
, 0,
310 0, out
, sizeof(out
)/sizeof(WCHAR
));
311 ok(!lstrcmpW(s_hi_sp
, out
) || !lstrcmpW(s_hi_crlf
, out
), "failed out=[%s]\n", debugstr_w(out
));
312 ok(r
==3 || r
==4,"failed: r=%d\n", r
);
314 /* carriage return line feed */
315 r
= doitW(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, fmt_hi_crlf
, 0,
316 0, out
, sizeof(out
)/sizeof(WCHAR
));
317 ok(!lstrcmpW(s_hi_sp
, out
), "failed out=[%s]\n", debugstr_w(out
));
318 ok(r
==3,"failed: r=%d\n", r
);
320 /* carriage return */
321 r
= doitW(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, fmt_cr
, 0,
322 0, out
, sizeof(out
)/sizeof(WCHAR
));
323 ok(!lstrcmpW(s_sp
, out
), "failed out=[%s]\n", debugstr_w(out
));
324 ok(r
==1,"failed: r=%d\n", r
);
326 /* double carriage return line feed */
327 r
= doitW(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, fmt_crcrlf
, 0,
328 0, out
, sizeof(out
)/sizeof(WCHAR
));
329 ok(!lstrcmpW(s_2sp
, out
), "failed out=[%s]\n", debugstr_w(out
));
330 ok(r
==2,"failed: r=%d\n", r
);
333 static void test_message_from_string(void)
335 CHAR out
[0x100] = {0};
337 static const WCHAR szwTest
[] = { 't','e','s','t',0};
340 r
= FormatMessageA(FORMAT_MESSAGE_FROM_STRING
, "test", 0,
341 0, out
, sizeof(out
)/sizeof(CHAR
),NULL
);
342 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
343 ok(r
==4,"failed: r=%d\n",r
);
345 /* using the format feature */
346 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!s!", 0,
347 0, out
, sizeof(out
)/sizeof(CHAR
), "test");
348 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
349 ok(r
==4,"failed: r=%d\n",r
);
352 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1", 0,
353 0, out
, sizeof(out
)/sizeof(CHAR
), "test");
354 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
355 ok(r
==4,"failed: r=%d\n",r
);
358 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1%2", 0,
359 0, out
, sizeof(out
)/sizeof(CHAR
), "te","st");
360 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
361 ok(r
==4,"failed: r=%d\n",r
);
364 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1%3%2%1", 0,
365 0, out
, sizeof(out
)/sizeof(CHAR
), "t","s","e");
366 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
367 ok(r
==4,"failed: r=%d\n",r
);
369 /* s doesn't seem to work in format strings */
370 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%!s!", 0,
371 0, out
, sizeof(out
)/sizeof(CHAR
), "test");
372 ok(!strcmp("!s!", out
),"failed out=[%s]\n",out
);
373 ok(r
==3,"failed: r=%d\n",r
);
376 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!ls!", 0,
377 0, out
, sizeof(out
)/sizeof(CHAR
), szwTest
);
378 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
379 ok(r
==4,"failed: r=%d\n",r
);
382 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!S!", 0,
383 0, out
, sizeof(out
)/sizeof(CHAR
), szwTest
);
384 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
385 ok(r
==4,"failed: r=%d\n",r
);
388 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!ws!", 0,
389 0, out
, sizeof(out
)/sizeof(CHAR
), szwTest
);
390 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
391 ok(r
==4,"failed: r=%d\n",r
);
394 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!c!%2!c!%3!c!%1!c!", 0,
395 0, out
, sizeof(out
)/sizeof(CHAR
), 't','e','s');
396 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
397 ok(r
==4,"failed: r=%d\n",r
);
400 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!lc!%2!lc!%3!lc!%1!lc!", 0,
401 0, out
, sizeof(out
)/sizeof(CHAR
), 't','e','s');
402 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
403 ok(r
==4,"failed: r=%d\n",r
);
406 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!wc!%2!wc!%3!wc!%1!wc!", 0,
407 0, out
, sizeof(out
)/sizeof(CHAR
), 't','e','s');
408 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
409 ok(r
==4,"failed: r=%d\n",r
);
412 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!C!%2!C!%3!C!%1!C!", 0,
413 0, out
, sizeof(out
)/sizeof(CHAR
), 't','e','s');
414 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
415 ok(r
==4,"failed: r=%d\n",r
);
418 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!d!%2!d!%3!d!", 0,
419 0, out
, sizeof(out
)/sizeof(CHAR
), 1,2,3);
420 ok(!strcmp("123", out
),"failed out=[%s]\n",out
);
421 ok(r
==3,"failed: r=%d\n",r
);
423 /* a single digit with some spacing */
424 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!4d!", 0,
425 0, out
, sizeof(out
)/sizeof(CHAR
), 1);
426 ok(!strcmp(" 1", out
),"failed out=[%s]\n",out
);
427 ok(r
==4,"failed: r=%d\n",r
);
429 /* a single digit, left justified */
430 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!-4d!", 0,
431 0, out
, sizeof(out
)/sizeof(CHAR
), 1);
432 ok(!strcmp("1 ", out
),"failed out=[%s]\n",out
);
433 ok(r
==4,"failed: r=%d\n",r
);
435 /* two digit decimal number */
436 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!4d!", 0,
437 0, out
, sizeof(out
)/sizeof(CHAR
), 11);
438 ok(!strcmp(" 11", out
),"failed out=[%s]\n",out
);
439 ok(r
==4,"failed: r=%d\n",r
);
442 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!4x!", 0,
443 0, out
, sizeof(out
)/sizeof(CHAR
), 11);
444 ok(!strcmp(" b", out
),"failed out=[%s]\n",out
);
445 ok(r
==4,"failed: r=%d\n",r
);
447 /* a hex number, upper case */
448 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!4X!", 0,
449 0, out
, sizeof(out
)/sizeof(CHAR
), 11);
450 ok(!strcmp(" B", out
),"failed out=[%s]\n",out
);
451 ok(r
==4,"failed: r=%d\n",r
);
453 /* a hex number, upper case, left justified */
454 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!-4X!", 0,
455 0, out
, sizeof(out
)/sizeof(CHAR
), 11);
456 ok(!strcmp("B ", out
),"failed out=[%s]\n",out
);
457 ok(r
==4,"failed: r=%d\n",r
);
459 /* a long hex number, upper case */
460 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "%1!4X!", 0,
461 0, out
, sizeof(out
)/sizeof(CHAR
), 0x1ab);
462 ok(!strcmp(" 1AB", out
),"failed out=[%s]\n",out
);
463 ok(r
==4,"failed: r=%d\n",r
);
466 r
= doit(FORMAT_MESSAGE_FROM_STRING
, " %%%% ", 0,
467 0, out
, sizeof(out
)/sizeof(CHAR
));
468 ok(!strcmp(" %% ", out
),"failed out=[%s]\n",out
);
469 ok(r
==4,"failed: r=%d\n",r
);
471 /* periods are special cases */
472 r
= doit(FORMAT_MESSAGE_FROM_STRING
, " %.%. %1!d!", 0,
473 0, out
, sizeof(out
)/sizeof(CHAR
), 0x1ab);
474 ok(!strcmp(" .. 427", out
),"failed out=[%s]\n",out
);
475 ok(r
==7,"failed: r=%d\n",r
);
477 /* %0 ends the line */
478 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "test%0test", 0,
479 0, out
, sizeof(out
)/sizeof(CHAR
));
480 ok(!strcmp("test", out
),"failed out=[%s]\n",out
);
481 ok(r
==4,"failed: r=%d\n",r
);
483 /* %! prints an exclamation */
484 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "yah%!%0 ", 0,
485 0, out
, sizeof(out
)/sizeof(CHAR
));
486 ok(!strcmp("yah!", out
),"failed out=[%s]\n",out
);
487 ok(r
==4,"failed: r=%d\n",r
);
490 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "% % ", 0,
491 0, out
, sizeof(out
)/sizeof(CHAR
));
492 ok(!strcmp(" ", out
),"failed out=[%s]\n",out
);
493 ok(r
==4,"failed: r=%d\n",r
);
496 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "hi\n", 0,
497 0, out
, sizeof(out
)/sizeof(CHAR
));
498 ok(!strcmp("hi\r\n", out
),"failed out=[%s]\n",out
);
499 ok(r
==4,"failed: r=%d\n",r
);
501 /* carriage return line feed */
502 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "hi\r\n", 0,
503 0, out
, sizeof(out
)/sizeof(CHAR
));
504 ok(!strcmp("hi\r\n", out
),"failed out=[%s]\n",out
);
505 ok(r
==4,"failed: r=%d\n",r
);
507 /* carriage return */
508 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "\r", 0,
509 0, out
, sizeof(out
)/sizeof(CHAR
));
510 ok(!strcmp("\r\n", out
),"failed out=[%s]\n",out
);
511 ok(r
==2,"failed: r=%d\n",r
);
513 /* double carriage return line feed */
514 r
= doit(FORMAT_MESSAGE_FROM_STRING
, "\r\r\n", 0,
515 0, out
, sizeof(out
)/sizeof(CHAR
));
516 ok(!strcmp("\r\n\r\n", out
),"failed out=[%s]\n",out
);
517 ok(r
==4,"failed: r=%d\n",r
);
519 /* change of pace... test the low byte of dwflags */
522 r
= doit(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, "hi\n", 0,
523 0, out
, sizeof(out
)/sizeof(CHAR
));
524 ok(!strcmp("hi ", out
) || !strcmp("hi\r\n", out
),"failed out=[%s]\n",out
);
525 ok(r
==3 || r
==4,"failed: r=%d\n",r
);
527 /* carriage return line feed */
528 r
= doit(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, "hi\r\n", 0,
529 0, out
, sizeof(out
)/sizeof(CHAR
));
530 ok(!strcmp("hi ", out
),"failed out=[%s]\n",out
);
531 ok(r
==3,"failed: r=%d\n",r
);
533 /* carriage return */
534 r
= doit(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, "\r", 0,
535 0, out
, sizeof(out
)/sizeof(CHAR
));
536 ok(!strcmp(" ", out
),"failed out=[%s]\n",out
);
537 ok(r
==1,"failed: r=%d\n",r
);
539 /* double carriage return line feed */
540 r
= doit(FORMAT_MESSAGE_FROM_STRING
| FORMAT_MESSAGE_MAX_WIDTH_MASK
, "\r\r\n", 0,
541 0, out
, sizeof(out
)/sizeof(CHAR
));
542 ok(!strcmp(" ", out
),"failed out=[%s]\n",out
);
543 ok(r
==2,"failed: r=%d\n",r
);
546 static void test_message_null_buffer(void)
550 SetLastError(0xdeadbeef);
551 ret
= FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL
, 0, 0, NULL
, 0, NULL
);
552 error
= GetLastError();
553 ok(!ret
, "FormatMessageA returned %u\n", ret
);
554 ok(error
== ERROR_NOT_ENOUGH_MEMORY
||
555 error
== ERROR_INVALID_PARAMETER
, /* win9x */
556 "last error %u\n", error
);
558 SetLastError(0xdeadbeef);
559 ret
= FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL
, 0, 0, NULL
, 0, NULL
);
560 error
= GetLastError();
561 if (!ret
&& error
== ERROR_CALL_NOT_IMPLEMENTED
)
563 win_skip("FormatMessageW is not implemented\n");
567 ok(!ret
, "FormatMessageW returned %u\n", ret
);
568 ok(error
== ERROR_INVALID_PARAMETER
, "last error %u\n", error
);
571 START_TEST(format_msg
)
573 test_message_from_string();
574 test_message_from_string_wide();
575 test_message_null_buffer();