1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================
6**
7** Source : test.c
8**
9** Purpose: Test for FormatMessageW() function
10**
11**
12**=========================================================*/
13
14#define UNICODE
15#include <palsuite.h>
16
17WCHAR OutBuffer[1024];
18
19/* Pass this test the string "INSERT" and it will succeed */
20
21int test1(int num, ...)
22{
23
24 WCHAR * TheString = convert("Pal %1!s! Testing");
25 int ReturnResult;
26 va_list TheList;
27 va_start(TheList,num);
28 memset( OutBuffer, 0, 1024 );
29
30 ReturnResult = FormatMessage(
31 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
32 TheString, /* message source */
33 0, /* message identifier */
34 0, /* language identifier */
35 OutBuffer, /* message buffer */
36 1024, /* maximum size of message buffer */
37 &TheList /* array of message inserts */
38 );
39
40 va_end(TheList);
41
42 if(ReturnResult == 0)
43 {
44 Fail("ERROR: The return value was 0, which indicates failure. "
45 "The function failed when trying to Format a simple string,"
46 " with the 's' formatter.");
47
48 }
49
50 if(memcmp(OutBuffer, convert("Pal INSERT Testing"),
51 wcslen(OutBuffer)*2+2) != 0)
52 {
53 Fail("ERROR: The formated string should have been 'Pal INSERT "
54 "Testing' but '%s' was returned.",
55 convertC(OutBuffer));
56 }
57
58
59 return PASS;
60}
61
62/* Pass this test the int 40 and it will succeed */
63
64int test2(int num, ...)
65{
66
67 WCHAR * TheString = convert("Pal %1!i! Testing");
68 int ReturnResult;
69 va_list TheList;
70 va_start(TheList,num);
71
72 memset( OutBuffer, 0, 1024 );
73
74 ReturnResult = FormatMessage(
75 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
76 TheString, /* message source */
77 0, /* message identifier */
78 0, /* language identifier */
79 OutBuffer, /* message buffer */
80 1024, /* maximum size of message buffer */
81 &TheList /* array of message inserts */
82 );
83
84 va_end(TheList);
85
86 if(ReturnResult == 0)
87 {
88 Fail("ERROR: The return value was 0, which indicates failure. "
89 "The function failed when trying to Format a simple string,"
90 " with the 'i' formatter.");
91 }
92
93 if(memcmp(OutBuffer, convert("Pal 40 Testing"),wcslen(OutBuffer)*2+2) != 0)
94 {
95 Fail("ERROR: The formated string should have been 'Pal 40 Testing' "
96 "but '%s' was returned.", convertC(OutBuffer));
97 }
98 return PASS;
99}
100
101/* Pass this test the character 'a' and it will succeed */
102
103int test3(int num, ...) {
104
105 WCHAR * TheString = convert("Pal %1!c! Testing");
106 int ReturnResult;
107 va_list TheList;
108 va_start(TheList,num);
109 memset( OutBuffer, 0, 1024 );
110 ReturnResult = FormatMessage(
111 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
112 TheString, /* message source */
113 0, /* message identifier */
114 0, /* language identifier */
115 OutBuffer, /* message buffer */
116 1024, /* maximum size of message buffer */
117 &TheList /* array of message inserts */
118 );
119
120 va_end(TheList);
121
122 if(ReturnResult == 0)
123 {
124 Fail("ERROR: The return value was 0, which indicates failure. "
125 "The function failed when trying to Format a simple string,"
126 " with the 'c' formatter.");
127 }
128
129 if(memcmp(OutBuffer, convert("Pal a Testing"),wcslen(OutBuffer)*2+2) != 0)
130 {
131 Fail("ERROR: The formated string should have been 'Pal a Testing' "
132 "but '%s' was returned.", convertC(OutBuffer));
133
134 }
135
136 return PASS;
137}
138
139/* Pass this test the character 'a' and it will succeed */
140
141int test4(int num, ...) {
142
143 WCHAR * TheString = convert("Pal %1!C! Testing");
144 int ReturnResult;
145 va_list TheList;
146 va_start(TheList,num);
147 memset( OutBuffer, 0, 1024 );
148 ReturnResult = FormatMessage(
149 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
150 TheString, /* message source */
151 0, /* message identifier */
152 0, /* language identifier */
153 OutBuffer, /* message buffer */
154 1024, /* maximum size of message buffer */
155 &TheList /* array of message inserts */
156 );
157
158 va_end(TheList);
159
160 if(ReturnResult == 0)
161 {
162 Fail("ERROR: The return value was 0, which indicates failure. "
163 "The function failed when trying to Format a simple string,"
164 " with the 'C' formatter.");
165 }
166
167 if(memcmp(OutBuffer, convert("Pal a Testing"),wcslen(OutBuffer)*2+2) != 0)
168 {
169 Fail("ERROR: The formated string should have been 'Pal a Testing' "
170 "but '%s' was returned.",convertC(OutBuffer));
171 }
172
173 return PASS;
174}
175
176/* Pass this test the number 57 and it will succeed */
177
178int test5(int num, ...)
179{
180
181 WCHAR * TheString = convert("Pal %1!d! Testing");
182 int ReturnResult;
183 va_list TheList;
184 va_start(TheList,num);
185 memset( OutBuffer, 0, 1024 );
186
187 ReturnResult = FormatMessage(
188 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
189 TheString, /* message source */
190 0, /* message identifier */
191 0, /* language identifier */
192 OutBuffer, /* message buffer */
193 1024, /* maximum size of message buffer */
194 &TheList /* array of message inserts */
195 );
196
197 va_end(TheList);
198
199 if(ReturnResult == 0)
200 {
201 Fail("ERROR: The return value was 0, which indicates failure. "
202 "The function failed when trying to Format a simple string, "
203 "with the 'd' formatter.");
204
205 }
206
207 if(memcmp(OutBuffer, convert("Pal 57 Testing"),wcslen(OutBuffer)*2+2) != 0)
208 {
209 Fail("ERROR: The formated string should have been 'Pal 57 Testing' "
210 "but '%s' was returned.",convertC(OutBuffer));
211
212 }
213
214 return PASS;
215}
216
217/* Pass this test the characters 'a' and 'b' and it will succeed. */
218
219int test6(int num, ...) {
220
221 WCHAR * TheString = convert("Pal %1!hc! and %2!hC! Testing");
222 int ReturnResult;
223 va_list TheList;
224 va_start(TheList,num);
225 memset( OutBuffer, 0, 1024 );
226
227 ReturnResult = FormatMessage(
228 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
229 TheString, /* message source */
230 0, /* message identifier */
231 0, /* language identifier */
232 OutBuffer, /* message buffer */
233 1024, /* maximum size of message buffer */
234 &TheList /* array of message inserts */
235 );
236
237 va_end(TheList);
238
239 if(ReturnResult == 0)
240 {
241 Fail("ERROR: The return value was 0, which indicates failure. "
242 "The function failed when trying to Format a simple string, "
243 "with the 'hc' and 'hC' formatters.");
244
245 }
246
247 if(memcmp(OutBuffer, convert("Pal a and b Testing"),
248 wcslen(OutBuffer)*2+2) != 0)
249 {
250 Fail("ERROR: The formated string should have been 'Pal a and b "
251 "Testing' but '%s' was returned.", convertC(OutBuffer));
252
253 }
254
255 return PASS;
256}
257
258/* Pass this test 90, the string 'foo' and the string 'bar' to succeed */
259
260int test7(int num, ...)
261{
262
263 WCHAR * TheString = convert("Pal %1!hd! and %2!hs! and %3!hS! Testing");
264 int ReturnResult;
265 va_list TheList;
266 va_start(TheList,num);
267 memset( OutBuffer, 0, 1024 );
268
269 ReturnResult = FormatMessage(
270 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
271 TheString, /* message source */
272 0, /* message identifier */
273 0, /* language identifier */
274 OutBuffer, /* message buffer */
275 1024, /* maximum size of message buffer */
276 &TheList /* array of message inserts */
277 );
278
279 va_end(TheList);
280
281 if(ReturnResult == 0)
282 {
283 Fail("ERROR: The return value was 0, which indicates failure. "
284 "The function failed when trying to Format a simple string, "
285 "with the 'hd', 'hs' and 'hS' formatters.");
286
287 }
288
289 if(memcmp(OutBuffer,
290 convert("Pal 90 and foo and bar Testing"),
291 wcslen(OutBuffer)*2+2) != 0)
292 {
293 Fail("ERROR: The formated string should have been 'Pal 90 and foo "
294 "and bar Testing' but '%s' was returned.",convertC(OutBuffer));
295 }
296
297 return PASS;
298}
299
300/* Pass this test the characters 'a', 'b' and the numbers 50 and 100 */
301
302int test8(int num, ...)
303{
304
305 WCHAR * TheString =
306 convert("Pal %1!lc! and %2!lC! and %3!ld! and %4!li! Testing");
307 int ReturnResult;
308 va_list TheList;
309 va_start(TheList,num);
310 memset( OutBuffer, 0, 1024 );
311
312 ReturnResult = FormatMessage(
313 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
314 TheString, /* message source */
315 0, /* message identifier */
316 0, /* language identifier */
317 OutBuffer, /* message buffer */
318 1024, /* maximum size of message buffer */
319 &TheList /* array of message inserts */
320 );
321
322 va_end(TheList);
323
324 if(ReturnResult == 0)
325 {
326 Fail("ERROR: The return value was 0, which indicates failure. "
327 "The function failed when trying to Format a simple string, "
328 "with the 'lc', 'lC', 'ld' and 'li' formatters.");
329
330 }
331
332 if(memcmp(OutBuffer,
333 convert("Pal a and b and 50 and 100 Testing"),
334 wcslen(OutBuffer)*2+2) != 0)
335 {
336 Fail("ERROR: The formated string should have been 'Pal a and b and 50"
337 " and 100 Testing' but '%s' was returned.",convertC(OutBuffer));
338
339 }
340
341 return PASS;
342}
343
344/* Pass this test the wide string 'foo' and 'bar' and the unsigned
345 int 56 to pass
346*/
347
348int test9(int num, ...) {
349
350 WCHAR * TheString = convert("Pal %1!ls! and %2!ls! and %3!lu! Testing");
351 int ReturnResult;
352 va_list TheList;
353 va_start(TheList,num);
354 memset( OutBuffer, 0, 1024 );
355 ReturnResult = FormatMessage(
356 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
357 TheString, /* message source */
358 0, /* message identifier */
359 0, /* language identifier */
360 OutBuffer, /* message buffer */
361 1024, /* maximum size of message buffer */
362 &TheList /* array of message inserts */
363 );
364
365 va_end(TheList);
366
367 if(ReturnResult == 0)
368 {
369 Fail("ERROR: The return value was 0, which indicates failure. "
370 "The function failed when trying to Format a simple string,"
371 " with the 'ls', 'lS' and 'lu' formatters.");
372
373 }
374
375 if(memcmp(OutBuffer,
376 convert("Pal foo and bar and 56 Testing"),
377 wcslen(OutBuffer)*2+2) != 0)
378 {
379 Fail("ERROR: The formated string should have been 'Pal foo and bar "
380 "and 56 Testing' but '%s' was returned.",convertC(OutBuffer));
381
382 }
383
384 return PASS;
385}
386
387/* Pass this test the hex values 0x123ab and 0x123cd */
388
389int test10(int num, ...)
390{
391
392 WCHAR * TheString = convert("Pal %1!lx! and %2!lX! Testing");
393 int ReturnResult;
394 va_list TheList;
395 va_start(TheList,num);
396 memset( OutBuffer, 0, 1024 );
397 ReturnResult = FormatMessage(
398 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
399 TheString, /* message source */
400 0, /* message identifier */
401 0, /* language identifier */
402 OutBuffer, /* message buffer */
403 1024, /* maximum size of message buffer */
404 &TheList /* array of message inserts */
405 );
406
407 va_end(TheList);
408
409 if(ReturnResult == 0)
410 {
411 Fail("ERROR: The return value was 0, which indicates failure. "
412 "The function failed when trying to Format a simple string, "
413 "with the 'lx' and 'lX' formatters.");
414
415 }
416
417 if(memcmp(OutBuffer,
418 convert("Pal 123ab and 123CD Testing"),
419 wcslen(OutBuffer)*2+2) != 0)
420 {
421 Fail("ERROR: The formated string should have been 'Pal 123ab and "
422 "123CD Testing' but '%s' was returned.", convertC(OutBuffer));
423
424 }
425
426 return PASS;
427}
428
429/* Pass this test a pointer to 0x123ab and the string 'foo' to pass */
430
431int test11(int num, ...)
432{
433
434 WCHAR * TheString = convert("Pal %1!p! and %2!S! Testing");
435 int ReturnResult;
436 va_list TheList;
437 va_start(TheList,num);
438 memset( OutBuffer, 0, 1024 );
439
440 ReturnResult = FormatMessage(
441 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
442 TheString, /* message source */
443 0, /* message identifier */
444 0, /* language identifier */
445 OutBuffer, /* message buffer */
446 1024, /* maximum size of message buffer */
447 &TheList /* array of message inserts */
448 );
449
450 va_end(TheList);
451
452 if(ReturnResult == 0)
453 {
454 Fail("ERROR: The return value was 0, which indicates failure. "
455 "The function failed when trying to Format a simple string, "
456 "with the 'p' and 'S' formatters.");
457
458 }
459
460/*
461** Run only on 64 bit platforms
462*/
463#if defined(BIT64)
464 Trace("Testing for 64 Bit Platforms \n");
465 if(memcmp(OutBuffer,
466 convert("Pal 00000000000123AB and foo Testing"),
467 wcslen(OutBuffer)*2+2) != 0 &&
468 /* BSD style */
469 memcmp( OutBuffer,
470 convert( "Pal 0x123ab and foo Testing" ),
471 wcslen(OutBuffer)*2+2 ) != 0 )
472 {
473 Fail("ERROR: The formated string should have been 'Pal 000123AB and "
474 "foo Testing' but '%s' was returned.",convertC(OutBuffer));
475
476 }
477
478#else
479 Trace("Testing for Non 64 Bit Platforms \n");
480 if(memcmp(OutBuffer,
481 convert("Pal 000123AB and foo Testing"),
482 wcslen(OutBuffer)*2+2) != 0 &&
483 /* BSD style */
484 memcmp( OutBuffer,
485 convert( "Pal 0x123ab and foo Testing" ),
486 wcslen(OutBuffer)*2+2 ) != 0 )
487 {
488 Fail("ERROR: The formated string should have been 'Pal 000123AB and "
489 "foo Testing' but '%s' was returned.",convertC(OutBuffer));
490
491 }
492
493#endif
494
495 return PASS;
496}
497
498/* Pass this test the unsigned int 100 and the hex values 0x123ab and 0x123cd
499to succeed */
500
501int test12(int num, ...)
502{
503
504 WCHAR * TheString = convert("Pal %1!u! and %2!x! and %3!X! Testing");
505 int ReturnResult;
506 va_list TheList;
507 va_start(TheList,num);
508 memset( OutBuffer, 0, 1024 );
509
510 ReturnResult = FormatMessage(
511 FORMAT_MESSAGE_FROM_STRING, /* source and processing options */
512 TheString, /* message source */
513 0, /* message identifier */
514 0, /* language identifier */
515 OutBuffer, /* message buffer */
516 1024, /* maximum size of message buffer */
517 &TheList /* array of message inserts */
518 );
519
520 va_end(TheList);
521
522 if(ReturnResult == 0)
523 {
524 Fail("ERROR: The return value was 0, which indicates failure. "
525 "The function failed when trying to Format a simple string, "
526 "with the 'u', 'x' and 'X' formatters.");
527
528 }
529
530 if(memcmp(OutBuffer,
531 convert("Pal 100 and 123ab and 123CD Testing"),
532 wcslen(OutBuffer)*2+2) != 0)
533 {
534 Fail("ERROR: The formated string should have been 'Pal 100 and "
535 "123ab and 123CD Testing' but '%s' was returned.",
536 convertC(OutBuffer));
537
538 }
539
540 return PASS;
541}
542
543int __cdecl main(int argc, char *argv[])
544{
545 WCHAR szwInsert[] = {'I','N','S','E','R','T','\0'};
546 WCHAR szwFoo[] = {'f','o','o','\0'};
547 WCHAR szwBar[] = {'b','a','r','\0'};
548
549 /*
550 * Initialize the PAL and return FAILURE if this fails
551 */
552
553 if(0 != (PAL_Initialize(argc, argv)))
554 {
555 return FAIL;
556 }
557
558 if(test1(0,szwInsert) || /* Test %s */
559 test2(0,40) || /* Test %i */
560 test3(0,'a') || /* Test %c */
561 test4(0,'a') || /* Test %C */
562 test5(0,57) || /* Test %d */
563 test6(0,'a','b') || /* Test %hc, %hC */
564 test7(0,90,"foo","bar") || /* Test %hd,hs,hS */
565 test8(0,'a','b',50,100) || /* Test %lc, lC, ld, li */
566 test9(0,szwFoo,szwBar,56) || /* Test %ls,lS,lu */
567 test10(0,0x123ab,0x123cd) || /* Test %lx, %lX */
568 test11(0,(void *)0x123ab,"foo") || /* Test %p, %S */
569 test12(0,100,0x123ab,0x123cd)) /* Test %u,x,X */
570 {
571
572
573 }
574
575 PAL_Terminate();
576 return PASS;
577
578}
579
580
581
582