2 *******************************************************************************
4 * Copyright (C) 2003-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: spreptst.c
10 * tab size: 8 (not used)
13 * created on: 2003jul11
14 * created by: Ram Viswanadha
16 #define USPREP_TYPE_NAMES_ARRAY
18 #include "unicode/utypes.h"
22 #include "unicode/ustring.h"
23 #include "unicode/putil.h"
25 #include "unicode/usprep.h"
33 parseMappings(const char *filename
, UStringPrepProfile
* data
, UBool reportError
, UErrorCode
*pErrorCode
);
36 compareMapping(UStringPrepProfile
* data
, uint32_t codepoint
, uint32_t* mapping
, int32_t mapLength
,
37 UStringPrepType option
);
40 compareFlagsForRange(UStringPrepProfile
* data
, uint32_t start
, uint32_t end
,UStringPrepType option
);
43 doStringPrepTest(const char* binFileName
, const char* txtFileName
, int32_t options
, UErrorCode
* errorCode
);
45 static void U_CALLCONV
46 strprepProfileLineFn(void *context
,
47 char *fields
[][2], int32_t fieldCount
,
48 UErrorCode
*pErrorCode
) {
53 UStringPrepProfile
* data
= (UStringPrepProfile
*) context
;
55 uint32_t rangeStart
=0,rangeEnd
=0;
57 typeName
= fields
[2][0];
60 if(strstr(typeName
, usprepTypeNames
[USPREP_UNASSIGNED
])!=NULL
){
62 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
65 compareFlagsForRange(data
, rangeStart
,rangeEnd
,USPREP_UNASSIGNED
);
67 }else if(strstr(typeName
, usprepTypeNames
[USPREP_PROHIBITED
])!=NULL
){
69 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
72 compareFlagsForRange(data
, rangeStart
,rangeEnd
,USPREP_PROHIBITED
);
74 }else if(strstr(typeName
, usprepTypeNames
[USPREP_MAP
])!=NULL
){
75 /* get the character code, field 0 */
76 code
=(uint32_t)uprv_strtoul(fields
[0][0], &end
, 16);
78 /* parse the mapping string */
79 length
=u_parseCodePoints(map
, mapping
, sizeof(mapping
)/4, pErrorCode
);
81 /* compare the mapping */
82 compareMapping(data
, code
,mapping
, length
,USPREP_MAP
);
84 *pErrorCode
= U_INVALID_FORMAT_ERROR
;
92 parseMappings(const char *filename
, UStringPrepProfile
* data
, UBool reportError
, UErrorCode
*pErrorCode
) {
95 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
99 u_parseDelimitedFile(filename
, ';', fields
, 3, strprepProfileLineFn
, (void*)data
, pErrorCode
);
101 /*fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);*/
103 if(U_FAILURE(*pErrorCode
) && (reportError
|| *pErrorCode
!=U_FILE_ACCESS_ERROR
)) {
104 log_err( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename
, u_errorName(*pErrorCode
));
109 static UStringPrepType
110 getValues(uint32_t result
, int32_t* value
, UBool
* isIndex
){
112 UStringPrepType type
;
115 * Initial value stored in the mapping table
116 * just return USPREP_TYPE_LIMIT .. so that
117 * the source codepoint is copied to the destination
119 type
= USPREP_TYPE_LIMIT
;
120 }else if(result
>= _SPREP_TYPE_THRESHOLD
){
121 type
= (UStringPrepType
) (result
- _SPREP_TYPE_THRESHOLD
);
125 /* ascertain if the value is index or delta */
128 *value
= result
>> 2;
132 *value
= (int16_t)result
;
133 *value
= (*value
>> 2);
136 if((result
>>2) == _SPREP_MAX_INDEX_VALUE
){
137 type
= USPREP_DELETE
;
146 compareMapping(UStringPrepProfile
* data
, uint32_t codepoint
, uint32_t* mapping
,int32_t mapLength
,
147 UStringPrepType type
){
150 UBool isIndex
= FALSE
;
151 UStringPrepType retType
;
152 int32_t value
=0, index
=0, delta
=0;
153 int32_t* indexes
= data
->indexes
;
154 UTrie trie
= data
->sprepTrie
;
155 const uint16_t* mappingData
= data
->mappingData
;
156 int32_t realLength
=0;
160 UTRIE_GET16(&trie
, codepoint
, result
);
161 retType
= getValues(result
,&value
,&isIndex
);
164 if(type
!= retType
&& retType
!= USPREP_DELETE
){
166 log_err( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint
, USPREP_MAP
, type
);
172 if(index
>= indexes
[_SPREP_ONE_UCHAR_MAPPING_INDEX_START
] &&
173 index
< indexes
[_SPREP_TWO_UCHARS_MAPPING_INDEX_START
]){
175 }else if(index
>= indexes
[_SPREP_TWO_UCHARS_MAPPING_INDEX_START
] &&
176 index
< indexes
[_SPREP_THREE_UCHARS_MAPPING_INDEX_START
]){
178 }else if(index
>= indexes
[_SPREP_THREE_UCHARS_MAPPING_INDEX_START
] &&
179 index
< indexes
[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START
]){
182 length
= mappingData
[index
++];
186 length
= (retType
== USPREP_DELETE
)? 0 : 1;
189 /* figure out the real length */
190 for(j
=0; j
<mapLength
; j
++){
191 if(mapping
[j
] > 0xFFFF){
198 if(realLength
!= length
){
199 log_err( "Did not get the expected length. Expected: %i Got: %i\n", mapLength
, length
);
203 for(i
=0; i
< mapLength
; i
++){
204 if(mapping
[i
] <= 0xFFFF){
205 if(mappingData
[index
+i
] != (uint16_t)mapping
[i
]){
206 log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping
[i
], mappingData
[index
+i
]);
209 UChar lead
= UTF16_LEAD(mapping
[i
]);
210 UChar trail
= UTF16_TRAIL(mapping
[i
]);
211 if(mappingData
[index
+i
] != lead
||
212 mappingData
[index
+i
+1] != trail
){
213 log_err( "Did not get the expected result. Expected: 0x%04X 0x%04X Got: 0x%04X 0x%04X\n", lead
, trail
, mappingData
[index
+i
], mappingData
[index
+i
+1]);
218 if(retType
!=USPREP_DELETE
&& (codepoint
-delta
) != (uint16_t)mapping
[0]){
219 log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping
[0],(codepoint
-delta
));
226 compareFlagsForRange(UStringPrepProfile
* data
,
227 uint32_t start
, uint32_t end
,
228 UStringPrepType type
){
231 UStringPrepType retType
;
234 UTrie trie
= data
->sprepTrie
;
236 // supplementary code point
237 UChar __lead16=UTF16_LEAD(0x2323E);
240 // get data for lead surrogate
241 (result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16));
242 __offset=(&idnTrie)->getFoldingOffset(result);
244 // get the real data from the folded lead/trail units
246 (result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff);
248 (result)=(uint32_t)((&idnTrie)->initialValue);
251 UTRIE_GET16(&idnTrie,0x2323E, result);
253 while(start
< end
+1){
254 UTRIE_GET16(&trie
,start
, result
);
255 retType
= getValues(result
, &value
, &isIndex
);
256 if(result
> _SPREP_TYPE_THRESHOLD
){
258 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start
,usprepTypeNames
[type
], usprepTypeNames
[retType
]);
261 if(type
== USPREP_PROHIBITED
&& ((result
& 0x01) != 0x01)){
262 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start
,usprepTypeNames
[type
], usprepTypeNames
[retType
]);
272 doStringPrepTest(const char* binFileName
, const char* txtFileName
, int32_t options
, UErrorCode
* errorCode
){
274 const char *testdatapath
= loadTestData(errorCode
);
275 const char *srcdatapath
= NULL
;
276 const char *relativepath
= NULL
;
277 char *filename
= NULL
;
278 UStringPrepProfile
* profile
= NULL
;
281 srcdatapath
= U_TOPSRCDIR
;
282 relativepath
= U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
284 srcdatapath
= ctest_dataOutDir();
285 relativepath
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
288 filename
= (char*) malloc(strlen(srcdatapath
)+strlen(relativepath
)+strlen(txtFileName
)+10 );
289 profile
= usprep_open(testdatapath
, binFileName
, errorCode
);
291 if(U_FAILURE(*errorCode
)){
292 log_err("Failed to load %s data file. Error: %s \n", binFileName
, u_errorName(*errorCode
));
295 /* open and load the txt file */
296 strcpy(filename
,srcdatapath
);
297 strcat(filename
,relativepath
);
298 strcat(filename
,txtFileName
);
300 parseMappings(filename
,profile
, TRUE
,errorCode
);
306 * Hey, Emacs, please set the following:
309 * indent-tabs-mode: nil