2 **********************************************************************
3 * Copyright (C) 2004-2014, 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
16 #include "unicode/ustdio.h"
17 #include "unicode/ustring.h"
18 #include "unicode/uloc.h"
23 const char *STANDARD_TEST_FILE
= "iotest-c.txt";
25 const char *STANDARD_TEST_LOCALE
= "en_US_POSIX";
28 #if !UCONFIG_NO_FORMATTING
29 static void TestFileFromICU(UFILE
*myFile
) {
31 float myFloat
= -1234.0;
32 int32_t newValuePtr
[1];
33 double newDoubleValuePtr
[1];
35 UChar uStringBuf
[256];
36 char myString
[256] = "";
37 char testBuf
[256] = "";
39 U_STRING_DECL(myStringOrig
, "My-String", 9);
41 U_STRING_INIT(myStringOrig
, "My-String", 9);
42 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
43 u_memset(uStringBuf
, 0x2a, sizeof(uStringBuf
)/sizeof(*uStringBuf
));
44 memset(myString
, '*', sizeof(myString
)/sizeof(*myString
));
45 memset(testBuf
, '*', sizeof(testBuf
)/sizeof(*testBuf
));
48 log_err("Can't write test file.\n");
53 if (sizeof(void *) == 4) {
54 origPtr
= (void *)0xdeadbeef;
55 } else if (sizeof(void *) == 8) {
56 origPtr
= (void *) INT64_C(0x1000200030004000);
57 } else if (sizeof(void *) == 16) {
62 } massiveBigEndianPtr
= {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
63 origPtr
= massiveBigEndianPtr
.ptr
;
65 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
69 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
70 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
71 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
72 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
73 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
74 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
75 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
76 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
77 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
78 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
79 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
80 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
81 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
82 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
83 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
84 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
85 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
86 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
87 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
88 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
91 log_err("Got feof while writing the file.\n");
95 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
96 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
97 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
99 u_fprintf(myFile
, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
100 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
); /* Should be 27 as stated later on. */
103 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
105 if (myFile
== NULL
) {
106 log_err("Can't read test file.");
110 if (u_feof(myFile
)) {
111 log_err("Got feof while reading the file and not at the end of the file.\n");
114 myUString
[0] = u_fgetc(myFile
);
115 if (myUString
[0] != 0x53 /* S */) {
116 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
118 u_fungetc(myUString
[0], myFile
);
119 myUString
[0] = u_fgetc(myFile
);
120 if (myUString
[0] != 0x53 /* S */) {
121 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString
[0]);
123 u_fungetc(myUString
[0], myFile
);
124 myUString
[0] = u_fgetc(myFile
);
125 if (myUString
[0] != 0x53 /* S */) {
126 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString
[0]);
128 u_fungetc(myUString
[0], myFile
);
129 myUString
[0] = u_fgetc(myFile
);
130 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
131 myUString
[2] = u_fgetc(myFile
);
132 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
133 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
135 u_fungetc(myUString
[2], myFile
);
136 u_fungetc(myUString
[1], myFile
);
137 u_fungetc(myUString
[0], myFile
);
142 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
143 if (*n
!= *newValuePtr
) {
144 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
147 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
148 if (*n
!= *newValuePtr
) {
149 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
152 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
153 if (*n
!= *newValuePtr
) {
154 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
157 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
158 if (*n
!= *newValuePtr
) {
159 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
162 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
163 if (*n
!= *newValuePtr
) {
164 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
167 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
168 if (*n
!= *newValuePtr
) {
169 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
171 *newDoubleValuePtr
= -1.0;
172 u_fscanf(myFile
, "Float %%f: %lf\n", newDoubleValuePtr
);
173 if (myFloat
!= *newDoubleValuePtr
) {
174 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
176 *newDoubleValuePtr
= -1.0;
177 u_fscanf(myFile
, "Lowercase float %%e: %le\n", newDoubleValuePtr
);
178 if (myFloat
!= *newDoubleValuePtr
) {
179 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
181 *newDoubleValuePtr
= -1.0;
182 u_fscanf(myFile
, "Uppercase float %%E: %lE\n", newDoubleValuePtr
);
183 if (myFloat
!= *newDoubleValuePtr
) {
184 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr
, myFloat
);
186 *newDoubleValuePtr
= -1.0;
187 u_fscanf(myFile
, "Lowercase float %%g: %lg\n", newDoubleValuePtr
);
188 if (myFloat
!= *newDoubleValuePtr
) {
189 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
191 *newDoubleValuePtr
= -1.0;
192 u_fscanf(myFile
, "Uppercase float %%G: %lG\n", newDoubleValuePtr
);
193 if (myFloat
!= *newDoubleValuePtr
) {
194 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
197 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
198 if (ptr
!= origPtr
) {
199 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
201 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
202 if (*myString
!= 'A') {
203 log_err("%%c Got: %c, Expected: A\n", *myString
);
205 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
206 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
207 log_err("%%C Got: %C, Expected: A\n", *myUString
);
209 u_fscanf(myFile
, "String %%s: %s\n", myString
);
210 if (strcmp(myString
, "My-String")) {
211 log_err("%%s Got: %s, Expected: My String\n", myString
);
213 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
214 if (strcmp(myString
, "(null)")) {
215 log_err("%%s Got: %s, Expected: My String\n", myString
);
217 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
218 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
219 if (strcmp(myString
, "My-String")) {
220 log_err("%%S Got: %S, Expected: My String\n", myUString
);
222 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
223 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
224 if (strcmp(myString
, "(null)")) {
225 log_err("%%S Got: %S, Expected: My String\n", myUString
);
227 *newDoubleValuePtr
= -1.0;
228 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
229 if (myFloat
!= *newDoubleValuePtr
) {
230 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
232 *newDoubleValuePtr
= -1.0;
233 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
234 if (myFloat
!= *newDoubleValuePtr
) {
235 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
238 u_fgets(myUString
, 4, myFile
);
241 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
242 if (strcmp(myString
, "\t\n") != 0) {
243 log_err("u_fgets got \"%s\"\n", myString
);
246 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
247 log_err("u_fgets did not return myUString\n");
249 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
250 if (strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
251 log_err("u_fgets got \"%s\"\n", myString
);
254 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
255 log_err("u_fgets did not return myUString\n");
257 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
258 if (strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
259 log_err("u_fgets got \"%s\"\n", myString
);
262 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
263 log_err("u_fgets did not return myUString\n");
265 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
266 if (strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
267 log_err("u_fgets got \"%s\"\n", myString
);
270 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
271 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
274 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
275 log_err("u_fgets did not return myUString\n");
277 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
278 if (strcmp(myString
, "") != 0) {
279 log_err("u_fgets got \"%s\"\n", myString
);
282 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
283 log_err("u_fgets did not return myUString\n");
285 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
286 if (strcmp(myString
, "\t") != 0) {
287 log_err("u_fgets got \"%s\"\n", myString
);
290 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
291 sizeof(myUString
)/sizeof(*myUString
));
292 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
293 log_err("u_fgets got \"%s\"\n", myString
);
296 if (u_feof(myFile
)) {
297 log_err("Got feof while reading the file and not at the end of the file.\n");
299 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
300 sizeof(myUString
)/sizeof(*myUString
));
301 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
302 log_err("u_fgets got \"%s\"\n", myString
);
304 if (!u_feof(myFile
)) {
305 log_err("Did not get feof while reading the end of the file.\n");
307 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
308 log_err("u_fscanf read data while reading the end of the file.\n");
314 static void TestFile(void) {
316 log_verbose("Testing u_fopen\n");
317 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
));
320 static void TestFinit(void) {
323 log_verbose("Testing u_finit\n");
324 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
325 TestFileFromICU(u_finit(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
326 fclose(standardFile
);
329 static void TestFadopt(void) {
332 log_verbose("Testing u_fadopt\n");
333 standardFile
= fopen(STANDARD_TEST_FILE
, "w");
334 TestFileFromICU(u_fadopt(standardFile
, STANDARD_TEST_LOCALE
, NULL
));
338 static void StdinBuffering(void) {
342 UFILE
*uStdIn
= NULL
;
343 UFILE
*uStdOut
= NULL
;
344 uStdIn
= u_finit(stdin
, NULL
, NULL
);
345 uStdOut
= u_finit(stdout
, NULL
, NULL
);
351 u_fgets(buff
, sizeof(buff
)/sizeof(buff
[0]), uStdIn
);
352 u_fprintf(uStdOut
, "%S\n", buff
);
353 u_fscanf(uStdIn
, "%d", &num
);
354 u_fprintf(uStdOut
, "%d\n", num
);
355 u_fscanf(uStdIn
, "%d", &num
);
356 u_fprintf(uStdOut
, "%d\n", num
);
358 log_verbose("Test disabled because it requires user interaction");
362 static void TestCodepageAndLocale(void) {
363 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
364 if (myFile
== NULL
) {
365 log_err("Can't write test file.\n");
368 if (u_fgetcodepage(myFile
) == NULL
369 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
371 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
372 u_fgetcodepage(myFile
), ucnv_getDefaultName());
374 #if !UCONFIG_NO_FORMATTING
375 if (u_fgetlocale(myFile
) == NULL
376 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
378 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
379 u_fgetlocale(myFile
), uloc_getDefault());
384 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
385 if (u_fgetcodepage(myFile
) == NULL
386 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
388 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
389 u_fgetcodepage(myFile
));
391 #if !UCONFIG_NO_FORMATTING
392 if (u_fgetlocale(myFile
) == NULL
393 || strcmp(u_fgetlocale(myFile
), "es") != 0)
395 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
396 u_fgetlocale(myFile
), "es");
401 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
402 if (u_fgetcodepage(myFile
) == NULL
403 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
405 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
406 u_fgetcodepage(myFile
));
408 #if !UCONFIG_NO_FORMATTING
409 if (u_fgetlocale(myFile
) == NULL
410 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
412 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
413 u_fgetlocale(myFile
), uloc_getDefault());
418 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
419 if (u_fgetcodepage(myFile
) == NULL
420 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
422 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
423 u_fgetcodepage(myFile
));
425 #if !UCONFIG_NO_FORMATTING
426 if (u_fgetlocale(myFile
) == NULL
427 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
429 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
430 u_fgetlocale(myFile
), "zh");
437 static void TestfgetsBuffers(void) {
439 UChar expectedBuffer
[2048];
440 static const char testStr
[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
441 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
442 int32_t expectedSize
= (int32_t)strlen(testStr
);
446 if (myFile
== NULL
) {
447 log_err("Can't write test file.\n");
451 u_fputc(0x3BC, myFile
);
452 if (u_fputc(0x110000, myFile
) != U_EOF
) {
453 log_err("u_fputc should return U_EOF for 0x110000.\n");
455 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
456 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
458 u_fputc(0xFF41, myFile
);
459 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
460 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
461 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
462 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
463 u_file_write(buffer
, expectedSize
, myFile
);
464 u_strcat(expectedBuffer
, buffer
);
468 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
469 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
470 if (u_fgetc(myFile
) != 0x3BC) {
471 log_err("The first character is wrong\n");
473 if (u_fgetc(myFile
) != 0xFF41) {
474 log_err("The second character is wrong\n");
476 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
477 log_err("Didn't get the buffer back\n");
480 readSize
= u_strlen(buffer
);
481 if (readSize
!= expectedSize
*repetitions
) {
482 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
484 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
485 log_err("u_fgets wrote too much data\n");
487 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
488 log_err("Did get expected string back\n");
490 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
491 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
495 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
497 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
499 u_fputc(0x3BC, myFile
);
500 u_fputc(0xFF41, myFile
);
501 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
502 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
503 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
504 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
505 u_file_write(buffer
, expectedSize
, myFile
);
506 u_strcat(expectedBuffer
, buffer
);
510 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
511 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
512 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
513 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
515 if (u_fgetc(myFile
) != 0x3BC) {
516 log_err("The first character is wrong\n");
518 if (u_fgetc(myFile
) != 0xFF41) {
519 log_err("The second character is wrong\n");
521 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
522 log_err("Didn't get the buffer back\n");
525 readSize
= u_strlen(buffer
);
526 if (readSize
!= expectedSize
*repetitions
) {
527 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
529 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
530 log_err("u_fgets wrote too much data\n");
532 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
533 log_err("Did get expected string back\n");
538 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
540 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
542 u_fputc(0xFF41, myFile
);
543 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
544 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
545 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
546 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
547 u_file_write(buffer
, expectedSize
, myFile
);
548 u_strcat(expectedBuffer
, buffer
);
552 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
553 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
554 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
555 log_err("Didn't get the buffer back\n");
558 readSize
= u_strlen(buffer
);
560 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
562 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
563 log_err("Did get expected string back\n");
565 if (buffer
[2] != 0xBEEF) {
566 log_err("u_fgets wrote too much data\n");
572 static void TestFileReadBuffering(void) {
574 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
578 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
579 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
580 u_file_write(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
584 u_memset(buffer
, 0xDEAD, sizeof(buffer
)/sizeof(buffer
[0]));
585 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
586 how_many
= u_file_read(buffer
, 1024, myFile
);
587 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
588 log_err("u_file_read read too much or not enough data\n");
593 static void TestfgetsLineCount(void) {
595 UChar expectedBuffer
[2048];
596 char charBuffer
[2048];
597 static const char testStr
[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
598 UFILE
*myFile
= NULL
;
599 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
600 int32_t expectedSize
= (int32_t)strlen(testStr
);
602 int32_t nlRepetitions
;
604 if (stdFile
== NULL
) {
605 log_err("Can't write test file.\n");
608 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
610 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
611 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
612 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
613 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
618 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
619 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
622 char *returnedCharBuffer
;
623 UChar
*returnedUCharBuffer
;
625 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
626 returnedCharBuffer
= fgets(charBuffer
, sizeof(charBuffer
)/sizeof(charBuffer
[0]), stdFile
);
627 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
629 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
630 /* Both returned NULL. stop. */
633 if (returnedCharBuffer
!= charBuffer
) {
634 log_err("Didn't get the charBuffer back\n");
637 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
638 if (returnedUCharBuffer
!= buffer
) {
639 log_err("Didn't get the buffer back\n");
642 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
643 log_err("buffers are different\n");
645 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
646 log_err("u_fgets wrote too much\n");
653 static void TestfgetsNewLineHandling(void) {
655 static const UChar testUStr
[][16] = {
672 {0x00A0, 0x000D, 0x000A, 0},
674 {0xd800, 0xdfff, 0x000D, 0},
687 UFILE
*myFile
= NULL
;
690 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
691 if (myFile
== NULL
) {
692 log_err("Can't write test file.\n");
695 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
696 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
700 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
702 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
703 UChar
*returnedUCharBuffer
;
705 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
706 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
708 if (!returnedUCharBuffer
) {
709 /* Returned NULL. stop. */
712 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
713 log_err("buffers are different at index = %d\n", lineIdx
);
715 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
716 log_err("u_fgets wrote too much\n");
719 if (lineIdx
!= (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0]))) {
720 log_err("u_fgets read too much\n");
722 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != NULL
) {
723 log_err("u_file_write wrote too much\n");
728 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
730 UChar expectedBuffer
[64];
731 int32_t lineLen
= strlen(line
);
732 UChar
*returnedUCharBuffer
;
734 UFILE
*myFile
= NULL
;
735 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
737 if (stdFile
== NULL
) {
738 log_err("Can't write test file.\n");
741 /* Write a prefix line and then write a bunch of lines */
742 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
743 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
744 fwrite(line
, lineLen
, 1, stdFile
);
748 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
749 if (myFile
== NULL
) {
750 log_err("Can't read test file.\n");
754 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
755 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
756 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
757 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
758 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
762 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
763 for (repetitions
= 0; ; repetitions
++) {
764 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
765 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
767 if (!returnedUCharBuffer
) {
768 /* returned NULL. stop. */
771 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
772 log_err("buffers are different at count %d\n", repetitions
);
774 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
775 log_err("u_fgets wrote too much\n");
778 if (repetitions
!= numRepititions
) {
779 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
784 static void TestfgetsNewLineCount(void) {
785 /* This makes sure that lines are correctly handled between buffer boundaries. */
786 TestLineCount("\n", "\n", 1024); /* Unix newlines */
787 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
788 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
789 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
790 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
791 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
794 static void TestFgetsLineBuffering(void) {
795 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
796 UChar
*returnedUCharBuffer
;
798 UFILE
*myFile
= NULL
;
799 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
801 if (stdFile
== NULL
) {
802 log_err("Can't write test file.\n");
805 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
807 /* Write one very long line */
808 for (repetitions
= 0; repetitions
< ((sizeof(buffer
)/sizeof(buffer
[0]))*2); repetitions
++) {
809 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
813 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
814 if (myFile
== NULL
) {
815 log_err("Can't read test file.\n");
819 /* Read part of one very long line */
820 returnedUCharBuffer
= u_fgets(buffer
, (sizeof(buffer
)/sizeof(buffer
[0]))-1, myFile
);
821 if (u_strlen(returnedUCharBuffer
) != ((sizeof(buffer
)/sizeof(buffer
[0]))-2)) {
822 log_err("Line is wrong length. Got %d. Expected %d.\n",
823 u_strlen(returnedUCharBuffer
), ((sizeof(buffer
)/sizeof(buffer
[0]))-2));
825 /* We better not read too much */
826 if (buffer
[(sizeof(buffer
)/sizeof(buffer
[0]))-1] != 0xBEEF) {
827 log_err("Too much data was written\n");
834 static void TestCodepage(void) {
835 UFILE
*myFile
= NULL
;
836 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
837 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
838 UChar testBuf
[sizeof(strABAccentA
)/sizeof(strABAccentA
[0])*2]; /* *2 to see if too much was */
839 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
841 UErrorCode status
= U_ZERO_ERROR
;
843 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
846 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
850 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
851 if (myFile
== NULL
) {
852 log_err("Can't write test file for iso-8859-1.\n");
855 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
856 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
858 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
861 /* Now see what we got wrote */
862 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
863 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
864 log_err("u_fsetcodepage didn't set the codepage\n");
866 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
867 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
868 log_err("The test data was read and written differently!\n");
870 if (retVal
!= u_strlen(strABAccentA
)) {
871 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
875 /* What happens on invalid input? */
876 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
877 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
878 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
880 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
881 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
883 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
884 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
886 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
887 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
888 log_err("The test data wasn't subsituted as expected\n");
892 /* Can't currently swap codepages midstream */
893 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
894 strcpy(convName
, u_fgetcodepage(myFile
));
895 u_file_read(testBuf
, 1, myFile
);
896 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
897 log_err("u_fsetcodepage set the codepage after reading a byte\n");
899 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
900 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
901 log_err("u_fsetcodepage changed the codepages after writing data\n");
903 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
904 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
907 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
908 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
909 log_err("The test data was read and written differently!\n");
911 if (retVal
!= u_strlen(strABAccentA
)) {
912 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
918 static void TestCodepageFlush(void) {
919 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING
920 log_verbose("Skipping, legacy conversion or formatting is disabled.");
922 UChar utf16String
[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
925 const char *enc
= "IBM-1388"; /* GBK EBCDIC stateful */
926 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, enc
);
931 if (myFile
== NULL
) {
932 log_err("Can't write test file %s\n", STANDARD_TEST_FILE
);
936 u_fprintf(myFile
, "%S", utf16String
);
939 /* now read it back */
940 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
941 if (myCFile
== NULL
) {
942 log_err("Can't read test file.");
946 inLen
= fread(inBuf
, 1, 200, myCFile
);
950 log_err("Failed during read of test file.");
954 /* check if shift in and out */
955 for(i
=0;i
<(int32_t)inLen
;i
++) {
956 if(inBuf
[i
]==0x0E) { /* SO */
958 } else if(inBuf
[i
]==0x0F) { /* SI */
964 log_err("Err: shift was unchanged\n");
965 } else if(shift
==1) {
966 log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
967 } else if(shift
==-1) {
968 log_verbose("OK: Shifted in (SI, 0x0F)\n");
972 log_err("Expected 12 bytes, read %d\n", inLen
);
974 log_verbose("OK: read %d bytes\n", inLen
);
981 #if !UCONFIG_NO_FORMATTING
982 static void TestFilePrintCompatibility(void) {
983 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", STANDARD_TEST_LOCALE
, NULL
);
987 static const UChar emptyStr
[] = {0};
988 char readBuf
[512] = "";
989 char testBuf
[512] = "";
992 if (myFile
== NULL
) {
993 log_err("Can't write test file.\n");
996 #if !UCONFIG_NO_FORMATTING
997 if (strcmp(u_fgetlocale(myFile
), STANDARD_TEST_LOCALE
) != 0) {
998 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
1002 /* Compare against C API compatibility */
1003 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1004 u_fprintf(myFile
, "%x ", num
);
1005 u_fprintf(myFile
, "%X ", num
);
1006 u_fprintf(myFile
, "%o ", num
);
1007 u_fprintf(myFile
, "%d ", num
);
1008 u_fprintf(myFile
, "%i ", num
);
1009 u_fprintf(myFile
, "%f ", (double)num
);
1010 /* u_fprintf(myFile, "%e ", (double)num);
1011 u_fprintf(myFile, "%E ", (double)num);*/
1012 u_fprintf(myFile
, "%g ", (double)num
);
1013 u_fprintf(myFile
, "%G", (double)num
);
1014 u_fputs(emptyStr
, myFile
);
1017 u_fprintf_u(myFile
, NEW_LINE
);
1019 for (num
= 0; num
< 0x80; num
++) {
1020 u_fprintf(myFile
, "%c", num
);
1024 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
1025 if (myCFile
== NULL
) {
1026 log_err("Can't read test file.");
1030 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1031 /* Note: gcc on Ubuntu complains if return value of scanf is ignored. */
1032 n
+= fscanf(myCFile
, "%s", readBuf
);
1033 sprintf(testBuf
, "%x", (int)num
);
1034 if (strcmp(readBuf
, testBuf
) != 0) {
1035 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1038 n
+= fscanf(myCFile
, "%s", readBuf
);
1039 sprintf(testBuf
, "%X", (int)num
);
1040 if (strcmp(readBuf
, testBuf
) != 0) {
1041 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1044 n
+= fscanf(myCFile
, "%s", readBuf
);
1045 sprintf(testBuf
, "%o", (int)num
);
1046 if (strcmp(readBuf
, testBuf
) != 0) {
1047 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1050 /* fprintf is not compatible on all platforms e.g. the iSeries */
1051 n
+= fscanf(myCFile
, "%s", readBuf
);
1052 sprintf(testBuf
, "%d", (int)num
);
1053 if (strcmp(readBuf
, testBuf
) != 0) {
1054 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1057 n
+= fscanf(myCFile
, "%s", readBuf
);
1058 sprintf(testBuf
, "%i", (int)num
);
1059 if (strcmp(readBuf
, testBuf
) != 0) {
1060 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1063 n
+= fscanf(myCFile
, "%s", readBuf
);
1064 sprintf(testBuf
, "%f", (double)num
);
1065 if (strcmp(readBuf
, testBuf
) != 0) {
1066 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1069 /* fscanf(myCFile, "%s", readBuf);
1070 sprintf(testBuf, "%e", (double)num);
1071 if (strcmp(readBuf, testBuf) != 0) {
1072 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1075 fscanf(myCFile, "%s", readBuf);
1076 sprintf(testBuf, "%E", (double)num);
1077 if (strcmp(readBuf, testBuf) != 0) {
1078 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1081 n
+= fscanf(myCFile
, "%s", readBuf
);
1082 sprintf(testBuf
, "%g", (double)num
);
1083 if (strcmp(readBuf
, testBuf
) != 0) {
1084 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1087 n
+= fscanf(myCFile
, "%s", readBuf
);
1088 sprintf(testBuf
, "%G", (double)num
);
1089 if (strcmp(readBuf
, testBuf
) != 0) {
1090 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1094 /* Properly eat the newlines */
1095 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1096 n
+= fscanf(myCFile
, "%c", &cVal
);
1097 if (cVal
!= C_NEW_LINE
[num
]) {
1098 log_err("OS newline error\n");
1101 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1102 n
+= fscanf(myCFile
, "%c", &cVal
);
1103 if (cVal
!= C_NEW_LINE
[num
]) {
1104 log_err("ustdio newline error\n");
1108 for (num
= 0; num
< 0x80; num
++) {
1110 n
+= fscanf(myCFile
, "%c", &cVal
);
1112 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1120 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1121 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);\
1122 if (myFile == NULL) {\
1123 log_err("Can't write test file for %s.\n", uFormat);\
1126 /* Reinitialize the buffer to verify null termination works. */\
1127 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1128 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
1130 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1132 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);\
1133 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
1135 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1136 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1137 if (strcmp(buffer, compBuffer) != 0) {\
1138 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1140 if (cNumPrinted != uNumPrinted) {\
1141 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1143 if (buffer[uNumPrinted+1] != '*') {\
1144 log_err("%" uFormat " too much stored\n");\
1147 #if !UCONFIG_NO_FORMATTING
1148 static void TestFprintfFormat(void) {
1149 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1150 static const char abcChars
[] = "abc";
1153 char compBuffer
[256];
1154 int32_t uNumPrinted
;
1155 int32_t cNumPrinted
;
1158 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1159 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1160 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1162 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1163 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1164 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1166 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1167 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1169 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1170 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1172 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1173 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1174 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1175 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1176 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1177 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1178 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1179 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1180 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1182 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1183 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1184 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1185 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1186 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1187 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1188 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1189 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1190 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1191 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1193 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1194 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1195 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1197 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1198 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1199 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1200 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1201 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1203 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1204 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1205 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1206 TestFPrintFormat("%#o", 123, "%#o", 123);
1207 TestFPrintFormat("%#o", -123, "%#o", -123);
1209 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1210 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1211 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1212 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1213 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1214 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1215 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1216 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1218 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1219 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1220 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1221 TestFPrintFormat("% d", 123456, "% d", 123456);
1222 TestFPrintFormat("% d", -123456, "% d", -123456);
1224 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1225 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1226 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1228 log_verbose("Get really crazy with the formatting.\n");
1230 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1231 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1232 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1233 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1235 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1236 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1237 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1238 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1239 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1240 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1241 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1242 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1243 TestFPrintFormat("%12d", 123, "%12d", 123);
1244 TestFPrintFormat("%12d", -123, "%12d", -123);
1245 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1246 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1248 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1249 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1250 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1251 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1252 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1253 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1254 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1255 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1256 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1257 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1258 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1259 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1260 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1261 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1263 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1264 /* Reinitialize the buffer to verify null termination works. */
1265 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1266 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1268 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1270 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1271 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1273 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1274 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1275 if (strcmp(buffer
, compBuffer
) != 0) {
1276 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1278 if (cNumPrinted
!= uNumPrinted
) {
1279 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1281 if (buffer
[uNumPrinted
+1] != '*') {
1282 log_err("%%d %% d %%d too much stored\n");
1287 #undef TestFPrintFormat
1289 #if !UCONFIG_NO_FORMATTING
1290 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1294 char compBuffer
[256];
1295 int32_t uNumScanned
;
1296 int32_t cNumScanned
;
1298 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1299 if (myFile
== NULL
) {
1300 log_err("Can't write test file for %s.\n", format
);
1303 /* Reinitialize the buffer to verify null termination works. */
1304 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1305 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1306 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1307 buffer
[sizeof(buffer
)/sizeof(*buffer
)-1] = 0;
1309 u_fprintf(myFile
, "%S", uValue
);
1311 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1312 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1314 if (expectedToPass
) {
1315 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1316 cNumScanned
= sscanf(cValue
, format
, buffer
);
1317 if (strncmp(buffer
, compBuffer
, sizeof(buffer
)/sizeof(*buffer
)) != 0) {
1318 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1320 if (cNumScanned
!= uNumScanned
) {
1321 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1323 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1324 log_err("%s too much stored\n", format
);
1328 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1329 log_err("%s too much stored on a failure\n", format
);
1335 #if !UCONFIG_NO_FORMATTING
1336 static void TestFScanset(void) {
1337 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1338 static const char abcChars
[] = "abccdefg";
1340 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1341 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1343 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1344 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1346 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1347 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1349 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1350 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1352 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1353 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1354 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1355 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1356 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1358 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1359 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1361 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1363 /* Extra long string for testing */
1364 TestFScanSetFormat(" %[qrst]",
1365 abcUChars
, abcChars
, TRUE
);
1367 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1370 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1371 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1372 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1373 /* The following is not deterministic on Windows */
1374 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1376 /* TODO: Need to specify precision with a "*" */
1379 #if !UCONFIG_NO_FORMATTING
1380 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1383 int32_t uNumScanned
;
1385 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1386 if (myFile
== NULL
) {
1387 log_err("Can't write test file for %s.\n", format
);
1390 /* Reinitialize the buffer to verify null termination works. */
1391 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1392 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1394 u_fprintf(myFile
, "%S", uValue
);
1396 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1397 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1399 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1400 log_err("%s too much stored on a failure\n", format
);
1404 #if !UCONFIG_NO_FORMATTING
1405 static void TestBadScanfFormat(void) {
1406 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1407 static const char abcChars
[] = "abccdefg";
1409 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1412 #if !UCONFIG_NO_FORMATTING
1413 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1415 UChar uBuffer2
[256];
1420 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, "UTF-8");
1422 log_err("Test file can't be opened\n");
1426 va_start(ap
, format
);
1427 count
= u_vfprintf(myFile
, format
, ap
);
1428 (void)count
; /* Suppress set but not used warning. */
1434 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, "UTF-8");
1436 log_err("Test file can't be opened\n");
1439 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1440 u_uastrcpy(uBuffer2
, expectedResult
);
1441 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1442 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1447 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", STANDARD_TEST_LOCALE
, NULL
);
1449 log_err("Test file can't be opened\n");
1452 u_uastrcpy(uBuffer
, format
);
1454 va_start(ap
, format
);
1455 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1461 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", STANDARD_TEST_LOCALE
, NULL
);
1463 log_err("Test file can't be opened\n");
1466 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1467 u_uastrcpy(uBuffer2
, expectedResult
);
1468 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1469 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1474 static void TestVargs(void) {
1475 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1479 static void TestUnicodeFormat(void)
1481 #if !UCONFIG_NO_FORMATTING
1482 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1483 UChar myUString
[256];
1485 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1486 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1488 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
1490 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1492 log_err("Test file can't be opened\n");
1495 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1498 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1500 log_err("Test file can't be opened\n");
1503 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1505 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1506 log_err("u_fscanf_u doesn't work.\n");
1511 static void TestFileWriteRetval(const char * a_pszEncoding
) {
1515 int32_t expected
= 10000; /* test with large data to test internal buffer looping */
1516 UChar testChar
= 0xBEEF;
1518 if (!*a_pszEncoding
|| 0 == strcmp(a_pszEncoding
, "ASCII")) {
1519 testChar
= 0x65; /* 'A' - otherwise read test will fail */
1522 buffer
= (UChar
*) malloc(expected
* sizeof(UChar
));
1524 log_err("Out of memory\n");
1529 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, a_pszEncoding
);
1532 log_err("Test file can't be opened for write\n");
1535 u_memset(buffer
, testChar
, expected
);
1536 count
= u_file_write(buffer
, expected
, myFile
);
1538 if (count
!= expected
) {
1540 log_err("u_file_write returned incorrect number of characters written\n");
1548 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, a_pszEncoding
);
1550 log_err("Test file can't be opened for read\n");
1553 for (count
= 0; count
< expected
; ++count
) {
1554 UChar gotChar
= u_fgetc(myFile
);
1555 if(gotChar
!= testChar
) {
1556 log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar
, testChar
);
1561 if (u_fgetc(myFile
) != U_EOF
) {
1562 log_err("u_fgetc did not return expected EOF\n");
1569 static void TestFileWriteRetvalUTF16(void) {
1570 TestFileWriteRetval("UTF-16");
1573 static void TestFileWriteRetvalUTF8(void) {
1574 TestFileWriteRetval("UTF-8");
1577 static void TestFileWriteRetvalASCII(void) {
1578 TestFileWriteRetval("ASCII");
1581 static void TestFileWriteRetvalNONE(void) {
1582 TestFileWriteRetval("");
1586 addFileTest(TestNode
** root
) {
1587 #if !UCONFIG_NO_FORMATTING
1588 addTest(root
, &TestFile
, "file/TestFile");
1589 addTest(root
, &TestFinit
, "file/TestFinit");
1590 addTest(root
, &TestFadopt
, "file/TestFadopt");
1592 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1593 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1594 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1595 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1596 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1597 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1598 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1599 addTest(root
, &TestCodepage
, "file/TestCodepage");
1600 addTest(root
, &TestCodepageFlush
, "file/TestCodepageFlush");
1601 addTest(root
, &TestFileWriteRetvalUTF16
, "file/TestFileWriteRetvalUTF16");
1602 addTest(root
, &TestFileWriteRetvalUTF8
, "file/TestFileWriteRetvalUTF8");
1603 addTest(root
, &TestFileWriteRetvalASCII
, "file/TestFileWriteRetvalASCII");
1604 addTest(root
, &TestFileWriteRetvalNONE
, "file/TestFileWriteRetvalNONE");
1605 #if !UCONFIG_NO_FORMATTING
1606 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1607 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1608 addTest(root
, &TestFScanset
, "file/TestFScanset");
1609 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1610 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1611 addTest(root
, &TestVargs
, "file/TestVargs");
1612 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");