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