icu/internaltests/conversion-test-kr.c
author jenda
Mon, 14 Nov 2011 18:12:00 +0100
changeset 87 2a9fa2fe31e4
parent 39 65491902381c
permissions -rw-r--r--
Added tag s11-fcs for changeset da5274f11060

//   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;

    printf("Conversion testing for ISO-2022-KR:\n\n");
    convto = ucnv_open("iso-2022-kr", &err);
    if (U_FAILURE(err)) {
	fprintf(stderr, "ucnv_open error - exiting.\n");
	exit(-1);
    }
    buf = (char *)malloc(1024);
    memset(buf, 0, 1024);
    unibuf = (UChar *)malloc(sizeof(UChar) * 4);
    unibuf[0] = 0xac00;
    unibuf[1] = 0x000d;
    unibuf[2] = 0x000a;
    unibuf[3] = 0xac00;

	bufp = buf;
        unibufp = unibuf;

	printf("Checking ucnv_fromUnicode()...\n");
	ucnv_fromUnicode(convto, &bufp, (const char *)bufp + 1024,
                             (const UChar **)&unibufp,
                             (const UChar *)unibuf + 4,
                             NULL, (1 > 0), &err);
	bufp += 15;
	unibufp = unibuf + 4;
	ucnv_fromUnicode(convto, &bufp, (const char *)bufp + 1024 - 15,
                             (const UChar **)&unibufp,
                             (const UChar *)unibufp,
                             NULL, (1 > 0), &err);
	if (U_FAILURE(err)) {
		fprintf(stderr, "conversion error!\n");
		exit(-1);
            }

	for (i = 0; buf[i]; i++)
		if (buf[i] == 0x1b)
			printf("%3d  %02x ESC\n", i, (unsigned char)buf[i]);
		else
			printf("%3d  %02x\n", i, (unsigned char)buf[i]);

	if (i > 0 && buf[i - 1] == 0x0f)
		printf("Conversion ended with SI - success.\n");
	else
		printf("Conversion didn't ended with SI - failed.\n");

	ucnv_resetFromUnicode(convto);

    memset(buf, 0, 1024);
    unibuf[0] = 0xac00;
    unibuf[1] = 0x000d;
    unibuf[2] = 0x000a;
    unibuf[3] = 0xac00;

	bufp = buf;
        unibufp = unibuf;
	printf("\nChecking ucnv_fromUChars()...\n");
	len32 = ucnv_fromUChars(convto, bufp, 1024, unibufp, 4, &err);
	for (i = 0; len32 > i; i++)
		if (buf[i] == 0x1b)
			printf("%3d  %02x ESC\n", i, (unsigned char)buf[i]);
		else
			printf("%3d  %02x\n", i, (unsigned char)buf[i]);

	if (i > 0 && buf[i - 1] == 0x0f)
		printf("Conversion ended with SI - success.\n");
	else
		printf("Conversion didn't ended with SI - failed.\n");

    if (convto) ucnv_close(convto);

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