Added DPMI segments structure and related function to avoid direct
[wine] / dlls / kernel / tests / format_msg.c
1 /* Unit test suite for FormatMessageA
2  *
3  * Copyright 2002 Mike McCormack for Codeweavers
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "wine/test.h"
21 #include "winbase.h"
22
23 /* #define ok(cond,failstr) if(!(cond)) {printf("line %d : %s\n",__LINE__,failstr);exit(1);} */
24
25 DWORD doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
26            LPSTR out, DWORD outsize, ... )
27 {
28     va_list list;
29     DWORD r;
30
31     va_start(list, outsize);
32     r = FormatMessageA(flags, src, msg_id,
33         lang_id, out, outsize, &list);
34     va_end(list);
35     return r;
36 }
37
38 void test_message_from_string(void)
39 {
40     CHAR out[0x100] = {0};
41     DWORD r;
42     WCHAR szwTest[] = { 't','e','s','t',0};
43
44     /* the basics */
45     r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0,
46         0, out, sizeof out/sizeof (CHAR),NULL);
47     ok(!strcmp("test", out),"failed");
48     ok(r==4,"failed");
49
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");
54     ok(r==4,"failed");
55
56     /* no format */
57     r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
58         0, out, sizeof out/sizeof (CHAR), "test");
59     ok(!strcmp("test", out),"failed");
60     ok(r==4,"failed");
61
62     /* two pieces */
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");
66     ok(r==4,"failed");
67
68     /* three pieces */
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");
72     ok(r==4,"failed");
73
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");
78     ok(r==3,"failed");
79
80     /* S is unicode */
81     r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0,
82         0, out, sizeof out/sizeof (CHAR), szwTest);
83     ok(!strcmp("test", out),"failed");
84     ok(r==4,"failed");
85
86     /* as characters */
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");
90     ok(r==4,"failed");
91
92     /* some numbers */
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");
96     ok(r==3,"failed");
97
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");
102     ok(r==4,"failed");
103
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");
108     ok(r==4,"failed");
109
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");
114     ok(r==4,"failed");
115
116     /* a hex number */
117     r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0,
118         0, out, sizeof out/sizeof (CHAR), 11);
119     ok(!strcmp("   b", out),"failed");
120     ok(r==4,"failed");
121
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");
126     ok(r==4,"failed");
127
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");
132     ok(r==4,"failed");
133
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");
138     ok(r==4,"failed");
139
140     /* two percent... */
141     r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0,
142         0, out, sizeof out/sizeof (CHAR));
143     ok(!strcmp(" %% ", out),"failed");
144     ok(r==4,"failed");
145
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");
150     ok(r==7,"failed");
151
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");
156     ok(r==4,"failed");
157
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");
162     ok(r==4,"failed");
163
164     /* %space */
165     r = doit(FORMAT_MESSAGE_FROM_STRING, "% %   ", 0,
166         0, out, sizeof out/sizeof (CHAR));
167     ok(!strcmp("    ", out),"failed");
168     ok(r==4,"failed");
169
170     /* line feed */
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");
174     ok(r==4,"failed");
175
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");
180     ok(r==4,"failed");
181
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");
186     ok(r==2,"failed");
187
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");
192     ok(r==4,"failed");
193
194     /* change of pace... test the low byte of dwflags */
195     /* line feed */
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),"failed");
199     ok(r==3,"failed");
200
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");
205     ok(r==3,"failed");
206
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");
211     ok(r==1,"failed");
212
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");
217     ok(r==2,"failed");
218 }
219
220 START_TEST(format_msg)
221 {
222     test_message_from_string();
223 }