Home | History | Annotate | Download | only in iotest
      1 /*
      2  **********************************************************************
      3  *   Copyright (C) 2004-2010, International Business Machines
      4  *   Corporation and others.  All Rights Reserved.
      5  **********************************************************************
      6  *   file name:  filetst.c
      7  *   encoding:   US-ASCII
      8  *   tab size:   8 (not used)
      9  *   indentation:4
     10  *
     11  *   created on: 2004apr06
     12  *   created by: George Rhoten
     13  */
     14 
     15 #include "iotest.h"
     16 #include "unicode/ustdio.h"
     17 #include "unicode/ustring.h"
     18 #include "unicode/uloc.h"
     19 
     20 #include <string.h>
     21 #include <stdlib.h>
     22 
     23 const char *STANDARD_TEST_FILE = "iotest-c.txt";
     24 
     25 
     26 #if !UCONFIG_NO_FORMATTING
     27 static void TestFileFromICU(UFILE *myFile) {
     28     int32_t n[1];
     29     float myFloat = -1234.0;
     30     int32_t newValuePtr[1];
     31     double newDoubleValuePtr[1];
     32     UChar myUString[256];
     33     UChar uStringBuf[256];
     34     char myString[256] = "";
     35     char testBuf[256] = "";
     36     void *origPtr, *ptr;
     37     U_STRING_DECL(myStringOrig, "My-String", 9);
     38 
     39     U_STRING_INIT(myStringOrig, "My-String", 9);
     40     u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
     41     u_memset(uStringBuf, 0x2a, sizeof(uStringBuf)/sizeof(*uStringBuf));
     42     memset(myString, '*', sizeof(myString)/sizeof(*myString));
     43     memset(testBuf, '*', sizeof(testBuf)/sizeof(*testBuf));
     44 
     45     if (myFile == NULL) {
     46         log_err("Can't write test file.\n");
     47         return;
     48     }
     49 
     50     *n = -1234;
     51     if (sizeof(void *) == 4) {
     52         origPtr = (void *)0xdeadbeef;
     53     } else if (sizeof(void *) == 8) {
     54         origPtr = (void *) INT64_C(0x1000200030004000);
     55     } else if (sizeof(void *) == 16) {
     56         /* iSeries */
     57         union {
     58             int32_t arr[4];
     59             void *ptr;
     60         } massiveBigEndianPtr = {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
     61         origPtr = massiveBigEndianPtr.ptr;
     62     } else {
     63         log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
     64     }
     65 
     66     /* Test fprintf */
     67     u_fprintf(myFile, "Signed decimal integer %%d: %d\n", *n);
     68     u_fprintf(myFile, "Signed decimal integer %%i: %i\n", *n);
     69     u_fprintf(myFile, "Unsigned octal integer %%o: %o\n", *n);
     70     u_fprintf(myFile, "Unsigned decimal integer %%u: %u\n", *n);
     71     u_fprintf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n);
     72     u_fprintf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n);
     73     u_fprintf(myFile, "Float %%f: %f\n", myFloat);
     74     u_fprintf(myFile, "Lowercase float %%e: %e\n", myFloat);
     75     u_fprintf(myFile, "Uppercase float %%E: %E\n", myFloat);
     76     u_fprintf(myFile, "Lowercase float %%g: %g\n", myFloat);
     77     u_fprintf(myFile, "Uppercase float %%G: %G\n", myFloat);
     78     u_fprintf(myFile, "Pointer %%p: %p\n", origPtr);
     79     u_fprintf(myFile, "Char %%c: %c\n", 'A');
     80     u_fprintf(myFile, "UChar %%C: %C\n", (UChar)0x0041); /*'A'*/
     81     u_fprintf(myFile, "String %%s: %s\n", "My-String");
     82     u_fprintf(myFile, "NULL String %%s: %s\n", NULL);
     83     u_fprintf(myFile, "Unicode String %%S: %S\n", myStringOrig);
     84     u_fprintf(myFile, "NULL Unicode String %%S: %S\n", NULL);
     85     u_fprintf(myFile, "Percent %%P (non-ANSI): %P\n", myFloat);
     86     u_fprintf(myFile, "Spell Out %%V (non-ANSI): %V\n", myFloat);
     87 
     88     if (u_feof(myFile)) {
     89         log_err("Got feof while writing the file.\n");
     90     }
     91 
     92     *n = 1;
     93     u_fprintf(myFile, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n, n, *n);
     94     u_fprintf(myFile, "Pointer to integer Value: %d\n", *n);
     95     u_fprintf(myFile, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
     96     *n = 1;
     97     u_fprintf(myFile, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n, (int*)n, (int)*n);
     98     fprintf(u_fgetfile(myFile), "\tNormal fprintf count value: n=%d\n", (int)*n); /* Should be 27 as stated later on. */
     99 
    100     u_fclose(myFile);
    101     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
    102 
    103     if (myFile == NULL) {
    104         log_err("Can't read test file.");
    105         return;
    106     }
    107 
    108     if (u_feof(myFile)) {
    109         log_err("Got feof while reading the file and not at the end of the file.\n");
    110     }
    111 
    112     myUString[0] = u_fgetc(myFile);
    113     if (myUString[0] != 0x53 /* S */) {
    114         log_err("u_fgetc 1 returned %X. Expected 'S'.", myString[0]);
    115     }
    116     u_fungetc(myUString[0], myFile);
    117     myUString[0] = u_fgetc(myFile);
    118     if (myUString[0] != 0x53 /* S */) {
    119         log_err("u_fgetc 2 returned %X. Expected 'S'.", myString[0]);
    120     }
    121     u_fungetc(myUString[0], myFile);
    122     myUString[0] = u_fgetc(myFile);
    123     if (myUString[0] != 0x53 /* S */) {
    124         log_err("u_fgetc 3 returned %X. Expected 'S'.", myString[0]);
    125     }
    126     u_fungetc(myUString[0], myFile);
    127     myUString[0] = u_fgetc(myFile);
    128     myUString[1] = (UChar)u_fgetcx(myFile); /* Mix getc and getcx and see what happens. */
    129     myUString[2] = u_fgetc(myFile);
    130     if (myUString[0] != 0x53 /* S */ && myUString[1] != 0x69 /* i */ && myUString[2] != 0x6E /* n */) {
    131         log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString[0], myString[1], myString[2]);
    132     }
    133     u_fungetc(myUString[2], myFile);
    134     u_fungetc(myUString[1], myFile);
    135     u_fungetc(myUString[0], myFile);
    136 
    137     *n = -1234;
    138 
    139     *newValuePtr = 1;
    140     u_fscanf(myFile, "Signed decimal integer %%d: %d\n", newValuePtr);
    141     if (*n != *newValuePtr) {
    142         log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
    143     }
    144     *newValuePtr = 1;
    145     u_fscanf(myFile, "Signed decimal integer %%i: %i\n", newValuePtr);
    146     if (*n != *newValuePtr) {
    147         log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
    148     }
    149     *newValuePtr = 1;
    150     u_fscanf(myFile, "Unsigned octal integer %%o: %o\n", newValuePtr);
    151     if (*n != *newValuePtr) {
    152         log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
    153     }
    154     *newValuePtr = 1;
    155     u_fscanf(myFile, "Unsigned decimal integer %%u: %u\n", newValuePtr);
    156     if (*n != *newValuePtr) {
    157         log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
    158     }
    159     *newValuePtr = 1;
    160     u_fscanf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr);
    161     if (*n != *newValuePtr) {
    162         log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
    163     }
    164     *newValuePtr = 1;
    165     u_fscanf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr);
    166     if (*n != *newValuePtr) {
    167         log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
    168     }
    169     *newDoubleValuePtr = -1.0;
    170     u_fscanf(myFile, "Float %%f: %lf\n", newDoubleValuePtr);
    171     if (myFloat != *newDoubleValuePtr) {
    172         log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
    173     }
    174     *newDoubleValuePtr = -1.0;
    175     u_fscanf(myFile, "Lowercase float %%e: %le\n", newDoubleValuePtr);
    176     if (myFloat != *newDoubleValuePtr) {
    177         log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
    178     }
    179     *newDoubleValuePtr = -1.0;
    180     u_fscanf(myFile, "Uppercase float %%E: %lE\n", newDoubleValuePtr);
    181     if (myFloat != *newDoubleValuePtr) {
    182         log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
    183     }
    184     *newDoubleValuePtr = -1.0;
    185     u_fscanf(myFile, "Lowercase float %%g: %lg\n", newDoubleValuePtr);
    186     if (myFloat != *newDoubleValuePtr) {
    187         log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
    188     }
    189     *newDoubleValuePtr = -1.0;
    190     u_fscanf(myFile, "Uppercase float %%G: %lG\n", newDoubleValuePtr);
    191     if (myFloat != *newDoubleValuePtr) {
    192         log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
    193     }
    194     ptr = NULL;
    195     u_fscanf(myFile, "Pointer %%p: %p\n", &ptr);
    196     if (ptr != origPtr) {
    197         log_err("%%p Got: %p, Expected: %p\n", ptr, origPtr);
    198     }
    199     u_fscanf(myFile, "Char %%c: %c\n", myString);
    200     if (*myString != 'A') {
    201         log_err("%%c Got: %c, Expected: A\n", *myString);
    202     }
    203     u_fscanf(myFile, "UChar %%C: %C\n", myUString);
    204     if (*myUString != (UChar)0x0041) { /*'A'*/
    205         log_err("%%C Got: %C, Expected: A\n", *myUString);
    206     }
    207     u_fscanf(myFile, "String %%s: %s\n", myString);
    208     if (strcmp(myString, "My-String")) {
    209         log_err("%%s Got: %s, Expected: My String\n", myString);
    210     }
    211     u_fscanf(myFile, "NULL String %%s: %s\n", myString);
    212     if (strcmp(myString, "(null)")) {
    213         log_err("%%s Got: %s, Expected: My String\n", myString);
    214     }
    215     u_fscanf(myFile, "Unicode String %%S: %S\n", myUString);
    216     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    217     if (strcmp(myString, "My-String")) {
    218         log_err("%%S Got: %S, Expected: My String\n", myUString);
    219     }
    220     u_fscanf(myFile, "NULL Unicode String %%S: %S\n", myUString);
    221     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    222     if (strcmp(myString, "(null)")) {
    223         log_err("%%S Got: %S, Expected: My String\n", myUString);
    224     }
    225     *newDoubleValuePtr = -1.0;
    226     u_fscanf(myFile, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr);
    227     if (myFloat != *newDoubleValuePtr) {
    228         log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
    229     }
    230     *newDoubleValuePtr = -1.0;
    231     u_fscanf(myFile, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr);
    232     if (myFloat != *newDoubleValuePtr) {
    233         log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
    234     }
    235 
    236     u_fgets(myUString, 4, myFile);
    237     myString[2] = '!';
    238     myString[3] = '!';
    239     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    240     if (myString == NULL || strcmp(myString, "\t\n") != 0) {
    241         log_err("u_fgets got \"%s\"\n", myString);
    242     }
    243 
    244     if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
    245         log_err("u_fgets did not return myUString\n");
    246     }
    247     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    248     if (myString == NULL || strcmp(myString, "Pointer to integer (Count) %n: n=1  n=1\n") != 0) {
    249         log_err("u_fgets got \"%s\"\n", myString);
    250     }
    251 
    252     if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
    253         log_err("u_fgets did not return myUString\n");
    254     }
    255     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    256     if (myString == NULL || strcmp(myString, "Pointer to integer Value: 37\n") != 0) {
    257         log_err("u_fgets got \"%s\"\n", myString);
    258     }
    259 
    260     if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
    261         log_err("u_fgets did not return myUString\n");
    262     }
    263     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    264     if (myString == NULL || strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
    265         log_err("u_fgets got \"%s\"\n", myString);
    266     }
    267 
    268     if (u_fgets(myUString, 0, myFile) != NULL) {
    269         log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString);
    270     }
    271 
    272     if (u_fgets(myUString, 1, myFile) != myUString) {
    273         log_err("u_fgets did not return myUString\n");
    274     }
    275     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    276     if (myString == NULL || strcmp(myString, "") != 0) {
    277         log_err("u_fgets got \"%s\"\n", myString);
    278     }
    279 
    280     if (u_fgets(myUString, 2, myFile) != myUString) {
    281         log_err("u_fgets did not return myUString\n");
    282     }
    283     u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
    284     if (myString == NULL || strcmp(myString, "\t") != 0) {
    285         log_err("u_fgets got \"%s\"\n", myString);
    286     }
    287 
    288     u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile),
    289         sizeof(myUString)/sizeof(*myUString));
    290     if (strcmp(myString, "Normal fprintf count: n=1  n=1\n") != 0) {
    291         log_err("u_fgets got \"%s\"\n", myString);
    292     }
    293 
    294     if (u_feof(myFile)) {
    295         log_err("Got feof while reading the file and not at the end of the file.\n");
    296     }
    297     u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile),
    298         sizeof(myUString)/sizeof(*myUString));
    299     if (strcmp(myString, "\tNormal fprintf count value: n=27\n") != 0) {
    300         log_err("u_fgets got \"%s\"\n", myString);
    301     }
    302     if (!u_feof(myFile)) {
    303         log_err("Did not get feof while reading the end of the file.\n");
    304     }
    305     if (u_fscanf(myFile, "%S\n", myUString) != 0) {
    306         log_err("u_fscanf read data while reading the end of the file.\n");
    307     }
    308 
    309     u_fclose(myFile);
    310 }
    311 
    312 static void TestFile(void) {
    313 /*    FILE *standardFile;*/
    314 
    315     log_verbose("Testing u_fopen\n");
    316     TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL));
    317 
    318 /* Don't know how to make this work without stdout or stderr */
    319 /*
    320     log_verbose("Testing u_finit\n");
    321     standardFile = fopen(STANDARD_TEST_FILE, "wb");
    322     TestFileFromICU(u_finit(standardFile, NULL, NULL));
    323     fclose(standardFile);
    324 
    325     log_verbose("Testing u_fadopt\n");
    326     standardFile = fopen(STANDARD_TEST_FILE, "wb");
    327     TestFileFromICU(u_fadopt(standardFile, NULL, NULL));
    328 */
    329 }
    330 #endif
    331 
    332 static void StdinBuffering(void) {
    333 #if 0
    334     UChar buff[255];
    335     int32_t num = 0;
    336     UFILE *uStdIn = NULL;
    337     UFILE *uStdOut = NULL;
    338     uStdIn = u_finit(stdin, NULL, NULL);
    339     uStdOut = u_finit(stdout, NULL, NULL);
    340     if (uStdIn == NULL)
    341         return;
    342 
    343     buff[0] = 0x40;
    344     buff[1] = 0;
    345     u_fgets(buff, sizeof(buff)/sizeof(buff[0]), uStdIn);
    346     u_fprintf(uStdOut, "%S\n", buff);
    347     u_fscanf(uStdIn, "%d", &num);
    348     u_fprintf(uStdOut, "%d\n", num);
    349     u_fscanf(uStdIn, "%d", &num);
    350     u_fprintf(uStdOut, "%d\n", num);
    351 #else
    352     log_verbose("Test disabled because it requires user interaction");
    353 #endif
    354 }
    355 
    356 static void TestCodepageAndLocale(void) {
    357     UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
    358     if (myFile == NULL) {
    359         log_err("Can't write test file.\n");
    360         return;
    361     }
    362     if (u_fgetcodepage(myFile) == NULL
    363         || strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
    364     {
    365         log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
    366             u_fgetcodepage(myFile), ucnv_getDefaultName());
    367     }
    368 #if !UCONFIG_NO_FORMATTING
    369     if (u_fgetlocale(myFile) == NULL
    370         || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
    371     {
    372         log_err("Didn't get the proper default locale. Got %s expected: %s\n",
    373             u_fgetlocale(myFile), uloc_getDefault());
    374     }
    375 #endif
    376     u_fclose(myFile);
    377 
    378     myFile = u_fopen(STANDARD_TEST_FILE, "w", "es", NULL);
    379     if (u_fgetcodepage(myFile) == NULL
    380         || strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
    381     {
    382         log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
    383             u_fgetcodepage(myFile));
    384     }
    385 #if !UCONFIG_NO_FORMATTING
    386     if (u_fgetlocale(myFile) == NULL
    387         || strcmp(u_fgetlocale(myFile), "es") != 0)
    388     {
    389         log_err("Didn't get the proper default locale. Got %s expected: %s\n",
    390             u_fgetlocale(myFile), "es");
    391     }
    392 #endif
    393     u_fclose(myFile);
    394 
    395     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
    396     if (u_fgetcodepage(myFile) == NULL
    397         || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
    398     {
    399         log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
    400             u_fgetcodepage(myFile));
    401     }
    402 #if !UCONFIG_NO_FORMATTING
    403     if (u_fgetlocale(myFile) == NULL
    404         || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
    405     {
    406         log_err("Didn't get the proper default locale. Got %s expected: %s\n",
    407             u_fgetlocale(myFile), uloc_getDefault());
    408     }
    409 #endif
    410     u_fclose(myFile);
    411 
    412     myFile = u_fopen(STANDARD_TEST_FILE, "w", "zh", "UTF-16");
    413     if (u_fgetcodepage(myFile) == NULL
    414         || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
    415     {
    416         log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
    417             u_fgetcodepage(myFile));
    418     }
    419 #if !UCONFIG_NO_FORMATTING
    420     if (u_fgetlocale(myFile) == NULL
    421         || strcmp(u_fgetlocale(myFile), "zh") != 0)
    422     {
    423         log_err("Didn't get the proper default locale. Got %s expected: %s\n",
    424             u_fgetlocale(myFile), "zh");
    425     }
    426 #endif
    427     u_fclose(myFile);
    428 }
    429 
    430 
    431 static void TestfgetsBuffers(void) {
    432     UChar buffer[2048];
    433     UChar expectedBuffer[2048];
    434     static const char testStr[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
    435     UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
    436     int32_t expectedSize = (int32_t)strlen(testStr);
    437     int32_t readSize;
    438     int32_t repetitions;
    439 
    440     if (myFile == NULL) {
    441         log_err("Can't write test file.\n");
    442         return;
    443     }
    444 
    445     u_fputc(0x3BC, myFile);
    446     if (u_fputc(0x110000, myFile) != U_EOF) {
    447         log_err("u_fputc should return U_EOF for 0x110000.\n");
    448     }
    449     if (u_fputc((UChar32)0xFFFFFFFFu, myFile) != U_EOF) {
    450         log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
    451     }
    452     u_fputc(0xFF41, myFile);
    453     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    454     u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
    455     u_uastrncpy(buffer, testStr, expectedSize+1);
    456     for (repetitions = 0; repetitions < 16; repetitions++) {
    457         u_file_write(buffer, expectedSize, myFile);
    458         u_strcat(expectedBuffer, buffer);
    459     }
    460     u_fclose(myFile);
    461 
    462     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    463     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16");
    464     if (u_fgetc(myFile) != 0x3BC) {
    465         log_err("The first character is wrong\n");
    466     }
    467     if (u_fgetc(myFile) != 0xFF41) {
    468         log_err("The second character is wrong\n");
    469     }
    470     if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) {
    471         log_err("Didn't get the buffer back\n");
    472         return;
    473     }
    474     readSize = u_strlen(buffer);
    475     if (readSize != expectedSize*repetitions) {
    476         log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
    477     }
    478     if (buffer[(expectedSize*repetitions) + 1] != 0xBEEF) {
    479         log_err("u_fgets wrote too much data\n");
    480     }
    481     if (u_strcmp(buffer, expectedBuffer) != 0) {
    482         log_err("Did get expected string back\n");
    483     }
    484     if (strcmp(u_fgetcodepage(myFile), "UTF-16") != 0) {
    485         log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile));
    486     }
    487     u_fclose(myFile);
    488 
    489     log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
    490 
    491     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
    492 
    493     u_fputc(0x3BC, myFile);
    494     u_fputc(0xFF41, myFile);
    495     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    496     u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
    497     u_uastrncpy(buffer, testStr, expectedSize+1);
    498     for (repetitions = 0; repetitions < 16; repetitions++) {
    499         u_file_write(buffer, expectedSize, myFile);
    500         u_strcat(expectedBuffer, buffer);
    501     }
    502     u_fclose(myFile);
    503 
    504     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    505     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
    506     if (strcmp(u_fgetcodepage(myFile), "UTF-8") != 0) {
    507         log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile));
    508     }
    509     if (u_fgetc(myFile) != 0x3BC) {
    510         log_err("The first character is wrong\n");
    511     }
    512     if (u_fgetc(myFile) != 0xFF41) {
    513         log_err("The second character is wrong\n");
    514     }
    515     if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) {
    516         log_err("Didn't get the buffer back\n");
    517         return;
    518     }
    519     readSize = u_strlen(buffer);
    520     if (readSize != expectedSize*repetitions) {
    521         log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
    522     }
    523     if (buffer[(expectedSize*repetitions) + 1] != 0xBEEF) {
    524         log_err("u_fgets wrote too much data\n");
    525     }
    526     if (u_strcmp(buffer, expectedBuffer) != 0) {
    527         log_err("Did get expected string back\n");
    528     }
    529     u_fclose(myFile);
    530 
    531 
    532     log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
    533 
    534     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
    535 
    536     u_fputc(0xFF41, myFile);
    537     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    538     u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
    539     u_uastrncpy(buffer, testStr, expectedSize+1);
    540     for (repetitions = 0; repetitions < 1; repetitions++) {
    541         u_file_write(buffer, expectedSize, myFile);
    542         u_strcat(expectedBuffer, buffer);
    543     }
    544     u_fclose(myFile);
    545 
    546     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    547     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
    548     if (u_fgets(buffer, 2, myFile) != buffer) {
    549         log_err("Didn't get the buffer back\n");
    550         return;
    551     }
    552     readSize = u_strlen(buffer);
    553     if (readSize != 1) {
    554         log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), 1);
    555     }
    556     if (buffer[0] != 0xFF41 || buffer[1] != 0) {
    557         log_err("Did get expected string back\n");
    558     }
    559     if (buffer[2] != 0xBEEF) {
    560         log_err("u_fgets wrote too much data\n");
    561     }
    562     u_fclose(myFile);
    563 
    564 }
    565 
    566 static void TestFileReadBuffering(void) {
    567     UChar buffer[1024];
    568     UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
    569     int32_t how_many;
    570     int32_t repetitions;
    571 
    572     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    573     for (repetitions = 0; repetitions < 2; repetitions++) {
    574         u_file_write(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
    575     }
    576 
    577     u_fclose(myFile);
    578     u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
    579     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16");
    580     how_many = u_file_read(buffer, 1024, myFile);
    581     if (how_many != 1024 || buffer[1023] != 0xBEEF) {
    582         log_err("u_file_read read too much or not enough data\n");
    583     }
    584     u_fclose(myFile);
    585 }
    586 
    587 static void TestfgetsLineCount(void) {
    588     UChar buffer[2048];
    589     UChar expectedBuffer[2048];
    590     char charBuffer[2048];
    591     static const char testStr[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
    592     UFILE *myFile = NULL;
    593     FILE *stdFile = fopen(STANDARD_TEST_FILE, "w");
    594     int32_t expectedSize = (int32_t)strlen(testStr);
    595     int32_t repetitions;
    596     int32_t nlRepetitions;
    597 
    598     if (stdFile == NULL) {
    599         log_err("Can't write test file.\n");
    600         return;
    601     }
    602     u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
    603 
    604     for (repetitions = 0; repetitions < 16; repetitions++) {
    605         fwrite(testStr, sizeof(testStr[0]), expectedSize, stdFile);
    606         for (nlRepetitions = 0; nlRepetitions < repetitions; nlRepetitions++) {
    607             fwrite("\n", sizeof(testStr[0]), 1, stdFile);
    608         }
    609     }
    610     fclose(stdFile);
    611 
    612     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
    613     stdFile = fopen(STANDARD_TEST_FILE, "r");
    614 
    615     for (;;) {
    616         char *returnedCharBuffer;
    617         UChar *returnedUCharBuffer;
    618 
    619         u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    620         returnedCharBuffer = fgets(charBuffer, sizeof(charBuffer)/sizeof(charBuffer[0]), stdFile);
    621         returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
    622 
    623         if (!returnedCharBuffer && !returnedUCharBuffer) {
    624             /* Both returned NULL. stop. */
    625             break;
    626         }
    627         if (returnedCharBuffer != charBuffer) {
    628             log_err("Didn't get the charBuffer back\n");
    629             continue;
    630         }
    631         u_uastrncpy(expectedBuffer, charBuffer, (int32_t)strlen(charBuffer)+1);
    632         if (returnedUCharBuffer != buffer) {
    633             log_err("Didn't get the buffer back\n");
    634             continue;
    635         }
    636         if (u_strcmp(buffer, expectedBuffer) != 0) {
    637             log_err("buffers are different\n");
    638         }
    639         if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
    640             log_err("u_fgets wrote too much\n");
    641         }
    642     }
    643     fclose(stdFile);
    644     u_fclose(myFile);
    645 }
    646 
    647 static void TestfgetsNewLineHandling(void) {
    648     UChar buffer[256];
    649     static const UChar testUStr[][16] = {
    650         {0x000D, 0},
    651         {0x000D, 0x000A, 0},
    652         {0x000D, 0},
    653         {0x000D, 0},
    654         {0x0085, 0},
    655         {0x000A, 0},
    656         {0x000D, 0},
    657         {0x000B, 0},
    658         {0x000C, 0},
    659         {0x000C, 0},
    660         {0x2028, 0},
    661         {0x0085, 0},
    662         {0x2029, 0},
    663         {0x0085, 0},
    664 
    665         {0x008B, 0x000D, 0},
    666         {0x00A0, 0x000D, 0x000A, 0},
    667         {0x3000, 0x000D, 0},
    668         {0xd800, 0xdfff, 0x000D, 0},
    669         {0x00AB, 0x0085, 0},
    670         {0x00AC, 0x000A, 0},
    671         {0x00AD, 0x000D, 0},
    672         {0x00BA, 0x000B, 0},
    673         {0x00AB, 0x000C, 0},
    674         {0x00B1, 0x000C, 0},
    675         {0x30BB, 0x2028, 0},
    676         {0x00A5, 0x0085, 0},
    677         {0x0080, 0x2029, 0},
    678         {0x00AF, 0x0085, 0}
    679 
    680     };
    681     UFILE *myFile = NULL;
    682     int32_t lineIdx;
    683 
    684     myFile = u_fopen(STANDARD_TEST_FILE, "wb", NULL, "UTF-8");
    685     if (myFile == NULL) {
    686         log_err("Can't write test file.\n");
    687         return;
    688     }
    689     for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) {
    690         u_file_write(testUStr[lineIdx], u_strlen(testUStr[lineIdx]), myFile);
    691     }
    692     u_fclose(myFile);
    693 
    694     myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, "UTF-8");
    695 
    696     for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) {
    697         UChar *returnedUCharBuffer;
    698 
    699         u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    700         returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
    701 
    702         if (!returnedUCharBuffer) {
    703             /* Returned NULL. stop. */
    704             break;
    705         }
    706         if (u_strcmp(buffer, testUStr[lineIdx]) != 0) {
    707             log_err("buffers are different at index = %d\n", lineIdx);
    708         }
    709         if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
    710             log_err("u_fgets wrote too much\n");
    711         }
    712     }
    713     if (lineIdx != (int32_t)(sizeof(testUStr)/sizeof(testUStr[0]))) {
    714         log_err("u_fgets read too much\n");
    715     }
    716     if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != NULL) {
    717         log_err("u_file_write wrote too much\n");
    718     }
    719     u_fclose(myFile);
    720 }
    721 
    722 static void TestLineCount(const char *prefixLine, const char *line, int32_t numRepititions) {
    723     UChar buffer[64];
    724     UChar expectedBuffer[64];
    725     int32_t lineLen = strlen(line);
    726     UChar *returnedUCharBuffer;
    727     int32_t repetitions;
    728     UFILE *myFile = NULL;
    729     FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb");
    730 
    731     if (stdFile == NULL) {
    732         log_err("Can't write test file.\n");
    733         return;
    734     }
    735     /* Write a prefix line and then write a bunch of lines */
    736     fwrite(prefixLine, strlen(prefixLine), 1, stdFile);
    737     for (repetitions = 0; repetitions < numRepititions; repetitions++) {
    738         fwrite(line, lineLen, 1, stdFile);
    739     }
    740     fclose(stdFile);
    741 
    742     myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL);
    743     if (myFile == NULL) {
    744         log_err("Can't read test file.\n");
    745         return;
    746     }
    747 
    748     /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
    749     u_uastrncpy(expectedBuffer, prefixLine, (int32_t)strlen(prefixLine)+1);
    750     returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
    751     if (u_strcmp(returnedUCharBuffer, expectedBuffer) != 0) {
    752         log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine);
    753         return;
    754     }
    755 
    756     u_uastrncpy(expectedBuffer, line, (int32_t)strlen(line)+1);
    757     for (repetitions = 0; ; repetitions++) {
    758         u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    759         returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
    760 
    761         if (!returnedUCharBuffer) {
    762             /* returned NULL. stop. */
    763             break;
    764         }
    765         if (u_strcmp(buffer, expectedBuffer) != 0) {
    766             log_err("buffers are different at count %d\n", repetitions);
    767         }
    768         if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
    769             log_err("u_fgets wrote too much\n");
    770         }
    771     }
    772     if (repetitions != numRepititions) {
    773         log_err("got wrong number of lines. got=%d expected=%d\n", repetitions, numRepititions);
    774     }
    775     u_fclose(myFile);
    776 }
    777 
    778 static void TestfgetsNewLineCount(void) {
    779     /* This makes sure that lines are correctly handled between buffer boundaries. */
    780     TestLineCount("\n", "\n", 1024);    /* Unix newlines */
    781     TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
    782     TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
    783     TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
    784     TestLineCount("\n", "a\n", 1024);    /* Unix newlines offset with data */
    785     TestLineCount("\n", "\r\n", 1024);  /* a mixed number of lines. */
    786 }
    787 
    788 static void TestFgetsLineBuffering(void) {
    789     UChar buffer[2003]; /* Use a non-power of 2 or 10 */
    790     UChar *returnedUCharBuffer;
    791     int32_t repetitions;
    792     UFILE *myFile = NULL;
    793     FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb");
    794 
    795     if (stdFile == NULL) {
    796         log_err("Can't write test file.\n");
    797         return;
    798     }
    799     u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
    800 
    801     /* Write one very long line */
    802     for (repetitions = 0; repetitions < ((sizeof(buffer)/sizeof(buffer[0]))*2); repetitions++) {
    803         fwrite(repetitions ? "1" : "2", 1, 1, stdFile);
    804     }
    805     fclose(stdFile);
    806 
    807     myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL);
    808     if (myFile == NULL) {
    809         log_err("Can't read test file.\n");
    810         return;
    811     }
    812 
    813     /* Read part of one very long line */
    814     returnedUCharBuffer = u_fgets(buffer, (sizeof(buffer)/sizeof(buffer[0]))-1, myFile);
    815     if (u_strlen(returnedUCharBuffer) != ((sizeof(buffer)/sizeof(buffer[0]))-2)) {
    816         log_err("Line is wrong length. Got %d. Expected %d.\n",
    817             u_strlen(returnedUCharBuffer), ((sizeof(buffer)/sizeof(buffer[0]))-2));
    818     }
    819     /* We better not read too much */
    820     if (buffer[(sizeof(buffer)/sizeof(buffer[0]))-1] != 0xBEEF) {
    821         log_err("Too much data was written\n");
    822     }
    823 
    824     u_fclose(myFile);
    825 }
    826 
    827 
    828 static void TestCodepage(void) {
    829     UFILE *myFile = NULL;
    830     static const UChar strABAccentA[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
    831     static const UChar strBadConversion[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
    832     UChar testBuf[sizeof(strABAccentA)/sizeof(strABAccentA[0])*2]; /* *2 to see if too much was  */
    833     char convName[UCNV_MAX_CONVERTER_NAME_LENGTH];
    834     int32_t retVal;
    835     UErrorCode status = U_ZERO_ERROR;
    836 
    837     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "absurd converter that can't be opened");
    838 
    839     if (myFile) {
    840         log_err("Recieved a UFILE * with an invalid codepage parameter\n");
    841         u_fclose(myFile);
    842     }
    843 
    844     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "ISO-8859-1");
    845     if (myFile == NULL) {
    846         log_err("Can't write test file for iso-8859-1.\n");
    847         return;
    848     }
    849     if (strcmp("ISO-8859-1", u_fgetcodepage(myFile)) != 0) {
    850         log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
    851     }
    852     u_file_write(strABAccentA, u_strlen(strABAccentA), myFile);
    853     u_fclose(myFile);
    854 
    855     /* Now see what we got wrote */
    856     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
    857     if (u_fsetcodepage("ISO-8859-1", myFile) != 0) {
    858         log_err("u_fsetcodepage didn't set the codepage\n");
    859     }
    860     retVal = u_file_read(testBuf, u_strlen(strABAccentA), myFile);
    861     if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
    862         log_err("The test data was read and written differently!\n");
    863     }
    864     if (retVal != u_strlen(strABAccentA)) {
    865         log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
    866     }
    867     u_fclose(myFile);
    868 
    869     /* What happens on invalid input? */
    870     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "ISO-8859-1");
    871     if (strcmp(ucnv_getName(u_fgetConverter(myFile), &status), "ISO-8859-1") != 0) {
    872         log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile), &status));
    873     }
    874     if (u_fsetcodepage("UTF-8", myFile) != 0) {
    875         log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
    876     }
    877     if (strcmp(ucnv_getName(u_fgetConverter(myFile), &status), "UTF-8") != 0) {
    878         log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile), &status));
    879     }
    880     retVal = u_file_read(testBuf, u_strlen(strBadConversion), myFile);
    881     if (u_strncmp(strBadConversion, testBuf, u_strlen(strBadConversion)) != 0) {
    882         log_err("The test data wasn't subsituted as expected\n");
    883     }
    884     u_fclose(myFile);
    885 
    886     /* Can't currently swap codepages midstream */
    887     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "ISO-8859-1");
    888     strcpy(convName, u_fgetcodepage(myFile));
    889     u_file_read(testBuf, 1, myFile);
    890     if (u_fsetcodepage("UTF-8", myFile) == 0) {
    891         log_err("u_fsetcodepage set the codepage after reading a byte\n");
    892     }
    893     retVal = u_file_read(testBuf + 1, u_strlen(strABAccentA) - 1, myFile);
    894     if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
    895         log_err("u_fsetcodepage changed the codepages after writing data\n");
    896     }
    897     if ((retVal + 1) != u_strlen(strABAccentA)) {
    898         log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
    899     }
    900     u_frewind(myFile);
    901     retVal = u_file_read(testBuf, u_strlen(strABAccentA), myFile);
    902     if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
    903         log_err("The test data was read and written differently!\n");
    904     }
    905     if (retVal != u_strlen(strABAccentA)) {
    906         log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
    907     }
    908     u_fclose(myFile);
    909 
    910 }
    911 
    912 #if !UCONFIG_NO_FORMATTING
    913 static void TestFilePrintCompatibility(void) {
    914     UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
    915     FILE *myCFile;
    916     int32_t num;
    917     char cVal;
    918     static const UChar emptyStr[] = {0};
    919     char readBuf[512] = "";
    920     char testBuf[512] = "";
    921 
    922     if (myFile == NULL) {
    923         log_err("Can't write test file.\n");
    924         return;
    925     }
    926 #if !UCONFIG_NO_FORMATTING
    927     if (strcmp(u_fgetlocale(myFile), "en_US_POSIX") != 0) {
    928         log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile));
    929     }
    930 #endif
    931 
    932     /* Compare against C API compatibility */
    933     for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
    934         u_fprintf(myFile, "%x ", num);
    935         u_fprintf(myFile, "%X ", num);
    936         u_fprintf(myFile, "%o ", num);
    937         u_fprintf(myFile, "%d ", num);
    938         u_fprintf(myFile, "%i ", num);
    939         u_fprintf(myFile, "%f ", (double)num);
    940 /*        u_fprintf(myFile, "%e ", (double)num);
    941         u_fprintf(myFile, "%E ", (double)num);*/
    942         u_fprintf(myFile, "%g ", (double)num);
    943         u_fprintf(myFile, "%G", (double)num);
    944         u_fputs(emptyStr, myFile);
    945     }
    946 
    947     u_fprintf_u(myFile, NEW_LINE);
    948 
    949     for (num = 0; num < 0x80; num++) {
    950         u_fprintf(myFile, "%c", num);
    951     }
    952 
    953     u_fclose(myFile);
    954     myCFile = fopen(STANDARD_TEST_FILE, "rb");
    955     if (myCFile == NULL) {
    956         log_err("Can't read test file.");
    957         return;
    958     }
    959 
    960     for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
    961         fscanf(myCFile, "%s", readBuf);
    962         sprintf(testBuf, "%x", (int)num);
    963         if (strcmp(readBuf, testBuf) != 0) {
    964             log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    965         }
    966 
    967         fscanf(myCFile, "%s", readBuf);
    968         sprintf(testBuf, "%X", (int)num);
    969         if (strcmp(readBuf, testBuf) != 0) {
    970             log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    971         }
    972 
    973         fscanf(myCFile, "%s", readBuf);
    974         sprintf(testBuf, "%o", (int)num);
    975         if (strcmp(readBuf, testBuf) != 0) {
    976             log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    977         }
    978 
    979         /* fprintf is not compatible on all platforms e.g. the iSeries */
    980         fscanf(myCFile, "%s", readBuf);
    981         sprintf(testBuf, "%d", (int)num);
    982         if (strcmp(readBuf, testBuf) != 0) {
    983             log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    984         }
    985 
    986         fscanf(myCFile, "%s", readBuf);
    987         sprintf(testBuf, "%i", (int)num);
    988         if (strcmp(readBuf, testBuf) != 0) {
    989             log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    990         }
    991 
    992         fscanf(myCFile, "%s", readBuf);
    993         sprintf(testBuf, "%f", (double)num);
    994         if (strcmp(readBuf, testBuf) != 0) {
    995             log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
    996         }
    997 
    998 /*        fscanf(myCFile, "%s", readBuf);
    999         sprintf(testBuf, "%e", (double)num);
   1000         if (strcmp(readBuf, testBuf) != 0) {
   1001             log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1002         }
   1003 
   1004         fscanf(myCFile, "%s", readBuf);
   1005         sprintf(testBuf, "%E", (double)num);
   1006         if (strcmp(readBuf, testBuf) != 0) {
   1007             log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1008         }*/
   1009 
   1010         fscanf(myCFile, "%s", readBuf);
   1011         sprintf(testBuf, "%g", (double)num);
   1012         if (strcmp(readBuf, testBuf) != 0) {
   1013             log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1014         }
   1015 
   1016         fscanf(myCFile, "%s", readBuf);
   1017         sprintf(testBuf, "%G", (double)num);
   1018         if (strcmp(readBuf, testBuf) != 0) {
   1019             log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1020         }
   1021     }
   1022 
   1023     /* Properly eat the newlines */
   1024     for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
   1025         fscanf(myCFile, "%c", &cVal);
   1026         if (cVal != C_NEW_LINE[num]) {
   1027             log_err("OS newline error\n");
   1028         }
   1029     }
   1030     for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
   1031         fscanf(myCFile, "%c", &cVal);
   1032         if (cVal != C_NEW_LINE[num]) {
   1033             log_err("ustdio newline error\n");
   1034         }
   1035     }
   1036 
   1037     for (num = 0; num < 0x80; num++) {
   1038         cVal = -1;
   1039         fscanf(myCFile, "%c", &cVal);
   1040         if (num != cVal) {
   1041             log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
   1042         }
   1043     }
   1044     fclose(myCFile);
   1045 }
   1046 #endif
   1047 
   1048 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
   1049     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
   1050     if (myFile == NULL) {\
   1051         log_err("Can't write test file for %s.\n", uFormat);\
   1052         return;\
   1053     }\
   1054     /* Reinitialize the buffer to verify null termination works. */\
   1055     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
   1056     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
   1057     \
   1058     uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
   1059     u_fclose(myFile);\
   1060     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
   1061     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
   1062     u_fclose(myFile);\
   1063     u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
   1064     cNumPrinted = sprintf(buffer, cFormat, cValue);\
   1065     if (strcmp(buffer, compBuffer) != 0) {\
   1066         log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
   1067     }\
   1068     if (cNumPrinted != uNumPrinted) {\
   1069         log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
   1070     }\
   1071     if (buffer[uNumPrinted+1] != '*') {\
   1072         log_err("%" uFormat " too much stored\n");\
   1073     }\
   1074 
   1075 #if !UCONFIG_NO_FORMATTING
   1076 static void TestFprintfFormat(void) {
   1077     static const UChar abcUChars[] = {0x61,0x62,0x63,0};
   1078     static const char abcChars[] = "abc";
   1079     UChar uBuffer[256];
   1080     char buffer[256];
   1081     char compBuffer[256];
   1082     int32_t uNumPrinted;
   1083     int32_t cNumPrinted;
   1084     UFILE *myFile;
   1085 
   1086     TestFPrintFormat("%8S", abcUChars, "%8s", abcChars);
   1087     TestFPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
   1088     TestFPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
   1089 
   1090     TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
   1091     TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
   1092     TestFPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
   1093 
   1094     TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
   1095     TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
   1096 
   1097     TestFPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
   1098     TestFPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
   1099 
   1100     TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
   1101     TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
   1102     TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
   1103     TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
   1104     TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
   1105     TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
   1106     TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
   1107     TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
   1108     TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
   1109 
   1110 /*    TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
   1111     TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
   1112     TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
   1113     TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
   1114     TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
   1115     TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
   1116     TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
   1117     TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
   1118     TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
   1119     TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
   1120 
   1121     TestFPrintFormat("%8x", 123456, "%8x", 123456);
   1122     TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
   1123     TestFPrintFormat("%08x", 123456, "%08x", 123456);
   1124 
   1125     TestFPrintFormat("%8X", 123456, "%8X", 123456);
   1126     TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
   1127     TestFPrintFormat("%08X", 123456, "%08X", 123456);
   1128     TestFPrintFormat("%#x", 123456, "%#x", 123456);
   1129     TestFPrintFormat("%#x", -123456, "%#x", -123456);
   1130 
   1131     TestFPrintFormat("%8o", 123456, "%8o", 123456);
   1132     TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
   1133     TestFPrintFormat("%08o", 123456, "%08o", 123456);
   1134     TestFPrintFormat("%#o", 123, "%#o", 123);
   1135     TestFPrintFormat("%#o", -123, "%#o", -123);
   1136 
   1137     TestFPrintFormat("%8u", 123456, "%8u", 123456);
   1138     TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
   1139     TestFPrintFormat("%08u", 123456, "%08u", 123456);
   1140     TestFPrintFormat("%8u", -123456, "%8u", -123456);
   1141     TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
   1142     TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
   1143     TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
   1144     TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
   1145 
   1146     TestFPrintFormat("%8d", 123456, "%8d", 123456);
   1147     TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
   1148     TestFPrintFormat("%08d", 123456, "%08d", 123456);
   1149     TestFPrintFormat("% d", 123456, "% d", 123456);
   1150     TestFPrintFormat("% d", -123456, "% d", -123456);
   1151 
   1152     TestFPrintFormat("%8i", 123456, "%8i", 123456);
   1153     TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
   1154     TestFPrintFormat("%08i", 123456, "%08i", 123456);
   1155 
   1156     log_verbose("Get really crazy with the formatting.\n");
   1157 
   1158     TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
   1159     TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
   1160     TestFPrintFormat("%#12x", 123, "%#12x", 123);
   1161     TestFPrintFormat("%#12x", -123, "%#12x", -123);
   1162 
   1163     TestFPrintFormat("%-+12d", 123,  "%-+12d", 123);
   1164     TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
   1165     TestFPrintFormat("%- 12d", 123,  "%- 12d", 123);
   1166     TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
   1167     TestFPrintFormat("%+12d", 123,   "%+12d", 123);
   1168     TestFPrintFormat("%+12d", -123,  "%+12d", -123);
   1169     TestFPrintFormat("% 12d", 123,   "% 12d", 123);
   1170     TestFPrintFormat("% 12d", -123,  "% 12d", -123);
   1171     TestFPrintFormat("%12d", 123,    "%12d", 123);
   1172     TestFPrintFormat("%12d", -123,   "%12d", -123);
   1173     TestFPrintFormat("%.12d", 123,   "%.12d", 123);
   1174     TestFPrintFormat("%.12d", -123,  "%.12d", -123);
   1175 
   1176     TestFPrintFormat("%-+12.1f", 1.234,  "%-+12.1f", 1.234);
   1177     TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
   1178     TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
   1179     TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
   1180     TestFPrintFormat("%+12.1f", 1.234,   "%+12.1f", 1.234);
   1181     TestFPrintFormat("%+12.1f", -1.234,  "%+12.1f", -1.234);
   1182     TestFPrintFormat("% 12.1f", 1.234,   "% 12.1f", 1.234);
   1183     TestFPrintFormat("% 12.1f", -1.234,  "% 12.1f", -1.234);
   1184     TestFPrintFormat("%12.1f", 1.234,    "%12.1f", 1.234);
   1185     TestFPrintFormat("%12.1f", -1.234,   "%12.1f", -1.234);
   1186     TestFPrintFormat("%.2f", 1.234,      "%.2f", 1.234);
   1187     TestFPrintFormat("%.2f", -1.234,     "%.2f", -1.234);
   1188     TestFPrintFormat("%3f", 1.234,       "%3f", 1.234);
   1189     TestFPrintFormat("%3f", -1.234,      "%3f", -1.234);
   1190 
   1191     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
   1192     /* Reinitialize the buffer to verify null termination works. */
   1193     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1194     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
   1195 
   1196     uNumPrinted = u_fprintf(myFile, "%d % d %d", -1234, 1234, 1234);
   1197     u_fclose(myFile);
   1198     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1199     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1200     u_fclose(myFile);
   1201     u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
   1202     cNumPrinted = sprintf(buffer, "%d % d %d", -1234, 1234, 1234);
   1203     if (strcmp(buffer, compBuffer) != 0) {
   1204         log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);
   1205     }
   1206     if (cNumPrinted != uNumPrinted) {
   1207         log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);
   1208     }
   1209     if (buffer[uNumPrinted+1] != '*') {
   1210         log_err("%%d %% d %%d too much stored\n");
   1211     }
   1212 }
   1213 #endif
   1214 
   1215 #undef TestFPrintFormat
   1216 
   1217 #if !UCONFIG_NO_FORMATTING
   1218 static void TestFScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
   1219     UFILE *myFile;
   1220     UChar uBuffer[256];
   1221     char buffer[256];
   1222     char compBuffer[256];
   1223     int32_t uNumScanned;
   1224     int32_t cNumScanned;
   1225 
   1226     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
   1227     if (myFile == NULL) {
   1228         log_err("Can't write test file for %s.\n", format);
   1229         return;
   1230     }
   1231     /* Reinitialize the buffer to verify null termination works. */
   1232     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1233     uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
   1234     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
   1235     buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0;
   1236 
   1237     u_fprintf(myFile, "%S", uValue);
   1238     u_fclose(myFile);
   1239     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1240     uNumScanned = u_fscanf(myFile, format, uBuffer);
   1241     u_fclose(myFile);
   1242     if (expectedToPass) {
   1243         u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
   1244         cNumScanned = sscanf(cValue, format, buffer);
   1245         if (strncmp(buffer, compBuffer, sizeof(buffer)/sizeof(*buffer)) != 0) {
   1246             log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
   1247         }
   1248         if (cNumScanned != uNumScanned) {
   1249             log_err("%s number printed Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
   1250         }
   1251         if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
   1252             log_err("%s too much stored\n", format);
   1253         }
   1254     }
   1255     else {
   1256         if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
   1257             log_err("%s too much stored on a failure\n", format);
   1258         }
   1259     }
   1260 }
   1261 #endif
   1262 
   1263 #if !UCONFIG_NO_FORMATTING
   1264 static void TestFScanset(void) {
   1265     static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
   1266     static const char abcChars[] = "abccdefg";
   1267 
   1268     TestFScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
   1269     TestFScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
   1270 
   1271     TestFScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
   1272     TestFScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
   1273 
   1274     TestFScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
   1275     TestFScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
   1276 
   1277     TestFScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
   1278     TestFScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
   1279 
   1280     TestFScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
   1281     TestFScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
   1282     TestFScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
   1283     TestFScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
   1284     TestFScanSetFormat("%[^e]  ", abcUChars, abcChars, TRUE);
   1285 
   1286     TestFScanSetFormat("%1[ab]  ", abcUChars, abcChars, TRUE);
   1287     TestFScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
   1288 
   1289     TestFScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
   1290 
   1291     /* Extra long string for testing */
   1292     TestFScanSetFormat("                                                                                                                         %[qrst]",
   1293         abcUChars, abcChars, TRUE);
   1294 
   1295     TestFScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
   1296 
   1297     /* Bad format */
   1298     TestFScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
   1299     TestFScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
   1300     TestFScanSetFormat("%[a", abcUChars, abcChars, FALSE);
   1301     /* The following is not deterministic on Windows */
   1302 /*    TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
   1303 
   1304     /* TODO: Need to specify precision with a "*" */
   1305 }
   1306 #endif
   1307 #if !UCONFIG_NO_FORMATTING
   1308 static void TestBadFScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
   1309     UFILE *myFile;
   1310     UChar uBuffer[256];
   1311     int32_t uNumScanned;
   1312 
   1313     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
   1314     if (myFile == NULL) {
   1315         log_err("Can't write test file for %s.\n", format);
   1316         return;
   1317     }
   1318     /* Reinitialize the buffer to verify null termination works. */
   1319     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1320     uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
   1321 
   1322     u_fprintf(myFile, "%S", uValue);
   1323     u_fclose(myFile);
   1324     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1325     uNumScanned = u_fscanf(myFile, format, uBuffer);
   1326     u_fclose(myFile);
   1327     if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
   1328         log_err("%s too much stored on a failure\n", format);
   1329     }
   1330 }
   1331 #endif
   1332 #if !UCONFIG_NO_FORMATTING
   1333 static void TestBadScanfFormat(void) {
   1334     static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
   1335     static const char abcChars[] = "abccdefg";
   1336 
   1337     TestBadFScanfFormat("%[]  ", abcUChars, abcChars);
   1338 }
   1339 #endif
   1340 #if !UCONFIG_NO_FORMATTING
   1341 static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
   1342     UChar uBuffer[256];
   1343     UChar uBuffer2[256];
   1344     va_list ap;
   1345     int32_t count;
   1346     UFILE *myFile;
   1347 
   1348     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "UTF-8");
   1349     if (!myFile) {
   1350         log_err("Test file can't be opened\n");
   1351         return;
   1352     }
   1353 
   1354     va_start(ap, format);
   1355     count = u_vfprintf(myFile, format, ap);
   1356     va_end(ap);
   1357 
   1358     u_fclose(myFile);
   1359 
   1360 
   1361     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", "UTF-8");
   1362     if (!myFile) {
   1363         log_err("Test file can't be opened\n");
   1364         return;
   1365     }
   1366     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1367     u_uastrcpy(uBuffer2, expectedResult);
   1368     if (u_strcmp(uBuffer, uBuffer2) != 0) {
   1369         log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
   1370     }
   1371     u_fclose(myFile);
   1372 
   1373 
   1374     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
   1375     if (!myFile) {
   1376         log_err("Test file can't be opened\n");
   1377         return;
   1378     }
   1379     u_uastrcpy(uBuffer, format);
   1380 
   1381     va_start(ap, format);
   1382     count = u_vfprintf_u(myFile, uBuffer, ap);
   1383     va_end(ap);
   1384 
   1385     u_fclose(myFile);
   1386 
   1387 
   1388     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1389     if (!myFile) {
   1390         log_err("Test file can't be opened\n");
   1391         return;
   1392     }
   1393     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1394     u_uastrcpy(uBuffer2, expectedResult);
   1395     if (u_strcmp(uBuffer, uBuffer2) != 0) {
   1396         log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
   1397     }
   1398     u_fclose(myFile);
   1399 }
   1400 
   1401 static void TestVargs(void) {
   1402     Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
   1403 }
   1404 #endif
   1405 
   1406 static void TestUnicodeFormat(void)
   1407 {
   1408 #if !UCONFIG_NO_FORMATTING
   1409     /* Make sure that invariant conversion doesn't happen on the _u formats. */
   1410     UChar myUString[256];
   1411     UFILE *myFile;
   1412     static const UChar TEST_STR[] = { 0x03BC, 0x0025, 0x0024, 0};
   1413     static const UChar PERCENT_S[] = { 0x03BC, 0x0025, 0x0053, 0};
   1414 
   1415     u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
   1416 
   1417     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
   1418     if (!myFile) {
   1419         log_err("Test file can't be opened\n");
   1420         return;
   1421     }
   1422     u_fprintf_u(myFile, PERCENT_S, TEST_STR);
   1423     u_fclose(myFile);
   1424 
   1425     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
   1426     if (!myFile) {
   1427         log_err("Test file can't be opened\n");
   1428         return;
   1429     }
   1430     u_fscanf_u(myFile, PERCENT_S, myUString);
   1431     u_fclose(myFile);
   1432     if (u_strcmp(TEST_STR, myUString) != 0) {
   1433         log_err("u_fscanf_u doesn't work.\n");
   1434     }
   1435 #endif
   1436 }
   1437 
   1438 static void TestFileWriteRetval(const char * a_pszEncoding) {
   1439     UChar * buffer;
   1440     UFILE * myFile;
   1441     int32_t count;
   1442     int32_t expected = 10000; /* test with large data to test internal buffer looping */
   1443     UChar   testChar = 0xBEEF;
   1444 
   1445     if (!*a_pszEncoding || 0 == strcmp(a_pszEncoding, "ASCII")) {
   1446         testChar = 0x65; /* 'A' - otherwise read test will fail */
   1447     }
   1448 
   1449     buffer = (UChar*) malloc(expected * sizeof(UChar));
   1450     if (!buffer) {
   1451         log_err("Out of memory\n");
   1452         return;
   1453     }
   1454 
   1455     /* write */
   1456     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, a_pszEncoding);
   1457     if (!myFile) {
   1458         free(buffer);
   1459         log_err("Test file can't be opened for write\n");
   1460         return;
   1461     }
   1462     u_memset(buffer, testChar, expected);
   1463     count = u_file_write(buffer, expected, myFile);
   1464     u_fclose(myFile);
   1465     if (count != expected) {
   1466         free(buffer);
   1467         log_err("u_file_write returned incorrect number of characters written\n");
   1468         return;
   1469     }
   1470 
   1471     free(buffer);
   1472     buffer = NULL;
   1473 
   1474     /* read */
   1475     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, a_pszEncoding);
   1476     if (!myFile) {
   1477         log_err("Test file can't be opened for read\n");
   1478         return;
   1479     }
   1480     for (count = 0; count < expected; ++count) {
   1481         UChar gotChar = u_fgetc(myFile);
   1482 	if(gotChar != testChar) {
   1483             log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar, testChar);
   1484             u_fclose(myFile);
   1485             return;
   1486         }
   1487     }
   1488     if (u_fgetc(myFile) != U_EOF) {
   1489         log_err("u_fgetc did not return expected EOF\n");
   1490         u_fclose(myFile);
   1491         return;
   1492         }
   1493     u_fclose(myFile);
   1494 }
   1495 
   1496 static void TestFileWriteRetvalUTF16(void) {
   1497     TestFileWriteRetval("UTF-16");
   1498 }
   1499 
   1500 static void TestFileWriteRetvalUTF8(void) {
   1501     TestFileWriteRetval("UTF-8");
   1502 }
   1503 
   1504 static void TestFileWriteRetvalASCII(void) {
   1505     TestFileWriteRetval("ASCII");
   1506 }
   1507 
   1508 static void TestFileWriteRetvalNONE(void) {
   1509     TestFileWriteRetval("");
   1510 }
   1511 
   1512 U_CFUNC void
   1513 addFileTest(TestNode** root) {
   1514 #if !UCONFIG_NO_FORMATTING
   1515     addTest(root, &TestFile, "file/TestFile");
   1516 #endif
   1517     addTest(root, &StdinBuffering, "file/StdinBuffering");
   1518     addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers");
   1519     addTest(root, &TestFileReadBuffering, "file/TestFileReadBuffering");
   1520     addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount");
   1521     addTest(root, &TestfgetsNewLineHandling, "file/TestfgetsNewLineHandling");
   1522     addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount");
   1523     addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering");
   1524     addTest(root, &TestCodepage, "file/TestCodepage");
   1525     addTest(root, &TestFileWriteRetvalUTF16, "file/TestFileWriteRetvalUTF16");
   1526     addTest(root, &TestFileWriteRetvalUTF8, "file/TestFileWriteRetvalUTF8");
   1527     addTest(root, &TestFileWriteRetvalASCII, "file/TestFileWriteRetvalASCII");
   1528     addTest(root, &TestFileWriteRetvalNONE, "file/TestFileWriteRetvalNONE");
   1529 #if !UCONFIG_NO_FORMATTING
   1530     addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale");
   1531     addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
   1532     addTest(root, &TestFScanset, "file/TestFScanset");
   1533     addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
   1534     addTest(root, &TestBadScanfFormat, "file/TestBadScanfFormat");
   1535     addTest(root, &TestVargs, "file/TestVargs");
   1536     addTest(root, &TestUnicodeFormat, "file/TestUnicodeFormat");
   1537 #endif
   1538 }
   1539