2 **********************************************************************
3 * Copyright (C) 2002-2004, 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/uchar.h"
22 #include "unicode/unistr.h"
23 #include "unicode/ustring.h"
26 #include "unicode/tstdtmod.h"
28 #if U_IOSTREAM_SOURCE >= 199711
34 // <strstream> is deprecated on some platforms, and the compiler complains very loudly if you use it.
38 #elif U_IOSTREAM_SOURCE >= 198506
40 #include <strstream.h>
48 const UChar NEW_LINE
[] = {0x0d,0x0a,0};
49 const char C_NEW_LINE
[] = {0x0d,0x0a,0};
50 #define UTF8_NEW_LINE "\x0d\x0a"
52 const UChar NEW_LINE
[] = {0x0a,0};
53 const char C_NEW_LINE
[] = {'\n',0};
54 #define UTF8_NEW_LINE "\x0a"
58 class DataDrivenLogger
: public TestLog
{
59 static const char* fgDataDir
;
60 static char *fgTestDataPath
;
63 static void cleanUp() {
66 fgTestDataPath
= NULL
;
69 virtual void errln( const UnicodeString
&message
) {
71 message
.extract(0, message
.length(), buffer
, sizeof(buffer
));
72 buffer
[3999] = 0; /* NULL terminate */
76 static const char * pathToDataDirectory(void)
79 if(fgDataDir
!= NULL
) {
83 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
84 // to point to the top of the build hierarchy, which may or
85 // may not be the same as the source directory, depending on
86 // the configure options used. At any rate,
87 // set the data path to the built data from this directory.
88 // The value is complete with quotes, so it can be used
89 // as-is as a string constant.
91 #if defined (U_TOPSRCDIR)
93 fgDataDir
= U_TOPSRCDIR U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
97 /* On Windows, the file name obtained from __FILE__ includes a full path.
98 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
99 * Change to "wherever\icu\source\data"
102 static char p
[sizeof(__FILE__
) + 10];
107 /* We want to back over three '\' chars. */
108 /* Only Windows should end up here, so looking for '\' is safe. */
109 for (i
=1; i
<=3; i
++) {
110 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
111 if (pBackSlash
!= NULL
) {
112 *pBackSlash
= 0; /* Truncate the string at the '\' */
116 if (pBackSlash
!= NULL
) {
117 /* We found and truncated three names from the path.
118 * Now append "source\data" and set the environment
120 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
);
124 /* __FILE__ on MSVC7 does not contain the directory */
125 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
128 fgDataDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
131 fgDataDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
141 static const char* loadTestData(UErrorCode
& err
){
142 if( fgTestDataPath
== NULL
){
143 const char* directory
=NULL
;
144 UResourceBundle
* test
=NULL
;
146 const char* tdrelativepath
;
148 #if defined (U_TOPBUILDDIR)
149 tdrelativepath
= "test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
150 directory
= U_TOPBUILDDIR
;
152 tdrelativepath
= ".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
153 directory
= pathToDataDirectory();
156 tdpath
= (char*) malloc(sizeof(char) *(( strlen(directory
) * strlen(tdrelativepath
)) + 100));
159 /* u_getDataDirectory shoul return \source\data ... set the
160 * directory to ..\source\data\..\test\testdata\out\testdata
162 strcpy(tdpath
, directory
);
163 strcat(tdpath
, tdrelativepath
);
164 strcat(tdpath
,"testdata");
166 test
=ures_open(tdpath
, "testtypes", &err
);
169 err
= U_FILE_ACCESS_ERROR
;
170 log_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath
, u_errorName(err
));
174 fgTestDataPath
= tdpath
;
176 return fgTestDataPath
;
179 virtual const char* getTestDataPath(UErrorCode
& err
) {
180 return loadTestData(err
);
184 const char* DataDrivenLogger::fgDataDir
= NULL
;
185 char* DataDrivenLogger::fgTestDataPath
= NULL
;
188 uto64(const UChar
*buffer
)
191 /* iterate through buffer */
193 /* read the next digit */
195 if (!u_isxdigit(*buffer
)) {
196 log_err("\\u%04X is not a valid hex digit for this test\n", (UChar
)*buffer
);
198 result
+= *buffer
- 0x0030 - (*buffer
>= 0x0041 ? (*buffer
>= 0x0061 ? 39 : 7) : 0);
206 static void U_CALLCONV
DataDrivenPrintf(void)
208 #if !UCONFIG_NO_FORMATTING
209 UErrorCode errorCode
;
210 TestDataModule
*dataModule
;
212 const DataMap
*testCase
;
213 DataDrivenLogger logger
;
216 char cFormat
[sizeof(cBuffer
)];
217 char cExpected
[sizeof(cBuffer
)];
218 UnicodeString tempStr
;
220 UChar expectedResult
[512];
228 int32_t uBufferLenReturned
;
230 const char *fileLocale
= "en_US_POSIX";
231 int32_t uFileBufferLenReturned
;
234 errorCode
=U_ZERO_ERROR
;
235 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
236 if(U_SUCCESS(errorCode
)) {
237 testData
=dataModule
->createTestData("printf", errorCode
);
238 if(U_SUCCESS(errorCode
)) {
239 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
240 if(U_FAILURE(errorCode
)) {
241 log_err("error retrieving icuio/printf test case %d - %s\n",
242 i
, u_errorName(errorCode
));
243 errorCode
=U_ZERO_ERROR
;
246 testFile
= u_fopen(STANDARD_TEST_FILE
, "w", fileLocale
, "UTF-8");
248 log_err("Can't open test file - %s\n",
252 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
253 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
254 tempStr
=testCase
->getString("format", errorCode
);
255 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
256 tempStr
=testCase
->getString("result", errorCode
);
257 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
258 tempStr
=testCase
->getString("argument", errorCode
);
259 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
260 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
261 if(U_FAILURE(errorCode
)) {
262 log_err("error retrieving icuio/printf test case %d - %s\n",
263 i
, u_errorName(errorCode
));
264 errorCode
=U_ZERO_ERROR
;
267 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
268 switch (testCase
->getString("argumentType", errorCode
)[0]) {
269 case 0x64: // 'd' double
270 dbl
= atof(u_austrcpy(cBuffer
, argument
));
271 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, dbl
);
272 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, dbl
);
274 case 0x31: // '1' int8_t
275 i8
= (int8_t)uto64(argument
);
276 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i8
);
277 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i8
);
279 case 0x32: // '2' int16_t
280 i16
= (int16_t)uto64(argument
);
281 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i16
);
282 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i16
);
284 case 0x34: // '4' int32_t
285 i32
= (int32_t)uto64(argument
);
286 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i32
);
287 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i32
);
289 case 0x38: // '8' int64_t
290 i64
= uto64(argument
);
291 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, i64
);
292 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, i64
);
294 case 0x73: // 's' char *
295 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
296 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, cBuffer
);
297 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, cBuffer
);
299 case 0x53: // 'S' UChar *
300 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, argument
);
301 uFileBufferLenReturned
= u_fprintf_u(testFile
, format
, argument
);
304 uBufferLenReturned
= 0;
305 uFileBufferLenReturned
= 0;
306 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
308 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
309 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
310 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
311 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
312 cBuffer
[sizeof(cBuffer
)-1] = 0;
313 log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
314 i
, cFormat
, cBuffer
, cExpected
);
316 if (uBuffer
[uBufferLenReturned
-1] == 0
317 || uBuffer
[uBufferLenReturned
] != 0
318 || uBuffer
[uBufferLenReturned
+1] != 0x2A
319 || uBuffer
[uBufferLenReturned
+2] != 0x2A)
321 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
322 cBuffer
[sizeof(cBuffer
)-1] = 0;
323 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
324 i
, cBuffer
, uBufferLenReturned
);
327 testFile
= u_fopen(STANDARD_TEST_FILE
, "r", fileLocale
, "UTF-8");
329 log_err("Can't open test file - %s\n",
333 u_fgets(uBuffer
, sizeof(uBuffer
)/sizeof(uBuffer
[0]), testFile
);
334 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
335 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
336 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
337 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
338 cBuffer
[sizeof(cBuffer
)-1] = 0;
339 log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
340 i
, cFormat
, cBuffer
, cExpected
);
342 if (uFileBufferLenReturned
!= uBufferLenReturned
)
344 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
345 cBuffer
[sizeof(cBuffer
)-1] = 0;
346 log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n",
347 uFileBufferLenReturned
, uBufferLenReturned
);
350 if(U_FAILURE(errorCode
)) {
351 log_err("error running icuio/printf test case %d - %s\n",
352 i
, u_errorName(errorCode
));
353 errorCode
=U_ZERO_ERROR
;
363 log_err("Failed: could not load test icuio data\n");
370 static void U_CALLCONV
DataDrivenScanf(void)
372 #if !UCONFIG_NO_FORMATTING
373 UErrorCode errorCode
;
374 TestDataModule
*dataModule
;
376 const DataMap
*testCase
;
377 DataDrivenLogger logger
;
380 char cExpected
[sizeof(cBuffer
)];
381 UnicodeString tempStr
;
383 UChar expectedResult
[512];
386 int8_t i8
, expected8
;
387 int16_t i16
, expected16
;
388 int32_t i32
, expected32
;
389 int64_t i64
, expected64
;
390 double dbl
, expectedDbl
;
391 int32_t uBufferLenReturned
;
393 //const char *fileLocale = "en_US_POSIX";
394 //int32_t uFileBufferLenReturned;
397 errorCode
=U_ZERO_ERROR
;
398 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
399 if(U_SUCCESS(errorCode
)) {
400 testData
=dataModule
->createTestData("scanf", errorCode
);
401 if(U_SUCCESS(errorCode
)) {
402 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
403 if(U_FAILURE(errorCode
)) {
404 log_err("error retrieving icuio/printf test case %d - %s\n",
405 i
, u_errorName(errorCode
));
406 errorCode
=U_ZERO_ERROR
;
409 /* testFile = u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8");
411 log_err("Can't open test file - %s\n",
414 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
415 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
416 tempStr
=testCase
->getString("format", errorCode
);
417 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
418 tempStr
=testCase
->getString("result", errorCode
);
419 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
420 tempStr
=testCase
->getString("argument", errorCode
);
421 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
422 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
423 if(U_FAILURE(errorCode
)) {
424 log_err("error retrieving icuio/printf test case %d - %s\n",
425 i
, u_errorName(errorCode
));
426 errorCode
=U_ZERO_ERROR
;
429 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
430 switch (testCase
->getString("argumentType", errorCode
)[0]) {
431 case 0x64: // 'd' double
432 expectedDbl
= atof(u_austrcpy(cBuffer
, expectedResult
));
433 uBufferLenReturned
= u_sscanf_u(argument
, format
, &dbl
);
434 //uFileBufferLenReturned = u_fscanf_u(testFile, format, dbl);
435 if (dbl
!= expectedDbl
) {
436 log_err("error in scanf test case[%d] Got: %f Exp: %f\n",
437 i
, dbl
, expectedDbl
);
440 case 0x31: // '1' int8_t
441 expected8
= (int8_t)uto64(expectedResult
);
442 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i8
);
443 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i8);
444 if (i8
!= expected8
) {
445 log_err("error in scanf test case[%d] Got: %02X Exp: %02X\n",
449 case 0x32: // '2' int16_t
450 expected16
= (int16_t)uto64(expectedResult
);
451 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i16
);
452 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i16);
453 if (i16
!= expected16
) {
454 log_err("error in scanf test case[%d] Got: %04X Exp: %04X\n",
458 case 0x34: // '4' int32_t
459 expected32
= (int32_t)uto64(expectedResult
);
460 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i32
);
461 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i32);
462 if (i32
!= expected32
) {
463 log_err("error in scanf test case[%d] Got: %08X Exp: %08X\n",
467 case 0x38: // '8' int64_t
468 expected64
= uto64(expectedResult
);
469 uBufferLenReturned
= u_sscanf_u(argument
, format
, &i64
);
470 //uFileBufferLenReturned = u_fscanf_u(testFile, format, i64);
471 if (i64
!= expected64
) {
472 log_err("error in scanf 64-bit. Test case = %d\n", i
);
475 case 0x73: // 's' char *
476 u_austrcpy(cExpected
, expectedResult
);
477 uBufferLenReturned
= u_sscanf_u(argument
, format
, cBuffer
);
478 //uFileBufferLenReturned = u_fscanf_u(testFile, format, cBuffer);
479 if (strcmp(cBuffer
, cExpected
) != 0) {
480 log_err("error in scanf char * string. Got \"%s\" Expected \"%s\". Test case = %d\n", cBuffer
, cExpected
, i
);
483 case 0x53: // 'S' UChar *
484 uBufferLenReturned
= u_sscanf_u(argument
, format
, uBuffer
);
485 //uFileBufferLenReturned = u_fscanf_u(testFile, format, argument);
486 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
487 u_austrcpy(cExpected
, format
);
488 u_austrcpy(cBuffer
, uBuffer
);
489 log_err("error in scanf UChar * string %s Got: \"%s\". Test case = %d\n", cExpected
, cBuffer
, i
);
493 uBufferLenReturned
= 0;
494 //uFileBufferLenReturned = 0;
495 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
497 if (uBufferLenReturned
!= 1) {
498 log_err("error scanf converted %d arguments. Test case = %d\n", uBufferLenReturned
, i
);
500 /* if (u_strcmp(uBuffer, expectedResult) != 0) {
501 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
502 u_austrncpy(cFormat, format, sizeof(cFormat));
503 u_austrncpy(cExpected, expectedResult, sizeof(cExpected));
504 cBuffer[sizeof(cBuffer)-1] = 0;
505 log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
506 i, cFormat, cBuffer, cExpected);
508 if (uBuffer[uBufferLenReturned-1] == 0
509 || uBuffer[uBufferLenReturned] != 0
510 || uBuffer[uBufferLenReturned+1] != 0x2A
511 || uBuffer[uBufferLenReturned+2] != 0x2A)
513 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
514 cBuffer[sizeof(cBuffer)-1] = 0;
515 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
516 i, cBuffer, uBufferLenReturned);
518 /* u_fclose(testFile);
519 testFile = u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8");
521 log_err("Can't open test file - %s\n",
525 u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile);
526 if (u_strcmp(uBuffer, expectedResult) != 0) {
527 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
528 u_austrncpy(cFormat, format, sizeof(cFormat));
529 u_austrncpy(cExpected, expectedResult, sizeof(cExpected));
530 cBuffer[sizeof(cBuffer)-1] = 0;
531 log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
532 i, cFormat, cBuffer, cExpected);
534 if (uFileBufferLenReturned != uBufferLenReturned)
536 u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer));
537 cBuffer[sizeof(cBuffer)-1] = 0;
538 log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n",
539 uFileBufferLenReturned, uBufferLenReturned);
542 if(U_FAILURE(errorCode
)) {
543 log_err("error running icuio/printf test case %d - %s\n",
544 i
, u_errorName(errorCode
));
545 errorCode
=U_ZERO_ERROR
;
548 // u_fclose(testFile);
555 log_err("Failed: could not load test icuio data\n");
562 static void U_CALLCONV
DataDrivenPrintfPrecision(void)
564 #if !UCONFIG_NO_FORMATTING
565 UErrorCode errorCode
;
566 TestDataModule
*dataModule
;
568 const DataMap
*testCase
;
569 DataDrivenLogger logger
;
572 char cFormat
[sizeof(cBuffer
)];
573 char cExpected
[sizeof(cBuffer
)];
574 UnicodeString tempStr
;
576 UChar expectedResult
[512];
585 int32_t uBufferLenReturned
;
587 errorCode
=U_ZERO_ERROR
;
588 dataModule
=TestDataModule::getTestDataModule("icuio", logger
, errorCode
);
589 if(U_SUCCESS(errorCode
)) {
590 testData
=dataModule
->createTestData("printfPrecision", errorCode
);
591 if(U_SUCCESS(errorCode
)) {
592 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
593 if(U_FAILURE(errorCode
)) {
594 log_err("error retrieving icuio/printf test case %d - %s\n",
595 i
, u_errorName(errorCode
));
596 errorCode
=U_ZERO_ERROR
;
599 u_memset(uBuffer
, 0x2A, sizeof(uBuffer
)/sizeof(uBuffer
[0]));
600 uBuffer
[sizeof(uBuffer
)/sizeof(uBuffer
[0])-1] = 0;
601 tempStr
=testCase
->getString("format", errorCode
);
602 tempStr
.extract(format
, sizeof(format
)/sizeof(format
[0]), errorCode
);
603 tempStr
=testCase
->getString("result", errorCode
);
604 tempStr
.extract(expectedResult
, sizeof(expectedResult
)/sizeof(expectedResult
[0]), errorCode
);
605 tempStr
=testCase
->getString("argument", errorCode
);
606 tempStr
.extract(argument
, sizeof(argument
)/sizeof(argument
[0]), errorCode
);
607 precision
=testCase
->getInt28("precision", errorCode
);
608 u_austrncpy(cBuffer
, format
, sizeof(cBuffer
));
609 if(U_FAILURE(errorCode
)) {
610 log_err("error retrieving icuio/printf test case %d - %s\n",
611 i
, u_errorName(errorCode
));
612 errorCode
=U_ZERO_ERROR
;
615 log_verbose("Test %d: format=\"%s\"\n", i
, cBuffer
);
616 switch (testCase
->getString("argumentType", errorCode
)[0]) {
617 case 0x64: // 'd' double
618 dbl
= atof(u_austrcpy(cBuffer
, argument
));
619 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, dbl
);
621 case 0x31: // '1' int8_t
622 i8
= (int8_t)uto64(argument
);
623 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i8
);
625 case 0x32: // '2' int16_t
626 i16
= (int16_t)uto64(argument
);
627 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i16
);
629 case 0x34: // '4' int32_t
630 i32
= (int32_t)uto64(argument
);
631 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i32
);
633 case 0x38: // '8' int64_t
634 i64
= uto64(argument
);
635 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, i64
);
637 case 0x73: // 's' char *
638 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
639 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, cBuffer
);
641 case 0x53: // 'S' UChar *
642 uBufferLenReturned
= u_sprintf_u(uBuffer
, format
, precision
, argument
);
645 uBufferLenReturned
= 0;
646 log_err("Unknown type %c for test %d\n", testCase
->getString("argumentType", errorCode
)[0], i
);
648 if (u_strcmp(uBuffer
, expectedResult
) != 0) {
649 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
650 u_austrncpy(cFormat
, format
, sizeof(cFormat
));
651 u_austrncpy(cExpected
, expectedResult
, sizeof(cExpected
));
652 cBuffer
[sizeof(cBuffer
)-1] = 0;
653 log_err("FAILURE test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n",
654 i
, cFormat
, cBuffer
, cExpected
);
656 if (uBuffer
[uBufferLenReturned
-1] == 0
657 || uBuffer
[uBufferLenReturned
] != 0
658 || uBuffer
[uBufferLenReturned
+1] != 0x2A
659 || uBuffer
[uBufferLenReturned
+2] != 0x2A)
661 u_austrncpy(cBuffer
, uBuffer
, sizeof(cBuffer
));
662 cBuffer
[sizeof(cBuffer
)-1] = 0;
663 log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n",
664 i
, cBuffer
, uBufferLenReturned
);
666 if(U_FAILURE(errorCode
)) {
667 log_err("error running icuio/printf test case %d - %s\n",
668 i
, u_errorName(errorCode
));
669 errorCode
=U_ZERO_ERROR
;
678 log_err("Failed: could not load test icuio data\n");
685 static void U_CALLCONV
TestStream(void)
687 #if U_IOSTREAM_SOURCE >= 198506
688 const UChar thisMu
[] = { 0x74, 0x48, 0x69, 0x73, 0x3BC, 0};
689 const UChar mu
[] = { 0x6D, 0x75, 0};
690 UnicodeString str1
= UNICODE_STRING_SIMPLE("str1");
691 UnicodeString str2
= UNICODE_STRING_SIMPLE(" <<");
692 UnicodeString str3
= UNICODE_STRING_SIMPLE("2");
693 UnicodeString str4
= UNICODE_STRING_SIMPLE(" UTF-8 ");
694 UnicodeString inStr
= UNICODE_STRING_SIMPLE(" UTF-8 ");
695 UnicodeString inStr2
;
696 char defConvName
[UCNV_MAX_CONVERTER_NAME_LENGTH
*2];
698 UErrorCode status
= U_ZERO_ERROR
;
700 static const char testStr
[] = "\x42\x65\x67\x69\x6E\x6E\x69\x6E\x67\x20\x6F\x66\x20\x74\x65\x73\x74\x20\x73\x74\x72\x31\x20\x20\x20\x3C\x3C\x32\x31\x20" UTF8_NEW_LINE
"\x20\x55\x54\x46\x2D\x38\x20\xCE\xBC\xF0\x90\x80\x81\xF0\x90\x80\x82";
702 str4
.append((UChar32
)0x03BC); /* mu */
703 str4
.append((UChar32
)0x10001);
704 str4
.append((UChar32
)0x10002);
706 /* release the default converter and use utf-8 for a bit */
707 defConv
= u_getDefaultConverter(&status
);
708 if (U_FAILURE(status
)) {
709 log_err("Can't get default converter\n");
713 strncpy(defConvName
, ucnv_getDefaultName(), sizeof(defConvName
)/sizeof(defConvName
[0]));
714 ucnv_setDefaultName("UTF-8");
717 ostringstream outTestStream
;
718 istringstream
inTestStream("\x20\x74\x48\x69\x73\xCE\xBC\xE2\x80\x82\x20\x6D\x75\x20\x77\x6F\x72\x6C\x64");
720 char testStreamBuf
[512];
721 ostrstream
outTestStream(testStreamBuf
, sizeof(testStreamBuf
));
722 istrstream
inTestStream("\x20\x74\x48\x69\x73\xCE\xBC\xE2\x80\x82\x20\x6D\x75\x20\x77\x6F\x72\x6C\x64", 0);
724 /* initialize testStreamBuf */
725 memset(testStreamBuf
, '*', sizeof(testStreamBuf
));
726 testStreamBuf
[sizeof(testStreamBuf
)-1] = 0;
729 outTestStream
<< "\x42\x65\x67\x69\x6E\x6E\x69\x6E\x67\x20\x6F\x66\x20\x74\x65\x73\x74\x20";
730 outTestStream
<< str1
<< "\x20\x20" << str2
<< str3
<< "\x31\x20" << UTF8_NEW_LINE
<< str4
<< ends
;
732 string tempStr
= outTestStream
.str();
733 const char *testStreamBuf
= tempStr
.c_str();
735 if (strcmp(testStreamBuf
, testStr
) != 0) {
736 log_err("Got: \"%s\", Expected: \"%s\"\n", testStreamBuf
, testStr
);
739 inTestStream
>> inStr
>> inStr2
;
740 if (inStr
.compare(thisMu
) != 0) {
741 u_austrncpy(inStrC
, inStr
.getBuffer(), inStr
.length());
742 inStrC
[inStr
.length()] = 0;
743 log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC
);
745 if (inStr2
.compare(mu
) != 0) {
746 u_austrncpy(inStrC
, inStr
.getBuffer(), inStr
.length());
747 inStrC
[inStr
.length()] = 0;
748 log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC
);
751 /* return the default converter to the original state. */
752 ucnv_setDefaultName(defConvName
);
753 defConv
= u_getDefaultConverter(&status
);
754 if (U_FAILURE(status
)) {
755 log_err("Can't get default converter");
760 log_info("U_IOSTREAM_SOURCE is disabled\n");
765 static void addAllTests(TestNode
** root
) {
769 #if !UCONFIG_NO_FORMATTING
770 addTest(root
, &DataDrivenPrintf
, "datadriv/DataDrivenPrintf");
771 addTest(root
, &DataDrivenPrintfPrecision
, "datadriv/DataDrivenPrintfPrecision");
772 addTest(root
, &DataDrivenScanf
, "datadriv/DataDrivenScanf");
774 addTest(root
, &TestStream
, "stream/TestStream");
777 /* returns the path to icu/source/data/out */
778 static const char *ctest_dataOutDir()
780 static const char *dataOutDir
= NULL
;
786 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
787 // to point to the top of the build hierarchy, which may or
788 // may not be the same as the source directory, depending on
789 // the configure options used. At any rate,
790 // set the data path to the built data from this directory.
791 // The value is complete with quotes, so it can be used
792 // as-is as a string constant.
794 #if defined (U_TOPBUILDDIR)
796 dataOutDir
= U_TOPBUILDDIR
"data"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
800 /* On Windows, the file name obtained from __FILE__ includes a full path.
801 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
802 * Change to "wherever\icu\source\data"
805 static char p
[sizeof(__FILE__
) + 20];
810 /* We want to back over three '\' chars. */
811 /* Only Windows should end up here, so looking for '\' is safe. */
812 for (i
=1; i
<=3; i
++) {
813 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
814 if (pBackSlash
!= NULL
) {
815 *pBackSlash
= 0; /* Truncate the string at the '\' */
819 if (pBackSlash
!= NULL
) {
820 /* We found and truncated three names from the path.
821 * Now append "source\data" and set the environment
823 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
);
827 /* __FILE__ on MSVC7 does not contain the directory */
828 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
831 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
834 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
843 /* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
844 * set, try to deduce the directory in which ICU was built,
845 * and set ICU_DATA to "icu/source/data" in that location.
846 * The intent is to allow the tests to have a good chance
847 * of running without requiring that the user manually set
848 * ICU_DATA. Common data isn't a problem, since it is
849 * picked up via a static (build time) reference, but the
850 * tests dynamically load some data.
852 static void ctest_setICU_DATA() {
854 /* No location for the data dir was identifiable.
855 * Add other fallbacks for the test data location here if the need arises
857 if (getenv("ICU_DATA") == NULL
) {
858 /* If ICU_DATA isn't set, set it to the usual location */
859 u_setDataDirectory(ctest_dataOutDir());
863 int main(int argc
, char* argv
[])
866 TestNode
*root
= NULL
;
867 UErrorCode errorCode
= U_ZERO_ERROR
;
869 /* Check whether ICU will initialize without forcing the build data directory into
870 * the ICU_DATA path. Success here means either the data dll contains data, or that
871 * this test program was run with ICU_DATA set externally. Failure of this check
872 * is normal when ICU data is not packaged into a shared library.
874 * Whether or not this test succeeds, we want to cleanup and reinitialize
875 * with a data path so that data loading from individual files can be tested.
878 if (U_FAILURE(errorCode
)) {
880 "#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
883 errorCode
= U_ZERO_ERROR
;
886 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
888 if (U_FAILURE(errorCode
)) {
890 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
891 "*** Check the ICU_DATA environment variable and \n"
892 "*** check that the data files are present.\n", argv
[0], u_errorName(errorCode
));
896 fprintf(stdout
, "Default charset for this run is %s\n", ucnv_getDefaultName());
899 nerrors
= processArgs(root
, argc
, argv
);
903 FILE* fileToRemove
= fopen(STANDARD_TEST_FILE
, "r");
904 /* This should delete any temporary files. */
906 fclose(fileToRemove
);
907 if (remove(STANDARD_TEST_FILE
) != 0) {
908 /* Maybe someone didn't close the file correctly. */
909 fprintf(stderr
, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE
);
916 cleanUpTestTree(root
);
917 DataDrivenLogger::cleanUp();