X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..249c4c5ea9376c24572daf9c2effa7484a282f14:/icuSources/test/iotest/filetst.c diff --git a/icuSources/test/iotest/filetst.c b/icuSources/test/iotest/filetst.c index a078e8f3..12646e6e 100644 --- a/icuSources/test/iotest/filetst.c +++ b/icuSources/test/iotest/filetst.c @@ -1,26 +1,31 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* -********************************************************************** -* Copyright (C) 2004-2004, International Business Machines -* Corporation and others. All Rights Reserved. -********************************************************************** -* file name: filetst.c -* encoding: US-ASCII -* tab size: 8 (not used) -* indentation:4 -* -* created on: 2004apr06 -* created by: George Rhoten -*/ - + ********************************************************************** + * Copyright (C) 2004-2016, International Business Machines + * Corporation and others. All Rights Reserved. + ********************************************************************** + * file name: filetst.c + * encoding: UTF-8 + * tab size: 8 (not used) + * indentation:4 + * + * created on: 2004apr06 + * created by: George Rhoten + */ + +#include "cmemory.h" #include "iotest.h" #include "unicode/ustdio.h" #include "unicode/ustring.h" #include "unicode/uloc.h" -#include "unicode/utrans.h" #include +#include + +const char *STANDARD_TEST_FILE = "iotest-c.txt"; -const char STANDARD_TEST_FILE[] = "iotest-c.txt"; +const char *STANDARD_TEST_LOCALE = "en_US_POSIX"; #if !UCONFIG_NO_FORMATTING @@ -37,10 +42,10 @@ static void TestFileFromICU(UFILE *myFile) { U_STRING_DECL(myStringOrig, "My-String", 9); U_STRING_INIT(myStringOrig, "My-String", 9); - u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString)); - u_memset(uStringBuf, 0x2a, sizeof(uStringBuf)/sizeof(*uStringBuf)); - memset(myString, '*', sizeof(myString)/sizeof(*myString)); - memset(testBuf, '*', sizeof(testBuf)/sizeof(*testBuf)); + u_memset(myUString, 0x2a, UPRV_LENGTHOF(myUString)); + u_memset(uStringBuf, 0x2a, UPRV_LENGTHOF(uStringBuf)); + memset(myString, '*', UPRV_LENGTHOF(myString)); + memset(testBuf, '*', UPRV_LENGTHOF(testBuf)); if (myFile == NULL) { log_err("Can't write test file.\n"); @@ -54,8 +59,11 @@ static void TestFileFromICU(UFILE *myFile) { origPtr = (void *) INT64_C(0x1000200030004000); } else if (sizeof(void *) == 16) { /* iSeries */ - int32_t massiveBigEndianPtr[] = { 0x10002000, 0x30004000, 0x50006000, 0x70008000 }; - origPtr = *((void **)massiveBigEndianPtr); + union { + int32_t arr[4]; + void *ptr; + } massiveBigEndianPtr = {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }}; + origPtr = massiveBigEndianPtr.ptr; } else { log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*)); } @@ -91,11 +99,11 @@ static void TestFileFromICU(UFILE *myFile) { u_fprintf(myFile, "Pointer to integer Value: %d\n", *n); u_fprintf(myFile, "This is a long test123456789012345678901234567890123456789012345678901234567890\n"); *n = 1; - fprintf(u_fgetfile(myFile), "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n, (int*)n, (int)*n); - fprintf(u_fgetfile(myFile), "\tNormal fprintf count value: n=%d\n", (int)*n); + u_fprintf(myFile, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n, (int*)n, (int)*n); + fprintf(u_fgetfile(myFile), "\tNormal fprintf count value: n=%d\n", (int)*n); /* Should be 27 as stated later on. */ u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL); if (myFile == NULL) { log_err("Can't read test file."); @@ -164,27 +172,27 @@ static void TestFileFromICU(UFILE *myFile) { log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n); } *newDoubleValuePtr = -1.0; - u_fscanf(myFile, "Float %%f: %f\n", newDoubleValuePtr); + u_fscanf(myFile, "Float %%f: %lf\n", newDoubleValuePtr); if (myFloat != *newDoubleValuePtr) { log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat); } *newDoubleValuePtr = -1.0; - u_fscanf(myFile, "Lowercase float %%e: %e\n", newDoubleValuePtr); + u_fscanf(myFile, "Lowercase float %%e: %le\n", newDoubleValuePtr); if (myFloat != *newDoubleValuePtr) { log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat); } *newDoubleValuePtr = -1.0; - u_fscanf(myFile, "Uppercase float %%E: %E\n", newDoubleValuePtr); + u_fscanf(myFile, "Uppercase float %%E: %lE\n", newDoubleValuePtr); if (myFloat != *newDoubleValuePtr) { log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat); } *newDoubleValuePtr = -1.0; - u_fscanf(myFile, "Lowercase float %%g: %g\n", newDoubleValuePtr); + u_fscanf(myFile, "Lowercase float %%g: %lg\n", newDoubleValuePtr); if (myFloat != *newDoubleValuePtr) { log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat); } *newDoubleValuePtr = -1.0; - u_fscanf(myFile, "Uppercase float %%G: %G\n", newDoubleValuePtr); + u_fscanf(myFile, "Uppercase float %%G: %lG\n", newDoubleValuePtr); if (myFloat != *newDoubleValuePtr) { log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat); } @@ -210,12 +218,12 @@ static void TestFileFromICU(UFILE *myFile) { log_err("%%s Got: %s, Expected: My String\n", myString); } u_fscanf(myFile, "Unicode String %%S: %S\n", myUString); - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); if (strcmp(myString, "My-String")) { log_err("%%S Got: %S, Expected: My String\n", myUString); } u_fscanf(myFile, "NULL Unicode String %%S: %S\n", myUString); - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); if (strcmp(myString, "(null)")) { log_err("%%S Got: %S, Expected: My String\n", myUString); } @@ -231,32 +239,34 @@ static void TestFileFromICU(UFILE *myFile) { } u_fgets(myUString, 4, myFile); - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "\t\n") != 0) { + myString[2] = '!'; + myString[3] = '!'; + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "\t\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } - if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) { + if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) { log_err("u_fgets did not return myUString\n"); } - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) { + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } - if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) { + if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) { log_err("u_fgets did not return myUString\n"); } - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "Pointer to integer Value: 37\n") != 0) { + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "Pointer to integer Value: 37\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } - if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) { + if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) { log_err("u_fgets did not return myUString\n"); } - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) { + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } @@ -267,21 +277,21 @@ static void TestFileFromICU(UFILE *myFile) { if (u_fgets(myUString, 1, myFile) != myUString) { log_err("u_fgets did not return myUString\n"); } - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "") != 0) { + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "") != 0) { log_err("u_fgets got \"%s\"\n", myString); } if (u_fgets(myUString, 2, myFile) != myUString) { log_err("u_fgets did not return myUString\n"); } - u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString)); - if (myString == NULL || strcmp(myString, "\t") != 0) { + u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString)); + if (strcmp(myString, "\t") != 0) { log_err("u_fgets got \"%s\"\n", myString); } - u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile), - sizeof(myUString)/sizeof(*myUString)); + u_austrncpy(myString, u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile), + UPRV_LENGTHOF(myUString)); if (strcmp(myString, "Normal fprintf count: n=1 n=1\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } @@ -289,8 +299,8 @@ static void TestFileFromICU(UFILE *myFile) { if (u_feof(myFile)) { log_err("Got feof while reading the file and not at the end of the file.\n"); } - u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile), - sizeof(myUString)/sizeof(*myUString)); + u_austrncpy(myString, u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile), + UPRV_LENGTHOF(myUString)); if (strcmp(myString, "\tNormal fprintf count value: n=27\n") != 0) { log_err("u_fgets got \"%s\"\n", myString); } @@ -305,18 +315,26 @@ static void TestFileFromICU(UFILE *myFile) { } static void TestFile(void) { -/* FILE *standardFile;*/ log_verbose("Testing u_fopen\n"); - TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL)); + TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL)); +} + +static void TestFinit(void) { + FILE *standardFile; -/* Don't know how to make this work without stdout or stderr */ -/* log_verbose("Testing u_finit\n"); - standardFile = fopen(STANDARD_TEST_FILE, "wb"); - TestFileFromICU(u_finit(standardFile, NULL, NULL)); + standardFile = fopen(STANDARD_TEST_FILE, "w"); + TestFileFromICU(u_finit(standardFile, STANDARD_TEST_LOCALE, NULL)); fclose(standardFile); -*/ +} + +static void TestFadopt(void) { + FILE *standardFile; + + log_verbose("Testing u_fadopt\n"); + standardFile = fopen(STANDARD_TEST_FILE, "w"); + TestFileFromICU(u_fadopt(standardFile, STANDARD_TEST_LOCALE, NULL)); } #endif @@ -333,7 +351,7 @@ static void StdinBuffering(void) { buff[0] = 0x40; buff[1] = 0; - u_fgets(buff, sizeof(buff)/sizeof(buff[0]), uStdIn); + u_fgets(buff, UPRV_LENGTHOF(buff), uStdIn); u_fprintf(uStdOut, "%S\n", buff); u_fscanf(uStdIn, "%d", &num); u_fprintf(uStdOut, "%d\n", num); @@ -434,9 +452,15 @@ static void TestfgetsBuffers(void) { } u_fputc(0x3BC, myFile); + if (u_fputc(0x110000, myFile) != U_EOF) { + log_err("u_fputc should return U_EOF for 0x110000.\n"); + } + if (u_fputc((UChar32)0xFFFFFFFFu, myFile) != U_EOF) { + log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n"); + } u_fputc(0xFF41, myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); - u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer)); u_uastrncpy(buffer, testStr, expectedSize+1); for (repetitions = 0; repetitions < 16; repetitions++) { u_file_write(buffer, expectedSize, myFile); @@ -444,7 +468,7 @@ static void TestfgetsBuffers(void) { } u_fclose(myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16"); if (u_fgetc(myFile) != 0x3BC) { log_err("The first character is wrong\n"); @@ -452,7 +476,7 @@ static void TestfgetsBuffers(void) { if (u_fgetc(myFile) != 0xFF41) { log_err("The second character is wrong\n"); } - if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) { + if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != buffer) { log_err("Didn't get the buffer back\n"); return; } @@ -477,8 +501,8 @@ static void TestfgetsBuffers(void) { u_fputc(0x3BC, myFile); u_fputc(0xFF41, myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); - u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer)); u_uastrncpy(buffer, testStr, expectedSize+1); for (repetitions = 0; repetitions < 16; repetitions++) { u_file_write(buffer, expectedSize, myFile); @@ -486,7 +510,7 @@ static void TestfgetsBuffers(void) { } u_fclose(myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8"); if (strcmp(u_fgetcodepage(myFile), "UTF-8") != 0) { log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile)); @@ -497,7 +521,7 @@ static void TestfgetsBuffers(void) { if (u_fgetc(myFile) != 0xFF41) { log_err("The second character is wrong\n"); } - if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) { + if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != buffer) { log_err("Didn't get the buffer back\n"); return; } @@ -519,8 +543,8 @@ static void TestfgetsBuffers(void) { myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8"); u_fputc(0xFF41, myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); - u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer)); u_uastrncpy(buffer, testStr, expectedSize+1); for (repetitions = 0; repetitions < 1; repetitions++) { u_file_write(buffer, expectedSize, myFile); @@ -528,7 +552,7 @@ static void TestfgetsBuffers(void) { } u_fclose(myFile); - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8"); if (u_fgets(buffer, 2, myFile) != buffer) { log_err("Didn't get the buffer back\n"); @@ -554,13 +578,13 @@ static void TestFileReadBuffering(void) { int32_t how_many; int32_t repetitions; - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); for (repetitions = 0; repetitions < 2; repetitions++) { - u_file_write(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile); + u_file_write(buffer, UPRV_LENGTHOF(buffer), myFile); } u_fclose(myFile); - u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0])); + u_memset(buffer, 0xDEAD, UPRV_LENGTHOF(buffer)); myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16"); how_many = u_file_read(buffer, 1024, myFile); if (how_many != 1024 || buffer[1023] != 0xBEEF) { @@ -584,7 +608,7 @@ static void TestfgetsLineCount(void) { log_err("Can't write test file.\n"); return; } - u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0])); + u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer)); for (repetitions = 0; repetitions < 16; repetitions++) { fwrite(testStr, sizeof(testStr[0]), expectedSize, stdFile); @@ -601,9 +625,9 @@ static void TestfgetsLineCount(void) { char *returnedCharBuffer; UChar *returnedUCharBuffer; - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); - returnedCharBuffer = fgets(charBuffer, sizeof(charBuffer)/sizeof(charBuffer[0]), stdFile); - returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + returnedCharBuffer = fgets(charBuffer, UPRV_LENGTHOF(charBuffer), stdFile); + returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile); if (!returnedCharBuffer && !returnedUCharBuffer) { /* Both returned NULL. stop. */ @@ -671,18 +695,18 @@ static void TestfgetsNewLineHandling(void) { log_err("Can't write test file.\n"); return; } - for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) { + for (lineIdx = 0; lineIdx < UPRV_LENGTHOF(testUStr); lineIdx++) { u_file_write(testUStr[lineIdx], u_strlen(testUStr[lineIdx]), myFile); } u_fclose(myFile); myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, "UTF-8"); - for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) { + for (lineIdx = 0; lineIdx < UPRV_LENGTHOF(testUStr); lineIdx++) { UChar *returnedUCharBuffer; - u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0])); - returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile); + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile); if (!returnedUCharBuffer) { /* Returned NULL. stop. */ @@ -695,21 +719,126 @@ static void TestfgetsNewLineHandling(void) { log_err("u_fgets wrote too much\n"); } } - if (lineIdx != (int32_t)(sizeof(testUStr)/sizeof(testUStr[0]))) { + if (lineIdx != UPRV_LENGTHOF(testUStr)) { log_err("u_fgets read too much\n"); } - if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != NULL) { + if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != NULL) { log_err("u_file_write wrote too much\n"); } u_fclose(myFile); } +static void TestLineCount(const char *prefixLine, const char *line, int32_t numRepititions) { + UChar buffer[64]; + UChar expectedBuffer[64]; + int32_t lineLen = strlen(line); + UChar *returnedUCharBuffer; + int32_t repetitions; + UFILE *myFile = NULL; + FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb"); + + if (stdFile == NULL) { + log_err("Can't write test file.\n"); + return; + } + /* Write a prefix line and then write a bunch of lines */ + fwrite(prefixLine, strlen(prefixLine), 1, stdFile); + for (repetitions = 0; repetitions < numRepititions; repetitions++) { + fwrite(line, lineLen, 1, stdFile); + } + fclose(stdFile); + + myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL); + if (myFile == NULL) { + log_err("Can't read test file.\n"); + return; + } + + /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */ + u_uastrncpy(expectedBuffer, prefixLine, (int32_t)strlen(prefixLine)+1); + returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile); + if (u_strcmp(returnedUCharBuffer, expectedBuffer) != 0) { + log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine); + return; + } + + u_uastrncpy(expectedBuffer, line, (int32_t)strlen(line)+1); + for (repetitions = 0; ; repetitions++) { + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile); + + if (!returnedUCharBuffer) { + /* returned NULL. stop. */ + break; + } + if (u_strcmp(buffer, expectedBuffer) != 0) { + log_err("buffers are different at count %d\n", repetitions); + } + if (buffer[u_strlen(buffer)+1] != 0xBEEF) { + log_err("u_fgets wrote too much\n"); + } + } + if (repetitions != numRepititions) { + log_err("got wrong number of lines. got=%d expected=%d\n", repetitions, numRepititions); + } + u_fclose(myFile); +} + +static void TestfgetsNewLineCount(void) { + /* This makes sure that lines are correctly handled between buffer boundaries. */ + TestLineCount("\n", "\n", 1024); /* Unix newlines */ + TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */ + TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */ + TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */ + TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */ + TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */ +} + +static void TestFgetsLineBuffering(void) { + UChar buffer[2003]; /* Use a non-power of 2 or 10 */ + UChar *returnedUCharBuffer; + int32_t repetitions; + UFILE *myFile = NULL; + FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb"); + + if (stdFile == NULL) { + log_err("Can't write test file.\n"); + return; + } + u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer)); + + /* Write one very long line */ + for (repetitions = 0; repetitions < (UPRV_LENGTHOF(buffer)*2); repetitions++) { + fwrite(repetitions ? "1" : "2", 1, 1, stdFile); + } + fclose(stdFile); + + myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL); + if (myFile == NULL) { + log_err("Can't read test file.\n"); + return; + } + + /* Read part of one very long line */ + returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer)-1, myFile); + if (u_strlen(returnedUCharBuffer) != (UPRV_LENGTHOF(buffer)-2)) { + log_err("Line is wrong length. Got %d. Expected %d.\n", + u_strlen(returnedUCharBuffer), (UPRV_LENGTHOF(buffer)-2)); + } + /* We better not read too much */ + if (buffer[UPRV_LENGTHOF(buffer)-1] != 0xBEEF) { + log_err("Too much data was written\n"); + } + + u_fclose(myFile); +} + static void TestCodepage(void) { UFILE *myFile = NULL; static const UChar strABAccentA[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0}; static const UChar strBadConversion[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0}; - UChar testBuf[sizeof(strABAccentA)/sizeof(strABAccentA[0])*2]; /* *2 to see if too much was */ + UChar testBuf[UPRV_LENGTHOF(strABAccentA)*2]; /* *2 to see if too much was */ char convName[UCNV_MAX_CONVERTER_NAME_LENGTH]; int32_t retVal; UErrorCode status = U_ZERO_ERROR; @@ -789,22 +918,86 @@ static void TestCodepage(void) { } +static void TestCodepageFlush(void) { +#if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING + log_verbose("Skipping, legacy conversion or formatting is disabled."); +#else + UChar utf16String[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 }; + uint8_t inBuf[200]; + size_t inLen =0; + const char *enc = "IBM-1388"; /* GBK EBCDIC stateful */ + UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", STANDARD_TEST_LOCALE, enc); + FILE *myCFile; + int shift = 0; + int32_t i; + + if (myFile == NULL) { + log_err("Can't write test file %s\n", STANDARD_TEST_FILE); + return; + } + + u_fprintf(myFile, "%S", utf16String); + u_fclose(myFile); + + /* now read it back */ + myCFile = fopen(STANDARD_TEST_FILE, "rb"); + if (myCFile == NULL) { + log_err("Can't read test file."); + return; + } + + inLen = fread(inBuf, 1, 200, myCFile); + fclose(myCFile); + + if(inLen<=0) { + log_err("Failed during read of test file."); + return; + } + + /* check if shift in and out */ + for(i=0;i<(int32_t)inLen;i++) { + if(inBuf[i]==0x0E) { /* SO */ + shift= 1; + } else if(inBuf[i]==0x0F) { /* SI */ + shift= -1; + } + } + + if(shift==0) { + log_err("Err: shift was unchanged\n"); + } else if(shift==1) { + log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n"); + } else if(shift==-1) { + log_verbose("OK: Shifted in (SI, 0x0F)\n"); + } + + if(inLen != 12) { + log_err("Expected 12 bytes, read %d\n", inLen); + } else { + log_verbose("OK: read %d bytes\n", inLen); + } + + +#endif +} + #if !UCONFIG_NO_FORMATTING static void TestFilePrintCompatibility(void) { - UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL); + UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", STANDARD_TEST_LOCALE, NULL); FILE *myCFile; int32_t num; char cVal; static const UChar emptyStr[] = {0}; char readBuf[512] = ""; char testBuf[512] = ""; + int32_t n = 0; if (myFile == NULL) { log_err("Can't write test file.\n"); return; } #if !UCONFIG_NO_FORMATTING - if (strcmp(u_fgetlocale(myFile), "en_US_POSIX") != 0) { + if (strcmp(u_fgetlocale(myFile), STANDARD_TEST_LOCALE) != 0) { log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile)); } #endif @@ -838,38 +1031,39 @@ static void TestFilePrintCompatibility(void) { } for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) { - fscanf(myCFile, "%s", readBuf); + /* Note: gcc on Ubuntu complains if return value of scanf is ignored. */ + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%x", (int)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%X", (int)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%o", (int)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } /* fprintf is not compatible on all platforms e.g. the iSeries */ - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%d", (int)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%i", (int)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%f", (double)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); @@ -887,13 +1081,13 @@ static void TestFilePrintCompatibility(void) { log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); }*/ - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%g", (double)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); } - fscanf(myCFile, "%s", readBuf); + n += fscanf(myCFile, "%s", readBuf); sprintf(testBuf, "%G", (double)num); if (strcmp(readBuf, testBuf) != 0) { log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf); @@ -902,13 +1096,13 @@ static void TestFilePrintCompatibility(void) { /* Properly eat the newlines */ for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) { - fscanf(myCFile, "%c", &cVal); + n += fscanf(myCFile, "%c", &cVal); if (cVal != C_NEW_LINE[num]) { log_err("OS newline error\n"); } } for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) { - fscanf(myCFile, "%c", &cVal); + n += fscanf(myCFile, "%c", &cVal); if (cVal != C_NEW_LINE[num]) { log_err("ustdio newline error\n"); } @@ -916,31 +1110,32 @@ static void TestFilePrintCompatibility(void) { for (num = 0; num < 0x80; num++) { cVal = -1; - fscanf(myCFile, "%c", &cVal); + n += fscanf(myCFile, "%c", &cVal); if (num != cVal) { log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num); } } + (void)n; fclose(myCFile); } #endif #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \ - myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\ + myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);\ if (myFile == NULL) {\ log_err("Can't write test file for %s.\n", uFormat);\ return;\ }\ /* Reinitialize the buffer to verify null termination works. */\ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\ - memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\ + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\ + memset(buffer, '*', UPRV_LENGTHOF(buffer));\ \ uNumPrinted = u_fprintf(myFile, uFormat, uValue);\ u_fclose(myFile);\ - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\ - u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\ + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);\ + u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);\ u_fclose(myFile);\ - u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\ + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\ cNumPrinted = sprintf(buffer, cFormat, cValue);\ if (strcmp(buffer, compBuffer) != 0) {\ log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\ @@ -1068,17 +1263,17 @@ static void TestFprintfFormat(void) { TestFPrintFormat("%3f", 1.234, "%3f", 1.234); TestFPrintFormat("%3f", -1.234, "%3f", -1.234); - myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL); /* Reinitialize the buffer to verify null termination works. */ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer)); - memset(buffer, '*', sizeof(buffer)/sizeof(*buffer)); + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer)); + memset(buffer, '*', UPRV_LENGTHOF(buffer)); uNumPrinted = u_fprintf(myFile, "%d % d %d", -1234, 1234, 1234); u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL); - u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL); + u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile); u_fclose(myFile); - u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer)); + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer)); cNumPrinted = sprintf(buffer, "%d % d %d", -1234, 1234, 1234); if (strcmp(buffer, compBuffer) != 0) { log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer); @@ -1109,20 +1304,20 @@ static void TestFScanSetFormat(const char *format, const UChar *uValue, const ch return; } /* Reinitialize the buffer to verify null termination works. */ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer)); - uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0; - memset(buffer, '*', sizeof(buffer)/sizeof(*buffer)); - buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0; + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer)); + uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; + memset(buffer, '*', UPRV_LENGTHOF(buffer)); + buffer[UPRV_LENGTHOF(buffer)-1] = 0; u_fprintf(myFile, "%S", uValue); u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL); uNumScanned = u_fscanf(myFile, format, uBuffer); u_fclose(myFile); if (expectedToPass) { - u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer)); + u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer)); cNumScanned = sscanf(cValue, format, buffer); - if (strncmp(buffer, compBuffer, sizeof(buffer)/sizeof(*buffer)) != 0) { + if (strncmp(buffer, compBuffer, UPRV_LENGTHOF(buffer)) != 0) { log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer); } if (cNumScanned != uNumScanned) { @@ -1196,12 +1391,12 @@ static void TestBadFScanfFormat(const char *format, const UChar *uValue, const c return; } /* Reinitialize the buffer to verify null termination works. */ - u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer)); - uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0; + u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer)); + uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; u_fprintf(myFile, "%S", uValue); u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL); uNumScanned = u_fscanf(myFile, format, uBuffer); u_fclose(myFile); if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) { @@ -1225,7 +1420,7 @@ static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) int32_t count; UFILE *myFile; - myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "UTF-8"); + myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, "UTF-8"); if (!myFile) { log_err("Test file can't be opened\n"); return; @@ -1233,17 +1428,18 @@ static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) va_start(ap, format); count = u_vfprintf(myFile, format, ap); + (void)count; /* Suppress set but not used warning. */ va_end(ap); u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", "UTF-8"); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, "UTF-8"); if (!myFile) { log_err("Test file can't be opened\n"); return; } - u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile); + u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile); u_uastrcpy(uBuffer2, expectedResult); if (u_strcmp(uBuffer, uBuffer2) != 0) { log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult); @@ -1251,7 +1447,7 @@ static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL); if (!myFile) { log_err("Test file can't be opened\n"); return; @@ -1265,12 +1461,12 @@ static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) u_fclose(myFile); - myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL); + myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL); if (!myFile) { log_err("Test file can't be opened\n"); return; } - u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile); + u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile); u_uastrcpy(uBuffer2, expectedResult); if (u_strcmp(uBuffer, uBuffer2) != 0) { log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult); @@ -1282,197 +1478,133 @@ 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) +static void TestUnicodeFormat(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; - } + /* Make sure that invariant conversion doesn't happen on the _u formats. */ + UChar myUString[256]; + UFILE *myFile; + static const UChar TEST_STR[] = { 0x03BC, 0x0025, 0x0024, 0}; + static const UChar PERCENT_S[] = { 0x03BC, 0x0025, 0x0053, 0}; - 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; - } + u_memset(myUString, 0x2a, UPRV_LENGTHOF(myUString)); - 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); + myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8"); + if (!myFile) { + log_err("Test file can't be opened\n"); return; } + u_fprintf_u(myFile, PERCENT_S, TEST_STR); + u_fclose(myFile); - 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"); + myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8"); + if (!myFile) { + log_err("Test file can't be opened\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"); + u_fscanf_u(myFile, PERCENT_S, myUString); + u_fclose(myFile); + if (u_strcmp(TEST_STR, myUString) != 0) { + log_err("u_fscanf_u doesn't work.\n"); } - - fclose(infile); -#endif #endif } +static void TestFileWriteRetval(const char * a_pszEncoding) { + UChar * buffer; + UFILE * myFile; + int32_t count; + int32_t expected = 10000; /* test with large data to test internal buffer looping */ + UChar testChar = 0xBEEF; + + if (!*a_pszEncoding || 0 == strcmp(a_pszEncoding, "ASCII")) { + testChar = 0x65; /* 'A' - otherwise read test will fail */ + } + + buffer = (UChar*) malloc(expected * sizeof(UChar)); + if (!buffer) { + log_err("Out of memory\n"); + return; + } + + /* write */ + myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, a_pszEncoding); + if (!myFile) { + free(buffer); + log_err("Test file can't be opened for write\n"); + return; + } + u_memset(buffer, testChar, expected); + count = u_file_write(buffer, expected, myFile); + u_fclose(myFile); + if (count != expected) { + free(buffer); + log_err("u_file_write returned incorrect number of characters written\n"); + return; + } + + free(buffer); + buffer = NULL; + + /* read */ + myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, a_pszEncoding); + if (!myFile) { + log_err("Test file can't be opened for read\n"); + return; + } + for (count = 0; count < expected; ++count) { + UChar gotChar = u_fgetc(myFile); + if(gotChar != testChar) { + log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar, testChar); + u_fclose(myFile); + return; + } + } + if (u_fgetc(myFile) != U_EOF) { + log_err("u_fgetc did not return expected EOF\n"); + u_fclose(myFile); + return; + } + u_fclose(myFile); +} + +static void TestFileWriteRetvalUTF16(void) { + TestFileWriteRetval("UTF-16"); +} + +static void TestFileWriteRetvalUTF8(void) { + TestFileWriteRetval("UTF-8"); +} + +static void TestFileWriteRetvalASCII(void) { + TestFileWriteRetval("ASCII"); +} + +static void TestFileWriteRetvalNONE(void) { + TestFileWriteRetval(""); +} + U_CFUNC void addFileTest(TestNode** root) { #if !UCONFIG_NO_FORMATTING addTest(root, &TestFile, "file/TestFile"); + addTest(root, &TestFinit, "file/TestFinit"); + addTest(root, &TestFadopt, "file/TestFadopt"); #endif addTest(root, &StdinBuffering, "file/StdinBuffering"); addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers"); addTest(root, &TestFileReadBuffering, "file/TestFileReadBuffering"); addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount"); addTest(root, &TestfgetsNewLineHandling, "file/TestfgetsNewLineHandling"); + addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount"); + addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering"); addTest(root, &TestCodepage, "file/TestCodepage"); + addTest(root, &TestCodepageFlush, "file/TestCodepageFlush"); + addTest(root, &TestFileWriteRetvalUTF16, "file/TestFileWriteRetvalUTF16"); + addTest(root, &TestFileWriteRetvalUTF8, "file/TestFileWriteRetvalUTF8"); + addTest(root, &TestFileWriteRetvalASCII, "file/TestFileWriteRetvalASCII"); + addTest(root, &TestFileWriteRetvalNONE, "file/TestFileWriteRetvalNONE"); #if !UCONFIG_NO_FORMATTING addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale"); addTest(root, &TestFprintfFormat, "file/TestFprintfFormat"); @@ -1480,12 +1612,6 @@ addFileTest(TestNode** root) { addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility"); 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 + addTest(root, &TestUnicodeFormat, "file/TestUnicodeFormat"); #endif }