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: test1.c
8**
9** Purpose: Tests WideCharToMultiByte with all the ASCII characters (0-127).
10** Also tests that WideCharToMultiByte handles different buffer
11** lengths correctly (0, -1, and a valid length)
12**
13**
14**==========================================================================*/
15
16#include <palsuite.h>
17
18int __cdecl main(int argc, char *argv[])
19{
20 char mbStr[128];
21 WCHAR wideStr[128];
22 int ret;
23 int i;
24 int k;
25 BOOL bRet=TRUE;
26
27 /* These codepages are currently supported by the PAL */
28 int codePages[] ={
29 CP_ACP,
30 CP_UTF8
31 };
32
33
34 if (PAL_Initialize(argc, argv))
35 {
36 return FAIL;
37 }
38
39
40 /* Go through all of the code pages */
41 for(i=0; i<(sizeof(codePages)/sizeof(int)); i++)
42 {
43
44 for (k=0; k<128; k++)
45 {
46 wideStr[k] = 127 - k;
47 mbStr[k] = 0;
48 }
49
50 /* Convert with buffer size of 0 */
51 ret = WideCharToMultiByte(codePages[i], 0, wideStr, -1,
52 mbStr, 0, NULL, NULL);
53 if (ret != 128)
54 {
55 Trace("WideCharToMultiByte did not return correct string length!\n"
56 "Got %d, expected %d for code page %d with error %u.\n",
57 ret, 128,codePages[i],GetLastError());
58 bRet=FALSE;
59 }
60
61 /* Make sure the ASCII set (0-127) gets translated correctly */
62 ret = WideCharToMultiByte(codePages[i], 0, wideStr, -1,
63 mbStr, 128, NULL, NULL);
64 if (ret != 128)
65 {
66 Trace("WideCharToMultiByte did not return correct string length!\n"
67 "Got %d, expected %d for code page %d with error %u.\n",
68 ret, 128,codePages[i],GetLastError());
69 bRet=FALSE;
70 }
71
72 for (k=0; k<128; k++)
73 {
74 if (mbStr[k] != 127 - k)
75 {
76 Trace("WideCharToMultiByte failed to translate correctly!\n"
77 "Expected character %d to be %c (%x), got %c (%x) for "
78 "code page %d\n",k, 127 - k, 127 - k,mbStr[k], mbStr[k],
79 codePages[i]);
80 bRet=FALSE;
81 }
82 }
83
84
85 /* try a 0 length string ("") */
86 wideStr[0] = '\0';
87 ret = WideCharToMultiByte(codePages[i], 0, wideStr, -1,
88 mbStr, 0, NULL, NULL);
89 if (ret != 1)
90 {
91 Trace("WideCharToMultiByte did not return correct string length!\n"
92 "Got %d, expected %d for code page %d with error %u.\n",
93 ret, 1,codePages[i],GetLastError());
94 bRet=FALSE;
95 }
96 }
97
98 int result = bRet ? PASS : FAIL;
99 PAL_TerminateEx(result);
100 return result;
101}
102
103