2
**********************************************************************
3
* Copyright (C) 2002-2002, International Business Machines
4
* Corporation and others. All Rights Reserved.
5
**********************************************************************
6
* file name: iotest.cpp
8
* tab size: 8 (not used)
11
* created on: 2002feb21
12
* created by: George Rhoten
16
#include "unicode/ustdio.h"
17
#include "unicode/ustream.h"
18
#include "unicode/uclean.h"
20
#include "unicode/ucnv.h"
21
#include "unicode/unistr.h"
22
#include "unicode/ustring.h"
23
#include "unicode/ctest.h"
24
#include "unicode/utrans.h"
27
#if U_IOSTREAM_SOURCE >= 199711
31
#elif U_IOSTREAM_SOURCE >= 198506
33
#include <strstream.h>
38
static char STANDARD_TEST_FILE[] = "iotest-c.txt";
39
static const int32_t STANDARD_TEST_NUM_RANGE = 1000;
42
static const UChar NEW_LINE[] = {0x0d,0x0a,0};
43
#define C_NEW_LINE "\r\n"
45
static const UChar NEW_LINE[] = {0x0a,0};
46
#define C_NEW_LINE "\n"
49
static void TestFileFromICU(UFILE *myFile) {
51
float myFloat = -1234.0;
53
UDate dec_31_1969 = -57600000.000000; /* TODO: These are not correct */
54
UDate midnight = 86400000.000000; /* TODO: These are not correct */
55
UDate myNewDate = -1.0;
56
int32_t newValuePtr[1];
57
double newDoubleValuePtr[1];
59
UChar uStringBuf[256];
60
char myString[256] = "";
61
char testBuf[256] = "";
63
u_memset(myUString, 0x2a, sizeof(myUString)/sizeof(*myUString));
64
u_memset(uStringBuf, 0x2a, sizeof(uStringBuf)/sizeof(*uStringBuf));
65
memset(myString, 0x2a, sizeof(myString)/sizeof(*myString));
66
memset(testBuf, 0x2a, sizeof(testBuf)/sizeof(*testBuf));
69
log_err("Can't write test file.");
76
u_fprintf(myFile, "Signed decimal integer %%d: %d\n", *n);
77
u_fprintf(myFile, "Signed decimal integer %%i: %i\n", *n);
78
u_fprintf(myFile, "Unsigned octal integer %%o: %o\n", *n);
79
u_fprintf(myFile, "Unsigned decimal integer %%u: %u\n", *n);
80
u_fprintf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", *n);
81
u_fprintf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", *n);
82
u_fprintf(myFile, "Float %%f: %f\n", myFloat);
83
u_fprintf(myFile, "Lowercase float %%e: %e\n", myFloat);
84
u_fprintf(myFile, "Uppercase float %%E: %E\n", myFloat);
85
u_fprintf(myFile, "Lowercase float %%g: %g\n", myFloat);
86
u_fprintf(myFile, "Uppercase float %%G: %G\n", myFloat);
87
// u_fprintf(myFile, "Pointer %%p: %p\n", myFile);
88
u_fprintf(myFile, "Char %%c: %c\n", 'A');
89
u_fprintf(myFile, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K\n", L'A');
90
u_fprintf(myFile, "String %%s: %s\n", "My-String");
91
u_fprintf(myFile, "NULL String %%s: %s\n", NULL);
92
u_fprintf(myFile, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", L"My-String");
93
u_fprintf(myFile, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", NULL);
94
u_fprintf(myFile, "Date %%D (non-ANSI): %D\n", myDate);
95
u_fprintf(myFile, "Time %%T (non-ANSI): %T\n", myDate);
96
u_fprintf(myFile, "Percent %%P (non-ANSI): %P\n", myFloat);
97
u_fprintf(myFile, "Currency %%M (non-ANSI): %M\n", myFloat);
98
u_fprintf(myFile, "Spell Out %%V (non-ANSI): %V\n", myFloat);
101
u_fprintf(myFile, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *n, n, *n);
102
u_fprintf(myFile, "Pointer to integer Value: %d\n", *n);
104
fprintf(u_fgetfile(myFile), "\tNormal fprintf count: n=%d %n n=%d\n", *n ,n, *n);
105
fprintf(u_fgetfile(myFile), "\tNormal fprintf count value: n=%d\n", *n);
108
myFile = u_fopen(STANDARD_TEST_FILE, "r", NULL, NULL);
110
if (myFile == NULL) {
111
log_err("Can't read test file.");
117
myString[0] = u_fgetc(myFile);
118
if (myString[0] != 0x53 /* S */) {
119
log_err("u_fgetc 1 returned %X. Expected 'S'.", myString[0]);
121
u_fungetc(myString[0], myFile);
122
myString[0] = u_fgetc(myFile);
123
if (myString[0] != 0x53 /* S */) {
124
log_err("u_fgetc 2 returned %X. Expected 'S'.", myString[0]);
126
u_fungetc(myString[0], myFile);
127
myString[0] = u_fgetc(myFile);
128
if (myString[0] != 0x53 /* S */) {
129
log_err("u_fgetc 3 returned %X. Expected 'S'.", myString[0]);
131
u_fungetc(myString[0], myFile);
134
u_fscanf(myFile, "Signed decimal integer %%d: %d\n", newValuePtr);
135
if (*n != *newValuePtr) {
136
log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
139
u_fscanf(myFile, "Signed decimal integer %%i: %i\n", newValuePtr);
140
if (*n != *newValuePtr) {
141
log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
144
u_fscanf(myFile, "Unsigned octal integer %%o: %o\n", newValuePtr);
145
if (*n != *newValuePtr) {
146
log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
149
u_fscanf(myFile, "Unsigned decimal integer %%u: %u\n", newValuePtr);
150
if (*n != *newValuePtr) {
151
log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
154
u_fscanf(myFile, "Lowercase unsigned hexadecimal integer %%x: %x\n", newValuePtr);
155
if (*n != *newValuePtr) {
156
log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
159
u_fscanf(myFile, "Uppercase unsigned hexadecimal integer %%X: %X\n", newValuePtr);
160
if (*n != *newValuePtr) {
161
log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
163
*newDoubleValuePtr = -1.0;
164
u_fscanf(myFile, "Float %%f: %f\n", newDoubleValuePtr);
165
if (myFloat != *newDoubleValuePtr) {
166
log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
168
*newDoubleValuePtr = -1.0;
169
u_fscanf(myFile, "Lowercase float %%e: %e\n", newDoubleValuePtr);
170
if (myFloat != *newDoubleValuePtr) {
171
log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
173
*newDoubleValuePtr = -1.0;
174
u_fscanf(myFile, "Uppercase float %%E: %E\n", newDoubleValuePtr);
175
if (myFloat != *newDoubleValuePtr) {
176
log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
178
*newDoubleValuePtr = -1.0;
179
u_fscanf(myFile, "Lowercase float %%g: %g\n", newDoubleValuePtr);
180
if (myFloat != *newDoubleValuePtr) {
181
log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
183
*newDoubleValuePtr = -1.0;
184
u_fscanf(myFile, "Uppercase float %%G: %G\n", newDoubleValuePtr);
185
if (myFloat != *newDoubleValuePtr) {
186
log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
188
// u_fscanf(myFile, "Pointer %%p: %p\n", newDoubleValue);
189
u_fscanf(myFile, "Char %%c: %c\n", myString);
190
if (*myString != 'A') {
191
log_err("%%c Got: %c, Expected: A\n", *myString);
193
u_fscanf(myFile, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K\n", myUString);
194
if (*myUString != L'A') {
195
log_err("%%C Got: %C, Expected: A\n", *myUString);
197
u_fscanf(myFile, "String %%s: %s\n", myString);
198
if (strcmp(myString, "My-String")) {
199
log_err("%%s Got: %s, Expected: My String\n", myString);
201
u_fscanf(myFile, "NULL String %%s: %s\n", myString);
202
if (strcmp(myString, "(null)")) {
203
log_err("%%s Got: %s, Expected: My String\n", myString);
205
u_fscanf(myFile, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", myUString);
206
u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
207
if (strcmp(myString, "My-String")) {
208
log_err("%%S Got: %S, Expected: My String\n", myUString);
210
u_fscanf(myFile, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U\n", myUString);
211
u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
212
if (strcmp(myString, "(null)")) {
213
log_err("%%S Got: %S, Expected: My String\n", myUString);
216
u_fscanf(myFile, "Date %%D (non-ANSI): %D\n", &myNewDate);
217
if (myNewDate != dec_31_1969) {
218
log_err("%%D Got: %f, Expected: %f\n", myNewDate, dec_31_1969);
221
u_fscanf(myFile, "Time %%T (non-ANSI): %T\n", &myNewDate);
222
if (myNewDate != midnight) {
223
log_err("%%T Got: %f, Expected: %f\n", myNewDate, midnight);
225
*newDoubleValuePtr = -1.0;
226
u_fscanf(myFile, "Percent %%P (non-ANSI): %P\n", newDoubleValuePtr);
227
if (myFloat != *newDoubleValuePtr) {
228
log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
230
*newDoubleValuePtr = -1.0;
231
u_fscanf(myFile, "Currency %%M (non-ANSI): %M\n", newDoubleValuePtr);
232
if (myFloat != *newDoubleValuePtr) {
233
log_err("%%P Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
235
*newDoubleValuePtr = -1.0;
236
u_fscanf(myFile, "Spell Out %%V (non-ANSI): %V\n", newDoubleValuePtr);
237
if (myFloat != *newDoubleValuePtr) {
238
log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
241
u_fgets(myFile, 4, myUString);
242
u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
243
if (myString == NULL || strcmp(myString, "\t\n") != 0) {
244
log_err("u_fgets got \"%s\"\n", myString);
247
if (u_fgets(myFile, sizeof(myUString)/sizeof(*myUString), myUString) != myUString) {
248
log_err("u_fgets did not return myUString\n");
250
u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
251
if (myString == NULL || strcmp(myString, "Pointer to integer (Count) %n: n=1 n=1\n") != 0) {
252
log_err("u_fgets got \"%s\"\n", myString);
255
if (u_fgets(myFile, sizeof(myUString)/sizeof(*myUString), myUString) != myUString) {
256
log_err("u_fgets did not return myUString\n");
258
u_austrncpy(myString, myUString, sizeof(myUString)/sizeof(*myUString));
259
if (myString == NULL || strcmp(myString, "Pointer to integer Value: 37\n") != 0) {
260
log_err("u_fgets got \"%s\"\n", myString);
263
if (u_fgets(myFile, 0, myUString) != NULL) {
264
log_err("u_fgets got \"%s\" and it should have returned NULL\n", myString);
267
if (u_fgets(myFile, 1, myUString) != myUString) {
268
log_err("u_fgets did not return myUString\n");
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);
275
if (u_fgets(myFile, 2, myUString) != myUString) {
276
log_err("u_fgets did not return myUString\n");
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);
286
static void TestFile() {
287
/* FILE *standardFile;*/
289
log_verbose("Testing u_fopen\n");
290
TestFileFromICU(u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL));
292
/* Don't know how to make this work without stdout or stderr */
294
log_verbose("Testing u_finit\n");
295
standardFile = fopen(STANDARD_TEST_FILE, "wb");
296
TestFileFromICU(u_finit(standardFile, NULL, NULL));
297
fclose(standardFile);
301
static void TestCodepage() {
302
UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "w", NULL, NULL);
303
if (u_fgetcodepage(myFile) == NULL
304
|| strcmp(u_fgetcodepage(myFile), ucnv_getDefaultName()) != 0)
306
log_err("Didn't get the proper default codepage. Got %s expected: %s\n",
307
u_fgetcodepage(myFile), ucnv_getDefaultName());
311
myFile = u_fopen(STANDARD_TEST_FILE, "w", "en", NULL);
312
if (u_fgetcodepage(myFile) == NULL
313
|| strcmp(u_fgetcodepage(myFile), "ISO-8859-1") != 0)
315
log_err("Didn't get the proper default codepage for \"en\". Got %s expected: iso-8859-1\n",
316
u_fgetcodepage(myFile));
322
static void TestFilePrintCompatibility() {
323
UFILE *myFile = u_fopen(STANDARD_TEST_FILE, "wb", "en_US_POSIX", NULL);
327
static const UChar emptyStr[] = {0};
328
char readBuf[512] = "";
329
char testBuf[512] = "";
331
if (myFile == NULL) {
332
log_err("Can't read test file.");
336
/* Compare against C API compatibility */
337
for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
338
u_fprintf(myFile, "%x ", num);
339
u_fprintf(myFile, "%X ", num);
340
u_fprintf(myFile, "%o ", num);
341
u_fprintf(myFile, "%d ", num);
342
u_fprintf(myFile, "%i ", num);
343
u_fprintf(myFile, "%f ", (double)num);
344
u_fprintf(myFile, "%e ", (double)num);
345
u_fprintf(myFile, "%E ", (double)num);
346
u_fprintf(myFile, "%g ", (double)num);
347
u_fprintf(myFile, "%G", (double)num);
348
u_fputs(emptyStr, myFile);
351
u_fprintf_u(myFile, NEW_LINE);
353
for (num = 0; num < 0x80; num++) {
354
u_fprintf(myFile, "%c", num);
358
myCFile = fopen(STANDARD_TEST_FILE, "rb");
359
if (myCFile == NULL) {
360
log_err("Can't read test file.");
364
for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
365
fscanf(myCFile, "%s", readBuf);
366
sprintf(testBuf, "%x", num);
367
if (strcmp(readBuf, testBuf) != 0) {
368
log_err("%%x Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
371
fscanf(myCFile, "%s", readBuf);
372
sprintf(testBuf, "%X", num);
373
if (strcmp(readBuf, testBuf) != 0) {
374
log_err("%%X Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
377
fscanf(myCFile, "%s", readBuf);
378
sprintf(testBuf, "%o", num);
379
if (strcmp(readBuf, testBuf) != 0) {
380
log_err("%%o Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
383
/* fprintf is not compatible on all platforms e.g. the iSeries */
384
fscanf(myCFile, "%s", readBuf);
385
sprintf(testBuf, "%d", num);
386
if (strcmp(readBuf, testBuf) != 0) {
387
log_err("%%d Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
390
fscanf(myCFile, "%s", readBuf);
391
sprintf(testBuf, "%i", num);
392
if (strcmp(readBuf, testBuf) != 0) {
393
log_err("%%i Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
396
fscanf(myCFile, "%s", readBuf);
397
sprintf(testBuf, "%f", (double)num);
398
if (strcmp(readBuf, testBuf) != 0) {
399
log_err("%%f Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
402
fscanf(myCFile, "%s", readBuf);
403
sprintf(testBuf, "%e", (double)num);
404
if (strcmp(readBuf, testBuf) != 0) {
405
log_err("%%e Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
408
fscanf(myCFile, "%s", readBuf);
409
sprintf(testBuf, "%E", (double)num);
410
if (strcmp(readBuf, testBuf) != 0) {
411
log_err("%%E Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
414
fscanf(myCFile, "%s", readBuf);
415
sprintf(testBuf, "%g", (double)num);
416
if (strcmp(readBuf, testBuf) != 0) {
417
log_err("%%g Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
420
fscanf(myCFile, "%s", readBuf);
421
sprintf(testBuf, "%G", (double)num);
422
if (strcmp(readBuf, testBuf) != 0) {
423
log_err("%%G Got: \"%s\", Expected: \"%s\"\n", readBuf, testBuf);
427
/* Properly eat the newlines */
428
for (num = 0; num < u_strlen(NEW_LINE); num++) {
429
fscanf(myCFile, "%c", &cVal);
430
if (cVal != NEW_LINE[num]) {
431
log_err("OS newline error\n");
434
for (num = 0; num < u_strlen(NEW_LINE); num++) {
435
fscanf(myCFile, "%c", &cVal);
436
if (cVal != NEW_LINE[num]) {
437
log_err("ustdio newline error\n");
441
for (num = 0; num < 0x80; num++) {
443
fscanf(myCFile, "%c", &cVal);
445
log_err("%%c Got: 0x%x, Expected: 0x%x\n", cVal, num);
451
#define TestFPrintFormat(uFormat, uValue, cFormat, cValue) \
452
myFile = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);\
453
/* Reinitialize the buffer to verify null termination works. */\
454
u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
455
memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
457
uNumPrinted = u_fprintf(myFile, uFormat, uValue);\
459
myFile = u_fopen(STANDARD_TEST_FILE, "r", "en_US_POSIX", NULL);\
460
u_fgets(myFile, sizeof(uBuffer)/sizeof(*uBuffer), uBuffer);\
462
u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(*uBuffer));\
463
cNumPrinted = sprintf(buffer, cFormat, cValue);\
464
if (strcmp(buffer, compBuffer) != 0) {\
465
log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
467
if (cNumPrinted != uNumPrinted) {\
468
log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
472
static void TestFprintfFormat() {
473
static const UChar abcUChars[] = {0x61,0x62,0x63,0};
474
static const char abcChars[] = "abc";
477
char compBuffer[256];
482
TestFPrintFormat("%8U", abcUChars, "%8s", abcChars);
483
TestFPrintFormat("%-8U", abcUChars, "%-8s", abcChars);
485
TestFPrintFormat("%8s", abcChars, "%8s", abcChars);
486
TestFPrintFormat("%-8s", abcChars, "%-8s", abcChars);
488
TestFPrintFormat("%8c", 0x65, "%8c", 0x65);
489
TestFPrintFormat("%-8c", 0x65, "%-8c", 0x65);
491
TestFPrintFormat("%8K", (UChar)0x65, "%8c", (char)0x65);
492
TestFPrintFormat("%-8K", (UChar)0x65, "%-8c", (char)0x65);
494
TestFPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
495
TestFPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
496
TestFPrintFormat("%10f", 123.456789, "%10f", 123.456789);
497
TestFPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
499
TestFPrintFormat("%10e", 1.23456789, "%10e", 1.23456789);
500
TestFPrintFormat("%-10e", 1.23456789, "%-10e", 1.23456789);
501
TestFPrintFormat("%10e", 123.456789, "%10e", 123.456789);
502
TestFPrintFormat("%-10e", 123.456789, "%-10e", 123.456789);
504
TestFPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
505
TestFPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
506
TestFPrintFormat("%10g", 123.456789, "%10g", 123.456789);
507
TestFPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
509
TestFPrintFormat("%8x", 123456, "%8x", 123456);
510
TestFPrintFormat("%-8x", 123456, "%-8x", 123456);
512
TestFPrintFormat("%8X", 123456, "%8X", 123456);
513
TestFPrintFormat("%-8X", 123456, "%-8X", 123456);
514
TestFPrintFormat("%#x", 123456, "%#x", 123456);
515
TestFPrintFormat("%#x", -123456, "%#x", -123456);
517
TestFPrintFormat("%8o", 123456, "%8o", 123456);
518
TestFPrintFormat("%-8o", 123456, "%-8o", 123456);
519
TestFPrintFormat("%#o", 123, "%#o", 123);
520
TestFPrintFormat("%#o", -123, "%#o", -123);
522
TestFPrintFormat("%8u", 123456, "%8u", 123456);
523
TestFPrintFormat("%-8u", 123456, "%-8u", 123456);
524
TestFPrintFormat("%8u", -123456, "%8u", -123456);
525
TestFPrintFormat("%-8u", -123456, "%-8u", -123456);
527
TestFPrintFormat("%8d", 123456, "%8d", 123456);
528
TestFPrintFormat("%-8d", 123456, "%-8d", 123456);
529
TestFPrintFormat("% d", 123456, "% d", 123456);
530
TestFPrintFormat("% d", -123456, "% d", -123456);
532
TestFPrintFormat("%8i", 123456, "%8i", 123456);
533
TestFPrintFormat("%-8i", 123456, "%-8i", 123456);
535
log_verbose("Get really crazy with the formatting.\n");
537
TestFPrintFormat("%-#12x", 123, "%-#12x", 123);
538
TestFPrintFormat("%-#12x", -123, "%-#12x", -123);
539
TestFPrintFormat("%#12x", 123, "%#12x", 123);
540
TestFPrintFormat("%#12x", -123, "%#12x", -123);
542
TestFPrintFormat("%-+12d", 123, "%-+12d", 123);
543
TestFPrintFormat("%-+12d", -123, "%-+12d", -123);
544
TestFPrintFormat("%- 12d", 123, "%- 12d", 123);
545
TestFPrintFormat("%- 12d", -123, "%- 12d", -123);
546
TestFPrintFormat("%+12d", 123, "%+12d", 123);
547
TestFPrintFormat("%+12d", -123, "%+12d", -123);
548
TestFPrintFormat("% 12d", 123, "% 12d", 123);
549
TestFPrintFormat("% 12d", -123, "% 12d", -123);
550
TestFPrintFormat("%12d", 123, "%12d", 123);
551
TestFPrintFormat("%12d", -123, "%12d", -123);
553
TestFPrintFormat("%-+12.1e", 1.234, "%-+12.1e", 1.234);
554
TestFPrintFormat("%-+12.1e", -1.234, "%-+12.1e", -1.234);
555
TestFPrintFormat("%- 12.1e", 1.234, "%- 12.1e", 1.234);
556
TestFPrintFormat("%- 12.1e", -1.234, "%- 12.1e", -1.234);
557
TestFPrintFormat("%+12.1e", 1.234, "%+12.1e", 1.234);
558
TestFPrintFormat("%+12.1e", -1.234, "%+12.1e", -1.234);
559
TestFPrintFormat("% 12.1e", 1.234, "% 12.1e", 1.234);
560
TestFPrintFormat("% 12.1e", -1.234, "% 12.1e", -1.234);
561
TestFPrintFormat("%12.1e", 1.234, "%12.1e", 1.234);
562
TestFPrintFormat("%12.1e", -1.234, "%12.1e", -1.234);
563
TestFPrintFormat("%.2e", 1.234, "%.2e", 1.234);
564
TestFPrintFormat("%.2e", -1.234, "%.2e", -1.234);
565
TestFPrintFormat("%3e", 1.234, "%3e", 1.234);
566
TestFPrintFormat("%3e", -1.234, "%3e", -1.234);
568
TestFPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
569
TestFPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
570
TestFPrintFormat("%- 12.1f", 1.234, "%- 12.1f", 1.234);
571
TestFPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
572
TestFPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
573
TestFPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
574
TestFPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
575
TestFPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
576
TestFPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
577
TestFPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
578
TestFPrintFormat("%.2f", 1.234, "%.2f", 1.234);
579
TestFPrintFormat("%.2f", -1.234, "%.2f", -1.234);
580
TestFPrintFormat("%3f", 1.234, "%3f", 1.234);
581
TestFPrintFormat("%3f", -1.234, "%3f", -1.234);
584
#undef TestFPrintFormat
586
static void TestString() {
588
float myFloat = -1234.0;
590
UDate dec_31_1969 = -57600000.000000; /* TODO: These are not correct */
591
UDate midnight = 86400000.000000; /* TODO: These are not correct */
592
UDate myNewDate = -1.0;
593
int32_t newValuePtr[1];
594
double newDoubleValuePtr[1];
595
UChar myUString[512];
596
UChar uStringBuf[512];
597
char myString[512] = "";
598
char testBuf[512] = "";
600
u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString));
601
u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf));
606
u_sprintf(uStringBuf, NULL, "Signed decimal integer d: %d", *n);
608
u_sscanf(uStringBuf, NULL, "Signed decimal integer d: %d", newValuePtr);
609
if (*n != *newValuePtr) {
610
log_err("%%d Got: %d, Expected: %d\n", *newValuePtr, *n);
613
u_sprintf(uStringBuf, NULL, "Signed decimal integer i: %i", *n);
615
u_sscanf(uStringBuf, NULL, "Signed decimal integer i: %i", newValuePtr);
616
if (*n != *newValuePtr) {
617
log_err("%%i Got: %i, Expected: %i\n", *newValuePtr, *n);
620
u_sprintf(uStringBuf, NULL, "Unsigned octal integer o: %o", *n);
622
u_sscanf(uStringBuf, NULL, "Unsigned octal integer o: %o", newValuePtr);
623
if (*n != *newValuePtr) {
624
log_err("%%o Got: %o, Expected: %o\n", *newValuePtr, *n);
627
u_sprintf(uStringBuf, NULL, "Unsigned decimal integer %%u: %u", *n);
629
u_sscanf(uStringBuf, NULL, "Unsigned decimal integer %%u: %u", newValuePtr);
630
if (*n != *newValuePtr) {
631
log_err("%%u Got: %u, Expected: %u\n", *newValuePtr, *n);
634
u_sprintf(uStringBuf, NULL, "Lowercase unsigned hexadecimal integer x: %x", *n);
636
u_sscanf(uStringBuf, NULL, "Lowercase unsigned hexadecimal integer x: %x", newValuePtr);
637
if (*n != *newValuePtr) {
638
log_err("%%x Got: %x, Expected: %x\n", *newValuePtr, *n);
641
u_sprintf(uStringBuf, NULL, "Uppercase unsigned hexadecimal integer X: %X", *n);
643
u_sscanf(uStringBuf, NULL, "Uppercase unsigned hexadecimal integer X: %X", newValuePtr);
644
if (*n != *newValuePtr) {
645
log_err("%%X Got: %X, Expected: %X\n", *newValuePtr, *n);
648
u_sprintf(uStringBuf, NULL, "Float f: %f", myFloat);
649
*newDoubleValuePtr = -1.0;
650
u_sscanf(uStringBuf, NULL, "Float f: %f", newDoubleValuePtr);
651
if (myFloat != *newDoubleValuePtr) {
652
log_err("%%f Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
655
u_sprintf(uStringBuf, NULL, "Lowercase float e: %e", myFloat);
656
*newDoubleValuePtr = -1.0;
657
u_sscanf(uStringBuf, NULL, "Lowercase float e: %e", newDoubleValuePtr);
658
if (myFloat != *newDoubleValuePtr) {
659
log_err("%%e Got: %e, Expected: %e\n", *newDoubleValuePtr, myFloat);
662
u_sprintf(uStringBuf, NULL, "Uppercase float E: %E", myFloat);
663
*newDoubleValuePtr = -1.0;
664
u_sscanf(uStringBuf, NULL, "Uppercase float E: %E", newDoubleValuePtr);
665
if (myFloat != *newDoubleValuePtr) {
666
log_err("%%E Got: %E, Expected: %E\n", *newDoubleValuePtr, myFloat);
669
u_sprintf(uStringBuf, NULL, "Lowercase float g: %g", myFloat);
670
*newDoubleValuePtr = -1.0;
671
u_sscanf(uStringBuf, NULL, "Lowercase float g: %g", newDoubleValuePtr);
672
if (myFloat != *newDoubleValuePtr) {
673
log_err("%%g Got: %g, Expected: %g\n", *newDoubleValuePtr, myFloat);
676
u_sprintf(uStringBuf, NULL, "Uppercase float G: %G", myFloat);
677
*newDoubleValuePtr = -1.0;
678
u_sscanf(uStringBuf, NULL, "Uppercase float G: %G", newDoubleValuePtr);
679
if (myFloat != *newDoubleValuePtr) {
680
log_err("%%G Got: %G, Expected: %G\n", *newDoubleValuePtr, myFloat);
683
// u_sprintf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
684
u_sprintf(uStringBuf, NULL, "Char c: %c", 'A');
685
u_sscanf(uStringBuf, NULL, "Char c: %c", myString);
686
if (*myString != 'A') {
687
log_err("%%c Got: %c, Expected: A\n", *myString);
690
u_sprintf(uStringBuf, NULL, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K", L'A');
691
u_sscanf(uStringBuf, NULL, "UChar %%K (non-ANSI, should be %%C for Microsoft?): %K", myUString);
692
if (*myUString != L'A') {
693
log_err("%%C Got: %C, Expected: A\n", *myUString);
696
u_sprintf(uStringBuf, NULL, "String %%s: %s", "My-String");
697
u_sscanf(uStringBuf, NULL, "String %%s: %s", myString);
698
if (strcmp(myString, "My-String")) {
699
log_err("%%s Got: %s, Expected: My-String\n", myString);
701
if (uStringBuf[20] != 0) {
702
log_err("String not terminated. Got %c\n", uStringBuf[20] );
704
u_sprintf(uStringBuf, NULL, "NULL String %%s: %s", NULL);
705
u_sscanf(uStringBuf, NULL, "NULL String %%s: %s", myString);
706
if (strcmp(myString, "(null)")) {
707
log_err("%%s Got: %s, Expected: My-String\n", myString);
710
u_sprintf(uStringBuf, NULL, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", L"My-String");
711
u_sscanf(uStringBuf, NULL, "Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", myUString);
712
u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString));
713
if (strcmp(myString, "My-String")) {
714
log_err("%%U Got: %s, Expected: My String\n", myString);
717
u_sprintf(uStringBuf, NULL, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", NULL);
718
u_sscanf(uStringBuf, NULL, "NULL Unicode String %%U (non-ANSI, should be %%S for Microsoft?): %U", myUString);
719
u_austrncpy(myString, myUString, sizeof(myString)/sizeof(*myString));
720
if (strcmp(myString, "(null)")) {
721
log_err("%%U Got: %s, Expected: (null)\n", myString);
724
u_sprintf(uStringBuf, NULL, "Date %%D (non-ANSI): %D", myDate);
726
u_sscanf(uStringBuf, NULL, "Date %%D (non-ANSI): %D", &myNewDate);
727
if (myNewDate != dec_31_1969) {
728
log_err("%%D Got: %f, Expected: %f\n", myNewDate, dec_31_1969);
731
u_sprintf(uStringBuf, NULL, "Time %%T (non-ANSI): %T", myDate);
733
u_sscanf(uStringBuf, NULL, "Time %%T (non-ANSI): %T", &myNewDate);
734
if (myNewDate != midnight) {
735
log_err("%%T Got: %f, Expected: %f\n", myNewDate, midnight);
738
u_sprintf(uStringBuf, NULL, "Percent %%P (non-ANSI): %P", myFloat);
739
*newDoubleValuePtr = -1.0;
740
u_sscanf(uStringBuf, NULL, "Percent %%P (non-ANSI): %P", newDoubleValuePtr);
741
if (myFloat != *newDoubleValuePtr) {
742
log_err("%%P Got: %P, Expected: %P\n", *newDoubleValuePtr, myFloat);
745
u_sprintf(uStringBuf, NULL, "Currency %%M (non-ANSI): %M", myFloat);
746
*newDoubleValuePtr = -1.0;
747
u_sscanf(uStringBuf, NULL, "Currency %%M (non-ANSI): %M", newDoubleValuePtr);
748
if (myFloat != *newDoubleValuePtr) {
749
log_err("%%P Got: %P, Expected: %P\n", *newDoubleValuePtr, myFloat);
752
u_sprintf(uStringBuf, NULL, "Spell Out %%V (non-ANSI): %V", myFloat);
753
*newDoubleValuePtr = -1.0;
754
u_sscanf(uStringBuf, NULL, "Spell Out %%V (non-ANSI): %V", newDoubleValuePtr);
755
if (myFloat != *newDoubleValuePtr) {
756
log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
760
u_sprintf(uStringBuf, NULL, "\t\nPointer to integer (Count) %%n: n=%d %n n=%d\n", *newValuePtr, newValuePtr, *newValuePtr);
761
if (*newValuePtr != 37) {
762
log_err("%%V Got: %f, Expected: %f\n", *newDoubleValuePtr, myFloat);
766
// u_sscanf(uStringBuf, NULL, "Pointer %%p: %p\n", myFile);
769
#define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) \
770
u_uastrncpy(testStr, "xxxxxxxxxxxxxx", sizeof(testStr)/sizeof(testStr[0]));\
771
size = u_snprintf(testStr, limit, "en_US_POSIX", format, value);\
772
u_austrncpy(cTestResult, testStr, sizeof(cTestResult)/sizeof(cTestResult[0]));\
773
if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
774
log_err("Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s\n",\
775
size, expectedSize, cTestResult, expectedStr);\
778
log_verbose("Got: %s\n", cTestResult);\
782
static void TestSnprintf() {
784
char cTestResult[256];
787
Test_u_snprintf(0, "%d", 123, 0, "xxxxxxxxxxxxxx");
788
Test_u_snprintf(2, "%d", 123, 2, "12xxxxxxxxxxxx");
789
Test_u_snprintf(3, "%d", 123, 3, "123xxxxxxxxxxx");
790
Test_u_snprintf(4, "%d", 123, 3, "123");
792
Test_u_snprintf(0, "%s", "abcd", 0, "xxxxxxxxxxxxxx");
793
Test_u_snprintf(3, "%s", "abcd", 3, "abcxxxxxxxxxxx");
794
Test_u_snprintf(4, "%s", "abcd", 4, "abcdxxxxxxxxxx");
795
Test_u_snprintf(5, "%s", "abcd", 4, "abcd");
797
Test_u_snprintf(0, "%e", 12.34, 0, "xxxxxxxxxxxxxx");
798
Test_u_snprintf(1, "%e", 12.34, 1, "1xxxxxxxxxxxxx");
799
Test_u_snprintf(2, "%e", 12.34, 2, "1.xxxxxxxxxxxx");
800
Test_u_snprintf(3, "%e", 12.34, 3, "1.2xxxxxxxxxxx");
801
Test_u_snprintf(5, "%e", 12.34, 5, "1.234xxxxxxxxx");
802
Test_u_snprintf(6, "%e", 12.34, 6, "1.2340xxxxxxxx");
803
Test_u_snprintf(8, "%e", 12.34, 8, "1.234000xxxxxx");
804
Test_u_snprintf(9, "%e", 12.34, 9, "1.234000exxxxx");
805
Test_u_snprintf(10, "%e", 12.34, 10, "1.234000e+xxxx");
806
Test_u_snprintf(11, "%e", 12.34, 11, "1.234000e+0xxx");
807
Test_u_snprintf(13, "%e", 12.34, 13, "1.234000e+001x");
808
Test_u_snprintf(14, "%e", 12.34, 13, "1.234000e+001");
811
#define TestSPrintFormat(uFormat, uValue, cFormat, cValue) \
812
/* Reinitialize the buffer to verify null termination works. */\
813
u_memset(uBuffer, 0x2a, sizeof(uBuffer)/sizeof(*uBuffer));\
814
memset(buffer, 0x2a, sizeof(buffer)/sizeof(*buffer));\
816
uNumPrinted = u_sprintf(uBuffer, "en_US_POSIX", uFormat, uValue);\
817
u_austrncpy(compBuffer, uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]));\
818
cNumPrinted = sprintf(buffer, cFormat, cValue);\
819
if (strcmp(buffer, compBuffer) != 0) {\
820
log_err("%" uFormat " Got: \"%s\", Expected: \"%s\"\n", compBuffer, buffer);\
822
if (cNumPrinted != uNumPrinted) {\
823
log_err("%" uFormat " number printed Got: %d, Expected: %d\n", uNumPrinted, cNumPrinted);\
827
static void TestSprintfFormat() {
828
static const UChar abcUChars[] = {0x61,0x62,0x63,0};
829
static const char abcChars[] = "abc";
832
char compBuffer[256];
836
TestSPrintFormat("%8U", abcUChars, "%8s", abcChars);
837
TestSPrintFormat("%-8U", abcUChars, "%-8s", abcChars);
839
TestSPrintFormat("%8s", abcChars, "%8s", abcChars);
840
TestSPrintFormat("%-8s", abcChars, "%-8s", abcChars);
842
TestSPrintFormat("%8c", 0x65, "%8c", 0x65);
843
TestSPrintFormat("%-8c", 0x65, "%-8c", 0x65);
845
TestSPrintFormat("%8K", (UChar)0x65, "%8c", (char)0x65);
846
TestSPrintFormat("%-8K", (UChar)0x65, "%-8c", (char)0x65);
848
TestSPrintFormat("%10f", 1.23456789, "%10f", 1.23456789);
849
TestSPrintFormat("%-10f", 1.23456789, "%-10f", 1.23456789);
850
TestSPrintFormat("%10f", 123.456789, "%10f", 123.456789);
851
TestSPrintFormat("%-10f", 123.456789, "%-10f", 123.456789);
853
TestSPrintFormat("%10e", 1.23456789, "%10e", 1.23456789);
854
TestSPrintFormat("%-10e", 1.23456789, "%-10e", 1.23456789);
855
TestSPrintFormat("%10e", 123.456789, "%10e", 123.456789);
856
TestSPrintFormat("%-10e", 123.456789, "%-10e", 123.456789);
858
TestSPrintFormat("%10g", 1.23456789, "%10g", 1.23456789);
859
TestSPrintFormat("%-10g", 1.23456789, "%-10g", 1.23456789);
860
TestSPrintFormat("%10g", 123.456789, "%10g", 123.456789);
861
TestSPrintFormat("%-10g", 123.456789, "%-10g", 123.456789);
863
TestSPrintFormat("%8x", 123456, "%8x", 123456);
864
TestSPrintFormat("%-8x", 123456, "%-8x", 123456);
866
TestSPrintFormat("%8X", 123456, "%8X", 123456);
867
TestSPrintFormat("%-8X", 123456, "%-8X", 123456);
868
TestSPrintFormat("%#x", 123456, "%#x", 123456);
869
TestSPrintFormat("%#x", -123456, "%#x", -123456);
871
TestSPrintFormat("%8o", 123456, "%8o", 123456);
872
TestSPrintFormat("%-8o", 123456, "%-8o", 123456);
873
TestSPrintFormat("%#o", 123, "%#o", 123);
874
TestSPrintFormat("%#o", -123, "%#o", -123);
876
TestSPrintFormat("%8u", 123456, "%8u", 123456);
877
TestSPrintFormat("%-8u", 123456, "%-8u", 123456);
878
TestSPrintFormat("%8u", -123456, "%8u", -123456);
879
TestSPrintFormat("%-8u", -123456, "%-8u", -123456);
881
TestSPrintFormat("%8d", 123456, "%8d", 123456);
882
TestSPrintFormat("%-8d", 123456, "%-8d", 123456);
883
TestSPrintFormat("% d", 123456, "% d", 123456);
884
TestSPrintFormat("% d", -123456, "% d", -123456);
886
TestSPrintFormat("%8i", 123456, "%8i", 123456);
887
TestSPrintFormat("%-8i", 123456, "%-8i", 123456);
889
log_verbose("Get really crazy with the formatting.\n");
891
TestSPrintFormat("%-#12x", 123, "%-#12x", 123);
892
TestSPrintFormat("%-#12x", -123, "%-#12x", -123);
893
TestSPrintFormat("%#12x", 123, "%#12x", 123);
894
TestSPrintFormat("%#12x", -123, "%#12x", -123);
896
TestSPrintFormat("%-+12d", 123, "%-+12d", 123);
897
TestSPrintFormat("%-+12d", -123, "%-+12d", -123);
898
TestSPrintFormat("%- 12d", 123, "%- 12d", 123);
899
TestSPrintFormat("%- 12d", -123, "%- 12d", -123);
900
TestSPrintFormat("%+12d", 123, "%+12d", 123);
901
TestSPrintFormat("%+12d", -123, "%+12d", -123);
902
TestSPrintFormat("% 12d", 123, "% 12d", 123);
903
TestSPrintFormat("% 12d", -123, "% 12d", -123);
904
TestSPrintFormat("%12d", 123, "%12d", 123);
905
TestSPrintFormat("%12d", -123, "%12d", -123);
907
TestSPrintFormat("%-+12.1e", 1.234, "%-+12.1e", 1.234);
908
TestSPrintFormat("%-+12.1e", -1.234, "%-+12.1e", -1.234);
909
TestSPrintFormat("%- 12.1e", 1.234, "%- 12.1e", 1.234);
910
TestSPrintFormat("%- 12.1e", -1.234, "%- 12.1e", -1.234);
911
TestSPrintFormat("%+12.1e", 1.234, "%+12.1e", 1.234);
912
TestSPrintFormat("%+12.1e", -1.234, "%+12.1e", -1.234);
913
TestSPrintFormat("% 12.1e", 1.234, "% 12.1e", 1.234);
914
TestSPrintFormat("% 12.1e", -1.234, "% 12.1e", -1.234);
915
TestSPrintFormat("%12.1e", 1.234, "%12.1e", 1.234);
916
TestSPrintFormat("%12.1e", -1.234, "%12.1e", -1.234);
917
TestSPrintFormat("%.2e", 1.234, "%.2e", 1.234);
918
TestSPrintFormat("%.2e", -1.234, "%.2e", -1.234);
919
TestSPrintFormat("%3e", 1.234, "%3e", 1.234);
920
TestSPrintFormat("%3e", -1.234, "%3e", -1.234);
922
TestSPrintFormat("%-+12.1f", 1.234, "%-+12.1f", 1.234);
923
TestSPrintFormat("%-+12.1f", -1.234, "%-+12.1f", -1.234);
924
TestSPrintFormat("%- 12.1f", 1.234, "%- 12.1f", 1.234);
925
TestSPrintFormat("%- 12.1f", -1.234, "%- 12.1f", -1.234);
926
TestSPrintFormat("%+12.1f", 1.234, "%+12.1f", 1.234);
927
TestSPrintFormat("%+12.1f", -1.234, "%+12.1f", -1.234);
928
TestSPrintFormat("% 12.1f", 1.234, "% 12.1f", 1.234);
929
TestSPrintFormat("% 12.1f", -1.234, "% 12.1f", -1.234);
930
TestSPrintFormat("%12.1f", 1.234, "%12.1f", 1.234);
931
TestSPrintFormat("%12.1f", -1.234, "%12.1f", -1.234);
932
TestSPrintFormat("%.2f", 1.234, "%.2f", 1.234);
933
TestSPrintFormat("%.2f", -1.234, "%.2f", -1.234);
934
TestSPrintFormat("%3f", 1.234, "%3f", 1.234);
935
TestSPrintFormat("%3f", -1.234, "%3f", -1.234);
938
#undef TestSPrintFormat
940
static void TestStringCompatibility() {
941
UChar myUString[256];
942
UChar uStringBuf[256];
943
char myString[256] = "";
944
char testBuf[256] = "";
947
u_memset(myUString, 0x0a, sizeof(myUString)/ sizeof(*myUString));
948
u_memset(uStringBuf, 0x0a, sizeof(uStringBuf) / sizeof(*uStringBuf));
950
/* Compare against C API compatibility */
951
for (num = -STANDARD_TEST_NUM_RANGE; num < STANDARD_TEST_NUM_RANGE; num++) {
952
sprintf(testBuf, "%x", num);
953
u_sprintf(uStringBuf, NULL, "%x", num);
954
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
955
if (strcmp(myString, testBuf) != 0) {
956
log_err("%%x Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
959
sprintf(testBuf, "%X", num);
960
u_sprintf(uStringBuf, NULL, "%X", num);
961
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
962
if (strcmp(myString, testBuf) != 0) {
963
log_err("%%X Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
966
sprintf(testBuf, "%o", num);
967
u_sprintf(uStringBuf, NULL, "%o", num);
968
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
969
if (strcmp(myString, testBuf) != 0) {
970
log_err("%%o Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
973
/* sprintf is not compatible on all platforms e.g. the iSeries*/
974
sprintf(testBuf, "%d", num);
975
u_sprintf(uStringBuf, "en_US_POSIX", "%d", num);
976
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
977
if (strcmp(myString, testBuf) != 0) {
978
log_err("%%d Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
981
sprintf(testBuf, "%i", num);
982
u_sprintf(uStringBuf, "en_US_POSIX", "%i", num);
983
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
984
if (strcmp(myString, testBuf) != 0) {
985
log_err("%%i Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
988
sprintf(testBuf, "%f", (double)num);
989
u_sprintf(uStringBuf, "en_US_POSIX", "%f", (double)num);
990
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
991
if (strcmp(myString, testBuf) != 0) {
992
log_err("%%f Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
995
sprintf(testBuf, "%e", (double)num);
996
u_sprintf(uStringBuf, "en_US_POSIX", "%e", (double)num);
997
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
998
if (strcmp(myString, testBuf) != 0) {
999
log_err("%%e Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1002
sprintf(testBuf, "%E", (double)num);
1003
u_sprintf(uStringBuf, "en_US_POSIX", "%E", (double)num);
1004
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1005
if (strcmp(myString, testBuf) != 0) {
1006
log_err("%%E Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1009
sprintf(testBuf, "%g", (double)num);
1010
u_sprintf(uStringBuf, "en_US_POSIX", "%g", (double)num);
1011
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1012
if (strcmp(myString, testBuf) != 0) {
1013
log_err("%%g Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1016
sprintf(testBuf, "%G", (double)num);
1017
u_sprintf(uStringBuf, "en_US_POSIX", "%G", (double)num);
1018
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1019
if (strcmp(myString, testBuf) != 0) {
1020
log_err("%%G Got: \"%s\", Expected: \"%s\"\n", myString, testBuf);
1024
for (num = 0; num < 0x80; num++) {
1025
testBuf[0] = (char)0xFF;
1026
uStringBuf[0] = (UChar)0xfffe;
1027
sprintf(testBuf, "%c", num);
1028
u_sprintf(uStringBuf, NULL, "%c", num);
1029
u_austrncpy(myString, uStringBuf, sizeof(myString)/sizeof(myString[0]));
1030
if (testBuf[0] != uStringBuf[0] || uStringBuf[0] != num) {
1031
log_err("%%c Got: 0x%x, Expected: 0x%x\n", myString[0], testBuf[0]);
1036
static void TestStream() {
1037
#if U_IOSTREAM_SOURCE >= 198506
1038
char testStreamBuf[512];
1039
const char *testStr = "Beginning of test str1 <<432 1" C_NEW_LINE " UTF-8 \xCE\xBC\xF0\x90\x80\x81\xF0\x90\x80\x82";
1040
ostrstream outTestStream(testStreamBuf, sizeof(testStreamBuf));
1041
istrstream inTestStream(" tHis\xCE\xBC\xE2\x80\x82 mu world", 0);
1042
const UChar thisMu[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0};
1043
const UChar mu[] = { 0x6D, 0x75, 0};
1044
UnicodeString str1 = UNICODE_STRING_SIMPLE("str1");
1045
UnicodeString str2 = UNICODE_STRING_SIMPLE(" <<");
1046
UnicodeString str3 = UNICODE_STRING_SIMPLE("4");
1047
UnicodeString str4 = UNICODE_STRING_SIMPLE(" UTF-8 ");
1048
UnicodeString inStr = UNICODE_STRING_SIMPLE(" UTF-8 ");
1049
UnicodeString inStr2;
1050
char defConvName[UCNV_MAX_CONVERTER_NAME_LENGTH*2];
1052
UErrorCode status = U_ZERO_ERROR;
1053
UConverter *defConv;
1055
str4.append((UChar32)0x03BC); /* mu */
1056
str4.append((UChar32)0x10001);
1057
str4.append((UChar32)0x10002);
1059
/* release the default converter and use utf-8 for a bit */
1060
defConv = u_getDefaultConverter(&status);
1061
if (U_FAILURE(status)) {
1062
log_err("Can't get default converter");
1065
ucnv_close(defConv);
1066
strncpy(defConvName, ucnv_getDefaultName(), sizeof(defConvName)/sizeof(defConvName[0]));
1067
ucnv_setDefaultName("UTF-8");
1069
outTestStream << "Beginning of test ";
1070
outTestStream << str1 << " " << str2 << str3 << 3 << "2 " << 1.0 << C_NEW_LINE << str4 << ends;
1071
if (strcmp(testStreamBuf, testStr) != 0) {
1072
log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf, testStr);
1075
inTestStream >> inStr >> inStr2;
1076
if (inStr.compare(thisMu) != 0) {
1077
u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
1078
inStrC[inStr.length()] = 0;
1079
log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC);
1081
if (inStr2.compare(mu) != 0) {
1082
u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
1083
inStrC[inStr.length()] = 0;
1084
log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC);
1087
/* return the default converter to the original state. */
1088
ucnv_setDefaultName(defConvName);
1089
defConv = u_getDefaultConverter(&status);
1090
if (U_FAILURE(status)) {
1091
log_err("Can't get default converter");
1094
ucnv_close(defConv);
1096
log_err("U_IOSTREAM_SOURCE is disabled");
1100
static void TestTranslitOps()
1103
UErrorCode err = U_ZERO_ERROR;
1104
UTransliterator *a = NULL, *b = NULL, *c = NULL;
1106
log_verbose("opening a transliterator and UFILE for testing\n");
1108
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL);
1111
log_err("Couldn't open test file for writing");
1115
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
1118
log_err("Err opening transliterator %s\n", u_errorName(err));
1124
log_verbose("setting a transliterator\n");
1125
b = u_fsettransliterator(f, U_WRITE, a, &err);
1128
log_err("Err setting transliterator %s\n", u_errorName(err));
1135
log_err("Err, a transliterator was already set!\n");
1138
log_verbose("un-setting transliterator (setting to null)\n");
1139
c = u_fsettransliterator(f, U_WRITE, NULL, &err);
1142
log_err("Err setting transliterator %s\n", u_errorName(err));
1149
log_err("Err, transliterator that came back was not the original one.\n");
1152
log_verbose("Trying to set read transliterator (should fail)\n");
1153
b = u_fsettransliterator(f, U_READ, NULL, &err);
1154
if(err != U_UNSUPPORTED_ERROR)
1156
log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err));
1162
log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err));
1171
static void TestTranslitOut()
1174
UErrorCode err = U_ZERO_ERROR;
1175
UTransliterator *a = NULL, *b = NULL, *c = NULL;
1177
UChar compare[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x0000 };
1181
log_verbose("opening a transliterator and UFILE for testing\n");
1183
f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "utf-16");
1186
log_err("Couldn't open test file for writing");
1190
a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err);
1193
log_err("Err opening transliterator %s\n", u_errorName(err));
1198
log_verbose("setting a transliterator\n");
1199
b = u_fsettransliterator(f, U_WRITE, a, &err);
1202
log_err("Err setting transliterator %s\n", u_errorName(err));
1209
log_err("Err, a transliterator was already set!\n");
1212
u_fprintf(f, "%KStephanos", 0xFEFF);
1216
log_verbose("Re reading test file to verify transliteration\n");
1217
infile = fopen(STANDARD_TEST_FILE, "rb");
1220
log_err("Couldn't reopen test file\n");
1224
len=fread(ubuf, sizeof(UChar), u_strlen(compare), infile);
1225
log_verbose("Read %d UChars\n", len);
1226
if(len != u_strlen(compare))
1228
log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare), len);
1232
if(u_strlen(compare) != u_strlen(ubuf))
1234
log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), len);
1237
if(u_strcmp(compare, ubuf))
1239
log_err("Read string doesn't match expected.\n");
1243
log_verbose("Read string matches expected.\n");
1250
static void addAllTests(TestNode** root) {
1251
addTest(root, &TestFile, "file/TestFile");
1252
addTest(root, &TestCodepage, "file/TestCodepage");
1253
addTest(root, &TestFprintfFormat, "file/TestFprintfFormat");
1254
addTest(root, &TestFilePrintCompatibility, "file/TestFilePrintCompatibility");
1255
addTest(root, &TestString, "string/TestString");
1256
addTest(root, &TestSprintfFormat, "string/TestSprintfFormat");
1257
addTest(root, &TestSnprintf, "string/TestSnprintf");
1258
addTest(root, &TestStringCompatibility, "string/TestStringCompatibility");
1259
addTest(root, &TestStream, "stream/TestStream");
1261
addTest(root, &TestTranslitOps, "translit/ops");
1262
addTest(root, &TestTranslitOut, "translit/out");
1265
int main(int argc, char* argv[])
1267
int32_t nerrors = 0;
1268
TestNode *root = NULL;
1271
nerrors = processArgs(root, argc, argv);
1273
cleanUpTestTree(root);