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