]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/iotest/iotest.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / iotest / iotest.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (C) 2002-2003, International Business Machines
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* file name: iotest.cpp
7* encoding: US-ASCII
8* tab size: 8 (not used)
9* indentation:4
10*
11* created on: 2002feb21
12* created by: George Rhoten
13*/
14
15
16#include "unicode/ustdio.h"
17#include "unicode/ustream.h"
18#include "unicode/uclean.h"
19
20#include "unicode/ucnv.h"
21#include "unicode/uloc.h"
22#include "unicode/unistr.h"
23#include "unicode/ustring.h"
24#include "unicode/ctest.h"
25#include "unicode/utrans.h"
26#include "ustr_imp.h"
27
28#if U_IOSTREAM_SOURCE >= 199711
29#include <iostream>
30#include <strstream>
31using namespace std;
32#elif U_IOSTREAM_SOURCE >= 198506
33#include <iostream.h>
34#include <strstream.h>
35#endif
36
37#include <string.h>
38
39static char STANDARD_TEST_FILE[] = "iotest-c.txt";
40static const int32_t STANDARD_TEST_NUM_RANGE = 1000;
41
42#ifdef WIN32
43static const UChar NEW_LINE[] = {0x0d,0x0a,0};
44#define C_NEW_LINE "\r\n"
45#else
46static const UChar NEW_LINE[] = {0x0a,0};
47#define C_NEW_LINE "\n"
48#endif
49
50static void TestFileFromICU(UFILE *myFile) {
51 int32_t n[1];
52 float myFloat = -1234.0;
53 UDate myDate = 0.0;
54 UDate dec_31_1969 = -57600000.000000; /* TODO: These are not correct */
55 UDate midnight = 86400000.000000; /* TODO: These are not correct */
56 UDate myNewDate = -1.0;
57 int32_t newValuePtr[1];
58 double newDoubleValuePtr[1];
59 UChar myUString[256];
60 UChar uStringBuf[256];
61 char myString[256] = "";
62 char testBuf[256] = "";
63
64 u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
65 u_memset(uStringBuf, 0x2a, sizeof(uStringBuf)/sizeof(*uStringBuf));
66 memset(myString, 0x2a, sizeof(myString)/sizeof(*myString));
67 memset(testBuf, 0x2a, sizeof(testBuf)/sizeof(*testBuf));
68
69 if (myFile == NULL) {
70 log_err("Can't write test file.");
71 return;
72 }
73
74 *n = -1234;
75
76 /* Test fprintf */
77 u_fprintf(myFile, "Signed decimal integer %%d: %d\n", *n);
78 u_fprintf(myFile, "Signed decimal integer %%i: %i\n", *n);
79 u_fprintf(myFile, "Unsigned octal integer %%o: %o\n", *n);
80 u_fprintf(myFile, "Unsigned decimal integer %%u: %u\n", *n);
81 u_fprintf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n);
82 u_fprintf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n);
83 u_fprintf(myFile, "Float %%f: %f\n", myFloat);
84 u_fprintf(myFile, "Lowercase float %%e: %e\n", myFloat);
85 u_fprintf(myFile, "Uppercase float %%E: %E\n", myFloat);
86 u_fprintf(myFile, "Lowercase float %%g: %g\n", myFloat);
87 u_fprintf(myFile, "Uppercase float %%G: %G\n", myFloat);
88// u_fprintf(myFile, "Pointer %%p: %p\n", myFile);
89 u_fprintf(myFile, "Char %%c: %c\n", 'A');
90 u_fprintf(myFile, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K\n", L'A');
91 u_fprintf(myFile, "String %%s: %s\n", "My-String");
92 u_fprintf(myFile, "NULL String %%s: %s\n", NULL);
93 u_fprintf(myFile, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", L"My-String");
94 u_fprintf(myFile, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", NULL);
95 u_fprintf(myFile, "Date %%D (non-ANSI): %D\n", myDate);
96 u_fprintf(myFile, "Time %%T (non-ANSI): %T\n", myDate);
97 u_fprintf(myFile, "Percent %%P (non-ANSI): %P\n", myFloat);
98 u_fprintf(myFile, "Currency %%M (non-ANSI): %M\n", myFloat);
99 u_fprintf(myFile, "Spell Out %%V (non-ANSI): %V\n", myFloat);
100
101 *n = 1;
102 u_fprintf(myFile, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n, n, *n);
103 u_fprintf(myFile, "Pointer to integer Value: %d\n", *n);
104 u_fprintf(myFile, "This is a long test123456789012345678901234567890123456789012345678901234567890\n");
105 *n = 1;
106 fprintf(u_fgetfile(myFile), "\tNormal fprintf count: n=%d %n n=%d\n", *n ,n, *n);
107 fprintf(u_fgetfile(myFile), "\tNormal fprintf count value: n=%d\n", *n);
108
109 u_fclose(myFile);
110 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
111
112 if (myFile == NULL) {
113 log_err("Can't read test file.");
114 return;
115 }
116
117 *n = -1234;
118
119 myString[0] = u_fgetc(myFile);
120 if (myString[0] != 0x53 /* S */) {
121 log_err("u_fgetc 1 returned %X. Expected 'S'.", myString[0]);
122 }
123 u_fungetc(myString[0], myFile);
124 myString[0] = u_fgetc(myFile);
125 if (myString[0] != 0x53 /* S */) {
126 log_err("u_fgetc 2 returned %X. Expected 'S'.", myString[0]);
127 }
128 u_fungetc(myString[0], myFile);
129 myString[0] = u_fgetc(myFile);
130 if (myString[0] != 0x53 /* S */) {
131 log_err("u_fgetc 3 returned %X. Expected 'S'.", myString[0]);
132 }
133 u_fungetc(myString[0], myFile);
134
135 *newValuePtr = 1;
136 u_fscanf(myFile, "Signed decimal integer %%d: %d\n", newValuePtr);
137 if (*n != *newValuePtr) {
138 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
139 }
140 *newValuePtr = 1;
141 u_fscanf(myFile, "Signed decimal integer %%i: %i\n", newValuePtr);
142 if (*n != *newValuePtr) {
143 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
144 }
145 *newValuePtr = 1;
146 u_fscanf(myFile, "Unsigned octal integer %%o: %o\n", newValuePtr);
147 if (*n != *newValuePtr) {
148 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
149 }
150 *newValuePtr = 1;
151 u_fscanf(myFile, "Unsigned decimal integer %%u: %u\n", newValuePtr);
152 if (*n != *newValuePtr) {
153 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
154 }
155 *newValuePtr = 1;
156 u_fscanf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr);
157 if (*n != *newValuePtr) {
158 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
159 }
160 *newValuePtr = 1;
161 u_fscanf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr);
162 if (*n != *newValuePtr) {
163 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
164 }
165 *newDoubleValuePtr = -1.0;
166 u_fscanf(myFile, "Float %%f: %f\n", newDoubleValuePtr);
167 if (myFloat != *newDoubleValuePtr) {
168 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
169 }
170 *newDoubleValuePtr = -1.0;
171 u_fscanf(myFile, "Lowercase float %%e: %e\n", newDoubleValuePtr);
172 if (myFloat != *newDoubleValuePtr) {
173 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
174 }
175 *newDoubleValuePtr = -1.0;
176 u_fscanf(myFile, "Uppercase float %%E: %E\n", newDoubleValuePtr);
177 if (myFloat != *newDoubleValuePtr) {
178 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
179 }
180 *newDoubleValuePtr = -1.0;
181 u_fscanf(myFile, "Lowercase float %%g: %g\n", newDoubleValuePtr);
182 if (myFloat != *newDoubleValuePtr) {
183 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
184 }
185 *newDoubleValuePtr = -1.0;
186 u_fscanf(myFile, "Uppercase float %%G: %G\n", newDoubleValuePtr);
187 if (myFloat != *newDoubleValuePtr) {
188 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
189 }
190// u_fscanf(myFile, "Pointer %%p: %p\n", newDoubleValue);
191 u_fscanf(myFile, "Char %%c: %c\n", myString);
192 if (*myString != 'A') {
193 log_err("%%c Got: %c, Expected: A\n", *myString);
194 }
195 u_fscanf(myFile, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K\n", myUString);
196 if (*myUString != L'A') {
197 log_err("%%C Got: %C, Expected: A\n", *myUString);
198 }
199 u_fscanf(myFile, "String %%s: %s\n", myString);
200 if (strcmp(myString, "My-String")) {
201 log_err("%%s Got: %s, Expected: My String\n", myString);
202 }
203 u_fscanf(myFile, "NULL String %%s: %s\n", myString);
204 if (strcmp(myString, "(null)")) {
205 log_err("%%s Got: %s, Expected: My String\n", myString);
206 }
207 u_fscanf(myFile, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", myUString);
208 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
209 if (strcmp(myString, "My-String")) {
210 log_err("%%S Got: %S, Expected: My String\n", myUString);
211 }
212 u_fscanf(myFile, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", myUString);
213 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
214 if (strcmp(myString, "(null)")) {
215 log_err("%%S Got: %S, Expected: My String\n", myUString);
216 }
217 myNewDate = -1.0;
218 u_fscanf(myFile, "Date %%D (non-ANSI): %D\n", &myNewDate);
219 if (myNewDate != dec_31_1969) {
220 log_err("%%D Got: %f, Expected: %f\n", myNewDate, dec_31_1969);
221 }
222 myNewDate = -1.0;
223 u_fscanf(myFile, "Time %%T (non-ANSI): %T\n", &myNewDate);
224 if (myNewDate != midnight) {
225 log_err("%%T Got: %f, Expected: %f\n", myNewDate, midnight);
226 }
227 *newDoubleValuePtr = -1.0;
228 u_fscanf(myFile, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr);
229 if (myFloat != *newDoubleValuePtr) {
230 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
231 }
232 *newDoubleValuePtr = -1.0;
233 u_fscanf(myFile, "Currency %%M (non-ANSI): %M\n", newDoubleValuePtr);
234 if (myFloat != *newDoubleValuePtr) {
235 log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
236 }
237 *newDoubleValuePtr = -1.0;
238 u_fscanf(myFile, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr);
239 if (myFloat != *newDoubleValuePtr) {
240 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
241 }
242
243 u_fgets(myFile, 4, myUString);
244 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
245 if (myString == NULL || strcmp(myString, "\t\n") != 0) {
246 log_err("u_fgets got \"%s\"\n", myString);
247 }
248
249 if (u_fgets(myFile, sizeof(myUString)/sizeof(*myUString), myUString) != myUString) {
250 log_err("u_fgets did not return myUString\n");
251 }
252 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
253 if (myString == NULL || strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
254 log_err("u_fgets got \"%s\"\n", myString);
255 }
256
257 if (u_fgets(myFile, sizeof(myUString)/sizeof(*myUString), myUString) != myUString) {
258 log_err("u_fgets did not return myUString\n");
259 }
260 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
261 if (myString == NULL || strcmp(myString, "Pointer to integer Value: 37\n") != 0) {
262 log_err("u_fgets got \"%s\"\n", myString);
263 }
264
265 if (u_fgets(myFile, sizeof(myUString)/sizeof(*myUString), myUString) != myUString) {
266 log_err("u_fgets did not return myUString\n");
267 }
268 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
269 if (myString == NULL || strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890\n") != 0) {
270 log_err("u_fgets got \"%s\"\n", myString);
271 }
272
273 if (u_fgets(myFile, 0, myUString) != NULL) {
274 log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString);
275 }
276
277 if (u_fgets(myFile, 1, myUString) != myUString) {
278 log_err("u_fgets did not return myUString\n");
279 }
280 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
281 if (myString == NULL || strcmp(myString, "") != 0) {
282 log_err("u_fgets got \"%s\"\n", myString);
283 }
284
285 if (u_fgets(myFile, 2, myUString) != myUString) {
286 log_err("u_fgets did not return myUString\n");
287 }
288 u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
289 if (myString == NULL || strcmp(myString, "\t") != 0) {
290 log_err("u_fgets got \"%s\"\n", myString);
291 }
292
293 u_fclose(myFile);
294}
295
296static void TestFile() {
297/* FILE *standardFile;*/
298
299 log_verbose("Testing u_fopen\n");
300 TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL));
301
302/* Don't know how to make this work without stdout or stderr */
303/*
304 log_verbose("Testing u_finit\n");
305 standardFile = fopen(STANDARD_TEST_FILE, "wb");
306 TestFileFromICU(u_finit(standardFile, NULL, NULL));
307 fclose(standardFile);
308*/
309}
310
311static void TestCodepageAndLocale() {
312 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
313 if (u_fgetcodepage(myFile) == NULL
314 || strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
315 {
316 log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
317 u_fgetcodepage(myFile), ucnv_getDefaultName());
318 }
319 if (u_fgetlocale(myFile) == NULL
320 || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
321 {
322 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
323 u_fgetlocale(myFile), uloc_getDefault());
324 }
325 u_fclose(myFile);
326
327 myFile = u_fopen(STANDARD_TEST_FILE, "w", "es", NULL);
328 if (u_fgetcodepage(myFile) == NULL
329 || strcmp(u_fgetcodepage(myFile), "ISO-8859-1") != 0)
330 {
331 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
332 u_fgetcodepage(myFile));
333 }
334 if (u_fgetlocale(myFile) == NULL
335 || strcmp(u_fgetlocale(myFile), "es") != 0)
336 {
337 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
338 u_fgetlocale(myFile), "es");
339 }
340 u_fclose(myFile);
341
342 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
343 if (u_fgetcodepage(myFile) == NULL
344 || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
345 {
346 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
347 u_fgetcodepage(myFile));
348 }
349 if (u_fgetlocale(myFile) == NULL
350 || strcmp(u_fgetlocale(myFile), uloc_getDefault()) != 0)
351 {
352 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
353 u_fgetlocale(myFile), uloc_getDefault());
354 }
355 u_fclose(myFile);
356
357 myFile = u_fopen(STANDARD_TEST_FILE, "w", "zh", "UTF-16");
358 if (u_fgetcodepage(myFile) == NULL
359 || strcmp(u_fgetcodepage(myFile), "UTF-16") != 0)
360 {
361 log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
362 u_fgetcodepage(myFile));
363 }
364 if (u_fgetlocale(myFile) == NULL
365 || strcmp(u_fgetlocale(myFile), "zh") != 0)
366 {
367 log_err("Didn't get the proper default locale. Got %s expected: %s\n",
368 u_fgetlocale(myFile), "zh");
369 }
370 u_fclose(myFile);
371}
372
373
374static void TestfgetsBuffers() {
375 UChar buffer[2048];
376 UChar expectedBuffer[2048];
377 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!";
378 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-16");
379 int32_t expectedSize = strlen(testStr);
380 int32_t readSize;
381 int32_t repetitions;
382
383 u_fputc(0x3BC, myFile);
384 u_fputc(0xFF41, myFile);
385 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
386 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
387 u_uastrncpy(buffer, testStr, expectedSize+1);
388 for (repetitions = 0; repetitions < 16; repetitions++) {
389 u_file_write(buffer, expectedSize, myFile);
390 u_strcat(expectedBuffer, buffer);
391 }
392 u_fclose(myFile);
393
394 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
395 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-16");
396 if (u_fgetc(myFile) != 0x3BC) {
397 log_err("The first character is wrong\n");
398 }
399 if (u_fgetc(myFile) != 0xFF41) {
400 log_err("The second character is wrong\n");
401 }
402 if (u_fgets(myFile, sizeof(buffer)/sizeof(buffer[0]), buffer) != buffer) {
403 log_err("Didn't get the buffer back\n");
404 return;
405 }
406 readSize = u_strlen(buffer);
407 if (readSize != expectedSize*repetitions) {
408 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
409 }
410 if (buffer[(expectedSize*repetitions) + 1] != 0xDEAD) {
411 log_err("u_fgets wrote too much data\n");
412 }
413 if (u_strcmp(buffer, expectedBuffer) != 0) {
414 log_err("Did get expected string back\n");
415 }
416 if (strcmp(u_fgetcodepage(myFile), "UTF-16") != 0) {
417 log_err("Got %s instead of UTF-16\n", u_fgetcodepage(myFile));
418 }
419 u_fclose(myFile);
420
421
422 log_verbose("Now trying a multi-byte encoding (UTF-8).\n");
423
424 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
425
426 u_fputc(0x3BC, myFile);
427 u_fputc(0xFF41, myFile);
428 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
429 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
430 u_uastrncpy(buffer, testStr, expectedSize+1);
431 for (repetitions = 0; repetitions < 16; repetitions++) {
432 u_file_write(buffer, expectedSize, myFile);
433 u_strcat(expectedBuffer, buffer);
434 }
435 u_fclose(myFile);
436
437 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
438 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
439 if (strcmp(u_fgetcodepage(myFile), "UTF-8") != 0) {
440 log_err("Got %s instead of UTF-8\n", u_fgetcodepage(myFile));
441 }
442 if (u_fgetc(myFile) != 0x3BC) {
443 log_err("The first character is wrong\n");
444 }
445 if (u_fgetc(myFile) != 0xFF41) {
446 log_err("The second character is wrong\n");
447 }
448 if (u_fgets(myFile, sizeof(buffer)/sizeof(buffer[0]), buffer) != buffer) {
449 log_err("Didn't get the buffer back\n");
450 return;
451 }
452 readSize = u_strlen(buffer);
453 if (readSize != expectedSize*repetitions) {
454 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), expectedSize*repetitions);
455 }
456 if (buffer[(expectedSize*repetitions) + 1] != 0xDEAD) {
457 log_err("u_fgets wrote too much data\n");
458 }
459 if (u_strcmp(buffer, expectedBuffer) != 0) {
460 log_err("Did get expected string back\n");
461 }
462 u_fclose(myFile);
463
464
465 log_verbose("Now trying a multi-byte encoding (UTF-8) with a really small buffer.\n");
466
467 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, "UTF-8");
468
469 u_fputc(0xFF41, myFile);
470 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
471 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
472 u_uastrncpy(buffer, testStr, expectedSize+1);
473 for (repetitions = 0; repetitions < 1; repetitions++) {
474 u_file_write(buffer, expectedSize, myFile);
475 u_strcat(expectedBuffer, buffer);
476 }
477 u_fclose(myFile);
478
479 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
480 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, "UTF-8");
481 if (u_fgets(myFile, 2, buffer) != buffer) {
482 log_err("Didn't get the buffer back\n");
483 return;
484 }
485 readSize = u_strlen(buffer);
486 if (readSize != 1) {
487 log_err("Buffer is the wrong size. Got %d Expected %d\n", u_strlen(buffer), 1);
488 }
489 if (buffer[0] != 0xFF41 || buffer[1] != 0) {
490 log_err("Did get expected string back\n");
491 }
492 if (buffer[2] != 0xDEAD) {
493 log_err("u_fgets wrote too much data\n");
494 }
495 u_fclose(myFile);
496
497}
498
499
500static void TestfgetsLineCount() {
501 UChar buffer[2048];
502 UChar expectedBuffer[2048];
503 char charBuffer[2048];
504 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!";
505 UFILE *myFile = NULL;
506 FILE *stdFile = fopen(STANDARD_TEST_FILE, "w");
507 int32_t expectedSize = strlen(testStr);
508 int32_t repetitions;
509 int32_t nlRepetitions;
510
511 u_memset(expectedBuffer, 0, sizeof(expectedBuffer)/sizeof(expectedBuffer[0]));
512
513 for (repetitions = 0; repetitions < 16; repetitions++) {
514 fwrite(testStr, sizeof(testStr[0]), expectedSize, stdFile);
515 for (nlRepetitions = 0; nlRepetitions < repetitions; nlRepetitions++) {
516 fwrite("\n", sizeof(testStr[0]), 1, stdFile);
517 }
518 }
519 fclose(stdFile);
520
521 myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
522 stdFile = fopen(STANDARD_TEST_FILE, "r");
523
524 for (;;) {
525 u_memset(buffer, 0xDEAD, sizeof(buffer)/sizeof(buffer[0]));
526 char *returnedCharBuffer = fgets(charBuffer, sizeof(charBuffer)/sizeof(charBuffer[0]), stdFile);
527 UChar *returnedUCharBuffer = u_fgets(myFile, sizeof(buffer)/sizeof(buffer[0]), buffer);
528
529 if (!returnedCharBuffer && !returnedUCharBuffer) {
530 /* Both returned NULL. stop. */
531 break;
532 }
533 if (returnedCharBuffer != charBuffer) {
534 log_err("Didn't get the charBuffer back\n");
535 continue;
536 }
537 u_uastrncpy(expectedBuffer, charBuffer, strlen(charBuffer)+1);
538 if (returnedUCharBuffer != buffer) {
539 log_err("Didn't get the buffer back\n");
540 continue;
541 }
542 if (u_strcmp(buffer, expectedBuffer) != 0) {
543 log_err("buffers are different\n");
544 }
545 if (buffer[u_strlen(buffer)+1] != 0xDEAD) {
546 log_err("u_fgets wrote too much\n");
547 }
548 }
549 fclose(stdFile);
550 u_fclose(myFile);
551}
552
553static void TestFilePrintCompatibility() {
554 UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
555 FILE *myCFile;
556 int32_t num;
557 char cVal;
558 static const UChar emptyStr[] = {0};
559 char readBuf[512] = "";
560 char testBuf[512] = "";
561
562 if (myFile == NULL) {
563 log_err("Can't read test file.");
564 return;
565 }
566
567 if (strcmp(u_fgetlocale(myFile), "en_US_POSIX") != 0) {
568 log_err("Got %s instead of en_US_POSIX for locale\n", u_fgetlocale(myFile));
569 }
570
571 /* Compare against C API compatibility */
572 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
573 u_fprintf(myFile, "%x ", num);
574 u_fprintf(myFile, "%X ", num);
575 u_fprintf(myFile, "%o ", num);
576 u_fprintf(myFile, "%d ", num);
577 u_fprintf(myFile, "%i ", num);
578 u_fprintf(myFile, "%f ", (double)num);
579 u_fprintf(myFile, "%e ", (double)num);
580 u_fprintf(myFile, "%E ", (double)num);
581 u_fprintf(myFile, "%g ", (double)num);
582 u_fprintf(myFile, "%G", (double)num);
583 u_fputs(emptyStr, myFile);
584 }
585
586 u_fprintf_u(myFile, NEW_LINE);
587
588 for (num = 0; num < 0x80; num++) {
589 u_fprintf(myFile, "%c", num);
590 }
591
592 u_fclose(myFile);
593 myCFile = fopen(STANDARD_TEST_FILE, "rb");
594 if (myCFile == NULL) {
595 log_err("Can't read test file.");
596 return;
597 }
598
599 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
600 fscanf(myCFile, "%s", readBuf);
601 sprintf(testBuf, "%x", num);
602 if (strcmp(readBuf, testBuf) != 0) {
603 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
604 }
605
606 fscanf(myCFile, "%s", readBuf);
607 sprintf(testBuf, "%X", num);
608 if (strcmp(readBuf, testBuf) != 0) {
609 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
610 }
611
612 fscanf(myCFile, "%s", readBuf);
613 sprintf(testBuf, "%o", num);
614 if (strcmp(readBuf, testBuf) != 0) {
615 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
616 }
617
618 /* fprintf is not compatible on all platforms e.g. the iSeries */
619 fscanf(myCFile, "%s", readBuf);
620 sprintf(testBuf, "%d", num);
621 if (strcmp(readBuf, testBuf) != 0) {
622 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
623 }
624
625 fscanf(myCFile, "%s", readBuf);
626 sprintf(testBuf, "%i", num);
627 if (strcmp(readBuf, testBuf) != 0) {
628 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
629 }
630
631 fscanf(myCFile, "%s", readBuf);
632 sprintf(testBuf, "%f", (double)num);
633 if (strcmp(readBuf, testBuf) != 0) {
634 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
635 }
636
637 fscanf(myCFile, "%s", readBuf);
638 sprintf(testBuf, "%e", (double)num);
639 if (strcmp(readBuf, testBuf) != 0) {
640 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
641 }
642
643 fscanf(myCFile, "%s", readBuf);
644 sprintf(testBuf, "%E", (double)num);
645 if (strcmp(readBuf, testBuf) != 0) {
646 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
647 }
648
649 fscanf(myCFile, "%s", readBuf);
650 sprintf(testBuf, "%g", (double)num);
651 if (strcmp(readBuf, testBuf) != 0) {
652 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
653 }
654
655 fscanf(myCFile, "%s", readBuf);
656 sprintf(testBuf, "%G", (double)num);
657 if (strcmp(readBuf, testBuf) != 0) {
658 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
659 }
660 }
661
662 /* Properly eat the newlines */
663 for (num = 0; num < u_strlen(NEW_LINE); num++) {
664 fscanf(myCFile, "%c", &cVal);
665 if (cVal != NEW_LINE[num]) {
666 log_err("OS newline error\n");
667 }
668 }
669 for (num = 0; num < u_strlen(NEW_LINE); num++) {
670 fscanf(myCFile, "%c", &cVal);
671 if (cVal != NEW_LINE[num]) {
672 log_err("ustdio newline error\n");
673 }
674 }
675
676 for (num = 0; num < 0x80; num++) {
677 cVal = -1;
678 fscanf(myCFile, "%c", &cVal);
679 if (num != cVal) {
680 log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
681 }
682 }
683 fclose(myCFile);
684}
685
686#define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
687 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
688 /* Reinitialize the buffer to verify null termination works. */\
689 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
690 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
691 \
692 uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
693 u_fclose(myFile);\
694 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
695 u_fgets(myFile, sizeof(uBuffer)/sizeof(*uBuffer), uBuffer);\
696 u_fclose(myFile);\
697 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
698 cNumPrinted = sprintf(buffer, cFormat, cValue);\
699 if (strcmp(buffer, compBuffer) != 0) {\
700 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
701 }\
702 if (cNumPrinted != uNumPrinted) {\
703 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
704 }\
705 if (buffer[uNumPrinted+1] != 0x2a) {\
706 log_err("%" uFormat " too much stored\n");\
707 }\
708
709#define TestFPrintFormat2(format, precision, value) \
710 myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
711 /* Reinitialize the buffer to verify null termination works. */\
712 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
713 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
714 \
715 uNumPrinted = u_fprintf(myFile, format, precision, value);\
716 u_fclose(myFile);\
717 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
718 u_fgets(myFile, sizeof(uBuffer)/sizeof(*uBuffer), uBuffer);\
719 u_fclose(myFile);\
720 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
721 cNumPrinted = sprintf(buffer, format, precision, value);\
722 if (strcmp(buffer, compBuffer) != 0) {\
723 log_err("%" format " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
724 }\
725 if (cNumPrinted != uNumPrinted) {\
726 log_err("%" format " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
727 }\
728
729static void TestFprintfFormat() {
730 static const UChar abcUChars[] = {0x61,0x62,0x63,0};
731 static const char abcChars[] = "abc";
732 UChar uBuffer[256];
733 char buffer[256];
734 char compBuffer[256];
735 int32_t uNumPrinted;
736 int32_t cNumPrinted;
737 UFILE *myFile;
738
739 TestFPrintFormat("%8U", abcUChars, "%8s", abcChars);
740 TestFPrintFormat("%-8U", abcUChars, "%-8s", abcChars);
741
742 TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
743 TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
744
745 TestFPrintFormat("%8c", 0x65, "%8c", 0x65);
746 TestFPrintFormat("%-8c", 0x65, "%-8c", 0x65);
747
748 TestFPrintFormat("%8K", (UChar)0x65, "%8c", (char)0x65);
749 TestFPrintFormat("%-8K", (UChar)0x65, "%-8c", (char)0x65);
750
751 TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
752 TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
753 TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
754 TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
755
756 TestFPrintFormat("%e", 1234567.89, "%e", 1234567.89);
757 TestFPrintFormat("%E", 1234567.89, "%E", 1234567.89);
758 TestFPrintFormat("%10e", 1.23456789, "%10e", 1.23456789);
759 TestFPrintFormat("%-10e", 1.23456789, "%-10e", 1.23456789);
760 TestFPrintFormat("%10e", 1234.56789, "%10e", 1234.56789);
761 TestFPrintFormat("%-10e", 1234.56789, "%-10e", 1234.56789);
762
763 TestFPrintFormat("%g", 123456.789, "%g", 123456.789);
764 TestFPrintFormat("%g", 1234567.89, "%g", 1234567.89);
765 TestFPrintFormat("%G", 1234567.89, "%G", 1234567.89);
766 TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
767 TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
768 TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
769 TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
770
771 TestFPrintFormat("%8x", 123456, "%8x", 123456);
772 TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
773
774 TestFPrintFormat("%8X", 123456, "%8X", 123456);
775 TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
776 TestFPrintFormat("%#x", 123456, "%#x", 123456);
777 TestFPrintFormat("%#x", -123456, "%#x", -123456);
778
779 TestFPrintFormat("%8o", 123456, "%8o", 123456);
780 TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
781 TestFPrintFormat("%#o", 123, "%#o", 123);
782 TestFPrintFormat("%#o", -123, "%#o", -123);
783
784 TestFPrintFormat("%8u", 123456, "%8u", 123456);
785 TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
786 TestFPrintFormat("%8u", -123456, "%8u", -123456);
787 TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
788
789 TestFPrintFormat("%8d", 123456, "%8d", 123456);
790 TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
791 TestFPrintFormat("% d", 123456, "% d", 123456);
792 TestFPrintFormat("% d", -123456, "% d", -123456);
793
794 TestFPrintFormat("%8i", 123456, "%8i", 123456);
795 TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
796
797 TestFPrintFormat2("%+1.*e", 4, 1.2345678);
798 TestFPrintFormat2("%+2.*e", 6, 1.2345678);
799
800 log_verbose("Get really crazy with the formatting.\n");
801
802 TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
803 TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
804 TestFPrintFormat("%#12x", 123, "%#12x", 123);
805 TestFPrintFormat("%#12x", -123, "%#12x", -123);
806
807 TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
808 TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
809 TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
810 TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
811 TestFPrintFormat("%+12d", 123, "%+12d", 123);
812 TestFPrintFormat("%+12d", -123, "%+12d", -123);
813 TestFPrintFormat("% 12d", 123, "% 12d", 123);
814 TestFPrintFormat("% 12d", -123, "% 12d", -123);
815 TestFPrintFormat("%12d", 123, "%12d", 123);
816 TestFPrintFormat("%12d", -123, "%12d", -123);
817
818 TestFPrintFormat("%-+12.1e", 1.234, "%-+12.1e", 1.234);
819 TestFPrintFormat("%-+12.1e", -1.234, "%-+12.1e", -1.234);
820 TestFPrintFormat("%- 12.1e", 1.234, "%- 12.1e", 1.234);
821 TestFPrintFormat("%- 12.1e", -1.234, "%- 12.1e", -1.234);
822 TestFPrintFormat("%+12.1e", 1.234, "%+12.1e", 1.234);
823 TestFPrintFormat("%+12.1e", -1.234, "%+12.1e", -1.234);
824 TestFPrintFormat("% 12.1e", 1.234, "% 12.1e", 1.234);
825 TestFPrintFormat("% 12.1e", -1.234, "% 12.1e", -1.234);
826 TestFPrintFormat("%12.1e", 1.234, "%12.1e", 1.234);
827 TestFPrintFormat("%12.1e", -1.234, "%12.1e", -1.234);
828 TestFPrintFormat("%.2e", 1.234, "%.2e", 1.234);
829 TestFPrintFormat("%.2e", -1.234, "%.2e", -1.234);
830 TestFPrintFormat("%3e", 1.234, "%3e", 1.234);
831 TestFPrintFormat("%3e", -1.234, "%3e", -1.234);
832
833 TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
834 TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
835 TestFPrintFormat("%- 12.1f", 1.234, "%- 12.1f", 1.234);
836 TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
837 TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
838 TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
839 TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
840 TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
841 TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
842 TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
843 TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
844 TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
845 TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
846 TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
847
848}
849
850#undef TestFPrintFormat
851
852static void TestString() {
853 int32_t n[1];
854 float myFloat = -1234.0;
855 UDate myDate = 0.0;
856 UDate dec_31_1969 = -57600000.000000; /* TODO: These are not correct */
857 UDate midnight = 86400000.000000; /* TODO: These are not correct */
858 UDate myNewDate = -1.0;
859 int32_t newValuePtr[1];
860 double newDoubleValuePtr[1];
861 UChar myUString[512];
862 UChar uStringBuf[512];
863 char myString[512] = "";
864 char testBuf[512] = "";
865
866 u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString));
867 u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf));
868
869 *n = -1234;
870
871 /* Test sprintf */
872 u_sprintf(uStringBuf, NULL, "Signed decimal integer d: %d", *n);
873 *newValuePtr = 1;
874 u_sscanf(uStringBuf, NULL, "Signed decimal integer d: %d", newValuePtr);
875 if (*n != *newValuePtr) {
876 log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
877 }
878
879 u_sprintf(uStringBuf, NULL, "Signed decimal integer i: %i", *n);
880 *newValuePtr = 1;
881 u_sscanf(uStringBuf, NULL, "Signed decimal integer i: %i", newValuePtr);
882 if (*n != *newValuePtr) {
883 log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
884 }
885
886 u_sprintf(uStringBuf, NULL, "Unsigned octal integer o: %o", *n);
887 *newValuePtr = 1;
888 u_sscanf(uStringBuf, NULL, "Unsigned octal integer o: %o", newValuePtr);
889 if (*n != *newValuePtr) {
890 log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
891 }
892
893 u_sprintf(uStringBuf, NULL, "Unsigned decimal integer %%u: %u", *n);
894 *newValuePtr = 1;
895 u_sscanf(uStringBuf, NULL, "Unsigned decimal integer %%u: %u", newValuePtr);
896 if (*n != *newValuePtr) {
897 log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
898 }
899
900 u_sprintf(uStringBuf, NULL, "Lowercase unsigned hexadecimal integer x: %x", *n);
901 *newValuePtr = 1;
902 u_sscanf(uStringBuf, NULL, "Lowercase unsigned hexadecimal integer x: %x", newValuePtr);
903 if (*n != *newValuePtr) {
904 log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
905 }
906
907 u_sprintf(uStringBuf, NULL, "Uppercase unsigned hexadecimal integer X: %X", *n);
908 *newValuePtr = 1;
909 u_sscanf(uStringBuf, NULL, "Uppercase unsigned hexadecimal integer X: %X", newValuePtr);
910 if (*n != *newValuePtr) {
911 log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
912 }
913
914 u_sprintf(uStringBuf, NULL, "Float f: %f", myFloat);
915 *newDoubleValuePtr = -1.0;
916 u_sscanf(uStringBuf, NULL, "Float f: %f", newDoubleValuePtr);
917 if (myFloat != *newDoubleValuePtr) {
918 log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
919 }
920
921 u_sprintf(uStringBuf, NULL, "Lowercase float e: %e", myFloat);
922 *newDoubleValuePtr = -1.0;
923 u_sscanf(uStringBuf, NULL, "Lowercase float e: %e", newDoubleValuePtr);
924 if (myFloat != *newDoubleValuePtr) {
925 log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
926 }
927
928 u_sprintf(uStringBuf, NULL, "Uppercase float E: %E", myFloat);
929 *newDoubleValuePtr = -1.0;
930 u_sscanf(uStringBuf, NULL, "Uppercase float E: %E", newDoubleValuePtr);
931 if (myFloat != *newDoubleValuePtr) {
932 log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
933 }
934
935 u_sprintf(uStringBuf, NULL, "Lowercase float g: %g", myFloat);
936 *newDoubleValuePtr = -1.0;
937 u_sscanf(uStringBuf, NULL, "Lowercase float g: %g", newDoubleValuePtr);
938 if (myFloat != *newDoubleValuePtr) {
939 log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
940 }
941
942 u_sprintf(uStringBuf, NULL, "Uppercase float G: %G", myFloat);
943 *newDoubleValuePtr = -1.0;
944 u_sscanf(uStringBuf, NULL, "Uppercase float G: %G", newDoubleValuePtr);
945 if (myFloat != *newDoubleValuePtr) {
946 log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
947 }
948
949// u_sprintf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
950 u_sprintf(uStringBuf, NULL, "Char c: %c", 'A');
951 u_sscanf(uStringBuf, NULL, "Char c: %c", myString);
952 if (*myString != 'A') {
953 log_err("%%c Got: %c, Expected: A\n", *myString);
954 }
955
956 u_sprintf(uStringBuf, NULL, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K", L'A');
957 u_sscanf(uStringBuf, NULL, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K", myUString);
958 if (*myUString != L'A') {
959 log_err("%%C Got: %C, Expected: A\n", *myUString);
960 }
961
962 u_sprintf(uStringBuf, NULL, "String %%s: %s", "My-String");
963 u_sscanf(uStringBuf, NULL, "String %%s: %s", myString);
964 if (strcmp(myString, "My-String")) {
965 log_err("%%s Got: %s, Expected: My-String\n", myString);
966 }
967 if (uStringBuf[20] != 0) {
968 log_err("String not terminated. Got %c\n", uStringBuf[20] );
969 }
970 u_sprintf(uStringBuf, NULL, "NULL String %%s: %s", NULL);
971 u_sscanf(uStringBuf, NULL, "NULL String %%s: %s", myString);
972 if (strcmp(myString, "(null)")) {
973 log_err("%%s Got: %s, Expected: My-String\n", myString);
974 }
975
976 u_sprintf(uStringBuf, NULL, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", L"My-String");
977 u_sscanf(uStringBuf, NULL, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", myUString);
978 u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString));
979 if (strcmp(myString, "My-String")) {
980 log_err("%%U Got: %s, Expected: My String\n", myString);
981 }
982
983 u_sprintf(uStringBuf, NULL, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", NULL);
984 u_sscanf(uStringBuf, NULL, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", myUString);
985 u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString));
986 if (strcmp(myString, "(null)")) {
987 log_err("%%U Got: %s, Expected: (null)\n", myString);
988 }
989
990 u_sprintf(uStringBuf, NULL, "Date %%D (non-ANSI): %D", myDate);
991 myNewDate = -1.0;
992 u_sscanf(uStringBuf, NULL, "Date %%D (non-ANSI): %D", &myNewDate);
993 if (myNewDate != dec_31_1969) {
994 log_err("%%D Got: %f, Expected: %f\n", myNewDate, dec_31_1969);
995 }
996
997 u_sprintf(uStringBuf, NULL, "Time %%T (non-ANSI): %T", myDate);
998 myNewDate = -1.0;
999 u_sscanf(uStringBuf, NULL, "Time %%T (non-ANSI): %T", &myNewDate);
1000 if (myNewDate != midnight) {
1001 log_err("%%T Got: %f, Expected: %f\n", myNewDate, midnight);
1002 }
1003
1004 u_sprintf(uStringBuf, NULL, "Percent %%P (non-ANSI): %P", myFloat);
1005 *newDoubleValuePtr = -1.0;
1006 u_sscanf(uStringBuf, NULL, "Percent %%P (non-ANSI): %P", newDoubleValuePtr);
1007 if (myFloat != *newDoubleValuePtr) {
1008 log_err("%%P Got: %P, Expected: %P\n", *newDoubleValuePtr, myFloat);
1009 }
1010
1011 u_sprintf(uStringBuf, NULL, "Currency %%M (non-ANSI): %M", myFloat);
1012 *newDoubleValuePtr = -1.0;
1013 u_sscanf(uStringBuf, NULL, "Currency %%M (non-ANSI): %M", newDoubleValuePtr);
1014 if (myFloat != *newDoubleValuePtr) {
1015 log_err("%%P Got: %P, Expected: %P\n", *newDoubleValuePtr, myFloat);
1016 }
1017
1018 u_sprintf(uStringBuf, NULL, "Spell Out %%V (non-ANSI): %V", myFloat);
1019 *newDoubleValuePtr = -1.0;
1020 u_sscanf(uStringBuf, NULL, "Spell Out %%V (non-ANSI): %V", newDoubleValuePtr);
1021 if (myFloat != *newDoubleValuePtr) {
1022 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
1023 }
1024
1025 *newValuePtr = 1;
1026 u_sprintf(uStringBuf, NULL, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *newValuePtr, newValuePtr, *newValuePtr);
1027 if (*newValuePtr != 37) {
1028 log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
1029 }
1030
1031 u_sprintf(myUString, NULL, "This is a long test123456789012345678901234567890123456789012345678901234567890");
1032 u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString));
1033 if (strcmp(myString, "This is a long test123456789012345678901234567890123456789012345678901234567890")) {
1034 log_err("%%U Got: %s, Expected: My String\n", myString);
1035 }
1036
1037
1038// u_sscanf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
1039}
1040
1041#define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
1042 u_uastrncpy(testStr, "xxxxxxxxxxxxxx", sizeof(testStr)/sizeof(testStr[0]));\
1043 size = u_snprintf(testStr, limit, "en_US_POSIX", format, value);\
1044 u_austrncpy(cTestResult, testStr, sizeof(cTestResult)/sizeof(cTestResult[0]));\
1045 if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
1046 log_err("Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s\n",\
1047 size, expectedSize, cTestResult, expectedStr);\
1048 }\
1049 else {\
1050 log_verbose("Got: %s\n", cTestResult);\
1051 }\
1052
1053
1054static void TestSnprintf() {
1055 UChar testStr[256];
1056 char cTestResult[256];
1057 int32_t size;
1058
1059 Test_u_snprintf(0, "%d", 123, 0, "xxxxxxxxxxxxxx");
1060 Test_u_snprintf(2, "%d", 123, 2, "12xxxxxxxxxxxx");
1061 Test_u_snprintf(3, "%d", 123, 3, "123xxxxxxxxxxx");
1062 Test_u_snprintf(4, "%d", 123, 3, "123");
1063
1064 Test_u_snprintf(0, "%s", "abcd", 0, "xxxxxxxxxxxxxx");
1065 Test_u_snprintf(3, "%s", "abcd", 3, "abcxxxxxxxxxxx");
1066 Test_u_snprintf(4, "%s", "abcd", 4, "abcdxxxxxxxxxx");
1067 Test_u_snprintf(5, "%s", "abcd", 4, "abcd");
1068
1069 Test_u_snprintf(0, "%e", 12.34, 0, "xxxxxxxxxxxxxx");
1070 Test_u_snprintf(1, "%e", 12.34, 1, "1xxxxxxxxxxxxx");
1071 Test_u_snprintf(2, "%e", 12.34, 2, "1.xxxxxxxxxxxx");
1072 Test_u_snprintf(3, "%e", 12.34, 3, "1.2xxxxxxxxxxx");
1073 Test_u_snprintf(5, "%e", 12.34, 5, "1.234xxxxxxxxx");
1074 Test_u_snprintf(6, "%e", 12.34, 6, "1.2340xxxxxxxx");
1075 Test_u_snprintf(8, "%e", 12.34, 8, "1.234000xxxxxx");
1076 Test_u_snprintf(9, "%e", 12.34, 9, "1.234000exxxxx");
1077 Test_u_snprintf(10, "%e", 12.34, 10, "1.234000e+xxxx");
1078 Test_u_snprintf(11, "%e", 12.34, 11, "1.234000e+0xxx");
1079 Test_u_snprintf(13, "%e", 12.34, 13, "1.234000e+001x");
1080 Test_u_snprintf(14, "%e", 12.34, 13, "1.234000e+001");
1081}
1082
1083#define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
1084 /* Reinitialize the buffer to verify null termination works. */\
1085 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1086 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
1087 \
1088 uNumPrinted = u_sprintf(uBuffer, "en_US_POSIX", uFormat, uValue);\
1089 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\
1090 cNumPrinted = sprintf(buffer, cFormat, cValue);\
1091 if (strcmp(buffer, compBuffer) != 0) {\
1092 log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1093 }\
1094 if (cNumPrinted != uNumPrinted) {\
1095 log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1096 }\
1097 if (buffer[uNumPrinted+1] != 0x2a) {\
1098 log_err("%" uFormat " too much stored\n");\
1099 }\
1100
1101#define TestSPrintFormat2(format, precision, value) \
1102 /* Reinitialize the buffer to verify null termination works. */\
1103 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1104 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
1105 \
1106 uNumPrinted = u_sprintf(uBuffer, "en_US_POSIX", format, precision, value);\
1107 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\
1108 cNumPrinted = sprintf(buffer, format, precision, value);\
1109 if (strcmp(buffer, compBuffer) != 0) {\
1110 log_err("%" format " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1111 }\
1112 if (cNumPrinted != uNumPrinted) {\
1113 log_err("%" format " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
1114 }\
1115
1116static void TestSprintfFormat() {
1117 static const UChar abcUChars[] = {0x61,0x62,0x63,0};
1118 static const char abcChars[] = "abc";
1119 UChar uBuffer[256];
1120 char buffer[256];
1121 char compBuffer[256];
1122 int32_t uNumPrinted;
1123 int32_t cNumPrinted;
1124
1125 TestSPrintFormat("%8U", abcUChars, "%8s", abcChars);
1126 TestSPrintFormat("%-8U", abcUChars, "%-8s", abcChars);
1127
1128 TestSPrintFormat("%8s", abcChars, "%8s", abcChars);
1129 TestSPrintFormat("%-8s", abcChars, "%-8s", abcChars);
1130
1131 TestSPrintFormat("%8c", 0x65, "%8c", 0x65);
1132 TestSPrintFormat("%-8c", 0x65, "%-8c", 0x65);
1133
1134 TestSPrintFormat("%8K", (UChar)0x65, "%8c", (char)0x65);
1135 TestSPrintFormat("%-8K", (UChar)0x65, "%-8c", (char)0x65);
1136
1137 TestSPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
1138 TestSPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
1139 TestSPrintFormat("%10f", 123.456789, "%10f", 123.456789);
1140 TestSPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
1141
1142 TestSPrintFormat("%e", 1234567.89, "%e", 1234567.89);
1143 TestSPrintFormat("%E", 1234567.89, "%E", 1234567.89);
1144 TestSPrintFormat("%10e", 1.23456789, "%10e", 1.23456789);
1145 TestSPrintFormat("%-10e", 1.23456789, "%-10e", 1.23456789);
1146 TestSPrintFormat("%10e", 123.456789, "%10e", 123.456789);
1147 TestSPrintFormat("%-10e", 123.456789, "%-10e", 123.456789);
1148
1149 TestSPrintFormat("%g", 123456.789, "%g", 123456.789);
1150 TestSPrintFormat("%g", 1234567.89, "%g", 1234567.89);
1151 TestSPrintFormat("%G", 1234567.89, "%G", 1234567.89);
1152 TestSPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
1153 TestSPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
1154 TestSPrintFormat("%10g", 123.456789, "%10g", 123.456789);
1155 TestSPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
1156
1157 TestSPrintFormat("%8x", 123456, "%8x", 123456);
1158 TestSPrintFormat("%-8x", 123456, "%-8x", 123456);
1159
1160 TestSPrintFormat("%8X", 123456, "%8X", 123456);
1161 TestSPrintFormat("%-8X", 123456, "%-8X", 123456);
1162 TestSPrintFormat("%#x", 123456, "%#x", 123456);
1163 TestSPrintFormat("%#x", -123456, "%#x", -123456);
1164
1165 TestSPrintFormat("%8o", 123456, "%8o", 123456);
1166 TestSPrintFormat("%-8o", 123456, "%-8o", 123456);
1167 TestSPrintFormat("%#o", 123, "%#o", 123);
1168 TestSPrintFormat("%#o", -123, "%#o", -123);
1169
1170 TestSPrintFormat("%8u", 123456, "%8u", 123456);
1171 TestSPrintFormat("%-8u", 123456, "%-8u", 123456);
1172 TestSPrintFormat("%8u", -123456, "%8u", -123456);
1173 TestSPrintFormat("%-8u", -123456, "%-8u", -123456);
1174
1175 TestSPrintFormat("%8d", 123456, "%8d", 123456);
1176 TestSPrintFormat("%-8d", 123456, "%-8d", 123456);
1177 TestSPrintFormat("% d", 123456, "% d", 123456);
1178 TestSPrintFormat("% d", -123456, "% d", -123456);
1179
1180 TestSPrintFormat("%8i", 123456, "%8i", 123456);
1181 TestSPrintFormat("%-8i", 123456, "%-8i", 123456);
1182
1183 TestSPrintFormat2("%+1.*e", 4, 1.2345678);
1184 TestSPrintFormat2("%+2.*e", 6, 1.2345678);
1185
1186 log_verbose("Get really crazy with the formatting.\n");
1187
1188 TestSPrintFormat("%-#12x", 123, "%-#12x", 123);
1189 TestSPrintFormat("%-#12x", -123, "%-#12x", -123);
1190 TestSPrintFormat("%#12x", 123, "%#12x", 123);
1191 TestSPrintFormat("%#12x", -123, "%#12x", -123);
1192
1193 TestSPrintFormat("%-+12d", 123, "%-+12d", 123);
1194 TestSPrintFormat("%-+12d", -123, "%-+12d", -123);
1195 TestSPrintFormat("%- 12d", 123, "%- 12d", 123);
1196 TestSPrintFormat("%- 12d", -123, "%- 12d", -123);
1197 TestSPrintFormat("%+12d", 123, "%+12d", 123);
1198 TestSPrintFormat("%+12d", -123, "%+12d", -123);
1199 TestSPrintFormat("% 12d", 123, "% 12d", 123);
1200 TestSPrintFormat("% 12d", -123, "% 12d", -123);
1201 TestSPrintFormat("%12d", 123, "%12d", 123);
1202 TestSPrintFormat("%12d", -123, "%12d", -123);
1203
1204 TestSPrintFormat("%-+12.1e", 1.234, "%-+12.1e", 1.234);
1205 TestSPrintFormat("%-+12.1e", -1.234, "%-+12.1e", -1.234);
1206 TestSPrintFormat("%- 12.1e", 1.234, "%- 12.1e", 1.234);
1207 TestSPrintFormat("%- 12.1e", -1.234, "%- 12.1e", -1.234);
1208 TestSPrintFormat("%+12.1e", 1.234, "%+12.1e", 1.234);
1209 TestSPrintFormat("%+12.1e", -1.234, "%+12.1e", -1.234);
1210 TestSPrintFormat("% 12.1e", 1.234, "% 12.1e", 1.234);
1211 TestSPrintFormat("% 12.1e", -1.234, "% 12.1e", -1.234);
1212 TestSPrintFormat("%12.1e", 1.234, "%12.1e", 1.234);
1213 TestSPrintFormat("%12.1e", -1.234, "%12.1e", -1.234);
1214 TestSPrintFormat("%.2e", 1.234, "%.2e", 1.234);
1215 TestSPrintFormat("%.2e", -1.234, "%.2e", -1.234);
1216 TestSPrintFormat("%3e", 1.234, "%3e", 1.234);
1217 TestSPrintFormat("%3e", -1.234, "%3e", -1.234);
1218
1219 TestSPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
1220 TestSPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
1221 TestSPrintFormat("%- 12.1f", 1.234, "%- 12.1f", 1.234);
1222 TestSPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
1223 TestSPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
1224 TestSPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
1225 TestSPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
1226 TestSPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
1227 TestSPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
1228 TestSPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
1229 TestSPrintFormat("%.2f", 1.234, "%.2f", 1.234);
1230 TestSPrintFormat("%.2f", -1.234, "%.2f", -1.234);
1231 TestSPrintFormat("%3f", 1.234, "%3f", 1.234);
1232 TestSPrintFormat("%3f", -1.234, "%3f", -1.234);
1233
1234}
1235
1236#undef TestSPrintFormat
1237
1238static void TestStringCompatibility() {
1239 UChar myUString[256];
1240 UChar uStringBuf[256];
1241 char myString[256] = "";
1242 char testBuf[256] = "";
1243 int32_t num;
1244
1245 u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString));
1246 u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf));
1247
1248 /* Compare against C API compatibility */
1249 for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
1250 sprintf(testBuf, "%x", num);
1251 u_sprintf(uStringBuf, NULL, "%x", num);
1252 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1253 if (strcmp(myString, testBuf) != 0) {
1254 log_err("%%x Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1255 }
1256
1257 sprintf(testBuf, "%X", num);
1258 u_sprintf(uStringBuf, NULL, "%X", num);
1259 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1260 if (strcmp(myString, testBuf) != 0) {
1261 log_err("%%X Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1262 }
1263
1264 sprintf(testBuf, "%o", num);
1265 u_sprintf(uStringBuf, NULL, "%o", num);
1266 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1267 if (strcmp(myString, testBuf) != 0) {
1268 log_err("%%o Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1269 }
1270
1271 /* sprintf is not compatible on all platforms e.g. the iSeries*/
1272 sprintf(testBuf, "%d", num);
1273 u_sprintf(uStringBuf, "en_US_POSIX", "%d", num);
1274 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1275 if (strcmp(myString, testBuf) != 0) {
1276 log_err("%%d Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1277 }
1278
1279 sprintf(testBuf, "%i", num);
1280 u_sprintf(uStringBuf, "en_US_POSIX", "%i", num);
1281 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1282 if (strcmp(myString, testBuf) != 0) {
1283 log_err("%%i Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1284 }
1285
1286 sprintf(testBuf, "%f", (double)num);
1287 u_sprintf(uStringBuf, "en_US_POSIX", "%f", (double)num);
1288 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1289 if (strcmp(myString, testBuf) != 0) {
1290 log_err("%%f Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1291 }
1292
1293 sprintf(testBuf, "%e", (double)num);
1294 u_sprintf(uStringBuf, "en_US_POSIX", "%e", (double)num);
1295 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1296 if (strcmp(myString, testBuf) != 0) {
1297 log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1298 }
1299
1300 sprintf(testBuf, "%E", (double)num);
1301 u_sprintf(uStringBuf, "en_US_POSIX", "%E", (double)num);
1302 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1303 if (strcmp(myString, testBuf) != 0) {
1304 log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1305 }
1306
1307 sprintf(testBuf, "%g", (double)num);
1308 u_sprintf(uStringBuf, "en_US_POSIX", "%g", (double)num);
1309 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1310 if (strcmp(myString, testBuf) != 0) {
1311 log_err("%%g Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1312 }
1313
1314 sprintf(testBuf, "%G", (double)num);
1315 u_sprintf(uStringBuf, "en_US_POSIX", "%G", (double)num);
1316 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1317 if (strcmp(myString, testBuf) != 0) {
1318 log_err("%%G Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1319 }
1320 }
1321
1322 for (num = 0; num < 0x80; num++) {
1323 testBuf[0] = (char)0xFF;
1324 uStringBuf[0] = (UChar)0xfffe;
1325 sprintf(testBuf, "%c", num);
1326 u_sprintf(uStringBuf, NULL, "%c", num);
1327 u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1328 if (testBuf[0] != uStringBuf[0] || uStringBuf[0] != num) {
1329 log_err("%%c Got: 0x%x, Expected: 0x%x\n", myString[0], testBuf[0]);
1330 }
1331 }
1332}
1333
1334#define TestSScanSetFormat(format, uValue, cValue) \
1335 /* Reinitialize the buffer to verify null termination works. */\
1336 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1337 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
1338 \
1339 uNumScanned = u_sscanf(uValue, NULL, format, uBuffer);\
1340 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\
1341 cNumScanned = sscanf(cValue, format, buffer);\
1342 if (strncmp(buffer, compBuffer, sizeof(uBuffer)/sizeof(uBuffer[0])) != 0) {\
1343 log_err("%" format " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1344 }\
1345 if (cNumScanned != uNumScanned) {\
1346 log_err("%" format " number scanned Got: %d, Expected: %d\n", uNumScanned, cNumScanned);\
1347 }\
1348 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {\
1349 log_err("%" format " too much stored\n");\
1350 }\
1351
1352static void TestSScanf() {
1353 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1354 static const char abcChars[] = "abccdefg";
1355 UChar uBuffer[256];
1356 char buffer[256];
1357 char compBuffer[256];
1358 int32_t uNumScanned;
1359 int32_t cNumScanned;
1360
1361 TestSScanSetFormat("%[bc]U", abcUChars, abcChars);
1362 TestSScanSetFormat("%[cb]U", abcUChars, abcChars);
1363
1364 TestSScanSetFormat("%[ab]U", abcUChars, abcChars);
1365 TestSScanSetFormat("%[ba]U", abcUChars, abcChars);
1366
1367 TestSScanSetFormat("%[ab]", abcUChars, abcChars);
1368 TestSScanSetFormat("%[ba]", abcUChars, abcChars);
1369
1370 TestSScanSetFormat("%[abcdefgh]", abcUChars, abcChars);
1371 TestSScanSetFormat("%[;hgfedcba]", abcUChars, abcChars);
1372
1373 TestSScanSetFormat("%[a-f]", abcUChars, abcChars);
1374 TestSScanSetFormat("%[f-a]", abcUChars, abcChars);
1375 TestSScanSetFormat("%[a-c]", abcUChars, abcChars);
1376 TestSScanSetFormat("%[c-a]", abcUChars, abcChars);
1377
1378 TestSScanSetFormat("%[^e-f]", abcUChars, abcChars);
1379
1380 TestSScanSetFormat("%[^a]", abcUChars, abcChars);
1381 TestSScanSetFormat("%[^e]", abcUChars, abcChars);
1382 TestSScanSetFormat("%[^ed]", abcUChars, abcChars);
1383 TestSScanSetFormat("%[^dc]", abcUChars, abcChars);
1384 TestSScanSetFormat("%[^e] ", abcUChars, abcChars);
1385
1386 TestSScanSetFormat("%[] ", abcUChars, abcChars);
1387 TestSScanSetFormat("%1[ab] ", abcUChars, abcChars);
1388 TestSScanSetFormat("%2[^f]", abcUChars, abcChars);
1389
1390 /* Bad format */
1391 TestSScanSetFormat("%[a", abcUChars, abcChars);
1392 /* The following is not deterministic on Windows */
1393/* TestSScanSetFormat("%[a-", abcUChars, abcChars);*/
1394 TestSScanSetFormat("%[a-]", abcUChars, abcChars);
1395
1396 /* TODO: Need to specify precision with a "*" */
1397}
1398
1399#undef TestSScanSetFormat
1400
1401
1402#define TestFScanSetFormat(format, uValue, cValue) \
1403 myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);\
1404 /* Reinitialize the buffer to verify null termination works. */\
1405 u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
1406 memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
1407 \
1408 u_fprintf(myFile, "%U", uValue);\
1409 u_fclose(myFile);\
1410 myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
1411 uNumScanned = u_fscanf(myFile, format, uBuffer);\
1412 u_fclose(myFile);\
1413 u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
1414 cNumScanned = sscanf(cValue, format, buffer);\
1415 if (strncmp(buffer, compBuffer, sizeof(uBuffer)/sizeof(*uBuffer)) != 0) {\
1416 log_err("%" format " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
1417 }\
1418 if (cNumScanned != uNumScanned) {\
1419 log_err("%" format " number printed Got: %d, Expected: %d\n", uNumScanned, cNumScanned);\
1420 }\
1421 if (uNumScanned > 0 && uBuffer[u_strlen(uBuffer)+1] != 0x2a) {\
1422 log_err("%" format " too much stored\n");\
1423 }\
1424
1425
1426static void TestFScanf() {
1427 UFILE *myFile;
1428 static const UChar abcUChars[] = {0x61,0x62,0x63,0x63,0x64,0x65,0x66,0x67,0};
1429 static const char abcChars[] = "abccdefg";
1430 UChar uBuffer[256];
1431 char buffer[256];
1432 char compBuffer[256];
1433 int32_t uNumScanned;
1434 int32_t cNumScanned;
1435
1436 TestFScanSetFormat("%[bc]U", abcUChars, abcChars);
1437 TestFScanSetFormat("%[cb]U", abcUChars, abcChars);
1438
1439 TestFScanSetFormat("%[ab]U", abcUChars, abcChars);
1440 TestFScanSetFormat("%[ba]U", abcUChars, abcChars);
1441
1442 TestFScanSetFormat("%[ab]", abcUChars, abcChars);
1443 TestFScanSetFormat("%[ba]", abcUChars, abcChars);
1444
1445 TestFScanSetFormat("%[abcdefgh]", abcUChars, abcChars);
1446 TestFScanSetFormat("%[;hgfedcba]", abcUChars, abcChars);
1447
1448 TestFScanSetFormat("%[a-f]", abcUChars, abcChars);
1449 TestFScanSetFormat("%[f-a]", abcUChars, abcChars);
1450 TestFScanSetFormat("%[a-c]", abcUChars, abcChars);
1451 TestFScanSetFormat("%[c-a]", abcUChars, abcChars);
1452
1453 TestFScanSetFormat("%[^e-f]", abcUChars, abcChars);
1454
1455 TestFScanSetFormat("%[^a]", abcUChars, abcChars);
1456 TestFScanSetFormat("%[^e]", abcUChars, abcChars);
1457 TestFScanSetFormat("%[^ed]", abcUChars, abcChars);
1458 TestFScanSetFormat("%[^dc]", abcUChars, abcChars);
1459 TestFScanSetFormat("%[^e] ", abcUChars, abcChars);
1460
1461 TestFScanSetFormat("%[] ", abcUChars, abcChars);
1462 TestFScanSetFormat("%1[ab] ", abcUChars, abcChars);
1463 TestFScanSetFormat("%2[^f]", abcUChars, abcChars);
1464
1465 /* Bad format */
1466 TestFScanSetFormat("%[a", abcUChars, abcChars);
1467 /* The following is not deterministic on Windows */
1468/* TestFScanSetFormat("%[a-", abcUChars, abcChars);*/
1469 TestFScanSetFormat("%[a-]", abcUChars, abcChars);
1470
1471 /* TODO: Need to specify precision with a "*" */
1472}
1473
1474static void TestStream() {
1475#if U_IOSTREAM_SOURCE >= 198506
1476 char testStreamBuf[512];
1477 const char *testStr = "Beginning of test str1 <<432 1" C_NEW_LINE " UTF-8 \xCE\xBC\xF0\x90\x80\x81\xF0\x90\x80\x82";
1478 ostrstream outTestStream(testStreamBuf, sizeof(testStreamBuf));
1479 istrstream inTestStream(" tHis\xCE\xBC\xE2\x80\x82 mu world", 0);
1480 const UChar thisMu[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0};
1481 const UChar mu[] = { 0x6D, 0x75, 0};
1482 UnicodeString str1 = UNICODE_STRING_SIMPLE("str1");
1483 UnicodeString str2 = UNICODE_STRING_SIMPLE(" <<");
1484 UnicodeString str3 = UNICODE_STRING_SIMPLE("4");
1485 UnicodeString str4 = UNICODE_STRING_SIMPLE(" UTF-8 ");
1486 UnicodeString inStr = UNICODE_STRING_SIMPLE(" UTF-8 ");
1487 UnicodeString inStr2;
1488 char defConvName[UCNV_MAX_CONVERTER_NAME_LENGTH*2];
1489 char inStrC[128];
1490 UErrorCode status = U_ZERO_ERROR;
1491 UConverter *defConv;
1492
1493 str4.append((UChar32)0x03BC); /* mu */
1494 str4.append((UChar32)0x10001);
1495 str4.append((UChar32)0x10002);
1496
1497 /* release the default converter and use utf-8 for a bit */
1498 defConv = u_getDefaultConverter(&status);
1499 if (U_FAILURE(status)) {
1500 log_err("Can't get default converter");
1501 return;
1502 }
1503 ucnv_close(defConv);
1504 strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0]));
1505 ucnv_setDefaultName("UTF-8");
1506
1507 outTestStream << "Beginning of test ";
1508 outTestStream << str1 << " " << str2 << str3 << 3 << "2 " << 1.0 << C_NEW_LINE << str4 << ends;
1509 if (strcmp(testStreamBuf, testStr) != 0) {
1510 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf, testStr);
1511 }
1512
1513 inTestStream >> inStr >> inStr2;
1514 if (inStr.compare(thisMu) != 0) {
1515 u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
1516 inStrC[inStr.length()] = 0;
1517 log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC);
1518 }
1519 if (inStr2.compare(mu) != 0) {
1520 u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
1521 inStrC[inStr.length()] = 0;
1522 log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC);
1523 }
1524
1525 /* return the default converter to the original state. */
1526 ucnv_setDefaultName(defConvName);
1527 defConv = u_getDefaultConverter(&status);
1528 if (U_FAILURE(status)) {
1529 log_err("Can't get default converter");
1530 return;
1531 }
1532 ucnv_close(defConv);
1533#else
1534 log_err("U_IOSTREAM_SOURCE is disabled");
1535#endif
1536}
1537
1538static void TestTranslitOps()
1539{
1540 UFILE *f;
1541 UErrorCode err = U_ZERO_ERROR;
1542 UTransliterator *a = NULL, *b = NULL, *c = NULL;
1543
1544 log_verbose("opening a transliterator and UFILE for testing\n");
1545
1546 f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
1547 if(f == NULL)
1548 {
1549 log_err("Couldn't open test file for writing");
1550 return;
1551 }
1552
1553 a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
1554 if(U_FAILURE(err))
1555 {
1556 log_err("Err opening transliterator %s\n", u_errorName(err));
1557 u_fclose(f);
1558 return;
1559 }
1560
1561
1562 log_verbose("setting a transliterator\n");
1563 b = u_fsettransliterator(f, U_WRITE, a, &err);
1564 if(U_FAILURE(err))
1565 {
1566 log_err("Err setting transliterator %s\n", u_errorName(err));
1567 u_fclose(f);
1568 return;
1569 }
1570
1571 if(b != NULL)
1572 {
1573 log_err("Err, a transliterator was already set!\n");
1574 }
1575
1576 log_verbose("un-setting transliterator (setting to null)\n");
1577 c = u_fsettransliterator(f, U_WRITE, NULL, &err);
1578 if(U_FAILURE(err))
1579 {
1580 log_err("Err setting transliterator %s\n", u_errorName(err));
1581 u_fclose(f);
1582 return;
1583 }
1584
1585 if(c != a)
1586 {
1587 log_err("Err, transliterator that came back was not the original one.\n");
1588 }
1589
1590 log_verbose("Trying to set read transliterator (should fail)\n");
1591 b = u_fsettransliterator(f, U_READ, NULL, &err);
1592 if(err != U_UNSUPPORTED_ERROR)
1593 {
1594 log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err));
1595 u_fclose(f);
1596 return;
1597 }
1598 else
1599 {
1600 log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err));
1601 err = U_ZERO_ERROR;
1602 }
1603
1604
1605 utrans_close(c);
1606 u_fclose(f);
1607}
1608
1609static void TestTranslitOut()
1610{
1611 UFILE *f;
1612 UErrorCode err = U_ZERO_ERROR;
1613 UTransliterator *a = NULL, *b = NULL, *c = NULL;
1614 FILE *infile;
1615 UChar compare[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x0000 };
1616 UChar ubuf[256];
1617 int len;
1618
1619 log_verbose("opening a transliterator and UFILE for testing\n");
1620
1621 f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "utf-16");
1622 if(f == NULL)
1623 {
1624 log_err("Couldn't open test file for writing");
1625 return;
1626 }
1627
1628 a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
1629 if(U_FAILURE(err))
1630 {
1631 log_err("Err opening transliterator %s\n", u_errorName(err));
1632 u_fclose(f);
1633 return;
1634 }
1635
1636 log_verbose("setting a transliterator\n");
1637 b = u_fsettransliterator(f, U_WRITE, a, &err);
1638 if(U_FAILURE(err))
1639 {
1640 log_err("Err setting transliterator %s\n", u_errorName(err));
1641 u_fclose(f);
1642 return;
1643 }
1644
1645 if(b != NULL)
1646 {
1647 log_err("Err, a transliterator was already set!\n");
1648 }
1649
1650 u_fprintf(f, "Stephanos");
1651
1652 u_fclose(f);
1653
1654 log_verbose("Re reading test file to verify transliteration\n");
1655 infile = fopen(STANDARD_TEST_FILE, "rb");
1656 if(infile == NULL)
1657 {
1658 log_err("Couldn't reopen test file\n");
1659 return;
1660 }
1661
1662 len=fread(ubuf, sizeof(UChar), u_strlen(compare), infile);
1663 log_verbose("Read %d UChars\n", len);
1664 if(len != u_strlen(compare))
1665 {
1666 log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare), len);
1667 }
1668 ubuf[len]=0;
1669
1670 if(u_strlen(compare) != u_strlen(ubuf))
1671 {
1672 log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), len);
1673 }
1674
1675 if(u_strcmp(compare, ubuf))
1676 {
1677 log_err("Read string doesn't match expected.\n");
1678 }
1679 else
1680 {
1681 log_verbose("Read string matches expected.\n");
1682 }
1683
1684 fclose(infile);
1685
1686}
1687
1688static void addAllTests(TestNode** root) {
1689 addTest(root, &TestFile, "file/TestFile");
1690 addTest(root, &TestCodepageAndLocale, "file/TestCodepageAndLocale");
1691 addTest(root, &TestfgetsBuffers, "file/TestfgetsBuffers");
1692 addTest(root, &TestfgetsLineCount, "file/TestfgetsLineCount");
1693 addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
1694 addTest(root, &TestFScanf, "file/TestFScanf");
1695 addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
1696
1697 addTest(root, &TestString, "string/TestString");
1698 addTest(root, &TestSprintfFormat, "string/TestSprintfFormat");
1699 addTest(root, &TestSnprintf, "string/TestSnprintf");
1700 addTest(root, &TestSScanf, "string/TestSScanf");
1701 addTest(root, &TestStringCompatibility, "string/TestStringCompatibility");
1702 addTest(root, &TestStream, "stream/TestStream");
1703
1704 addTest(root, &TestTranslitOps, "translit/ops");
1705 addTest(root, &TestTranslitOut, "translit/out");
1706}
1707
1708int main(int argc, char* argv[])
1709{
1710 int32_t nerrors = 0;
1711 TestNode *root = NULL;
1712
1713 addAllTests(&root);
1714 nerrors = processArgs(root, argc, argv);
1715
1716 cleanUpTestTree(root);
1717 u_cleanup();
1718 return nerrors;
1719}