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