]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/iotest/filetst.c
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / iotest / filetst.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955 3/*
46f4442e 4 **********************************************************************
2ca993e8 5 * Copyright (C) 2004-2016, International Business Machines
46f4442e
A
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * file name: filetst.c
f3c0d7a5 9 * encoding: UTF-8
46f4442e
A
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2004apr06
14 * created by: George Rhoten
15 */
374ca955 16
2ca993e8 17#include "cmemory.h"
374ca955
A
18#include "iotest.h"
19#include "unicode/ustdio.h"
20#include "unicode/ustring.h"
21#include "unicode/uloc.h"
374ca955
A
22
23#include <string.h>
729e4ab9 24#include <stdlib.h>
374ca955 25
46f4442e 26const char *STANDARD_TEST_FILE = "iotest-c.txt";
374ca955 27
57a6839d
A
28const char *STANDARD_TEST_LOCALE = "en_US_POSIX";
29
374ca955
A
30
31#if !UCONFIG_NO_FORMATTING
32static 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);
2ca993e8
A
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));
374ca955
A
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 */
73c04bcf
A
62 union {
63 int32_t arr[4];
64 void *ptr;
65 } massiveBigEndianPtr = {{ 0x10002000, 0x30004000, 0x50006000, 0x70008000 }};
66 origPtr = massiveBigEndianPtr.ptr;
374ca955
A
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;
73c04bcf
A
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. */
374ca955
A
104
105 u_fclose(myFile);
57a6839d 106 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);
374ca955
A
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;
73c04bcf 175 u_fscanf(myFile, "Float %%f: %lf\n", newDoubleValuePtr);
374ca955
A
176 if (myFloat != *newDoubleValuePtr) {
177 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
178 }
179 *newDoubleValuePtr = -1.0;
73c04bcf 180 u_fscanf(myFile, "Lowercase float %%e: %le\n", newDoubleValuePtr);
374ca955
A
181 if (myFloat != *newDoubleValuePtr) {
182 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
183 }
184 *newDoubleValuePtr = -1.0;
73c04bcf 185 u_fscanf(myFile, "Uppercase float %%E: %lE\n", newDoubleValuePtr);
374ca955
A
186 if (myFloat != *newDoubleValuePtr) {
187 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
188 }
189 *newDoubleValuePtr = -1.0;
73c04bcf 190 u_fscanf(myFile, "Lowercase float %%g: %lg\n", newDoubleValuePtr);
374ca955
A
191 if (myFloat != *newDoubleValuePtr) {
192 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
193 }
194 *newDoubleValuePtr = -1.0;
73c04bcf 195 u_fscanf(myFile, "Uppercase float %%G: %lG\n", newDoubleValuePtr);
374ca955
A
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);
2ca993e8 221 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
374ca955
A
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);
2ca993e8 226 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
374ca955
A
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);
46f4442e
A
242 myString[2] = '!';
243 myString[3] = '!';
2ca993e8 244 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 245 if (strcmp(myString, "\t\n") != 0) {
374ca955
A
246 log_err("u_fgets got \"%s\"\n", myString);
247 }
248
2ca993e8 249 if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) {
374ca955
A
250 log_err("u_fgets did not return myUString\n");
251 }
2ca993e8 252 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 253 if (strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
374ca955
A
254 log_err("u_fgets got \"%s\"\n", myString);
255 }
256
2ca993e8 257 if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) {
374ca955
A
258 log_err("u_fgets did not return myUString\n");
259 }
2ca993e8 260 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 261 if (strcmp(myString, "Pointer to integer Value: 37\n") != 0) {
374ca955
A
262 log_err("u_fgets got \"%s\"\n", myString);
263 }
264
2ca993e8 265 if (u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile) != myUString) {
374ca955
A
266 log_err("u_fgets did not return myUString\n");
267 }
2ca993e8 268 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 269 if (strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
374ca955
A
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 }
2ca993e8 280 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 281 if (strcmp(myString, "") != 0) {
374ca955
A
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 }
2ca993e8 288 u_austrncpy(myString, myUString, UPRV_LENGTHOF(myUString));
b331163b 289 if (strcmp(myString, "\t") != 0) {
374ca955
A
290 log_err("u_fgets got \"%s\"\n", myString);
291 }
292
2ca993e8
A
293 u_austrncpy(myString, u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile),
294 UPRV_LENGTHOF(myUString));
374ca955
A
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 }
2ca993e8
A
302 u_austrncpy(myString, u_fgets(myUString, UPRV_LENGTHOF(myUString), myFile),
303 UPRV_LENGTHOF(myUString));
374ca955
A
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
317static void TestFile(void) {
374ca955
A
318
319 log_verbose("Testing u_fopen\n");
57a6839d
A
320 TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL));
321}
322
323static void TestFinit(void) {
324 FILE *standardFile;
374ca955 325
374ca955 326 log_verbose("Testing u_finit\n");
57a6839d
A
327 standardFile = fopen(STANDARD_TEST_FILE, "w");
328 TestFileFromICU(u_finit(standardFile, STANDARD_TEST_LOCALE, NULL));
374ca955 329 fclose(standardFile);
57a6839d
A
330}
331
332static void TestFadopt(void) {
333 FILE *standardFile;
729e4ab9
A
334
335 log_verbose("Testing u_fadopt\n");
57a6839d
A
336 standardFile = fopen(STANDARD_TEST_FILE, "w");
337 TestFileFromICU(u_fadopt(standardFile, STANDARD_TEST_LOCALE, NULL));
374ca955
A
338}
339#endif
340
341static 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;
2ca993e8 354 u_fgets(buff, UPRV_LENGTHOF(buff), uStdIn);
374ca955
A
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
365static 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
440static 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);
73c04bcf
A
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 }
374ca955 461 u_fputc(0xFF41, myFile);
2ca993e8
A
462 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
463 u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer));
374ca955
A
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
2ca993e8 471 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
374ca955
A
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 }
2ca993e8 479 if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != buffer) {
374ca955
A
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);
2ca993e8
A
504 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
505 u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer));
374ca955
A
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
2ca993e8 513 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
374ca955
A
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 }
2ca993e8 524 if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != buffer) {
374ca955
A
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);
2ca993e8
A
546 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
547 u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer));
374ca955
A
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
2ca993e8 555 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
374ca955
A
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
575static 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
2ca993e8 581 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
374ca955 582 for (repetitions = 0; repetitions < 2; repetitions++) {
2ca993e8 583 u_file_write(buffer, UPRV_LENGTHOF(buffer), myFile);
374ca955
A
584 }
585
586 u_fclose(myFile);
2ca993e8 587 u_memset(buffer, 0xDEAD, UPRV_LENGTHOF(buffer));
374ca955
A
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
596static 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 }
2ca993e8 611 u_memset(expectedBuffer, 0, UPRV_LENGTHOF(expectedBuffer));
374ca955
A
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
2ca993e8
A
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);
374ca955
A
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
656static 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 }
2ca993e8 698 for (lineIdx = 0; lineIdx < UPRV_LENGTHOF(testUStr); lineIdx++) {
374ca955
A
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
2ca993e8 705 for (lineIdx = 0; lineIdx < UPRV_LENGTHOF(testUStr); lineIdx++) {
374ca955
A
706 UChar *returnedUCharBuffer;
707
2ca993e8
A
708 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
709 returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile);
374ca955
A
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 }
2ca993e8 722 if (lineIdx != UPRV_LENGTHOF(testUStr)) {
374ca955
A
723 log_err("u_fgets read too much\n");
724 }
2ca993e8 725 if (u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile) != NULL) {
374ca955
A
726 log_err("u_file_write wrote too much\n");
727 }
728 u_fclose(myFile);
729}
730
73c04bcf
A
731static void TestLineCount(const char *prefixLine, const char *line, int32_t numRepititions) {
732 UChar buffer[64];
733 UChar expectedBuffer[64];
3d1f044b 734 int32_t lineLen = (int32_t)strlen(line);
73c04bcf
A
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);
2ca993e8 759 returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile);
73c04bcf
A
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++) {
2ca993e8
A
767 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
768 returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer), myFile);
73c04bcf
A
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
787static 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
797static 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 }
2ca993e8 808 u_memset(buffer, 0xBEEF, UPRV_LENGTHOF(buffer));
73c04bcf
A
809
810 /* Write one very long line */
2ca993e8 811 for (repetitions = 0; repetitions < (UPRV_LENGTHOF(buffer)*2); repetitions++) {
73c04bcf
A
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 */
2ca993e8
A
823 returnedUCharBuffer = u_fgets(buffer, UPRV_LENGTHOF(buffer)-1, myFile);
824 if (u_strlen(returnedUCharBuffer) != (UPRV_LENGTHOF(buffer)-2)) {
73c04bcf 825 log_err("Line is wrong length. Got %d. Expected %d.\n",
2ca993e8 826 u_strlen(returnedUCharBuffer), (UPRV_LENGTHOF(buffer)-2));
73c04bcf
A
827 }
828 /* We better not read too much */
2ca993e8 829 if (buffer[UPRV_LENGTHOF(buffer)-1] != 0xBEEF) {
73c04bcf
A
830 log_err("Too much data was written\n");
831 }
832
833 u_fclose(myFile);
834}
835
374ca955
A
836
837static 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};
2ca993e8 841 UChar testBuf[UPRV_LENGTHOF(strABAccentA)*2]; /* *2 to see if too much was */
374ca955
A
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
729e4ab9 921static void TestCodepageFlush(void) {
4388f060
A
922#if UCONFIG_NO_LEGACY_CONVERSION || UCONFIG_NO_FORMATTING
923 log_verbose("Skipping, legacy conversion or formatting is disabled.");
729e4ab9
A
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 */
57a6839d 929 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", STANDARD_TEST_LOCALE, enc);
729e4ab9
A
930 FILE *myCFile;
931 int shift = 0;
4388f060 932 int32_t i;
729e4ab9
A
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 */
4388f060 958 for(i=0;i<(int32_t)inLen;i++) {
729e4ab9
A
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
374ca955
A
984#if !UCONFIG_NO_FORMATTING
985static void TestFilePrintCompatibility(void) {
57a6839d 986 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", STANDARD_TEST_LOCALE, NULL);
374ca955
A
987 FILE *myCFile;
988 int32_t num;
989 char cVal;
990 static const UChar emptyStr[] = {0};
991 char readBuf[512] = "";
992 char testBuf[512] = "";
57a6839d 993 int32_t n = 0;
374ca955
A
994
995 if (myFile == NULL) {
996 log_err("Can't write test file.\n");
997 return;
998 }
999#if !UCONFIG_NO_FORMATTING
57a6839d 1000 if (strcmp(u_fgetlocale(myFile), STANDARD_TEST_LOCALE) != 0) {
374ca955
A
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++) {
57a6839d
A
1034 /* Note: gcc on Ubuntu complains if return value of scanf is ignored. */
1035 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1041 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1047 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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 */
57a6839d 1054 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1060 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1066 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1084 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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
57a6839d 1090 n += fscanf(myCFile, "%s", readBuf);
374ca955
A
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++) {
57a6839d 1099 n += fscanf(myCFile, "%c", &cVal);
374ca955
A
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++) {
57a6839d 1105 n += fscanf(myCFile, "%c", &cVal);
374ca955
A
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;
57a6839d 1113 n += fscanf(myCFile, "%c", &cVal);
374ca955
A
1114 if (num != cVal) {
1115 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
1116 }
1117 }
57a6839d 1118 (void)n;
374ca955
A
1119 fclose(myCFile);
1120}
1121#endif
1122
340931cb 1123#define TestFPrintFormat(uFormat, uValue, cFormat, cValue) UPRV_BLOCK_MACRO_BEGIN { \
57a6839d 1124 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);\
374ca955
A
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. */\
2ca993e8
A
1130 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));\
1131 memset(buffer, '*', UPRV_LENGTHOF(buffer));\
374ca955
A
1132 \
1133 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
1134 u_fclose(myFile);\
57a6839d 1135 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);\
2ca993e8 1136 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);\
374ca955 1137 u_fclose(myFile);\
2ca993e8 1138 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));\
374ca955
A
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 }\
340931cb 1149} UPRV_BLOCK_MACRO_END
374ca955
A
1150
1151#if !UCONFIG_NO_FORMATTING
1152static void TestFprintfFormat(void) {
1153 static const UChar abcUChars[] = {0x61,0x62,0x63,0};
1154 static const char abcChars[] = "abc";
1155 UChar uBuffer[256];
1156 char buffer[256];
1157 char compBuffer[256];
1158 int32_t uNumPrinted;
1159 int32_t cNumPrinted;
1160 UFILE *myFile;
1161
1162 TestFPrintFormat("%8S", abcUChars, "%8s", abcChars);
1163 TestFPrintFormat("%-8S", abcUChars, "%-8s", abcChars);
1164 TestFPrintFormat("%.2S", abcUChars, "%.2s", abcChars); /* strlen is 3 */
1165
1166 TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
1167 TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
1168 TestFPrintFormat("%.2s", abcChars, "%.2s", abcChars); /* strlen is 3 */
1169
1170 TestFPrintFormat("%8c", (char)'e', "%8c", (char)'e');
1171 TestFPrintFormat("%-8c", (char)'e', "%-8c", (char)'e');
1172
1173 TestFPrintFormat("%8C", (UChar)0x65, "%8c", (char)'e');
1174 TestFPrintFormat("%-8C", (UChar)0x65, "%-8c", (char)'e');
1175
1176 TestFPrintFormat("%f", 1.23456789, "%f", 1.23456789);
1177 TestFPrintFormat("%f", 12345.6789, "%f", 12345.6789);
1178 TestFPrintFormat("%f", 123456.789, "%f", 123456.789);
1179 TestFPrintFormat("%f", 1234567.89, "%f", 1234567.89);
1180 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1181 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1182 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1183 TestFPrintFormat("%10.4f", 123.456789, "%10.4f", 123.456789);
1184 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1185
1186/* TestFPrintFormat("%g", 12345.6789, "%g", 12345.6789);
1187 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
1188 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1189 TestFPrintFormat("%G", 123456.789, "%G", 123456.789);
1190 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);*/
1191 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1192 TestFPrintFormat("%10.4g", 1.23456789, "%10.4g", 1.23456789);
1193 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1194 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1195 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1196
1197 TestFPrintFormat("%8x", 123456, "%8x", 123456);
1198 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
1199 TestFPrintFormat("%08x", 123456, "%08x", 123456);
1200
1201 TestFPrintFormat("%8X", 123456, "%8X", 123456);
1202 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
1203 TestFPrintFormat("%08X", 123456, "%08X", 123456);
1204 TestFPrintFormat("%#x", 123456, "%#x", 123456);
1205 TestFPrintFormat("%#x", -123456, "%#x", -123456);
1206
1207 TestFPrintFormat("%8o", 123456, "%8o", 123456);
1208 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
1209 TestFPrintFormat("%08o", 123456, "%08o", 123456);
1210 TestFPrintFormat("%#o", 123, "%#o", 123);
1211 TestFPrintFormat("%#o", -123, "%#o", -123);
1212
1213 TestFPrintFormat("%8u", 123456, "%8u", 123456);
1214 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
1215 TestFPrintFormat("%08u", 123456, "%08u", 123456);
1216 TestFPrintFormat("%8u", -123456, "%8u", -123456);
1217 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
1218 TestFPrintFormat("%.5u", 123456, "%.5u", 123456);
1219 TestFPrintFormat("%.6u", 123456, "%.6u", 123456);
1220 TestFPrintFormat("%.7u", 123456, "%.7u", 123456);
1221
1222 TestFPrintFormat("%8d", 123456, "%8d", 123456);
1223 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
1224 TestFPrintFormat("%08d", 123456, "%08d", 123456);
1225 TestFPrintFormat("% d", 123456, "% d", 123456);
1226 TestFPrintFormat("% d", -123456, "% d", -123456);
1227
1228 TestFPrintFormat("%8i", 123456, "%8i", 123456);
1229 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
1230 TestFPrintFormat("%08i", 123456, "%08i", 123456);
1231
1232 log_verbose("Get really crazy with the formatting.\n");
1233
1234 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
1235 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
1236 TestFPrintFormat("%#12x", 123, "%#12x", 123);
1237 TestFPrintFormat("%#12x", -123, "%#12x", -123);
1238
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 TestFPrintFormat("%.12d", -123, "%.12d", -123);
1251
1252 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1253 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1254 TestFPrintFormat("%- 12.10f", 1.234, "%- 12.10f", 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("%12.1f", -1.234, "%12.1f", -1.234);
1262 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1263 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1264 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
1265 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
1266
57a6839d 1267 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);
374ca955 1268 /* Reinitialize the buffer to verify null termination works. */
2ca993e8
A
1269 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));
1270 memset(buffer, '*', UPRV_LENGTHOF(buffer));
374ca955
A
1271
1272 uNumPrinted = u_fprintf(myFile, "%d % d %d", -1234, 1234, 1234);
1273 u_fclose(myFile);
57a6839d 1274 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);
2ca993e8 1275 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);
374ca955 1276 u_fclose(myFile);
2ca993e8 1277 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));
374ca955
A
1278 cNumPrinted = sprintf(buffer, "%d % d %d", -1234, 1234, 1234);
1279 if (strcmp(buffer, compBuffer) != 0) {
1280 log_err("%%d %% d %%d Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);
1281 }
1282 if (cNumPrinted != uNumPrinted) {
1283 log_err("%%d %% d %%d number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);
1284 }
1285 if (buffer[uNumPrinted+1] != '*') {
1286 log_err("%%d %% d %%d too much stored\n");
1287 }
1288}
1289#endif
1290
1291#undef TestFPrintFormat
1292
1293#if !UCONFIG_NO_FORMATTING
1294static void TestFScanSetFormat(const char *format, const UChar *uValue, const char *cValue, UBool expectedToPass) {
1295 UFILE *myFile;
1296 UChar uBuffer[256];
1297 char buffer[256];
1298 char compBuffer[256];
1299 int32_t uNumScanned;
1300 int32_t cNumScanned;
1301
1302 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
1303 if (myFile == NULL) {
1304 log_err("Can't write test file for %s.\n", format);
1305 return;
1306 }
1307 /* Reinitialize the buffer to verify null termination works. */
2ca993e8
A
1308 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));
1309 uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0;
1310 memset(buffer, '*', UPRV_LENGTHOF(buffer));
1311 buffer[UPRV_LENGTHOF(buffer)-1] = 0;
374ca955
A
1312
1313 u_fprintf(myFile, "%S", uValue);
1314 u_fclose(myFile);
57a6839d 1315 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);
374ca955
A
1316 uNumScanned = u_fscanf(myFile, format, uBuffer);
1317 u_fclose(myFile);
1318 if (expectedToPass) {
2ca993e8 1319 u_austrncpy(compBuffer, uBuffer, UPRV_LENGTHOF(uBuffer));
374ca955 1320 cNumScanned = sscanf(cValue, format, buffer);
2ca993e8 1321 if (strncmp(buffer, compBuffer, UPRV_LENGTHOF(buffer)) != 0) {
374ca955
A
1322 log_err("%s Got: \"%s\", Expected: \"%s\"\n", format, compBuffer, buffer);
1323 }
1324 if (cNumScanned != uNumScanned) {
1325 log_err("%s number printed Got: %d, Expected: %d\n", format, uNumScanned, cNumScanned);
1326 }
1327 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {
1328 log_err("%s too much stored\n", format);
1329 }
1330 }
1331 else {
1332 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
1333 log_err("%s too much stored on a failure\n", format);
1334 }
1335 }
1336}
1337#endif
1338
1339#if !UCONFIG_NO_FORMATTING
1340static void TestFScanset(void) {
1341 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1342 static const char abcChars[] = "abccdefg";
1343
1344 TestFScanSetFormat("%[bc]S", abcUChars, abcChars, TRUE);
1345 TestFScanSetFormat("%[cb]S", abcUChars, abcChars, TRUE);
1346
1347 TestFScanSetFormat("%[ab]S", abcUChars, abcChars, TRUE);
1348 TestFScanSetFormat("%[ba]S", abcUChars, abcChars, TRUE);
1349
1350 TestFScanSetFormat("%[ab]", abcUChars, abcChars, TRUE);
1351 TestFScanSetFormat("%[ba]", abcUChars, abcChars, TRUE);
1352
1353 TestFScanSetFormat("%[abcdefgh]", abcUChars, abcChars, TRUE);
1354 TestFScanSetFormat("%[;hgfedcba]", abcUChars, abcChars, TRUE);
1355
1356 TestFScanSetFormat("%[^a]", abcUChars, abcChars, TRUE);
1357 TestFScanSetFormat("%[^e]", abcUChars, abcChars, TRUE);
1358 TestFScanSetFormat("%[^ed]", abcUChars, abcChars, TRUE);
1359 TestFScanSetFormat("%[^dc]", abcUChars, abcChars, TRUE);
1360 TestFScanSetFormat("%[^e] ", abcUChars, abcChars, TRUE);
1361
1362 TestFScanSetFormat("%1[ab] ", abcUChars, abcChars, TRUE);
1363 TestFScanSetFormat("%2[^f]", abcUChars, abcChars, TRUE);
1364
1365 TestFScanSetFormat("%[qrst]", abcUChars, abcChars, TRUE);
1366
1367 /* Extra long string for testing */
1368 TestFScanSetFormat(" %[qrst]",
1369 abcUChars, abcChars, TRUE);
1370
1371 TestFScanSetFormat("%[a-]", abcUChars, abcChars, TRUE);
1372
1373 /* Bad format */
1374 TestFScanSetFormat("%[f-a]", abcUChars, abcChars, FALSE);
1375 TestFScanSetFormat("%[c-a]", abcUChars, abcChars, FALSE);
1376 TestFScanSetFormat("%[a", abcUChars, abcChars, FALSE);
1377 /* The following is not deterministic on Windows */
1378/* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1379
1380 /* TODO: Need to specify precision with a "*" */
1381}
1382#endif
1383#if !UCONFIG_NO_FORMATTING
1384static void TestBadFScanfFormat(const char *format, const UChar *uValue, const char *cValue) {
340931cb 1385 (void)cValue; // suppress compiler warnings about unused variable
374ca955
A
1386 UFILE *myFile;
1387 UChar uBuffer[256];
1388 int32_t uNumScanned;
1389
1390 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
1391 if (myFile == NULL) {
1392 log_err("Can't write test file for %s.\n", format);
1393 return;
1394 }
1395 /* Reinitialize the buffer to verify null termination works. */
2ca993e8
A
1396 u_memset(uBuffer, 0x2a, UPRV_LENGTHOF(uBuffer));
1397 uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0;
374ca955
A
1398
1399 u_fprintf(myFile, "%S", uValue);
1400 u_fclose(myFile);
57a6839d 1401 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);
374ca955
A
1402 uNumScanned = u_fscanf(myFile, format, uBuffer);
1403 u_fclose(myFile);
1404 if (uNumScanned != 0 || uBuffer[0] != 0x2a || uBuffer[1] != 0x2a) {
1405 log_err("%s too much stored on a failure\n", format);
1406 }
1407}
1408#endif
1409#if !UCONFIG_NO_FORMATTING
1410static void TestBadScanfFormat(void) {
1411 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1412 static const char abcChars[] = "abccdefg";
1413
1414 TestBadFScanfFormat("%[] ", abcUChars, abcChars);
1415}
1416#endif
1417#if !UCONFIG_NO_FORMATTING
1418static void Test_u_vfprintf(const char *expectedResult, const char *format, ...) {
1419 UChar uBuffer[256];
1420 UChar uBuffer2[256];
1421 va_list ap;
1422 int32_t count;
1423 UFILE *myFile;
1424
57a6839d 1425 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, "UTF-8");
374ca955
A
1426 if (!myFile) {
1427 log_err("Test file can't be opened\n");
1428 return;
1429 }
1430
1431 va_start(ap, format);
1432 count = u_vfprintf(myFile, format, ap);
57a6839d 1433 (void)count; /* Suppress set but not used warning. */
374ca955
A
1434 va_end(ap);
1435
1436 u_fclose(myFile);
1437
1438
57a6839d 1439 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, "UTF-8");
374ca955
A
1440 if (!myFile) {
1441 log_err("Test file can't be opened\n");
1442 return;
1443 }
2ca993e8 1444 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);
374ca955
A
1445 u_uastrcpy(uBuffer2, expectedResult);
1446 if (u_strcmp(uBuffer, uBuffer2) != 0) {
1447 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
1448 }
1449 u_fclose(myFile);
1450
1451
57a6839d 1452 myFile = u_fopen(STANDARD_TEST_FILE, "w", STANDARD_TEST_LOCALE, NULL);
374ca955
A
1453 if (!myFile) {
1454 log_err("Test file can't be opened\n");
1455 return;
1456 }
1457 u_uastrcpy(uBuffer, format);
1458
1459 va_start(ap, format);
1460 count = u_vfprintf_u(myFile, uBuffer, ap);
1461 va_end(ap);
1462
1463 u_fclose(myFile);
1464
1465
57a6839d 1466 myFile = u_fopen(STANDARD_TEST_FILE, "r", STANDARD_TEST_LOCALE, NULL);
374ca955
A
1467 if (!myFile) {
1468 log_err("Test file can't be opened\n");
1469 return;
1470 }
2ca993e8 1471 u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), myFile);
374ca955
A
1472 u_uastrcpy(uBuffer2, expectedResult);
1473 if (u_strcmp(uBuffer, uBuffer2) != 0) {
1474 log_err("Got two different results for \"%s\" expected \"%s\"\n", format, expectedResult);
1475 }
1476 u_fclose(myFile);
1477}
1478
1479static void TestVargs(void) {
1480 Test_u_vfprintf("8 9 a B 8.9", "%d %u %x %X %.1f", 8, 9, 10, 11, 8.9);
1481}
1482#endif
374ca955 1483
73c04bcf 1484static void TestUnicodeFormat(void)
374ca955
A
1485{
1486#if !UCONFIG_NO_FORMATTING
73c04bcf
A
1487 /* Make sure that invariant conversion doesn't happen on the _u formats. */
1488 UChar myUString[256];
1489 UFILE *myFile;
1490 static const UChar TEST_STR[] = { 0x03BC, 0x0025, 0x0024, 0};
1491 static const UChar PERCENT_S[] = { 0x03BC, 0x0025, 0x0053, 0};
374ca955 1492
2ca993e8 1493 u_memset(myUString, 0x2a, UPRV_LENGTHOF(myUString));
374ca955 1494
73c04bcf
A
1495 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
1496 if (!myFile) {
1497 log_err("Test file can't be opened\n");
374ca955
A
1498 return;
1499 }
73c04bcf
A
1500 u_fprintf_u(myFile, PERCENT_S, TEST_STR);
1501 u_fclose(myFile);
374ca955 1502
73c04bcf
A
1503 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
1504 if (!myFile) {
1505 log_err("Test file can't be opened\n");
374ca955
A
1506 return;
1507 }
73c04bcf
A
1508 u_fscanf_u(myFile, PERCENT_S, myUString);
1509 u_fclose(myFile);
1510 if (u_strcmp(TEST_STR, myUString) != 0) {
1511 log_err("u_fscanf_u doesn't work.\n");
374ca955 1512 }
374ca955
A
1513#endif
1514}
1515
729e4ab9
A
1516static void TestFileWriteRetval(const char * a_pszEncoding) {
1517 UChar * buffer;
1518 UFILE * myFile;
1519 int32_t count;
1520 int32_t expected = 10000; /* test with large data to test internal buffer looping */
1521 UChar testChar = 0xBEEF;
1522
1523 if (!*a_pszEncoding || 0 == strcmp(a_pszEncoding, "ASCII")) {
1524 testChar = 0x65; /* 'A' - otherwise read test will fail */
1525 }
1526
1527 buffer = (UChar*) malloc(expected * sizeof(UChar));
1528 if (!buffer) {
1529 log_err("Out of memory\n");
1530 return;
1531 }
1532
1533 /* write */
1534 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, a_pszEncoding);
1535 if (!myFile) {
1536 free(buffer);
1537 log_err("Test file can't be opened for write\n");
1538 return;
1539 }
1540 u_memset(buffer, testChar, expected);
1541 count = u_file_write(buffer, expected, myFile);
1542 u_fclose(myFile);
1543 if (count != expected) {
1544 free(buffer);
1545 log_err("u_file_write returned incorrect number of characters written\n");
1546 return;
1547 }
1548
1549 free(buffer);
1550 buffer = NULL;
1551
1552 /* read */
1553 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, a_pszEncoding);
1554 if (!myFile) {
1555 log_err("Test file can't be opened for read\n");
1556 return;
1557 }
1558 for (count = 0; count < expected; ++count) {
1559 UChar gotChar = u_fgetc(myFile);
1560 if(gotChar != testChar) {
1561 log_err("u_fgetc returned unexpected character U+%04X expected U+%04X\n", gotChar, testChar);
1562 u_fclose(myFile);
1563 return;
1564 }
1565 }
1566 if (u_fgetc(myFile) != U_EOF) {
1567 log_err("u_fgetc did not return expected EOF\n");
1568 u_fclose(myFile);
1569 return;
1570 }
1571 u_fclose(myFile);
1572}
1573
1574static void TestFileWriteRetvalUTF16(void) {
1575 TestFileWriteRetval("UTF-16");
1576}
1577
1578static void TestFileWriteRetvalUTF8(void) {
1579 TestFileWriteRetval("UTF-8");
1580}
1581
1582static void TestFileWriteRetvalASCII(void) {
1583 TestFileWriteRetval("ASCII");
1584}
1585
1586static void TestFileWriteRetvalNONE(void) {
1587 TestFileWriteRetval("");
1588}
73c04bcf 1589
374ca955
A
1590U_CFUNC void
1591addFileTest(TestNode** root) {
1592#if !UCONFIG_NO_FORMATTING
1593 addTest(root, &TestFile, "file/TestFile");
57a6839d
A
1594 addTest(root, &TestFinit, "file/TestFinit");
1595 addTest(root, &TestFadopt, "file/TestFadopt");
374ca955
A
1596#endif
1597 addTest(root, &StdinBuffering, "file/StdinBuffering");
1598 addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers");
1599 addTest(root, &TestFileReadBuffering, "file/TestFileReadBuffering");
1600 addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount");
1601 addTest(root, &TestfgetsNewLineHandling, "file/TestfgetsNewLineHandling");
73c04bcf
A
1602 addTest(root, &TestfgetsNewLineCount, "file/TestfgetsNewLineCount");
1603 addTest(root, &TestFgetsLineBuffering, "file/TestFgetsLineBuffering");
374ca955 1604 addTest(root, &TestCodepage, "file/TestCodepage");
729e4ab9
A
1605 addTest(root, &TestCodepageFlush, "file/TestCodepageFlush");
1606 addTest(root, &TestFileWriteRetvalUTF16, "file/TestFileWriteRetvalUTF16");
1607 addTest(root, &TestFileWriteRetvalUTF8, "file/TestFileWriteRetvalUTF8");
1608 addTest(root, &TestFileWriteRetvalASCII, "file/TestFileWriteRetvalASCII");
1609 addTest(root, &TestFileWriteRetvalNONE, "file/TestFileWriteRetvalNONE");
374ca955
A
1610#if !UCONFIG_NO_FORMATTING
1611 addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale");
1612 addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
1613 addTest(root, &TestFScanset, "file/TestFScanset");
1614 addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
1615 addTest(root, &TestBadScanfFormat, "file/TestBadScanfFormat");
1616 addTest(root, &TestVargs, "file/TestVargs");
73c04bcf 1617 addTest(root, &TestUnicodeFormat, "file/TestUnicodeFormat");
374ca955
A
1618#endif
1619}