2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2003-07-24 23:23:19 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2003-2012, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2003-07-24 23:23:19 +00:00
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: spreptst.c
|
2017-02-03 18:57:23 +00:00
|
|
|
* encoding: UTF-8
|
2003-07-24 23:23:19 +00:00
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2003jul11
|
|
|
|
* created by: Ram Viswanadha
|
|
|
|
*/
|
2003-09-02 23:26:01 +00:00
|
|
|
#define USPREP_TYPE_NAMES_ARRAY
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-12-11 17:40:28 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_IDNA
|
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
#include "unicode/ustring.h"
|
2004-09-23 00:24:36 +00:00
|
|
|
#include "unicode/putil.h"
|
2003-07-24 23:23:19 +00:00
|
|
|
#include "cintltst.h"
|
|
|
|
#include "unicode/usprep.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2003-07-24 23:23:19 +00:00
|
|
|
#include "sprpimpl.h"
|
|
|
|
#include "uparse.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "ustr_imp.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
parseMappings(const char *filename, UStringPrepProfile* data, UBool reportError, UErrorCode *pErrorCode);
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
static void
|
|
|
|
compareMapping(UStringPrepProfile* data, uint32_t codepoint, uint32_t* mapping, int32_t mapLength,
|
2003-07-24 23:23:19 +00:00
|
|
|
UStringPrepType option);
|
|
|
|
|
|
|
|
static void
|
|
|
|
compareFlagsForRange(UStringPrepProfile* data, uint32_t start, uint32_t end,UStringPrepType option);
|
|
|
|
|
2003-12-02 03:12:49 +00:00
|
|
|
void
|
|
|
|
doStringPrepTest(const char* binFileName, const char* txtFileName, int32_t options, UErrorCode* errorCode);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
strprepProfileLineFn(void *context,
|
|
|
|
char *fields[][2], int32_t fieldCount,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
uint32_t mapping[40];
|
|
|
|
char *end, *map;
|
|
|
|
uint32_t code;
|
|
|
|
int32_t length;
|
|
|
|
UStringPrepProfile* data = (UStringPrepProfile*) context;
|
|
|
|
const char* typeName;
|
|
|
|
uint32_t rangeStart=0,rangeEnd =0;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
typeName = fields[2][0];
|
|
|
|
map = fields[1][0];
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
if(strstr(typeName, usprepTypeNames[USPREP_UNASSIGNED])!=NULL){
|
|
|
|
|
|
|
|
u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode);
|
|
|
|
|
|
|
|
/* store the range */
|
|
|
|
compareFlagsForRange(data, rangeStart,rangeEnd,USPREP_UNASSIGNED);
|
|
|
|
|
2003-07-25 20:12:44 +00:00
|
|
|
}else if(strstr(typeName, usprepTypeNames[USPREP_PROHIBITED])!=NULL){
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
u_parseCodePointRange(fields[0][0], &rangeStart,&rangeEnd, pErrorCode);
|
|
|
|
|
|
|
|
/* store the range */
|
|
|
|
compareFlagsForRange(data, rangeStart,rangeEnd,USPREP_PROHIBITED);
|
|
|
|
|
2003-07-25 20:12:44 +00:00
|
|
|
}else if(strstr(typeName, usprepTypeNames[USPREP_MAP])!=NULL){
|
2003-07-24 23:23:19 +00:00
|
|
|
/* get the character code, field 0 */
|
|
|
|
code=(uint32_t)uprv_strtoul(fields[0][0], &end, 16);
|
|
|
|
|
|
|
|
/* parse the mapping string */
|
|
|
|
length=u_parseCodePoints(map, mapping, sizeof(mapping)/4, pErrorCode);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
/* compare the mapping */
|
|
|
|
compareMapping(data, code,mapping, length,USPREP_MAP);
|
|
|
|
}else{
|
|
|
|
*pErrorCode = U_INVALID_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
parseMappings(const char *filename, UStringPrepProfile* data, UBool reportError, UErrorCode *pErrorCode) {
|
|
|
|
char *fields[3][2];
|
|
|
|
|
|
|
|
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_parseDelimitedFile(filename, ';', fields, 3, strprepProfileLineFn, (void*)data, pErrorCode);
|
|
|
|
|
2003-07-25 23:02:40 +00:00
|
|
|
/*fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);*/
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*pErrorCode) && (reportError || *pErrorCode!=U_FILE_ACCESS_ERROR)) {
|
|
|
|
log_err( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename, u_errorName(*pErrorCode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static UStringPrepType
|
|
|
|
getValues(uint32_t result, int32_t* value, UBool* isIndex){
|
|
|
|
|
|
|
|
UStringPrepType type;
|
|
|
|
if(result == 0){
|
2003-08-05 01:25:54 +00:00
|
|
|
/*
|
|
|
|
* Initial value stored in the mapping table
|
2003-07-24 23:23:19 +00:00
|
|
|
* just return USPREP_TYPE_LIMIT .. so that
|
|
|
|
* the source codepoint is copied to the destination
|
|
|
|
*/
|
|
|
|
type = USPREP_TYPE_LIMIT;
|
|
|
|
}else if(result >= _SPREP_TYPE_THRESHOLD){
|
|
|
|
type = (UStringPrepType) (result - _SPREP_TYPE_THRESHOLD);
|
|
|
|
}else{
|
|
|
|
/* get the type */
|
|
|
|
type = USPREP_MAP;
|
|
|
|
/* ascertain if the value is index or delta */
|
|
|
|
if(result & 0x02){
|
|
|
|
*isIndex = TRUE;
|
2003-08-05 01:25:54 +00:00
|
|
|
*value = result >> 2;
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
}else{
|
|
|
|
*isIndex = FALSE;
|
|
|
|
*value = (int16_t)result;
|
|
|
|
*value = (*value >> 2);
|
|
|
|
|
|
|
|
}
|
|
|
|
if((result>>2) == _SPREP_MAX_INDEX_VALUE){
|
|
|
|
type = USPREP_DELETE;
|
|
|
|
isIndex =FALSE;
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
static void
|
|
|
|
compareMapping(UStringPrepProfile* data, uint32_t codepoint, uint32_t* mapping,int32_t mapLength,
|
2003-07-24 23:23:19 +00:00
|
|
|
UStringPrepType type){
|
|
|
|
uint32_t result = 0;
|
|
|
|
int32_t length=0;
|
|
|
|
UBool isIndex = FALSE;
|
|
|
|
UStringPrepType retType;
|
2012-08-03 19:09:42 +00:00
|
|
|
int32_t value=0, idx=0, delta=0;
|
2003-07-24 23:23:19 +00:00
|
|
|
int32_t* indexes = data->indexes;
|
2003-08-05 01:25:54 +00:00
|
|
|
UTrie trie = data->sprepTrie;
|
2003-07-24 23:23:19 +00:00
|
|
|
const uint16_t* mappingData = data->mappingData;
|
|
|
|
int32_t realLength =0;
|
|
|
|
int32_t j=0;
|
|
|
|
int8_t i=0;
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
UTRIE_GET16(&trie, codepoint, result);
|
|
|
|
retType = getValues(result,&value,&isIndex);
|
|
|
|
|
|
|
|
|
|
|
|
if(type != retType && retType != USPREP_DELETE){
|
|
|
|
|
|
|
|
log_err( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint, USPREP_MAP, type);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isIndex){
|
2012-08-03 19:09:42 +00:00
|
|
|
idx = value;
|
|
|
|
if(idx >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] &&
|
|
|
|
idx < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){
|
2003-07-24 23:23:19 +00:00
|
|
|
length = 1;
|
2012-08-03 19:09:42 +00:00
|
|
|
}else if(idx >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] &&
|
|
|
|
idx < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){
|
2003-07-24 23:23:19 +00:00
|
|
|
length = 2;
|
2012-08-03 19:09:42 +00:00
|
|
|
}else if(idx >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] &&
|
|
|
|
idx < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){
|
2003-07-24 23:23:19 +00:00
|
|
|
length = 3;
|
|
|
|
}else{
|
2012-08-03 19:09:42 +00:00
|
|
|
length = mappingData[idx++];
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
delta = value;
|
|
|
|
length = (retType == USPREP_DELETE)? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/* figure out the real length */
|
2003-07-24 23:23:19 +00:00
|
|
|
for(j=0; j<mapLength; j++){
|
|
|
|
if(mapping[j] > 0xFFFF){
|
|
|
|
realLength +=2;
|
|
|
|
}else{
|
|
|
|
realLength++;
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(realLength != length){
|
|
|
|
log_err( "Did not get the expected length. Expected: %i Got: %i\n", mapLength, length);
|
|
|
|
}
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
if(isIndex){
|
|
|
|
for(i =0; i< mapLength; i++){
|
|
|
|
if(mapping[i] <= 0xFFFF){
|
2012-08-03 19:09:42 +00:00
|
|
|
if(mappingData[idx+i] != (uint16_t)mapping[i]){
|
|
|
|
log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[i], mappingData[idx+i]);
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
}else{
|
2011-07-27 05:53:56 +00:00
|
|
|
UChar lead = U16_LEAD(mapping[i]);
|
|
|
|
UChar trail = U16_TRAIL(mapping[i]);
|
2012-08-03 19:09:42 +00:00
|
|
|
if(mappingData[idx+i] != lead ||
|
|
|
|
mappingData[idx+i+1] != trail){
|
|
|
|
log_err( "Did not get the expected result. Expected: 0x%04X 0x%04X Got: 0x%04X 0x%04X\n", lead, trail, mappingData[idx+i], mappingData[idx+i+1]);
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(retType!=USPREP_DELETE && (codepoint-delta) != (uint16_t)mapping[0]){
|
|
|
|
log_err("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[0],(codepoint-delta));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-08-05 01:25:54 +00:00
|
|
|
compareFlagsForRange(UStringPrepProfile* data,
|
2003-07-24 23:23:19 +00:00
|
|
|
uint32_t start, uint32_t end,
|
|
|
|
UStringPrepType type){
|
|
|
|
|
|
|
|
uint32_t result =0 ;
|
|
|
|
UStringPrepType retType;
|
|
|
|
UBool isIndex=FALSE;
|
|
|
|
int32_t value=0;
|
2003-08-05 01:25:54 +00:00
|
|
|
UTrie trie = data->sprepTrie;
|
|
|
|
/*
|
|
|
|
// supplementary code point
|
2011-07-27 05:53:56 +00:00
|
|
|
UChar __lead16=U16_LEAD(0x2323E);
|
2003-07-24 23:23:19 +00:00
|
|
|
int32_t __offset;
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
// get data for lead surrogate
|
2003-07-24 23:23:19 +00:00
|
|
|
(result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16));
|
|
|
|
__offset=(&idnTrie)->getFoldingOffset(result);
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
// get the real data from the folded lead/trail units
|
2003-07-24 23:23:19 +00:00
|
|
|
if(__offset>0) {
|
|
|
|
(result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff);
|
|
|
|
} else {
|
|
|
|
(result)=(uint32_t)((&idnTrie)->initialValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
UTRIE_GET16(&idnTrie,0x2323E, result);
|
|
|
|
*/
|
|
|
|
while(start < end+1){
|
|
|
|
UTRIE_GET16(&trie,start, result);
|
|
|
|
retType = getValues(result, &value, &isIndex);
|
|
|
|
if(result > _SPREP_TYPE_THRESHOLD){
|
|
|
|
if(retType != type){
|
|
|
|
log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(type == USPREP_PROHIBITED && ((result & 0x01) != 0x01)){
|
|
|
|
log_err( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start++;
|
|
|
|
}
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
void
|
2003-07-24 23:23:19 +00:00
|
|
|
doStringPrepTest(const char* binFileName, const char* txtFileName, int32_t options, UErrorCode* errorCode){
|
|
|
|
|
|
|
|
const char *testdatapath = loadTestData(errorCode);
|
2004-08-04 23:40:31 +00:00
|
|
|
const char *srcdatapath = NULL;
|
|
|
|
const char *relativepath = NULL;
|
|
|
|
char *filename = NULL;
|
|
|
|
UStringPrepProfile* profile = NULL;
|
|
|
|
|
|
|
|
#ifdef U_TOPSRCDIR
|
|
|
|
srcdatapath = U_TOPSRCDIR;
|
|
|
|
relativepath = U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
|
|
|
|
#else
|
|
|
|
srcdatapath = ctest_dataOutDir();
|
|
|
|
relativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
|
|
|
|
#endif
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2004-08-04 23:40:31 +00:00
|
|
|
profile = usprep_open(testdatapath, binFileName, errorCode);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
2008-04-10 16:53:16 +00:00
|
|
|
if(*errorCode == U_FILE_ACCESS_ERROR) {
|
|
|
|
log_data_err("Failed to load %s data file. Error: %s \n", binFileName, u_errorName(*errorCode));
|
|
|
|
return;
|
|
|
|
} else if(U_FAILURE(*errorCode)){
|
2003-07-24 23:23:19 +00:00
|
|
|
log_err("Failed to load %s data file. Error: %s \n", binFileName, u_errorName(*errorCode));
|
|
|
|
return;
|
|
|
|
}
|
2011-06-10 18:56:08 +00:00
|
|
|
filename = (char*) malloc(strlen(srcdatapath)+strlen(relativepath)+strlen(txtFileName)+10 );
|
2003-07-24 23:23:19 +00:00
|
|
|
/* open and load the txt file */
|
2003-07-25 20:12:44 +00:00
|
|
|
strcpy(filename,srcdatapath);
|
|
|
|
strcat(filename,relativepath);
|
|
|
|
strcat(filename,txtFileName);
|
2003-07-24 23:23:19 +00:00
|
|
|
|
|
|
|
parseMappings(filename,profile, TRUE,errorCode);
|
2003-07-25 20:12:44 +00:00
|
|
|
|
|
|
|
free(filename);
|
2003-07-24 23:23:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Hey, Emacs, please set the following:
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
2003-08-05 01:25:54 +00:00
|
|
|
*/
|