summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/locale_info/WideCharToMultiByte/test4/test4.c
blob: 8ab5fe90af8ce571dac359cd0a962b64b3dd05d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================================
**
** Source: test4.c
**
** Purpose: Tests that WideCharToMultiByte correctly handles WC_NO_BEST_FIT_CHARS
**
**
**==========================================================================*/


#include <palsuite.h>

/* C with a circumflex */
wchar_t ustr[2] = { 0x108, 0 };

/* expected conversion when best fit is allowed on Windows */
char* lpBestFitRes = "C";

/* expected conversion when no default character is specified */
char* lpResStr1 = "?";

/* expected conversion when the default character is 'k' */
char myDefaultChar = 'k';
char* lpResStr2 = "k";

int 
TestWideCharToMultiByte(
       IN UINT CodePage,
       IN DWORD dwFlags,
       IN LPCSTR lpDefaultChar,
       IN LPSTR lpResStr)
{
  char mbstr[30];
  int ret;
  int testStatus = PASS;
  BOOL usedDefaultChar = FALSE;

  printf("WideCharToMultiByte (CodePage=%d, dwFlags=%#x, default=%c)\n",
         CodePage, dwFlags, lpDefaultChar?*lpDefaultChar:' ');
  ret = WideCharToMultiByte(CodePage, dwFlags, ustr, -1, mbstr, sizeof(mbstr), 
                            lpDefaultChar, &usedDefaultChar);
  if (ret != 0) {
    printf("   converted C with circumflex to in Unicode to multibyte: "
           "\"%s\"\n", mbstr);
    printf("   used default character?: %d\n", usedDefaultChar);
    if (strcmp(mbstr, lpResStr) != 0 || usedDefaultChar != TRUE) 
    {
       printf("!!!! failed conversion !!!!\n");
       testStatus = FAIL;
    }
  }
  else {
    printf("!!!! failed conversion !!!!\n");
    testStatus = FAIL;
  }
  return testStatus;
}

int __cdecl main(int argc, char *argv[])
{    
  int testStatus = PASS;

  if (PAL_Initialize(argc, argv))
  {
      return FAIL;
  }

  /* Use WideCharToMultiByte to convert the string in code page CP_ACP.
   * Note that the resulting string will be different on Windows PAL and 
   * Unix PAL. On Windows, the default best fit behavior will map C with 
   * circumflex to C. 
   * 
   *   testStatus |= TestWideCharToMultiByte(CP_ACP, 0, NULL, lpBestFitRes);
   *
   * On Unix, where there is no support for finding best fit, it will be 
   * mapped to a '?'. In addition, it will trigger an ASSERT in the dbg/chk
   * builds.
   *
   *   testStatus |= TestWideCharToMultiByte(CP_ACP, 0, NULL, lpResStr1);
   */

  /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS to convert the string 
   * in CP_ACP (1252 by default). This will prevent it from mapping the C 
   * with circumflex to its closest match in the ANSI code page: C
   */
  testStatus |= TestWideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, NULL, lpResStr1);


  /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS and a default character 
   * to convert the string. This will prevent it from mapping the C with 
   * circumflex to its closest match in the ANSI code page: C. It will be
   * replaced with the specified default character.
   */
  testStatus |= TestWideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, &myDefaultChar, lpResStr2);

  /* Use WideCharToMultiByte to convert the string in code page 1253 
   * Note that the resulting string will be different on Windows PAL and 
   * Unix PAL. On Windows, the default best fit behavior will map C with 
   * circumflex to C. 
   * 
   *   testStatus |= TestWideCharToMultiByte(1253, 0, NULL, lpBestFitRes);
   *
   * On Unix, where there is no support for finding best fit, it will be 
   * mapped to a '?'. In addition, it will trigger an ASSERT in the dbg/chk
   * builds.
   *
   *   testStatus |= TestWideCharToMultiByte(1253, 0, NULL, lpResStr1);
   */

  /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS to convert the string 
   * in 1253. This will prevent it from mapping the C 
   * with circumflex to its closest match in the ANSI code page: C
   */
  testStatus |= TestWideCharToMultiByte(1253, WC_NO_BEST_FIT_CHARS, NULL, lpResStr1);

  /* Use WideCharToMultiByte with WC_NO_BEST_FIR_CHARS and a default 
   * character to convert the string in 1253. This will prevent it from 
   * mapping the C with circumflex to its closest match in the ANSI code 
   * page: C. It will be replaced with the specified default character.
   */
  testStatus |= TestWideCharToMultiByte(1253, WC_NO_BEST_FIT_CHARS, &myDefaultChar, lpResStr2);

  PAL_TerminateEx(testStatus);

  return testStatus;
}