2 **********************************************************************
3 * Copyright (C) 2004-2004, 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"
19 #include "unicode/utrans.h"
23 const char STANDARD_TEST_FILE
[] = "iotest-c.txt";
26 #if !UCONFIG_NO_FORMATTING
27 static void TestFileFromICU(UFILE
*myFile
) {
29 float myFloat
= -1234.0;
30 int32_t newValuePtr
[1];
31 double newDoubleValuePtr
[1];
33 UChar uStringBuf
[256];
34 char myString
[256] = "";
35 char testBuf
[256] = "";
37 U_STRING_DECL(myStringOrig
, "My-String", 9);
39 U_STRING_INIT(myStringOrig
, "My-String", 9);
40 u_memset(myUString
, 0x2a, sizeof(myUString
)/sizeof(*myUString
));
41 u_memset(uStringBuf
, 0x2a, sizeof(uStringBuf
)/sizeof(*uStringBuf
));
42 memset(myString
, '*', sizeof(myString
)/sizeof(*myString
));
43 memset(testBuf
, '*', sizeof(testBuf
)/sizeof(*testBuf
));
46 log_err("Can't write test file.\n");
51 if (sizeof(void *) == 4) {
52 origPtr
= (void *)0xdeadbeef;
53 } else if (sizeof(void *) == 8) {
54 origPtr
= (void *) INT64_C(0x1000200030004000);
55 } else if (sizeof(void *) == 16) {
57 int32_t massiveBigEndianPtr
[] = { 0x10002000, 0x30004000, 0x50006000, 0x70008000 };
58 origPtr
= *((void **)massiveBigEndianPtr
);
60 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
64 u_fprintf(myFile
, "Signed decimal integer %%d: %d\n", *n
);
65 u_fprintf(myFile
, "Signed decimal integer %%i: %i\n", *n
);
66 u_fprintf(myFile
, "Unsigned octal integer %%o: %o\n", *n
);
67 u_fprintf(myFile
, "Unsigned decimal integer %%u: %u\n", *n
);
68 u_fprintf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n
);
69 u_fprintf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n
);
70 u_fprintf(myFile
, "Float %%f: %f\n", myFloat
);
71 u_fprintf(myFile
, "Lowercase float %%e: %e\n", myFloat
);
72 u_fprintf(myFile
, "Uppercase float %%E: %E\n", myFloat
);
73 u_fprintf(myFile
, "Lowercase float %%g: %g\n", myFloat
);
74 u_fprintf(myFile
, "Uppercase float %%G: %G\n", myFloat
);
75 u_fprintf(myFile
, "Pointer %%p: %p\n", origPtr
);
76 u_fprintf(myFile
, "Char %%c: %c\n", 'A');
77 u_fprintf(myFile
, "UChar %%C: %C\n", (UChar
)0x0041); /*'A'*/
78 u_fprintf(myFile
, "String %%s: %s\n", "My-String");
79 u_fprintf(myFile
, "NULL String %%s: %s\n", NULL
);
80 u_fprintf(myFile
, "Unicode String %%S: %S\n", myStringOrig
);
81 u_fprintf(myFile
, "NULL Unicode String %%S: %S\n", NULL
);
82 u_fprintf(myFile
, "Percent %%P (non-ANSI): %P\n", myFloat
);
83 u_fprintf(myFile
, "Spell Out %%V (non-ANSI): %V\n", myFloat
);
86 log_err("Got feof while writing the file.\n");
90 u_fprintf(myFile
, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n
, n
, *n
);
91 u_fprintf(myFile
, "Pointer to integer Value: %d\n", *n
);
92 u_fprintf(myFile
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
94 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count: n=%d %n n=%d\n", (int)*n
, (int*)n
, (int)*n
);
95 fprintf(u_fgetfile(myFile
), "\tNormal fprintf count value: n=%d\n", (int)*n
);
98 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
100 if (myFile
== NULL
) {
101 log_err("Can't read test file.");
105 if (u_feof(myFile
)) {
106 log_err("Got feof while reading the file and not at the end of the file.\n");
109 myUString
[0] = u_fgetc(myFile
);
110 if (myUString
[0] != 0x53 /* S */) {
111 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString
[0]);
113 u_fungetc(myUString
[0], myFile
);
114 myUString
[0] = u_fgetc(myFile
);
115 if (myUString
[0] != 0x53 /* S */) {
116 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString
[0]);
118 u_fungetc(myUString
[0], myFile
);
119 myUString
[0] = u_fgetc(myFile
);
120 if (myUString
[0] != 0x53 /* S */) {
121 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString
[0]);
123 u_fungetc(myUString
[0], myFile
);
124 myUString
[0] = u_fgetc(myFile
);
125 myUString
[1] = (UChar
)u_fgetcx(myFile
); /* Mix getc and getcx and see what happens. */
126 myUString
[2] = u_fgetc(myFile
);
127 if (myUString
[0] != 0x53 /* S */ && myUString
[1] != 0x69 /* i */ && myUString
[2] != 0x6E /* n */) {
128 log_err("u_fgetcx returned \\u%04X\\u%04X\\u%04X. Expected 'Sin'.", myString
[0], myString
[1], myString
[2]);
130 u_fungetc(myUString
[2], myFile
);
131 u_fungetc(myUString
[1], myFile
);
132 u_fungetc(myUString
[0], myFile
);
137 u_fscanf(myFile
, "Signed decimal integer %%d: %d\n", newValuePtr
);
138 if (*n
!= *newValuePtr
) {
139 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr
, *n
);
142 u_fscanf(myFile
, "Signed decimal integer %%i: %i\n", newValuePtr
);
143 if (*n
!= *newValuePtr
) {
144 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr
, *n
);
147 u_fscanf(myFile
, "Unsigned octal integer %%o: %o\n", newValuePtr
);
148 if (*n
!= *newValuePtr
) {
149 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr
, *n
);
152 u_fscanf(myFile
, "Unsigned decimal integer %%u: %u\n", newValuePtr
);
153 if (*n
!= *newValuePtr
) {
154 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr
, *n
);
157 u_fscanf(myFile
, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr
);
158 if (*n
!= *newValuePtr
) {
159 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr
, *n
);
162 u_fscanf(myFile
, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr
);
163 if (*n
!= *newValuePtr
) {
164 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr
, *n
);
166 *newDoubleValuePtr
= -1.0;
167 u_fscanf(myFile
, "Float %%f: %f\n", newDoubleValuePtr
);
168 if (myFloat
!= *newDoubleValuePtr
) {
169 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
171 *newDoubleValuePtr
= -1.0;
172 u_fscanf(myFile
, "Lowercase float %%e: %e\n", newDoubleValuePtr
);
173 if (myFloat
!= *newDoubleValuePtr
) {
174 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr
, myFloat
);
176 *newDoubleValuePtr
= -1.0;
177 u_fscanf(myFile
, "Uppercase float %%E: %E\n", newDoubleValuePtr
);
178 if (myFloat
!= *newDoubleValuePtr
) {
179 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr
, myFloat
);
181 *newDoubleValuePtr
= -1.0;
182 u_fscanf(myFile
, "Lowercase float %%g: %g\n", newDoubleValuePtr
);
183 if (myFloat
!= *newDoubleValuePtr
) {
184 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr
, myFloat
);
186 *newDoubleValuePtr
= -1.0;
187 u_fscanf(myFile
, "Uppercase float %%G: %G\n", newDoubleValuePtr
);
188 if (myFloat
!= *newDoubleValuePtr
) {
189 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr
, myFloat
);
192 u_fscanf(myFile
, "Pointer %%p: %p\n", &ptr
);
193 if (ptr
!= origPtr
) {
194 log_err("%%p Got: %p, Expected: %p\n", ptr
, origPtr
);
196 u_fscanf(myFile
, "Char %%c: %c\n", myString
);
197 if (*myString
!= 'A') {
198 log_err("%%c Got: %c, Expected: A\n", *myString
);
200 u_fscanf(myFile
, "UChar %%C: %C\n", myUString
);
201 if (*myUString
!= (UChar
)0x0041) { /*'A'*/
202 log_err("%%C Got: %C, Expected: A\n", *myUString
);
204 u_fscanf(myFile
, "String %%s: %s\n", myString
);
205 if (strcmp(myString
, "My-String")) {
206 log_err("%%s Got: %s, Expected: My String\n", myString
);
208 u_fscanf(myFile
, "NULL String %%s: %s\n", myString
);
209 if (strcmp(myString
, "(null)")) {
210 log_err("%%s Got: %s, Expected: My String\n", myString
);
212 u_fscanf(myFile
, "Unicode String %%S: %S\n", myUString
);
213 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
214 if (strcmp(myString
, "My-String")) {
215 log_err("%%S Got: %S, Expected: My String\n", myUString
);
217 u_fscanf(myFile
, "NULL Unicode String %%S: %S\n", myUString
);
218 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
219 if (strcmp(myString
, "(null)")) {
220 log_err("%%S Got: %S, Expected: My String\n", myUString
);
222 *newDoubleValuePtr
= -1.0;
223 u_fscanf(myFile
, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr
);
224 if (myFloat
!= *newDoubleValuePtr
) {
225 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
227 *newDoubleValuePtr
= -1.0;
228 u_fscanf(myFile
, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr
);
229 if (myFloat
!= *newDoubleValuePtr
) {
230 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr
, myFloat
);
233 u_fgets(myUString
, 4, myFile
);
234 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
235 if (myString
== NULL
|| strcmp(myString
, "\t\n") != 0) {
236 log_err("u_fgets got \"%s\"\n", myString
);
239 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
240 log_err("u_fgets did not return myUString\n");
242 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
243 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
244 log_err("u_fgets got \"%s\"\n", myString
);
247 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
248 log_err("u_fgets did not return myUString\n");
250 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
251 if (myString
== NULL
|| strcmp(myString
, "Pointer to integer Value: 37\n") != 0) {
252 log_err("u_fgets got \"%s\"\n", myString
);
255 if (u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
) != myUString
) {
256 log_err("u_fgets did not return myUString\n");
258 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
259 if (myString
== NULL
|| strcmp(myString
, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
260 log_err("u_fgets got \"%s\"\n", myString
);
263 if (u_fgets(myUString
, 0, myFile
) != NULL
) {
264 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString
);
267 if (u_fgets(myUString
, 1, myFile
) != myUString
) {
268 log_err("u_fgets did not return myUString\n");
270 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
271 if (myString
== NULL
|| strcmp(myString
, "") != 0) {
272 log_err("u_fgets got \"%s\"\n", myString
);
275 if (u_fgets(myUString
, 2, myFile
) != myUString
) {
276 log_err("u_fgets did not return myUString\n");
278 u_austrncpy(myString
, myUString
, sizeof(myUString
)/sizeof(*myUString
));
279 if (myString
== NULL
|| strcmp(myString
, "\t") != 0) {
280 log_err("u_fgets got \"%s\"\n", myString
);
283 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
284 sizeof(myUString
)/sizeof(*myUString
));
285 if (strcmp(myString
, "Normal fprintf count: n=1 n=1\n") != 0) {
286 log_err("u_fgets got \"%s\"\n", myString
);
289 if (u_feof(myFile
)) {
290 log_err("Got feof while reading the file and not at the end of the file.\n");
292 u_austrncpy(myString
, u_fgets(myUString
, sizeof(myUString
)/sizeof(*myUString
), myFile
),
293 sizeof(myUString
)/sizeof(*myUString
));
294 if (strcmp(myString
, "\tNormal fprintf count value: n=27\n") != 0) {
295 log_err("u_fgets got \"%s\"\n", myString
);
297 if (!u_feof(myFile
)) {
298 log_err("Did not get feof while reading the end of the file.\n");
300 if (u_fscanf(myFile
, "%S\n", myUString
) != 0) {
301 log_err("u_fscanf read data while reading the end of the file.\n");
307 static void TestFile(void) {
308 /* FILE *standardFile;*/
310 log_verbose("Testing u_fopen\n");
311 TestFileFromICU(u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
));
313 /* Don't know how to make this work without stdout or stderr */
315 log_verbose("Testing u_finit\n");
316 standardFile = fopen(STANDARD_TEST_FILE, "wb");
317 TestFileFromICU(u_finit(standardFile, NULL, NULL));
318 fclose(standardFile);
323 static void StdinBuffering(void) {
327 UFILE
*uStdIn
= NULL
;
328 UFILE
*uStdOut
= NULL
;
329 uStdIn
= u_finit(stdin
, NULL
, NULL
);
330 uStdOut
= u_finit(stdout
, NULL
, NULL
);
336 u_fgets(buff
, sizeof(buff
)/sizeof(buff
[0]), uStdIn
);
337 u_fprintf(uStdOut
, "%S\n", buff
);
338 u_fscanf(uStdIn
, "%d", &num
);
339 u_fprintf(uStdOut
, "%d\n", num
);
340 u_fscanf(uStdIn
, "%d", &num
);
341 u_fprintf(uStdOut
, "%d\n", num
);
343 log_verbose("Test disabled because it requires user interaction");
347 static void TestCodepageAndLocale(void) {
348 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
349 if (myFile
== NULL
) {
350 log_err("Can't write test file.\n");
353 if (u_fgetcodepage(myFile
) == NULL
354 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
356 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
357 u_fgetcodepage(myFile
), ucnv_getDefaultName());
359 #if !UCONFIG_NO_FORMATTING
360 if (u_fgetlocale(myFile
) == NULL
361 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
363 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
364 u_fgetlocale(myFile
), uloc_getDefault());
369 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "es", NULL
);
370 if (u_fgetcodepage(myFile
) == NULL
371 || strcmp(u_fgetcodepage(myFile
), ucnv_getDefaultName()) != 0)
373 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
374 u_fgetcodepage(myFile
));
376 #if !UCONFIG_NO_FORMATTING
377 if (u_fgetlocale(myFile
) == NULL
378 || strcmp(u_fgetlocale(myFile
), "es") != 0)
380 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
381 u_fgetlocale(myFile
), "es");
386 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
387 if (u_fgetcodepage(myFile
) == NULL
388 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
390 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
391 u_fgetcodepage(myFile
));
393 #if !UCONFIG_NO_FORMATTING
394 if (u_fgetlocale(myFile
) == NULL
395 || strcmp(u_fgetlocale(myFile
), uloc_getDefault()) != 0)
397 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
398 u_fgetlocale(myFile
), uloc_getDefault());
403 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "zh", "UTF-16");
404 if (u_fgetcodepage(myFile
) == NULL
405 || strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0)
407 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
408 u_fgetcodepage(myFile
));
410 #if !UCONFIG_NO_FORMATTING
411 if (u_fgetlocale(myFile
) == NULL
412 || strcmp(u_fgetlocale(myFile
), "zh") != 0)
414 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
415 u_fgetlocale(myFile
), "zh");
422 static void TestfgetsBuffers(void) {
424 UChar expectedBuffer
[2048];
425 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!";
426 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
427 int32_t expectedSize
= (int32_t)strlen(testStr
);
431 if (myFile
== NULL
) {
432 log_err("Can't write test file.\n");
436 u_fputc(0x3BC, myFile
);
437 u_fputc(0xFF41, myFile
);
438 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
439 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
440 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
441 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
442 u_file_write(buffer
, expectedSize
, myFile
);
443 u_strcat(expectedBuffer
, buffer
);
447 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
448 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
449 if (u_fgetc(myFile
) != 0x3BC) {
450 log_err("The first character is wrong\n");
452 if (u_fgetc(myFile
) != 0xFF41) {
453 log_err("The second character is wrong\n");
455 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
456 log_err("Didn't get the buffer back\n");
459 readSize
= u_strlen(buffer
);
460 if (readSize
!= expectedSize
*repetitions
) {
461 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
463 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
464 log_err("u_fgets wrote too much data\n");
466 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
467 log_err("Did get expected string back\n");
469 if (strcmp(u_fgetcodepage(myFile
), "UTF-16") != 0) {
470 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile
));
474 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
476 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
478 u_fputc(0x3BC, myFile
);
479 u_fputc(0xFF41, myFile
);
480 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
481 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
482 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
483 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
484 u_file_write(buffer
, expectedSize
, myFile
);
485 u_strcat(expectedBuffer
, buffer
);
489 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
490 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
491 if (strcmp(u_fgetcodepage(myFile
), "UTF-8") != 0) {
492 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile
));
494 if (u_fgetc(myFile
) != 0x3BC) {
495 log_err("The first character is wrong\n");
497 if (u_fgetc(myFile
) != 0xFF41) {
498 log_err("The second character is wrong\n");
500 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != buffer
) {
501 log_err("Didn't get the buffer back\n");
504 readSize
= u_strlen(buffer
);
505 if (readSize
!= expectedSize
*repetitions
) {
506 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), expectedSize
*repetitions
);
508 if (buffer
[(expectedSize
*repetitions
) + 1] != 0xBEEF) {
509 log_err("u_fgets wrote too much data\n");
511 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
512 log_err("Did get expected string back\n");
517 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
519 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-8");
521 u_fputc(0xFF41, myFile
);
522 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
523 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
524 u_uastrncpy(buffer
, testStr
, expectedSize
+1);
525 for (repetitions
= 0; repetitions
< 1; repetitions
++) {
526 u_file_write(buffer
, expectedSize
, myFile
);
527 u_strcat(expectedBuffer
, buffer
);
531 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
532 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-8");
533 if (u_fgets(buffer
, 2, myFile
) != buffer
) {
534 log_err("Didn't get the buffer back\n");
537 readSize
= u_strlen(buffer
);
539 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer
), 1);
541 if (buffer
[0] != 0xFF41 || buffer
[1] != 0) {
542 log_err("Did get expected string back\n");
544 if (buffer
[2] != 0xBEEF) {
545 log_err("u_fgets wrote too much data\n");
551 static void TestFileReadBuffering(void) {
553 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "UTF-16");
557 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
558 for (repetitions
= 0; repetitions
< 2; repetitions
++) {
559 u_file_write(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
563 u_memset(buffer
, 0xDEAD, sizeof(buffer
)/sizeof(buffer
[0]));
564 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "UTF-16");
565 how_many
= u_file_read(buffer
, 1024, myFile
);
566 if (how_many
!= 1024 || buffer
[1023] != 0xBEEF) {
567 log_err("u_file_read read too much or not enough data\n");
572 static void TestfgetsLineCount(void) {
574 UChar expectedBuffer
[2048];
575 char charBuffer
[2048];
576 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!";
577 UFILE
*myFile
= NULL
;
578 FILE *stdFile
= fopen(STANDARD_TEST_FILE
, "w");
579 int32_t expectedSize
= (int32_t)strlen(testStr
);
581 int32_t nlRepetitions
;
583 if (stdFile
== NULL
) {
584 log_err("Can't write test file.\n");
587 u_memset(expectedBuffer
, 0, sizeof(expectedBuffer
)/sizeof(expectedBuffer
[0]));
589 for (repetitions
= 0; repetitions
< 16; repetitions
++) {
590 fwrite(testStr
, sizeof(testStr
[0]), expectedSize
, stdFile
);
591 for (nlRepetitions
= 0; nlRepetitions
< repetitions
; nlRepetitions
++) {
592 fwrite("\n", sizeof(testStr
[0]), 1, stdFile
);
597 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
598 stdFile
= fopen(STANDARD_TEST_FILE
, "r");
601 char *returnedCharBuffer
;
602 UChar
*returnedUCharBuffer
;
604 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
605 returnedCharBuffer
= fgets(charBuffer
, sizeof(charBuffer
)/sizeof(charBuffer
[0]), stdFile
);
606 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
608 if (!returnedCharBuffer
&& !returnedUCharBuffer
) {
609 /* Both returned NULL. stop. */
612 if (returnedCharBuffer
!= charBuffer
) {
613 log_err("Didn't get the charBuffer back\n");
616 u_uastrncpy(expectedBuffer
, charBuffer
, (int32_t)strlen(charBuffer
)+1);
617 if (returnedUCharBuffer
!= buffer
) {
618 log_err("Didn't get the buffer back\n");
621 if (u_strcmp(buffer
, expectedBuffer
) != 0) {
622 log_err("buffers are different\n");
624 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
625 log_err("u_fgets wrote too much\n");
632 static void TestfgetsNewLineHandling(void) {
634 static const UChar testUStr
[][16] = {
651 {0x00A0, 0x000D, 0x000A, 0},
653 {0xd800, 0xdfff, 0x000D, 0},
666 UFILE
*myFile
= NULL
;
669 myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", NULL
, "UTF-8");
670 if (myFile
== NULL
) {
671 log_err("Can't write test file.\n");
674 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
675 u_file_write(testUStr
[lineIdx
], u_strlen(testUStr
[lineIdx
]), myFile
);
679 myFile
= u_fopen(STANDARD_TEST_FILE
, "rb", NULL
, "UTF-8");
681 for (lineIdx
= 0; lineIdx
< (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0])); lineIdx
++) {
682 UChar
*returnedUCharBuffer
;
684 u_memset(buffer
, 0xBEEF, sizeof(buffer
)/sizeof(buffer
[0]));
685 returnedUCharBuffer
= u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
);
687 if (!returnedUCharBuffer
) {
688 /* Returned NULL. stop. */
691 if (u_strcmp(buffer
, testUStr
[lineIdx
]) != 0) {
692 log_err("buffers are different at index = %d\n", lineIdx
);
694 if (buffer
[u_strlen(buffer
)+1] != 0xBEEF) {
695 log_err("u_fgets wrote too much\n");
698 if (lineIdx
!= (int32_t)(sizeof(testUStr
)/sizeof(testUStr
[0]))) {
699 log_err("u_fgets read too much\n");
701 if (u_fgets(buffer
, sizeof(buffer
)/sizeof(buffer
[0]), myFile
) != NULL
) {
702 log_err("u_file_write wrote too much\n");
708 static void TestCodepage(void) {
709 UFILE
*myFile
= NULL
;
710 static const UChar strABAccentA
[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
711 static const UChar strBadConversion
[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
712 UChar testBuf
[sizeof(strABAccentA
)/sizeof(strABAccentA
[0])*2]; /* *2 to see if too much was */
713 char convName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
715 UErrorCode status
= U_ZERO_ERROR
;
717 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "absurd converter that can't be opened");
720 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
724 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, "ISO-8859-1");
725 if (myFile
== NULL
) {
726 log_err("Can't write test file for iso-8859-1.\n");
729 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile
)) != 0) {
730 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
732 u_file_write(strABAccentA
, u_strlen(strABAccentA
), myFile
);
735 /* Now see what we got wrote */
736 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, NULL
);
737 if (u_fsetcodepage("ISO-8859-1", myFile
) != 0) {
738 log_err("u_fsetcodepage didn't set the codepage\n");
740 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
741 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
742 log_err("The test data was read and written differently!\n");
744 if (retVal
!= u_strlen(strABAccentA
)) {
745 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
749 /* What happens on invalid input? */
750 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
751 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "ISO-8859-1") != 0) {
752 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
754 if (u_fsetcodepage("UTF-8", myFile
) != 0) {
755 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
757 if (strcmp(ucnv_getName(u_fgetConverter(myFile
), &status
), "UTF-8") != 0) {
758 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile
), &status
));
760 retVal
= u_file_read(testBuf
, u_strlen(strBadConversion
), myFile
);
761 if (u_strncmp(strBadConversion
, testBuf
, u_strlen(strBadConversion
)) != 0) {
762 log_err("The test data wasn't subsituted as expected\n");
766 /* Can't currently swap codepages midstream */
767 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", NULL
, "ISO-8859-1");
768 strcpy(convName
, u_fgetcodepage(myFile
));
769 u_file_read(testBuf
, 1, myFile
);
770 if (u_fsetcodepage("UTF-8", myFile
) == 0) {
771 log_err("u_fsetcodepage set the codepage after reading a byte\n");
773 retVal
= u_file_read(testBuf
+ 1, u_strlen(strABAccentA
) - 1, myFile
);
774 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
775 log_err("u_fsetcodepage changed the codepages after writing data\n");
777 if ((retVal
+ 1) != u_strlen(strABAccentA
)) {
778 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
781 retVal
= u_file_read(testBuf
, u_strlen(strABAccentA
), myFile
);
782 if (u_strncmp(strABAccentA
, testBuf
, u_strlen(strABAccentA
)) != 0) {
783 log_err("The test data was read and written differently!\n");
785 if (retVal
!= u_strlen(strABAccentA
)) {
786 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal
, u_strlen(strABAccentA
));
792 #if !UCONFIG_NO_FORMATTING
793 static void TestFilePrintCompatibility(void) {
794 UFILE
*myFile
= u_fopen(STANDARD_TEST_FILE
, "wb", "en_US_POSIX", NULL
);
798 static const UChar emptyStr
[] = {0};
799 char readBuf
[512] = "";
800 char testBuf
[512] = "";
802 if (myFile
== NULL
) {
803 log_err("Can't write test file.\n");
806 #if !UCONFIG_NO_FORMATTING
807 if (strcmp(u_fgetlocale(myFile
), "en_US_POSIX") != 0) {
808 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile
));
812 /* Compare against C API compatibility */
813 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
814 u_fprintf(myFile
, "%x ", num
);
815 u_fprintf(myFile
, "%X ", num
);
816 u_fprintf(myFile
, "%o ", num
);
817 u_fprintf(myFile
, "%d ", num
);
818 u_fprintf(myFile
, "%i ", num
);
819 u_fprintf(myFile
, "%f ", (double)num
);
820 /* u_fprintf(myFile, "%e ", (double)num);
821 u_fprintf(myFile, "%E ", (double)num);*/
822 u_fprintf(myFile
, "%g ", (double)num
);
823 u_fprintf(myFile
, "%G", (double)num
);
824 u_fputs(emptyStr
, myFile
);
827 u_fprintf_u(myFile
, NEW_LINE
);
829 for (num
= 0; num
< 0x80; num
++) {
830 u_fprintf(myFile
, "%c", num
);
834 myCFile
= fopen(STANDARD_TEST_FILE
, "rb");
835 if (myCFile
== NULL
) {
836 log_err("Can't read test file.");
840 for (num
= -STANDARD_TEST_NUM_RANGE
; num
< STANDARD_TEST_NUM_RANGE
; num
++) {
841 fscanf(myCFile
, "%s", readBuf
);
842 sprintf(testBuf
, "%x", (int)num
);
843 if (strcmp(readBuf
, testBuf
) != 0) {
844 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
847 fscanf(myCFile
, "%s", readBuf
);
848 sprintf(testBuf
, "%X", (int)num
);
849 if (strcmp(readBuf
, testBuf
) != 0) {
850 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
853 fscanf(myCFile
, "%s", readBuf
);
854 sprintf(testBuf
, "%o", (int)num
);
855 if (strcmp(readBuf
, testBuf
) != 0) {
856 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
859 /* fprintf is not compatible on all platforms e.g. the iSeries */
860 fscanf(myCFile
, "%s", readBuf
);
861 sprintf(testBuf
, "%d", (int)num
);
862 if (strcmp(readBuf
, testBuf
) != 0) {
863 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
866 fscanf(myCFile
, "%s", readBuf
);
867 sprintf(testBuf
, "%i", (int)num
);
868 if (strcmp(readBuf
, testBuf
) != 0) {
869 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
872 fscanf(myCFile
, "%s", readBuf
);
873 sprintf(testBuf
, "%f", (double)num
);
874 if (strcmp(readBuf
, testBuf
) != 0) {
875 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
878 /* fscanf(myCFile, "%s", readBuf);
879 sprintf(testBuf, "%e", (double)num);
880 if (strcmp(readBuf, testBuf) != 0) {
881 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
884 fscanf(myCFile, "%s", readBuf);
885 sprintf(testBuf, "%E", (double)num);
886 if (strcmp(readBuf, testBuf) != 0) {
887 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
890 fscanf(myCFile
, "%s", readBuf
);
891 sprintf(testBuf
, "%g", (double)num
);
892 if (strcmp(readBuf
, testBuf
) != 0) {
893 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
896 fscanf(myCFile
, "%s", readBuf
);
897 sprintf(testBuf
, "%G", (double)num
);
898 if (strcmp(readBuf
, testBuf
) != 0) {
899 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf
, testBuf
);
903 /* Properly eat the newlines */
904 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
905 fscanf(myCFile
, "%c", &cVal
);
906 if (cVal
!= C_NEW_LINE
[num
]) {
907 log_err("OS newline error\n");
910 for (num
= 0; num
< (int32_t)strlen(C_NEW_LINE
); num
++) {
911 fscanf(myCFile
, "%c", &cVal
);
912 if (cVal
!= C_NEW_LINE
[num
]) {
913 log_err("ustdio newline error\n");
917 for (num
= 0; num
< 0x80; num
++) {
919 fscanf(myCFile
, "%c", &cVal
);
921 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal
, num
);
928 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
929 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
930 if (myFile == NULL) {\
931 log_err("Can't write test file for %s.\n", uFormat);\
934 /* Reinitialize the buffer to verify null termination works. */\
935 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
936 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
938 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
940 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
941 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
943 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
944 cNumPrinted = sprintf(buffer, cFormat, cValue);\
945 if (strcmp(buffer, compBuffer) != 0) {\
946 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
948 if (cNumPrinted != uNumPrinted) {\
949 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
951 if (buffer[uNumPrinted+1] != '*') {\
952 log_err("%" uFormat " too much stored\n");\
955 #if !UCONFIG_NO_FORMATTING
956 static void TestFprintfFormat(void) {
957 static const UChar abcUChars
[] = {0x61,0x62,0x63,0};
958 static const char abcChars
[] = "abc";
961 char compBuffer
[256];
966 TestFPrintFormat("%8S", abcUChars
, "%8s", abcChars
);
967 TestFPrintFormat("%-8S", abcUChars
, "%-8s", abcChars
);
968 TestFPrintFormat("%.2S", abcUChars
, "%.2s", abcChars
); /* strlen is 3 */
970 TestFPrintFormat("%8s", abcChars
, "%8s", abcChars
);
971 TestFPrintFormat("%-8s", abcChars
, "%-8s", abcChars
);
972 TestFPrintFormat("%.2s", abcChars
, "%.2s", abcChars
); /* strlen is 3 */
974 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
975 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
977 TestFPrintFormat("%8C", (UChar
)0x65, "%8c", (char)'e');
978 TestFPrintFormat("%-8C", (UChar
)0x65, "%-8c", (char)'e');
980 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
981 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
982 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
983 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
984 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
985 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
986 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
987 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
988 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
990 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
991 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
992 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
993 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
994 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
995 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
996 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
997 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
998 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
999 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1001 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1002 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1003 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1005 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1006 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1007 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1008 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1009 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1011 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1012 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1013 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1014 TestFPrintFormat("%#o", 123, "%#o", 123);
1015 TestFPrintFormat("%#o", -123, "%#o", -123);
1017 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1018 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1019 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1020 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1021 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1022 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1023 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1024 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1026 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1027 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1028 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1029 TestFPrintFormat("% d", 123456, "% d", 123456);
1030 TestFPrintFormat("% d", -123456, "% d", -123456);
1032 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1033 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1034 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1036 log_verbose("Get really crazy with the formatting.\n");
1038 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1039 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1040 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1041 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1043 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1044 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1045 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1046 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1047 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1048 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1049 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1050 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1051 TestFPrintFormat("%12d", 123, "%12d", 123);
1052 TestFPrintFormat("%12d", -123, "%12d", -123);
1053 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1054 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1056 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1057 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1058 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1059 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1060 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1061 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1062 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1063 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1064 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1065 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1066 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1067 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1068 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1069 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1071 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1072 /* Reinitialize the buffer to verify null termination works. */
1073 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1074 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1076 uNumPrinted
= u_fprintf(myFile
, "%d % d %d", -1234, 1234, 1234);
1078 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1079 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1081 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1082 cNumPrinted
= sprintf(buffer
, "%d % d %d", -1234, 1234, 1234);
1083 if (strcmp(buffer
, compBuffer
) != 0) {
1084 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer
, buffer
);
1086 if (cNumPrinted
!= uNumPrinted
) {
1087 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted
, cNumPrinted
);
1089 if (buffer
[uNumPrinted
+1] != '*') {
1090 log_err("%%d %% d %%d too much stored\n");
1095 #undef TestFPrintFormat
1097 #if !UCONFIG_NO_FORMATTING
1098 static void TestFScanSetFormat(const char *format
, const UChar
*uValue
, const char *cValue
, UBool expectedToPass
) {
1102 char compBuffer
[256];
1103 int32_t uNumScanned
;
1104 int32_t cNumScanned
;
1106 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1107 if (myFile
== NULL
) {
1108 log_err("Can't write test file for %s.\n", format
);
1111 /* Reinitialize the buffer to verify null termination works. */
1112 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1113 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1114 memset(buffer
, '*', sizeof(buffer
)/sizeof(*buffer
));
1115 buffer
[sizeof(buffer
)/sizeof(*buffer
)-1] = 0;
1117 u_fprintf(myFile
, "%S", uValue
);
1119 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1120 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1122 if (expectedToPass
) {
1123 u_austrncpy(compBuffer
, uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
));
1124 cNumScanned
= sscanf(cValue
, format
, buffer
);
1125 if (strncmp(buffer
, compBuffer
, sizeof(buffer
)/sizeof(*buffer
)) != 0) {
1126 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format
, compBuffer
, buffer
);
1128 if (cNumScanned
!= uNumScanned
) {
1129 log_err("%s number printed Got: %d, Expected: %d\n", format
, uNumScanned
, cNumScanned
);
1131 if (uNumScanned
> 0 && uBuffer
[u_strlen(uBuffer
)+1] != 0x2a) {
1132 log_err("%s too much stored\n", format
);
1136 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1137 log_err("%s too much stored on a failure\n", format
);
1143 #if !UCONFIG_NO_FORMATTING
1144 static void TestFScanset(void) {
1145 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1146 static const char abcChars
[] = "abccdefg";
1148 TestFScanSetFormat("%[bc]S", abcUChars
, abcChars
, TRUE
);
1149 TestFScanSetFormat("%[cb]S", abcUChars
, abcChars
, TRUE
);
1151 TestFScanSetFormat("%[ab]S", abcUChars
, abcChars
, TRUE
);
1152 TestFScanSetFormat("%[ba]S", abcUChars
, abcChars
, TRUE
);
1154 TestFScanSetFormat("%[ab]", abcUChars
, abcChars
, TRUE
);
1155 TestFScanSetFormat("%[ba]", abcUChars
, abcChars
, TRUE
);
1157 TestFScanSetFormat("%[abcdefgh]", abcUChars
, abcChars
, TRUE
);
1158 TestFScanSetFormat("%[;hgfedcba]", abcUChars
, abcChars
, TRUE
);
1160 TestFScanSetFormat("%[^a]", abcUChars
, abcChars
, TRUE
);
1161 TestFScanSetFormat("%[^e]", abcUChars
, abcChars
, TRUE
);
1162 TestFScanSetFormat("%[^ed]", abcUChars
, abcChars
, TRUE
);
1163 TestFScanSetFormat("%[^dc]", abcUChars
, abcChars
, TRUE
);
1164 TestFScanSetFormat("%[^e] ", abcUChars
, abcChars
, TRUE
);
1166 TestFScanSetFormat("%1[ab] ", abcUChars
, abcChars
, TRUE
);
1167 TestFScanSetFormat("%2[^f]", abcUChars
, abcChars
, TRUE
);
1169 TestFScanSetFormat("%[qrst]", abcUChars
, abcChars
, TRUE
);
1171 /* Extra long string for testing */
1172 TestFScanSetFormat(" %[qrst]",
1173 abcUChars
, abcChars
, TRUE
);
1175 TestFScanSetFormat("%[a-]", abcUChars
, abcChars
, TRUE
);
1178 TestFScanSetFormat("%[f-a]", abcUChars
, abcChars
, FALSE
);
1179 TestFScanSetFormat("%[c-a]", abcUChars
, abcChars
, FALSE
);
1180 TestFScanSetFormat("%[a", abcUChars
, abcChars
, FALSE
);
1181 /* The following is not deterministic on Windows */
1182 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1184 /* TODO: Need to specify precision with a "*" */
1187 #if !UCONFIG_NO_FORMATTING
1188 static void TestBadFScanfFormat(const char *format
, const UChar
*uValue
, const char *cValue
) {
1191 int32_t uNumScanned
;
1193 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", NULL
, NULL
);
1194 if (myFile
== NULL
) {
1195 log_err("Can't write test file for %s.\n", format
);
1198 /* Reinitialize the buffer to verify null termination works. */
1199 u_memset(uBuffer
, 0x2a, sizeof(uBuffer
)/sizeof(*uBuffer
));
1200 uBuffer
[sizeof(uBuffer
)/sizeof(*uBuffer
)-1] = 0;
1202 u_fprintf(myFile
, "%S", uValue
);
1204 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1205 uNumScanned
= u_fscanf(myFile
, format
, uBuffer
);
1207 if (uNumScanned
!= 0 || uBuffer
[0] != 0x2a || uBuffer
[1] != 0x2a) {
1208 log_err("%s too much stored on a failure\n", format
);
1212 #if !UCONFIG_NO_FORMATTING
1213 static void TestBadScanfFormat(void) {
1214 static const UChar abcUChars
[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1215 static const char abcChars
[] = "abccdefg";
1217 TestBadFScanfFormat("%[] ", abcUChars
, abcChars
);
1220 #if !UCONFIG_NO_FORMATTING
1221 static void Test_u_vfprintf(const char *expectedResult
, const char *format
, ...) {
1223 UChar uBuffer2
[256];
1228 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "UTF-8");
1230 log_err("Test file can't be opened\n");
1234 va_start(ap
, format
);
1235 count
= u_vfprintf(myFile
, format
, ap
);
1241 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", "UTF-8");
1243 log_err("Test file can't be opened\n");
1246 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1247 u_uastrcpy(uBuffer2
, expectedResult
);
1248 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1249 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1254 myFile
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1256 log_err("Test file can't be opened\n");
1259 u_uastrcpy(uBuffer
, format
);
1261 va_start(ap
, format
);
1262 count
= u_vfprintf_u(myFile
, uBuffer
, ap
);
1268 myFile
= u_fopen(STANDARD_TEST_FILE
, "r", "en_US_POSIX", NULL
);
1270 log_err("Test file can't be opened\n");
1273 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(*uBuffer
), myFile
);
1274 u_uastrcpy(uBuffer2
, expectedResult
);
1275 if (u_strcmp(uBuffer
, uBuffer2
) != 0) {
1276 log_err("Got two different results for \"%s\" expected \"%s\"\n", format
, expectedResult
);
1281 static void TestVargs(void) {
1282 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1285 static void TestTranslitOps(void)
1287 #if !UCONFIG_NO_TRANSLITERATION
1289 UErrorCode err
= U_ZERO_ERROR
;
1290 UTransliterator
*a
= NULL
, *b
= NULL
, *c
= NULL
;
1292 log_verbose("opening a transliterator and UFILE for testing\n");
1294 f
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
1297 log_err("Couldn't open test file for writing\n");
1301 a
= utrans_open("Latin-Greek", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
1304 log_err("Error opening transliterator %s\n", u_errorName(err
));
1310 log_verbose("setting a transliterator\n");
1311 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
1314 log_err("Error setting transliterator %s\n", u_errorName(err
));
1321 log_err("Error, a transliterator was already set!\n");
1324 b
= u_fsettransliterator(NULL
, U_WRITE
, a
, &err
);
1325 if(err
!= U_ILLEGAL_ARGUMENT_ERROR
)
1327 log_err("Error setting transliterator on NULL file err=%s\n", u_errorName(err
));
1332 log_err("Error getting the same transliterator was not returned on NULL file\n");
1335 err
= U_FILE_ACCESS_ERROR
;
1336 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
1337 if(err
!= U_FILE_ACCESS_ERROR
)
1339 log_err("Error setting transliterator on error status err=%s\n", u_errorName(err
));
1344 log_err("Error getting the same transliterator on error status\n");
1349 log_verbose("un-setting transliterator (setting to null)\n");
1350 c
= u_fsettransliterator(f
, U_WRITE
, NULL
, &err
);
1353 log_err("Err setting transliterator %s\n", u_errorName(err
));
1360 log_err("Err, transliterator that came back was not the original one.\n");
1363 log_verbose("Trying to set read transliterator (should fail)\n");
1364 b
= u_fsettransliterator(f
, U_READ
, NULL
, &err
);
1365 if(err
!= U_UNSUPPORTED_ERROR
)
1367 log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err
));
1373 log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err
));
1383 static void TestTranslitOut(void)
1385 #if !UCONFIG_NO_FORMATTING
1386 #if !UCONFIG_NO_TRANSLITERATION
1388 UErrorCode err
= U_ZERO_ERROR
;
1389 UTransliterator
*a
= NULL
, *b
= NULL
, *c
= NULL
;
1391 UChar compare
[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x0000 };
1395 log_verbose("opening a transliterator and UFILE for testing\n");
1397 f
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "utf-16");
1400 log_err("Couldn't open test file for writing\n");
1404 a
= utrans_open("Latin-Greek", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
1407 log_err("Err opening transliterator %s\n", u_errorName(err
));
1412 log_verbose("setting a transliterator\n");
1413 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
1416 log_err("Err setting transliterator %s\n", u_errorName(err
));
1423 log_err("Err, a transliterator was already set!\n");
1426 u_fprintf(f
, "Stephanos");
1430 log_verbose("Re reading test file to verify transliteration\n");
1431 infile
= fopen(STANDARD_TEST_FILE
, "rb");
1434 log_err("Couldn't reopen test file\n");
1438 len
=fread(ubuf
, sizeof(UChar
), u_strlen(compare
), infile
);
1439 log_verbose("Read %d UChars\n", len
);
1440 if(len
!= u_strlen(compare
))
1442 log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare
), len
);
1446 if(u_strlen(compare
) != u_strlen(ubuf
))
1448 log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare
), len
);
1451 if(u_strcmp(compare
, ubuf
))
1453 log_err("Read string doesn't match expected.\n");
1457 log_verbose("Read string matches expected.\n");
1466 addFileTest(TestNode
** root
) {
1467 #if !UCONFIG_NO_FORMATTING
1468 addTest(root
, &TestFile
, "file/TestFile");
1470 addTest(root
, &StdinBuffering
, "file/StdinBuffering");
1471 addTest(root
, &TestfgetsBuffers
, "file/TestfgetsBuffers");
1472 addTest(root
, &TestFileReadBuffering
, "file/TestFileReadBuffering");
1473 addTest(root
, &TestfgetsLineCount
, "file/TestfgetsLineCount");
1474 addTest(root
, &TestfgetsNewLineHandling
, "file/TestfgetsNewLineHandling");
1475 addTest(root
, &TestCodepage
, "file/TestCodepage");
1476 #if !UCONFIG_NO_FORMATTING
1477 addTest(root
, &TestCodepageAndLocale
, "file/TestCodepageAndLocale");
1478 addTest(root
, &TestFprintfFormat
, "file/TestFprintfFormat");
1479 addTest(root
, &TestFScanset
, "file/TestFScanset");
1480 addTest(root
, &TestFilePrintCompatibility
, "file/TestFilePrintCompatibility");
1481 addTest(root
, &TestBadScanfFormat
, "file/TestBadScanfFormat");
1482 addTest(root
, &TestVargs
, "file/TestVargs");
1485 #if !UCONFIG_NO_TRANSLITERATION
1486 addTest(root
, &TestTranslitOps
, "file/translit/ops");
1487 #if !UCONFIG_NO_FORMATTING
1488 addTest(root
, &TestTranslitOut
, "file/translit/out");