icu/internaltests/conversion-test-jp-2.c
author Pavel Heimlich <pavel.heimlich@oracle.com>
Tue, 02 Nov 2010 11:29:20 +0100
changeset 39 65491902381c
permissions -rw-r--r--
add internal tests for Oracle fixes/enhancements

//   Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <unicode/utypes.h>
#include <unicode/ucnv.h>
#include <unicode/unistr.h>
#include <unicode/translit.h>

main()
{
    UBool ret = TRUE;
    UConverter *convfrom = 0;
    UConverter *convto = 0;
    UErrorCode err = U_ZERO_ERROR;
    UBool flush;
    const char *cbufp;
    char *bufp;
    char *buf = 0;
    int i;
    int32_t len32;

    const UChar *unibufbp;
    UChar *unibufp;
    UChar *unibuf = 0;
    UChar *saveunibuf;

    printf("Conversion test for ISO-2022-JP's JIS X 0201 C0 characters:\n\n");
    convto = ucnv_open("iso-2022-jp", &err);
    if (U_FAILURE(err)) {
	fprintf(stderr, "ucnv_open error - exiting.\n");
	exit(-1);
    }
    buf = (char *)malloc(1024);
    memset(buf, 0, 1024);

    /* ESC $ B ?? ESC ( J A \n for JIS X 0201
    */
    strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x4a\x41\x0a");

    /* ESC $ B ?? ESC ( J \n for JIS X 0201
    strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x4a\x0a");
    */

    /* ESC $ B ?? ESC ( B \n for ASCII
    strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x42\x0a");
    */

    saveunibuf = unibuf = (UChar *)malloc(sizeof(UChar) * 100);
    memset(unibuf, 0, sizeof(UChar) * 100);

    bufp = buf;
    unibufp = unibuf;

    printf("Converting by using ucnv_toUnicode()...\n");
    ucnv_toUnicode(convto, &unibufp, (const UChar *)(unibuf + 100),
		(const char **)&bufp, (const char *)(bufp + strlen(buf)), NULL,
		TRUE, &err);

    if (U_FAILURE(err)) {
	fprintf(stderr, "conversion error!\n");
	exit(-1);
    }

    for (i = 0; unibuf < unibufp; unibuf++)
	printf("%3d  U+%04X\n", i++, *unibuf);

    if (i > 2 && *(unibuf - 3) == 0x7247 && *(unibuf - 2) == 0x41 &&
	*(unibuf - 1) == 0xa)
	printf("Conversion done correctly - success.\n");
    else
	printf("Conversion done incorrectly - failed.\n");
    if (convto) ucnv_close(convto);

    if (buf) delete[] buf;
    if (saveunibuf) delete[] saveunibuf;
}