]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
51004dcb | 3 | * Copyright (C) 2002-2012, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | ********************************************************************** | |
6 | * file name: iotest.cpp | |
7 | * encoding: US-ASCII | |
8 | * tab size: 8 (not used) | |
9 | * indentation:4 | |
10 | * | |
11 | * created on: 2002feb21 | |
12 | * created by: George Rhoten | |
13 | */ | |
14 | ||
15 | ||
16 | #include "unicode/ustdio.h" | |
b75a7d8f A |
17 | #include "unicode/uclean.h" |
18 | ||
19 | #include "unicode/ucnv.h" | |
374ca955 | 20 | #include "unicode/uchar.h" |
b75a7d8f A |
21 | #include "unicode/unistr.h" |
22 | #include "unicode/ustring.h" | |
374ca955 A |
23 | #include "ustr_cnv.h" |
24 | #include "iotest.h" | |
25 | #include "unicode/tstdtmod.h" | |
73c04bcf | 26 | #include "putilimp.h" |
b75a7d8f A |
27 | |
28 | #include <string.h> | |
374ca955 | 29 | #include <stdlib.h> |
b75a7d8f | 30 | |
374ca955 A |
31 | class DataDrivenLogger : public TestLog { |
32 | static const char* fgDataDir; | |
33 | static char *fgTestDataPath; | |
b75a7d8f | 34 | |
374ca955 A |
35 | public: |
36 | static void cleanUp() { | |
37 | if (fgTestDataPath) { | |
38 | free(fgTestDataPath); | |
39 | fgTestDataPath = NULL; | |
40 | } | |
b75a7d8f | 41 | } |
374ca955 A |
42 | virtual void errln( const UnicodeString &message ) { |
43 | char buffer[4000]; | |
44 | message.extract(0, message.length(), buffer, sizeof(buffer)); | |
45 | buffer[3999] = 0; /* NULL terminate */ | |
46 | log_err(buffer); | |
b75a7d8f | 47 | } |
729e4ab9 A |
48 | |
49 | virtual void logln( const UnicodeString &message ) { | |
50 | char buffer[4000]; | |
51 | message.extract(0, message.length(), buffer, sizeof(buffer)); | |
52 | buffer[3999] = 0; /* NULL terminate */ | |
53 | log_info(buffer); | |
54 | } | |
55 | ||
46f4442e A |
56 | virtual void dataerrln( const UnicodeString &message ) { |
57 | char buffer[4000]; | |
58 | message.extract(0, message.length(), buffer, sizeof(buffer)); | |
59 | buffer[3999] = 0; /* NULL terminate */ | |
60 | log_data_err(buffer); | |
61 | } | |
b75a7d8f | 62 | |
374ca955 | 63 | static const char * pathToDataDirectory(void) |
b75a7d8f | 64 | { |
b75a7d8f | 65 | |
374ca955 A |
66 | if(fgDataDir != NULL) { |
67 | return fgDataDir; | |
b75a7d8f | 68 | } |
b75a7d8f | 69 | |
374ca955 A |
70 | /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst |
71 | // to point to the top of the build hierarchy, which may or | |
72 | // may not be the same as the source directory, depending on | |
73 | // the configure options used. At any rate, | |
74 | // set the data path to the built data from this directory. | |
75 | // The value is complete with quotes, so it can be used | |
76 | // as-is as a string constant. | |
77 | */ | |
78 | #if defined (U_TOPSRCDIR) | |
79 | { | |
80 | fgDataDir = U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING; | |
b75a7d8f | 81 | } |
374ca955 A |
82 | #else |
83 | ||
84 | /* On Windows, the file name obtained from __FILE__ includes a full path. | |
85 | * This file is "wherever\icu\source\test\cintltst\cintltst.c" | |
86 | * Change to "wherever\icu\source\data" | |
87 | */ | |
88 | { | |
89 | static char p[sizeof(__FILE__) + 10]; | |
90 | char *pBackSlash; | |
91 | int i; | |
92 | ||
93 | strcpy(p, __FILE__); | |
94 | /* We want to back over three '\' chars. */ | |
95 | /* Only Windows should end up here, so looking for '\' is safe. */ | |
96 | for (i=1; i<=3; i++) { | |
97 | pBackSlash = strrchr(p, U_FILE_SEP_CHAR); | |
98 | if (pBackSlash != NULL) { | |
99 | *pBackSlash = 0; /* Truncate the string at the '\' */ | |
100 | } | |
101 | } | |
102 | ||
103 | if (pBackSlash != NULL) { | |
104 | /* We found and truncated three names from the path. | |
105 | * Now append "source\data" and set the environment | |
106 | */ | |
107 | strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING ); | |
108 | fgDataDir = p; | |
109 | } | |
110 | else { | |
111 | /* __FILE__ on MSVC7 does not contain the directory */ | |
51004dcb | 112 | FILE *file = fopen(".." U_FILE_SEP_STRING".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r"); |
374ca955 A |
113 | if (file) { |
114 | fclose(file); | |
51004dcb | 115 | fgDataDir = ".." U_FILE_SEP_STRING".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING; |
374ca955 A |
116 | } |
117 | else { | |
51004dcb | 118 | fgDataDir = ".." U_FILE_SEP_STRING".." U_FILE_SEP_STRING".." U_FILE_SEP_STRING".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING; |
374ca955 A |
119 | } |
120 | } | |
b75a7d8f | 121 | } |
374ca955 | 122 | #endif |
b75a7d8f | 123 | |
374ca955 | 124 | return fgDataDir; |
b75a7d8f | 125 | |
b75a7d8f A |
126 | } |
127 | ||
374ca955 A |
128 | static const char* loadTestData(UErrorCode& err){ |
129 | if( fgTestDataPath == NULL){ | |
130 | const char* directory=NULL; | |
131 | UResourceBundle* test =NULL; | |
132 | char* tdpath=NULL; | |
133 | const char* tdrelativepath; | |
b75a7d8f | 134 | |
374ca955 | 135 | #if defined (U_TOPBUILDDIR) |
51004dcb | 136 | tdrelativepath = "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING; |
374ca955 A |
137 | directory = U_TOPBUILDDIR; |
138 | #else | |
51004dcb | 139 | tdrelativepath = ".." U_FILE_SEP_STRING "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING; |
374ca955 A |
140 | directory = pathToDataDirectory(); |
141 | #endif | |
b75a7d8f | 142 | |
374ca955 | 143 | tdpath = (char*) malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 100)); |
b75a7d8f | 144 | |
b75a7d8f | 145 | |
374ca955 A |
146 | /* u_getDataDirectory shoul return \source\data ... set the |
147 | * directory to ..\source\data\..\test\testdata\out\testdata | |
148 | */ | |
149 | strcpy(tdpath, directory); | |
150 | strcat(tdpath, tdrelativepath); | |
151 | strcat(tdpath,"testdata"); | |
b75a7d8f | 152 | |
374ca955 | 153 | test=ures_open(tdpath, "testtypes", &err); |
b75a7d8f | 154 | |
374ca955 A |
155 | if(U_FAILURE(err)){ |
156 | err = U_FILE_ACCESS_ERROR; | |
46f4442e | 157 | log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(err)); |
374ca955 A |
158 | return ""; |
159 | } | |
160 | ures_close(test); | |
161 | fgTestDataPath = tdpath; | |
b75a7d8f | 162 | } |
374ca955 | 163 | return fgTestDataPath; |
b75a7d8f A |
164 | } |
165 | ||
374ca955 A |
166 | virtual const char* getTestDataPath(UErrorCode& err) { |
167 | return loadTestData(err); | |
b75a7d8f | 168 | } |
374ca955 A |
169 | }; |
170 | ||
171 | const char* DataDrivenLogger::fgDataDir = NULL; | |
172 | char* DataDrivenLogger::fgTestDataPath = NULL; | |
b75a7d8f | 173 | |
51004dcb | 174 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO |
374ca955 A |
175 | static int64_t |
176 | uto64(const UChar *buffer) | |
177 | { | |
178 | int64_t result = 0; | |
179 | /* iterate through buffer */ | |
180 | while(*buffer) { | |
181 | /* read the next digit */ | |
182 | result *= 16; | |
183 | if (!u_isxdigit(*buffer)) { | |
184 | log_err("\\u%04X is not a valid hex digit for this test\n", (UChar)*buffer); | |
b75a7d8f | 185 | } |
374ca955 A |
186 | result += *buffer - 0x0030 - (*buffer >= 0x0041 ? (*buffer >= 0x0061 ? 39 : 7) : 0); |
187 | buffer++; | |
b75a7d8f | 188 | } |
374ca955 | 189 | return result; |
b75a7d8f | 190 | } |
51004dcb | 191 | #endif |
b75a7d8f | 192 | |
374ca955 A |
193 | U_CDECL_BEGIN |
194 | static void U_CALLCONV DataDrivenPrintf(void) | |
195 | { | |
729e4ab9 | 196 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO |
374ca955 A |
197 | UErrorCode errorCode; |
198 | TestDataModule *dataModule; | |
199 | TestData *testData; | |
200 | const DataMap *testCase; | |
201 | DataDrivenLogger logger; | |
202 | UChar uBuffer[512]; | |
203 | char cBuffer[512]; | |
204 | char cFormat[sizeof(cBuffer)]; | |
205 | char cExpected[sizeof(cBuffer)]; | |
206 | UnicodeString tempStr; | |
207 | UChar format[512]; | |
208 | UChar expectedResult[512]; | |
209 | UChar argument[512]; | |
210 | int32_t i; | |
211 | int8_t i8; | |
212 | int16_t i16; | |
213 | int32_t i32; | |
214 | int64_t i64; | |
215 | double dbl; | |
216 | int32_t uBufferLenReturned; | |
217 | ||
218 | const char *fileLocale = "en_US_POSIX"; | |
219 | int32_t uFileBufferLenReturned; | |
729e4ab9 | 220 | LocalUFILEPointer testFile; |
374ca955 A |
221 | |
222 | errorCode=U_ZERO_ERROR; | |
223 | dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode); | |
224 | if(U_SUCCESS(errorCode)) { | |
225 | testData=dataModule->createTestData("printf", errorCode); | |
226 | if(U_SUCCESS(errorCode)) { | |
227 | for(i=0; testData->nextCase(testCase, errorCode); ++i) { | |
228 | if(U_FAILURE(errorCode)) { | |
229 | log_err("error retrieving icuio/printf test case %d - %s\n", | |
230 | i, u_errorName(errorCode)); | |
231 | errorCode=U_ZERO_ERROR; | |
232 | continue; | |
233 | } | |
729e4ab9 A |
234 | testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8")); |
235 | if (testFile.isNull()) { | |
374ca955 A |
236 | log_err("Can't open test file - %s\n", |
237 | STANDARD_TEST_FILE); | |
238 | continue; | |
239 | } | |
240 | u_memset(uBuffer, 0x2A, sizeof(uBuffer)/sizeof(uBuffer[0])); | |
241 | uBuffer[sizeof(uBuffer)/sizeof(uBuffer[0])-1] = 0; | |
242 | tempStr=testCase->getString("format", errorCode); | |
243 | tempStr.extract(format, sizeof(format)/sizeof(format[0]), errorCode); | |
244 | tempStr=testCase->getString("result", errorCode); | |
245 | tempStr.extract(expectedResult, sizeof(expectedResult)/sizeof(expectedResult[0]), errorCode); | |
246 | tempStr=testCase->getString("argument", errorCode); | |
247 | tempStr.extract(argument, sizeof(argument)/sizeof(argument[0]), errorCode); | |
248 | u_austrncpy(cBuffer, format, sizeof(cBuffer)); | |
249 | if(U_FAILURE(errorCode)) { | |
250 | log_err("error retrieving icuio/printf test case %d - %s\n", | |
251 | i, u_errorName(errorCode)); | |
252 | errorCode=U_ZERO_ERROR; | |
253 | continue; | |
254 | } | |
255 | log_verbose("Test %d: format=\"%s\"\n", i, cBuffer); | |
256 | switch (testCase->getString("argumentType", errorCode)[0]) { | |
257 | case 0x64: // 'd' double | |
258 | dbl = atof(u_austrcpy(cBuffer, argument)); | |
259 | uBufferLenReturned = u_sprintf_u(uBuffer, format, dbl); | |
729e4ab9 | 260 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, dbl); |
374ca955 A |
261 | break; |
262 | case 0x31: // '1' int8_t | |
263 | i8 = (int8_t)uto64(argument); | |
264 | uBufferLenReturned = u_sprintf_u(uBuffer, format, i8); | |
729e4ab9 | 265 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i8); |
374ca955 A |
266 | break; |
267 | case 0x32: // '2' int16_t | |
268 | i16 = (int16_t)uto64(argument); | |
269 | uBufferLenReturned = u_sprintf_u(uBuffer, format, i16); | |
729e4ab9 | 270 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i16); |
374ca955 A |
271 | break; |
272 | case 0x34: // '4' int32_t | |
273 | i32 = (int32_t)uto64(argument); | |
274 | uBufferLenReturned = u_sprintf_u(uBuffer, format, i32); | |
729e4ab9 | 275 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i32); |
374ca955 A |
276 | break; |
277 | case 0x38: // '8' int64_t | |
278 | i64 = uto64(argument); | |
279 | uBufferLenReturned = u_sprintf_u(uBuffer, format, i64); | |
729e4ab9 | 280 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i64); |
374ca955 A |
281 | break; |
282 | case 0x73: // 's' char * | |
73c04bcf | 283 | u_austrncpy(cBuffer, argument, sizeof(cBuffer)); |
374ca955 | 284 | uBufferLenReturned = u_sprintf_u(uBuffer, format, cBuffer); |
729e4ab9 | 285 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, cBuffer); |
374ca955 A |
286 | break; |
287 | case 0x53: // 'S' UChar * | |
288 | uBufferLenReturned = u_sprintf_u(uBuffer, format, argument); | |
729e4ab9 | 289 | uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, argument); |
374ca955 A |
290 | break; |
291 | default: | |
292 | uBufferLenReturned = 0; | |
293 | uFileBufferLenReturned = 0; | |
294 | log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); | |
295 | } | |
296 | if (u_strcmp(uBuffer, expectedResult) != 0) { | |
297 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
298 | u_austrncpy(cFormat, format, sizeof(cFormat)); | |
299 | u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); | |
300 | cBuffer[sizeof(cBuffer)-1] = 0; | |
301 | log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", | |
302 | i, cFormat, cBuffer, cExpected); | |
303 | } | |
46f4442e A |
304 | if (uBufferLenReturned <= 0) { |
305 | log_err("FAILURE test case %d - \"%s\" is an empty string.\n", | |
306 | i, cBuffer); | |
307 | } | |
308 | else if (uBuffer[uBufferLenReturned-1] == 0 | |
374ca955 A |
309 | || uBuffer[uBufferLenReturned] != 0 |
310 | || uBuffer[uBufferLenReturned+1] != 0x2A | |
311 | || uBuffer[uBufferLenReturned+2] != 0x2A) | |
312 | { | |
313 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
314 | cBuffer[sizeof(cBuffer)-1] = 0; | |
315 | log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n", | |
316 | i, cBuffer, uBufferLenReturned); | |
317 | } | |
729e4ab9 A |
318 | testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8")); |
319 | if (testFile.isNull()) { | |
374ca955 A |
320 | log_err("Can't open test file - %s\n", |
321 | STANDARD_TEST_FILE); | |
322 | } | |
323 | uBuffer[0]=0; | |
729e4ab9 | 324 | u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile.getAlias()); |
374ca955 A |
325 | if (u_strcmp(uBuffer, expectedResult) != 0) { |
326 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
327 | u_austrncpy(cFormat, format, sizeof(cFormat)); | |
328 | u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); | |
329 | cBuffer[sizeof(cBuffer)-1] = 0; | |
330 | log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", | |
331 | i, cFormat, cBuffer, cExpected); | |
332 | } | |
333 | if (uFileBufferLenReturned != uBufferLenReturned) | |
334 | { | |
335 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
336 | cBuffer[sizeof(cBuffer)-1] = 0; | |
337 | log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n", | |
338 | uFileBufferLenReturned, uBufferLenReturned); | |
339 | } | |
340 | ||
341 | if(U_FAILURE(errorCode)) { | |
342 | log_err("error running icuio/printf test case %d - %s\n", | |
343 | i, u_errorName(errorCode)); | |
344 | errorCode=U_ZERO_ERROR; | |
345 | continue; | |
346 | } | |
374ca955 A |
347 | } |
348 | delete testData; | |
349 | } | |
350 | delete dataModule; | |
b75a7d8f | 351 | } |
374ca955 | 352 | else { |
46f4442e | 353 | log_data_err("Failed: could not load test icuio data\n"); |
b75a7d8f | 354 | } |
374ca955 A |
355 | #endif |
356 | } | |
357 | U_CDECL_END | |
b75a7d8f | 358 | |
374ca955 A |
359 | U_CDECL_BEGIN |
360 | static void U_CALLCONV DataDrivenScanf(void) | |
361 | { | |
729e4ab9 | 362 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO |
374ca955 A |
363 | UErrorCode errorCode; |
364 | TestDataModule *dataModule; | |
365 | TestData *testData; | |
366 | const DataMap *testCase; | |
367 | DataDrivenLogger logger; | |
368 | UChar uBuffer[512]; | |
369 | char cBuffer[512]; | |
370 | char cExpected[sizeof(cBuffer)]; | |
371 | UnicodeString tempStr; | |
372 | UChar format[512]; | |
373 | UChar expectedResult[512]; | |
374 | UChar argument[512]; | |
375 | int32_t i; | |
376 | int8_t i8, expected8; | |
377 | int16_t i16, expected16; | |
378 | int32_t i32, expected32; | |
379 | int64_t i64, expected64; | |
380 | double dbl, expectedDbl; | |
73c04bcf | 381 | volatile float flt, expectedFlt; // Use volatile in order to get around an Intel compiler issue. |
374ca955 A |
382 | int32_t uBufferLenReturned; |
383 | ||
384 | //const char *fileLocale = "en_US_POSIX"; | |
385 | //int32_t uFileBufferLenReturned; | |
386 | //UFILE *testFile; | |
387 | ||
388 | errorCode=U_ZERO_ERROR; | |
389 | dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode); | |
390 | if(U_SUCCESS(errorCode)) { | |
391 | testData=dataModule->createTestData("scanf", errorCode); | |
392 | if(U_SUCCESS(errorCode)) { | |
393 | for(i=0; testData->nextCase(testCase, errorCode); ++i) { | |
394 | if(U_FAILURE(errorCode)) { | |
395 | log_err("error retrieving icuio/printf test case %d - %s\n", | |
396 | i, u_errorName(errorCode)); | |
397 | errorCode=U_ZERO_ERROR; | |
398 | continue; | |
399 | } | |
400 | /* testFile = u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8"); | |
401 | if (!testFile) { | |
402 | log_err("Can't open test file - %s\n", | |
403 | STANDARD_TEST_FILE); | |
404 | }*/ | |
405 | u_memset(uBuffer, 0x2A, sizeof(uBuffer)/sizeof(uBuffer[0])); | |
406 | uBuffer[sizeof(uBuffer)/sizeof(uBuffer[0])-1] = 0; | |
407 | tempStr=testCase->getString("format", errorCode); | |
408 | tempStr.extract(format, sizeof(format)/sizeof(format[0]), errorCode); | |
409 | tempStr=testCase->getString("result", errorCode); | |
410 | tempStr.extract(expectedResult, sizeof(expectedResult)/sizeof(expectedResult[0]), errorCode); | |
411 | tempStr=testCase->getString("argument", errorCode); | |
412 | tempStr.extract(argument, sizeof(argument)/sizeof(argument[0]), errorCode); | |
413 | u_austrncpy(cBuffer, format, sizeof(cBuffer)); | |
414 | if(U_FAILURE(errorCode)) { | |
415 | log_err("error retrieving icuio/printf test case %d - %s\n", | |
416 | i, u_errorName(errorCode)); | |
417 | errorCode=U_ZERO_ERROR; | |
418 | continue; | |
419 | } | |
420 | log_verbose("Test %d: format=\"%s\"\n", i, cBuffer); | |
421 | switch (testCase->getString("argumentType", errorCode)[0]) { | |
422 | case 0x64: // 'd' double | |
423 | expectedDbl = atof(u_austrcpy(cBuffer, expectedResult)); | |
424 | uBufferLenReturned = u_sscanf_u(argument, format, &dbl); | |
425 | //uFileBufferLenReturned = u_fscanf_u(testFile, format, dbl); | |
426 | if (dbl != expectedDbl) { | |
427 | log_err("error in scanf test case[%d] Got: %f Exp: %f\n", | |
428 | i, dbl, expectedDbl); | |
429 | } | |
430 | break; | |
73c04bcf A |
431 | case 0x66: // 'f' float |
432 | expectedFlt = (float)atof(u_austrcpy(cBuffer, expectedResult)); | |
433 | uBufferLenReturned = u_sscanf_u(argument, format, &flt); | |
434 | //uFileBufferLenReturned = u_fscanf_u(testFile, format, flt); | |
435 | if (flt != expectedFlt) { | |
436 | log_err("error in scanf test case[%d] Got: %f Exp: %f\n", | |
437 | i, flt, expectedFlt); | |
438 | } | |
439 | break; | |
374ca955 A |
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", | |
446 | i, i8, expected8); | |
447 | } | |
448 | break; | |
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", | |
455 | i, i16, expected16); | |
456 | } | |
457 | break; | |
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", | |
464 | i, i32, expected32); | |
465 | } | |
466 | break; | |
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); | |
473 | } | |
474 | break; | |
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); | |
481 | } | |
482 | break; | |
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); | |
490 | } | |
491 | break; | |
492 | default: | |
493 | uBufferLenReturned = 0; | |
494 | //uFileBufferLenReturned = 0; | |
495 | log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); | |
496 | } | |
497 | if (uBufferLenReturned != 1) { | |
498 | log_err("error scanf converted %d arguments. Test case = %d\n", uBufferLenReturned, i); | |
499 | } | |
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); | |
507 | } | |
508 | if (uBuffer[uBufferLenReturned-1] == 0 | |
509 | || uBuffer[uBufferLenReturned] != 0 | |
510 | || uBuffer[uBufferLenReturned+1] != 0x2A | |
511 | || uBuffer[uBufferLenReturned+2] != 0x2A) | |
512 | { | |
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); | |
517 | }*/ | |
518 | /* u_fclose(testFile); | |
519 | testFile = u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8"); | |
520 | if (!testFile) { | |
521 | log_err("Can't open test file - %s\n", | |
522 | STANDARD_TEST_FILE); | |
523 | } | |
524 | uBuffer[0]; | |
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); | |
533 | } | |
534 | if (uFileBufferLenReturned != uBufferLenReturned) | |
535 | { | |
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); | |
540 | } | |
541 | */ | |
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; | |
546 | continue; | |
547 | } | |
548 | // u_fclose(testFile); | |
549 | } | |
550 | delete testData; | |
551 | } | |
552 | delete dataModule; | |
b75a7d8f | 553 | } |
374ca955 | 554 | else { |
46f4442e | 555 | log_data_err("Failed: could not load test icuio data\n"); |
b75a7d8f | 556 | } |
374ca955 | 557 | #endif |
b75a7d8f | 558 | } |
374ca955 | 559 | U_CDECL_END |
b75a7d8f | 560 | |
374ca955 A |
561 | U_CDECL_BEGIN |
562 | static void U_CALLCONV DataDrivenPrintfPrecision(void) | |
563 | { | |
729e4ab9 | 564 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO |
374ca955 A |
565 | UErrorCode errorCode; |
566 | TestDataModule *dataModule; | |
567 | TestData *testData; | |
568 | const DataMap *testCase; | |
569 | DataDrivenLogger logger; | |
570 | UChar uBuffer[512]; | |
571 | char cBuffer[512]; | |
572 | char cFormat[sizeof(cBuffer)]; | |
573 | char cExpected[sizeof(cBuffer)]; | |
574 | UnicodeString tempStr; | |
575 | UChar format[512]; | |
576 | UChar expectedResult[512]; | |
577 | UChar argument[512]; | |
578 | int32_t precision; | |
579 | int32_t i; | |
580 | int8_t i8; | |
581 | int16_t i16; | |
582 | int32_t i32; | |
583 | int64_t i64; | |
584 | double dbl; | |
585 | int32_t uBufferLenReturned; | |
586 | ||
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; | |
597 | continue; | |
598 | } | |
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; | |
613 | continue; | |
614 | } | |
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); | |
620 | break; | |
621 | case 0x31: // '1' int8_t | |
622 | i8 = (int8_t)uto64(argument); | |
623 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i8); | |
624 | break; | |
625 | case 0x32: // '2' int16_t | |
626 | i16 = (int16_t)uto64(argument); | |
627 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i16); | |
628 | break; | |
629 | case 0x34: // '4' int32_t | |
630 | i32 = (int32_t)uto64(argument); | |
631 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i32); | |
632 | break; | |
633 | case 0x38: // '8' int64_t | |
634 | i64 = uto64(argument); | |
635 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i64); | |
636 | break; | |
637 | case 0x73: // 's' char * | |
638 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
639 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, cBuffer); | |
640 | break; | |
641 | case 0x53: // 'S' UChar * | |
642 | uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, argument); | |
643 | break; | |
644 | default: | |
645 | uBufferLenReturned = 0; | |
646 | log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); | |
647 | } | |
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); | |
655 | } | |
46f4442e A |
656 | if (uBufferLenReturned <= 0) { |
657 | log_err("FAILURE test case %d - \"%s\" is an empty string.\n", | |
658 | i, cBuffer); | |
659 | } | |
660 | else if (uBuffer[uBufferLenReturned-1] == 0 | |
374ca955 A |
661 | || uBuffer[uBufferLenReturned] != 0 |
662 | || uBuffer[uBufferLenReturned+1] != 0x2A | |
663 | || uBuffer[uBufferLenReturned+2] != 0x2A) | |
664 | { | |
665 | u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); | |
666 | cBuffer[sizeof(cBuffer)-1] = 0; | |
667 | log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n", | |
668 | i, cBuffer, uBufferLenReturned); | |
669 | } | |
670 | if(U_FAILURE(errorCode)) { | |
671 | log_err("error running icuio/printf test case %d - %s\n", | |
672 | i, u_errorName(errorCode)); | |
673 | errorCode=U_ZERO_ERROR; | |
674 | continue; | |
675 | } | |
676 | } | |
677 | delete testData; | |
b75a7d8f | 678 | } |
374ca955 | 679 | delete dataModule; |
b75a7d8f | 680 | } |
374ca955 | 681 | else { |
46f4442e | 682 | log_data_err("Failed: could not load test icuio data\n"); |
b75a7d8f | 683 | } |
374ca955 | 684 | #endif |
b75a7d8f | 685 | } |
374ca955 | 686 | U_CDECL_END |
b75a7d8f | 687 | |
374ca955 A |
688 | static void addAllTests(TestNode** root) { |
689 | addFileTest(root); | |
690 | addStringTest(root); | |
73c04bcf | 691 | addTranslitTest(root); |
b75a7d8f | 692 | |
729e4ab9 | 693 | #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_LEGACY_CONVERSION |
374ca955 A |
694 | addTest(root, &DataDrivenPrintf, "datadriv/DataDrivenPrintf"); |
695 | addTest(root, &DataDrivenPrintfPrecision, "datadriv/DataDrivenPrintfPrecision"); | |
696 | addTest(root, &DataDrivenScanf, "datadriv/DataDrivenScanf"); | |
697 | #endif | |
4388f060 | 698 | #if U_IOSTREAM_SOURCE >= 199711 |
73c04bcf | 699 | addStreamTests(root); |
729e4ab9 | 700 | #endif |
b75a7d8f A |
701 | } |
702 | ||
374ca955 A |
703 | /* returns the path to icu/source/data/out */ |
704 | static const char *ctest_dataOutDir() | |
b75a7d8f | 705 | { |
374ca955 | 706 | static const char *dataOutDir = NULL; |
b75a7d8f | 707 | |
374ca955 A |
708 | if(dataOutDir) { |
709 | return dataOutDir; | |
b75a7d8f A |
710 | } |
711 | ||
374ca955 A |
712 | /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst |
713 | // to point to the top of the build hierarchy, which may or | |
714 | // may not be the same as the source directory, depending on | |
715 | // the configure options used. At any rate, | |
716 | // set the data path to the built data from this directory. | |
717 | // The value is complete with quotes, so it can be used | |
718 | // as-is as a string constant. | |
719 | */ | |
720 | #if defined (U_TOPBUILDDIR) | |
b75a7d8f | 721 | { |
51004dcb | 722 | dataOutDir = U_TOPBUILDDIR "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING; |
b75a7d8f | 723 | } |
374ca955 | 724 | #else |
b75a7d8f | 725 | |
374ca955 A |
726 | /* On Windows, the file name obtained from __FILE__ includes a full path. |
727 | * This file is "wherever\icu\source\test\cintltst\cintltst.c" | |
728 | * Change to "wherever\icu\source\data" | |
729 | */ | |
b75a7d8f | 730 | { |
374ca955 A |
731 | static char p[sizeof(__FILE__) + 20]; |
732 | char *pBackSlash; | |
733 | int i; | |
734 | ||
735 | strcpy(p, __FILE__); | |
736 | /* We want to back over three '\' chars. */ | |
737 | /* Only Windows should end up here, so looking for '\' is safe. */ | |
738 | for (i=1; i<=3; i++) { | |
739 | pBackSlash = strrchr(p, U_FILE_SEP_CHAR); | |
740 | if (pBackSlash != NULL) { | |
741 | *pBackSlash = 0; /* Truncate the string at the '\' */ | |
742 | } | |
743 | } | |
b75a7d8f | 744 | |
374ca955 A |
745 | if (pBackSlash != NULL) { |
746 | /* We found and truncated three names from the path. | |
747 | * Now append "source\data" and set the environment | |
748 | */ | |
749 | strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING); | |
750 | dataOutDir = p; | |
751 | } | |
752 | else { | |
753 | /* __FILE__ on MSVC7 does not contain the directory */ | |
51004dcb | 754 | FILE *file = fopen(".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r"); |
374ca955 A |
755 | if (file) { |
756 | fclose(file); | |
51004dcb | 757 | dataOutDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING; |
374ca955 A |
758 | } |
759 | else { | |
51004dcb | 760 | dataOutDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING; |
374ca955 A |
761 | } |
762 | } | |
b75a7d8f | 763 | } |
374ca955 | 764 | #endif |
b75a7d8f | 765 | |
374ca955 | 766 | return dataOutDir; |
b75a7d8f A |
767 | } |
768 | ||
374ca955 A |
769 | /* ctest_setICU_DATA - if the ICU_DATA environment variable is not already |
770 | * set, try to deduce the directory in which ICU was built, | |
771 | * and set ICU_DATA to "icu/source/data" in that location. | |
772 | * The intent is to allow the tests to have a good chance | |
773 | * of running without requiring that the user manually set | |
774 | * ICU_DATA. Common data isn't a problem, since it is | |
775 | * picked up via a static (build time) reference, but the | |
776 | * tests dynamically load some data. | |
777 | */ | |
778 | static void ctest_setICU_DATA() { | |
779 | ||
780 | /* No location for the data dir was identifiable. | |
781 | * Add other fallbacks for the test data location here if the need arises | |
782 | */ | |
783 | if (getenv("ICU_DATA") == NULL) { | |
784 | /* If ICU_DATA isn't set, set it to the usual location */ | |
785 | u_setDataDirectory(ctest_dataOutDir()); | |
786 | } | |
b75a7d8f A |
787 | } |
788 | ||
46f4442e A |
789 | U_CDECL_BEGIN |
790 | /* | |
791 | * Note: this assumes that context is a pointer to STANDARD_TEST_FILE. It would be | |
792 | * cleaner to define an acutal context with a string pointer in it and set STANDARD_TEST_FILE | |
793 | * after the call to initArgs()... | |
794 | */ | |
795 | static int U_CALLCONV argHandler(int arg, int /*argc*/, const char * const argv[], void *context) | |
796 | { | |
797 | const char **str = (const char **) context; | |
798 | ||
799 | if (argv[arg][0] != '/' && argv[arg][0] != '-') { | |
800 | *str = argv[arg]; | |
801 | return 1; | |
802 | } | |
803 | ||
804 | return 0; | |
805 | } | |
806 | U_CDECL_END | |
807 | ||
b75a7d8f A |
808 | int main(int argc, char* argv[]) |
809 | { | |
810 | int32_t nerrors = 0; | |
811 | TestNode *root = NULL; | |
374ca955 | 812 | UErrorCode errorCode = U_ZERO_ERROR; |
73c04bcf A |
813 | UDate startTime, endTime; |
814 | int32_t diffTime; | |
815 | ||
729e4ab9 | 816 | startTime = uprv_getRawUTCtime(); |
374ca955 A |
817 | |
818 | /* Check whether ICU will initialize without forcing the build data directory into | |
819 | * the ICU_DATA path. Success here means either the data dll contains data, or that | |
820 | * this test program was run with ICU_DATA set externally. Failure of this check | |
821 | * is normal when ICU data is not packaged into a shared library. | |
822 | * | |
823 | * Whether or not this test succeeds, we want to cleanup and reinitialize | |
824 | * with a data path so that data loading from individual files can be tested. | |
825 | */ | |
826 | u_init(&errorCode); | |
827 | if (U_FAILURE(errorCode)) { | |
828 | fprintf(stderr, | |
829 | "#### Note: ICU Init without build-specific setDataDirectory() failed.\n"); | |
830 | } | |
831 | u_cleanup(); | |
832 | errorCode = U_ZERO_ERROR; | |
46f4442e A |
833 | if (!initArgs(argc, argv, argHandler, (void *) &STANDARD_TEST_FILE)) { |
834 | /* Error already displayed. */ | |
835 | return -1; | |
836 | } | |
374ca955 A |
837 | |
838 | /* Initialize ICU */ | |
839 | ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */ | |
840 | u_init(&errorCode); | |
841 | if (U_FAILURE(errorCode)) { | |
842 | fprintf(stderr, | |
729e4ab9 | 843 | "#### ERROR! %s: u_init() failed with status = \"%s\".\n" |
374ca955 A |
844 | "*** Check the ICU_DATA environment variable and \n" |
845 | "*** check that the data files are present.\n", argv[0], u_errorName(errorCode)); | |
846 | return 1; | |
847 | } | |
848 | ||
849 | fprintf(stdout, "Default charset for this run is %s\n", ucnv_getDefaultName()); | |
b75a7d8f A |
850 | |
851 | addAllTests(&root); | |
46f4442e | 852 | nerrors = runTestRequest(root, argc, argv); |
b75a7d8f | 853 | |
374ca955 A |
854 | #if 1 |
855 | { | |
856 | FILE* fileToRemove = fopen(STANDARD_TEST_FILE, "r"); | |
857 | /* This should delete any temporary files. */ | |
858 | if (fileToRemove) { | |
859 | fclose(fileToRemove); | |
729e4ab9 | 860 | log_verbose("Deleting: %s\n", STANDARD_TEST_FILE); |
374ca955 A |
861 | if (remove(STANDARD_TEST_FILE) != 0) { |
862 | /* Maybe someone didn't close the file correctly. */ | |
863 | fprintf(stderr, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE); | |
864 | nerrors += 1; | |
865 | } | |
866 | } | |
867 | } | |
868 | #endif | |
869 | ||
b75a7d8f | 870 | cleanUpTestTree(root); |
374ca955 | 871 | DataDrivenLogger::cleanUp(); |
b75a7d8f | 872 | u_cleanup(); |
73c04bcf | 873 | |
729e4ab9 | 874 | endTime = uprv_getRawUTCtime(); |
73c04bcf A |
875 | diffTime = (int32_t)(endTime - startTime); |
876 | printf("Elapsed Time: %02d:%02d:%02d.%03d\n", | |
877 | (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR), | |
878 | (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE), | |
879 | (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND), | |
880 | (int)(diffTime%U_MILLIS_PER_SECOND)); | |
881 | ||
b75a7d8f A |
882 | return nerrors; |
883 | } |