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