format_msg.c 7.96 KB
Newer Older
1 2
/* Unit test suite for FormatMessageA
 *
3
 * Copyright 2002 Mike McCormack for CodeWeavers
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
18 19
 */

20 21
#include <stdarg.h>

22
#include "wine/test.h"
23
#include "windef.h"
24 25 26 27
#include "winbase.h"

/* #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 30 31 32 33 34 35 36 37 38 39 40
           LPSTR out, DWORD outsize, ... )
{
    va_list list;
    DWORD r;

    va_start(list, outsize);
    r = FormatMessageA(flags, src, msg_id,
        lang_id, out, outsize, &list);
    va_end(list);
    return r;
}

41
static void test_message_from_string(void)
42 43 44
{
    CHAR out[0x100] = {0};
    DWORD r;
45
    static const WCHAR szwTest[] = { 't','e','s','t',0};
46 47 48

    /* the basics */
    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);
52 53 54

    /* using the format feature */
    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);
58 59 60

    /* no format */
    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);
64 65 66

    /* two pieces */
    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);
70 71 72

    /* three pieces */
    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);
76 77 78

    /* s doesn't seem to work in format strings */
    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);
82 83 84

    /* S is unicode */
    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);
88 89 90

    /* as characters */
    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);
94 95 96

    /* some numbers */
    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);
100 101 102

    /* a single digit with some spacing */
    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);
106 107 108

    /* a single digit, left justified */
    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);
112 113 114

    /* two digit decimal number */
    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);
118 119 120

    /* a hex number */
    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);
124 125 126

    /* a hex number, upper case */
    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);
130 131 132

    /* a hex number, upper case, left justified */
    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);
136 137 138

    /* a long hex number, upper case */
    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);
142 143 144

    /* two percent... */
    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);
148 149 150

    /* periods are special cases */
    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);
154 155 156

    /* %0 ends the line */
    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);
160 161 162

    /* %! prints an exclaimation */
    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);
166 167 168

    /* %space */
    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);
172 173 174

    /* line feed */
    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);
178 179 180

    /* carriage return line feed */
    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);
184 185 186

    /* carriage return line feed */
    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);
190 191 192

    /* carriage return line feed */
    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);
196 197 198 199

    /* change of pace... test the low byte of dwflags */
    /* line feed */
    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);
203 204 205

    /* carriage return line feed */
    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);
209 210 211

    /* carriage return line feed */
    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);
215 216 217

    /* carriage return line feed */
    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);
221 222 223 224 225 226
}

START_TEST(format_msg)
{
    test_message_from_string();
}