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