1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 * tab size: 8 (not used)
13 * created on: 2004apr06
14 * created by: George Rhoten
19 #include "unicode/ustdio.h"
20 #include "unicode/ustring.h"
21 #include "unicode/uloc.h"
26 const char *STANDARD_TEST_FILE
= "iotest-c.txt";
28 const char *STANDARD_TEST_LOCALE
= "en_US_POSIX";
31 #if !UCONFIG_NO_FORMATTING
32 static void TestFileFromICU(UFILE
*myFile
) {
34 float myFloat
= -1234.0;
35 int32_t newValuePtr
[1];
36 double newDoubleValuePtr
[1];
38 UChar uStringBuf
[256];
39 char myString
[256] = "";
40 char testBuf
[256] = "";
42 U_STRING_DECL(myStringOrig
, "My-String", 9);
44 U_STRING_INIT(myStringOrig
, "My-String", 9);
45 u_memset(myUString
, 0x2a, UPRV_LENGTHOF(myUString
));
46 u_memset(uStringBuf
, 0x2a, UPRV_LENGTHOF(uStringBuf
));
47 memset(myString
, '*', UPRV_LENGTHOF(myString
));
48 memset(testBuf
, '*', UPRV_LENGTHOF(testBuf
));
51 log_err("Can't write test file.\n");
56 if (sizeof(void *) == 4) {
57 origPtr
= (void *)0xdeadbeef;
58 } else if (sizeof(void *) == 8) {
59 origPtr
= (void *) INT64_C(0x1000200030004000);
60 } else if (sizeof(void *) == 16) {
65 } massiveBigEndianPtr
= {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
66 origPtr
= massiveBigEndianPtr
.ptr
;
68 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
72 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
73 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
74 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
75 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
76 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
77 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
78 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
79 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
80 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
81 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
82 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
83 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
84 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
85 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
86 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
87 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
88 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
89 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
90 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
91 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
94 log_err("Got feof while writing the file.\n");
98 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
99 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
100 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
102 u_fprintf(myFile
, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
103 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
); /* Should be 27 as stated later on. */
106 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
108 if (myFile
== NULL
) {
109 log_err("Can't read test file.");
113 if (u_feof(myFile
)) {
114 log_err("Got feof while reading the file and not at the end of the file.\n");
117 myUString
[0] = u_fgetc(myFile
);
118 if (myUString
[0] != 0x53 /* S */) {
119 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
121 u_fungetc(myUString
[0], myFile
);
122 myUString
[0] = u_fgetc(myFile
);
123 if (myUString
[0] != 0x53 /* S */) {
124 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString
[0]);
126 u_fungetc(myUString
[0], myFile
);
127 myUString
[0] = u_fgetc(myFile
);
128 if (myUString
[0] != 0x53 /* S */) {
129 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString
[0]);
131 u_fungetc(myUString
[0], myFile
);
132 myUString
[0] = u_fgetc(myFile
);
133 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
134 myUString
[2] = u_fgetc(myFile
);
135 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
136 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
138 u_fungetc(myUString
[2], myFile
);
139 u_fungetc(myUString
[1], myFile
);
140 u_fungetc(myUString
[0], myFile
);
145 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
146 if (*n
!= *newValuePtr
) {
147 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
150 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
151 if (*n
!= *newValuePtr
) {
152 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
155 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
156 if (*n
!= *newValuePtr
) {
157 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
160 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
161 if (*n
!= *newValuePtr
) {
162 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
165 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
166 if (*n
!= *newValuePtr
) {
167 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
170 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
171 if (*n
!= *newValuePtr
) {
172 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
174 *newDoubleValuePtr
= -1.0;
175 u_fscanf(myFile
, "Float %%f: %lf\n", newDoubleValuePtr
);
176 if (myFloat
!= *newDoubleValuePtr
) {
177 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
179 *newDoubleValuePtr
= -1.0;
180 u_fscanf(myFile
, "Lowercase float %%e: %le\n", newDoubleValuePtr
);
181 if (myFloat
!= *newDoubleValuePtr
) {
182 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
184 *newDoubleValuePtr
= -1.0;
185 u_fscanf(myFile
, "Uppercase float %%E: %lE\n", newDoubleValuePtr
);
186 if (myFloat
!= *newDoubleValuePtr
) {
187 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr
, myFloat
);
189 *newDoubleValuePtr
= -1.0;
190 u_fscanf(myFile
, "Lowercase float %%g: %lg\n", newDoubleValuePtr
);
191 if (myFloat
!= *newDoubleValuePtr
) {
192 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
194 *newDoubleValuePtr
= -1.0;
195 u_fscanf(myFile
, "Uppercase float %%G: %lG\n", newDoubleValuePtr
);
196 if (myFloat
!= *newDoubleValuePtr
) {
197 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
200 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
201 if (ptr
!= origPtr
) {
202 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
204 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
205 if (*myString
!= 'A') {
206 log_err("%%c Got: %c, Expected: A\n", *myString
);
208 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
209 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
210 log_err("%%C Got: %C, Expected: A\n", *myUString
);
212 u_fscanf(myFile
, "String %%s: %s\n", myString
);
213 if (strcmp(myString
, "My-String")) {
214 log_err("%%s Got: %s, Expected: My String\n", myString
);
216 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
217 if (strcmp(myString
, "(null)")) {
218 log_err("%%s Got: %s, Expected: My String\n", myString
);
220 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
221 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
222 if (strcmp(myString
, "My-String")) {
223 log_err("%%S Got: %S, Expected: My String\n", myUString
);
225 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
226 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
227 if (strcmp(myString
, "(null)")) {
228 log_err("%%S Got: %S, Expected: My String\n", myUString
);
230 *newDoubleValuePtr
= -1.0;
231 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
232 if (myFloat
!= *newDoubleValuePtr
) {
233 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
235 *newDoubleValuePtr
= -1.0;
236 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
237 if (myFloat
!= *newDoubleValuePtr
) {
238 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
241 u_fgets(myUString
, 4, myFile
);
244 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
245 if (strcmp(myString
, "\t\n") != 0) {
246 log_err("u_fgets got \"%s\"\n", myString
);
249 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
250 log_err("u_fgets did not return myUString\n");
252 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
253 if (strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
254 log_err("u_fgets got \"%s\"\n", myString
);
257 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
258 log_err("u_fgets did not return myUString\n");
260 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
261 if (strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
262 log_err("u_fgets got \"%s\"\n", myString
);
265 if (u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
) != myUString
) {
266 log_err("u_fgets did not return myUString\n");
268 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
269 if (strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
270 log_err("u_fgets got \"%s\"\n", myString
);
273 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
274 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
277 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
278 log_err("u_fgets did not return myUString\n");
280 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
281 if (strcmp(myString
, "") != 0) {
282 log_err("u_fgets got \"%s\"\n", myString
);
285 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
286 log_err("u_fgets did not return myUString\n");
288 u_austrncpy(myString
, myUString
, UPRV_LENGTHOF(myUString
));
289 if (strcmp(myString
, "\t") != 0) {
290 log_err("u_fgets got \"%s\"\n", myString
);
293 u_austrncpy(myString
, u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
),
294 UPRV_LENGTHOF(myUString
));
295 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
296 log_err("u_fgets got \"%s\"\n", myString
);
299 if (u_feof(myFile
)) {
300 log_err("Got feof while reading the file and not at the end of the file.\n");
302 u_austrncpy(myString
, u_fgets(myUString
, UPRV_LENGTHOF(myUString
), myFile
),
303 UPRV_LENGTHOF(myUString
));
304 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
305 log_err("u_fgets got \"%s\"\n", myString
);
307 if (!u_feof(myFile
)) {
308 log_err("Did not get feof while reading the end of the file.\n");
310 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
311 log_err("u_fscanf read data while reading the end of the file.\n");
317 static void TestFile(void) {
319 log_verbose("Testing u_fopen\n");
320 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
));
323 static void TestFinit(void) {
326 log_verbose("Testing u_finit\n");
327 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
328 TestFileFromICU(u_finit(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
329 fclose(standardFile
);
332 static void TestFadopt(void) {
335 log_verbose("Testing u_fadopt\n");
336 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
337 TestFileFromICU(u_fadopt(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
341 static void StdinBuffering(void) {
345 UFILE
*uStdIn
= NULL
;
346 UFILE
*uStdOut
= NULL
;
347 uStdIn
= u_finit(stdin
, NULL
, NULL
);
348 uStdOut
= u_finit(stdout
, NULL
, NULL
);
354 u_fgets(buff
, UPRV_LENGTHOF(buff
), uStdIn
);
355 u_fprintf(uStdOut
, "%S\n", buff
);
356 u_fscanf(uStdIn
, "%d", &num
);
357 u_fprintf(uStdOut
, "%d\n", num
);
358 u_fscanf(uStdIn
, "%d", &num
);
359 u_fprintf(uStdOut
, "%d\n", num
);
361 log_verbose("Test disabled because it requires user interaction");
365 static void TestCodepageAndLocale(void) {
366 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
367 if (myFile
== NULL
) {
368 log_err("Can't write test file.\n");
371 if (u_fgetcodepage(myFile
) == NULL
372 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
374 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
375 u_fgetcodepage(myFile
), ucnv_getDefaultName());
377 #if !UCONFIG_NO_FORMATTING
378 if (u_fgetlocale(myFile
) == NULL
379 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
381 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
382 u_fgetlocale(myFile
), uloc_getDefault());
387 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
388 if (u_fgetcodepage(myFile
) == NULL
389 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
391 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
392 u_fgetcodepage(myFile
));
394 #if !UCONFIG_NO_FORMATTING
395 if (u_fgetlocale(myFile
) == NULL
396 || strcmp(u_fgetlocale(myFile
), "es") != 0)
398 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
399 u_fgetlocale(myFile
), "es");
404 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
405 if (u_fgetcodepage(myFile
) == NULL
406 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
408 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
409 u_fgetcodepage(myFile
));
411 #if !UCONFIG_NO_FORMATTING
412 if (u_fgetlocale(myFile
) == NULL
413 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
415 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
416 u_fgetlocale(myFile
), uloc_getDefault());
421 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
422 if (u_fgetcodepage(myFile
) == NULL
423 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
425 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
426 u_fgetcodepage(myFile
));
428 #if !UCONFIG_NO_FORMATTING
429 if (u_fgetlocale(myFile
) == NULL
430 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
432 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
433 u_fgetlocale(myFile
), "zh");
440 static void TestfgetsBuffers(void) {
442 UChar expectedBuffer
[2048];
443 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!";
444 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
445 int32_t expectedSize
= (int32_t)strlen(testStr
);
449 if (myFile
== NULL
) {
450 log_err("Can't write test file.\n");
454 u_fputc(0x3BC, myFile
);
455 if (u_fputc(0x110000, myFile
) != U_EOF
) {
456 log_err("u_fputc should return U_EOF for 0x110000.\n");
458 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
459 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
461 u_fputc(0xFF41, myFile
);
462 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
463 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
464 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
465 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
466 u_file_write(buffer
, expectedSize
, myFile
);
467 u_strcat(expectedBuffer
, buffer
);
471 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
472 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
473 if (u_fgetc(myFile
) != 0x3BC) {
474 log_err("The first character is wrong\n");
476 if (u_fgetc(myFile
) != 0xFF41) {
477 log_err("The second character is wrong\n");
479 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != buffer
) {
480 log_err("Didn't get the buffer back\n");
483 readSize
= u_strlen(buffer
);
484 if (readSize
!= expectedSize
*repetitions
) {
485 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
487 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
488 log_err("u_fgets wrote too much data\n");
490 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
491 log_err("Did get expected string back\n");
493 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
494 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
498 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
500 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
502 u_fputc(0x3BC, myFile
);
503 u_fputc(0xFF41, myFile
);
504 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
505 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
506 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
507 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
508 u_file_write(buffer
, expectedSize
, myFile
);
509 u_strcat(expectedBuffer
, buffer
);
513 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
514 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
515 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
516 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
518 if (u_fgetc(myFile
) != 0x3BC) {
519 log_err("The first character is wrong\n");
521 if (u_fgetc(myFile
) != 0xFF41) {
522 log_err("The second character is wrong\n");
524 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != buffer
) {
525 log_err("Didn't get the buffer back\n");
528 readSize
= u_strlen(buffer
);
529 if (readSize
!= expectedSize
*repetitions
) {
530 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
532 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
533 log_err("u_fgets wrote too much data\n");
535 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
536 log_err("Did get expected string back\n");
541 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
543 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
545 u_fputc(0xFF41, myFile
);
546 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
547 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
548 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
549 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
550 u_file_write(buffer
, expectedSize
, myFile
);
551 u_strcat(expectedBuffer
, buffer
);
555 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
556 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
557 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
558 log_err("Didn't get the buffer back\n");
561 readSize
= u_strlen(buffer
);
563 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
565 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
566 log_err("Did get expected string back\n");
568 if (buffer
[2] != 0xBEEF) {
569 log_err("u_fgets wrote too much data\n");
575 static void TestFileReadBuffering(void) {
577 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
581 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
582 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
583 u_file_write(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
587 u_memset(buffer
, 0xDEAD, UPRV_LENGTHOF(buffer
));
588 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
589 how_many
= u_file_read(buffer
, 1024, myFile
);
590 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
591 log_err("u_file_read read too much or not enough data\n");
596 static void TestfgetsLineCount(void) {
598 UChar expectedBuffer
[2048];
599 char charBuffer
[2048];
600 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!";
601 UFILE
*myFile
= NULL
;
602 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
603 int32_t expectedSize
= (int32_t)strlen(testStr
);
605 int32_t nlRepetitions
;
607 if (stdFile
== NULL
) {
608 log_err("Can't write test file.\n");
611 u_memset(expectedBuffer
, 0, UPRV_LENGTHOF(expectedBuffer
));
613 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
614 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
615 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
616 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
621 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
622 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
625 char *returnedCharBuffer
;
626 UChar
*returnedUCharBuffer
;
628 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
629 returnedCharBuffer
= fgets(charBuffer
, UPRV_LENGTHOF(charBuffer
), stdFile
);
630 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
632 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
633 /* Both returned NULL. stop. */
636 if (returnedCharBuffer
!= charBuffer
) {
637 log_err("Didn't get the charBuffer back\n");
640 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
641 if (returnedUCharBuffer
!= buffer
) {
642 log_err("Didn't get the buffer back\n");
645 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
646 log_err("buffers are different\n");
648 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
649 log_err("u_fgets wrote too much\n");
656 static void TestfgetsNewLineHandling(void) {
658 static const UChar testUStr
[][16] = {
675 {0x00A0, 0x000D, 0x000A, 0},
677 {0xd800, 0xdfff, 0x000D, 0},
690 UFILE
*myFile
= NULL
;
693 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
694 if (myFile
== NULL
) {
695 log_err("Can't write test file.\n");
698 for (lineIdx
= 0; lineIdx
< UPRV_LENGTHOF(testUStr
); lineIdx
++) {
699 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
703 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
705 for (lineIdx
= 0; lineIdx
< UPRV_LENGTHOF(testUStr
); lineIdx
++) {
706 UChar
*returnedUCharBuffer
;
708 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
709 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
711 if (!returnedUCharBuffer
) {
712 /* Returned NULL. stop. */
715 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
716 log_err("buffers are different at index = %d\n", lineIdx
);
718 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
719 log_err("u_fgets wrote too much\n");
722 if (lineIdx
!= UPRV_LENGTHOF(testUStr
)) {
723 log_err("u_fgets read too much\n");
725 if (u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
) != NULL
) {
726 log_err("u_file_write wrote too much\n");
731 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
733 UChar expectedBuffer
[64];
734 int32_t lineLen
= (int32_t)strlen(line
);
735 UChar
*returnedUCharBuffer
;
737 UFILE
*myFile
= NULL
;
738 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
740 if (stdFile
== NULL
) {
741 log_err("Can't write test file.\n");
744 /* Write a prefix line and then write a bunch of lines */
745 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
746 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
747 fwrite(line
, lineLen
, 1, stdFile
);
751 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
752 if (myFile
== NULL
) {
753 log_err("Can't read test file.\n");
757 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
758 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
759 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
760 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
761 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
765 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
766 for (repetitions
= 0; ; repetitions
++) {
767 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
768 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
), myFile
);
770 if (!returnedUCharBuffer
) {
771 /* returned NULL. stop. */
774 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
775 log_err("buffers are different at count %d\n", repetitions
);
777 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
778 log_err("u_fgets wrote too much\n");
781 if (repetitions
!= numRepititions
) {
782 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
787 static void TestfgetsNewLineCount(void) {
788 /* This makes sure that lines are correctly handled between buffer boundaries. */
789 TestLineCount("\n", "\n", 1024); /* Unix newlines */
790 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
791 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
792 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
793 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
794 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
797 static void TestFgetsLineBuffering(void) {
798 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
799 UChar
*returnedUCharBuffer
;
801 UFILE
*myFile
= NULL
;
802 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
804 if (stdFile
== NULL
) {
805 log_err("Can't write test file.\n");
808 u_memset(buffer
, 0xBEEF, UPRV_LENGTHOF(buffer
));
810 /* Write one very long line */
811 for (repetitions
= 0; repetitions
< (UPRV_LENGTHOF(buffer
)*2); repetitions
++) {
812 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
816 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
817 if (myFile
== NULL
) {
818 log_err("Can't read test file.\n");
822 /* Read part of one very long line */
823 returnedUCharBuffer
= u_fgets(buffer
, UPRV_LENGTHOF(buffer
)-1, myFile
);
824 if (u_strlen(returnedUCharBuffer
) != (UPRV_LENGTHOF(buffer
)-2)) {
825 log_err("Line is wrong length. Got %d. Expected %d.\n",
826 u_strlen(returnedUCharBuffer
), (UPRV_LENGTHOF(buffer
)-2));
828 /* We better not read too much */
829 if (buffer
[UPRV_LENGTHOF(buffer
)-1] != 0xBEEF) {
830 log_err("Too much data was written\n");
837 static void TestCodepage(void) {
838 UFILE
*myFile
= NULL
;
839 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
840 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
841 UChar testBuf
[UPRV_LENGTHOF(strABAccentA
)*2]; /* *2 to see if too much was */
842 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
844 UErrorCode status
= U_ZERO_ERROR
;
846 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
849 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
853 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
854 if (myFile
== NULL
) {
855 log_err("Can't write test file for iso-8859-1.\n");
858 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
859 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
861 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
864 /* Now see what we got wrote */
865 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
866 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
867 log_err("u_fsetcodepage didn't set the codepage\n");
869 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
870 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
871 log_err("The test data was read and written differently!\n");
873 if (retVal
!= u_strlen(strABAccentA
)) {
874 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
878 /* What happens on invalid input? */
879 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
880 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
881 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
883 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
884 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
886 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
887 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
889 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
890 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
891 log_err("The test data wasn't subsituted as expected\n");
895 /* Can't currently swap codepages midstream */
896 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
897 strcpy(convName
, u_fgetcodepage(myFile
));
898 u_file_read(testBuf
, 1, myFile
);
899 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
900 log_err("u_fsetcodepage set the codepage after reading a byte\n");
902 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
903 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
904 log_err("u_fsetcodepage changed the codepages after writing data\n");
906 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
907 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
910 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
911 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
912 log_err("The test data was read and written differently!\n");
914 if (retVal
!= u_strlen(strABAccentA
)) {
915 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
921 static void TestCodepageFlush(void) {
922 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING
923 log_verbose("Skipping, legacy conversion or formatting is disabled.");
925 UChar utf16String
[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
928 const char *enc
= "IBM-1388"; /* GBK EBCDIC stateful */
929 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, enc
);
934 if (myFile
== NULL
) {
935 log_err("Can't write test file %s\n", STANDARD_TEST_FILE
);
939 u_fprintf(myFile
, "%S", utf16String
);
942 /* now read it back */
943 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
944 if (myCFile
== NULL
) {
945 log_err("Can't read test file.");
949 inLen
= fread(inBuf
, 1, 200, myCFile
);
953 log_err("Failed during read of test file.");
957 /* check if shift in and out */
958 for(i
=0;i
<(int32_t)inLen
;i
++) {
959 if(inBuf
[i
]==0x0E) { /* SO */
961 } else if(inBuf
[i
]==0x0F) { /* SI */
967 log_err("Err: shift was unchanged\n");
968 } else if(shift
==1) {
969 log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
970 } else if(shift
==-1) {
971 log_verbose("OK: Shifted in (SI, 0x0F)\n");
975 log_err("Expected 12 bytes, read %d\n", inLen
);
977 log_verbose("OK: read %d bytes\n", inLen
);
984 #if !UCONFIG_NO_FORMATTING
985 static void TestFilePrintCompatibility(void) {
986 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, NULL
);
990 static const UChar emptyStr
[] = {0};
991 char readBuf
[512] = "";
992 char testBuf
[512] = "";
995 if (myFile
== NULL
) {
996 log_err("Can't write test file.\n");
999 #if !UCONFIG_NO_FORMATTING
1000 if (strcmp(u_fgetlocale(myFile
), STANDARD_TEST_LOCALE
) != 0) {
1001 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
1005 /* Compare against C API compatibility */
1006 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1007 u_fprintf(myFile
, "%x ", num
);
1008 u_fprintf(myFile
, "%X ", num
);
1009 u_fprintf(myFile
, "%o ", num
);
1010 u_fprintf(myFile
, "%d ", num
);
1011 u_fprintf(myFile
, "%i ", num
);
1012 u_fprintf(myFile
, "%f ", (double)num
);
1013 /* u_fprintf(myFile, "%e ", (double)num);
1014 u_fprintf(myFile, "%E ", (double)num);*/
1015 u_fprintf(myFile
, "%g ", (double)num
);
1016 u_fprintf(myFile
, "%G", (double)num
);
1017 u_fputs(emptyStr
, myFile
);
1020 u_fprintf_u(myFile
, NEW_LINE
);
1022 for (num
= 0; num
< 0x80; num
++) {
1023 u_fprintf(myFile
, "%c", num
);
1027 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
1028 if (myCFile
== NULL
) {
1029 log_err("Can't read test file.");
1033 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1034 /* Note: gcc on Ubuntu complains if return value of scanf is ignored. */
1035 n
+= fscanf(myCFile
, "%s", readBuf
);
1036 sprintf(testBuf
, "%x", (int)num
);
1037 if (strcmp(readBuf
, testBuf
) != 0) {
1038 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1041 n
+= fscanf(myCFile
, "%s", readBuf
);
1042 sprintf(testBuf
, "%X", (int)num
);
1043 if (strcmp(readBuf
, testBuf
) != 0) {
1044 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1047 n
+= fscanf(myCFile
, "%s", readBuf
);
1048 sprintf(testBuf
, "%o", (int)num
);
1049 if (strcmp(readBuf
, testBuf
) != 0) {
1050 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1053 /* fprintf is not compatible on all platforms e.g. the iSeries */
1054 n
+= fscanf(myCFile
, "%s", readBuf
);
1055 sprintf(testBuf
, "%d", (int)num
);
1056 if (strcmp(readBuf
, testBuf
) != 0) {
1057 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1060 n
+= fscanf(myCFile
, "%s", readBuf
);
1061 sprintf(testBuf
, "%i", (int)num
);
1062 if (strcmp(readBuf
, testBuf
) != 0) {
1063 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1066 n
+= fscanf(myCFile
, "%s", readBuf
);
1067 sprintf(testBuf
, "%f", (double)num
);
1068 if (strcmp(readBuf
, testBuf
) != 0) {
1069 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1072 /* fscanf(myCFile, "%s", readBuf);
1073 sprintf(testBuf, "%e", (double)num);
1074 if (strcmp(readBuf, testBuf) != 0) {
1075 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1078 fscanf(myCFile, "%s", readBuf);
1079 sprintf(testBuf, "%E", (double)num);
1080 if (strcmp(readBuf, testBuf) != 0) {
1081 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1084 n
+= fscanf(myCFile
, "%s", readBuf
);
1085 sprintf(testBuf
, "%g", (double)num
);
1086 if (strcmp(readBuf
, testBuf
) != 0) {
1087 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1090 n
+= fscanf(myCFile
, "%s", readBuf
);
1091 sprintf(testBuf
, "%G", (double)num
);
1092 if (strcmp(readBuf
, testBuf
) != 0) {
1093 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1097 /* Properly eat the newlines */
1098 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1099 n
+= fscanf(myCFile
, "%c", &cVal
);
1100 if (cVal
!= C_NEW_LINE
[num
]) {
1101 log_err("OS newline error\n");
1104 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1105 n
+= fscanf(myCFile
, "%c", &cVal
);
1106 if (cVal
!= C_NEW_LINE
[num
]) {
1107 log_err("ustdio newline error\n");
1111 for (num
= 0; num
< 0x80; num
++) {
1113 n
+= fscanf(myCFile
, "%c", &cVal
);
1115 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1123 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1124 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);\
1125 if (myFile == NULL) {\
1126 log_err("Can't write test file for %s.\n", uFormat);\
1129 /* Reinitialize the buffer to verify null termination works. */\
1130 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
1131 memset(buffer, '*', UPRV_LENGTHOF(buffer));\
1133 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1135 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);\
1136 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);\
1138 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
1139 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1140 if (strcmp(buffer, compBuffer) != 0) {\
1141 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1143 if (cNumPrinted != uNumPrinted) {\
1144 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1146 if (buffer[uNumPrinted+1] != '*') {\
1147 log_err("%" uFormat " too much stored\n");\
1150 #if !UCONFIG_NO_FORMATTING
1151 static void TestFprintfFormat(void) {
1152 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1153 static const char abcChars
[] = "abc";
1156 char compBuffer
[256];
1157 int32_t uNumPrinted
;
1158 int32_t cNumPrinted
;
1161 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1162 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1163 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1165 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1166 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1167 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1169 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1170 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1172 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1173 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1175 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1176 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1177 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1178 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1179 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1180 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1181 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1182 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1183 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1185 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1186 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1187 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1188 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1189 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1190 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1191 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1192 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1193 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1194 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1196 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1197 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1198 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1200 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1201 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1202 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1203 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1204 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1206 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1207 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1208 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1209 TestFPrintFormat("%#o", 123, "%#o", 123);
1210 TestFPrintFormat("%#o", -123, "%#o", -123);
1212 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1213 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1214 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1215 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1216 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1217 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1218 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1219 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1221 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1222 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1223 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1224 TestFPrintFormat("% d", 123456, "% d", 123456);
1225 TestFPrintFormat("% d", -123456, "% d", -123456);
1227 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1228 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1229 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1231 log_verbose("Get really crazy with the formatting.\n");
1233 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1234 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1235 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1236 TestFPrintFormat("%#12x", -123, "%#12x", -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);
1248 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1249 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1251 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1252 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1253 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 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("%12.1f", 1.234, "%12.1f", 1.234);
1260 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1261 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1262 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1263 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1264 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1266 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1267 /* Reinitialize the buffer to verify null termination works. */
1268 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1269 memset(buffer
, '*', UPRV_LENGTHOF(buffer
));
1271 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1273 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1274 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1276 u_austrncpy(compBuffer
, uBuffer
, UPRV_LENGTHOF(uBuffer
));
1277 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1278 if (strcmp(buffer
, compBuffer
) != 0) {
1279 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1281 if (cNumPrinted
!= uNumPrinted
) {
1282 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1284 if (buffer
[uNumPrinted
+1] != '*') {
1285 log_err("%%d %% d %%d too much stored\n");
1290 #undef TestFPrintFormat
1292 #if !UCONFIG_NO_FORMATTING
1293 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1297 char compBuffer
[256];
1298 int32_t uNumScanned
;
1299 int32_t cNumScanned
;
1301 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1302 if (myFile
== NULL
) {
1303 log_err("Can't write test file for %s.\n", format
);
1306 /* Reinitialize the buffer to verify null termination works. */
1307 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1308 uBuffer
[UPRV_LENGTHOF(uBuffer
)-1] = 0;
1309 memset(buffer
, '*', UPRV_LENGTHOF(buffer
));
1310 buffer
[UPRV_LENGTHOF(buffer
)-1] = 0;
1312 u_fprintf(myFile
, "%S", uValue
);
1314 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1315 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1317 if (expectedToPass
) {
1318 u_austrncpy(compBuffer
, uBuffer
, UPRV_LENGTHOF(uBuffer
));
1319 cNumScanned
= sscanf(cValue
, format
, buffer
);
1320 if (strncmp(buffer
, compBuffer
, UPRV_LENGTHOF(buffer
)) != 0) {
1321 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1323 if (cNumScanned
!= uNumScanned
) {
1324 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1326 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1327 log_err("%s too much stored\n", format
);
1331 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1332 log_err("%s too much stored on a failure\n", format
);
1338 #if !UCONFIG_NO_FORMATTING
1339 static void TestFScanset(void) {
1340 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1341 static const char abcChars
[] = "abccdefg";
1343 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1344 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1346 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1347 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1349 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1350 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1352 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1353 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1355 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1356 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1357 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1358 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1359 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1361 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1362 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1364 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1366 /* Extra long string for testing */
1367 TestFScanSetFormat(" %[qrst]",
1368 abcUChars
, abcChars
, TRUE
);
1370 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1373 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1374 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1375 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1376 /* The following is not deterministic on Windows */
1377 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1379 /* TODO: Need to specify precision with a "*" */
1382 #if !UCONFIG_NO_FORMATTING
1383 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1386 int32_t uNumScanned
;
1388 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1389 if (myFile
== NULL
) {
1390 log_err("Can't write test file for %s.\n", format
);
1393 /* Reinitialize the buffer to verify null termination works. */
1394 u_memset(uBuffer
, 0x2a, UPRV_LENGTHOF(uBuffer
));
1395 uBuffer
[UPRV_LENGTHOF(uBuffer
)-1] = 0;
1397 u_fprintf(myFile
, "%S", uValue
);
1399 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1400 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1402 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1403 log_err("%s too much stored on a failure\n", format
);
1407 #if !UCONFIG_NO_FORMATTING
1408 static void TestBadScanfFormat(void) {
1409 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1410 static const char abcChars
[] = "abccdefg";
1412 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1415 #if !UCONFIG_NO_FORMATTING
1416 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1418 UChar uBuffer2
[256];
1423 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, "UTF-8");
1425 log_err("Test file can't be opened\n");
1429 va_start(ap
, format
);
1430 count
= u_vfprintf(myFile
, format
, ap
);
1431 (void)count
; /* Suppress set but not used warning. */
1437 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, "UTF-8");
1439 log_err("Test file can't be opened\n");
1442 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1443 u_uastrcpy(uBuffer2
, expectedResult
);
1444 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1445 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1450 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1452 log_err("Test file can't be opened\n");
1455 u_uastrcpy(uBuffer
, format
);
1457 va_start(ap
, format
);
1458 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1464 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1466 log_err("Test file can't be opened\n");
1469 u_fgets(uBuffer
, UPRV_LENGTHOF(uBuffer
), myFile
);
1470 u_uastrcpy(uBuffer2
, expectedResult
);
1471 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1472 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1477 static void TestVargs(void) {
1478 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1482 static void TestUnicodeFormat(void)
1484 #if !UCONFIG_NO_FORMATTING
1485 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1486 UChar myUString
[256];
1488 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1489 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1491 u_memset(myUString
, 0x2a, UPRV_LENGTHOF(myUString
));
1493 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1495 log_err("Test file can't be opened\n");
1498 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1501 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1503 log_err("Test file can't be opened\n");
1506 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1508 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1509 log_err("u_fscanf_u doesn't work.\n");
1514 static void TestFileWriteRetval(const char * a_pszEncoding
) {
1518 int32_t expected
= 10000; /* test with large data to test internal buffer looping */
1519 UChar testChar
= 0xBEEF;
1521 if (!*a_pszEncoding
|| 0 == strcmp(a_pszEncoding
, "ASCII")) {
1522 testChar
= 0x65; /* 'A' - otherwise read test will fail */
1525 buffer
= (UChar
*) malloc(expected
* sizeof(UChar
));
1527 log_err("Out of memory\n");
1532 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, a_pszEncoding
);
1535 log_err("Test file can't be opened for write\n");
1538 u_memset(buffer
, testChar
, expected
);
1539 count
= u_file_write(buffer
, expected
, myFile
);
1541 if (count
!= expected
) {
1543 log_err("u_file_write returned incorrect number of characters written\n");
1551 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, a_pszEncoding
);
1553 log_err("Test file can't be opened for read\n");
1556 for (count
= 0; count
< expected
; ++count
) {
1557 UChar gotChar
= u_fgetc(myFile
);
1558 if(gotChar
!= testChar
) {
1559 log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar
, testChar
);
1564 if (u_fgetc(myFile
) != U_EOF
) {
1565 log_err("u_fgetc did not return expected EOF\n");
1572 static void TestFileWriteRetvalUTF16(void) {
1573 TestFileWriteRetval("UTF-16");
1576 static void TestFileWriteRetvalUTF8(void) {
1577 TestFileWriteRetval("UTF-8");
1580 static void TestFileWriteRetvalASCII(void) {
1581 TestFileWriteRetval("ASCII");
1584 static void TestFileWriteRetvalNONE(void) {
1585 TestFileWriteRetval("");
1589 addFileTest(TestNode
** root
) {
1590 #if !UCONFIG_NO_FORMATTING
1591 addTest(root
, &TestFile
, "file/TestFile");
1592 addTest(root
, &TestFinit
, "file/TestFinit");
1593 addTest(root
, &TestFadopt
, "file/TestFadopt");
1595 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1596 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1597 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1598 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1599 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1600 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1601 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1602 addTest(root
, &TestCodepage
, "file/TestCodepage");
1603 addTest(root
, &TestCodepageFlush
, "file/TestCodepageFlush");
1604 addTest(root
, &TestFileWriteRetvalUTF16
, "file/TestFileWriteRetvalUTF16");
1605 addTest(root
, &TestFileWriteRetvalUTF8
, "file/TestFileWriteRetvalUTF8");
1606 addTest(root
, &TestFileWriteRetvalASCII
, "file/TestFileWriteRetvalASCII");
1607 addTest(root
, &TestFileWriteRetvalNONE
, "file/TestFileWriteRetvalNONE");
1608 #if !UCONFIG_NO_FORMATTING
1609 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1610 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1611 addTest(root
, &TestFScanset
, "file/TestFScanset");
1612 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1613 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1614 addTest(root
, &TestVargs
, "file/TestVargs");
1615 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");