]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/udatatst.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / udatatst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1998-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6/*
7* File test.c
8*
9* Modification History:
10*
11* Date Name Description
12* 02/22/2000 Madhu Creation
13*******************************************************************************
14*/
15
16#include "unicode/utypes.h"
17#include "unicode/udata.h"
18#include "unicode/uchar.h"
19#include "unicode/ucnv.h"
20#include "unicode/ures.h"
21#include "unicode/ustring.h"
22#include "cmemory.h"
23#include "cstring.h"
24#include "filestrm.h"
25#include "cintltst.h"
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <stdlib.h>
31#include <stdio.h>
32
33#ifdef WIN32
34#include <io.h>
35#else
36#include <unistd.h>
37#endif
38
39static void TestUDataOpen(void);
40static void TestUDataOpenChoiceDemo1(void);
41static void TestUDataOpenChoiceDemo2(void);
42static void TestUDataGetInfo(void);
43static void TestUDataGetMemory(void);
44static void TestUDataSetAppData(void);
45static void TestErrorConditions(void);
46static void TestAppData(void);
47static void TestICUDataName(void);
48
49void addUDataTest(TestNode** root);
50
51void
52addUDataTest(TestNode** root)
53{
54 addTest(root, &TestUDataOpen, "udatatst/TestUDataOpen" );
55 addTest(root, &TestUDataOpenChoiceDemo1, "udatatst/TestUDataOpenChoiceDemo1");
56 addTest(root, &TestUDataOpenChoiceDemo2, "udatatst/TestUDataOpenChoiceDemo2");
57 addTest(root, &TestUDataGetInfo, "udatatst/TestUDataGetInfo" );
58 addTest(root, &TestUDataGetMemory, "udatatst/TestUDataGetMemory" );
59 addTest(root, &TestUDataSetAppData, "udatatst/TestUDataSetAppData" );
60 addTest(root, &TestErrorConditions, "udatatst/TestErrorConditions");
61 addTest(root, &TestAppData, "udatatst/TestAppData" );
62 addTest(root, &TestICUDataName, "udatatst/TestICUDataName" );
63
64}
65
66#if 0
67static void lots_of_mallocs()
68{
69 int q;
70 for(q=1;q<100;q++)
71 {
72 free(malloc(q));
73 malloc(q*2);
74 }
75
76}
77#endif
78
79static void TestUDataOpen(){
80 UDataMemory *result;
81 UErrorCode status=U_ZERO_ERROR;
82 const char* memMap[][2]={
83 {"tz", "icu"},
84 {"cnvalias", "icu"},
85 {"unames", "icu"},
86 {"ibm-37_P100-1995", "cnv"}
87 };
88 const char* name = "test";
89 const char* type = "icu";
90 const char dirSepString[] = {U_FILE_SEP_CHAR, 0};
91
92 char* path=(char*)malloc(sizeof(char) * (strlen(ctest_dataOutDir())
93 + strlen(U_ICUDATA_NAME)
94 + strlen("/build")+1 ) );
95
96 char *icuDataFilePath = 0;
97 struct stat stat_buf;
98
99 const char* testPath=loadTestData(&status);
100
101 /* lots_of_mallocs(); */
102
103 strcat(strcpy(path, ctest_dataOutDir()), U_ICUDATA_NAME);
104
105 log_verbose("Testing udata_open()\n");
106 result=udata_open(testPath, type, name, &status);
107 if(U_FAILURE(status)){
108 log_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", testPath, name, type, myErrorName(status));
109 } else {
110 log_verbose("PASS: udata_open worked\n");
111 udata_close(result);
112 }
113
114 /* If the ICU system common data file is present in this confiugration,
115 * verify that udata_open can explicitly fetch items from it.
116 * If packaging mode == dll, the file may not exist. So, if the file is
117 * missing, skip this test without error.
118 */
119 icuDataFilePath = (char *)malloc(strlen(path) + 10);
120 strcpy(icuDataFilePath, path);
121 strcat(icuDataFilePath, ".dat");
122 /* lots_of_mallocs(); */
123 if (stat(icuDataFilePath, &stat_buf) == 0)
124 {
125 int i;
126 log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
127 for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){
128 /* lots_of_mallocs(); */
129 status=U_ZERO_ERROR;
130 result=udata_open(path, memMap[i][1], memMap[i][0], &status);
131 if(U_FAILURE(status)) {
132 log_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", path, memMap[i][0], memMap[i][1], myErrorName(status));
133 } else {
134 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", path, memMap[i][0], memMap[i][1]);
135 udata_close(result);
136 }
137 }
138 }
139 else
140 {
141 /* lots_of_mallocs(); */
142 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
143 icuDataFilePath);
144 }
145 free(icuDataFilePath);
146 icuDataFilePath = NULL;
147 /* lots_of_mallocs(); */
148
149 /* If the ICU individual files used to build the ICU system common data are
150 * present in this configuration,
151 * verify that udata_open can explicitly open them.
152 * These data files are present in the ICU data/build directory after a build
153 * completes. Tests are most commonly run with the data directory pointing
154 * back into this directory structure, but this is not required. Soooo, if
155 * the files are missing, skip this test without error.
156 */
157 /* lots_of_mallocs(); */
158 icuDataFilePath = (char *)malloc(strlen(ctest_dataOutDir()) + 50);
159 strcpy(icuDataFilePath, ctest_dataOutDir());
160 strcat(icuDataFilePath, "build");
161 strcat(icuDataFilePath, dirSepString);
162 strcat(icuDataFilePath, U_ICUDATA_NAME);
163 strcat(icuDataFilePath, "_");
164 strcat(icuDataFilePath, "tz.icu");
165
166 /* lots_of_mallocs(); */
167 if (stat(icuDataFilePath, &stat_buf) == 0)
168 {
169 int i;
170 log_verbose("%s exists, so..\n", icuDataFilePath);
171 strcpy(icuDataFilePath, ctest_dataOutDir());
172 strcat(icuDataFilePath, "build");
173 strcat(icuDataFilePath, dirSepString);
174 strcat(icuDataFilePath, U_ICUDATA_NAME);
175 log_verbose("Testing udata_open() on %s\n", icuDataFilePath);
176 for(i=0; i<sizeof(memMap)/sizeof(memMap[0]); i++){
177 status=U_ZERO_ERROR;
178 result=udata_open(icuDataFilePath, memMap[i][1], memMap[i][0], &status);
179 if(U_FAILURE(status)) {
180 log_err("FAIL: udata_open() failed for path = %s, name=%s, type=%s, \n errorcode=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1], myErrorName(status));
181 } else {
182 log_verbose("PASS: udata_open worked for path = %s, name=%s, type=%s\n", icuDataFilePath, memMap[i][0], memMap[i][1]);
183 udata_close(result);
184 }
185 }
186 }
187 else
188 {
189 log_verbose("Skipping tests of udata_open() on %s. File not present in this configuration.\n",
190 icuDataFilePath);
191 }
192
193 free(icuDataFilePath);
194 icuDataFilePath = NULL;
195
196 /*
197 * Test fallback file names for open of separate data files.
198 * With these params to udata_open:
199 * path = wherever/testdata
200 * type = typ
201 * name = nam
202 * these files will be tried first:
203 * wherever/testudata_nam.typ
204 * testudata_nam.typ
205 * A test data file named testudata_nam.typ exists for the purpose of testing this.
206 */
207 log_verbose("Testing udata_open, with base_name.type style fallback to individual file.\n");
208
209 status = U_ZERO_ERROR;
210 result = udata_open( testPath, "typ", "nam", &status);
211 if (status != U_ZERO_ERROR) {
212 log_err("FAIL: udata_open( \"%s\", \"typ\", \"nam\") returned status %s\n", testPath, u_errorName(status));
213 }
214 udata_close(result);
215 free(icuDataFilePath);
216
217
218 /* This type of path is deprecated */
219 /*
220 * Another fallback test. Paths ending with a trailing directory separator
221 * take a slightly different code path, with the "base name" from the path
222 * being empty in the internal udata_open logic.
223 */
224
225/* log_verbose("Testing udata_open, with path containing a trailing directory separator.\n"); */
226/* icuDataFilePath = (char *)malloc(strlen(u_getDataDirectory()) + 50); */
227/* strcpy(icuDataFilePath, testPath); */
228/* status = U_ZERO_ERROR; */
229/* result = udata_open( icuDataFilePath, "cnv", "test1", &status); */
230/* if (status != U_ZERO_ERROR) { */
231/* log_err("FAIL: udata_open( \"%s\", \"cnv\", \"test1\") returned status %s\n", icuDataFilePath, u_errorName(status)); */
232/* } */
233/* udata_close(result); */
234/* free(icuDataFilePath); */
235
236
237 log_verbose("Testing udata_open() with a non existing binary file\n");
238 result=udata_open("testdata", "tst", "nonexist", &status);
239 if(status==U_FILE_ACCESS_ERROR){
240 log_verbose("Opening udata_open with non-existing file handled correctly.\n");
241 status=U_ZERO_ERROR;
242 } else {
243 log_err("calling udata_open with non-existing file [testdata | nonexist.tst] not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status));
244 if(U_SUCCESS(status)) {
245 udata_close(result);
246 }
247 }
248
249 if(result != NULL){
250 log_err("calling udata_open with non-existing file didn't return a null value\n");
251 } else {
252 log_verbose("calling udat_open with non-existing file returned null as expected\n");
253 }
254
255 free(path);
256}
257
258
259
260static void TestUDataSetAppData(){
261/* UDataMemory *dataItem;*/
262
263 UErrorCode status=U_ZERO_ERROR;
264 int fileHandle = 0; /* We are going to read the testdata.dat file */
265 struct stat statBuf;
266 size_t fileSize = 0;
267 char *fileBuf = 0;
268
269 size_t i;
270
271 /* Open the testdata.dat file, using normal */
272 const char* tdrelativepath = loadTestData(&status);
273 char* filePath=(char*)malloc(sizeof(char) * (strlen(tdrelativepath) + strlen(".dat") +1 +strlen(tdrelativepath)) );
274
275 strcpy(filePath, tdrelativepath);
276 strcat(filePath, ".dat");
277
278 log_verbose("Testing udata_setAppData() with %s\n", filePath);
279
280#ifdef WIN32
281 fileHandle = open( filePath, O_RDONLY | O_BINARY );
282#else
283 fileHandle = open( filePath, O_RDONLY);
284#endif
285 if( fileHandle == -1 ) {
286 log_err("FAIL: TestUDataSetAppData() can not open(\"%s\", O_RDONLY)\n", filePath);
287 goto cleanupAndReturn;
288 }
289
290 /*
291 *Find the size of testdata.dat, and read the whole thing into memory
292 */
293 if (fstat(fileHandle, &statBuf) == 0) {
294 fileSize = statBuf.st_size;
295 }
296 if (fileSize == 0) {
297 log_err("FAIL: TestUDataSetAppData() can not find size of file \"%s\".\n", filePath);
298 goto cleanupAndReturn;
299 }
300
301 fileBuf = (char *)ctst_malloc(fileSize);
302 if (fileBuf == 0) {
303 log_err("FAIL: TestUDataSetAppData() can not malloc(%d) for file \"%s\".\n", fileSize, filePath);
304 goto cleanupAndReturn;
305 }
306
307 i = read(fileHandle, fileBuf, fileSize);
308 if (i != fileSize) {
309 log_err("FAIL: TestUDataSetAppData() error reading file \"%s\".\n", filePath);
310 goto cleanupAndReturn;
311 }
312
313 /*
314 * Got testdata.dat into memory, now we try setAppData using the memory image.
315 */
316
317 status=U_ZERO_ERROR;
318 udata_setAppData("appData1", fileBuf, &status);
319 if (status != U_ZERO_ERROR) {
320 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) "
321 " returned status of %s\n", u_errorName(status));
322 goto cleanupAndReturn;
323 }
324
325 udata_setAppData("appData2", fileBuf, &status);
326 if (status != U_ZERO_ERROR) {
327 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
328 " returned status of %s\n", u_errorName(status));
329 goto cleanupAndReturn;
330 }
331
332 /* If we try to setAppData with the same name a second time, we should get a
333 * a using default warning.
334 */
335 udata_setAppData("appData2", fileBuf, &status);
336 if (status != U_USING_DEFAULT_WARNING) {
337 log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) "
338 " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status));
339 }
340
341
342 /** It is no longer correct to use udata_setAppData to change the
343 package of a contained item.
344
345 dataItem = udata_open("appData1", "res", "te_IN", &status); **/
346
347cleanupAndReturn:
348 /* Note: fileBuf is not deleted because ICU retains a pointer to it
349 * forever (until ICU is shut down).
350 */
351 if (fileHandle > 0) {
352 close(fileHandle);
353 }
354 free(filePath);
355 return;
356}
357
358
359static UBool U_CALLCONV
360isAcceptable1(void *context,
361 const char *type, const char *name,
362 const UDataInfo *pInfo) {
363
364 if( pInfo->size>=20 &&
365 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
366 pInfo->charsetFamily==U_CHARSET_FAMILY &&
367 pInfo->dataFormat[0]==0x43 && /* dataFormat="CvAl" */
368 pInfo->dataFormat[1]==0x76 &&
369 pInfo->dataFormat[2]==0x41 &&
370 pInfo->dataFormat[3]==0x6c &&
371 pInfo->formatVersion[0]==3 )
372 {
373 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable1()\n", name, type);
374 return TRUE;
375 } else {
376 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable1():-\n"
377 "\tsize = %d\n"
378 "\tisBigEndian = %d\n"
379 "\tcharsetFamily = %d\n"
380 "\tformatVersion[0] = %d\n"
381 "\tdataVersion[0] = %d\n"
382 "\tdataFormat = %c%c%c%c\n",
383 name, type, pInfo->size, pInfo->isBigEndian, pInfo->charsetFamily, pInfo->formatVersion[0],
384 pInfo->dataVersion[0], pInfo->dataFormat[0], pInfo->dataFormat[1], pInfo->dataFormat[2],
385 pInfo->dataFormat[3]);
386 log_verbose("Call another verifing function to accept the data\n");
387 return FALSE;
388 }
389}
390
391static UBool U_CALLCONV
392isAcceptable2(void *context,
393 const char *type, const char *name,
394 const UDataInfo *pInfo){
395 UVersionInfo unicodeVersion;
396
397 u_getUnicodeVersion(unicodeVersion);
398
399 if( pInfo->size>=20 &&
400 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
401 pInfo->charsetFamily==U_CHARSET_FAMILY &&
402 pInfo->dataFormat[0]==0x75 && /* dataFormat="unam" */
403 pInfo->dataFormat[1]==0x6e &&
404 pInfo->dataFormat[2]==0x61 &&
405 pInfo->dataFormat[3]==0x6d &&
406 pInfo->formatVersion[0]==1 &&
407 pInfo->dataVersion[0]==unicodeVersion[0] )
408 {
409 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable2()\n", name, type);
410 return TRUE;
411 } else {
412 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable2()\n", name, type);
413
414 return FALSE;
415 }
416
417
418}
419static UBool U_CALLCONV
420isAcceptable3(void *context,
421 const char *type, const char *name,
422 const UDataInfo *pInfo){
423
424 if( pInfo->size>=20 &&
425 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
426 pInfo->charsetFamily==U_CHARSET_FAMILY &&
427 pInfo->dataFormat[0]==0x54 && /* dataFormat="test" */
428 pInfo->dataFormat[1]==0x65 &&
429 pInfo->dataFormat[2]==0x73 &&
430 pInfo->dataFormat[3]==0x74 &&
431 pInfo->formatVersion[0]==1 &&
432 pInfo->dataVersion[0]==1 ) {
433 log_verbose("The data from \"%s.%s\" IS acceptable using the verifing function isAcceptable3()\n", name, type);
434
435 return TRUE;
436 } else {
437 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable3()\n", name, type);
438 return FALSE;
439 }
440
441
442}
443
444static void TestUDataOpenChoiceDemo1() {
445 UDataMemory *result;
446 UErrorCode status=U_ZERO_ERROR;
447
448 const char* name[]={
449 "cnvalias",
450 "unames",
451 "test"
452 };
453 const char* type="icu";
454 const char* testPath="testdata";
455
456 result=udata_openChoice(NULL, "icu", name[0], isAcceptable1, NULL, &status);
457 if(U_FAILURE(status)){
458 log_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[0], type, myErrorName(status));
459 } else {
460 log_verbose("PASS: udata_openChoice worked\n");
461 udata_close(result);
462 }
463
464 result=udata_openChoice(NULL, type, name[1], isAcceptable1, NULL, &status);
465 if(U_FAILURE(status)){
466 status=U_ZERO_ERROR;
467 result=udata_openChoice(NULL, type, name[1], isAcceptable2, NULL, &status);
468 if(U_FAILURE(status)){
469 log_err("FAIL: udata_openChoice() failed name=%s, type=%s, \n errorcode=%s\n", name[1], type, myErrorName(status));
470 }
471 }
472
473 if(U_SUCCESS(status)){
474 udata_close(result);
475 }
476
477 result=udata_openChoice(testPath, type, name[2], isAcceptable1, NULL, &status);
478 if(U_FAILURE(status)){
479 status=U_ZERO_ERROR;
480 result=udata_openChoice(testPath, type, name[2], isAcceptable3, NULL, &status);
481 if(U_FAILURE(status)){
482 log_err("FAIL: udata_openChoice() failed path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name[2], type, myErrorName(status));
483 }
484 }
485
486 if(U_SUCCESS(status)){
487 udata_close(result);
488 }
489}
490
491static UBool U_CALLCONV
492isAcceptable(void *context,
493 const char *type, const char *name,
494 const UDataInfo *pInfo){
495 if( pInfo->size>=20 &&
496 pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
497 pInfo->charsetFamily==U_CHARSET_FAMILY &&
498 pInfo->dataFormat[0]==0x54 && /* dataFormat="test" */
499 pInfo->dataFormat[1]==0x65 &&
500 pInfo->dataFormat[2]==0x73 &&
501 pInfo->dataFormat[3]==0x74 &&
502 pInfo->formatVersion[0]==1 &&
503 pInfo->dataVersion[0]==1 &&
504 *((int*)context) == 2 ) {
505 log_verbose("The data from\"%s.%s\" IS acceptable using the verifing function isAcceptable()\n", name, type);
506
507 return TRUE;
508 } else {
509 log_verbose("The data from \"%s.%s\" IS NOT acceptable using the verifing function isAcceptable()\n", name, type);
510 return FALSE;
511 }
512}
513
514
515/* This test checks to see if the isAcceptable function is being called correctly. */
516
517static void TestUDataOpenChoiceDemo2() {
518 UDataMemory *result;
519 UErrorCode status=U_ZERO_ERROR;
520 int i;
521 int p=2;
522
523 const char* name="test";
524 const char* type="icu";
525 const char* path = loadTestData(&status);
526
527 result=udata_openChoice(path, type, name, isAcceptable, &p, &status);
528 if(U_FAILURE(status)){
529 log_err("failed to load data at p=%s t=%s n=%s, isAcceptable", path, type, name);
530 }
531 if(U_SUCCESS(status) ) {
532 udata_close(result);
533 }
534
535 p=0;
536 for(i=0;i<2; i++){
537 result=udata_openChoice(path, type, name, isAcceptable, &p, &status);
538 if(p<2) {
539 if(U_FAILURE(status) && status==U_INVALID_FORMAT_ERROR){
540 log_verbose("Loads the data but rejects it as expected %s\n", myErrorName(status));
541 status=U_ZERO_ERROR;
542 p++;
543 }
544 else {
545 log_err("FAIL: failed to either load the data or to reject the loaded data. ERROR=%s\n", myErrorName(status) );
546 }
547 }
548 else if(p == 2) {
549 if(U_FAILURE(status)) {
550 log_err("FAIL: failed to load the data and accept it. ERROR=%s\n", myErrorName(status) );
551 }
552 else {
553 log_verbose("Loads the data and accepts it for p==2 as expected\n");
554 udata_close(result);
555 }
556 }
557 }
558}
559
560
561static void TestUDataGetInfo() {
562
563 UDataMemory *result;
564 /* UDataInfo cf. udata.h */
565 static UDataInfo dataInfo={
566 30, /*sizeof(UDataInfo),*/
567 0,
568
569 U_IS_BIG_ENDIAN,
570 U_CHARSET_FAMILY,
571 sizeof(UChar),
572 0,
573
574 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
575 {9, 0, 0, 0}, /* formatVersion */
576 {4, 0, 0, 0} /* dataVersion */
577 };
578 UErrorCode status=U_ZERO_ERROR;
579 const char* name="cnvalias";
580 const char* name2="test";
581 const char* type="icu";
582
583 const char* testPath=loadTestData(&status);
584
585 log_verbose("Testing udata_getInfo() for cnvalias.icu\n");
586 result=udata_open(NULL, "icu", name, &status);
587 if(U_FAILURE(status)){
588 log_err("FAIL: udata_open() failed for path = NULL, name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status));
589 return;
590 }
591 udata_getInfo(result, &dataInfo);
592 if(dataInfo.size==20 && dataInfo.size!=30 &&
593 dataInfo.isBigEndian==U_IS_BIG_ENDIAN &&
594 dataInfo.charsetFamily==U_CHARSET_FAMILY &&
595 dataInfo.dataFormat[0]==0x43 && dataInfo.dataFormat[0]!=0x54 && /* dataFormat="CvAl" and not "Test". The values are set for cnvalias.dat*/
596 dataInfo.dataFormat[1]==0x76 && dataInfo.dataFormat[1]!=0x65 &&
597 dataInfo.dataFormat[2]==0x41 && dataInfo.dataFormat[2]!=0x73 &&
598 dataInfo.dataFormat[3]==0x6c && dataInfo.dataFormat[3]!=0x74 &&
599 dataInfo.formatVersion[0]!=9 && /*formatVersion is also set to the one for cnvalias*/
600 dataInfo.dataVersion[0]!=4 && /*dataVersion*/
601 dataInfo.dataVersion[1]!=0 ){
602 log_verbose("PASS: udata_getInfo() filled in the right values\n");
603 } else {
604 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
605 }
606 udata_close(result);
607
608
609 log_verbose("Testing udata_getInfo() for test.icu\n");
610 result=udata_open(testPath, type, name2, &status);
611 if(U_FAILURE(status)) {
612 log_err("FAIL: udata_open() failed for path=%s name2=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status));
613 return;
614 }
615 udata_getInfo(result, &dataInfo);
616 if(dataInfo.size==20 &&
617 dataInfo.isBigEndian==U_IS_BIG_ENDIAN &&
618 dataInfo.charsetFamily==U_CHARSET_FAMILY &&
619 dataInfo.dataFormat[0]==0x54 && /* dataFormat="Test". The values are set for test.dat*/
620 dataInfo.dataFormat[1]==0x65 &&
621 dataInfo.dataFormat[2]==0x73 &&
622 dataInfo.dataFormat[3]==0x74 &&
623 dataInfo.formatVersion[0]==1 && /*formatVersion is also set to the one for test*/
624 dataInfo.dataVersion[0]==1 && /*dataVersion*/
625 dataInfo.dataVersion[1]==0 )
626 {
627 log_verbose("PASS: udata_getInfo() filled in the right values\n");
628 } else {
629 log_err("FAIL: udata_getInfo() filled in the wrong values\n");
630 }
631 udata_close(result);
632}
633
634static void TestUDataGetMemory() {
635
636 UDataMemory *result;
637 const int32_t *table=NULL;
638 uint16_t* intValue=0;
639 UErrorCode status=U_ZERO_ERROR;
640 const char* name="cnvalias";
641 const char* type;
642
643 const char* name2="test";
644
645 const char* testPath = loadTestData(&status);
646
647 type="icu";
648 log_verbose("Testing udata_getMemory() for \"cnvalias.icu\"\n");
649 result=udata_openChoice(NULL, type, name, isAcceptable1, NULL, &status);
650 if(U_FAILURE(status)){
651 log_err("FAIL: udata_openChoice() failed for name=%s, type=%s, \n errorcode=%s\n", name, type, myErrorName(status));
652 return;
653 }
654 table=(const int32_t *)udata_getMemory(result);
655
656 /* The alias table may list more converters than what's actually available now. [grhoten] */
657 if(ucnv_countAvailable() > table[1]) /*???*/
658 log_err("FAIL: udata_getMemory() failed ucnv_countAvailable returned = %d, expected = %d\n", ucnv_countAvailable(), table[1+2*(*table)]);
659
660 udata_close(result);
661
662 type="icu";
663 log_verbose("Testing udata_getMemory for \"test.icu\"()\n");
664 result=udata_openChoice(testPath, type, name2, isAcceptable3, NULL, &status);
665 if(U_FAILURE(status)){
666 log_err("FAIL: udata_openChoice() failed for path=%s name=%s, type=%s, \n errorcode=%s\n", testPath, name2, type, myErrorName(status));
667 return;
668 }
669 intValue=(uint16_t *)udata_getMemory(result);
670 /*printf("%d ..... %s", *(intValue), intValue+1));*/
671 if( *intValue != 2000 || strcmp((char*)(intValue+1), "YEAR") != 0 )
672 log_err("FAIL: udata_getMemory() failed: intValue :- Expected:2000 Got:%d \n\tstringValue:- Expected:YEAR Got:%s\n", *intValue, (intValue+1));
673
674 udata_close(result);
675
676}
677
678static void TestErrorConditions(){
679
680 UDataMemory *result=NULL;
681 UErrorCode status=U_ZERO_ERROR;
682 uint16_t* intValue=0;
683 static UDataInfo dataInfo={
684 30, /*sizeof(UDataInfo),*/
685 0,
686
687 U_IS_BIG_ENDIAN,
688 U_CHARSET_FAMILY,
689 sizeof(UChar),
690 0,
691
692 {0x54, 0x65, 0x73, 0x74}, /* dataFormat="Test" */
693 {9, 0, 0, 0}, /* formatVersion */
694 {4, 0, 0, 0} /* dataVersion */
695 };
696
697 const char* name = "test";
698 const char* type="icu";
699
700 const char *testPath = loadTestData(&status);
701
702 status = U_ILLEGAL_ARGUMENT_ERROR;
703 /*Try udata_open with status != U_ZERO_ERROR*/
704 log_verbose("Testing udata_open() with status != U_ZERO_ERROR\n");
705 result=udata_open(testPath, type, name, &status);
706 if(result != NULL){
707 log_err("FAIL: udata_open() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode !=U_ZERO_ERROR\n", testPath, name, type);
708 udata_close(result);
709
710 } else {
711 log_verbose("PASS: udata_open with errorCode != U_ZERO_ERROR failed as expected\n");
712 }
713
714 /*Try udata_open with data name=NULL*/
715 log_verbose("Testing udata_open() with data name=NULL\n");
716 status=U_ZERO_ERROR;
717 result=udata_open(testPath, type, NULL, &status);
718 if(U_FAILURE(status)){
719 if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){
720 log_err("FAIL: udata_open() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status));
721 }else{
722 log_verbose("PASS: udata_open with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status));
723 }
724 }else{
725 log_err("FAIL: udata_open() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath, type);
726 udata_close(result);
727 }
728
729
730 /*Try udata_openChoice with status != U_ZERO_ERROR*/
731 log_verbose("Testing udata_openChoice() with status != U_ZERO_ERROR\n");
732 status=U_ILLEGAL_ARGUMENT_ERROR;
733 result=udata_openChoice(testPath, type, name, isAcceptable3, NULL, &status);
734 if(result != NULL){
735 log_err("FAIL: udata_openChoice() is supposed to fail for path = %s, name=%s, type=%s, \n errorcode != U_ZERO_ERROR\n", testPath, name, type);
736 udata_close(result);
737 } else {
738 log_verbose("PASS: udata_openChoice() with errorCode != U_ZERO_ERROR failed as expected\n");
739 }
740
741 /*Try udata_open with data name=NULL*/
742 log_verbose("Testing udata_openChoice() with data name=NULL\n");
743 status=U_ZERO_ERROR;
744 result=udata_openChoice(testPath, type, NULL, isAcceptable3, NULL, &status);
745 if(U_FAILURE(status)){
746 if(status != U_ILLEGAL_ARGUMENT_ERROR || result != NULL){
747 log_err("FAIL: udata_openChoice() with name=NULL should return NULL and errocode U_ILLEGAL_ARGUMENT_ERROR, GOT: errorcode=%s\n", myErrorName(status));
748 }else{
749 log_verbose("PASS: udata_openChoice with name=NULL failed as expected and errorcode = %s as expected\n", myErrorName(status));
750 }
751 }else{
752 log_err("FAIL: udata_openChoice() with data name=NULL is supposed to fail for path = %s, name=NULL type=%s errorcode=U_ZERO_ERROR \n", testPath, type);
753 udata_close(result);
754 }
755
756 /*Try udata_getMemory with UDataMemory=NULL*/
757 log_verbose("Testing udata_getMemory with UDataMemory=NULL\n");
758 intValue=(uint16_t*)udata_getMemory(NULL);
759 if(intValue != NULL){
760 log_err("FAIL: udata_getMemory with UDataMemory = NULL is supposed to fail\n");
761 }
762
763 /*Try udata_getInfo with UDataMemory=NULL*/
764 status=U_ZERO_ERROR;
765 udata_getInfo(NULL, &dataInfo);
766 if(dataInfo.size != 0){
767 log_err("FAIL : udata_getInfo with UDataMemory = NULL us supposed to fail\n");
768 }
769
770 /*Try udata_openChoice with a non existing binary file*/
771 log_verbose("Testing udata_openChoice() with a non existing binary file\n");
772 result=udata_openChoice(testPath, "tst", "nonexist", isAcceptable3, NULL, &status);
773 if(status==U_FILE_ACCESS_ERROR){
774 log_verbose("Opening udata_openChoice with non-existing file handled correctly.\n");
775 status=U_ZERO_ERROR;
776 } else {
777 log_err("calling udata_open with non-existing file not handled correctly\n. Expected: U_FILE_ACCESS_ERROR, Got: %s\n", myErrorName(status));
778 if(U_SUCCESS(status)) {
779 udata_close(result);
780 }
781 }
782
783 if(result != NULL){
784 log_err("calling udata_open with non-existing file didn't return a null value\n");
785 } else {
786 log_verbose("calling udat_open with non-existing file returned null as expected\n");
787 }
788}
789
790/* Test whether apps and ICU can each have their own root.res */
791static void TestAppData()
792{
793 UResourceBundle *icu, *app;
794 UResourceBundle *tmp = NULL;
795 UResourceBundle *tmp2 = NULL;
796
797 const UChar *appString;
798 const UChar *icuString;
799
800 int32_t len;
801
802 UErrorCode status = U_ZERO_ERROR;
803 char testMsgBuf[256];
804
805 const char* testPath=loadTestData(&status);
806
807 icu = ures_open(NULL, "root", &status);
808 if(U_FAILURE(status))
809 {
810 log_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__, __LINE__, u_errorName(status));
811 return;
812 }
813 /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */
814 status = U_ZERO_ERROR;
815
816 app = ures_open(testPath, "root", &status);
817 if(U_FAILURE(status))
818 {
819 log_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__, __LINE__, testPath, u_errorName(status));
820 return;
821 }
822 /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */
823
824 tmp = ures_getByKey(icu, "Version", tmp, &status);
825 if(U_FAILURE(status))
826 {
827 log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status));
828 return;
829 }
830
831 icuString = ures_getString(tmp, &len, &status);
832 if(U_FAILURE(status))
833 {
834 log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status));
835 return;
836 }
837 /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */
838
839
840 tmp2 = ures_getByKey(app, "Version", tmp2, &status);
841 if(U_FAILURE(status))
842 {
843 log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status));
844 return;
845 }
846
847 appString = ures_getString(tmp2, &len, &status);
848 if(U_FAILURE(status))
849 {
850 log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status));
851 return;
852 }
853
854 /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */
855
856
857 if(!u_strcmp(icuString, appString))
858 {
859 log_err("%s:%d: Error! Expected ICU and App root version strings to be DIFFERENT but they are both %s and %s\n", __FILE__, __LINE__, austrdup(icuString),
860 austrdup(appString));
861 }
862 else
863 {
864 log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__,
865 __LINE__, u_austrcpy(testMsgBuf, appString), u_austrcpy(testMsgBuf, icuString));
866 }
867
868 ures_close(tmp);
869 ures_close(tmp2);
870 ures_close(icu);
871 ures_close(app);
872}
873
874static void TestICUDataName()
875{
876 UVersionInfo icuVersion;
877 char expectDataName[20];
878 unsigned int expectLen = 8;
879
880 char typeChar = '?';
881
882 /* Print out the version # we have .. */
883 log_verbose("utypes.h says U_ICUDATA_NAME = %s\n", U_ICUDATA_NAME);
884
885 /* Build up the version # we expect to get */
886 u_getVersion(icuVersion);
887
888 switch(U_CHARSET_FAMILY)
889 {
890 case U_ASCII_FAMILY:
891 switch(U_IS_BIG_ENDIAN)
892 {
893 case 1:
894 typeChar = 'b';
895 break;
896 case 0:
897 typeChar = 'l';
898 break;
899 default:
900 log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", (int)U_IS_BIG_ENDIAN);
901 /* return; */
902 }
903 break;
904 case U_EBCDIC_FAMILY:
905 typeChar = 'e';
906 break;
907 }
908
909 sprintf(expectDataName, "%s%d%d%c",
910 "icudt",
911 (int)icuVersion[0],
912 (int)icuVersion[1],
913 typeChar);
914
915 log_verbose("Expected: %s\n", expectDataName);
916 if(uprv_strlen(expectDataName) != expectLen)
917 {
918 log_err("*Expected* length is wrong (test err?), should be %d is %d\n",
919 expectLen, uprv_strlen(expectDataName));
920 }
921
922 if(uprv_strlen(U_ICUDATA_NAME) != expectLen)
923 {
924 log_err("U_ICUDATA_NAME length should be %d is %d\n",
925 expectLen, uprv_strlen(U_ICUDATA_NAME));
926 }
927
928 if(uprv_strcmp(U_ICUDATA_NAME, expectDataName))
929 {
930 log_err("U_ICUDATA_NAME should be %s but is %s\n",
931 expectDataName, U_ICUDATA_NAME);
932 }
933
934 /* ICUDATA_NAME comes from the build system on *nix */
935#ifdef ICUDATA_NAME
936 if(uprv_strcmp(U_ICUDATA_NAME, ICUDATA_NAME))
937 {
938 log_err("ICUDATA_NAME and U_ICUDATA_NAME don't match: "
939 "ICUDATA_NAME=%s, U_ICUDATA_NAME=%s. Check configure.in, icudefs.mk.in, utypes.h...\n", ICUDATA_NAME, U_ICUDATA_NAME);
940 }
941 else
942 {
943 log_verbose("ICUDATA_NAME=%s (from icudefs.mk), U_ICUDATA_NAME=%s (from utypes.h)\n", ICUDATA_NAME, U_ICUDATA_NAME);
944 }
945#endif
946
947}