]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/filetst.c
ICU-400.39.tar.gz
[apple/icu.git] / icuSources / test / iotest / filetst.c
1 /*
2 **********************************************************************
3 * Copyright (C) 2004-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: filetst.c
7 * encoding: US-ASCII
8 * tab size: 8 (not used)
9 * indentation:4
10 *
11 * created on: 2004apr06
12 * created by: George Rhoten
13 */
14
15 #include "iotest.h"
16 #include "unicode/ustdio.h"
17 #include "unicode/ustring.h"
18 #include "unicode/uloc.h"
19
20 #include <string.h>
21
22 const char *STANDARD_TEST_FILE = "iotest-c.txt";
23
24
25 #if !UCONFIG_NO_FORMATTING
26 static void TestFileFromICU(UFILE *myFile) {
27 int32_t n[1];
28 float myFloat = -1234.0;
29 int32_t newValuePtr[1];
30 double newDoubleValuePtr[1];
31 UChar myUString[256];
32 UChar uStringBuf[256];
33 char myString[256] = "";
34 char testBuf[256] = "";
35 void *origPtr, *ptr;
36 U_STRING_DECL(myStringOrig, "My-String", 9);
37
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));
43
44 if (myFile == NULL) {
45 log_err("Can't write test file.\n");
46 return;
47 }
48
49 *n = -1234;
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) {
55 /* iSeries */
56 union {
57 int32_t arr[4];
58 void *ptr;
59 } massiveBigEndianPtr = {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
60 origPtr = massiveBigEndianPtr.ptr;
61 } else {
62 log_err("sizeof(void*)=%d hasn't been tested before", (int)sizeof(void*));
63 }
64
65 /* Test fprintf */
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);
86
87 if (u_feof(myFile)) {
88 log_err("Got feof while writing the file.\n");
89 }
90
91 *n = 1;
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");
95 *n = 1;
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. */
98
99 u_fclose(myFile);
100 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
101
102 if (myFile == NULL) {
103 log_err("Can't read test file.");
104 return;
105 }
106
107 if (u_feof(myFile)) {
108 log_err("Got feof while reading the file and not at the end of the file.\n");
109 }
110
111 myUString[0] = u_fgetc(myFile);
112 if (myUString[0] != 0x53 /* S */) {
113 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString[0]);
114 }
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]);
119 }
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]);
124 }
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]);
131 }
132 u_fungetc(myUString[2], myFile);
133 u_fungetc(myUString[1], myFile);
134 u_fungetc(myUString[0], myFile);
135
136 *n = -1234;
137
138 *newValuePtr = 1;
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);
142 }
143 *newValuePtr = 1;
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);
147 }
148 *newValuePtr = 1;
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);
152 }
153 *newValuePtr = 1;
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);
157 }
158 *newValuePtr = 1;
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);
162 }
163 *newValuePtr = 1;
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);
167 }
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);
172 }
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);
177 }
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);
182 }
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);
187 }
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);
192 }
193 ptr = NULL;
194 u_fscanf(myFile, "Pointer %%p: %p\n", &ptr);
195 if (ptr != origPtr) {
196 log_err("%%p Got: %p, Expected: %p\n", ptr, origPtr);
197 }
198 u_fscanf(myFile, "Char %%c: %c\n", myString);
199 if (*myString != 'A') {
200 log_err("%%c Got: %c, Expected: A\n", *myString);
201 }
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);
205 }
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);
209 }
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);
213 }
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);
218 }
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);
223 }
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);
228 }
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);
233 }
234
235 u_fgets(myUString, 4, myFile);
236 myString[2] = '!';
237 myString[3] = '!';
238 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
239 if (myString == NULL || strcmp(myString, "\t\n") != 0) {
240 log_err("u_fgets got \"%s\"\n", myString);
241 }
242
243 if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
244 log_err("u_fgets did not return myUString\n");
245 }
246 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
247 if (myString == NULL || strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
248 log_err("u_fgets got \"%s\"\n", myString);
249 }
250
251 if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
252 log_err("u_fgets did not return myUString\n");
253 }
254 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
255 if (myString == NULL || strcmp(myString, "Pointer to integer Value: 37\n") != 0) {
256 log_err("u_fgets got \"%s\"\n", myString);
257 }
258
259 if (u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile) != myUString) {
260 log_err("u_fgets did not return myUString\n");
261 }
262 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
263 if (myString == NULL || strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
264 log_err("u_fgets got \"%s\"\n", myString);
265 }
266
267 if (u_fgets(myUString, 0, myFile) != NULL) {
268 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString);
269 }
270
271 if (u_fgets(myUString, 1, myFile) != myUString) {
272 log_err("u_fgets did not return myUString\n");
273 }
274 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
275 if (myString == NULL || strcmp(myString, "") != 0) {
276 log_err("u_fgets got \"%s\"\n", myString);
277 }
278
279 if (u_fgets(myUString, 2, myFile) != myUString) {
280 log_err("u_fgets did not return myUString\n");
281 }
282 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
283 if (myString == NULL || strcmp(myString, "\t") != 0) {
284 log_err("u_fgets got \"%s\"\n", myString);
285 }
286
287 u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile),
288 sizeof(myUString)/sizeof(*myUString));
289 if (strcmp(myString, "Normal fprintf count: n=1 n=1\n") != 0) {
290 log_err("u_fgets got \"%s\"\n", myString);
291 }
292
293 if (u_feof(myFile)) {
294 log_err("Got feof while reading the file and not at the end of the file.\n");
295 }
296 u_austrncpy(myString, u_fgets(myUString, sizeof(myUString)/sizeof(*myUString), myFile),
297 sizeof(myUString)/sizeof(*myUString));
298 if (strcmp(myString, "\tNormal fprintf count value: n=27\n") != 0) {
299 log_err("u_fgets got \"%s\"\n", myString);
300 }
301 if (!u_feof(myFile)) {
302 log_err("Did not get feof while reading the end of the file.\n");
303 }
304 if (u_fscanf(myFile, "%S\n", myUString) != 0) {
305 log_err("u_fscanf read data while reading the end of the file.\n");
306 }
307
308 u_fclose(myFile);
309 }
310
311 static void TestFile(void) {
312 /* FILE *standardFile;*/
313
314 log_verbose("Testing u_fopen\n");
315 TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL));
316
317 /* Don't know how to make this work without stdout or stderr */
318 /*
319 log_verbose("Testing u_finit\n");
320 standardFile = fopen(STANDARD_TEST_FILE, "wb");
321 TestFileFromICU(u_finit(standardFile, NULL, NULL));
322 fclose(standardFile);
323 */
324 }
325 #endif
326
327 static void StdinBuffering(void) {
328 #if 0
329 UChar buff[255];
330 int32_t num = 0;
331 UFILE *uStdIn = NULL;
332 UFILE *uStdOut = NULL;
333 uStdIn = u_finit(stdin, NULL, NULL);
334 uStdOut = u_finit(stdout, NULL, NULL);
335 if (uStdIn == NULL)
336 return;
337
338 buff[0] = 0x40;
339 buff[1] = 0;
340 u_fgets(buff, sizeof(buff)/sizeof(buff[0]), uStdIn);
341 u_fprintf(uStdOut, "%S\n", buff);
342 u_fscanf(uStdIn, "%d", &num);
343 u_fprintf(uStdOut, "%d\n", num);
344 u_fscanf(uStdIn, "%d", &num);
345 u_fprintf(uStdOut, "%d\n", num);
346 #else
347 log_verbose("Test disabled because it requires user interaction");
348 #endif
349 }
350
351 static void TestCodepageAndLocale(void) {
352 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
353 if (myFile == NULL) {
354 log_err("Can't write test file.\n");
355 return;
356 }
357 if (u_fgetcodepage(myFile) == NULL
358 || strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
359 {
360 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
361 u_fgetcodepage(myFile), ucnv_getDefaultName());
362 }
363 #if !UCONFIG_NO_FORMATTING
364 if (u_fgetlocale(myFile) == NULL
365 || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
366 {
367 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
368 u_fgetlocale(myFile), uloc_getDefault());
369 }
370 #endif
371 u_fclose(myFile);
372
373 myFile = u_fopen(STANDARD_TEST_FILE, "w", "es", NULL);
374 if (u_fgetcodepage(myFile) == NULL
375 || strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
376 {
377 log_err("Didn't get the proper default codepage for \"es\". Got %s expected: iso-8859-1\n",
378 u_fgetcodepage(myFile));
379 }
380 #if !UCONFIG_NO_FORMATTING
381 if (u_fgetlocale(myFile) == NULL
382 || strcmp(u_fgetlocale(myFile), "es") != 0)
383 {
384 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
385 u_fgetlocale(myFile), "es");
386 }
387 #endif
388 u_fclose(myFile);
389
390 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
391 if (u_fgetcodepage(myFile) == NULL
392 || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
393 {
394 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
395 u_fgetcodepage(myFile));
396 }
397 #if !UCONFIG_NO_FORMATTING
398 if (u_fgetlocale(myFile) == NULL
399 || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
400 {
401 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
402 u_fgetlocale(myFile), uloc_getDefault());
403 }
404 #endif
405 u_fclose(myFile);
406
407 myFile = u_fopen(STANDARD_TEST_FILE, "w", "zh", "UTF-16");
408 if (u_fgetcodepage(myFile) == NULL
409 || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
410 {
411 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
412 u_fgetcodepage(myFile));
413 }
414 #if !UCONFIG_NO_FORMATTING
415 if (u_fgetlocale(myFile) == NULL
416 || strcmp(u_fgetlocale(myFile), "zh") != 0)
417 {
418 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
419 u_fgetlocale(myFile), "zh");
420 }
421 #endif
422 u_fclose(myFile);
423 }
424
425
426 static void TestfgetsBuffers(void) {
427 UChar buffer[2048];
428 UChar expectedBuffer[2048];
429 static const char testStr[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
430 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
431 int32_t expectedSize = (int32_t)strlen(testStr);
432 int32_t readSize;
433 int32_t repetitions;
434
435 if (myFile == NULL) {
436 log_err("Can't write test file.\n");
437 return;
438 }
439
440 u_fputc(0x3BC, myFile);
441 if (u_fputc(0x110000, myFile) != U_EOF) {
442 log_err("u_fputc should return U_EOF for 0x110000.\n");
443 }
444 if (u_fputc((UChar32)0xFFFFFFFFu, myFile) != U_EOF) {
445 log_err("u_fputc should return U_EOF for 0xFFFFFFFF.\n");
446 }
447 u_fputc(0xFF41, myFile);
448 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
449 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
450 u_uastrncpy(buffer, testStr, expectedSize+1);
451 for (repetitions = 0; repetitions < 16; repetitions++) {
452 u_file_write(buffer, expectedSize, myFile);
453 u_strcat(expectedBuffer, buffer);
454 }
455 u_fclose(myFile);
456
457 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
458 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16");
459 if (u_fgetc(myFile) != 0x3BC) {
460 log_err("The first character is wrong\n");
461 }
462 if (u_fgetc(myFile) != 0xFF41) {
463 log_err("The second character is wrong\n");
464 }
465 if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) {
466 log_err("Didn't get the buffer back\n");
467 return;
468 }
469 readSize = u_strlen(buffer);
470 if (readSize != expectedSize*repetitions) {
471 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
472 }
473 if (buffer[(expectedSize*repetitions) + 1] != 0xBEEF) {
474 log_err("u_fgets wrote too much data\n");
475 }
476 if (u_strcmp(buffer, expectedBuffer) != 0) {
477 log_err("Did get expected string back\n");
478 }
479 if (strcmp(u_fgetcodepage(myFile), "UTF-16") != 0) {
480 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile));
481 }
482 u_fclose(myFile);
483
484 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
485
486 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
487
488 u_fputc(0x3BC, myFile);
489 u_fputc(0xFF41, myFile);
490 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
491 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
492 u_uastrncpy(buffer, testStr, expectedSize+1);
493 for (repetitions = 0; repetitions < 16; repetitions++) {
494 u_file_write(buffer, expectedSize, myFile);
495 u_strcat(expectedBuffer, buffer);
496 }
497 u_fclose(myFile);
498
499 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
500 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
501 if (strcmp(u_fgetcodepage(myFile), "UTF-8") != 0) {
502 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile));
503 }
504 if (u_fgetc(myFile) != 0x3BC) {
505 log_err("The first character is wrong\n");
506 }
507 if (u_fgetc(myFile) != 0xFF41) {
508 log_err("The second character is wrong\n");
509 }
510 if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != buffer) {
511 log_err("Didn't get the buffer back\n");
512 return;
513 }
514 readSize = u_strlen(buffer);
515 if (readSize != expectedSize*repetitions) {
516 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
517 }
518 if (buffer[(expectedSize*repetitions) + 1] != 0xBEEF) {
519 log_err("u_fgets wrote too much data\n");
520 }
521 if (u_strcmp(buffer, expectedBuffer) != 0) {
522 log_err("Did get expected string back\n");
523 }
524 u_fclose(myFile);
525
526
527 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
528
529 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
530
531 u_fputc(0xFF41, myFile);
532 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
533 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
534 u_uastrncpy(buffer, testStr, expectedSize+1);
535 for (repetitions = 0; repetitions < 1; repetitions++) {
536 u_file_write(buffer, expectedSize, myFile);
537 u_strcat(expectedBuffer, buffer);
538 }
539 u_fclose(myFile);
540
541 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
542 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
543 if (u_fgets(buffer, 2, myFile) != buffer) {
544 log_err("Didn't get the buffer back\n");
545 return;
546 }
547 readSize = u_strlen(buffer);
548 if (readSize != 1) {
549 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), 1);
550 }
551 if (buffer[0] != 0xFF41 || buffer[1] != 0) {
552 log_err("Did get expected string back\n");
553 }
554 if (buffer[2] != 0xBEEF) {
555 log_err("u_fgets wrote too much data\n");
556 }
557 u_fclose(myFile);
558
559 }
560
561 static void TestFileReadBuffering(void) {
562 UChar buffer[1024];
563 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
564 int32_t how_many;
565 int32_t repetitions;
566
567 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
568 for (repetitions = 0; repetitions < 2; repetitions++) {
569 u_file_write(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
570 }
571
572 u_fclose(myFile);
573 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
574 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16");
575 how_many = u_file_read(buffer, 1024, myFile);
576 if (how_many != 1024 || buffer[1023] != 0xBEEF) {
577 log_err("u_file_read read too much or not enough data\n");
578 }
579 u_fclose(myFile);
580 }
581
582 static void TestfgetsLineCount(void) {
583 UChar buffer[2048];
584 UChar expectedBuffer[2048];
585 char charBuffer[2048];
586 static const char testStr[] = "This is a test string that tests u_fgets. It makes sure that we don't try to read too much!";
587 UFILE *myFile = NULL;
588 FILE *stdFile = fopen(STANDARD_TEST_FILE, "w");
589 int32_t expectedSize = (int32_t)strlen(testStr);
590 int32_t repetitions;
591 int32_t nlRepetitions;
592
593 if (stdFile == NULL) {
594 log_err("Can't write test file.\n");
595 return;
596 }
597 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
598
599 for (repetitions = 0; repetitions < 16; repetitions++) {
600 fwrite(testStr, sizeof(testStr[0]), expectedSize, stdFile);
601 for (nlRepetitions = 0; nlRepetitions < repetitions; nlRepetitions++) {
602 fwrite("\n", sizeof(testStr[0]), 1, stdFile);
603 }
604 }
605 fclose(stdFile);
606
607 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
608 stdFile = fopen(STANDARD_TEST_FILE, "r");
609
610 for (;;) {
611 char *returnedCharBuffer;
612 UChar *returnedUCharBuffer;
613
614 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
615 returnedCharBuffer = fgets(charBuffer, sizeof(charBuffer)/sizeof(charBuffer[0]), stdFile);
616 returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
617
618 if (!returnedCharBuffer && !returnedUCharBuffer) {
619 /* Both returned NULL. stop. */
620 break;
621 }
622 if (returnedCharBuffer != charBuffer) {
623 log_err("Didn't get the charBuffer back\n");
624 continue;
625 }
626 u_uastrncpy(expectedBuffer, charBuffer, (int32_t)strlen(charBuffer)+1);
627 if (returnedUCharBuffer != buffer) {
628 log_err("Didn't get the buffer back\n");
629 continue;
630 }
631 if (u_strcmp(buffer, expectedBuffer) != 0) {
632 log_err("buffers are different\n");
633 }
634 if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
635 log_err("u_fgets wrote too much\n");
636 }
637 }
638 fclose(stdFile);
639 u_fclose(myFile);
640 }
641
642 static void TestfgetsNewLineHandling(void) {
643 UChar buffer[256];
644 static const UChar testUStr[][16] = {
645 {0x000D, 0},
646 {0x000D, 0x000A, 0},
647 {0x000D, 0},
648 {0x000D, 0},
649 {0x0085, 0},
650 {0x000A, 0},
651 {0x000D, 0},
652 {0x000B, 0},
653 {0x000C, 0},
654 {0x000C, 0},
655 {0x2028, 0},
656 {0x0085, 0},
657 {0x2029, 0},
658 {0x0085, 0},
659
660 {0x008B, 0x000D, 0},
661 {0x00A0, 0x000D, 0x000A, 0},
662 {0x3000, 0x000D, 0},
663 {0xd800, 0xdfff, 0x000D, 0},
664 {0x00AB, 0x0085, 0},
665 {0x00AC, 0x000A, 0},
666 {0x00AD, 0x000D, 0},
667 {0x00BA, 0x000B, 0},
668 {0x00AB, 0x000C, 0},
669 {0x00B1, 0x000C, 0},
670 {0x30BB, 0x2028, 0},
671 {0x00A5, 0x0085, 0},
672 {0x0080, 0x2029, 0},
673 {0x00AF, 0x0085, 0}
674
675 };
676 UFILE *myFile = NULL;
677 int32_t lineIdx;
678
679 myFile = u_fopen(STANDARD_TEST_FILE, "wb", NULL, "UTF-8");
680 if (myFile == NULL) {
681 log_err("Can't write test file.\n");
682 return;
683 }
684 for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) {
685 u_file_write(testUStr[lineIdx], u_strlen(testUStr[lineIdx]), myFile);
686 }
687 u_fclose(myFile);
688
689 myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, "UTF-8");
690
691 for (lineIdx = 0; lineIdx < (int32_t)(sizeof(testUStr)/sizeof(testUStr[0])); lineIdx++) {
692 UChar *returnedUCharBuffer;
693
694 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
695 returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
696
697 if (!returnedUCharBuffer) {
698 /* Returned NULL. stop. */
699 break;
700 }
701 if (u_strcmp(buffer, testUStr[lineIdx]) != 0) {
702 log_err("buffers are different at index = %d\n", lineIdx);
703 }
704 if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
705 log_err("u_fgets wrote too much\n");
706 }
707 }
708 if (lineIdx != (int32_t)(sizeof(testUStr)/sizeof(testUStr[0]))) {
709 log_err("u_fgets read too much\n");
710 }
711 if (u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile) != NULL) {
712 log_err("u_file_write wrote too much\n");
713 }
714 u_fclose(myFile);
715 }
716
717 static void TestLineCount(const char *prefixLine, const char *line, int32_t numRepititions) {
718 UChar buffer[64];
719 UChar expectedBuffer[64];
720 int32_t lineLen = strlen(line);
721 UChar *returnedUCharBuffer;
722 int32_t repetitions;
723 UFILE *myFile = NULL;
724 FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb");
725
726 if (stdFile == NULL) {
727 log_err("Can't write test file.\n");
728 return;
729 }
730 /* Write a prefix line and then write a bunch of lines */
731 fwrite(prefixLine, strlen(prefixLine), 1, stdFile);
732 for (repetitions = 0; repetitions < numRepititions; repetitions++) {
733 fwrite(line, lineLen, 1, stdFile);
734 }
735 fclose(stdFile);
736
737 myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL);
738 if (myFile == NULL) {
739 log_err("Can't read test file.\n");
740 return;
741 }
742
743 /* Read the prefix line. This can make sure that a Windows newline is either on a boundary or before it. */
744 u_uastrncpy(expectedBuffer, prefixLine, (int32_t)strlen(prefixLine)+1);
745 returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
746 if (u_strcmp(returnedUCharBuffer, expectedBuffer) != 0) {
747 log_err("prefix buffer is different. prefix=\"%s\"\n", prefixLine);
748 return;
749 }
750
751 u_uastrncpy(expectedBuffer, line, (int32_t)strlen(line)+1);
752 for (repetitions = 0; ; repetitions++) {
753 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
754 returnedUCharBuffer = u_fgets(buffer, sizeof(buffer)/sizeof(buffer[0]), myFile);
755
756 if (!returnedUCharBuffer) {
757 /* returned NULL. stop. */
758 break;
759 }
760 if (u_strcmp(buffer, expectedBuffer) != 0) {
761 log_err("buffers are different at count %d\n", repetitions);
762 }
763 if (buffer[u_strlen(buffer)+1] != 0xBEEF) {
764 log_err("u_fgets wrote too much\n");
765 }
766 }
767 if (repetitions != numRepititions) {
768 log_err("got wrong number of lines. got=%d expected=%d\n", repetitions, numRepititions);
769 }
770 u_fclose(myFile);
771 }
772
773 static void TestfgetsNewLineCount(void) {
774 /* This makes sure that lines are correctly handled between buffer boundaries. */
775 TestLineCount("\n", "\n", 1024); /* Unix newlines */
776 TestLineCount("\r\n", "\r\n", 1024);/* Windows newlines */
777 TestLineCount("a\r\n", "\r\n", 1024);/* Windows newlines offset by 1 byte */
778 TestLineCount("\r\n", "a\r\n", 1024);/* Windows newlines offset with data */
779 TestLineCount("\n", "a\n", 1024); /* Unix newlines offset with data */
780 TestLineCount("\n", "\r\n", 1024); /* a mixed number of lines. */
781 }
782
783 static void TestFgetsLineBuffering(void) {
784 UChar buffer[2003]; /* Use a non-power of 2 or 10 */
785 UChar *returnedUCharBuffer;
786 int32_t repetitions;
787 UFILE *myFile = NULL;
788 FILE *stdFile = fopen(STANDARD_TEST_FILE, "wb");
789
790 if (stdFile == NULL) {
791 log_err("Can't write test file.\n");
792 return;
793 }
794 u_memset(buffer, 0xBEEF, sizeof(buffer)/sizeof(buffer[0]));
795
796 /* Write one very long line */
797 for (repetitions = 0; repetitions < ((sizeof(buffer)/sizeof(buffer[0]))*2); repetitions++) {
798 fwrite(repetitions ? "1" : "2", 1, 1, stdFile);
799 }
800 fclose(stdFile);
801
802 myFile = u_fopen(STANDARD_TEST_FILE, "rb", NULL, NULL);
803 if (myFile == NULL) {
804 log_err("Can't read test file.\n");
805 return;
806 }
807
808 /* Read part of one very long line */
809 returnedUCharBuffer = u_fgets(buffer, (sizeof(buffer)/sizeof(buffer[0]))-1, myFile);
810 if (u_strlen(returnedUCharBuffer) != ((sizeof(buffer)/sizeof(buffer[0]))-2)) {
811 log_err("Line is wrong length. Got %d. Expected %d.\n",
812 u_strlen(returnedUCharBuffer), ((sizeof(buffer)/sizeof(buffer[0]))-2));
813 }
814 /* We better not read too much */
815 if (buffer[(sizeof(buffer)/sizeof(buffer[0]))-1] != 0xBEEF) {
816 log_err("Too much data was written\n");
817 }
818
819 u_fclose(myFile);
820 }
821
822
823 static void TestCodepage(void) {
824 UFILE *myFile = NULL;
825 static const UChar strABAccentA[] = { 0x0041, 0x0042, 0x00C1, 0x0043, 0};
826 static const UChar strBadConversion[] = { 0x0041, 0x0042, 0xfffd, 0x0043, 0};
827 UChar testBuf[sizeof(strABAccentA)/sizeof(strABAccentA[0])*2]; /* *2 to see if too much was */
828 char convName[UCNV_MAX_CONVERTER_NAME_LENGTH];
829 int32_t retVal;
830 UErrorCode status = U_ZERO_ERROR;
831
832 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "absurd converter that can't be opened");
833
834 if (myFile) {
835 log_err("Recieved a UFILE * with an invalid codepage parameter\n");
836 u_fclose(myFile);
837 }
838
839 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "ISO-8859-1");
840 if (myFile == NULL) {
841 log_err("Can't write test file for iso-8859-1.\n");
842 return;
843 }
844 if (strcmp("ISO-8859-1", u_fgetcodepage(myFile)) != 0) {
845 log_err("Couldn't get ISO-8859-1 back as opened codepage\n");
846 }
847 u_file_write(strABAccentA, u_strlen(strABAccentA), myFile);
848 u_fclose(myFile);
849
850 /* Now see what we got wrote */
851 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
852 if (u_fsetcodepage("ISO-8859-1", myFile) != 0) {
853 log_err("u_fsetcodepage didn't set the codepage\n");
854 }
855 retVal = u_file_read(testBuf, u_strlen(strABAccentA), myFile);
856 if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
857 log_err("The test data was read and written differently!\n");
858 }
859 if (retVal != u_strlen(strABAccentA)) {
860 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
861 }
862 u_fclose(myFile);
863
864 /* What happens on invalid input? */
865 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "ISO-8859-1");
866 if (strcmp(ucnv_getName(u_fgetConverter(myFile), &status), "ISO-8859-1") != 0) {
867 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile), &status));
868 }
869 if (u_fsetcodepage("UTF-8", myFile) != 0) {
870 log_err("u_fsetcodepage didn't set the codepage to UTF-8\n");
871 }
872 if (strcmp(ucnv_getName(u_fgetConverter(myFile), &status), "UTF-8") != 0) {
873 log_err("u_fgetConverter returned %s\n", ucnv_getName(u_fgetConverter(myFile), &status));
874 }
875 retVal = u_file_read(testBuf, u_strlen(strBadConversion), myFile);
876 if (u_strncmp(strBadConversion, testBuf, u_strlen(strBadConversion)) != 0) {
877 log_err("The test data wasn't subsituted as expected\n");
878 }
879 u_fclose(myFile);
880
881 /* Can't currently swap codepages midstream */
882 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "ISO-8859-1");
883 strcpy(convName, u_fgetcodepage(myFile));
884 u_file_read(testBuf, 1, myFile);
885 if (u_fsetcodepage("UTF-8", myFile) == 0) {
886 log_err("u_fsetcodepage set the codepage after reading a byte\n");
887 }
888 retVal = u_file_read(testBuf + 1, u_strlen(strABAccentA) - 1, myFile);
889 if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
890 log_err("u_fsetcodepage changed the codepages after writing data\n");
891 }
892 if ((retVal + 1) != u_strlen(strABAccentA)) {
893 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
894 }
895 u_frewind(myFile);
896 retVal = u_file_read(testBuf, u_strlen(strABAccentA), myFile);
897 if (u_strncmp(strABAccentA, testBuf, u_strlen(strABAccentA)) != 0) {
898 log_err("The test data was read and written differently!\n");
899 }
900 if (retVal != u_strlen(strABAccentA)) {
901 log_err("The test data returned different lengths. Got: %d, Expected %d\n", retVal, u_strlen(strABAccentA));
902 }
903 u_fclose(myFile);
904
905 }
906
907 #if !UCONFIG_NO_FORMATTING
908 static void TestFilePrintCompatibility(void) {
909 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
910 FILE *myCFile;
911 int32_t num;
912 char cVal;
913 static const UChar emptyStr[] = {0};
914 char readBuf[512] = "";
915 char testBuf[512] = "";
916
917 if (myFile == NULL) {
918 log_err("Can't write test file.\n");
919 return;
920 }
921 #if !UCONFIG_NO_FORMATTING
922 if (strcmp(u_fgetlocale(myFile), "en_US_POSIX") != 0) {
923 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile));
924 }
925 #endif
926
927 /* Compare against C API compatibility */
928 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
929 u_fprintf(myFile, "%x ", num);
930 u_fprintf(myFile, "%X ", num);
931 u_fprintf(myFile, "%o ", num);
932 u_fprintf(myFile, "%d ", num);
933 u_fprintf(myFile, "%i ", num);
934 u_fprintf(myFile, "%f ", (double)num);
935 /* u_fprintf(myFile, "%e ", (double)num);
936 u_fprintf(myFile, "%E ", (double)num);*/
937 u_fprintf(myFile, "%g ", (double)num);
938 u_fprintf(myFile, "%G", (double)num);
939 u_fputs(emptyStr, myFile);
940 }
941
942 u_fprintf_u(myFile, NEW_LINE);
943
944 for (num = 0; num < 0x80; num++) {
945 u_fprintf(myFile, "%c", num);
946 }
947
948 u_fclose(myFile);
949 myCFile = fopen(STANDARD_TEST_FILE, "rb");
950 if (myCFile == NULL) {
951 log_err("Can't read test file.");
952 return;
953 }
954
955 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
956 fscanf(myCFile, "%s", readBuf);
957 sprintf(testBuf, "%x", (int)num);
958 if (strcmp(readBuf, testBuf) != 0) {
959 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
960 }
961
962 fscanf(myCFile, "%s", readBuf);
963 sprintf(testBuf, "%X", (int)num);
964 if (strcmp(readBuf, testBuf) != 0) {
965 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
966 }
967
968 fscanf(myCFile, "%s", readBuf);
969 sprintf(testBuf, "%o", (int)num);
970 if (strcmp(readBuf, testBuf) != 0) {
971 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
972 }
973
974 /* fprintf is not compatible on all platforms e.g. the iSeries */
975 fscanf(myCFile, "%s", readBuf);
976 sprintf(testBuf, "%d", (int)num);
977 if (strcmp(readBuf, testBuf) != 0) {
978 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
979 }
980
981 fscanf(myCFile, "%s", readBuf);
982 sprintf(testBuf, "%i", (int)num);
983 if (strcmp(readBuf, testBuf) != 0) {
984 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
985 }
986
987 fscanf(myCFile, "%s", readBuf);
988 sprintf(testBuf, "%f", (double)num);
989 if (strcmp(readBuf, testBuf) != 0) {
990 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
991 }
992
993 /* fscanf(myCFile, "%s", readBuf);
994 sprintf(testBuf, "%e", (double)num);
995 if (strcmp(readBuf, testBuf) != 0) {
996 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
997 }
998
999 fscanf(myCFile, "%s", readBuf);
1000 sprintf(testBuf, "%E", (double)num);
1001 if (strcmp(readBuf, testBuf) != 0) {
1002 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1003 }*/
1004
1005 fscanf(myCFile, "%s", readBuf);
1006 sprintf(testBuf, "%g", (double)num);
1007 if (strcmp(readBuf, testBuf) != 0) {
1008 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1009 }
1010
1011 fscanf(myCFile, "%s", readBuf);
1012 sprintf(testBuf, "%G", (double)num);
1013 if (strcmp(readBuf, testBuf) != 0) {
1014 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
1015 }
1016 }
1017
1018 /* Properly eat the newlines */
1019 for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
1020 fscanf(myCFile, "%c", &cVal);
1021 if (cVal != C_NEW_LINE[num]) {
1022 log_err("OS newline error\n");
1023 }
1024 }
1025 for (num = 0; num < (int32_t)strlen(C_NEW_LINE); num++) {
1026 fscanf(myCFile, "%c", &cVal);
1027 if (cVal != C_NEW_LINE[num]) {
1028 log_err("ustdio newline error\n");
1029 }
1030 }
1031
1032 for (num = 0; num < 0x80; num++) {
1033 cVal = -1;
1034 fscanf(myCFile, "%c", &cVal);
1035 if (num != cVal) {
1036 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
1037 }
1038 }
1039 fclose(myCFile);
1040 }
1041 #endif
1042
1043 #define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
1044 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
1045 if (myFile == NULL) {\
1046 log_err("Can't write test file for %s.\n", uFormat);\
1047 return;\
1048 }\
1049 /* Reinitialize the buffer to verify null termination works. */\
1050 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1051 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));\
1052 \
1053 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1054 u_fclose(myFile);\
1055 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
1056 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);\
1057 u_fclose(myFile);\
1058 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1059 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1060 if (strcmp(buffer, compBuffer) != 0) {\
1061 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1062 }\
1063 if (cNumPrinted != uNumPrinted) {\
1064 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1065 }\
1066 if (buffer[uNumPrinted+1] != '*') {\
1067 log_err("%" uFormat " too much stored\n");\
1068 }\
1069
1070 #if !UCONFIG_NO_FORMATTING
1071 static void TestFprintfFormat(void) {
1072 static const UChar abcUChars[] = {0x61,0x62,0x63,0};
1073 static const char abcChars[] = "abc";
1074 UChar uBuffer[256];
1075 char buffer[256];
1076 char compBuffer[256];
1077 int32_t uNumPrinted;
1078 int32_t cNumPrinted;
1079 UFILE *myFile;
1080
1081 TestFPrintFormat("%8S", abcUChars, "%8s", abcChars);
1082 TestFPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
1083 TestFPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
1084
1085 TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
1086 TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
1087 TestFPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
1088
1089 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1090 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1091
1092 TestFPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
1093 TestFPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
1094
1095 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1096 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1097 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1098 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1099 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1100 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1101 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1102 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1103 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1104
1105 /* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1106 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1107 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1108 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1109 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1110 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1111 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1112 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1113 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1114 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1115
1116 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1117 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1118 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1119
1120 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1121 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1122 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1123 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1124 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1125
1126 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1127 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1128 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1129 TestFPrintFormat("%#o", 123, "%#o", 123);
1130 TestFPrintFormat("%#o", -123, "%#o", -123);
1131
1132 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1133 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1134 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1135 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1136 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1137 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1138 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1139 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1140
1141 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1142 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1143 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1144 TestFPrintFormat("% d", 123456, "% d", 123456);
1145 TestFPrintFormat("% d", -123456, "% d", -123456);
1146
1147 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1148 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1149 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1150
1151 log_verbose("Get really crazy with the formatting.\n");
1152
1153 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1154 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1155 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1156 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1157
1158 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
1159 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
1160 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
1161 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
1162 TestFPrintFormat("%+12d", 123, "%+12d", 123);
1163 TestFPrintFormat("%+12d", -123, "%+12d", -123);
1164 TestFPrintFormat("% 12d", 123, "% 12d", 123);
1165 TestFPrintFormat("% 12d", -123, "% 12d", -123);
1166 TestFPrintFormat("%12d", 123, "%12d", 123);
1167 TestFPrintFormat("%12d", -123, "%12d", -123);
1168 TestFPrintFormat("%.12d", 123, "%.12d", 123);
1169 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1170
1171 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1172 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1173 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 1.234);
1174 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1175 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1176 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1177 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1178 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1179 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1180 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1181 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1182 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1183 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1184 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1185
1186 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
1187 /* Reinitialize the buffer to verify null termination works. */
1188 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
1189 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
1190
1191 uNumPrinted = u_fprintf(myFile, "%d % d %d", -1234, 1234, 1234);
1192 u_fclose(myFile);
1193 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
1194 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
1195 u_fclose(myFile);
1196 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
1197 cNumPrinted = sprintf(buffer, "%d % d %d", -1234, 1234, 1234);
1198 if (strcmp(buffer, compBuffer) != 0) {
1199 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);
1200 }
1201 if (cNumPrinted != uNumPrinted) {
1202 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);
1203 }
1204 if (buffer[uNumPrinted+1] != '*') {
1205 log_err("%%d %% d %%d too much stored\n");
1206 }
1207 }
1208 #endif
1209
1210 #undef TestFPrintFormat
1211
1212 #if !UCONFIG_NO_FORMATTING
1213 static void TestFScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
1214 UFILE *myFile;
1215 UChar uBuffer[256];
1216 char buffer[256];
1217 char compBuffer[256];
1218 int32_t uNumScanned;
1219 int32_t cNumScanned;
1220
1221 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
1222 if (myFile == NULL) {
1223 log_err("Can't write test file for %s.\n", format);
1224 return;
1225 }
1226 /* Reinitialize the buffer to verify null termination works. */
1227 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
1228 uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
1229 memset(buffer, '*', sizeof(buffer)/sizeof(*buffer));
1230 buffer[sizeof(buffer)/sizeof(*buffer)-1] = 0;
1231
1232 u_fprintf(myFile, "%S", uValue);
1233 u_fclose(myFile);
1234 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
1235 uNumScanned = u_fscanf(myFile, format, uBuffer);
1236 u_fclose(myFile);
1237 if (expectedToPass) {
1238 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));
1239 cNumScanned = sscanf(cValue, format, buffer);
1240 if (strncmp(buffer, compBuffer, sizeof(buffer)/sizeof(*buffer)) != 0) {
1241 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
1242 }
1243 if (cNumScanned != uNumScanned) {
1244 log_err("%s number printed Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
1245 }
1246 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
1247 log_err("%s too much stored\n", format);
1248 }
1249 }
1250 else {
1251 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
1252 log_err("%s too much stored on a failure\n", format);
1253 }
1254 }
1255 }
1256 #endif
1257
1258 #if !UCONFIG_NO_FORMATTING
1259 static void TestFScanset(void) {
1260 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1261 static const char abcChars[] = "abccdefg";
1262
1263 TestFScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
1264 TestFScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
1265
1266 TestFScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
1267 TestFScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
1268
1269 TestFScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
1270 TestFScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
1271
1272 TestFScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
1273 TestFScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
1274
1275 TestFScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
1276 TestFScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
1277 TestFScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
1278 TestFScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
1279 TestFScanSetFormat("%[^e] ", abcUChars, abcChars, TRUE);
1280
1281 TestFScanSetFormat("%1[ab] ", abcUChars, abcChars, TRUE);
1282 TestFScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
1283
1284 TestFScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
1285
1286 /* Extra long string for testing */
1287 TestFScanSetFormat(" %[qrst]",
1288 abcUChars, abcChars, TRUE);
1289
1290 TestFScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
1291
1292 /* Bad format */
1293 TestFScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
1294 TestFScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
1295 TestFScanSetFormat("%[a", abcUChars, abcChars, FALSE);
1296 /* The following is not deterministic on Windows */
1297 /* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1298
1299 /* TODO: Need to specify precision with a "*" */
1300 }
1301 #endif
1302 #if !UCONFIG_NO_FORMATTING
1303 static void TestBadFScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
1304 UFILE *myFile;
1305 UChar uBuffer[256];
1306 int32_t uNumScanned;
1307
1308 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
1309 if (myFile == NULL) {
1310 log_err("Can't write test file for %s.\n", format);
1311 return;
1312 }
1313 /* Reinitialize the buffer to verify null termination works. */
1314 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));
1315 uBuffer[sizeof(uBuffer)/sizeof(*uBuffer)-1] = 0;
1316
1317 u_fprintf(myFile, "%S", uValue);
1318 u_fclose(myFile);
1319 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
1320 uNumScanned = u_fscanf(myFile, format, uBuffer);
1321 u_fclose(myFile);
1322 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
1323 log_err("%s too much stored on a failure\n", format);
1324 }
1325 }
1326 #endif
1327 #if !UCONFIG_NO_FORMATTING
1328 static void TestBadScanfFormat(void) {
1329 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1330 static const char abcChars[] = "abccdefg";
1331
1332 TestBadFScanfFormat("%[] ", abcUChars, abcChars);
1333 }
1334 #endif
1335 #if !UCONFIG_NO_FORMATTING
1336 static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
1337 UChar uBuffer[256];
1338 UChar uBuffer2[256];
1339 va_list ap;
1340 int32_t count;
1341 UFILE *myFile;
1342
1343 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "UTF-8");
1344 if (!myFile) {
1345 log_err("Test file can't be opened\n");
1346 return;
1347 }
1348
1349 va_start(ap, format);
1350 count = u_vfprintf(myFile, format, ap);
1351 va_end(ap);
1352
1353 u_fclose(myFile);
1354
1355
1356 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", "UTF-8");
1357 if (!myFile) {
1358 log_err("Test file can't be opened\n");
1359 return;
1360 }
1361 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
1362 u_uastrcpy(uBuffer2, expectedResult);
1363 if (u_strcmp(uBuffer, uBuffer2) != 0) {
1364 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
1365 }
1366 u_fclose(myFile);
1367
1368
1369 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
1370 if (!myFile) {
1371 log_err("Test file can't be opened\n");
1372 return;
1373 }
1374 u_uastrcpy(uBuffer, format);
1375
1376 va_start(ap, format);
1377 count = u_vfprintf_u(myFile, uBuffer, ap);
1378 va_end(ap);
1379
1380 u_fclose(myFile);
1381
1382
1383 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);
1384 if (!myFile) {
1385 log_err("Test file can't be opened\n");
1386 return;
1387 }
1388 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(*uBuffer), myFile);
1389 u_uastrcpy(uBuffer2, expectedResult);
1390 if (u_strcmp(uBuffer, uBuffer2) != 0) {
1391 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
1392 }
1393 u_fclose(myFile);
1394 }
1395
1396 static void TestVargs(void) {
1397 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1398 }
1399 #endif
1400
1401 static void TestUnicodeFormat(void)
1402 {
1403 #if !UCONFIG_NO_FORMATTING
1404 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1405 UChar myUString[256];
1406 UFILE *myFile;
1407 static const UChar TEST_STR[] = { 0x03BC, 0x0025, 0x0024, 0};
1408 static const UChar PERCENT_S[] = { 0x03BC, 0x0025, 0x0053, 0};
1409
1410 u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
1411
1412 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
1413 if (!myFile) {
1414 log_err("Test file can't be opened\n");
1415 return;
1416 }
1417 u_fprintf_u(myFile, PERCENT_S, TEST_STR);
1418 u_fclose(myFile);
1419
1420 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
1421 if (!myFile) {
1422 log_err("Test file can't be opened\n");
1423 return;
1424 }
1425 u_fscanf_u(myFile, PERCENT_S, myUString);
1426 u_fclose(myFile);
1427 if (u_strcmp(TEST_STR, myUString) != 0) {
1428 log_err("u_fscanf_u doesn't work.\n");
1429 }
1430 #endif
1431 }
1432
1433
1434 U_CFUNC void
1435 addFileTest(TestNode** root) {
1436 #if !UCONFIG_NO_FORMATTING
1437 addTest(root, &TestFile, "file/TestFile");
1438 #endif
1439 addTest(root, &StdinBuffering, "file/StdinBuffering");
1440 addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers");
1441 addTest(root, &TestFileReadBuffering, "file/TestFileReadBuffering");
1442 addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount");
1443 addTest(root, &TestfgetsNewLineHandling, "file/TestfgetsNewLineHandling");
1444 addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount");
1445 addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering");
1446 addTest(root, &TestCodepage, "file/TestCodepage");
1447 #if !UCONFIG_NO_FORMATTING
1448 addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale");
1449 addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
1450 addTest(root, &TestFScanset, "file/TestFScanset");
1451 addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
1452 addTest(root, &TestBadScanfFormat, "file/TestBadScanfFormat");
1453 addTest(root, &TestVargs, "file/TestVargs");
1454 addTest(root, &TestUnicodeFormat, "file/TestUnicodeFormat");
1455 #endif
1456 }