2 **********************************************************************
3 * Copyright (C) 2004-2011, 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";
26 #if !UCONFIG_NO_FORMATTING
27 static void TestFileFromICU(UFILE
*myFile
) {
29 float myFloat
= -1234.0;
30 int32_t newValuePtr
[1];
31 double newDoubleValuePtr
[1];
33 UChar uStringBuf
[256];
34 char myString
[256] = "";
35 char testBuf
[256] = "";
37 U_STRING_DECL(myStringOrig
, "My-String", 9);
39 U_STRING_INIT(myStringOrig
, "My-String", 9);
40 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
41 u_memset(uStringBuf
, 0x2a, sizeof(uStringBuf
)/sizeof(*uStringBuf
));
42 memset(myString
, '*', sizeof(myString
)/sizeof(*myString
));
43 memset(testBuf
, '*', sizeof(testBuf
)/sizeof(*testBuf
));
46 log_err("Can't write test file.\n");
51 if (sizeof(void *) == 4) {
52 origPtr
= (void *)0xdeadbeef;
53 } else if (sizeof(void *) == 8) {
54 origPtr
= (void *) INT64_C(0x1000200030004000);
55 } else if (sizeof(void *) == 16) {
60 } massiveBigEndianPtr
= {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
61 origPtr
= massiveBigEndianPtr
.ptr
;
63 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
67 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
68 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
69 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
70 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
71 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
72 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
73 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
74 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
75 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
76 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
77 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
78 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
79 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
80 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
81 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
82 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
83 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
84 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
85 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
86 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
89 log_err("Got feof while writing the file.\n");
93 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
94 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
95 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
97 u_fprintf(myFile
, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
98 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
); /* Should be 27 as stated later on. */
101 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
103 if (myFile
== NULL
) {
104 log_err("Can't read test file.");
108 if (u_feof(myFile
)) {
109 log_err("Got feof while reading the file and not at the end of the file.\n");
112 myUString
[0] = u_fgetc(myFile
);
113 if (myUString
[0] != 0x53 /* S */) {
114 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
116 u_fungetc(myUString
[0], myFile
);
117 myUString
[0] = u_fgetc(myFile
);
118 if (myUString
[0] != 0x53 /* S */) {
119 log_err("u_fgetc 2 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 3 returned %X. Expected 'S'.", myString
[0]);
126 u_fungetc(myUString
[0], myFile
);
127 myUString
[0] = u_fgetc(myFile
);
128 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
129 myUString
[2] = u_fgetc(myFile
);
130 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
131 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
133 u_fungetc(myUString
[2], myFile
);
134 u_fungetc(myUString
[1], myFile
);
135 u_fungetc(myUString
[0], myFile
);
140 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
141 if (*n
!= *newValuePtr
) {
142 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
145 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
146 if (*n
!= *newValuePtr
) {
147 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
150 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
151 if (*n
!= *newValuePtr
) {
152 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
155 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
156 if (*n
!= *newValuePtr
) {
157 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
160 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
161 if (*n
!= *newValuePtr
) {
162 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
165 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
166 if (*n
!= *newValuePtr
) {
167 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
169 *newDoubleValuePtr
= -1.0;
170 u_fscanf(myFile
, "Float %%f: %lf\n", newDoubleValuePtr
);
171 if (myFloat
!= *newDoubleValuePtr
) {
172 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
174 *newDoubleValuePtr
= -1.0;
175 u_fscanf(myFile
, "Lowercase float %%e: %le\n", newDoubleValuePtr
);
176 if (myFloat
!= *newDoubleValuePtr
) {
177 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
179 *newDoubleValuePtr
= -1.0;
180 u_fscanf(myFile
, "Uppercase 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
, "Lowercase float %%g: %lg\n", newDoubleValuePtr
);
186 if (myFloat
!= *newDoubleValuePtr
) {
187 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
189 *newDoubleValuePtr
= -1.0;
190 u_fscanf(myFile
, "Uppercase float %%G: %lG\n", newDoubleValuePtr
);
191 if (myFloat
!= *newDoubleValuePtr
) {
192 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
195 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
196 if (ptr
!= origPtr
) {
197 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
199 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
200 if (*myString
!= 'A') {
201 log_err("%%c Got: %c, Expected: A\n", *myString
);
203 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
204 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
205 log_err("%%C Got: %C, Expected: A\n", *myUString
);
207 u_fscanf(myFile
, "String %%s: %s\n", myString
);
208 if (strcmp(myString
, "My-String")) {
209 log_err("%%s Got: %s, Expected: My String\n", myString
);
211 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
212 if (strcmp(myString
, "(null)")) {
213 log_err("%%s Got: %s, Expected: My String\n", myString
);
215 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
216 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
217 if (strcmp(myString
, "My-String")) {
218 log_err("%%S Got: %S, Expected: My String\n", myUString
);
220 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
221 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
222 if (strcmp(myString
, "(null)")) {
223 log_err("%%S Got: %S, Expected: My String\n", myUString
);
225 *newDoubleValuePtr
= -1.0;
226 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
227 if (myFloat
!= *newDoubleValuePtr
) {
228 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
230 *newDoubleValuePtr
= -1.0;
231 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
232 if (myFloat
!= *newDoubleValuePtr
) {
233 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
236 u_fgets(myUString
, 4, myFile
);
239 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
240 if (myString
== NULL
|| strcmp(myString
, "\t\n") != 0) {
241 log_err("u_fgets got \"%s\"\n", myString
);
244 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
245 log_err("u_fgets did not return myUString\n");
247 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
248 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
249 log_err("u_fgets got \"%s\"\n", myString
);
252 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
253 log_err("u_fgets did not return myUString\n");
255 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
256 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
257 log_err("u_fgets got \"%s\"\n", myString
);
260 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
261 log_err("u_fgets did not return myUString\n");
263 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
264 if (myString
== NULL
|| strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
265 log_err("u_fgets got \"%s\"\n", myString
);
268 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
269 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
272 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
273 log_err("u_fgets did not return myUString\n");
275 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
276 if (myString
== NULL
|| strcmp(myString
, "") != 0) {
277 log_err("u_fgets got \"%s\"\n", myString
);
280 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
281 log_err("u_fgets did not return myUString\n");
283 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
284 if (myString
== NULL
|| strcmp(myString
, "\t") != 0) {
285 log_err("u_fgets got \"%s\"\n", myString
);
288 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
289 sizeof(myUString
)/sizeof(*myUString
));
290 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
291 log_err("u_fgets got \"%s\"\n", myString
);
294 if (u_feof(myFile
)) {
295 log_err("Got feof while reading the file and not at the end of the file.\n");
297 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
298 sizeof(myUString
)/sizeof(*myUString
));
299 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
300 log_err("u_fgets got \"%s\"\n", myString
);
302 if (!u_feof(myFile
)) {
303 log_err("Did not get feof while reading the end of the file.\n");
305 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
306 log_err("u_fscanf read data while reading the end of the file.\n");
312 static void TestFile(void) {
313 /* FILE *standardFile;*/
315 log_verbose("Testing u_fopen\n");
316 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
));
318 /* Don't know how to make this work without stdout or stderr */
320 log_verbose("Testing u_finit\n");
321 standardFile = fopen(STANDARD_TEST_FILE, "wb");
322 TestFileFromICU(u_finit(standardFile, NULL, NULL));
323 fclose(standardFile);
325 log_verbose("Testing u_fadopt\n");
326 standardFile = fopen(STANDARD_TEST_FILE, "wb");
327 TestFileFromICU(u_fadopt(standardFile, NULL, NULL));
332 static void StdinBuffering(void) {
336 UFILE
*uStdIn
= NULL
;
337 UFILE
*uStdOut
= NULL
;
338 uStdIn
= u_finit(stdin
, NULL
, NULL
);
339 uStdOut
= u_finit(stdout
, NULL
, NULL
);
345 u_fgets(buff
, sizeof(buff
)/sizeof(buff
[0]), uStdIn
);
346 u_fprintf(uStdOut
, "%S\n", buff
);
347 u_fscanf(uStdIn
, "%d", &num
);
348 u_fprintf(uStdOut
, "%d\n", num
);
349 u_fscanf(uStdIn
, "%d", &num
);
350 u_fprintf(uStdOut
, "%d\n", num
);
352 log_verbose("Test disabled because it requires user interaction");
356 static void TestCodepageAndLocale(void) {
357 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
358 if (myFile
== NULL
) {
359 log_err("Can't write test file.\n");
362 if (u_fgetcodepage(myFile
) == NULL
363 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
365 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
366 u_fgetcodepage(myFile
), ucnv_getDefaultName());
368 #if !UCONFIG_NO_FORMATTING
369 if (u_fgetlocale(myFile
) == NULL
370 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
372 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
373 u_fgetlocale(myFile
), uloc_getDefault());
378 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
379 if (u_fgetcodepage(myFile
) == NULL
380 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
382 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
383 u_fgetcodepage(myFile
));
385 #if !UCONFIG_NO_FORMATTING
386 if (u_fgetlocale(myFile
) == NULL
387 || strcmp(u_fgetlocale(myFile
), "es") != 0)
389 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
390 u_fgetlocale(myFile
), "es");
395 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
396 if (u_fgetcodepage(myFile
) == NULL
397 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
399 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
400 u_fgetcodepage(myFile
));
402 #if !UCONFIG_NO_FORMATTING
403 if (u_fgetlocale(myFile
) == NULL
404 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
406 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
407 u_fgetlocale(myFile
), uloc_getDefault());
412 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
413 if (u_fgetcodepage(myFile
) == NULL
414 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
416 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
417 u_fgetcodepage(myFile
));
419 #if !UCONFIG_NO_FORMATTING
420 if (u_fgetlocale(myFile
) == NULL
421 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
423 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
424 u_fgetlocale(myFile
), "zh");
431 static void TestfgetsBuffers(void) {
433 UChar expectedBuffer
[2048];
434 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!";
435 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
436 int32_t expectedSize
= (int32_t)strlen(testStr
);
440 if (myFile
== NULL
) {
441 log_err("Can't write test file.\n");
445 u_fputc(0x3BC, myFile
);
446 if (u_fputc(0x110000, myFile
) != U_EOF
) {
447 log_err("u_fputc should return U_EOF for 0x110000.\n");
449 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
450 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
452 u_fputc(0xFF41, myFile
);
453 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
454 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
455 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
456 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
457 u_file_write(buffer
, expectedSize
, myFile
);
458 u_strcat(expectedBuffer
, buffer
);
462 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
463 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
464 if (u_fgetc(myFile
) != 0x3BC) {
465 log_err("The first character is wrong\n");
467 if (u_fgetc(myFile
) != 0xFF41) {
468 log_err("The second character is wrong\n");
470 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
471 log_err("Didn't get the buffer back\n");
474 readSize
= u_strlen(buffer
);
475 if (readSize
!= expectedSize
*repetitions
) {
476 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
478 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
479 log_err("u_fgets wrote too much data\n");
481 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
482 log_err("Did get expected string back\n");
484 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
485 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
489 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
491 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
493 u_fputc(0x3BC, myFile
);
494 u_fputc(0xFF41, myFile
);
495 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
496 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
497 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
498 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
499 u_file_write(buffer
, expectedSize
, myFile
);
500 u_strcat(expectedBuffer
, buffer
);
504 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
505 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
506 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
507 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
509 if (u_fgetc(myFile
) != 0x3BC) {
510 log_err("The first character is wrong\n");
512 if (u_fgetc(myFile
) != 0xFF41) {
513 log_err("The second character is wrong\n");
515 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
516 log_err("Didn't get the buffer back\n");
519 readSize
= u_strlen(buffer
);
520 if (readSize
!= expectedSize
*repetitions
) {
521 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
523 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
524 log_err("u_fgets wrote too much data\n");
526 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
527 log_err("Did get expected string back\n");
532 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
534 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
536 u_fputc(0xFF41, myFile
);
537 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
538 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
539 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
540 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
541 u_file_write(buffer
, expectedSize
, myFile
);
542 u_strcat(expectedBuffer
, buffer
);
546 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
547 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
548 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
549 log_err("Didn't get the buffer back\n");
552 readSize
= u_strlen(buffer
);
554 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
556 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
557 log_err("Did get expected string back\n");
559 if (buffer
[2] != 0xBEEF) {
560 log_err("u_fgets wrote too much data\n");
566 static void TestFileReadBuffering(void) {
568 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
572 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
573 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
574 u_file_write(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
578 u_memset(buffer
, 0xDEAD, sizeof(buffer
)/sizeof(buffer
[0]));
579 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
580 how_many
= u_file_read(buffer
, 1024, myFile
);
581 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
582 log_err("u_file_read read too much or not enough data\n");
587 static void TestfgetsLineCount(void) {
589 UChar expectedBuffer
[2048];
590 char charBuffer
[2048];
591 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!";
592 UFILE
*myFile
= NULL
;
593 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
594 int32_t expectedSize
= (int32_t)strlen(testStr
);
596 int32_t nlRepetitions
;
598 if (stdFile
== NULL
) {
599 log_err("Can't write test file.\n");
602 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
604 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
605 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
606 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
607 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
612 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
613 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
616 char *returnedCharBuffer
;
617 UChar
*returnedUCharBuffer
;
619 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
620 returnedCharBuffer
= fgets(charBuffer
, sizeof(charBuffer
)/sizeof(charBuffer
[0]), stdFile
);
621 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
623 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
624 /* Both returned NULL. stop. */
627 if (returnedCharBuffer
!= charBuffer
) {
628 log_err("Didn't get the charBuffer back\n");
631 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
632 if (returnedUCharBuffer
!= buffer
) {
633 log_err("Didn't get the buffer back\n");
636 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
637 log_err("buffers are different\n");
639 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
640 log_err("u_fgets wrote too much\n");
647 static void TestfgetsNewLineHandling(void) {
649 static const UChar testUStr
[][16] = {
666 {0x00A0, 0x000D, 0x000A, 0},
668 {0xd800, 0xdfff, 0x000D, 0},
681 UFILE
*myFile
= NULL
;
684 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
685 if (myFile
== NULL
) {
686 log_err("Can't write test file.\n");
689 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
690 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
694 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
696 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
697 UChar
*returnedUCharBuffer
;
699 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
700 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
702 if (!returnedUCharBuffer
) {
703 /* Returned NULL. stop. */
706 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
707 log_err("buffers are different at index = %d\n", lineIdx
);
709 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
710 log_err("u_fgets wrote too much\n");
713 if (lineIdx
!= (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0]))) {
714 log_err("u_fgets read too much\n");
716 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != NULL
) {
717 log_err("u_file_write wrote too much\n");
722 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
724 UChar expectedBuffer
[64];
725 int32_t lineLen
= strlen(line
);
726 UChar
*returnedUCharBuffer
;
728 UFILE
*myFile
= NULL
;
729 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
731 if (stdFile
== NULL
) {
732 log_err("Can't write test file.\n");
735 /* Write a prefix line and then write a bunch of lines */
736 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
737 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
738 fwrite(line
, lineLen
, 1, stdFile
);
742 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
743 if (myFile
== NULL
) {
744 log_err("Can't read test file.\n");
748 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
749 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
750 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
751 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
752 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
756 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
757 for (repetitions
= 0; ; repetitions
++) {
758 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
759 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
761 if (!returnedUCharBuffer
) {
762 /* returned NULL. stop. */
765 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
766 log_err("buffers are different at count %d\n", repetitions
);
768 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
769 log_err("u_fgets wrote too much\n");
772 if (repetitions
!= numRepititions
) {
773 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
778 static void TestfgetsNewLineCount(void) {
779 /* This makes sure that lines are correctly handled between buffer boundaries. */
780 TestLineCount("\n", "\n", 1024); /* Unix newlines */
781 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
782 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
783 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
784 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
785 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
788 static void TestFgetsLineBuffering(void) {
789 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
790 UChar
*returnedUCharBuffer
;
792 UFILE
*myFile
= NULL
;
793 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
795 if (stdFile
== NULL
) {
796 log_err("Can't write test file.\n");
799 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
801 /* Write one very long line */
802 for (repetitions
= 0; repetitions
< ((sizeof(buffer
)/sizeof(buffer
[0]))*2); repetitions
++) {
803 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
807 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
808 if (myFile
== NULL
) {
809 log_err("Can't read test file.\n");
813 /* Read part of one very long line */
814 returnedUCharBuffer
= u_fgets(buffer
, (sizeof(buffer
)/sizeof(buffer
[0]))-1, myFile
);
815 if (u_strlen(returnedUCharBuffer
) != ((sizeof(buffer
)/sizeof(buffer
[0]))-2)) {
816 log_err("Line is wrong length. Got %d. Expected %d.\n",
817 u_strlen(returnedUCharBuffer
), ((sizeof(buffer
)/sizeof(buffer
[0]))-2));
819 /* We better not read too much */
820 if (buffer
[(sizeof(buffer
)/sizeof(buffer
[0]))-1] != 0xBEEF) {
821 log_err("Too much data was written\n");
828 static void TestCodepage(void) {
829 UFILE
*myFile
= NULL
;
830 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
831 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
832 UChar testBuf
[sizeof(strABAccentA
)/sizeof(strABAccentA
[0])*2]; /* *2 to see if too much was */
833 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
835 UErrorCode status
= U_ZERO_ERROR
;
837 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
840 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
844 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
845 if (myFile
== NULL
) {
846 log_err("Can't write test file for iso-8859-1.\n");
849 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
850 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
852 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
855 /* Now see what we got wrote */
856 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
857 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
858 log_err("u_fsetcodepage didn't set the codepage\n");
860 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
861 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
862 log_err("The test data was read and written differently!\n");
864 if (retVal
!= u_strlen(strABAccentA
)) {
865 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
869 /* What happens on invalid input? */
870 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
871 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
872 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
874 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
875 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
877 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
878 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
880 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
881 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
882 log_err("The test data wasn't subsituted as expected\n");
886 /* Can't currently swap codepages midstream */
887 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
888 strcpy(convName
, u_fgetcodepage(myFile
));
889 u_file_read(testBuf
, 1, myFile
);
890 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
891 log_err("u_fsetcodepage set the codepage after reading a byte\n");
893 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
894 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
895 log_err("u_fsetcodepage changed the codepages after writing data\n");
897 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
898 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
901 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
902 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
903 log_err("The test data was read and written differently!\n");
905 if (retVal
!= u_strlen(strABAccentA
)) {
906 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
912 static void TestCodepageFlush(void) {
913 #if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING
914 log_verbose("Skipping, legacy conversion or formatting is disabled.");
916 UChar utf16String
[] = { 0x39, 0x39, 0x39, 0x20, 0x65E0, 0x6CD6, 0x5728, 0x0000 };
919 const char *enc
= "IBM-1388"; /* GBK EBCDIC stateful */
920 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", "en_US_POSIX", enc
);
925 if (myFile
== NULL
) {
926 log_err("Can't write test file %s\n", STANDARD_TEST_FILE
);
930 u_fprintf(myFile
, "%S", utf16String
);
933 /* now read it back */
934 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
935 if (myCFile
== NULL
) {
936 log_err("Can't read test file.");
940 inLen
= fread(inBuf
, 1, 200, myCFile
);
944 log_err("Failed during read of test file.");
948 /* check if shift in and out */
949 for(i
=0;i
<(int32_t)inLen
;i
++) {
950 if(inBuf
[i
]==0x0E) { /* SO */
952 } else if(inBuf
[i
]==0x0F) { /* SI */
958 log_err("Err: shift was unchanged\n");
959 } else if(shift
==1) {
960 log_err("Err: at end of string, we were still shifted out (SO, 0x0E).\n");
961 } else if(shift
==-1) {
962 log_verbose("OK: Shifted in (SI, 0x0F)\n");
966 log_err("Expected 12 bytes, read %d\n", inLen
);
968 log_verbose("OK: read %d bytes\n", inLen
);
975 #if !UCONFIG_NO_FORMATTING
976 static void TestFilePrintCompatibility(void) {
977 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", "en_US_POSIX", NULL
);
981 static const UChar emptyStr
[] = {0};
982 char readBuf
[512] = "";
983 char testBuf
[512] = "";
985 if (myFile
== NULL
) {
986 log_err("Can't write test file.\n");
989 #if !UCONFIG_NO_FORMATTING
990 if (strcmp(u_fgetlocale(myFile
), "en_US_POSIX") != 0) {
991 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
995 /* Compare against C API compatibility */
996 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
997 u_fprintf(myFile
, "%x ", num
);
998 u_fprintf(myFile
, "%X ", num
);
999 u_fprintf(myFile
, "%o ", num
);
1000 u_fprintf(myFile
, "%d ", num
);
1001 u_fprintf(myFile
, "%i ", num
);
1002 u_fprintf(myFile
, "%f ", (double)num
);
1003 /* u_fprintf(myFile, "%e ", (double)num);
1004 u_fprintf(myFile, "%E ", (double)num);*/
1005 u_fprintf(myFile
, "%g ", (double)num
);
1006 u_fprintf(myFile
, "%G", (double)num
);
1007 u_fputs(emptyStr
, myFile
);
1010 u_fprintf_u(myFile
, NEW_LINE
);
1012 for (num
= 0; num
< 0x80; num
++) {
1013 u_fprintf(myFile
, "%c", num
);
1017 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
1018 if (myCFile
== NULL
) {
1019 log_err("Can't read test file.");
1023 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
1024 fscanf(myCFile
, "%s", readBuf
);
1025 sprintf(testBuf
, "%x", (int)num
);
1026 if (strcmp(readBuf
, testBuf
) != 0) {
1027 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1030 fscanf(myCFile
, "%s", readBuf
);
1031 sprintf(testBuf
, "%X", (int)num
);
1032 if (strcmp(readBuf
, testBuf
) != 0) {
1033 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1036 fscanf(myCFile
, "%s", readBuf
);
1037 sprintf(testBuf
, "%o", (int)num
);
1038 if (strcmp(readBuf
, testBuf
) != 0) {
1039 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1042 /* fprintf is not compatible on all platforms e.g. the iSeries */
1043 fscanf(myCFile
, "%s", readBuf
);
1044 sprintf(testBuf
, "%d", (int)num
);
1045 if (strcmp(readBuf
, testBuf
) != 0) {
1046 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1049 fscanf(myCFile
, "%s", readBuf
);
1050 sprintf(testBuf
, "%i", (int)num
);
1051 if (strcmp(readBuf
, testBuf
) != 0) {
1052 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1055 fscanf(myCFile
, "%s", readBuf
);
1056 sprintf(testBuf
, "%f", (double)num
);
1057 if (strcmp(readBuf
, testBuf
) != 0) {
1058 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1061 /* fscanf(myCFile, "%s", readBuf);
1062 sprintf(testBuf, "%e", (double)num);
1063 if (strcmp(readBuf, testBuf) != 0) {
1064 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1067 fscanf(myCFile, "%s", readBuf);
1068 sprintf(testBuf, "%E", (double)num);
1069 if (strcmp(readBuf, testBuf) != 0) {
1070 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1073 fscanf(myCFile
, "%s", readBuf
);
1074 sprintf(testBuf
, "%g", (double)num
);
1075 if (strcmp(readBuf
, testBuf
) != 0) {
1076 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1079 fscanf(myCFile
, "%s", readBuf
);
1080 sprintf(testBuf
, "%G", (double)num
);
1081 if (strcmp(readBuf
, testBuf
) != 0) {
1082 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1086 /* Properly eat the newlines */
1087 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1088 fscanf(myCFile
, "%c", &cVal
);
1089 if (cVal
!= C_NEW_LINE
[num
]) {
1090 log_err("OS newline error\n");
1093 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1094 fscanf(myCFile
, "%c", &cVal
);
1095 if (cVal
!= C_NEW_LINE
[num
]) {
1096 log_err("ustdio newline error\n");
1100 for (num
= 0; num
< 0x80; num
++) {
1102 fscanf(myCFile
, "%c", &cVal
);
1104 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1111 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1112 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
1113 if (myFile == NULL) {\
1114 log_err("Can't write test file for %s.\n", uFormat);\
1117 /* Reinitialize the buffer to verify null termination works. */\
1118 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1119 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
1121 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1123 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
1124 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
1126 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1127 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1128 if (strcmp(buffer, compBuffer) != 0) {\
1129 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1131 if (cNumPrinted != uNumPrinted) {\
1132 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1134 if (buffer[uNumPrinted+1] != '*') {\
1135 log_err("%" uFormat " too much stored\n");\
1138 #if !UCONFIG_NO_FORMATTING
1139 static void TestFprintfFormat(void) {
1140 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1141 static const char abcChars
[] = "abc";
1144 char compBuffer
[256];
1145 int32_t uNumPrinted
;
1146 int32_t cNumPrinted
;
1149 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1150 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1151 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1153 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1154 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1155 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1157 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1158 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1160 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1161 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1163 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1164 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1165 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1166 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1167 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1168 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1169 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1170 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1171 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1173 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1174 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1175 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1176 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1177 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1178 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1179 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1180 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1181 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1182 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1184 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1185 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1186 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1188 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1189 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1190 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1191 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1192 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1194 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1195 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1196 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1197 TestFPrintFormat("%#o", 123, "%#o", 123);
1198 TestFPrintFormat("%#o", -123, "%#o", -123);
1200 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1201 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1202 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1203 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1204 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1205 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1206 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1207 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1209 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1210 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1211 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1212 TestFPrintFormat("% d", 123456, "% d", 123456);
1213 TestFPrintFormat("% d", -123456, "% d", -123456);
1215 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1216 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1217 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1219 log_verbose("Get really crazy with the formatting.\n");
1221 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1222 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1223 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1224 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1226 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1227 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1228 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1229 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1230 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1231 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1232 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1233 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1234 TestFPrintFormat("%12d", 123, "%12d", 123);
1235 TestFPrintFormat("%12d", -123, "%12d", -123);
1236 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1237 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1239 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1240 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1241 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1242 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1243 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1244 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1245 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1246 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1247 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1248 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1249 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1250 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1251 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1252 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1254 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1255 /* Reinitialize the buffer to verify null termination works. */
1256 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1257 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1259 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1261 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1262 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1264 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1265 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1266 if (strcmp(buffer
, compBuffer
) != 0) {
1267 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1269 if (cNumPrinted
!= uNumPrinted
) {
1270 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1272 if (buffer
[uNumPrinted
+1] != '*') {
1273 log_err("%%d %% d %%d too much stored\n");
1278 #undef TestFPrintFormat
1280 #if !UCONFIG_NO_FORMATTING
1281 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1285 char compBuffer
[256];
1286 int32_t uNumScanned
;
1287 int32_t cNumScanned
;
1289 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1290 if (myFile
== NULL
) {
1291 log_err("Can't write test file for %s.\n", format
);
1294 /* Reinitialize the buffer to verify null termination works. */
1295 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1296 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1297 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1298 buffer
[sizeof(buffer
)/sizeof(*buffer
)-1] = 0;
1300 u_fprintf(myFile
, "%S", uValue
);
1302 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1303 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1305 if (expectedToPass
) {
1306 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1307 cNumScanned
= sscanf(cValue
, format
, buffer
);
1308 if (strncmp(buffer
, compBuffer
, sizeof(buffer
)/sizeof(*buffer
)) != 0) {
1309 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1311 if (cNumScanned
!= uNumScanned
) {
1312 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1314 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1315 log_err("%s too much stored\n", format
);
1319 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1320 log_err("%s too much stored on a failure\n", format
);
1326 #if !UCONFIG_NO_FORMATTING
1327 static void TestFScanset(void) {
1328 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1329 static const char abcChars
[] = "abccdefg";
1331 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1332 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1334 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1335 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1337 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1338 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1340 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1341 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1343 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1344 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1345 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1346 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1347 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1349 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1350 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1352 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1354 /* Extra long string for testing */
1355 TestFScanSetFormat(" %[qrst]",
1356 abcUChars
, abcChars
, TRUE
);
1358 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1361 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1362 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1363 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1364 /* The following is not deterministic on Windows */
1365 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1367 /* TODO: Need to specify precision with a "*" */
1370 #if !UCONFIG_NO_FORMATTING
1371 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1374 int32_t uNumScanned
;
1376 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1377 if (myFile
== NULL
) {
1378 log_err("Can't write test file for %s.\n", format
);
1381 /* Reinitialize the buffer to verify null termination works. */
1382 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1383 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1385 u_fprintf(myFile
, "%S", uValue
);
1387 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1388 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1390 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1391 log_err("%s too much stored on a failure\n", format
);
1395 #if !UCONFIG_NO_FORMATTING
1396 static void TestBadScanfFormat(void) {
1397 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1398 static const char abcChars
[] = "abccdefg";
1400 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1403 #if !UCONFIG_NO_FORMATTING
1404 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1406 UChar uBuffer2
[256];
1411 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "UTF-8");
1413 log_err("Test file can't be opened\n");
1417 va_start(ap
, format
);
1418 count
= u_vfprintf(myFile
, format
, ap
);
1424 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", "UTF-8");
1426 log_err("Test file can't be opened\n");
1429 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1430 u_uastrcpy(uBuffer2
, expectedResult
);
1431 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1432 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1437 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1439 log_err("Test file can't be opened\n");
1442 u_uastrcpy(uBuffer
, format
);
1444 va_start(ap
, format
);
1445 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1451 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1453 log_err("Test file can't be opened\n");
1456 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1457 u_uastrcpy(uBuffer2
, expectedResult
);
1458 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1459 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1464 static void TestVargs(void) {
1465 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1469 static void TestUnicodeFormat(void)
1471 #if !UCONFIG_NO_FORMATTING
1472 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1473 UChar myUString
[256];
1475 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1476 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1478 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
1480 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1482 log_err("Test file can't be opened\n");
1485 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1488 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1490 log_err("Test file can't be opened\n");
1493 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1495 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1496 log_err("u_fscanf_u doesn't work.\n");
1501 static void TestFileWriteRetval(const char * a_pszEncoding
) {
1505 int32_t expected
= 10000; /* test with large data to test internal buffer looping */
1506 UChar testChar
= 0xBEEF;
1508 if (!*a_pszEncoding
|| 0 == strcmp(a_pszEncoding
, "ASCII")) {
1509 testChar
= 0x65; /* 'A' - otherwise read test will fail */
1512 buffer
= (UChar
*) malloc(expected
* sizeof(UChar
));
1514 log_err("Out of memory\n");
1519 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, a_pszEncoding
);
1522 log_err("Test file can't be opened for write\n");
1525 u_memset(buffer
, testChar
, expected
);
1526 count
= u_file_write(buffer
, expected
, myFile
);
1528 if (count
!= expected
) {
1530 log_err("u_file_write returned incorrect number of characters written\n");
1538 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, a_pszEncoding
);
1540 log_err("Test file can't be opened for read\n");
1543 for (count
= 0; count
< expected
; ++count
) {
1544 UChar gotChar
= u_fgetc(myFile
);
1545 if(gotChar
!= testChar
) {
1546 log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar
, testChar
);
1551 if (u_fgetc(myFile
) != U_EOF
) {
1552 log_err("u_fgetc did not return expected EOF\n");
1559 static void TestFileWriteRetvalUTF16(void) {
1560 TestFileWriteRetval("UTF-16");
1563 static void TestFileWriteRetvalUTF8(void) {
1564 TestFileWriteRetval("UTF-8");
1567 static void TestFileWriteRetvalASCII(void) {
1568 TestFileWriteRetval("ASCII");
1571 static void TestFileWriteRetvalNONE(void) {
1572 TestFileWriteRetval("");
1576 addFileTest(TestNode
** root
) {
1577 #if !UCONFIG_NO_FORMATTING
1578 addTest(root
, &TestFile
, "file/TestFile");
1580 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1581 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1582 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1583 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1584 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1585 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1586 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1587 addTest(root
, &TestCodepage
, "file/TestCodepage");
1588 addTest(root
, &TestCodepageFlush
, "file/TestCodepageFlush");
1589 addTest(root
, &TestFileWriteRetvalUTF16
, "file/TestFileWriteRetvalUTF16");
1590 addTest(root
, &TestFileWriteRetvalUTF8
, "file/TestFileWriteRetvalUTF8");
1591 addTest(root
, &TestFileWriteRetvalASCII
, "file/TestFileWriteRetvalASCII");
1592 addTest(root
, &TestFileWriteRetvalNONE
, "file/TestFileWriteRetvalNONE");
1593 #if !UCONFIG_NO_FORMATTING
1594 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1595 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1596 addTest(root
, &TestFScanset
, "file/TestFScanset");
1597 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1598 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1599 addTest(root
, &TestVargs
, "file/TestVargs");
1600 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");