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