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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/test.h"
26 /* #define ok(cond,failstr) if(!(cond)) {printf("line %d : %s\n",__LINE__,failstr);exit(1);} */
28 static DWORD doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
29 LPSTR out, DWORD outsize, ... )
34 va_start(list, outsize);
35 r = FormatMessageA(flags, src, msg_id,
36 lang_id, out, outsize, &list);
41 static void test_message_from_string(void)
43 CHAR out[0x100] = {0};
45 static const WCHAR szwTest[] = { 't','e','s','t',0};
48 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0,
49 0, out, sizeof(out)/sizeof(CHAR),NULL);
50 ok(!strcmp("test", out),"failed out=[%s]\n",out);
51 ok(r==4,"failed: r=%d\n",r);
53 /* using the format feature */
54 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0,
55 0, out, sizeof(out)/sizeof(CHAR), "test");
56 ok(!strcmp("test", out),"failed out=[%s]\n",out);
57 ok(r==4,"failed: r=%d\n",r);
60 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
61 0, out, sizeof(out)/sizeof(CHAR), "test");
62 ok(!strcmp("test", out),"failed out=[%s]\n",out);
63 ok(r==4,"failed: r=%d\n",r);
66 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%2", 0,
67 0, out, sizeof(out)/sizeof(CHAR), "te","st");
68 ok(!strcmp("test", out),"failed out=[%s]\n",out);
69 ok(r==4,"failed: r=%d\n",r);
72 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%3%2%1", 0,
73 0, out, sizeof(out)/sizeof(CHAR), "t","s","e");
74 ok(!strcmp("test", out),"failed out=[%s]\n",out);
75 ok(r==4,"failed: r=%d\n",r);
77 /* s doesn't seem to work in format strings */
78 r = doit(FORMAT_MESSAGE_FROM_STRING, "%!s!", 0,
79 0, out, sizeof(out)/sizeof(CHAR), "test");
80 ok(!strcmp("!s!", out),"failed out=[%s]\n",out);
81 ok(r==3,"failed: r=%d\n",r);
84 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0,
85 0, out, sizeof(out)/sizeof(CHAR), szwTest);
86 ok(!strcmp("test", out),"failed out=[%s]\n",out);
87 ok(r==4,"failed: r=%d\n",r);
90 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!c!%2!c!%3!c!%1!c!", 0,
91 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
92 ok(!strcmp("test", out),"failed out=[%s]\n",out);
93 ok(r==4,"failed: r=%d\n",r);
96 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!d!%2!d!%3!d!", 0,
97 0, out, sizeof(out)/sizeof(CHAR), 1,2,3);
98 ok(!strcmp("123", out),"failed out=[%s]\n",out);
99 ok(r==3,"failed: r=%d\n",r);
101 /* a single digit with some spacing */
102 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
103 0, out, sizeof(out)/sizeof(CHAR), 1);
104 ok(!strcmp(" 1", out),"failed out=[%s]\n",out);
105 ok(r==4,"failed: r=%d\n",r);
107 /* a single digit, left justified */
108 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4d!", 0,
109 0, out, sizeof(out)/sizeof(CHAR), 1);
110 ok(!strcmp("1 ", out),"failed out=[%s]\n",out);
111 ok(r==4,"failed: r=%d\n",r);
113 /* two digit decimal number */
114 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
115 0, out, sizeof(out)/sizeof(CHAR), 11);
116 ok(!strcmp(" 11", out),"failed out=[%s]\n",out);
117 ok(r==4,"failed: r=%d\n",r);
120 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0,
121 0, out, sizeof(out)/sizeof(CHAR), 11);
122 ok(!strcmp(" b", out),"failed out=[%s]\n",out);
123 ok(r==4,"failed: r=%d\n",r);
125 /* a hex number, upper case */
126 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
127 0, out, sizeof(out)/sizeof(CHAR), 11);
128 ok(!strcmp(" B", out),"failed out=[%s]\n",out);
129 ok(r==4,"failed: r=%d\n",r);
131 /* a hex number, upper case, left justified */
132 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4X!", 0,
133 0, out, sizeof(out)/sizeof(CHAR), 11);
134 ok(!strcmp("B ", out),"failed out=[%s]\n",out);
135 ok(r==4,"failed: r=%d\n",r);
137 /* a long hex number, upper case */
138 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
139 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
140 ok(!strcmp(" 1AB", out),"failed out=[%s]\n",out);
141 ok(r==4,"failed: r=%d\n",r);
144 r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0,
145 0, out, sizeof(out)/sizeof(CHAR));
146 ok(!strcmp(" %% ", out),"failed out=[%s]\n",out);
147 ok(r==4,"failed: r=%d\n",r);
149 /* periods are special cases */
150 r = doit(FORMAT_MESSAGE_FROM_STRING, " %.%. %1!d!", 0,
151 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
152 ok(!strcmp(" .. 427", out),"failed out=[%s]\n",out);
153 ok(r==7,"failed: r=%d\n",r);
155 /* %0 ends the line */
156 r = doit(FORMAT_MESSAGE_FROM_STRING, "test%0test", 0,
157 0, out, sizeof(out)/sizeof(CHAR));
158 ok(!strcmp("test", out),"failed out=[%s]\n",out);
159 ok(r==4,"failed: r=%d\n",r);
161 /* %! prints an exclamation */
162 r = doit(FORMAT_MESSAGE_FROM_STRING, "yah%!%0 ", 0,
163 0, out, sizeof(out)/sizeof(CHAR));
164 ok(!strcmp("yah!", out),"failed out=[%s]\n",out);
165 ok(r==4,"failed: r=%d\n",r);
168 r = doit(FORMAT_MESSAGE_FROM_STRING, "% % ", 0,
169 0, out, sizeof(out)/sizeof(CHAR));
170 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
171 ok(r==4,"failed: r=%d\n",r);
174 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\n", 0,
175 0, out, sizeof(out)/sizeof(CHAR));
176 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
177 ok(r==4,"failed: r=%d\n",r);
179 /* carriage return line feed */
180 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\r\n", 0,
181 0, out, sizeof(out)/sizeof(CHAR));
182 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
183 ok(r==4,"failed: r=%d\n",r);
185 /* carriage return line feed */
186 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r", 0,
187 0, out, sizeof(out)/sizeof(CHAR));
188 ok(!strcmp("\r\n", out),"failed out=[%s]\n",out);
189 ok(r==2,"failed: r=%d\n",r);
191 /* carriage return line feed */
192 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r\r\n", 0,
193 0, out, sizeof(out)/sizeof(CHAR));
194 ok(!strcmp("\r\n\r\n", out),"failed out=[%s]\n",out);
195 ok(r==4,"failed: r=%d\n",r);
197 /* change of pace... test the low byte of dwflags */
199 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0,
200 0, out, sizeof(out)/sizeof(CHAR));
201 ok(!strcmp("hi ", out) || !strcmp("hi\r\n", out),"failed out=[%s]\n",out);
202 ok(r==3 || r==4,"failed: r=%d\n",r);
204 /* carriage return line feed */
205 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0,
206 0, out, sizeof(out)/sizeof(CHAR));
207 ok(!strcmp("hi ", out),"failed out=[%s]\n",out);
208 ok(r==3,"failed: r=%d\n",r);
210 /* carriage return line feed */
211 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0,
212 0, out, sizeof(out)/sizeof(CHAR));
213 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
214 ok(r==1,"failed: r=%d\n",r);
216 /* carriage return line feed */
217 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0,
218 0, out, sizeof(out)/sizeof(CHAR));
219 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
220 ok(r==2,"failed: r=%d\n",r);
223 static void test_message_null_buffer(void)
227 SetLastError(0xdeadbeef);
228 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
229 error = GetLastError();
230 ok(!ret, "FormatMessageA returned %u\n", ret);
231 ok(error == ERROR_NOT_ENOUGH_MEMORY ||
232 error == ERROR_INVALID_PARAMETER, /* win9x */
233 "last error %u\n", error);
235 SetLastError(0xdeadbeef);
236 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
237 error = GetLastError();
238 if (!ret && error == ERROR_CALL_NOT_IMPLEMENTED)
240 skip("FormatMessageW is not implemented\n");
244 ok(!ret, "FormatMessageW returned %u\n", ret);
245 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
248 START_TEST(format_msg)
250 test_message_from_string();
251 test_message_null_buffer();