2 **********************************************************************
3 * Copyright (C) 2002-2006, 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/uclean.h"
19 #include "unicode/ucnv.h"
20 #include "unicode/uchar.h"
21 #include "unicode/unistr.h"
22 #include "unicode/ustring.h"
25 #include "unicode/tstdtmod.h"
31 class DataDrivenLogger
: public TestLog
{
32 static const char* fgDataDir
;
33 static char *fgTestDataPath
;
36 static void cleanUp() {
39 fgTestDataPath
= NULL
;
42 virtual void errln( const UnicodeString
&message
) {
44 message
.extract(0, message
.length(), buffer
, sizeof(buffer
));
45 buffer
[3999] = 0; /* NULL terminate */
49 static const char * pathToDataDirectory(void)
52 if(fgDataDir
!= NULL
) {
56 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
57 // to point to the top of the build hierarchy, which may or
58 // may not be the same as the source directory, depending on
59 // the configure options used. At any rate,
60 // set the data path to the built data from this directory.
61 // The value is complete with quotes, so it can be used
62 // as-is as a string constant.
64 #if defined (U_TOPSRCDIR)
66 fgDataDir
= U_TOPSRCDIR U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
70 /* On Windows, the file name obtained from __FILE__ includes a full path.
71 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
72 * Change to "wherever\icu\source\data"
75 static char p
[sizeof(__FILE__
) + 10];
80 /* We want to back over three '\' chars. */
81 /* Only Windows should end up here, so looking for '\' is safe. */
82 for (i
=1; i
<=3; i
++) {
83 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
84 if (pBackSlash
!= NULL
) {
85 *pBackSlash
= 0; /* Truncate the string at the '\' */
89 if (pBackSlash
!= NULL
) {
90 /* We found and truncated three names from the path.
91 * Now append "source\data" and set the environment
93 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
);
97 /* __FILE__ on MSVC7 does not contain the directory */
98 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
101 fgDataDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
104 fgDataDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
114 static const char* loadTestData(UErrorCode
& err
){
115 if( fgTestDataPath
== NULL
){
116 const char* directory
=NULL
;
117 UResourceBundle
* test
=NULL
;
119 const char* tdrelativepath
;
121 #if defined (U_TOPBUILDDIR)
122 tdrelativepath
= "test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
123 directory
= U_TOPBUILDDIR
;
125 tdrelativepath
= ".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
126 directory
= pathToDataDirectory();
129 tdpath
= (char*) malloc(sizeof(char) *(( strlen(directory
) * strlen(tdrelativepath
)) + 100));
132 /* u_getDataDirectory shoul return \source\data ... set the
133 * directory to ..\source\data\..\test\testdata\out\testdata
135 strcpy(tdpath
, directory
);
136 strcat(tdpath
, tdrelativepath
);
137 strcat(tdpath
,"testdata");
139 test
=ures_open(tdpath
, "testtypes", &err
);
142 err
= U_FILE_ACCESS_ERROR
;
143 log_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath
, u_errorName(err
));
147 fgTestDataPath
= tdpath
;
149 return fgTestDataPath
;
152 virtual const char* getTestDataPath(UErrorCode
& err
) {
153 return loadTestData(err
);
157 const char* DataDrivenLogger::fgDataDir
= NULL
;
158 char* DataDrivenLogger::fgTestDataPath
= NULL
;
161 uto64(const UChar
*buffer
)
164 /* iterate through buffer */
166 /* read the next digit */
168 if (!u_isxdigit(*buffer
)) {
169 log_err("\\u%04X is not a valid hex digit for this test\n", (UChar
)*buffer
);
171 result
+= *buffer
- 0x0030 - (*buffer
>= 0x0041 ? (*buffer
>= 0x0061 ? 39 : 7) : 0);
179 static void U_CALLCONV
DataDrivenPrintf(void)
181 #if !UCONFIG_NO_FORMATTING
182 UErrorCode errorCode
;
183 TestDataModule
*dataModule
;
185 const DataMap
*testCase
;
186 DataDrivenLogger logger
;
189 char cFormat
[sizeof(cBuffer
)];
190 char cExpected
[sizeof(cBuffer
)];
191 UnicodeString tempStr
;
193 UChar expectedResult
[512];
201 int32_t uBufferLenReturned
;
203 const char *fileLocale
= "en_US_POSIX";
204 int32_t uFileBufferLenReturned
;
207 errorCode
=U_ZERO_ERROR
;
208 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
209 if(U_SUCCESS(errorCode
)) {
210 testData
=dataModule
->createTestData("printf", errorCode
);
211 if(U_SUCCESS(errorCode
)) {
212 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
213 if(U_FAILURE(errorCode
)) {
214 log_err("error retrieving icuio/printf test case %d - %s\n",
215 i
, u_errorName(errorCode
));
216 errorCode
=U_ZERO_ERROR
;
219 testFile
= u_fopen(STANDARD_TEST_FILE
, "w", fileLocale
, "UTF-8");
221 log_err("Can't open test file - %s\n",
225 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
226 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
227 tempStr
=testCase
->getString("format", errorCode
);
228 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
229 tempStr
=testCase
->getString("result", errorCode
);
230 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
231 tempStr
=testCase
->getString("argument", errorCode
);
232 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
233 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
234 if(U_FAILURE(errorCode
)) {
235 log_err("error retrieving icuio/printf test case %d - %s\n",
236 i
, u_errorName(errorCode
));
237 errorCode
=U_ZERO_ERROR
;
240 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
241 switch (testCase
->getString("argumentType", errorCode
)[0]) {
242 case 0x64: // 'd' double
243 dbl
= atof(u_austrcpy(cBuffer
, argument
));
244 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, dbl
);
245 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, dbl
);
247 case 0x31: // '1' int8_t
248 i8
= (int8_t)uto64(argument
);
249 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i8
);
250 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i8
);
252 case 0x32: // '2' int16_t
253 i16
= (int16_t)uto64(argument
);
254 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i16
);
255 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i16
);
257 case 0x34: // '4' int32_t
258 i32
= (int32_t)uto64(argument
);
259 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i32
);
260 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i32
);
262 case 0x38: // '8' int64_t
263 i64
= uto64(argument
);
264 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i64
);
265 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i64
);
267 case 0x73: // 's' char *
268 u_austrncpy(cBuffer
, argument
, sizeof(cBuffer
));
269 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, cBuffer
);
270 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, cBuffer
);
272 case 0x53: // 'S' UChar *
273 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, argument
);
274 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, argument
);
277 uBufferLenReturned
= 0;
278 uFileBufferLenReturned
= 0;
279 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
281 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
282 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
283 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
284 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
285 cBuffer
[sizeof(cBuffer
)-1] = 0;
286 log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
287 i
, cFormat
, cBuffer
, cExpected
);
289 if (uBuffer
[uBufferLenReturned
-1] == 0
290 || uBuffer
[uBufferLenReturned
] != 0
291 || uBuffer
[uBufferLenReturned
+1] != 0x2A
292 || uBuffer
[uBufferLenReturned
+2] != 0x2A)
294 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
295 cBuffer
[sizeof(cBuffer
)-1] = 0;
296 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
297 i
, cBuffer
, uBufferLenReturned
);
300 testFile
= u_fopen(STANDARD_TEST_FILE
, "r", fileLocale
, "UTF-8");
302 log_err("Can't open test file - %s\n",
306 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(uBuffer
[0]), testFile
);
307 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
308 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
309 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
310 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
311 cBuffer
[sizeof(cBuffer
)-1] = 0;
312 log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
313 i
, cFormat
, cBuffer
, cExpected
);
315 if (uFileBufferLenReturned
!= uBufferLenReturned
)
317 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
318 cBuffer
[sizeof(cBuffer
)-1] = 0;
319 log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n",
320 uFileBufferLenReturned
, uBufferLenReturned
);
323 if(U_FAILURE(errorCode
)) {
324 log_err("error running icuio/printf test case %d - %s\n",
325 i
, u_errorName(errorCode
));
326 errorCode
=U_ZERO_ERROR
;
336 log_err("Failed: could not load test icuio data\n");
343 static void U_CALLCONV
DataDrivenScanf(void)
345 #if !UCONFIG_NO_FORMATTING
346 UErrorCode errorCode
;
347 TestDataModule
*dataModule
;
349 const DataMap
*testCase
;
350 DataDrivenLogger logger
;
353 char cExpected
[sizeof(cBuffer
)];
354 UnicodeString tempStr
;
356 UChar expectedResult
[512];
359 int8_t i8
, expected8
;
360 int16_t i16
, expected16
;
361 int32_t i32
, expected32
;
362 int64_t i64
, expected64
;
363 double dbl
, expectedDbl
;
364 volatile float flt
, expectedFlt
; // Use volatile in order to get around an Intel compiler issue.
365 int32_t uBufferLenReturned
;
367 //const char *fileLocale = "en_US_POSIX";
368 //int32_t uFileBufferLenReturned;
371 errorCode
=U_ZERO_ERROR
;
372 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
373 if(U_SUCCESS(errorCode
)) {
374 testData
=dataModule
->createTestData("scanf", errorCode
);
375 if(U_SUCCESS(errorCode
)) {
376 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
377 if(U_FAILURE(errorCode
)) {
378 log_err("error retrieving icuio/printf test case %d - %s\n",
379 i
, u_errorName(errorCode
));
380 errorCode
=U_ZERO_ERROR
;
383 /* testFile = u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8");
385 log_err("Can't open test file - %s\n",
388 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
389 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
390 tempStr
=testCase
->getString("format", errorCode
);
391 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
392 tempStr
=testCase
->getString("result", errorCode
);
393 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
394 tempStr
=testCase
->getString("argument", errorCode
);
395 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
396 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
397 if(U_FAILURE(errorCode
)) {
398 log_err("error retrieving icuio/printf test case %d - %s\n",
399 i
, u_errorName(errorCode
));
400 errorCode
=U_ZERO_ERROR
;
403 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
404 switch (testCase
->getString("argumentType", errorCode
)[0]) {
405 case 0x64: // 'd' double
406 expectedDbl
= atof(u_austrcpy(cBuffer
, expectedResult
));
407 uBufferLenReturned
= u_sscanf_u(argument
, format
, &dbl
);
408 //uFileBufferLenReturned = u_fscanf_u(testFile, format, dbl);
409 if (dbl
!= expectedDbl
) {
410 log_err("error in scanf test case[%d] Got: %f Exp: %f\n",
411 i
, dbl
, expectedDbl
);
414 case 0x66: // 'f' float
415 expectedFlt
= (float)atof(u_austrcpy(cBuffer
, expectedResult
));
416 uBufferLenReturned
= u_sscanf_u(argument
, format
, &flt
);
417 //uFileBufferLenReturned = u_fscanf_u(testFile, format, flt);
418 if (flt
!= expectedFlt
) {
419 log_err("error in scanf test case[%d] Got: %f Exp: %f\n",
420 i
, flt
, expectedFlt
);
423 case 0x31: // '1' int8_t
424 expected8
= (int8_t)uto64(expectedResult
);
425 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i8
);
426 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i8);
427 if (i8
!= expected8
) {
428 log_err("error in scanf test case[%d] Got: %02X Exp: %02X\n",
432 case 0x32: // '2' int16_t
433 expected16
= (int16_t)uto64(expectedResult
);
434 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i16
);
435 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i16);
436 if (i16
!= expected16
) {
437 log_err("error in scanf test case[%d] Got: %04X Exp: %04X\n",
441 case 0x34: // '4' int32_t
442 expected32
= (int32_t)uto64(expectedResult
);
443 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i32
);
444 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i32);
445 if (i32
!= expected32
) {
446 log_err("error in scanf test case[%d] Got: %08X Exp: %08X\n",
450 case 0x38: // '8' int64_t
451 expected64
= uto64(expectedResult
);
452 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i64
);
453 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i64);
454 if (i64
!= expected64
) {
455 log_err("error in scanf 64-bit. Test case = %d\n", i
);
458 case 0x73: // 's' char *
459 u_austrcpy(cExpected
, expectedResult
);
460 uBufferLenReturned
= u_sscanf_u(argument
, format
, cBuffer
);
461 //uFileBufferLenReturned = u_fscanf_u(testFile, format, cBuffer);
462 if (strcmp(cBuffer
, cExpected
) != 0) {
463 log_err("error in scanf char * string. Got \"%s\" Expected \"%s\". Test case = %d\n", cBuffer
, cExpected
, i
);
466 case 0x53: // 'S' UChar *
467 uBufferLenReturned
= u_sscanf_u(argument
, format
, uBuffer
);
468 //uFileBufferLenReturned = u_fscanf_u(testFile, format, argument);
469 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
470 u_austrcpy(cExpected
, format
);
471 u_austrcpy(cBuffer
, uBuffer
);
472 log_err("error in scanf UChar * string %s Got: \"%s\". Test case = %d\n", cExpected
, cBuffer
, i
);
476 uBufferLenReturned
= 0;
477 //uFileBufferLenReturned = 0;
478 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
480 if (uBufferLenReturned
!= 1) {
481 log_err("error scanf converted %d arguments. Test case = %d\n", uBufferLenReturned
, i
);
483 /* if (u_strcmp(uBuffer, expectedResult) != 0) {
484 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
485 u_austrncpy(cFormat, format, sizeof(cFormat));
486 u_austrncpy(cExpected, expectedResult, sizeof(cExpected));
487 cBuffer[sizeof(cBuffer)-1] = 0;
488 log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
489 i, cFormat, cBuffer, cExpected);
491 if (uBuffer[uBufferLenReturned-1] == 0
492 || uBuffer[uBufferLenReturned] != 0
493 || uBuffer[uBufferLenReturned+1] != 0x2A
494 || uBuffer[uBufferLenReturned+2] != 0x2A)
496 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
497 cBuffer[sizeof(cBuffer)-1] = 0;
498 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
499 i, cBuffer, uBufferLenReturned);
501 /* u_fclose(testFile);
502 testFile = u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8");
504 log_err("Can't open test file - %s\n",
508 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile);
509 if (u_strcmp(uBuffer, expectedResult) != 0) {
510 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
511 u_austrncpy(cFormat, format, sizeof(cFormat));
512 u_austrncpy(cExpected, expectedResult, sizeof(cExpected));
513 cBuffer[sizeof(cBuffer)-1] = 0;
514 log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
515 i, cFormat, cBuffer, cExpected);
517 if (uFileBufferLenReturned != uBufferLenReturned)
519 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
520 cBuffer[sizeof(cBuffer)-1] = 0;
521 log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n",
522 uFileBufferLenReturned, uBufferLenReturned);
525 if(U_FAILURE(errorCode
)) {
526 log_err("error running icuio/printf test case %d - %s\n",
527 i
, u_errorName(errorCode
));
528 errorCode
=U_ZERO_ERROR
;
531 // u_fclose(testFile);
538 log_err("Failed: could not load test icuio data\n");
545 static void U_CALLCONV
DataDrivenPrintfPrecision(void)
547 #if !UCONFIG_NO_FORMATTING
548 UErrorCode errorCode
;
549 TestDataModule
*dataModule
;
551 const DataMap
*testCase
;
552 DataDrivenLogger logger
;
555 char cFormat
[sizeof(cBuffer
)];
556 char cExpected
[sizeof(cBuffer
)];
557 UnicodeString tempStr
;
559 UChar expectedResult
[512];
568 int32_t uBufferLenReturned
;
570 errorCode
=U_ZERO_ERROR
;
571 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
572 if(U_SUCCESS(errorCode
)) {
573 testData
=dataModule
->createTestData("printfPrecision", errorCode
);
574 if(U_SUCCESS(errorCode
)) {
575 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
576 if(U_FAILURE(errorCode
)) {
577 log_err("error retrieving icuio/printf test case %d - %s\n",
578 i
, u_errorName(errorCode
));
579 errorCode
=U_ZERO_ERROR
;
582 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
583 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
584 tempStr
=testCase
->getString("format", errorCode
);
585 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
586 tempStr
=testCase
->getString("result", errorCode
);
587 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
588 tempStr
=testCase
->getString("argument", errorCode
);
589 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
590 precision
=testCase
->getInt28("precision", errorCode
);
591 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
592 if(U_FAILURE(errorCode
)) {
593 log_err("error retrieving icuio/printf test case %d - %s\n",
594 i
, u_errorName(errorCode
));
595 errorCode
=U_ZERO_ERROR
;
598 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
599 switch (testCase
->getString("argumentType", errorCode
)[0]) {
600 case 0x64: // 'd' double
601 dbl
= atof(u_austrcpy(cBuffer
, argument
));
602 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, dbl
);
604 case 0x31: // '1' int8_t
605 i8
= (int8_t)uto64(argument
);
606 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i8
);
608 case 0x32: // '2' int16_t
609 i16
= (int16_t)uto64(argument
);
610 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i16
);
612 case 0x34: // '4' int32_t
613 i32
= (int32_t)uto64(argument
);
614 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i32
);
616 case 0x38: // '8' int64_t
617 i64
= uto64(argument
);
618 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i64
);
620 case 0x73: // 's' char *
621 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
622 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, cBuffer
);
624 case 0x53: // 'S' UChar *
625 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, argument
);
628 uBufferLenReturned
= 0;
629 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
631 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
632 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
633 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
634 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
635 cBuffer
[sizeof(cBuffer
)-1] = 0;
636 log_err("FAILURE test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
637 i
, cFormat
, cBuffer
, cExpected
);
639 if (uBuffer
[uBufferLenReturned
-1] == 0
640 || uBuffer
[uBufferLenReturned
] != 0
641 || uBuffer
[uBufferLenReturned
+1] != 0x2A
642 || uBuffer
[uBufferLenReturned
+2] != 0x2A)
644 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
645 cBuffer
[sizeof(cBuffer
)-1] = 0;
646 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
647 i
, cBuffer
, uBufferLenReturned
);
649 if(U_FAILURE(errorCode
)) {
650 log_err("error running icuio/printf test case %d - %s\n",
651 i
, u_errorName(errorCode
));
652 errorCode
=U_ZERO_ERROR
;
661 log_err("Failed: could not load test icuio data\n");
667 static void addAllTests(TestNode
** root
) {
670 addTranslitTest(root
);
672 #if !UCONFIG_NO_FORMATTING
673 addTest(root
, &DataDrivenPrintf
, "datadriv/DataDrivenPrintf");
674 addTest(root
, &DataDrivenPrintfPrecision
, "datadriv/DataDrivenPrintfPrecision");
675 addTest(root
, &DataDrivenScanf
, "datadriv/DataDrivenScanf");
677 addStreamTests(root
);
680 /* returns the path to icu/source/data/out */
681 static const char *ctest_dataOutDir()
683 static const char *dataOutDir
= NULL
;
689 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
690 // to point to the top of the build hierarchy, which may or
691 // may not be the same as the source directory, depending on
692 // the configure options used. At any rate,
693 // set the data path to the built data from this directory.
694 // The value is complete with quotes, so it can be used
695 // as-is as a string constant.
697 #if defined (U_TOPBUILDDIR)
699 dataOutDir
= U_TOPBUILDDIR
"data"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
703 /* On Windows, the file name obtained from __FILE__ includes a full path.
704 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
705 * Change to "wherever\icu\source\data"
708 static char p
[sizeof(__FILE__
) + 20];
713 /* We want to back over three '\' chars. */
714 /* Only Windows should end up here, so looking for '\' is safe. */
715 for (i
=1; i
<=3; i
++) {
716 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
717 if (pBackSlash
!= NULL
) {
718 *pBackSlash
= 0; /* Truncate the string at the '\' */
722 if (pBackSlash
!= NULL
) {
723 /* We found and truncated three names from the path.
724 * Now append "source\data" and set the environment
726 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
);
730 /* __FILE__ on MSVC7 does not contain the directory */
731 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
734 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
737 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
746 /* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
747 * set, try to deduce the directory in which ICU was built,
748 * and set ICU_DATA to "icu/source/data" in that location.
749 * The intent is to allow the tests to have a good chance
750 * of running without requiring that the user manually set
751 * ICU_DATA. Common data isn't a problem, since it is
752 * picked up via a static (build time) reference, but the
753 * tests dynamically load some data.
755 static void ctest_setICU_DATA() {
757 /* No location for the data dir was identifiable.
758 * Add other fallbacks for the test data location here if the need arises
760 if (getenv("ICU_DATA") == NULL
) {
761 /* If ICU_DATA isn't set, set it to the usual location */
762 u_setDataDirectory(ctest_dataOutDir());
766 int main(int argc
, char* argv
[])
769 TestNode
*root
= NULL
;
770 UErrorCode errorCode
= U_ZERO_ERROR
;
771 UDate startTime
, endTime
;
774 startTime
= uprv_getUTCtime();
776 /* Check whether ICU will initialize without forcing the build data directory into
777 * the ICU_DATA path. Success here means either the data dll contains data, or that
778 * this test program was run with ICU_DATA set externally. Failure of this check
779 * is normal when ICU data is not packaged into a shared library.
781 * Whether or not this test succeeds, we want to cleanup and reinitialize
782 * with a data path so that data loading from individual files can be tested.
785 if (U_FAILURE(errorCode
)) {
787 "#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
790 errorCode
= U_ZERO_ERROR
;
793 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
795 if (U_FAILURE(errorCode
)) {
797 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
798 "*** Check the ICU_DATA environment variable and \n"
799 "*** check that the data files are present.\n", argv
[0], u_errorName(errorCode
));
803 fprintf(stdout
, "Default charset for this run is %s\n", ucnv_getDefaultName());
806 nerrors
= processArgs(root
, argc
, argv
);
810 FILE* fileToRemove
= fopen(STANDARD_TEST_FILE
, "r");
811 /* This should delete any temporary files. */
813 fclose(fileToRemove
);
814 if (remove(STANDARD_TEST_FILE
) != 0) {
815 /* Maybe someone didn't close the file correctly. */
816 fprintf(stderr
, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE
);
823 cleanUpTestTree(root
);
824 DataDrivenLogger::cleanUp();
827 endTime
= uprv_getUTCtime();
828 diffTime
= (int32_t)(endTime
- startTime
);
829 printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
830 (int)((diffTime%U_MILLIS_PER_DAY
)/U_MILLIS_PER_HOUR
),
831 (int)((diffTime%U_MILLIS_PER_HOUR
)/U_MILLIS_PER_MINUTE
),
832 (int)((diffTime%U_MILLIS_PER_MINUTE
)/U_MILLIS_PER_SECOND
),
833 (int)(diffTime%U_MILLIS_PER_SECOND
));