2 **********************************************************************
3 * Copyright (C) 2004-2006, 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", NULL
, 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
);
236 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
237 if (myString
== NULL
|| strcmp(myString
, "\t\n") != 0) {
238 log_err("u_fgets got \"%s\"\n", myString
);
241 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
242 log_err("u_fgets did not return myUString\n");
244 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
245 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
246 log_err("u_fgets got \"%s\"\n", myString
);
249 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
250 log_err("u_fgets did not return myUString\n");
252 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
253 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
254 log_err("u_fgets got \"%s\"\n", myString
);
257 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
258 log_err("u_fgets did not return myUString\n");
260 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
261 if (myString
== NULL
|| strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
262 log_err("u_fgets got \"%s\"\n", myString
);
265 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
266 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
269 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
270 log_err("u_fgets did not return myUString\n");
272 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
273 if (myString
== NULL
|| strcmp(myString
, "") != 0) {
274 log_err("u_fgets got \"%s\"\n", myString
);
277 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
278 log_err("u_fgets did not return myUString\n");
280 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
281 if (myString
== NULL
|| strcmp(myString
, "\t") != 0) {
282 log_err("u_fgets got \"%s\"\n", myString
);
285 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
286 sizeof(myUString
)/sizeof(*myUString
));
287 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
288 log_err("u_fgets got \"%s\"\n", myString
);
291 if (u_feof(myFile
)) {
292 log_err("Got feof while reading the file and not at the end of the file.\n");
294 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
295 sizeof(myUString
)/sizeof(*myUString
));
296 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
297 log_err("u_fgets got \"%s\"\n", myString
);
299 if (!u_feof(myFile
)) {
300 log_err("Did not get feof while reading the end of the file.\n");
302 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
303 log_err("u_fscanf read data while reading the end of the file.\n");
309 static void TestFile(void) {
310 /* FILE *standardFile;*/
312 log_verbose("Testing u_fopen\n");
313 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
));
315 /* Don't know how to make this work without stdout or stderr */
317 log_verbose("Testing u_finit\n");
318 standardFile = fopen(STANDARD_TEST_FILE, "wb");
319 TestFileFromICU(u_finit(standardFile, NULL, NULL));
320 fclose(standardFile);
325 static void StdinBuffering(void) {
329 UFILE
*uStdIn
= NULL
;
330 UFILE
*uStdOut
= NULL
;
331 uStdIn
= u_finit(stdin
, NULL
, NULL
);
332 uStdOut
= u_finit(stdout
, NULL
, NULL
);
338 u_fgets(buff
, sizeof(buff
)/sizeof(buff
[0]), uStdIn
);
339 u_fprintf(uStdOut
, "%S\n", buff
);
340 u_fscanf(uStdIn
, "%d", &num
);
341 u_fprintf(uStdOut
, "%d\n", num
);
342 u_fscanf(uStdIn
, "%d", &num
);
343 u_fprintf(uStdOut
, "%d\n", num
);
345 log_verbose("Test disabled because it requires user interaction");
349 static void TestCodepageAndLocale(void) {
350 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
351 if (myFile
== NULL
) {
352 log_err("Can't write test file.\n");
355 if (u_fgetcodepage(myFile
) == NULL
356 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
358 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
359 u_fgetcodepage(myFile
), ucnv_getDefaultName());
361 #if !UCONFIG_NO_FORMATTING
362 if (u_fgetlocale(myFile
) == NULL
363 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
365 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
366 u_fgetlocale(myFile
), uloc_getDefault());
371 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
372 if (u_fgetcodepage(myFile
) == NULL
373 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
375 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
376 u_fgetcodepage(myFile
));
378 #if !UCONFIG_NO_FORMATTING
379 if (u_fgetlocale(myFile
) == NULL
380 || strcmp(u_fgetlocale(myFile
), "es") != 0)
382 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
383 u_fgetlocale(myFile
), "es");
388 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
389 if (u_fgetcodepage(myFile
) == NULL
390 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
392 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
393 u_fgetcodepage(myFile
));
395 #if !UCONFIG_NO_FORMATTING
396 if (u_fgetlocale(myFile
) == NULL
397 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
399 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
400 u_fgetlocale(myFile
), uloc_getDefault());
405 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
406 if (u_fgetcodepage(myFile
) == NULL
407 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
409 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
410 u_fgetcodepage(myFile
));
412 #if !UCONFIG_NO_FORMATTING
413 if (u_fgetlocale(myFile
) == NULL
414 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
416 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
417 u_fgetlocale(myFile
), "zh");
424 static void TestfgetsBuffers(void) {
426 UChar expectedBuffer
[2048];
427 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!";
428 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
429 int32_t expectedSize
= (int32_t)strlen(testStr
);
433 if (myFile
== NULL
) {
434 log_err("Can't write test file.\n");
438 u_fputc(0x3BC, myFile
);
439 if (u_fputc(0x110000, myFile
) != U_EOF
) {
440 log_err("u_fputc should return U_EOF for 0x110000.\n");
442 if (u_fputc((UChar32
)0xFFFFFFFFu
, myFile
) != U_EOF
) {
443 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
445 u_fputc(0xFF41, myFile
);
446 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
447 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
448 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
449 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
450 u_file_write(buffer
, expectedSize
, myFile
);
451 u_strcat(expectedBuffer
, buffer
);
455 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
456 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
457 if (u_fgetc(myFile
) != 0x3BC) {
458 log_err("The first character is wrong\n");
460 if (u_fgetc(myFile
) != 0xFF41) {
461 log_err("The second character is wrong\n");
463 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
464 log_err("Didn't get the buffer back\n");
467 readSize
= u_strlen(buffer
);
468 if (readSize
!= expectedSize
*repetitions
) {
469 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
471 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
472 log_err("u_fgets wrote too much data\n");
474 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
475 log_err("Did get expected string back\n");
477 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
478 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
482 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
484 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
486 u_fputc(0x3BC, myFile
);
487 u_fputc(0xFF41, myFile
);
488 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
489 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
490 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
491 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
492 u_file_write(buffer
, expectedSize
, myFile
);
493 u_strcat(expectedBuffer
, buffer
);
497 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
498 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
499 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
500 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
502 if (u_fgetc(myFile
) != 0x3BC) {
503 log_err("The first character is wrong\n");
505 if (u_fgetc(myFile
) != 0xFF41) {
506 log_err("The second character is wrong\n");
508 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
509 log_err("Didn't get the buffer back\n");
512 readSize
= u_strlen(buffer
);
513 if (readSize
!= expectedSize
*repetitions
) {
514 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
516 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
517 log_err("u_fgets wrote too much data\n");
519 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
520 log_err("Did get expected string back\n");
525 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
527 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
529 u_fputc(0xFF41, myFile
);
530 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
531 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
532 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
533 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
534 u_file_write(buffer
, expectedSize
, myFile
);
535 u_strcat(expectedBuffer
, buffer
);
539 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
540 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
541 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
542 log_err("Didn't get the buffer back\n");
545 readSize
= u_strlen(buffer
);
547 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
549 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
550 log_err("Did get expected string back\n");
552 if (buffer
[2] != 0xBEEF) {
553 log_err("u_fgets wrote too much data\n");
559 static void TestFileReadBuffering(void) {
561 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
565 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
566 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
567 u_file_write(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
571 u_memset(buffer
, 0xDEAD, sizeof(buffer
)/sizeof(buffer
[0]));
572 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
573 how_many
= u_file_read(buffer
, 1024, myFile
);
574 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
575 log_err("u_file_read read too much or not enough data\n");
580 static void TestfgetsLineCount(void) {
582 UChar expectedBuffer
[2048];
583 char charBuffer
[2048];
584 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!";
585 UFILE
*myFile
= NULL
;
586 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
587 int32_t expectedSize
= (int32_t)strlen(testStr
);
589 int32_t nlRepetitions
;
591 if (stdFile
== NULL
) {
592 log_err("Can't write test file.\n");
595 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
597 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
598 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
599 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
600 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
605 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
606 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
609 char *returnedCharBuffer
;
610 UChar
*returnedUCharBuffer
;
612 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
613 returnedCharBuffer
= fgets(charBuffer
, sizeof(charBuffer
)/sizeof(charBuffer
[0]), stdFile
);
614 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
616 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
617 /* Both returned NULL. stop. */
620 if (returnedCharBuffer
!= charBuffer
) {
621 log_err("Didn't get the charBuffer back\n");
624 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
625 if (returnedUCharBuffer
!= buffer
) {
626 log_err("Didn't get the buffer back\n");
629 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
630 log_err("buffers are different\n");
632 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
633 log_err("u_fgets wrote too much\n");
640 static void TestfgetsNewLineHandling(void) {
642 static const UChar testUStr
[][16] = {
659 {0x00A0, 0x000D, 0x000A, 0},
661 {0xd800, 0xdfff, 0x000D, 0},
674 UFILE
*myFile
= NULL
;
677 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
678 if (myFile
== NULL
) {
679 log_err("Can't write test file.\n");
682 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
683 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
687 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
689 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
690 UChar
*returnedUCharBuffer
;
692 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
693 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
695 if (!returnedUCharBuffer
) {
696 /* Returned NULL. stop. */
699 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
700 log_err("buffers are different at index = %d\n", lineIdx
);
702 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
703 log_err("u_fgets wrote too much\n");
706 if (lineIdx
!= (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0]))) {
707 log_err("u_fgets read too much\n");
709 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != NULL
) {
710 log_err("u_file_write wrote too much\n");
715 static void TestLineCount(const char *prefixLine
, const char *line
, int32_t numRepititions
) {
717 UChar expectedBuffer
[64];
718 int32_t lineLen
= strlen(line
);
719 UChar
*returnedUCharBuffer
;
721 UFILE
*myFile
= NULL
;
722 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
724 if (stdFile
== NULL
) {
725 log_err("Can't write test file.\n");
728 /* Write a prefix line and then write a bunch of lines */
729 fwrite(prefixLine
, strlen(prefixLine
), 1, stdFile
);
730 for (repetitions
= 0; repetitions
< numRepititions
; repetitions
++) {
731 fwrite(line
, lineLen
, 1, stdFile
);
735 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
736 if (myFile
== NULL
) {
737 log_err("Can't read test file.\n");
741 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
742 u_uastrncpy(expectedBuffer
, prefixLine
, (int32_t)strlen(prefixLine
)+1);
743 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
744 if (u_strcmp(returnedUCharBuffer
, expectedBuffer
) != 0) {
745 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine
);
749 u_uastrncpy(expectedBuffer
, line
, (int32_t)strlen(line
)+1);
750 for (repetitions
= 0; ; repetitions
++) {
751 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
752 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
754 if (!returnedUCharBuffer
) {
755 /* returned NULL. stop. */
758 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
759 log_err("buffers are different at count %d\n", repetitions
);
761 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
762 log_err("u_fgets wrote too much\n");
765 if (repetitions
!= numRepititions
) {
766 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions
, numRepititions
);
771 static void TestfgetsNewLineCount(void) {
772 /* This makes sure that lines are correctly handled between buffer boundaries. */
773 TestLineCount("\n", "\n", 1024); /* Unix newlines */
774 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
775 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
776 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
777 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
778 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
781 static void TestFgetsLineBuffering(void) {
782 UChar buffer
[2003]; /* Use a non-power of 2 or 10 */
783 UChar
*returnedUCharBuffer
;
785 UFILE
*myFile
= NULL
;
786 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "wb");
788 if (stdFile
== NULL
) {
789 log_err("Can't write test file.\n");
792 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
794 /* Write one very long line */
795 for (repetitions
= 0; repetitions
< ((sizeof(buffer
)/sizeof(buffer
[0]))*2); repetitions
++) {
796 fwrite(repetitions
? "1" : "2", 1, 1, stdFile
);
800 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, NULL
);
801 if (myFile
== NULL
) {
802 log_err("Can't read test file.\n");
806 /* Read part of one very long line */
807 returnedUCharBuffer
= u_fgets(buffer
, (sizeof(buffer
)/sizeof(buffer
[0]))-1, myFile
);
808 if (u_strlen(returnedUCharBuffer
) != ((sizeof(buffer
)/sizeof(buffer
[0]))-2)) {
809 log_err("Line is wrong length. Got %d. Expected %d.\n",
810 u_strlen(returnedUCharBuffer
), ((sizeof(buffer
)/sizeof(buffer
[0]))-2));
812 /* We better not read too much */
813 if (buffer
[(sizeof(buffer
)/sizeof(buffer
[0]))-1] != 0xBEEF) {
814 log_err("Too much data was written\n");
821 static void TestCodepage(void) {
822 UFILE
*myFile
= NULL
;
823 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
824 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
825 UChar testBuf
[sizeof(strABAccentA
)/sizeof(strABAccentA
[0])*2]; /* *2 to see if too much was */
826 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
828 UErrorCode status
= U_ZERO_ERROR
;
830 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
833 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
837 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
838 if (myFile
== NULL
) {
839 log_err("Can't write test file for iso-8859-1.\n");
842 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
843 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
845 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
848 /* Now see what we got wrote */
849 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
850 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
851 log_err("u_fsetcodepage didn't set the codepage\n");
853 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
854 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
855 log_err("The test data was read and written differently!\n");
857 if (retVal
!= u_strlen(strABAccentA
)) {
858 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
862 /* What happens on invalid input? */
863 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
864 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
865 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
867 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
868 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
870 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
871 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
873 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
874 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
875 log_err("The test data wasn't subsituted as expected\n");
879 /* Can't currently swap codepages midstream */
880 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
881 strcpy(convName
, u_fgetcodepage(myFile
));
882 u_file_read(testBuf
, 1, myFile
);
883 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
884 log_err("u_fsetcodepage set the codepage after reading a byte\n");
886 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
887 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
888 log_err("u_fsetcodepage changed the codepages after writing data\n");
890 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
891 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
894 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
895 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
896 log_err("The test data was read and written differently!\n");
898 if (retVal
!= u_strlen(strABAccentA
)) {
899 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
905 #if !UCONFIG_NO_FORMATTING
906 static void TestFilePrintCompatibility(void) {
907 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", "en_US_POSIX", NULL
);
911 static const UChar emptyStr
[] = {0};
912 char readBuf
[512] = "";
913 char testBuf
[512] = "";
915 if (myFile
== NULL
) {
916 log_err("Can't write test file.\n");
919 #if !UCONFIG_NO_FORMATTING
920 if (strcmp(u_fgetlocale(myFile
), "en_US_POSIX") != 0) {
921 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
925 /* Compare against C API compatibility */
926 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
927 u_fprintf(myFile
, "%x ", num
);
928 u_fprintf(myFile
, "%X ", num
);
929 u_fprintf(myFile
, "%o ", num
);
930 u_fprintf(myFile
, "%d ", num
);
931 u_fprintf(myFile
, "%i ", num
);
932 u_fprintf(myFile
, "%f ", (double)num
);
933 /* u_fprintf(myFile, "%e ", (double)num);
934 u_fprintf(myFile, "%E ", (double)num);*/
935 u_fprintf(myFile
, "%g ", (double)num
);
936 u_fprintf(myFile
, "%G", (double)num
);
937 u_fputs(emptyStr
, myFile
);
940 u_fprintf_u(myFile
, NEW_LINE
);
942 for (num
= 0; num
< 0x80; num
++) {
943 u_fprintf(myFile
, "%c", num
);
947 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
948 if (myCFile
== NULL
) {
949 log_err("Can't read test file.");
953 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
954 fscanf(myCFile
, "%s", readBuf
);
955 sprintf(testBuf
, "%x", (int)num
);
956 if (strcmp(readBuf
, testBuf
) != 0) {
957 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
960 fscanf(myCFile
, "%s", readBuf
);
961 sprintf(testBuf
, "%X", (int)num
);
962 if (strcmp(readBuf
, testBuf
) != 0) {
963 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
966 fscanf(myCFile
, "%s", readBuf
);
967 sprintf(testBuf
, "%o", (int)num
);
968 if (strcmp(readBuf
, testBuf
) != 0) {
969 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
972 /* fprintf is not compatible on all platforms e.g. the iSeries */
973 fscanf(myCFile
, "%s", readBuf
);
974 sprintf(testBuf
, "%d", (int)num
);
975 if (strcmp(readBuf
, testBuf
) != 0) {
976 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
979 fscanf(myCFile
, "%s", readBuf
);
980 sprintf(testBuf
, "%i", (int)num
);
981 if (strcmp(readBuf
, testBuf
) != 0) {
982 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
985 fscanf(myCFile
, "%s", readBuf
);
986 sprintf(testBuf
, "%f", (double)num
);
987 if (strcmp(readBuf
, testBuf
) != 0) {
988 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
991 /* fscanf(myCFile, "%s", readBuf);
992 sprintf(testBuf, "%e", (double)num);
993 if (strcmp(readBuf, testBuf) != 0) {
994 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
997 fscanf(myCFile, "%s", readBuf);
998 sprintf(testBuf, "%E", (double)num);
999 if (strcmp(readBuf, testBuf) != 0) {
1000 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1003 fscanf(myCFile
, "%s", readBuf
);
1004 sprintf(testBuf
, "%g", (double)num
);
1005 if (strcmp(readBuf
, testBuf
) != 0) {
1006 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1009 fscanf(myCFile
, "%s", readBuf
);
1010 sprintf(testBuf
, "%G", (double)num
);
1011 if (strcmp(readBuf
, testBuf
) != 0) {
1012 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
1016 /* Properly eat the newlines */
1017 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1018 fscanf(myCFile
, "%c", &cVal
);
1019 if (cVal
!= C_NEW_LINE
[num
]) {
1020 log_err("OS newline error\n");
1023 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
1024 fscanf(myCFile
, "%c", &cVal
);
1025 if (cVal
!= C_NEW_LINE
[num
]) {
1026 log_err("ustdio newline error\n");
1030 for (num
= 0; num
< 0x80; num
++) {
1032 fscanf(myCFile
, "%c", &cVal
);
1034 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
1041 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1042 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
1043 if (myFile == NULL) {\
1044 log_err("Can't write test file for %s.\n", uFormat);\
1047 /* Reinitialize the buffer to verify null termination works. */\
1048 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1049 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
1051 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1053 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
1054 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
1056 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1057 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1058 if (strcmp(buffer, compBuffer) != 0) {\
1059 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1061 if (cNumPrinted != uNumPrinted) {\
1062 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1064 if (buffer[uNumPrinted+1] != '*') {\
1065 log_err("%" uFormat " too much stored\n");\
1068 #if !UCONFIG_NO_FORMATTING
1069 static void TestFprintfFormat(void) {
1070 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
1071 static const char abcChars
[] = "abc";
1074 char compBuffer
[256];
1075 int32_t uNumPrinted
;
1076 int32_t cNumPrinted
;
1079 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
1080 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
1081 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
1083 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
1084 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
1085 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
1087 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1088 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1090 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
1091 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
1093 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1094 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1095 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1096 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1097 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1098 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1099 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1100 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1101 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1103 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1104 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1105 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1106 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1107 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1108 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1109 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1110 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1111 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1112 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1114 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1115 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1116 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1118 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1119 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1120 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1121 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1122 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1124 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1125 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1126 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1127 TestFPrintFormat("%#o", 123, "%#o", 123);
1128 TestFPrintFormat("%#o", -123, "%#o", -123);
1130 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1131 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1132 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1133 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1134 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1135 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1136 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1137 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1139 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1140 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1141 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1142 TestFPrintFormat("% d", 123456, "% d", 123456);
1143 TestFPrintFormat("% d", -123456, "% d", -123456);
1145 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1146 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1147 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1149 log_verbose("Get really crazy with the formatting.\n");
1151 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1152 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1153 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1154 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1156 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1157 TestFPrintFormat("%-+12d", -123, "%-+12d", -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);
1169 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1170 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1171 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1172 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1173 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 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("%.2f", 1.234, "%.2f", 1.234);
1180 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1181 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1182 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1184 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1185 /* Reinitialize the buffer to verify null termination works. */
1186 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1187 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1189 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1191 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1192 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1194 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1195 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1196 if (strcmp(buffer
, compBuffer
) != 0) {
1197 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1199 if (cNumPrinted
!= uNumPrinted
) {
1200 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1202 if (buffer
[uNumPrinted
+1] != '*') {
1203 log_err("%%d %% d %%d too much stored\n");
1208 #undef TestFPrintFormat
1210 #if !UCONFIG_NO_FORMATTING
1211 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1215 char compBuffer
[256];
1216 int32_t uNumScanned
;
1217 int32_t cNumScanned
;
1219 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1220 if (myFile
== NULL
) {
1221 log_err("Can't write test file for %s.\n", format
);
1224 /* Reinitialize the buffer to verify null termination works. */
1225 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1226 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1227 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1228 buffer
[sizeof(buffer
)/sizeof(*buffer
)-1] = 0;
1230 u_fprintf(myFile
, "%S", uValue
);
1232 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1233 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1235 if (expectedToPass
) {
1236 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1237 cNumScanned
= sscanf(cValue
, format
, buffer
);
1238 if (strncmp(buffer
, compBuffer
, sizeof(buffer
)/sizeof(*buffer
)) != 0) {
1239 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1241 if (cNumScanned
!= uNumScanned
) {
1242 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1244 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1245 log_err("%s too much stored\n", format
);
1249 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1250 log_err("%s too much stored on a failure\n", format
);
1256 #if !UCONFIG_NO_FORMATTING
1257 static void TestFScanset(void) {
1258 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1259 static const char abcChars
[] = "abccdefg";
1261 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1262 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1264 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1265 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1267 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1268 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1270 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1271 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1273 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1274 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1275 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1276 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1277 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1279 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1280 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1282 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1284 /* Extra long string for testing */
1285 TestFScanSetFormat(" %[qrst]",
1286 abcUChars
, abcChars
, TRUE
);
1288 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1291 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1292 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1293 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1294 /* The following is not deterministic on Windows */
1295 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1297 /* TODO: Need to specify precision with a "*" */
1300 #if !UCONFIG_NO_FORMATTING
1301 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1304 int32_t uNumScanned
;
1306 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1307 if (myFile
== NULL
) {
1308 log_err("Can't write test file for %s.\n", format
);
1311 /* Reinitialize the buffer to verify null termination works. */
1312 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1313 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1315 u_fprintf(myFile
, "%S", uValue
);
1317 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1318 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1320 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1321 log_err("%s too much stored on a failure\n", format
);
1325 #if !UCONFIG_NO_FORMATTING
1326 static void TestBadScanfFormat(void) {
1327 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1328 static const char abcChars
[] = "abccdefg";
1330 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1333 #if !UCONFIG_NO_FORMATTING
1334 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1336 UChar uBuffer2
[256];
1341 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "UTF-8");
1343 log_err("Test file can't be opened\n");
1347 va_start(ap
, format
);
1348 count
= u_vfprintf(myFile
, format
, ap
);
1354 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", "UTF-8");
1356 log_err("Test file can't be opened\n");
1359 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1360 u_uastrcpy(uBuffer2
, expectedResult
);
1361 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1362 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1367 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1369 log_err("Test file can't be opened\n");
1372 u_uastrcpy(uBuffer
, format
);
1374 va_start(ap
, format
);
1375 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1381 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1383 log_err("Test file can't be opened\n");
1386 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1387 u_uastrcpy(uBuffer2
, expectedResult
);
1388 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1389 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1394 static void TestVargs(void) {
1395 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1399 static void TestUnicodeFormat(void)
1401 #if !UCONFIG_NO_FORMATTING
1402 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1403 UChar myUString
[256];
1405 static const UChar TEST_STR
[] = { 0x03BC, 0x0025, 0x0024, 0};
1406 static const UChar PERCENT_S
[] = { 0x03BC, 0x0025, 0x0053, 0};
1408 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
1410 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
1412 log_err("Test file can't be opened\n");
1415 u_fprintf_u(myFile
, PERCENT_S
, TEST_STR
);
1418 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
1420 log_err("Test file can't be opened\n");
1423 u_fscanf_u(myFile
, PERCENT_S
, myUString
);
1425 if (u_strcmp(TEST_STR
, myUString
) != 0) {
1426 log_err("u_fscanf_u doesn't work.\n");
1433 addFileTest(TestNode
** root
) {
1434 #if !UCONFIG_NO_FORMATTING
1435 addTest(root
, &TestFile
, "file/TestFile");
1437 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1438 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1439 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1440 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1441 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1442 addTest(root
, &TestfgetsNewLineCount
, "file/TestfgetsNewLineCount");
1443 addTest(root
, &TestFgetsLineBuffering
, "file/TestFgetsLineBuffering");
1444 addTest(root
, &TestCodepage
, "file/TestCodepage");
1445 #if !UCONFIG_NO_FORMATTING
1446 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1447 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1448 addTest(root
, &TestFScanset
, "file/TestFScanset");
1449 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1450 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1451 addTest(root
, &TestVargs
, "file/TestVargs");
1452 addTest(root
, &TestUnicodeFormat
, "file/TestUnicodeFormat");