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: test3.c
8**
9** Purpose: Tests that WideCharToMultiByte correctly handles the following
10** error conditions: insufficient buffer space, invalid code pages,
11** and invalid flags.
12**
13**
14**==========================================================================*/
15
16
17#include <palsuite.h>
18
19
20int __cdecl main(int argc, char *argv[])
21{
22 char mbStr[128];
23 WCHAR wideStr[128];
24 int ret;
25 int i;
26 int k;
27 BOOL bRet=TRUE;
28
29 /* These codepages are currently supported by the PAL */
30 int codePages[] ={
31 CP_ACP,
32 CP_UTF8
33 };
34
35
36 if (PAL_Initialize(argc, argv))
37 {
38 return FAIL;
39 }
40
41 /* Go through all of the code pages */
42 for(i=0; i<(sizeof(codePages)/sizeof(int)); i++)
43 {
44
45 for (k=0; k<128; k++)
46 {
47 wideStr[k] = 'a';
48 mbStr[k] = 0;
49 }
50
51 wideStr[127] = 0;
52
53 /* try with insufficient buffer space */
54 ret = WideCharToMultiByte(codePages[i], 0, wideStr, -1,
55 mbStr, 10, NULL, NULL);
56 if (ret != 0)
57 {
58 Trace("WideCharToMultiByte did not return an error!\n"
59 "Expected return of 0, got %d for code page %d.\n", ret,
60 codePages[i]);
61 bRet = FALSE;
62 }
63
64 ret = GetLastError();
65 if (ret != ERROR_INSUFFICIENT_BUFFER)
66 {
67 Fail("WideCharToMultiByte set the last error to %u instead of "
68 "ERROR_INSUFFICIENT_BUFFER for code page %d.\n",
69 GetLastError(),codePages[i]);
70 bRet = FALSE;
71 }
72 }
73
74 /* Return failure if any of the code pages returned the wrong results */
75 if(!bRet)
76 {
77 return FAIL;
78 }
79
80 /* try with a wacky code page */
81 ret = WideCharToMultiByte(-1, 0, wideStr, -1, mbStr, 128, NULL, NULL);
82 if (ret != 0)
83 {
84 Fail("WideCharToMultiByte did not return an error!\n"
85 "Expected return of 0, got %d for invalid code page.\n", ret);
86 }
87
88 ret = GetLastError();
89 if (ret != ERROR_INVALID_PARAMETER)
90 {
91 Fail("WideCharToMultiByte set the last error to %u instead of "
92 "ERROR_INVALID_PARAMETER for invalid code page -1.\n",
93 GetLastError());
94 }
95
96 PAL_Terminate();
97
98 return PASS;
99}
100
101