2 **********************************************************************
3 * Copyright (C) 2004-2008, 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"
22 const char *STANDARD_TEST_FILE
= "iotest-c.txt";
25 #if !UCONFIG_NO_FORMATTING
26 static void TestFileFromICU(UFILE
*myFile
) {
28 float myFloat
= -1234.0;
29 int32_t newValuePtr
[1];
30 double newDoubleValuePtr
[1];
32 UChar uStringBuf
[256];
33 char myString
[256] = "";
34 char testBuf
[256] = "";
36 U_STRING_DECL(myStringOrig
, "My-String", 9);
38 U_STRING_INIT(myStringOrig
, "My-String", 9);
39 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
40 u_memset(uStringBuf
, 0x2a, sizeof(uStringBuf
)/sizeof(*uStringBuf
));
41 memset(myString
, '*', sizeof(myString
)/sizeof(*myString
));
42 memset(testBuf
, '*', sizeof(testBuf
)/sizeof(*testBuf
));
45 log_err("Can't write test file.\n");
50 if (sizeof(void *) == 4) {
51 origPtr
= (void *)0xdeadbeef;
52 } else if (sizeof(void *) == 8) {
53 origPtr
= (void *) INT64_C(0x1000200030004000);
54 } else if (sizeof(void *) == 16) {
59 } massiveBigEndianPtr
= {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
60 origPtr
= massiveBigEndianPtr
.ptr
;
62 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
66 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
67 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
68 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
69 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
70 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
71 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
72 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
73 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
74 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
75 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
76 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
77 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
78 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
79 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
80 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
81 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
82 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
83 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
84 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
85 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
88 log_err("Got feof while writing the file.\n");
92 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
93 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
94 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
96 u_fprintf(myFile
, "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
97 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
); /* Should be 27 as stated later on. */
100 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
102 if (myFile
== NULL
) {
103 log_err("Can't read test file.");
107 if (u_feof(myFile
)) {
108 log_err("Got feof while reading the file and not at the end of the file.\n");
111 myUString
[0] = u_fgetc(myFile
);
112 if (myUString
[0] != 0x53 /* S */) {
113 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
115 u_fungetc(myUString
[0], myFile
);
116 myUString
[0] = u_fgetc(myFile
);
117 if (myUString
[0] != 0x53 /* S */) {
118 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString
[0]);
120 u_fungetc(myUString
[0], myFile
);
121 myUString
[0] = u_fgetc(myFile
);
122 if (myUString
[0] != 0x53 /* S */) {
123 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString
[0]);
125 u_fungetc(myUString
[0], myFile
);
126 myUString
[0] = u_fgetc(myFile
);
127 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
128 myUString
[2] = u_fgetc(myFile
);
129 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
130 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
132 u_fungetc(myUString
[2], myFile
);
133 u_fungetc(myUString
[1], myFile
);
134 u_fungetc(myUString
[0], myFile
);
139 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
140 if (*n
!= *newValuePtr
) {
141 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
144 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
145 if (*n
!= *newValuePtr
) {
146 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
149 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
150 if (*n
!= *newValuePtr
) {
151 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
154 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
155 if (*n
!= *newValuePtr
) {
156 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
159 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
160 if (*n
!= *newValuePtr
) {
161 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
164 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
165 if (*n
!= *newValuePtr
) {
166 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
168 *newDoubleValuePtr
= -1.0;
169 u_fscanf(myFile
, "Float %%f: %lf\n", newDoubleValuePtr
);
170 if (myFloat
!= *newDoubleValuePtr
) {
171 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
173 *newDoubleValuePtr
= -1.0;
174 u_fscanf(myFile
, "Lowercase float %%e: %le\n", newDoubleValuePtr
);
175 if (myFloat
!= *newDoubleValuePtr
) {
176 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
178 *newDoubleValuePtr
= -1.0;
179 u_fscanf(myFile
, "Uppercase float %%E: %lE\n", newDoubleValuePtr
);
180 if (myFloat
!= *newDoubleValuePtr
) {
181 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr
, myFloat
);
183 *newDoubleValuePtr
= -1.0;
184 u_fscanf(myFile
, "Lowercase float %%g: %lg\n", newDoubleValuePtr
);
185 if (myFloat
!= *newDoubleValuePtr
) {
186 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
188 *newDoubleValuePtr
= -1.0;
189 u_fscanf(myFile
, "Uppercase float %%G: %lG\n", newDoubleValuePtr
);
190 if (myFloat
!= *newDoubleValuePtr
) {
191 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
194 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
195 if (ptr
!= origPtr
) {
196 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
198 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
199 if (*myString
!= 'A') {
200 log_err("%%c Got: %c, Expected: A\n", *myString
);
202 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
203 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
204 log_err("%%C Got: %C, Expected: A\n", *myUString
);
206 u_fscanf(myFile
, "String %%s: %s\n", myString
);
207 if (strcmp(myString
, "My-String")) {
208 log_err("%%s Got: %s, Expected: My String\n", myString
);
210 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
211 if (strcmp(myString
, "(null)")) {
212 log_err("%%s Got: %s, Expected: My String\n", myString
);
214 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
215 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
216 if (strcmp(myString
, "My-String")) {
217 log_err("%%S Got: %S, Expected: My String\n", myUString
);
219 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
220 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
221 if (strcmp(myString
, "(null)")) {
222 log_err("%%S Got: %S, Expected: My String\n", myUString
);
224 *newDoubleValuePtr
= -1.0;
225 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
226 if (myFloat
!= *newDoubleValuePtr
) {
227 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
229 *newDoubleValuePtr
= -1.0;
230 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
231 if (myFloat
!= *newDoubleValuePtr
) {
232 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
235 u_fgets(myUString
, 4, myFile
);
238 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
239 if (myString
== NULL
|| strcmp(myString
, "\t\n") != 0) {
240 log_err("u_fgets got \"%s\"\n", myString
);
243 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
244 log_err("u_fgets did not return myUString\n");
246 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
247 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
248 log_err("u_fgets got \"%s\"\n", myString
);
251 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
252 log_err("u_fgets did not return myUString\n");
254 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
255 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
256 log_err("u_fgets got \"%s\"\n", myString
);
259 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
260 log_err("u_fgets did not return myUString\n");
262 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
263 if (myString
== NULL
|| strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
264 log_err("u_fgets got \"%s\"\n", myString
);
267 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
268 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
271 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
272 log_err("u_fgets did not return myUString\n");
274 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
275 if (myString
== NULL
|| strcmp(myString
, "") != 0) {
276 log_err("u_fgets got \"%s\"\n", myString
);
279 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
280 log_err("u_fgets did not return myUString\n");
282 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
283 if (myString
== NULL
|| strcmp(myString
, "\t") != 0) {
284 log_err("u_fgets got \"%s\"\n", myString
);
287 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
288 sizeof(myUString
)/sizeof(*myUString
));
289 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
290 log_err("u_fgets got \"%s\"\n", myString
);
293 if (u_feof(myFile
)) {
294 log_err("Got feof while reading the file and not at the end of the file.\n");
296 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
297 sizeof(myUString
)/sizeof(*myUString
));
298 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
299 log_err("u_fgets got \"%s\"\n", myString
);
301 if (!u_feof(myFile
)) {
302 log_err("Did not get feof while reading the end of the file.\n");
304 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
305 log_err("u_fscanf read data while reading the end of the file.\n");
311 static void TestFile(void) {
312 /* FILE *standardFile;*/
314 log_verbose("Testing u_fopen\n");
315 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
));
317 /* Don't know how to make this work without stdout or stderr */
319 log_verbose("Testing u_finit\n");
320 standardFile = fopen(STANDARD_TEST_FILE, "wb");
321 TestFileFromICU(u_finit(standardFile, NULL, NULL));
322 fclose(standardFile);
327 static void StdinBuffering(void) {
331 UFILE
*uStdIn
= NULL
;
332 UFILE
*uStdOut
= NULL
;
333 uStdIn
= u_finit(stdin
, NULL
, NULL
);
334 uStdOut
= u_finit(stdout
, NULL
, NULL
);
340 u_fgets(buff
, sizeof(buff
)/sizeof(buff
[0]), uStdIn
);
341 u_fprintf(uStdOut
, "%S\n", buff
);
342 u_fscanf(uStdIn
, "%d", &num
);
343 u_fprintf(uStdOut
, "%d\n", num
);
344 u_fscanf(uStdIn
, "%d", &num
);
345 u_fprintf(uStdOut
, "%d\n", num
);
347 log_verbose("Test disabled because it requires user interaction");
351 static void TestCodepageAndLocale(void) {
352 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
353 if (myFile
== NULL
) {
354 log_err("Can't write test file.\n");
357 if (u_fgetcodepage(myFile
) == NULL
358 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
360 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
361 u_fgetcodepage(myFile
), ucnv_getDefaultName());
363 #if !UCONFIG_NO_FORMATTING
364 if (u_fgetlocale(myFile
) == NULL
365 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
367 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
368 u_fgetlocale(myFile
), uloc_getDefault());
373 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
374 if (u_fgetcodepage(myFile
) == NULL
375 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
377 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
378 u_fgetcodepage(myFile
));
380 #if !UCONFIG_NO_FORMATTING
381 if (u_fgetlocale(myFile
) == NULL
382 || strcmp(u_fgetlocale(myFile
), "es") != 0)
384 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
385 u_fgetlocale(myFile
), "es");
390 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
391 if (u_fgetcodepage(myFile
) == NULL
392 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
394 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
395 u_fgetcodepage(myFile
));
397 #if !UCONFIG_NO_FORMATTING
398 if (u_fgetlocale(myFile
) == NULL
399 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
401 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
402 u_fgetlocale(myFile
), uloc_getDefault());
407 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
408 if (u_fgetcodepage(myFile
) == NULL
409 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
411 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
412 u_fgetcodepage(myFile
));
414 #if !UCONFIG_NO_FORMATTING
415 if (u_fgetlocale(myFile
) == NULL
416 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
418 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
419 u_fgetlocale(myFile
), "zh");
426 static void TestfgetsBuffers(void) {
428 UChar expectedBuffer
[2048];
429 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!";
430 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
431 int32_t expectedSize
= (int32_t)strlen(testStr
);
435 if (myFile
== NULL
) {
436 log_err("Can't write test file.\n");
440 u_fputc(0x3BC, myFile
);
441 if (u_fputc(0x110000, myFile
) != U_EOF
) {
442 log_err("u_fputc should return U_EOF for 0x110000.\n");
444 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
445 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
447 u_fputc(0xFF41, myFile
);
448 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
449 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
450 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
451 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
452 u_file_write(buffer
, expectedSize
, myFile
);
453 u_strcat(expectedBuffer
, buffer
);
457 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
458 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
459 if (u_fgetc(myFile
) != 0x3BC) {
460 log_err("The first character is wrong\n");
462 if (u_fgetc(myFile
) != 0xFF41) {
463 log_err("The second character is wrong\n");
465 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
466 log_err("Didn't get the buffer back\n");
469 readSize
= u_strlen(buffer
);
470 if (readSize
!= expectedSize
*repetitions
) {
471 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
473 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
474 log_err("u_fgets wrote too much data\n");
476 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
477 log_err("Did get expected string back\n");
479 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
480 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
484 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
486 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
488 u_fputc(0x3BC, myFile
);
489 u_fputc(0xFF41, myFile
);
490 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
491 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
492 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
493 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
494 u_file_write(buffer
, expectedSize
, myFile
);
495 u_strcat(expectedBuffer
, buffer
);
499 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
500 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
501 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
502 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
504 if (u_fgetc(myFile
) != 0x3BC) {
505 log_err("The first character is wrong\n");
507 if (u_fgetc(myFile
) != 0xFF41) {
508 log_err("The second character is wrong\n");
510 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
511 log_err("Didn't get the buffer back\n");
514 readSize
= u_strlen(buffer
);
515 if (readSize
!= expectedSize
*repetitions
) {
516 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
518 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
519 log_err("u_fgets wrote too much data\n");
521 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
522 log_err("Did get expected string back\n");
527 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
529 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
531 u_fputc(0xFF41, myFile
);
532 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
533 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
534 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
535 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
536 u_file_write(buffer
, expectedSize
, myFile
);
537 u_strcat(expectedBuffer
, buffer
);
541 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
542 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
543 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
544 log_err("Didn't get the buffer back\n");
547 readSize
= u_strlen(buffer
);
549 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
551 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
552 log_err("Did get expected string back\n");
554 if (buffer
[2] != 0xBEEF) {
555 log_err("u_fgets wrote too much data\n");
561 static void TestFileReadBuffering(void) {
563 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
567 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
568 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
569 u_file_write(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
573 u_memset(buffer
, 0xDEAD, sizeof(buffer
)/sizeof(buffer
[0]));
574 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
575 how_many
= u_file_read(buffer
, 1024, myFile
);
576 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
577 log_err("u_file_read read too much or not enough data\n");
582 static void TestfgetsLineCount(void) {
584 UChar expectedBuffer
[2048];
585 char charBuffer
[2048];
586 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!";
587 UFILE
*myFile
= NULL
;
588 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
589 int32_t expectedSize
= (int32_t)strlen(testStr
);
591 int32_t nlRepetitions
;
593 if (stdFile
== NULL
) {
594 log_err("Can't write test file.\n");
597 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
599 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
600 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
601 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
602 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
607 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
608 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
611 char *returnedCharBuffer
;
612 UChar
*returnedUCharBuffer
;
614 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
615 returnedCharBuffer
= fgets(charBuffer
, sizeof(charBuffer
)/sizeof(charBuffer
[0]), stdFile
);
616 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
618 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
619 /* Both returned NULL. stop. */
622 if (returnedCharBuffer
!= charBuffer
) {
623 log_err("Didn't get the charBuffer back\n");
626 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
627 if (returnedUCharBuffer
!= buffer
) {
628 log_err("Didn't get the buffer back\n");
631 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
632 log_err("buffers are different\n");
634 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
635 log_err("u_fgets wrote too much\n");
642 static void TestfgetsNewLineHandling(void) {
644 static const UChar testUStr
[][16] = {
661 {0x00A0, 0x000D, 0x000A, 0},
663 {0xd800, 0xdfff, 0x000D, 0},
676 UFILE
*myFile
= NULL
;
679 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
680 if (myFile
== NULL
) {
681 log_err("Can't write test file.\n");
684 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
685 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
689 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
691 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
692 UChar
*returnedUCharBuffer
;
694 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
695 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
697 if (!returnedUCharBuffer
) {
698 /* Returned NULL. stop. */
701 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
702 log_err("buffers are different at index = %d\n", lineIdx
);
704 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
705 log_err("u_fgets wrote too much\n");
708 if (lineIdx
!= (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0]))) {
709 log_err("u_fgets read too much\n");
711 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != NULL
) {
712 log_err("u_file_write wrote too much\n");
717 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
719 UChar expectedBuffer
[64];
720 int32_t lineLen
= strlen(line
);
721 UChar
*returnedUCharBuffer
;
723 UFILE
*myFile
= NULL
;
724 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
726 if (stdFile
== NULL
) {
727 log_err("Can't write test file.\n");
730 /* Write a prefix line and then write a bunch of lines */
731 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
732 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
733 fwrite(line
, lineLen
, 1, stdFile
);
737 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
738 if (myFile
== NULL
) {
739 log_err("Can't read test file.\n");
743 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
744 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
745 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
746 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
747 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
751 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
752 for (repetitions
= 0; ; repetitions
++) {
753 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
754 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
756 if (!returnedUCharBuffer
) {
757 /* returned NULL. stop. */
760 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
761 log_err("buffers are different at count %d\n", repetitions
);
763 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
764 log_err("u_fgets wrote too much\n");
767 if (repetitions
!= numRepititions
) {
768 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
773 static void TestfgetsNewLineCount(void) {
774 /* This makes sure that lines are correctly handled between buffer boundaries. */
775 TestLineCount("\n", "\n", 1024); /* Unix newlines */
776 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
777 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
778 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
779 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
780 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
783 static void TestFgetsLineBuffering(void) {
784 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
785 UChar
*returnedUCharBuffer
;
787 UFILE
*myFile
= NULL
;
788 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
790 if (stdFile
== NULL
) {
791 log_err("Can't write test file.\n");
794 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
796 /* Write one very long line */
797 for (repetitions
= 0; repetitions
< ((sizeof(buffer
)/sizeof(buffer
[0]))*2); repetitions
++) {
798 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
802 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
803 if (myFile
== NULL
) {
804 log_err("Can't read test file.\n");
808 /* Read part of one very long line */
809 returnedUCharBuffer
= u_fgets(buffer
, (sizeof(buffer
)/sizeof(buffer
[0]))-1, myFile
);
810 if (u_strlen(returnedUCharBuffer
) != ((sizeof(buffer
)/sizeof(buffer
[0]))-2)) {
811 log_err("Line is wrong length. Got %d. Expected %d.\n",
812 u_strlen(returnedUCharBuffer
), ((sizeof(buffer
)/sizeof(buffer
[0]))-2));
814 /* We better not read too much */
815 if (buffer
[(sizeof(buffer
)/sizeof(buffer
[0]))-1] != 0xBEEF) {
816 log_err("Too much data was written\n");
823 static void TestCodepage(void) {
824 UFILE
*myFile
= NULL
;
825 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
826 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
827 UChar testBuf
[sizeof(strABAccentA
)/sizeof(strABAccentA
[0])*2]; /* *2 to see if too much was */
828 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
830 UErrorCode status
= U_ZERO_ERROR
;
832 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
835 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
839 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
840 if (myFile
== NULL
) {
841 log_err("Can't write test file for iso-8859-1.\n");
844 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
845 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
847 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
850 /* Now see what we got wrote */
851 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
852 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
853 log_err("u_fsetcodepage didn't set the codepage\n");
855 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
856 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
857 log_err("The test data was read and written differently!\n");
859 if (retVal
!= u_strlen(strABAccentA
)) {
860 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
864 /* What happens on invalid input? */
865 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
866 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
867 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
869 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
870 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
872 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
873 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
875 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
876 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
877 log_err("The test data wasn't subsituted as expected\n");
881 /* Can't currently swap codepages midstream */
882 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
883 strcpy(convName
, u_fgetcodepage(myFile
));
884 u_file_read(testBuf
, 1, myFile
);
885 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
886 log_err("u_fsetcodepage set the codepage after reading a byte\n");
888 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
889 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
890 log_err("u_fsetcodepage changed the codepages after writing data\n");
892 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
893 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
896 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
897 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
898 log_err("The test data was read and written differently!\n");
900 if (retVal
!= u_strlen(strABAccentA
)) {
901 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
907 #if !UCONFIG_NO_FORMATTING
908 static void TestFilePrintCompatibility(void) {
909 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", "en_US_POSIX", NULL
);
913 static const UChar emptyStr
[] = {0};
914 char readBuf
[512] = "";
915 char testBuf
[512] = "";
917 if (myFile
== NULL
) {
918 log_err("Can't write test file.\n");
921 #if !UCONFIG_NO_FORMATTING
922 if (strcmp(u_fgetlocale(myFile
), "en_US_POSIX") != 0) {
923 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
927 /* Compare against C API compatibility */
928 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
929 u_fprintf(myFile
, "%x ", num
);
930 u_fprintf(myFile
, "%X ", num
);
931 u_fprintf(myFile
, "%o ", num
);
932 u_fprintf(myFile
, "%d ", num
);
933 u_fprintf(myFile
, "%i ", num
);
934 u_fprintf(myFile
, "%f ", (double)num
);
935 /* u_fprintf(myFile, "%e ", (double)num);
936 u_fprintf(myFile, "%E ", (double)num);*/
937 u_fprintf(myFile
, "%g ", (double)num
);
938 u_fprintf(myFile
, "%G", (double)num
);
939 u_fputs(emptyStr
, myFile
);
942 u_fprintf_u(myFile
, NEW_LINE
);
944 for (num
= 0; num
< 0x80; num
++) {
945 u_fprintf(myFile
, "%c", num
);
949 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
950 if (myCFile
== NULL
) {
951 log_err("Can't read test file.");
955 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
956 fscanf(myCFile
, "%s", readBuf
);
957 sprintf(testBuf
, "%x", (int)num
);
958 if (strcmp(readBuf
, testBuf
) != 0) {
959 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
962 fscanf(myCFile
, "%s", readBuf
);
963 sprintf(testBuf
, "%X", (int)num
);
964 if (strcmp(readBuf
, testBuf
) != 0) {
965 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
968 fscanf(myCFile
, "%s", readBuf
);
969 sprintf(testBuf
, "%o", (int)num
);
970 if (strcmp(readBuf
, testBuf
) != 0) {
971 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
974 /* fprintf is not compatible on all platforms e.g. the iSeries */
975 fscanf(myCFile
, "%s", readBuf
);
976 sprintf(testBuf
, "%d", (int)num
);
977 if (strcmp(readBuf
, testBuf
) != 0) {
978 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
981 fscanf(myCFile
, "%s", readBuf
);
982 sprintf(testBuf
, "%i", (int)num
);
983 if (strcmp(readBuf
, testBuf
) != 0) {
984 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
987 fscanf(myCFile
, "%s", readBuf
);
988 sprintf(testBuf
, "%f", (double)num
);
989 if (strcmp(readBuf
, testBuf
) != 0) {
990 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
993 /* fscanf(myCFile, "%s", readBuf);
994 sprintf(testBuf, "%e", (double)num);
995 if (strcmp(readBuf, testBuf) != 0) {
996 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
999 fscanf(myCFile, "%s", readBuf);
1000 sprintf(testBuf, "%E", (double)num);
1001 if (strcmp(readBuf, testBuf) != 0) {
1002 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1005 fscanf(myCFile
, "%s", readBuf
);
1006 sprintf(testBuf
, "%g", (double)num
);
1007 if (strcmp(readBuf
, testBuf
) != 0) {
1008 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1011 fscanf(myCFile
, "%s", readBuf
);
1012 sprintf(testBuf
, "%G", (double)num
);
1013 if (strcmp(readBuf
, testBuf
) != 0) {
1014 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1018 /* Properly eat the newlines */
1019 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1020 fscanf(myCFile
, "%c", &cVal
);
1021 if (cVal
!= C_NEW_LINE
[num
]) {
1022 log_err("OS newline error\n");
1025 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1026 fscanf(myCFile
, "%c", &cVal
);
1027 if (cVal
!= C_NEW_LINE
[num
]) {
1028 log_err("ustdio newline error\n");
1032 for (num
= 0; num
< 0x80; num
++) {
1034 fscanf(myCFile
, "%c", &cVal
);
1036 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1043 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1044 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
1045 if (myFile == NULL) {\
1046 log_err("Can't write test file for %s.\n", uFormat);\
1049 /* Reinitialize the buffer to verify null termination works. */\
1050 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1051 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
1053 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1055 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
1056 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
1058 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1059 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1060 if (strcmp(buffer, compBuffer) != 0) {\
1061 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1063 if (cNumPrinted != uNumPrinted) {\
1064 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1066 if (buffer[uNumPrinted+1] != '*') {\
1067 log_err("%" uFormat " too much stored\n");\
1070 #if !UCONFIG_NO_FORMATTING
1071 static void TestFprintfFormat(void) {
1072 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1073 static const char abcChars
[] = "abc";
1076 char compBuffer
[256];
1077 int32_t uNumPrinted
;
1078 int32_t cNumPrinted
;
1081 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1082 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1083 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1085 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1086 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1087 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1089 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1090 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1092 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1093 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1095 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1096 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1097 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1098 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1099 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1100 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1101 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1102 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1103 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1105 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1106 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1107 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1108 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1109 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1110 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1111 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1112 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1113 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1114 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1116 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1117 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1118 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1120 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1121 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1122 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1123 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1124 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1126 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1127 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1128 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1129 TestFPrintFormat("%#o", 123, "%#o", 123);
1130 TestFPrintFormat("%#o", -123, "%#o", -123);
1132 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1133 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1134 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1135 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1136 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1137 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1138 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1139 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1141 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1142 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1143 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1144 TestFPrintFormat("% d", 123456, "% d", 123456);
1145 TestFPrintFormat("% d", -123456, "% d", -123456);
1147 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1148 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1149 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1151 log_verbose("Get really crazy with the formatting.\n");
1153 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1154 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1155 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1156 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1158 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1159 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1160 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1161 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1162 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1163 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1164 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1165 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1166 TestFPrintFormat("%12d", 123, "%12d", 123);
1167 TestFPrintFormat("%12d", -123, "%12d", -123);
1168 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1169 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1171 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1172 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1173 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1174 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1175 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1176 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1177 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1178 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1179 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1180 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1181 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1182 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1183 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1184 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1186 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1187 /* Reinitialize the buffer to verify null termination works. */
1188 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1189 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1191 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1193 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1194 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1196 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1197 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1198 if (strcmp(buffer
, compBuffer
) != 0) {
1199 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1201 if (cNumPrinted
!= uNumPrinted
) {
1202 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1204 if (buffer
[uNumPrinted
+1] != '*') {
1205 log_err("%%d %% d %%d too much stored\n");
1210 #undef TestFPrintFormat
1212 #if !UCONFIG_NO_FORMATTING
1213 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1217 char compBuffer
[256];
1218 int32_t uNumScanned
;
1219 int32_t cNumScanned
;
1221 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1222 if (myFile
== NULL
) {
1223 log_err("Can't write test file for %s.\n", format
);
1226 /* Reinitialize the buffer to verify null termination works. */
1227 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1228 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1229 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1230 buffer
[sizeof(buffer
)/sizeof(*buffer
)-1] = 0;
1232 u_fprintf(myFile
, "%S", uValue
);
1234 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1235 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1237 if (expectedToPass
) {
1238 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1239 cNumScanned
= sscanf(cValue
, format
, buffer
);
1240 if (strncmp(buffer
, compBuffer
, sizeof(buffer
)/sizeof(*buffer
)) != 0) {
1241 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1243 if (cNumScanned
!= uNumScanned
) {
1244 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1246 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1247 log_err("%s too much stored\n", format
);
1251 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1252 log_err("%s too much stored on a failure\n", format
);
1258 #if !UCONFIG_NO_FORMATTING
1259 static void TestFScanset(void) {
1260 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1261 static const char abcChars
[] = "abccdefg";
1263 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1264 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1266 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1267 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1269 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1270 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1272 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1273 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1275 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1276 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1277 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1278 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1279 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1281 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1282 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1284 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1286 /* Extra long string for testing */
1287 TestFScanSetFormat(" %[qrst]",
1288 abcUChars
, abcChars
, TRUE
);
1290 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1293 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1294 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1295 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1296 /* The following is not deterministic on Windows */
1297 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1299 /* TODO: Need to specify precision with a "*" */
1302 #if !UCONFIG_NO_FORMATTING
1303 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1306 int32_t uNumScanned
;
1308 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1309 if (myFile
== NULL
) {
1310 log_err("Can't write test file for %s.\n", format
);
1313 /* Reinitialize the buffer to verify null termination works. */
1314 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1315 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1317 u_fprintf(myFile
, "%S", uValue
);
1319 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1320 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1322 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1323 log_err("%s too much stored on a failure\n", format
);
1327 #if !UCONFIG_NO_FORMATTING
1328 static void TestBadScanfFormat(void) {
1329 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1330 static const char abcChars
[] = "abccdefg";
1332 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1335 #if !UCONFIG_NO_FORMATTING
1336 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1338 UChar uBuffer2
[256];
1343 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "UTF-8");
1345 log_err("Test file can't be opened\n");
1349 va_start(ap
, format
);
1350 count
= u_vfprintf(myFile
, format
, ap
);
1356 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", "UTF-8");
1358 log_err("Test file can't be opened\n");
1361 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1362 u_uastrcpy(uBuffer2
, expectedResult
);
1363 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1364 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1369 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1371 log_err("Test file can't be opened\n");
1374 u_uastrcpy(uBuffer
, format
);
1376 va_start(ap
, format
);
1377 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1383 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1385 log_err("Test file can't be opened\n");
1388 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1389 u_uastrcpy(uBuffer2
, expectedResult
);
1390 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1391 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1396 static void TestVargs(void) {
1397 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1401 static void TestUnicodeFormat(void)
1403 #if !UCONFIG_NO_FORMATTING
1404 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1405 UChar myUString
[256];
1407 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1408 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1410 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
1412 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1414 log_err("Test file can't be opened\n");
1417 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1420 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1422 log_err("Test file can't be opened\n");
1425 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1427 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1428 log_err("u_fscanf_u doesn't work.\n");
1435 addFileTest(TestNode
** root
) {
1436 #if !UCONFIG_NO_FORMATTING
1437 addTest(root
, &TestFile
, "file/TestFile");
1439 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1440 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1441 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1442 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1443 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1444 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1445 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1446 addTest(root
, &TestCodepage
, "file/TestCodepage");
1447 #if !UCONFIG_NO_FORMATTING
1448 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1449 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1450 addTest(root
, &TestFScanset
, "file/TestFScanset");
1451 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1452 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1453 addTest(root
, &TestVargs
, "file/TestVargs");
1454 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");