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 static void TestCodepageFlush(void) {
    913 #if UCONFIG_NO_LEGACY_CONVERSION
    914   log_verbose("Skipping, legacy conversion is disabled.");
    915 #else
    916   UChar utf16String[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
    917   uint8_t inBuf[200];
    918   size_t inLen =0;
    919   const char *enc = "IBM-1388"; /* GBK EBCDIC stateful */
    920   UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", enc);
    921   FILE *myCFile;
    922   int shift = 0;
    923   int i;
    924 
    925   if (myFile == NULL) {
    926     log_err("Can't write test file %s\n", STANDARD_TEST_FILE);
    927     return;
    928   }
    929 
    930   u_fprintf(myFile, "%S", utf16String);
    931   u_fclose(myFile);
    932 
    933   /* now read it back */
    934     myCFile = fopen(STANDARD_TEST_FILE, "rb");
    935     if (myCFile == NULL) {
    936         log_err("Can't read test file.");
    937         return;
    938     }
    939 
    940     inLen = fread(inBuf, 1, 200, myCFile);
    941     fclose(myCFile);
    942 
    943     if(inLen<=0) {
    944       log_err("Failed during read of test file.");
    945       return;
    946     }
    947 
    948     /* check if shift in and out */
    949     for(i=0;i<inLen;i++) {
    950       if(inBuf[i]==0x0E) {  /* SO */
    951         shift= 1;
    952       } else if(inBuf[i]==0x0F) { /* SI */
    953         shift= -1;
    954       }
    955     }
    956 
    957     if(shift==0) {
    958       log_err("Err: shift was unchanged\n");
    959     } else if(shift==1) {
    960       log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
    961     } else if(shift==-1) {
    962       log_verbose("OK: Shifted in (SI, 0x0F)\n");
    963     }
    964 
    965     if(inLen != 12) {
    966       log_err("Expected 12 bytes, read %d\n", inLen);
    967     } else {
    968       log_verbose("OK: read %d bytes\n", inLen);
    969     }
    970 
    971 
    972 #endif
    973 }
    974 
    975 #if !UCONFIG_NO_FORMATTING
    976 static void TestFilePrintCompatibility(void) {
    977     UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
    978     FILE *myCFile;
    979     int32_t num;
    980     char cVal;
    981     static const UChar emptyStr[] = {0};
    982     char readBuf[512] = "";
    983     char testBuf[512] = "";
    984 
    985     if (myFile == NULL) {
    986         log_err("Can't write test file.\n");
    987         return;
    988     }
    989 #if !UCONFIG_NO_FORMATTING
    990     if (strcmp(u_fgetlocale(myFile), "en_US_POSIX") != 0) {
    991         log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile));
    992     }
    993 #endif
    994 
    995     /* Compare against C API compatibility */
    996     for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
    997         u_fprintf(myFile, "%x ", num);
    998         u_fprintf(myFile, "%X ", num);
    999         u_fprintf(myFile, "%o ", num);
   1000         u_fprintf(myFile, "%d ", num);
   1001         u_fprintf(myFile, "%i ", num);
   1002         u_fprintf(myFile, "%f ", (double)num);
   1003 /*        u_fprintf(myFile, "%e ", (double)num);
   1004         u_fprintf(myFile, "%E ", (double)num);*/
   1005         u_fprintf(myFile, "%g ", (double)num);
   1006         u_fprintf(myFile, "%G", (double)num);
   1007         u_fputs(emptyStr, myFile);
   1008     }
   1009 
   1010     u_fprintf_u(myFile, NEW_LINE);
   1011 
   1012     for (num = 0; num < 0x80; num++) {
   1013         u_fprintf(myFile, "%c", num);
   1014     }
   1015 
   1016     u_fclose(myFile);
   1017     myCFile = fopen(STANDARD_TEST_FILE, "rb");
   1018     if (myCFile == NULL) {
   1019         log_err("Can't read test file.");
   1020         return;
   1021     }
   1022 
   1023     for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
   1024         fscanf(myCFile, "%s", readBuf);
   1025         sprintf(testBuf, "%x", (int)num);
   1026         if (strcmp(readBuf, testBuf) != 0) {
   1027             log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1028         }
   1029 
   1030         fscanf(myCFile, "%s", readBuf);
   1031         sprintf(testBuf, "%X", (int)num);
   1032         if (strcmp(readBuf, testBuf) != 0) {
   1033             log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1034         }
   1035 
   1036         fscanf(myCFile, "%s", readBuf);
   1037         sprintf(testBuf, "%o", (int)num);
   1038         if (strcmp(readBuf, testBuf) != 0) {
   1039             log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1040         }
   1041 
   1042         /* fprintf is not compatible on all platforms e.g. the iSeries */
   1043         fscanf(myCFile, "%s", readBuf);
   1044         sprintf(testBuf, "%d", (int)num);
   1045         if (strcmp(readBuf, testBuf) != 0) {
   1046             log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1047         }
   1048 
   1049         fscanf(myCFile, "%s", readBuf);
   1050         sprintf(testBuf, "%i", (int)num);
   1051         if (strcmp(readBuf, testBuf) != 0) {
   1052             log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1053         }
   1054 
   1055         fscanf(myCFile, "%s", readBuf);
   1056         sprintf(testBuf, "%f", (double)num);
   1057         if (strcmp(readBuf, testBuf) != 0) {
   1058             log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1059         }
   1060 
   1061 /*        fscanf(myCFile, "%s", readBuf);
   1062         sprintf(testBuf, "%e", (double)num);
   1063         if (strcmp(readBuf, testBuf) != 0) {
   1064             log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1065         }
   1066 
   1067         fscanf(myCFile, "%s", readBuf);
   1068         sprintf(testBuf, "%E", (double)num);
   1069         if (strcmp(readBuf, testBuf) != 0) {
   1070             log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1071         }*/
   1072 
   1073         fscanf(myCFile, "%s", readBuf);
   1074         sprintf(testBuf, "%g", (double)num);
   1075         if (strcmp(readBuf, testBuf) != 0) {
   1076             log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1077         }
   1078 
   1079         fscanf(myCFile, "%s", readBuf);
   1080         sprintf(testBuf, "%G", (double)num);
   1081         if (strcmp(readBuf, testBuf) != 0) {
   1082             log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
   1083         }
   1084     }
   1085 
   1086     /* Properly eat the newlines */
   1087     for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
   1088         fscanf(myCFile, "%c", &cVal);
   1089         if (cVal != C_NEW_LINE[num]) {
   1090             log_err("OS newline error\n");
   1091         }
   1092     }
   1093     for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
   1094         fscanf(myCFile, "%c", &cVal);
   1095         if (cVal != C_NEW_LINE[num]) {
   1096             log_err("ustdio newline error\n");
   1097         }
   1098     }
   1099 
   1100     for (num = 0; num < 0x80; num++) {
   1101         cVal = -1;
   1102         fscanf(myCFile, "%c", &cVal);
   1103         if (num != cVal) {
   1104             log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
   1105         }
   1106     }
   1107     fclose(myCFile);
   1108 }
   1109 #endif
   1110 
   1111 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
   1112     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
   1113     if (myFile == NULL) {\
   1114         log_err("Can't write test file for %s.\n", uFormat);\
   1115         return;\
   1116     }\
   1117     /* Reinitialize the buffer to verify null termination works. */\
   1118     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
   1119     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
   1120     \
   1121     uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
   1122     u_fclose(myFile);\
   1123     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
   1124     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
   1125     u_fclose(myFile);\
   1126     u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
   1127     cNumPrinted = sprintf(buffer, cFormat, cValue);\
   1128     if (strcmp(buffer, compBuffer) != 0) {\
   1129         log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
   1130     }\
   1131     if (cNumPrinted != uNumPrinted) {\
   1132         log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
   1133     }\
   1134     if (buffer[uNumPrinted+1] != '*') {\
   1135         log_err("%" uFormat " too much stored\n");\
   1136     }\
   1137 
   1138 #if !UCONFIG_NO_FORMATTING
   1139 static void TestFprintfFormat(void) {
   1140     static const UChar abcUChars[] = {0x61,0x62,0x63,0};
   1141     static const char abcChars[] = "abc";
   1142     UChar uBuffer[256];
   1143     char buffer[256];
   1144     char compBuffer[256];
   1145     int32_t uNumPrinted;
   1146     int32_t cNumPrinted;
   1147     UFILE *myFile;
   1148 
   1149     TestFPrintFormat("%8S", abcUChars, "%8s", abcChars);
   1150     TestFPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
   1151     TestFPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
   1152 
   1153     TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
   1154     TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
   1155     TestFPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
   1156 
   1157     TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
   1158     TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
   1159 
   1160     TestFPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
   1161     TestFPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
   1162 
   1163     TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
   1164     TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
   1165     TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
   1166     TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
   1167     TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
   1168     TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
   1169     TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
   1170     TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
   1171     TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
   1172 
   1173 /*    TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
   1174     TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
   1175     TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
   1176     TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
   1177     TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
   1178     TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
   1179     TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
   1180     TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
   1181     TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
   1182     TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
   1183 
   1184     TestFPrintFormat("%8x", 123456, "%8x", 123456);
   1185     TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
   1186     TestFPrintFormat("%08x", 123456, "%08x", 123456);
   1187 
   1188     TestFPrintFormat("%8X", 123456, "%8X", 123456);
   1189     TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
   1190     TestFPrintFormat("%08X", 123456, "%08X", 123456);
   1191     TestFPrintFormat("%#x", 123456, "%#x", 123456);
   1192     TestFPrintFormat("%#x", -123456, "%#x", -123456);
   1193 
   1194     TestFPrintFormat("%8o", 123456, "%8o", 123456);
   1195     TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
   1196     TestFPrintFormat("%08o", 123456, "%08o", 123456);
   1197     TestFPrintFormat("%#o", 123, "%#o", 123);
   1198     TestFPrintFormat("%#o", -123, "%#o", -123);
   1199 
   1200     TestFPrintFormat("%8u", 123456, "%8u", 123456);
   1201     TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
   1202     TestFPrintFormat("%08u", 123456, "%08u", 123456);
   1203     TestFPrintFormat("%8u", -123456, "%8u", -123456);
   1204     TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
   1205     TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
   1206     TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
   1207     TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
   1208 
   1209     TestFPrintFormat("%8d", 123456, "%8d", 123456);
   1210     TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
   1211     TestFPrintFormat("%08d", 123456, "%08d", 123456);
   1212     TestFPrintFormat("% d", 123456, "% d", 123456);
   1213     TestFPrintFormat("% d", -123456, "% d", -123456);
   1214 
   1215     TestFPrintFormat("%8i", 123456, "%8i", 123456);
   1216     TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
   1217     TestFPrintFormat("%08i", 123456, "%08i", 123456);
   1218 
   1219     log_verbose("Get really crazy with the formatting.\n");
   1220 
   1221     TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
   1222     TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
   1223     TestFPrintFormat("%#12x", 123, "%#12x", 123);
   1224     TestFPrintFormat("%#12x", -123, "%#12x", -123);
   1225 
   1226     TestFPrintFormat("%-+12d", 123,  "%-+12d", 123);
   1227     TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
   1228     TestFPrintFormat("%- 12d", 123,  "%- 12d", 123);
   1229     TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
   1230     TestFPrintFormat("%+12d", 123,   "%+12d", 123);
   1231     TestFPrintFormat("%+12d", -123,  "%+12d", -123);
   1232     TestFPrintFormat("% 12d", 123,   "% 12d", 123);
   1233     TestFPrintFormat("% 12d", -123,  "% 12d", -123);
   1234     TestFPrintFormat("%12d", 123,    "%12d", 123);
   1235     TestFPrintFormat("%12d", -123,   "%12d", -123);
   1236     TestFPrintFormat("%.12d", 123,   "%.12d", 123);
   1237     TestFPrintFormat("%.12d", -123,  "%.12d", -123);
   1238 
   1239     TestFPrintFormat("%-+12.1f", 1.234,  "%-+12.1f", 1.234);
   1240     TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
   1241     TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
   1242     TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
   1243     TestFPrintFormat("%+12.1f", 1.234,   "%+12.1f", 1.234);
   1244     TestFPrintFormat("%+12.1f", -1.234,  "%+12.1f", -1.234);
   1245     TestFPrintFormat("% 12.1f", 1.234,   "% 12.1f", 1.234);
   1246     TestFPrintFormat("% 12.1f", -1.234,  "% 12.1f", -1.234);
   1247     TestFPrintFormat("%12.1f", 1.234,    "%12.1f", 1.234);
   1248     TestFPrintFormat("%12.1f", -1.234,   "%12.1f", -1.234);
   1249     TestFPrintFormat("%.2f", 1.234,      "%.2f", 1.234);
   1250     TestFPrintFormat("%.2f", -1.234,     "%.2f", -1.234);
   1251     TestFPrintFormat("%3f", 1.234,       "%3f", 1.234);
   1252     TestFPrintFormat("%3f", -1.234,      "%3f", -1.234);
   1253 
   1254     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
   1255     /* Reinitialize the buffer to verify null termination works. */
   1256     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1257     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
   1258 
   1259     uNumPrinted = u_fprintf(myFile, "%d % d %d", -1234, 1234, 1234);
   1260     u_fclose(myFile);
   1261     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1262     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1263     u_fclose(myFile);
   1264     u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
   1265     cNumPrinted = sprintf(buffer, "%d % d %d", -1234, 1234, 1234);
   1266     if (strcmp(buffer, compBuffer) != 0) {
   1267         log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);
   1268     }
   1269     if (cNumPrinted != uNumPrinted) {
   1270         log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);
   1271     }
   1272     if (buffer[uNumPrinted+1] != '*') {
   1273         log_err("%%d %% d %%d too much stored\n");
   1274     }
   1275 }
   1276 #endif
   1277 
   1278 #undef TestFPrintFormat
   1279 
   1280 #if !UCONFIG_NO_FORMATTING
   1281 static void TestFScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
   1282     UFILE *myFile;
   1283     UChar uBuffer[256];
   1284     char buffer[256];
   1285     char compBuffer[256];
   1286     int32_t uNumScanned;
   1287     int32_t cNumScanned;
   1288 
   1289     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
   1290     if (myFile == NULL) {
   1291         log_err("Can't write test file for %s.\n", format);
   1292         return;
   1293     }
   1294     /* Reinitialize the buffer to verify null termination works. */
   1295     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1296     uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
   1297     memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
   1298     buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0;
   1299 
   1300     u_fprintf(myFile, "%S", uValue);
   1301     u_fclose(myFile);
   1302     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1303     uNumScanned = u_fscanf(myFile, format, uBuffer);
   1304     u_fclose(myFile);
   1305     if (expectedToPass) {
   1306         u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
   1307         cNumScanned = sscanf(cValue, format, buffer);
   1308         if (strncmp(buffer, compBuffer, sizeof(buffer)/sizeof(*buffer)) != 0) {
   1309             log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
   1310         }
   1311         if (cNumScanned != uNumScanned) {
   1312             log_err("%s number printed Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
   1313         }
   1314         if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
   1315             log_err("%s too much stored\n", format);
   1316         }
   1317     }
   1318     else {
   1319         if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
   1320             log_err("%s too much stored on a failure\n", format);
   1321         }
   1322     }
   1323 }
   1324 #endif
   1325 
   1326 #if !UCONFIG_NO_FORMATTING
   1327 static void TestFScanset(void) {
   1328     static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
   1329     static const char abcChars[] = "abccdefg";
   1330 
   1331     TestFScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
   1332     TestFScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
   1333 
   1334     TestFScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
   1335     TestFScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
   1336 
   1337     TestFScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
   1338     TestFScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
   1339 
   1340     TestFScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
   1341     TestFScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
   1342 
   1343     TestFScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
   1344     TestFScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
   1345     TestFScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
   1346     TestFScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
   1347     TestFScanSetFormat("%[^e]  ", abcUChars, abcChars, TRUE);
   1348 
   1349     TestFScanSetFormat("%1[ab]  ", abcUChars, abcChars, TRUE);
   1350     TestFScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
   1351 
   1352     TestFScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
   1353 
   1354     /* Extra long string for testing */
   1355     TestFScanSetFormat("                                                                                                                         %[qrst]",
   1356         abcUChars, abcChars, TRUE);
   1357 
   1358     TestFScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
   1359 
   1360     /* Bad format */
   1361     TestFScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
   1362     TestFScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
   1363     TestFScanSetFormat("%[a", abcUChars, abcChars, FALSE);
   1364     /* The following is not deterministic on Windows */
   1365 /*    TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
   1366 
   1367     /* TODO: Need to specify precision with a "*" */
   1368 }
   1369 #endif
   1370 #if !UCONFIG_NO_FORMATTING
   1371 static void TestBadFScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
   1372     UFILE *myFile;
   1373     UChar uBuffer[256];
   1374     int32_t uNumScanned;
   1375 
   1376     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
   1377     if (myFile == NULL) {
   1378         log_err("Can't write test file for %s.\n", format);
   1379         return;
   1380     }
   1381     /* Reinitialize the buffer to verify null termination works. */
   1382     u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
   1383     uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
   1384 
   1385     u_fprintf(myFile, "%S", uValue);
   1386     u_fclose(myFile);
   1387     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1388     uNumScanned = u_fscanf(myFile, format, uBuffer);
   1389     u_fclose(myFile);
   1390     if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
   1391         log_err("%s too much stored on a failure\n", format);
   1392     }
   1393 }
   1394 #endif
   1395 #if !UCONFIG_NO_FORMATTING
   1396 static void TestBadScanfFormat(void) {
   1397     static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
   1398     static const char abcChars[] = "abccdefg";
   1399 
   1400     TestBadFScanfFormat("%[]  ", abcUChars, abcChars);
   1401 }
   1402 #endif
   1403 #if !UCONFIG_NO_FORMATTING
   1404 static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
   1405     UChar uBuffer[256];
   1406     UChar uBuffer2[256];
   1407     va_list ap;
   1408     int32_t count;
   1409     UFILE *myFile;
   1410 
   1411     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "UTF-8");
   1412     if (!myFile) {
   1413         log_err("Test file can't be opened\n");
   1414         return;
   1415     }
   1416 
   1417     va_start(ap, format);
   1418     count = u_vfprintf(myFile, format, ap);
   1419     va_end(ap);
   1420 
   1421     u_fclose(myFile);
   1422 
   1423 
   1424     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", "UTF-8");
   1425     if (!myFile) {
   1426         log_err("Test file can't be opened\n");
   1427         return;
   1428     }
   1429     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1430     u_uastrcpy(uBuffer2, expectedResult);
   1431     if (u_strcmp(uBuffer, uBuffer2) != 0) {
   1432         log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
   1433     }
   1434     u_fclose(myFile);
   1435 
   1436 
   1437     myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
   1438     if (!myFile) {
   1439         log_err("Test file can't be opened\n");
   1440         return;
   1441     }
   1442     u_uastrcpy(uBuffer, format);
   1443 
   1444     va_start(ap, format);
   1445     count = u_vfprintf_u(myFile, uBuffer, ap);
   1446     va_end(ap);
   1447 
   1448     u_fclose(myFile);
   1449 
   1450 
   1451     myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
   1452     if (!myFile) {
   1453         log_err("Test file can't be opened\n");
   1454         return;
   1455     }
   1456     u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
   1457     u_uastrcpy(uBuffer2, expectedResult);
   1458     if (u_strcmp(uBuffer, uBuffer2) != 0) {
   1459         log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
   1460     }
   1461     u_fclose(myFile);
   1462 }
   1463 
   1464 static void TestVargs(void) {
   1465     Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
   1466 }
   1467 #endif
   1468 
   1469 static void TestUnicodeFormat(void)
   1470 {
   1471 #if !UCONFIG_NO_FORMATTING
   1472     /* Make sure that invariant conversion doesn't happen on the _u formats. */
   1473     UChar myUString[256];
   1474     UFILE *myFile;
   1475     static const UChar TEST_STR[] = { 0x03BC, 0x0025, 0x0024, 0};
   1476     static const UChar PERCENT_S[] = { 0x03BC, 0x0025, 0x0053, 0};
   1477 
   1478     u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
   1479 
   1480     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
   1481     if (!myFile) {
   1482         log_err("Test file can't be opened\n");
   1483         return;
   1484     }
   1485     u_fprintf_u(myFile, PERCENT_S, TEST_STR);
   1486     u_fclose(myFile);
   1487 
   1488     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
   1489     if (!myFile) {
   1490         log_err("Test file can't be opened\n");
   1491         return;
   1492     }
   1493     u_fscanf_u(myFile, PERCENT_S, myUString);
   1494     u_fclose(myFile);
   1495     if (u_strcmp(TEST_STR, myUString) != 0) {
   1496         log_err("u_fscanf_u doesn't work.\n");
   1497     }
   1498 #endif
   1499 }
   1500 
   1501 static void TestFileWriteRetval(const char * a_pszEncoding) {
   1502     UChar * buffer;
   1503     UFILE * myFile;
   1504     int32_t count;
   1505     int32_t expected = 10000; /* test with large data to test internal buffer looping */
   1506     UChar   testChar = 0xBEEF;
   1507 
   1508     if (!*a_pszEncoding || 0 == strcmp(a_pszEncoding, "ASCII")) {
   1509         testChar = 0x65; /* 'A' - otherwise read test will fail */
   1510     }
   1511 
   1512     buffer = (UChar*) malloc(expected * sizeof(UChar));
   1513     if (!buffer) {
   1514         log_err("Out of memory\n");
   1515         return;
   1516     }
   1517 
   1518     /* write */
   1519     myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, a_pszEncoding);
   1520     if (!myFile) {
   1521         free(buffer);
   1522         log_err("Test file can't be opened for write\n");
   1523         return;
   1524     }
   1525     u_memset(buffer, testChar, expected);
   1526     count = u_file_write(buffer, expected, myFile);
   1527     u_fclose(myFile);
   1528     if (count != expected) {
   1529         free(buffer);
   1530         log_err("u_file_write returned incorrect number of characters written\n");
   1531         return;
   1532     }
   1533 
   1534     free(buffer);
   1535     buffer = NULL;
   1536 
   1537     /* read */
   1538     myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, a_pszEncoding);
   1539     if (!myFile) {
   1540         log_err("Test file can't be opened for read\n");
   1541         return;
   1542     }
   1543     for (count = 0; count < expected; ++count) {
   1544         UChar gotChar = u_fgetc(myFile);
   1545 	if(gotChar != testChar) {
   1546             log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar, testChar);
   1547             u_fclose(myFile);
   1548             return;
   1549         }
   1550     }
   1551     if (u_fgetc(myFile) != U_EOF) {
   1552         log_err("u_fgetc did not return expected EOF\n");
   1553         u_fclose(myFile);
   1554         return;
   1555         }
   1556     u_fclose(myFile);
   1557 }
   1558 
   1559 static void TestFileWriteRetvalUTF16(void) {
   1560     TestFileWriteRetval("UTF-16");
   1561 }
   1562 
   1563 static void TestFileWriteRetvalUTF8(void) {
   1564     TestFileWriteRetval("UTF-8");
   1565 }
   1566 
   1567 static void TestFileWriteRetvalASCII(void) {
   1568     TestFileWriteRetval("ASCII");
   1569 }
   1570 
   1571 static void TestFileWriteRetvalNONE(void) {
   1572     TestFileWriteRetval("");
   1573 }
   1574 
   1575 U_CFUNC void
   1576 addFileTest(TestNode** root) {
   1577 #if !UCONFIG_NO_FORMATTING
   1578     addTest(root, &TestFile, "file/TestFile");
   1579 #endif
   1580     addTest(root, &StdinBuffering, "file/StdinBuffering");
   1581     addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers");
   1582     addTest(root, &TestFileReadBuffering, "file/TestFileReadBuffering");
   1583     addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount");
   1584     addTest(root, &TestfgetsNewLineHandling, "file/TestfgetsNewLineHandling");
   1585     addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount");
   1586     addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering");
   1587     addTest(root, &TestCodepage, "file/TestCodepage");
   1588     addTest(root, &TestCodepageFlush, "file/TestCodepageFlush");
   1589     addTest(root, &TestFileWriteRetvalUTF16, "file/TestFileWriteRetvalUTF16");
   1590     addTest(root, &TestFileWriteRetvalUTF8, "file/TestFileWriteRetvalUTF8");
   1591     addTest(root, &TestFileWriteRetvalASCII, "file/TestFileWriteRetvalASCII");
   1592     addTest(root, &TestFileWriteRetvalNONE, "file/TestFileWriteRetvalNONE");
   1593 #if !UCONFIG_NO_FORMATTING
   1594     addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale");
   1595     addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
   1596     addTest(root, &TestFScanset, "file/TestFScanset");
   1597     addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
   1598     addTest(root, &TestBadScanfFormat, "file/TestBadScanfFormat");
   1599     addTest(root, &TestVargs, "file/TestVargs");
   1600     addTest(root, &TestUnicodeFormat, "file/TestUnicodeFormat");
   1601 #endif
   1602 }
   1603