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