ICU-4222 Make sure that the transliterator works on strings,

and make sure that the transliterator flushes independently from IO flushing.

X-SVN-Rev: 17990
This commit is contained in:
George Rhoten 2005-06-23 07:10:52 +00:00
parent d15b186c69
commit 51d2f74bc1
9 changed files with 356 additions and 217 deletions

View File

@ -1,7 +1,7 @@
/*
*******************************************************************************
*
* Copyright (C) 1998-2004, International Business Machines
* Copyright (C) 1998-2005, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
@ -76,10 +76,11 @@ struct UFILE {
* Like u_file_write but takes a flush parameter
*/
U_CAPI int32_t U_EXPORT2
u_file_write_flush( const UChar *chars,
int32_t count,
UFILE *f,
UBool flush);
u_file_write_flush( const UChar *chars,
int32_t count,
UFILE *f,
UBool flushIO,
UBool flushTranslit);
/**
* Fill a UFILE's buffer with converted codepage data.

View File

@ -233,7 +233,7 @@ ufile_flush_translit(UFILE *f)
return;
#endif
u_file_write_flush(NULL, 0, f, TRUE);
u_file_write_flush(NULL, 0, f, FALSE, TRUE);
}
@ -290,10 +290,11 @@ u_fputc(UChar32 uc,
U_CAPI int32_t U_EXPORT2
u_file_write_flush( const UChar *chars,
int32_t count,
UFILE *f,
UBool flush)
u_file_write_flush(const UChar *chars,
int32_t count,
UFILE *f,
UBool flushIO,
UBool flushTranslit)
{
/* Set up conversion parameters */
UErrorCode status = U_ZERO_ERROR;
@ -305,32 +306,33 @@ u_file_write_flush( const UChar *chars,
int32_t written = 0;
int32_t numConverted = 0;
if (!f->fFile) {
int32_t charsLeft = (int32_t)(f->str.fLimit - f->str.fPos);
if (flush && charsLeft > count) {
count++;
}
written = ufmt_min(count, charsLeft);
u_strncpy(f->str.fPos, chars, written);
f->str.fPos += written;
return written;
}
if (count < 0) {
count = u_strlen(chars);
}
mySourceEnd = chars + count;
#if !UCONFIG_NO_TRANSLITERATION
if((f->fTranslit) && (f->fTranslit->translit))
{
/* Do the transliteration */
mySource = u_file_translit(f, chars, &count, flush);
mySource = u_file_translit(f, chars, &count, flushTranslit);
sourceAlias = mySource;
mySourceEnd = mySource + count;
}
#endif
/* Write to a string. */
if (!f->fFile) {
int32_t charsLeft = (int32_t)(f->str.fLimit - f->str.fPos);
if (flushIO && charsLeft > count) {
count++;
}
written = ufmt_min(count, charsLeft);
u_strncpy(f->str.fPos, mySource, written);
f->str.fPos += written;
return written;
}
mySourceEnd = mySource + count;
/* Perform the conversion in a loop */
do {
status = U_ZERO_ERROR;
@ -342,7 +344,7 @@ u_file_write_flush( const UChar *chars,
&mySource,
mySourceEnd,
NULL,
flush,
flushIO,
&status);
} else { /*weiv: do the invariant conversion */
u_UCharsToChars(mySource, myTarget, count);
@ -372,7 +374,7 @@ u_file_write( const UChar *chars,
int32_t count,
UFILE *f)
{
return u_file_write_flush(chars,count,f,FALSE);
return u_file_write_flush(chars,count,f,FALSE,FALSE);
}

View File

@ -32,7 +32,7 @@ CPPFLAGS += -I$(top_builddir)/common -I$(top_srcdir)/common -I$(top_srcdir)/i18n
DEFS += -D'U_TOPSRCDIR="$(top_srcdir)/"' -D'U_TOPBUILDDIR="$(BUILDDIR)"'
LIBS = $(LIBCTESTFW) $(LIBICUTOOLUTIL) $(LIBICUIO) $(LIBICUI18N) $(LIBICUUC) $(DEFAULT_LIBS) $(LIB_M)
OBJECTS = iotest.o strtst.o filetst.o
OBJECTS = iotest.o strtst.o filetst.o trnstst.o
DEPS = $(OBJECTS:.o=.d)

View File

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 2004-2004, International Business Machines
* Copyright (C) 2004-2005, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* file name: filetst.c
@ -16,7 +16,6 @@
#include "unicode/ustdio.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
#include "unicode/utrans.h"
#include <string.h>
@ -1282,185 +1281,6 @@ static void TestVargs(void) {
Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
}
#endif
static void TestTranslitOps(void)
{
#if !UCONFIG_NO_TRANSLITERATION
UFILE *f;
UErrorCode err = U_ZERO_ERROR;
UTransliterator *a = NULL, *b = NULL, *c = NULL;
log_verbose("opening a transliterator and UFILE for testing\n");
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
if(f == NULL)
{
log_err("Couldn't open test file for writing\n");
return;
}
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Error opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(U_FAILURE(err))
{
log_err("Error setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != NULL)
{
log_err("Error, a transliterator was already set!\n");
}
b = u_fsettransliterator(NULL, U_WRITE, a, &err);
if(err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("Error setting transliterator on NULL file err=%s\n", u_errorName(err));
}
if(b != a)
{
log_err("Error getting the same transliterator was not returned on NULL file\n");
}
err = U_FILE_ACCESS_ERROR;
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(err != U_FILE_ACCESS_ERROR)
{
log_err("Error setting transliterator on error status err=%s\n", u_errorName(err));
}
if(b != a)
{
log_err("Error getting the same transliterator on error status\n");
}
err = U_ZERO_ERROR;
log_verbose("un-setting transliterator (setting to null)\n");
c = u_fsettransliterator(f, U_WRITE, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(c != a)
{
log_err("Err, transliterator that came back was not the original one.\n");
}
log_verbose("Trying to set read transliterator (should fail)\n");
b = u_fsettransliterator(f, U_READ, NULL, &err);
if(err != U_UNSUPPORTED_ERROR)
{
log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err));
u_fclose(f);
return;
}
else
{
log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err));
err = U_ZERO_ERROR;
}
utrans_close(c);
u_fclose(f);
#endif
}
static void TestTranslitOut(void)
{
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_TRANSLITERATION
UFILE *f;
UErrorCode err = U_ZERO_ERROR;
UTransliterator *a = NULL, *b = NULL, *c = NULL;
FILE *infile;
UChar compare[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x0000 };
UChar ubuf[256];
int len;
log_verbose("opening a transliterator and UFILE for testing\n");
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "utf-16");
if(f == NULL)
{
log_err("Couldn't open test file for writing\n");
return;
}
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != NULL)
{
log_err("Err, a transliterator was already set!\n");
}
u_fprintf(f, "Stephanos");
u_fclose(f);
log_verbose("Re reading test file to verify transliteration\n");
infile = fopen(STANDARD_TEST_FILE, "rb");
if(infile == NULL)
{
log_err("Couldn't reopen test file\n");
return;
}
len=fread(ubuf, sizeof(UChar), u_strlen(compare), infile);
log_verbose("Read %d UChars\n", len);
if(len != u_strlen(compare))
{
log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare), len);
}
ubuf[len]=0;
if(u_strlen(compare) != u_strlen(ubuf))
{
log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), len);
}
if(u_strcmp(compare, ubuf))
{
log_err("Read string doesn't match expected.\n");
}
else
{
log_verbose("Read string matches expected.\n");
}
fclose(infile);
#endif
#endif
}
U_CFUNC void
addFileTest(TestNode** root) {
@ -1481,11 +1301,4 @@ addFileTest(TestNode** root) {
addTest(root, &TestBadScanfFormat, "file/TestBadScanfFormat");
addTest(root, &TestVargs, "file/TestVargs");
#endif
#if !UCONFIG_NO_TRANSLITERATION
addTest(root, &TestTranslitOps, "file/translit/ops");
#if !UCONFIG_NO_FORMATTING
addTest(root, &TestTranslitOut, "file/translit/out");
#endif
#endif
}

View File

@ -767,6 +767,7 @@ U_CDECL_END
static void addAllTests(TestNode** root) {
addFileTest(root);
addStringTest(root);
addTranslitTest(root);
#if !UCONFIG_NO_FORMATTING
addTest(root, &DataDrivenPrintf, "datadriv/DataDrivenPrintf");

View File

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 2004-2004, International Business Machines
* Copyright (C) 2004-2005, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* file name: iotest.h
@ -24,6 +24,9 @@ addStringTest(TestNode** root);
U_CFUNC void
addFileTest(TestNode** root);
U_CFUNC void
addTranslitTest(TestNode** root);
U_CDECL_BEGIN
extern const UChar NEW_LINE[];
extern const char C_NEW_LINE[];

View File

@ -147,6 +147,9 @@
<File
RelativePath="strtst.c">
</File>
<File
RelativePath="trnstst.c">
</File>
</Filter>
<Filter
Name="Header Files"

View File

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 2004-2004, International Business Machines
* Copyright (C) 2004-2005, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* file name: strtst.c

View File

@ -0,0 +1,316 @@
/*
**********************************************************************
* Copyright (C) 2005-2005, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* file name: strtst.c
* created by: George Rhoten
*/
#include "iotest.h"
#include "unicode/ustdio.h"
#include "unicode/utrans.h"
#include "unicode/ustring.h"
static void TestTranslitOps(void)
{
#if !UCONFIG_NO_TRANSLITERATION
UFILE *f;
UErrorCode err = U_ZERO_ERROR;
UTransliterator *a = NULL, *b = NULL, *c = NULL;
log_verbose("opening a transliterator and UFILE for testing\n");
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
if(f == NULL)
{
log_err("Couldn't open test file for writing\n");
return;
}
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Error opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(U_FAILURE(err))
{
log_err("Error setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != NULL)
{
log_err("Error, a transliterator was already set!\n");
}
b = u_fsettransliterator(NULL, U_WRITE, a, &err);
if(err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("Error setting transliterator on NULL file err=%s\n", u_errorName(err));
}
if(b != a)
{
log_err("Error getting the same transliterator was not returned on NULL file\n");
}
err = U_FILE_ACCESS_ERROR;
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(err != U_FILE_ACCESS_ERROR)
{
log_err("Error setting transliterator on error status err=%s\n", u_errorName(err));
}
if(b != a)
{
log_err("Error getting the same transliterator on error status\n");
}
err = U_ZERO_ERROR;
log_verbose("un-setting transliterator (setting to null)\n");
c = u_fsettransliterator(f, U_WRITE, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(c != a)
{
log_err("Err, transliterator that came back was not the original one.\n");
}
log_verbose("Trying to set read transliterator (should fail)\n");
b = u_fsettransliterator(f, U_READ, NULL, &err);
if(err != U_UNSUPPORTED_ERROR)
{
log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err));
u_fclose(f);
return;
}
else
{
log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err));
err = U_ZERO_ERROR;
}
utrans_close(c);
u_fclose(f);
#endif
}
static void TestTranslitFileOut(void)
{
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_TRANSLITERATION
UFILE *f;
UErrorCode err = U_ZERO_ERROR;
UTransliterator *a = NULL, *b = NULL, *c = NULL;
FILE *infile;
UChar compare[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x043C, 0x0000 };
UChar ubuf[256];
int len;
log_verbose("opening a transliterator and UFILE for testing\n");
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "utf-16");
if(f == NULL)
{
log_err("Couldn't open test file for writing\n");
return;
}
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != NULL)
{
log_err("Err, a transliterator was already set!\n");
}
u_fprintf(f, "Stephanos");
c = utrans_open("Latin-Cyrillic", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, c, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != a)
{
log_err("Error: a different transliterator was returned!\n");
}
utrans_close(b);
u_fprintf(f, "m");
u_fclose(f);
log_verbose("Re reading test file to verify transliteration\n");
infile = fopen(STANDARD_TEST_FILE, "rb");
if(infile == NULL)
{
log_err("Couldn't reopen test file\n");
return;
}
len=fread(ubuf, sizeof(UChar), u_strlen(compare), infile);
log_verbose("Read %d UChars\n", len);
if(len != u_strlen(compare))
{
log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare), len);
}
ubuf[len]=0;
if(u_strlen(compare) != u_strlen(ubuf))
{
log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), len);
}
if(u_strcmp(compare, ubuf))
{
log_err("Read string doesn't match expected.\n");
}
else
{
log_verbose("Read string matches expected.\n");
}
fclose(infile);
#endif
#endif
}
static void TestTranslitStringOut(void)
{
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_TRANSLITERATION
UFILE *f;
UErrorCode err = U_ZERO_ERROR;
UTransliterator *a = NULL, *b = NULL, *c = NULL;
UChar compare[] = { 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x043C, 0x0000 };
UChar ubuf[256];
log_verbose("opening a transliterator and UFILE for testing\n");
f = u_fstropen(ubuf, sizeof(ubuf)/sizeof(ubuf[0]), "en_US_POSIX");
if(f == NULL)
{
log_err("Couldn't open test file for writing\n");
return;
}
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, a, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != NULL)
{
log_err("Err, a transliterator was already set!\n");
}
u_fprintf(f, "Stephanos");
c = utrans_open("Latin-Cyrillic", UTRANS_FORWARD, NULL, -1, NULL, &err);
if(U_FAILURE(err))
{
log_err("Err opening transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
log_verbose("setting a transliterator\n");
b = u_fsettransliterator(f, U_WRITE, c, &err);
if(U_FAILURE(err))
{
log_err("Err setting transliterator %s\n", u_errorName(err));
u_fclose(f);
return;
}
if(b != a)
{
log_err("Error: a different transliterator was returned!\n");
}
utrans_close(b);
u_fprintf(f, "m");
u_fclose(f);
if(u_strlen(compare) != u_strlen(ubuf))
{
log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), u_strlen(ubuf));
}
if(u_strcmp(compare, ubuf))
{
log_err("Read string doesn't match expected.\n");
}
else
{
log_verbose("Read string matches expected.\n");
}
#endif
#endif
}
U_CFUNC void
addTranslitTest(TestNode** root) {
#if !UCONFIG_NO_TRANSLITERATION
addTest(root, &TestTranslitOps, "translit/ops");
#if !UCONFIG_NO_FORMATTING
addTest(root, &TestTranslitFileOut, "translit/fileOut");
addTest(root, &TestTranslitStringOut, "translit/stringOut");
#endif
#endif
}