[8772] | 1 | /* $Id: MultiByteToWideChar.c,v 1.1 2002-06-26 07:11:03 sandervl Exp $
|
---|
| 2 | *
|
---|
| 3 | * Test of MultiByteToWideChar().
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 2002 knut st. osmundsen (bird@anduin.net)
|
---|
| 6 | *
|
---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | /*******************************************************************************
|
---|
| 12 | * Header Files *
|
---|
| 13 | *******************************************************************************/
|
---|
| 14 | #include <windows.h>
|
---|
| 15 |
|
---|
| 16 | #include <process.h>
|
---|
| 17 | #include <string.h>
|
---|
| 18 | #include <stdio.h>
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Display syntax
|
---|
| 23 | */
|
---|
| 24 | int syntax(const char * arg0)
|
---|
| 25 | {
|
---|
| 26 | const char *psz = strrchr(arg0, '\\');
|
---|
| 27 | if (!psz)
|
---|
| 28 | psz = strrchr(arg0, '/');
|
---|
| 29 | else if (strrchr(psz, '/'))
|
---|
| 30 | psz = strrchr(psz, '/');
|
---|
| 31 | if (!psz)
|
---|
| 32 | psz = strrchr(arg0, ':');
|
---|
| 33 | if (!psz)
|
---|
| 34 | psz = arg0;
|
---|
| 35 | else
|
---|
| 36 | psz++;
|
---|
| 37 |
|
---|
| 38 | printf("Syntax: %s <testcase#>\n"
|
---|
| 39 | "\n"
|
---|
| 40 | "Testcases: \n"
|
---|
| 41 | , psz);
|
---|
| 42 | return -1;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | *
|
---|
| 49 | * @returns 0 on success.
|
---|
| 50 | * non-zero on failure.
|
---|
| 51 | * @param argv A
|
---|
| 52 | * @status completely implemented.
|
---|
| 53 | * @author knut st. osmundsen (bird@anduin.net)
|
---|
| 54 | * @remark
|
---|
| 55 | */
|
---|
| 56 | int main(int argc, char **argv)
|
---|
| 57 | {
|
---|
| 58 | int i,j,k;
|
---|
| 59 | int iCase;
|
---|
| 60 | int argi;
|
---|
| 61 | int rc = 0;
|
---|
| 62 |
|
---|
| 63 | /*
|
---|
| 64 | * Check for syntax request.
|
---|
| 65 | */
|
---|
| 66 | if (argc != 2 || atol(argv[1]) == 0)
|
---|
| 67 | return syntax(argv[0]);
|
---|
| 68 |
|
---|
| 69 | for (argi = 1; argi < argc; argi++)
|
---|
| 70 | if ( (argv[argi][0] == '-' || argv[argi][0] == '/')
|
---|
| 71 | && (argv[argi][1] == '-' || argv[argi][1] == 'h' || argv[argi][1] == 'H')
|
---|
| 72 | )
|
---|
| 73 | return syntax(argv[0]);
|
---|
| 74 |
|
---|
| 75 | iCase = atol(argv[1]);
|
---|
| 76 | switch (iCase)
|
---|
| 77 | {
|
---|
| 78 | /*
|
---|
| 79 | * Simple testcases.
|
---|
| 80 | */
|
---|
| 81 | case 1:
|
---|
| 82 | {
|
---|
| 83 | #define CHPAD 0xaf
|
---|
| 84 | #define WCPAD 0xafaf
|
---|
| 85 | static struct _SimpleTests
|
---|
| 86 | {
|
---|
| 87 | int iRc; /* Return code. */
|
---|
| 88 | int iCodepage; /* Codepage of the source string. */
|
---|
| 89 | int fFlags; /* Flags to pass on. */
|
---|
| 90 | int cchSrc; /* srclen = delta + strlen(pszString) */
|
---|
| 91 | const char * pszSrc; /* Source string. */
|
---|
| 92 | int cwcDst; /* dstlen = delta + sizeof(awcDst) */
|
---|
| 93 | const WCHAR awcDst[256]; /* Result string. */
|
---|
| 94 | } aTests[] =
|
---|
| 95 | {
|
---|
| 96 | /* rc cp fFlags srclen srcstr dstlen dststr */
|
---|
| 97 | { 7, 865, MB_PRECOMPOSED, -1, "", 0, {WCPAD}},
|
---|
| 98 | { 7, 865, MB_PRECOMPOSED, -1, "", 7, {0x00f8,0x00e6,0x00e5,0x00d8,0x00c6,0x00c5,0x0000}},
|
---|
| 99 | { 0, 865, MB_PRECOMPOSED, -1, "bcdEfghiJklmn", 6, {0x00f8,0x00e6,0x00e5,0x00d8,0x00c6,0x00c5}},
|
---|
| 100 | { 3, 865, MB_PRECOMPOSED, 3, "bcdEfghiJklmn", 6, {0x00f8,0x00e6,0x00e5, WCPAD, WCPAD, WCPAD}},
|
---|
| 101 | { 20, 865, MB_PRECOMPOSED, 20, "\0abcdefghijklmnopqrstuvwxyz", 0, {WCPAD}},
|
---|
| 102 | { 20, 865, MB_PRECOMPOSED, 20, "\0abcdefghijklmnopqrstuvwxyz", 20, {0x00f8,0x00e6,0x00e5,0x0000,0x00d8,0x00c6,0x00c5,0x0061,0x0062,0x0063,
|
---|
| 103 | 0x0064,0x0065,0x0066,0x0067,0x0068,0x0069,0x006a,0x006b,0x006c,0x006d}},
|
---|
| 104 | { 20, 865, MB_PRECOMPOSED, 20, "\0abcdefghijklmnopqrstuvwxyz", 21, {0x00f8,0x00e6,0x00e5,0x0000,0x00d8,0x00c6,0x00c5,0x0061,0x0062,0x0063,
|
---|
| 105 | 0x0064,0x0065,0x0066,0x0067,0x0068,0x0069,0x006a,0x006b,0x006c,0x006d, WCPAD}},
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | /*
|
---|
| 110 | * Loop thru the testcases in aTests.
|
---|
| 111 | */
|
---|
| 112 | for (i = 0; i < sizeof(aTests) / sizeof(aTests[0]); i++)
|
---|
| 113 | {
|
---|
| 114 | WCHAR awcDst[256];
|
---|
| 115 | int iApiRc;
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Initialize the buffer with known value.
|
---|
| 119 | * Print call.
|
---|
| 120 | * Do call.
|
---|
| 121 | * Print result.
|
---|
| 122 | */
|
---|
| 123 | memset(&awcDst[0], CHPAD, sizeof(awcDst));
|
---|
| 124 |
|
---|
| 125 | printf("%-2d: MultiByteToWideChar(%d, %#x, \"%s\", %d, %#x, %d)",
|
---|
| 126 | i,
|
---|
| 127 | aTests[i].iCodepage,
|
---|
| 128 | aTests[i].fFlags,
|
---|
| 129 | aTests[i].pszSrc,
|
---|
| 130 | aTests[i].cchSrc,
|
---|
| 131 | awcDst,
|
---|
| 132 | aTests[i].cwcDst);
|
---|
| 133 |
|
---|
| 134 | iApiRc = MultiByteToWideChar(aTests[i].iCodepage,
|
---|
| 135 | aTests[i].fFlags,
|
---|
| 136 | aTests[i].pszSrc,
|
---|
| 137 | aTests[i].cchSrc,
|
---|
| 138 | awcDst,
|
---|
| 139 | aTests[i].cwcDst);
|
---|
| 140 | printf(" -> %d\n", iApiRc);
|
---|
| 141 |
|
---|
| 142 | /*
|
---|
| 143 | * Check return value.
|
---|
| 144 | */
|
---|
| 145 | if (iApiRc != aTests[i].iRc)
|
---|
| 146 | {
|
---|
| 147 | printf("error(%d): bad rc. rc=%d expected=%d.\n", i, iApiRc, aTests[i].iRc);
|
---|
| 148 | rc++;
|
---|
| 149 | }
|
---|
| 150 | /*else*/
|
---|
| 151 | /*
|
---|
| 152 | * Check the output string (if any).
|
---|
| 153 | */
|
---|
| 154 | if ( aTests[i].cwcDst > 0
|
---|
| 155 | && memcmp(&awcDst[0], aTests[i].awcDst, aTests[i].cwcDst * sizeof(WCHAR))
|
---|
| 156 | )
|
---|
| 157 | {
|
---|
| 158 | printf("error(%d): result string mismatch.\n", i);
|
---|
| 159 | for (j = 0; j < aTests[i].cwcDst; j += 10)
|
---|
| 160 | {
|
---|
| 161 | printf("actual: 0x%04x", awcDst[j]);
|
---|
| 162 | for (k = j + 1; k < aTests[i].cwcDst && k - j < 10; k++)
|
---|
| 163 | printf(",0x%04x", awcDst[k]);
|
---|
| 164 | printf("\n"
|
---|
| 165 | "expected:0x%04x", aTests[i].awcDst[k]);
|
---|
| 166 | for (k = j + 1; k < aTests[i].cwcDst && k - j < 10; k++)
|
---|
| 167 | printf(",0x%04x", aTests[i].awcDst[k]);
|
---|
| 168 | printf("\n");
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | /*else*/
|
---|
| 172 | /*
|
---|
| 173 | * Check the padding of the output buffer.
|
---|
| 174 | */
|
---|
| 175 | if (aTests[i].cwcDst > 0)
|
---|
| 176 | {
|
---|
| 177 | const char *pchEnd = (const char*)&awcDst[0] + sizeof(awcDst);
|
---|
| 178 | const char *pch = (const char*)&awcDst[aTests[i].cwcDst];
|
---|
| 179 | int cPaddingErrors = 0;
|
---|
| 180 | while (pch < pchEnd)
|
---|
| 181 | {
|
---|
| 182 | if (*pch != CHPAD)
|
---|
| 183 | {
|
---|
| 184 | printf("error(%d) - padding error at buffer offset %#x: 0x%02x != 0x%02x\n",
|
---|
| 185 | i, pch - (const char*)&awcDst[0], *pch, CHPAD);
|
---|
| 186 | if (++cPaddingErrors > 5)
|
---|
| 187 | break;
|
---|
| 188 | }
|
---|
| 189 | pch++;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | } /* for aTests */
|
---|
| 194 | break;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | default:
|
---|
| 198 | printf("fatal error: Invalid testcase number (%d).\n", atol(argv[1]));
|
---|
| 199 | rc = 16;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | printf(" . %d Errors .\n", rc);
|
---|
| 203 | fflush(stdout); /* odin32 bug! */
|
---|
| 204 | return rc;
|
---|
| 205 | }
|
---|