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: test4.c |
8 | ** |
9 | ** Purpose: Tests that WideCharToMultiByte correctly handles WC_NO_BEST_FIT_CHARS |
10 | ** |
11 | ** |
12 | **==========================================================================*/ |
13 | |
14 | |
15 | #include <palsuite.h> |
16 | |
17 | /* C with a circumflex */ |
18 | wchar_t ustr[2] = { 0x108, 0 }; |
19 | |
20 | /* expected conversion when best fit is allowed on Windows */ |
21 | char* lpBestFitRes = "C" ; |
22 | |
23 | /* expected conversion when no default character is specified */ |
24 | char* lpResStr1 = "?" ; |
25 | |
26 | /* expected conversion when the default character is 'k' */ |
27 | char myDefaultChar = 'k'; |
28 | char* lpResStr2 = "k" ; |
29 | |
30 | int |
31 | TestWideCharToMultiByte( |
32 | IN UINT CodePage, |
33 | IN DWORD dwFlags, |
34 | IN LPCSTR lpDefaultChar, |
35 | IN LPSTR lpResStr) |
36 | { |
37 | char mbstr[30]; |
38 | int ret; |
39 | int testStatus = PASS; |
40 | BOOL usedDefaultChar = FALSE; |
41 | |
42 | printf("WideCharToMultiByte (CodePage=%d, dwFlags=%#x, default=%c)\n" , |
43 | CodePage, dwFlags, lpDefaultChar?*lpDefaultChar:' '); |
44 | ret = WideCharToMultiByte(CodePage, dwFlags, ustr, -1, mbstr, sizeof(mbstr), |
45 | lpDefaultChar, &usedDefaultChar); |
46 | if (ret != 0) { |
47 | printf(" converted C with circumflex to in Unicode to multibyte: " |
48 | "\"%s\"\n" , mbstr); |
49 | printf(" used default character?: %d\n" , usedDefaultChar); |
50 | if (strcmp(mbstr, lpResStr) != 0 || usedDefaultChar != TRUE) |
51 | { |
52 | printf("!!!! failed conversion !!!!\n" ); |
53 | testStatus = FAIL; |
54 | } |
55 | } |
56 | else { |
57 | printf("!!!! failed conversion !!!!\n" ); |
58 | testStatus = FAIL; |
59 | } |
60 | return testStatus; |
61 | } |
62 | |
63 | int __cdecl main(int argc, char *argv[]) |
64 | { |
65 | int testStatus = PASS; |
66 | |
67 | if (PAL_Initialize(argc, argv)) |
68 | { |
69 | return FAIL; |
70 | } |
71 | |
72 | /* Use WideCharToMultiByte to convert the string in code page CP_ACP. |
73 | * Note that the resulting string will be different on Windows PAL and |
74 | * Unix PAL. On Windows, the default best fit behavior will map C with |
75 | * circumflex to C. |
76 | * |
77 | * testStatus |= TestWideCharToMultiByte(CP_ACP, 0, NULL, lpBestFitRes); |
78 | * |
79 | * On Unix, where there is no support for finding best fit, it will be |
80 | * mapped to a '?'. In addition, it will trigger an ASSERT in the dbg/chk |
81 | * builds. |
82 | * |
83 | * testStatus |= TestWideCharToMultiByte(CP_ACP, 0, NULL, lpResStr1); |
84 | */ |
85 | |
86 | /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS to convert the string |
87 | * in CP_ACP (1252 by default). This will prevent it from mapping the C |
88 | * with circumflex to its closest match in the ANSI code page: C |
89 | */ |
90 | testStatus |= TestWideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, NULL, lpResStr1); |
91 | |
92 | |
93 | /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS and a default character |
94 | * to convert the string. This will prevent it from mapping the C with |
95 | * circumflex to its closest match in the ANSI code page: C. It will be |
96 | * replaced with the specified default character. |
97 | */ |
98 | testStatus |= TestWideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, &myDefaultChar, lpResStr2); |
99 | |
100 | /* Use WideCharToMultiByte to convert the string in code page 1253 |
101 | * Note that the resulting string will be different on Windows PAL and |
102 | * Unix PAL. On Windows, the default best fit behavior will map C with |
103 | * circumflex to C. |
104 | * |
105 | * testStatus |= TestWideCharToMultiByte(1253, 0, NULL, lpBestFitRes); |
106 | * |
107 | * On Unix, where there is no support for finding best fit, it will be |
108 | * mapped to a '?'. In addition, it will trigger an ASSERT in the dbg/chk |
109 | * builds. |
110 | * |
111 | * testStatus |= TestWideCharToMultiByte(1253, 0, NULL, lpResStr1); |
112 | */ |
113 | |
114 | /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS to convert the string |
115 | * in 1253. This will prevent it from mapping the C |
116 | * with circumflex to its closest match in the ANSI code page: C |
117 | */ |
118 | testStatus |= TestWideCharToMultiByte(1253, WC_NO_BEST_FIT_CHARS, NULL, lpResStr1); |
119 | |
120 | /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS and a default |
121 | * character to convert the string in 1253. This will prevent it from |
122 | * mapping the C with circumflex to its closest match in the ANSI code |
123 | * page: C. It will be replaced with the specified default character. |
124 | */ |
125 | testStatus |= TestWideCharToMultiByte(1253, WC_NO_BEST_FIT_CHARS, &myDefaultChar, lpResStr2); |
126 | |
127 | PAL_TerminateEx(testStatus); |
128 | |
129 | return testStatus; |
130 | } |
131 | |
132 | |