2 *******************************************************************************
4 * Copyright (C) 2003-2011, 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"
26 #include "unicode/utf16.h"
34 parseMappings(const char *filename
, UStringPrepProfile
* data
, UBool reportError
, UErrorCode
*pErrorCode
);
37 compareMapping(UStringPrepProfile
* data
, uint32_t codepoint
, uint32_t* mapping
, int32_t mapLength
,
38 UStringPrepType option
);
41 compareFlagsForRange(UStringPrepProfile
* data
, uint32_t start
, uint32_t end
,UStringPrepType option
);
44 doStringPrepTest(const char* binFileName
, const char* txtFileName
, int32_t options
, UErrorCode
* errorCode
);
46 static void U_CALLCONV
47 strprepProfileLineFn(void *context
,
48 char *fields
[][2], int32_t fieldCount
,
49 UErrorCode
*pErrorCode
) {
54 UStringPrepProfile
* data
= (UStringPrepProfile
*) context
;
56 uint32_t rangeStart
=0,rangeEnd
=0;
58 typeName
= fields
[2][0];
61 if(strstr(typeName
, usprepTypeNames
[USPREP_UNASSIGNED
])!=NULL
){
63 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
66 compareFlagsForRange(data
, rangeStart
,rangeEnd
,USPREP_UNASSIGNED
);
68 }else if(strstr(typeName
, usprepTypeNames
[USPREP_PROHIBITED
])!=NULL
){
70 u_parseCodePointRange(fields
[0][0], &rangeStart
,&rangeEnd
, pErrorCode
);
73 compareFlagsForRange(data
, rangeStart
,rangeEnd
,USPREP_PROHIBITED
);
75 }else if(strstr(typeName
, usprepTypeNames
[USPREP_MAP
])!=NULL
){
76 /* get the character code, field 0 */
77 code
=(uint32_t)uprv_strtoul(fields
[0][0], &end
, 16);
79 /* parse the mapping string */
80 length
=u_parseCodePoints(map
, mapping
, sizeof(mapping
)/4, pErrorCode
);
82 /* compare the mapping */
83 compareMapping(data
, code
,mapping
, length
,USPREP_MAP
);
85 *pErrorCode
= U_INVALID_FORMAT_ERROR
;
93 parseMappings(const char *filename
, UStringPrepProfile
* data
, UBool reportError
, UErrorCode
*pErrorCode
) {
96 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
100 u_parseDelimitedFile(filename
, ';', fields
, 3, strprepProfileLineFn
, (void*)data
, pErrorCode
);
102 /*fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);*/
104 if(U_FAILURE(*pErrorCode
) && (reportError
|| *pErrorCode
!=U_FILE_ACCESS_ERROR
)) {
105 log_err( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename
, u_errorName(*pErrorCode
));
110 static UStringPrepType
111 getValues(uint32_t result
, int32_t* value
, UBool
* isIndex
){
113 UStringPrepType type
;
116 * Initial value stored in the mapping table
117 * just return USPREP_TYPE_LIMIT .. so that
118 * the source codepoint is copied to the destination
120 type
= USPREP_TYPE_LIMIT
;
121 }else if(result
>= _SPREP_TYPE_THRESHOLD
){
122 type
= (UStringPrepType
) (result
- _SPREP_TYPE_THRESHOLD
);
126 /* ascertain if the value is index or delta */
129 *value
= result
>> 2;
133 *value
= (int16_t)result
;
134 *value
= (*value
>> 2);
137 if((result
>>2) == _SPREP_MAX_INDEX_VALUE
){
138 type
= USPREP_DELETE
;
147 compareMapping(UStringPrepProfile
* data
, uint32_t codepoint
, uint32_t* mapping
,int32_t mapLength
,
148 UStringPrepType type
){
151 UBool isIndex
= FALSE
;
152 UStringPrepType retType
;
153 int32_t value
=0, index
=0, delta
=0;
154 int32_t* indexes
= data
->indexes
;
155 UTrie trie
= data
->sprepTrie
;
156 const uint16_t* mappingData
= data
->mappingData
;
157 int32_t realLength
=0;
161 UTRIE_GET16(&trie
, codepoint
, result
);
162 retType
= getValues(result
,&value
,&isIndex
);
165 if(type
!= retType
&& retType
!= USPREP_DELETE
){
167 log_err( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint
, USPREP_MAP
, type
);
173 if(index
>= indexes
[_SPREP_ONE_UCHAR_MAPPING_INDEX_START
] &&
174 index
< indexes
[_SPREP_TWO_UCHARS_MAPPING_INDEX_START
]){
176 }else if(index
>= indexes
[_SPREP_TWO_UCHARS_MAPPING_INDEX_START
] &&
177 index
< indexes
[_SPREP_THREE_UCHARS_MAPPING_INDEX_START
]){
179 }else if(index
>= indexes
[_SPREP_THREE_UCHARS_MAPPING_INDEX_START
] &&
180 index
< indexes
[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START
]){
183 length
= mappingData
[index
++];
187 length
= (retType
== USPREP_DELETE
)? 0 : 1;
190 /* figure out the real length */
191 for(j
=0; j
<mapLength
; j
++){
192 if(mapping
[j
] > 0xFFFF){
199 if(realLength
!= length
){
200 log_err( "Did not get the expected length. Expected: %i Got: %i\n", mapLength
, length
);
204 for(i
=0; i
< mapLength
; i
++){
205 if(mapping
[i
] <= 0xFFFF){
206 if(mappingData
[index
+i
] != (uint16_t)mapping
[i
]){
207 log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping
[i
], mappingData
[index
+i
]);
210 UChar lead
= U16_LEAD(mapping
[i
]);
211 UChar trail
= U16_TRAIL(mapping
[i
]);
212 if(mappingData
[index
+i
] != lead
||
213 mappingData
[index
+i
+1] != trail
){
214 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]);
219 if(retType
!=USPREP_DELETE
&& (codepoint
-delta
) != (uint16_t)mapping
[0]){
220 log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping
[0],(codepoint
-delta
));
227 compareFlagsForRange(UStringPrepProfile
* data
,
228 uint32_t start
, uint32_t end
,
229 UStringPrepType type
){
232 UStringPrepType retType
;
235 UTrie trie
= data
->sprepTrie
;
237 // supplementary code point
238 UChar __lead16=U16_LEAD(0x2323E);
241 // get data for lead surrogate
242 (result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16));
243 __offset=(&idnTrie)->getFoldingOffset(result);
245 // get the real data from the folded lead/trail units
247 (result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff);
249 (result)=(uint32_t)((&idnTrie)->initialValue);
252 UTRIE_GET16(&idnTrie,0x2323E, result);
254 while(start
< end
+1){
255 UTRIE_GET16(&trie
,start
, result
);
256 retType
= getValues(result
, &value
, &isIndex
);
257 if(result
> _SPREP_TYPE_THRESHOLD
){
259 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start
,usprepTypeNames
[type
], usprepTypeNames
[retType
]);
262 if(type
== USPREP_PROHIBITED
&& ((result
& 0x01) != 0x01)){
263 log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start
,usprepTypeNames
[type
], usprepTypeNames
[retType
]);
273 doStringPrepTest(const char* binFileName
, const char* txtFileName
, int32_t options
, UErrorCode
* errorCode
){
275 const char *testdatapath
= loadTestData(errorCode
);
276 const char *srcdatapath
= NULL
;
277 const char *relativepath
= NULL
;
278 char *filename
= NULL
;
279 UStringPrepProfile
* profile
= NULL
;
282 srcdatapath
= U_TOPSRCDIR
;
283 relativepath
= U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
285 srcdatapath
= ctest_dataOutDir();
286 relativepath
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
289 profile
= usprep_open(testdatapath
, binFileName
, errorCode
);
291 if(*errorCode
== U_FILE_ACCESS_ERROR
) {
292 log_data_err("Failed to load %s data file. Error: %s \n", binFileName
, u_errorName(*errorCode
));
294 } else if(U_FAILURE(*errorCode
)){
295 log_err("Failed to load %s data file. Error: %s \n", binFileName
, u_errorName(*errorCode
));
298 filename
= (char*) malloc(strlen(srcdatapath
)+strlen(relativepath
)+strlen(txtFileName
)+10 );
299 /* open and load the txt file */
300 strcpy(filename
,srcdatapath
);
301 strcat(filename
,relativepath
);
302 strcat(filename
,txtFileName
);
304 parseMappings(filename
,profile
, TRUE
,errorCode
);
310 * Hey, Emacs, please set the following:
313 * indent-tabs-mode: nil