2 **********************************************************************
3 * Copyright (C) 2004-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 * tab size: 8 (not used)
11 * created on: 2004apr06
12 * created by: George Rhoten
17 #include "unicode/ustdio.h"
18 #include "unicode/ustring.h"
19 #include "unicode/uloc.h"
24 const char *STANDARD_TEST_FILE
= "iotest-c.txt";
26 const char *STANDARD_TEST_LOCALE
= "en_US_POSIX";
29 #if !UCONFIG_NO_FORMATTING
30 static void TestFileFromICU(UFILE
*myFile
) {
32 float myFloat
= -1234.0;
33 int32_t newValuePtr
[1];
34 double newDoubleValuePtr
[1];
36 UChar uStringBuf
[256];
37 char myString
[256] = "";
38 char testBuf
[256] = "";
40 U_STRING_DECL(myStringOrig
, "My-String", 9);
42 U_STRING_INIT(myStringOrig
, "My-String", 9);
43 u_memset(myUString
, 0x2a, UPRV_LENGTHOF(myUString
));
44 u_memset(uStringBuf
, 0x2a, UPRV_LENGTHOF(uStringBuf
));
45 memset(myString
, '*', UPRV_LENGTHOF(myString
));
46 memset(testBuf
, '*', UPRV_LENGTHOF(testBuf
));
49 log_err("Can't write test file.\n");
54 if (sizeof(void *) == 4) {
55 origPtr
= (void *)0xdeadbeef;
56 } else if (sizeof(void *) == 8) {
57 origPtr
= (void *) INT64_C(0x1000200030004000);
58 } else if (sizeof(void *) == 16) {
63 } massiveBigEndianPtr
= {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
64 origPtr
= massiveBigEndianPtr
.ptr
;
66 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
70 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
71 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
72 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
73 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
74 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
75 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
76 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
77 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
78 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
79 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
80 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
81 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
82 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
83 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
84 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
85 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
86 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
87 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
88 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
89 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
92 log_err("Got feof while writing the file.\n");
96 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
97 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
98 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
100 u_fprintf(myFile
, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
101 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
); /* Should be 27 as stated later on. */
104 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
106 if (myFile
== NULL
) {
107 log_err("Can't read test file.");
111 if (u_feof(myFile
)) {
112 log_err("Got feof while reading the file and not at the end of the file.\n");
115 myUString
[0] = u_fgetc(myFile
);
116 if (myUString
[0] != 0x53 /* S */) {
117 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
119 u_fungetc(myUString
[0], myFile
);
120 myUString
[0] = u_fgetc(myFile
);
121 if (myUString
[0] != 0x53 /* S */) {
122 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString
[0]);
124 u_fungetc(myUString
[0], myFile
);
125 myUString
[0] = u_fgetc(myFile
);
126 if (myUString
[0] != 0x53 /* S */) {
127 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString
[0]);
129 u_fungetc(myUString
[0], myFile
);
130 myUString
[0] = u_fgetc(myFile
);
131 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
132 myUString
[2] = u_fgetc(myFile
);
133 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
134 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
136 u_fungetc(myUString
[2], myFile
);
137 u_fungetc(myUString
[1], myFile
);
138 u_fungetc(myUString
[0], myFile
);
143 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
144 if (*n
!= *newValuePtr
) {
145 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
148 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
149 if (*n
!= *newValuePtr
) {
150 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
153 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
154 if (*n
!= *newValuePtr
) {
155 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
158 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
159 if (*n
!= *newValuePtr
) {
160 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
163 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
164 if (*n
!= *newValuePtr
) {
165 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
168 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
169 if (*n
!= *newValuePtr
) {
170 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
172 *newDoubleValuePtr
= -1.0;
173 u_fscanf(myFile
, "Float %%f: %lf\n", newDoubleValuePtr
);
174 if (myFloat
!= *newDoubleValuePtr
) {
175 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
177 *newDoubleValuePtr
= -1.0;
178 u_fscanf(myFile
, "Lowercase float %%e: %le\n", newDoubleValuePtr
);
179 if (myFloat
!= *newDoubleValuePtr
) {
180 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
182 *newDoubleValuePtr
= -1.0;
183 u_fscanf(myFile
, "Uppercase float %%E: %lE\n", newDoubleValuePtr
);
184 if (myFloat
!= *newDoubleValuePtr
) {
185 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr
, myFloat
);
187 *newDoubleValuePtr
= -1.0;
188 u_fscanf(myFile
, "Lowercase float %%g: %lg\n", newDoubleValuePtr
);
189 if (myFloat
!= *newDoubleValuePtr
) {
190 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
192 *newDoubleValuePtr
= -1.0;
193 u_fscanf(myFile
, "Uppercase float %%G: %lG\n", newDoubleValuePtr
);
194 if (myFloat
!= *newDoubleValuePtr
) {
195 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
198 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
199 if (ptr
!= origPtr
) {
200 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
202 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
203 if (*myString
!= 'A') {
204 log_err("%%c Got: %c, Expected: A\n", *myString
);
206 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
207 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
208 log_err("%%C Got: %C, Expected: A\n", *myUString
);
210 u_fscanf(myFile
, "String %%s: %s\n", myString
);
211 if (strcmp(myString
, "My-String")) {
212 log_err("%%s Got: %s, Expected: My String\n", myString
);
214 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
215 if (strcmp(myString
, "(null)")) {
216 log_err("%%s Got: %s, Expected: My String\n", myString
);
218 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
219 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
220 if (strcmp(myString
, "My-String")) {
221 log_err("%%S Got: %S, Expected: My String\n", myUString
);
223 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
224 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
225 if (strcmp(myString
, "(null)")) {
226 log_err("%%S Got: %S, Expected: My String\n", myUString
);
228 *newDoubleValuePtr
= -1.0;
229 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
230 if (myFloat
!= *newDoubleValuePtr
) {
231 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
233 *newDoubleValuePtr
= -1.0;
234 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
235 if (myFloat
!= *newDoubleValuePtr
) {
236 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
239 u_fgets(myUString
, 4, myFile
);
242 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
243 if (strcmp(myString
, "\t\n") != 0) {
244 log_err("u_fgets got \"%s\"\n", myString
);
247 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
248 log_err("u_fgets did not return myUString\n");
250 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
251 if (strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
252 log_err("u_fgets got \"%s\"\n", myString
);
255 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
256 log_err("u_fgets did not return myUString\n");
258 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
259 if (strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
260 log_err("u_fgets got \"%s\"\n", myString
);
263 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
264 log_err("u_fgets did not return myUString\n");
266 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
267 if (strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
268 log_err("u_fgets got \"%s\"\n", myString
);
271 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
272 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
275 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
276 log_err("u_fgets did not return myUString\n");
278 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
279 if (strcmp(myString
, "") != 0) {
280 log_err("u_fgets got \"%s\"\n", myString
);
283 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
284 log_err("u_fgets did not return myUString\n");
286 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
287 if (strcmp(myString
, "\t") != 0) {
288 log_err("u_fgets got \"%s\"\n", myString
);
291 u_austrncpy(myString
, u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
),
292 UPRV_LENGTHOF(myUString
));
293 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
294 log_err("u_fgets got \"%s\"\n", myString
);
297 if (u_feof(myFile
)) {
298 log_err("Got feof while reading the file and not at the end of the file.\n");
300 u_austrncpy(myString
, u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
),
301 UPRV_LENGTHOF(myUString
));
302 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
303 log_err("u_fgets got \"%s\"\n", myString
);
305 if (!u_feof(myFile
)) {
306 log_err("Did not get feof while reading the end of the file.\n");
308 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
309 log_err("u_fscanf read data while reading the end of the file.\n");
315 static void TestFile(void) {
317 log_verbose("Testing u_fopen\n");
318 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
));
321 static void TestFinit(void) {
324 log_verbose("Testing u_finit\n");
325 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
326 TestFileFromICU(u_finit(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
327 fclose(standardFile
);
330 static void TestFadopt(void) {
333 log_verbose("Testing u_fadopt\n");
334 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
335 TestFileFromICU(u_fadopt(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
339 static void StdinBuffering(void) {
343 UFILE
*uStdIn
= NULL
;
344 UFILE
*uStdOut
= NULL
;
345 uStdIn
= u_finit(stdin
, NULL
, NULL
);
346 uStdOut
= u_finit(stdout
, NULL
, NULL
);
352 u_fgets(buff
, UPRV_LENGTHOF(buff
), uStdIn
);
353 u_fprintf(uStdOut
, "%S\n", buff
);
354 u_fscanf(uStdIn
, "%d", &num
);
355 u_fprintf(uStdOut
, "%d\n", num
);
356 u_fscanf(uStdIn
, "%d", &num
);
357 u_fprintf(uStdOut
, "%d\n", num
);
359 log_verbose("Test disabled because it requires user interaction");
363 static void TestCodepageAndLocale(void) {
364 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
365 if (myFile
== NULL
) {
366 log_err("Can't write test file.\n");
369 if (u_fgetcodepage(myFile
) == NULL
370 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
372 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
373 u_fgetcodepage(myFile
), ucnv_getDefaultName());
375 #if !UCONFIG_NO_FORMATTING
376 if (u_fgetlocale(myFile
) == NULL
377 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
379 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
380 u_fgetlocale(myFile
), uloc_getDefault());
385 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
386 if (u_fgetcodepage(myFile
) == NULL
387 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
389 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
390 u_fgetcodepage(myFile
));
392 #if !UCONFIG_NO_FORMATTING
393 if (u_fgetlocale(myFile
) == NULL
394 || strcmp(u_fgetlocale(myFile
), "es") != 0)
396 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
397 u_fgetlocale(myFile
), "es");
402 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
403 if (u_fgetcodepage(myFile
) == NULL
404 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
406 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
407 u_fgetcodepage(myFile
));
409 #if !UCONFIG_NO_FORMATTING
410 if (u_fgetlocale(myFile
) == NULL
411 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
413 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
414 u_fgetlocale(myFile
), uloc_getDefault());
419 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
420 if (u_fgetcodepage(myFile
) == NULL
421 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
423 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
424 u_fgetcodepage(myFile
));
426 #if !UCONFIG_NO_FORMATTING
427 if (u_fgetlocale(myFile
) == NULL
428 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
430 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
431 u_fgetlocale(myFile
), "zh");
438 static void TestfgetsBuffers(void) {
440 UChar expectedBuffer
[2048];
441 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!";
442 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
443 int32_t expectedSize
= (int32_t)strlen(testStr
);
447 if (myFile
== NULL
) {
448 log_err("Can't write test file.\n");
452 u_fputc(0x3BC, myFile
);
453 if (u_fputc(0x110000, myFile
) != U_EOF
) {
454 log_err("u_fputc should return U_EOF for 0x110000.\n");
456 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
457 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
459 u_fputc(0xFF41, myFile
);
460 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
461 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
462 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
463 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
464 u_file_write(buffer
, expectedSize
, myFile
);
465 u_strcat(expectedBuffer
, buffer
);
469 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
470 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
471 if (u_fgetc(myFile
) != 0x3BC) {
472 log_err("The first character is wrong\n");
474 if (u_fgetc(myFile
) != 0xFF41) {
475 log_err("The second character is wrong\n");
477 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != buffer
) {
478 log_err("Didn't get the buffer back\n");
481 readSize
= u_strlen(buffer
);
482 if (readSize
!= expectedSize
*repetitions
) {
483 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
485 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
486 log_err("u_fgets wrote too much data\n");
488 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
489 log_err("Did get expected string back\n");
491 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
492 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
496 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
498 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
500 u_fputc(0x3BC, myFile
);
501 u_fputc(0xFF41, myFile
);
502 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
503 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
504 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
505 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
506 u_file_write(buffer
, expectedSize
, myFile
);
507 u_strcat(expectedBuffer
, buffer
);
511 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
512 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
513 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
514 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
516 if (u_fgetc(myFile
) != 0x3BC) {
517 log_err("The first character is wrong\n");
519 if (u_fgetc(myFile
) != 0xFF41) {
520 log_err("The second character is wrong\n");
522 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != buffer
) {
523 log_err("Didn't get the buffer back\n");
526 readSize
= u_strlen(buffer
);
527 if (readSize
!= expectedSize
*repetitions
) {
528 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
530 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
531 log_err("u_fgets wrote too much data\n");
533 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
534 log_err("Did get expected string back\n");
539 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
541 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
543 u_fputc(0xFF41, myFile
);
544 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
545 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
546 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
547 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
548 u_file_write(buffer
, expectedSize
, myFile
);
549 u_strcat(expectedBuffer
, buffer
);
553 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
554 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
555 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
556 log_err("Didn't get the buffer back\n");
559 readSize
= u_strlen(buffer
);
561 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
563 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
564 log_err("Did get expected string back\n");
566 if (buffer
[2] != 0xBEEF) {
567 log_err("u_fgets wrote too much data\n");
573 static void TestFileReadBuffering(void) {
575 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
579 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
580 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
581 u_file_write(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
585 u_memset(buffer
, 0xDEAD, UPRV_LENGTHOF(buffer
));
586 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
587 how_many
= u_file_read(buffer
, 1024, myFile
);
588 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
589 log_err("u_file_read read too much or not enough data\n");
594 static void TestfgetsLineCount(void) {
596 UChar expectedBuffer
[2048];
597 char charBuffer
[2048];
598 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!";
599 UFILE
*myFile
= NULL
;
600 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
601 int32_t expectedSize
= (int32_t)strlen(testStr
);
603 int32_t nlRepetitions
;
605 if (stdFile
== NULL
) {
606 log_err("Can't write test file.\n");
609 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
611 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
612 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
613 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
614 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
619 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
620 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
623 char *returnedCharBuffer
;
624 UChar
*returnedUCharBuffer
;
626 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
627 returnedCharBuffer
= fgets(charBuffer
, UPRV_LENGTHOF(charBuffer
), stdFile
);
628 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
630 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
631 /* Both returned NULL. stop. */
634 if (returnedCharBuffer
!= charBuffer
) {
635 log_err("Didn't get the charBuffer back\n");
638 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
639 if (returnedUCharBuffer
!= buffer
) {
640 log_err("Didn't get the buffer back\n");
643 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
644 log_err("buffers are different\n");
646 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
647 log_err("u_fgets wrote too much\n");
654 static void TestfgetsNewLineHandling(void) {
656 static const UChar testUStr
[][16] = {
673 {0x00A0, 0x000D, 0x000A, 0},
675 {0xd800, 0xdfff, 0x000D, 0},
688 UFILE
*myFile
= NULL
;
691 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
692 if (myFile
== NULL
) {
693 log_err("Can't write test file.\n");
696 for (lineIdx
= 0; lineIdx
< UPRV_LENGTHOF(testUStr
); lineIdx
++) {
697 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
701 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
703 for (lineIdx
= 0; lineIdx
< UPRV_LENGTHOF(testUStr
); lineIdx
++) {
704 UChar
*returnedUCharBuffer
;
706 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
707 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
709 if (!returnedUCharBuffer
) {
710 /* Returned NULL. stop. */
713 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
714 log_err("buffers are different at index = %d\n", lineIdx
);
716 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
717 log_err("u_fgets wrote too much\n");
720 if (lineIdx
!= UPRV_LENGTHOF(testUStr
)) {
721 log_err("u_fgets read too much\n");
723 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != NULL
) {
724 log_err("u_file_write wrote too much\n");
729 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
731 UChar expectedBuffer
[64];
732 int32_t lineLen
= strlen(line
);
733 UChar
*returnedUCharBuffer
;
735 UFILE
*myFile
= NULL
;
736 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
738 if (stdFile
== NULL
) {
739 log_err("Can't write test file.\n");
742 /* Write a prefix line and then write a bunch of lines */
743 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
744 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
745 fwrite(line
, lineLen
, 1, stdFile
);
749 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
750 if (myFile
== NULL
) {
751 log_err("Can't read test file.\n");
755 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
756 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
757 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
758 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
759 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
763 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
764 for (repetitions
= 0; ; repetitions
++) {
765 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
766 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
768 if (!returnedUCharBuffer
) {
769 /* returned NULL. stop. */
772 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
773 log_err("buffers are different at count %d\n", repetitions
);
775 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
776 log_err("u_fgets wrote too much\n");
779 if (repetitions
!= numRepititions
) {
780 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
785 static void TestfgetsNewLineCount(void) {
786 /* This makes sure that lines are correctly handled between buffer boundaries. */
787 TestLineCount("\n", "\n", 1024); /* Unix newlines */
788 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
789 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
790 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
791 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
792 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
795 static void TestFgetsLineBuffering(void) {
796 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
797 UChar
*returnedUCharBuffer
;
799 UFILE
*myFile
= NULL
;
800 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
802 if (stdFile
== NULL
) {
803 log_err("Can't write test file.\n");
806 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
808 /* Write one very long line */
809 for (repetitions
= 0; repetitions
< (UPRV_LENGTHOF(buffer
)*2); repetitions
++) {
810 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
814 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
815 if (myFile
== NULL
) {
816 log_err("Can't read test file.\n");
820 /* Read part of one very long line */
821 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
)-1, myFile
);
822 if (u_strlen(returnedUCharBuffer
) != (UPRV_LENGTHOF(buffer
)-2)) {
823 log_err("Line is wrong length. Got %d. Expected %d.\n",
824 u_strlen(returnedUCharBuffer
), (UPRV_LENGTHOF(buffer
)-2));
826 /* We better not read too much */
827 if (buffer
[UPRV_LENGTHOF(buffer
)-1] != 0xBEEF) {
828 log_err("Too much data was written\n");
835 static void TestCodepage(void) {
836 UFILE
*myFile
= NULL
;
837 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
838 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
839 UChar testBuf
[UPRV_LENGTHOF(strABAccentA
)*2]; /* *2 to see if too much was */
840 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
842 UErrorCode status
= U_ZERO_ERROR
;
844 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
847 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
851 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
852 if (myFile
== NULL
) {
853 log_err("Can't write test file for iso-8859-1.\n");
856 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
857 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
859 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
862 /* Now see what we got wrote */
863 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
864 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
865 log_err("u_fsetcodepage didn't set the codepage\n");
867 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
868 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
869 log_err("The test data was read and written differently!\n");
871 if (retVal
!= u_strlen(strABAccentA
)) {
872 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
876 /* What happens on invalid input? */
877 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
878 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
879 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
881 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
882 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
884 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
885 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
887 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
888 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
889 log_err("The test data wasn't subsituted as expected\n");
893 /* Can't currently swap codepages midstream */
894 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
895 strcpy(convName
, u_fgetcodepage(myFile
));
896 u_file_read(testBuf
, 1, myFile
);
897 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
898 log_err("u_fsetcodepage set the codepage after reading a byte\n");
900 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
901 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
902 log_err("u_fsetcodepage changed the codepages after writing data\n");
904 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
905 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
908 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
909 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
910 log_err("The test data was read and written differently!\n");
912 if (retVal
!= u_strlen(strABAccentA
)) {
913 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
919 static void TestCodepageFlush(void) {
920 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING
921 log_verbose("Skipping, legacy conversion or formatting is disabled.");
923 UChar utf16String
[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
926 const char *enc
= "IBM-1388"; /* GBK EBCDIC stateful */
927 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, enc
);
932 if (myFile
== NULL
) {
933 log_err("Can't write test file %s\n", STANDARD_TEST_FILE
);
937 u_fprintf(myFile
, "%S", utf16String
);
940 /* now read it back */
941 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
942 if (myCFile
== NULL
) {
943 log_err("Can't read test file.");
947 inLen
= fread(inBuf
, 1, 200, myCFile
);
951 log_err("Failed during read of test file.");
955 /* check if shift in and out */
956 for(i
=0;i
<(int32_t)inLen
;i
++) {
957 if(inBuf
[i
]==0x0E) { /* SO */
959 } else if(inBuf
[i
]==0x0F) { /* SI */
965 log_err("Err: shift was unchanged\n");
966 } else if(shift
==1) {
967 log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
968 } else if(shift
==-1) {
969 log_verbose("OK: Shifted in (SI, 0x0F)\n");
973 log_err("Expected 12 bytes, read %d\n", inLen
);
975 log_verbose("OK: read %d bytes\n", inLen
);
982 #if !UCONFIG_NO_FORMATTING
983 static void TestFilePrintCompatibility(void) {
984 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, NULL
);
988 static const UChar emptyStr
[] = {0};
989 char readBuf
[512] = "";
990 char testBuf
[512] = "";
993 if (myFile
== NULL
) {
994 log_err("Can't write test file.\n");
997 #if !UCONFIG_NO_FORMATTING
998 if (strcmp(u_fgetlocale(myFile
), STANDARD_TEST_LOCALE
) != 0) {
999 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
1003 /* Compare against C API compatibility */
1004 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1005 u_fprintf(myFile
, "%x ", num
);
1006 u_fprintf(myFile
, "%X ", num
);
1007 u_fprintf(myFile
, "%o ", num
);
1008 u_fprintf(myFile
, "%d ", num
);
1009 u_fprintf(myFile
, "%i ", num
);
1010 u_fprintf(myFile
, "%f ", (double)num
);
1011 /* u_fprintf(myFile, "%e ", (double)num);
1012 u_fprintf(myFile, "%E ", (double)num);*/
1013 u_fprintf(myFile
, "%g ", (double)num
);
1014 u_fprintf(myFile
, "%G", (double)num
);
1015 u_fputs(emptyStr
, myFile
);
1018 u_fprintf_u(myFile
, NEW_LINE
);
1020 for (num
= 0; num
< 0x80; num
++) {
1021 u_fprintf(myFile
, "%c", num
);
1025 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
1026 if (myCFile
== NULL
) {
1027 log_err("Can't read test file.");
1031 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1032 /* Note: gcc on Ubuntu complains if return value of scanf is ignored. */
1033 n
+= fscanf(myCFile
, "%s", readBuf
);
1034 sprintf(testBuf
, "%x", (int)num
);
1035 if (strcmp(readBuf
, testBuf
) != 0) {
1036 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1039 n
+= fscanf(myCFile
, "%s", readBuf
);
1040 sprintf(testBuf
, "%X", (int)num
);
1041 if (strcmp(readBuf
, testBuf
) != 0) {
1042 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1045 n
+= fscanf(myCFile
, "%s", readBuf
);
1046 sprintf(testBuf
, "%o", (int)num
);
1047 if (strcmp(readBuf
, testBuf
) != 0) {
1048 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1051 /* fprintf is not compatible on all platforms e.g. the iSeries */
1052 n
+= fscanf(myCFile
, "%s", readBuf
);
1053 sprintf(testBuf
, "%d", (int)num
);
1054 if (strcmp(readBuf
, testBuf
) != 0) {
1055 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1058 n
+= fscanf(myCFile
, "%s", readBuf
);
1059 sprintf(testBuf
, "%i", (int)num
);
1060 if (strcmp(readBuf
, testBuf
) != 0) {
1061 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1064 n
+= fscanf(myCFile
, "%s", readBuf
);
1065 sprintf(testBuf
, "%f", (double)num
);
1066 if (strcmp(readBuf
, testBuf
) != 0) {
1067 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1070 /* fscanf(myCFile, "%s", readBuf);
1071 sprintf(testBuf, "%e", (double)num);
1072 if (strcmp(readBuf, testBuf) != 0) {
1073 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1076 fscanf(myCFile, "%s", readBuf);
1077 sprintf(testBuf, "%E", (double)num);
1078 if (strcmp(readBuf, testBuf) != 0) {
1079 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1082 n
+= fscanf(myCFile
, "%s", readBuf
);
1083 sprintf(testBuf
, "%g", (double)num
);
1084 if (strcmp(readBuf
, testBuf
) != 0) {
1085 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1088 n
+= fscanf(myCFile
, "%s", readBuf
);
1089 sprintf(testBuf
, "%G", (double)num
);
1090 if (strcmp(readBuf
, testBuf
) != 0) {
1091 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1095 /* Properly eat the newlines */
1096 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1097 n
+= fscanf(myCFile
, "%c", &cVal
);
1098 if (cVal
!= C_NEW_LINE
[num
]) {
1099 log_err("OS newline error\n");
1102 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1103 n
+= fscanf(myCFile
, "%c", &cVal
);
1104 if (cVal
!= C_NEW_LINE
[num
]) {
1105 log_err("ustdio newline error\n");
1109 for (num
= 0; num
< 0x80; num
++) {
1111 n
+= fscanf(myCFile
, "%c", &cVal
);
1113 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1121 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1122 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);\
1123 if (myFile == NULL) {\
1124 log_err("Can't write test file for %s.\n", uFormat);\
1127 /* Reinitialize the buffer to verify null termination works. */\
1128 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
1129 memset(buffer, '*', UPRV_LENGTHOF(buffer));\
1131 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1133 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);\
1134 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);\
1136 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
1137 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1138 if (strcmp(buffer, compBuffer) != 0) {\
1139 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1141 if (cNumPrinted != uNumPrinted) {\
1142 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1144 if (buffer[uNumPrinted+1] != '*') {\
1145 log_err("%" uFormat " too much stored\n");\
1148 #if !UCONFIG_NO_FORMATTING
1149 static void TestFprintfFormat(void) {
1150 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1151 static const char abcChars
[] = "abc";
1154 char compBuffer
[256];
1155 int32_t uNumPrinted
;
1156 int32_t cNumPrinted
;
1159 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1160 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1161 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1163 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1164 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1165 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1167 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1168 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1170 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1171 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1173 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1174 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1175 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1176 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1177 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1178 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1179 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1180 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1181 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1183 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1184 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1185 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1186 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1187 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1188 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1189 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1190 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1191 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1192 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1194 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1195 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1196 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1198 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1199 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1200 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1201 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1202 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1204 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1205 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1206 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1207 TestFPrintFormat("%#o", 123, "%#o", 123);
1208 TestFPrintFormat("%#o", -123, "%#o", -123);
1210 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1211 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1212 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1213 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1214 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1215 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1216 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1217 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1219 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1220 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1221 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1222 TestFPrintFormat("% d", 123456, "% d", 123456);
1223 TestFPrintFormat("% d", -123456, "% d", -123456);
1225 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1226 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1227 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1229 log_verbose("Get really crazy with the formatting.\n");
1231 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1232 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1233 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1234 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1236 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1237 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1238 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1239 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1240 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1241 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1242 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1243 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1244 TestFPrintFormat("%12d", 123, "%12d", 123);
1245 TestFPrintFormat("%12d", -123, "%12d", -123);
1246 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1247 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1249 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1250 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1251 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1252 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1253 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1254 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1255 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1256 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1257 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1258 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1259 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1260 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1261 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1262 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1264 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1265 /* Reinitialize the buffer to verify null termination works. */
1266 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1267 memset(buffer
, '*', UPRV_LENGTHOF(buffer
));
1269 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1271 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1272 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1274 u_austrncpy(compBuffer
, uBuffer
, UPRV_LENGTHOF(uBuffer
));
1275 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1276 if (strcmp(buffer
, compBuffer
) != 0) {
1277 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1279 if (cNumPrinted
!= uNumPrinted
) {
1280 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1282 if (buffer
[uNumPrinted
+1] != '*') {
1283 log_err("%%d %% d %%d too much stored\n");
1288 #undef TestFPrintFormat
1290 #if !UCONFIG_NO_FORMATTING
1291 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1295 char compBuffer
[256];
1296 int32_t uNumScanned
;
1297 int32_t cNumScanned
;
1299 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1300 if (myFile
== NULL
) {
1301 log_err("Can't write test file for %s.\n", format
);
1304 /* Reinitialize the buffer to verify null termination works. */
1305 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1306 uBuffer
[UPRV_LENGTHOF(uBuffer
)-1] = 0;
1307 memset(buffer
, '*', UPRV_LENGTHOF(buffer
));
1308 buffer
[UPRV_LENGTHOF(buffer
)-1] = 0;
1310 u_fprintf(myFile
, "%S", uValue
);
1312 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1313 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1315 if (expectedToPass
) {
1316 u_austrncpy(compBuffer
, uBuffer
, UPRV_LENGTHOF(uBuffer
));
1317 cNumScanned
= sscanf(cValue
, format
, buffer
);
1318 if (strncmp(buffer
, compBuffer
, UPRV_LENGTHOF(buffer
)) != 0) {
1319 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1321 if (cNumScanned
!= uNumScanned
) {
1322 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1324 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1325 log_err("%s too much stored\n", format
);
1329 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1330 log_err("%s too much stored on a failure\n", format
);
1336 #if !UCONFIG_NO_FORMATTING
1337 static void TestFScanset(void) {
1338 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1339 static const char abcChars
[] = "abccdefg";
1341 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1342 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1344 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1345 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1347 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1348 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1350 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1351 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1353 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1354 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1355 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1356 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1357 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1359 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1360 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1362 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1364 /* Extra long string for testing */
1365 TestFScanSetFormat(" %[qrst]",
1366 abcUChars
, abcChars
, TRUE
);
1368 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1371 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1372 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1373 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1374 /* The following is not deterministic on Windows */
1375 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1377 /* TODO: Need to specify precision with a "*" */
1380 #if !UCONFIG_NO_FORMATTING
1381 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1384 int32_t uNumScanned
;
1386 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1387 if (myFile
== NULL
) {
1388 log_err("Can't write test file for %s.\n", format
);
1391 /* Reinitialize the buffer to verify null termination works. */
1392 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1393 uBuffer
[UPRV_LENGTHOF(uBuffer
)-1] = 0;
1395 u_fprintf(myFile
, "%S", uValue
);
1397 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1398 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1400 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1401 log_err("%s too much stored on a failure\n", format
);
1405 #if !UCONFIG_NO_FORMATTING
1406 static void TestBadScanfFormat(void) {
1407 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1408 static const char abcChars
[] = "abccdefg";
1410 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1413 #if !UCONFIG_NO_FORMATTING
1414 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1416 UChar uBuffer2
[256];
1421 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, "UTF-8");
1423 log_err("Test file can't be opened\n");
1427 va_start(ap
, format
);
1428 count
= u_vfprintf(myFile
, format
, ap
);
1429 (void)count
; /* Suppress set but not used warning. */
1435 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, "UTF-8");
1437 log_err("Test file can't be opened\n");
1440 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1441 u_uastrcpy(uBuffer2
, expectedResult
);
1442 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1443 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1448 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1450 log_err("Test file can't be opened\n");
1453 u_uastrcpy(uBuffer
, format
);
1455 va_start(ap
, format
);
1456 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1462 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1464 log_err("Test file can't be opened\n");
1467 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1468 u_uastrcpy(uBuffer2
, expectedResult
);
1469 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1470 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1475 static void TestVargs(void) {
1476 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1480 static void TestUnicodeFormat(void)
1482 #if !UCONFIG_NO_FORMATTING
1483 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1484 UChar myUString
[256];
1486 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1487 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1489 u_memset(myUString
, 0x2a, UPRV_LENGTHOF(myUString
));
1491 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1493 log_err("Test file can't be opened\n");
1496 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1499 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1501 log_err("Test file can't be opened\n");
1504 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1506 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1507 log_err("u_fscanf_u doesn't work.\n");
1512 static void TestFileWriteRetval(const char * a_pszEncoding
) {
1516 int32_t expected
= 10000; /* test with large data to test internal buffer looping */
1517 UChar testChar
= 0xBEEF;
1519 if (!*a_pszEncoding
|| 0 == strcmp(a_pszEncoding
, "ASCII")) {
1520 testChar
= 0x65; /* 'A' - otherwise read test will fail */
1523 buffer
= (UChar
*) malloc(expected
* sizeof(UChar
));
1525 log_err("Out of memory\n");
1530 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, a_pszEncoding
);
1533 log_err("Test file can't be opened for write\n");
1536 u_memset(buffer
, testChar
, expected
);
1537 count
= u_file_write(buffer
, expected
, myFile
);
1539 if (count
!= expected
) {
1541 log_err("u_file_write returned incorrect number of characters written\n");
1549 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, a_pszEncoding
);
1551 log_err("Test file can't be opened for read\n");
1554 for (count
= 0; count
< expected
; ++count
) {
1555 UChar gotChar
= u_fgetc(myFile
);
1556 if(gotChar
!= testChar
) {
1557 log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar
, testChar
);
1562 if (u_fgetc(myFile
) != U_EOF
) {
1563 log_err("u_fgetc did not return expected EOF\n");
1570 static void TestFileWriteRetvalUTF16(void) {
1571 TestFileWriteRetval("UTF-16");
1574 static void TestFileWriteRetvalUTF8(void) {
1575 TestFileWriteRetval("UTF-8");
1578 static void TestFileWriteRetvalASCII(void) {
1579 TestFileWriteRetval("ASCII");
1582 static void TestFileWriteRetvalNONE(void) {
1583 TestFileWriteRetval("");
1587 addFileTest(TestNode
** root
) {
1588 #if !UCONFIG_NO_FORMATTING
1589 addTest(root
, &TestFile
, "file/TestFile");
1590 addTest(root
, &TestFinit
, "file/TestFinit");
1591 addTest(root
, &TestFadopt
, "file/TestFadopt");
1593 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1594 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1595 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1596 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1597 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1598 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1599 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1600 addTest(root
, &TestCodepage
, "file/TestCodepage");
1601 addTest(root
, &TestCodepageFlush
, "file/TestCodepageFlush");
1602 addTest(root
, &TestFileWriteRetvalUTF16
, "file/TestFileWriteRetvalUTF16");
1603 addTest(root
, &TestFileWriteRetvalUTF8
, "file/TestFileWriteRetvalUTF8");
1604 addTest(root
, &TestFileWriteRetvalASCII
, "file/TestFileWriteRetvalASCII");
1605 addTest(root
, &TestFileWriteRetvalNONE
, "file/TestFileWriteRetvalNONE");
1606 #if !UCONFIG_NO_FORMATTING
1607 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1608 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1609 addTest(root
, &TestFScanset
, "file/TestFScanset");
1610 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1611 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1612 addTest(root
, &TestVargs
, "file/TestVargs");
1613 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");