From 51d2f74bc1d176c2e1d5e63b04b587c27ed1e3e4 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Thu, 23 Jun 2005 07:10:52 +0000 Subject: [PATCH] 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 --- icu4c/source/io/ufile.h | 11 +- icu4c/source/io/ustdio.c | 44 ++-- icu4c/source/test/iotest/Makefile.in | 2 +- icu4c/source/test/iotest/filetst.c | 189 +-------------- icu4c/source/test/iotest/iotest.cpp | 1 + icu4c/source/test/iotest/iotest.h | 5 +- icu4c/source/test/iotest/iotest.vcproj | 3 + icu4c/source/test/iotest/strtst.c | 2 +- icu4c/source/test/iotest/trnstst.c | 316 +++++++++++++++++++++++++ 9 files changed, 356 insertions(+), 217 deletions(-) create mode 100644 icu4c/source/test/iotest/trnstst.c diff --git a/icu4c/source/io/ufile.h b/icu4c/source/io/ufile.h index 8be6d7314a..4a26178950 100644 --- a/icu4c/source/io/ufile.h +++ b/icu4c/source/io/ufile.h @@ -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. diff --git a/icu4c/source/io/ustdio.c b/icu4c/source/io/ustdio.c index e125a85650..45782cebb9 100644 --- a/icu4c/source/io/ustdio.c +++ b/icu4c/source/io/ustdio.c @@ -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); } diff --git a/icu4c/source/test/iotest/Makefile.in b/icu4c/source/test/iotest/Makefile.in index c7484e33d4..3e22044f4d 100644 --- a/icu4c/source/test/iotest/Makefile.in +++ b/icu4c/source/test/iotest/Makefile.in @@ -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) diff --git a/icu4c/source/test/iotest/filetst.c b/icu4c/source/test/iotest/filetst.c index a078e8f3ae..92639a4058 100644 --- a/icu4c/source/test/iotest/filetst.c +++ b/icu4c/source/test/iotest/filetst.c @@ -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 @@ -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 } diff --git a/icu4c/source/test/iotest/iotest.cpp b/icu4c/source/test/iotest/iotest.cpp index d6addba5f0..fecea24cca 100644 --- a/icu4c/source/test/iotest/iotest.cpp +++ b/icu4c/source/test/iotest/iotest.cpp @@ -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"); diff --git a/icu4c/source/test/iotest/iotest.h b/icu4c/source/test/iotest/iotest.h index 9bda8e61bf..6e63d95817 100644 --- a/icu4c/source/test/iotest/iotest.h +++ b/icu4c/source/test/iotest/iotest.h @@ -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[]; diff --git a/icu4c/source/test/iotest/iotest.vcproj b/icu4c/source/test/iotest/iotest.vcproj index 116b23662a..682f9a3925 100644 --- a/icu4c/source/test/iotest/iotest.vcproj +++ b/icu4c/source/test/iotest/iotest.vcproj @@ -147,6 +147,9 @@ + +