2000-01-15 02:00:06 +00:00
/********************************************************************
* COPYRIGHT :
2001-03-21 19:46:49 +00:00
* Copyright ( c ) 1997 - 2001 , International Business Machines Corporation and
2000-01-15 02:00:06 +00:00
* others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/********************************************************************************
1999-08-16 21:50:52 +00:00
*
* File CAPITEST . C
*
* Modification History :
* Name Description
1999-10-18 22:48:32 +00:00
* Madhu Katragadda Ported for C API
1999-08-16 21:50:52 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/ /* C API TEST For COLLATOR */
2002-09-20 17:54:45 +00:00
1999-12-28 23:57:50 +00:00
# include "unicode/utypes.h"
2002-09-20 17:54:45 +00:00
# if !UCONFIG_NO_COLLATION
2001-03-08 18:18:13 +00:00
# include "ucol_imp.h"
1999-12-28 23:57:50 +00:00
# include "unicode/uloc.h"
1999-08-16 21:50:52 +00:00
# include "cintltst.h"
# include "capitst.h"
1999-12-28 23:57:50 +00:00
# include "unicode/ustring.h"
2000-08-02 19:53:47 +00:00
# include "unicode/ures.h"
2002-08-21 19:09:33 +00:00
# include "unicode/ucoleitr.h"
2000-08-14 23:14:23 +00:00
# include "cmemory.h"
2002-08-02 06:55:51 +00:00
# include "cstring.h"
2001-04-03 00:32:05 +00:00
# include "ccolltst.h"
2002-08-21 19:09:33 +00:00
# include <stdio.h>
2000-08-02 19:53:47 +00:00
2002-08-21 19:09:33 +00:00
static void TestAttribute ( void ) ;
2002-08-02 06:55:51 +00:00
void addCollAPITest ( TestNode * * root )
{
/* WEIVTODO: return tests here */
addTest ( root , & TestProperty , " tscoll/capitst/TestProperty " ) ;
addTest ( root , & TestRuleBasedColl , " tscoll/capitst/TestRuleBasedColl " ) ;
addTest ( root , & TestCompare , " tscoll/capitst/TestCompare " ) ;
addTest ( root , & TestSortKey , " tscoll/capitst/TestSortKey " ) ;
addTest ( root , & TestHashCode , " tscoll/capitst/TestHashCode " ) ;
addTest ( root , & TestElemIter , " tscoll/capitst/TestElemIter " ) ;
addTest ( root , & TestGetAll , " tscoll/capitst/TestGetAll " ) ;
/*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
addTest ( root , & TestDecomposition , " tscoll/capitst/TestDecomposition " ) ;
addTest ( root , & TestSafeClone , " tscoll/capitst/TestSafeClone " ) ;
addTest ( root , & TestGetSetAttr , " tscoll/capitst/TestGetSetAttr " ) ;
addTest ( root , & TestBounds , " tscoll/capitst/TestBounds " ) ;
addTest ( root , & TestGetLocale , " tscoll/capitst/TestGetLocale " ) ;
addTest ( root , & TestSortKeyBufferOverrun , " tscoll/capitst/TestSortKeyBufferOverrun " ) ;
2002-08-21 19:09:33 +00:00
addTest ( root , & TestAttribute , " tscoll/capitst/TestAttribute " ) ;
2002-09-17 06:27:51 +00:00
addTest ( root , & TestGetTailoredSet , " tscoll/capitst/TestGetTailoredSet " ) ;
2002-08-21 19:09:33 +00:00
2002-08-02 06:55:51 +00:00
}
void TestGetSetAttr ( void ) {
2001-11-13 22:55:05 +00:00
UErrorCode status = U_ZERO_ERROR ;
UCollator * coll = ucol_open ( NULL , & status ) ;
struct attrTest {
UColAttribute att ;
UColAttributeValue val [ 5 ] ;
uint32_t valueSize ;
UColAttributeValue nonValue ;
} attrs [ ] = {
{ UCOL_FRENCH_COLLATION , { UCOL_ON , UCOL_OFF } , 2 , UCOL_SHIFTED } ,
{ UCOL_ALTERNATE_HANDLING , { UCOL_NON_IGNORABLE , UCOL_SHIFTED } , 2 , UCOL_OFF } , /* attribute for handling variable elements*/
{ UCOL_CASE_FIRST , { UCOL_OFF , UCOL_LOWER_FIRST , UCOL_UPPER_FIRST } , 3 , UCOL_SHIFTED } , /* who goes first, lower case or uppercase */
{ UCOL_CASE_LEVEL , { UCOL_ON , UCOL_OFF } , 2 , UCOL_SHIFTED } , /* do we have an extra case level */
{ UCOL_NORMALIZATION_MODE , { UCOL_ON , UCOL_OFF } , 2 , UCOL_SHIFTED } , /* attribute for normalization */
{ UCOL_DECOMPOSITION_MODE , { UCOL_ON , UCOL_OFF } , 2 , UCOL_SHIFTED } ,
{ UCOL_STRENGTH , { UCOL_PRIMARY , UCOL_SECONDARY , UCOL_TERTIARY , UCOL_QUATERNARY , UCOL_IDENTICAL } , 5 , UCOL_SHIFTED } , /* attribute for strength */
{ UCOL_HIRAGANA_QUATERNARY_MODE , { UCOL_ON , UCOL_OFF } , 2 , UCOL_SHIFTED } , /* when turned on, this attribute */
} ;
UColAttribute currAttr ;
UColAttributeValue value ;
uint32_t i = 0 , j = 0 ;
for ( i = 0 ; i < sizeof ( attrs ) / sizeof ( attrs [ 0 ] ) ; i + + ) {
currAttr = attrs [ i ] . att ;
ucol_setAttribute ( coll , currAttr , UCOL_DEFAULT , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ucol_setAttribute with the default value returned error: %s \n " , u_errorName ( status ) ) ;
break ;
}
value = ucol_getAttribute ( coll , currAttr , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ucol_getAttribute returned error: %s \n " , u_errorName ( status ) ) ;
break ;
}
for ( j = 0 ; j < attrs [ i ] . valueSize ; j + + ) {
ucol_setAttribute ( coll , currAttr , attrs [ i ] . val [ j ] , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ucol_setAttribute with the value %i returned error: %s \n " , attrs [ i ] . val [ j ] , u_errorName ( status ) ) ;
break ;
}
}
status = U_ZERO_ERROR ;
ucol_setAttribute ( coll , currAttr , attrs [ i ] . nonValue , & status ) ;
if ( U_SUCCESS ( status ) ) {
log_err ( " ucol_setAttribute with the bad value didn't return an error \n " ) ;
break ;
}
status = U_ZERO_ERROR ;
ucol_setAttribute ( coll , currAttr , value , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ucol_setAttribute with the default valuereturned error: %s \n " , u_errorName ( status ) ) ;
break ;
}
}
status = U_ZERO_ERROR ;
value = ucol_getAttribute ( coll , UCOL_ATTRIBUTE_COUNT , & status ) ;
if ( U_SUCCESS ( status ) ) {
log_err ( " ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error \n " ) ;
}
status = U_ZERO_ERROR ;
ucol_setAttribute ( coll , UCOL_ATTRIBUTE_COUNT , UCOL_DEFAULT , & status ) ;
if ( U_SUCCESS ( status ) ) {
log_err ( " ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error \n " ) ;
}
status = U_ZERO_ERROR ;
2001-11-14 19:32:46 +00:00
ucol_close ( coll ) ;
2001-11-13 22:55:05 +00:00
}
2000-08-02 19:53:47 +00:00
1999-08-16 21:50:52 +00:00
static void doAssert ( int condition , const char * message )
{
if ( condition = = 0 ) {
1999-11-23 22:46:47 +00:00
log_err ( " ERROR : %s \n " , message ) ;
1999-08-16 21:50:52 +00:00
}
}
2001-02-26 10:56:43 +00:00
#if 0
/* We don't have default rules, at least not in the previous sense */
2000-08-02 19:53:47 +00:00
void TestGetDefaultRules ( ) {
2000-08-09 23:34:41 +00:00
uint32_t size = 0 ;
2000-08-02 19:53:47 +00:00
UErrorCode status = U_ZERO_ERROR ;
UCollator * coll = NULL ;
int32_t len1 = 0 , len2 = 0 ;
2000-08-09 23:34:41 +00:00
uint8_t * binColData = NULL ;
2000-08-02 19:53:47 +00:00
UResourceBundle * res = NULL ;
2000-08-09 23:34:41 +00:00
UResourceBundle * binColl = NULL ;
uint8_t * binResult = NULL ;
2000-08-02 19:53:47 +00:00
const UChar * defaultRulesArray = ucol_getDefaultRulesArray ( & size ) ;
log_verbose ( " Test the function ucol_getDefaultRulesArray() \n " ) ;
2001-09-22 01:24:03 +00:00
coll = ucol_openRules ( defaultRulesArray , size , UCOL_ON , UCOL_PRIMARY , & status ) ;
2000-08-09 23:34:41 +00:00
if ( U_SUCCESS ( status ) & & coll ! = NULL ) {
binColData = ( uint8_t * ) ucol_cloneRuleData ( coll , & len1 , & status ) ;
2000-08-02 19:53:47 +00:00
}
status = U_ZERO_ERROR ;
res = ures_open ( NULL , " root " , & status ) ;
2000-08-09 23:34:41 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " ERROR: Failed to get resource for \" root Locale \" with %s " , myErrorName ( status ) ) ;
return ;
}
2000-08-02 19:53:47 +00:00
binColl = ures_getByKey ( res , " %%Collation " , binColl , & status ) ;
if ( U_SUCCESS ( status ) ) {
2000-08-09 23:34:41 +00:00
binResult = ( uint8_t * ) ures_getBinary ( binColl , & len2 , & status ) ;
2000-08-02 19:53:47 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " ERROR: ures_getBinary() failed \n " ) ;
}
} else {
2000-08-09 23:34:41 +00:00
log_err ( " ERROR: ures_getByKey(locale(default), %%Collation) failed " ) ;
}
2000-08-02 19:53:47 +00:00
if ( len1 ! = len2 ) {
log_err ( " Error: ucol_getDefaultRulesArray() failed to return the correct length. \n " ) ;
}
if ( memcmp ( binColData , binResult , len1 ) ! = 0 ) {
log_err ( " Error: ucol_getDefaultRulesArray() failed \n " ) ;
}
2002-07-29 21:04:18 +00:00
free ( binColData ) ;
2000-08-14 23:14:23 +00:00
ures_close ( binColl ) ;
2000-08-02 19:53:47 +00:00
ures_close ( res ) ;
ucol_close ( coll ) ;
}
2001-02-26 10:56:43 +00:00
# endif
1999-08-16 21:50:52 +00:00
/* Collator Properties
ucol_open , ucol_strcoll , getStrength / setStrength
getDecomposition / setDecomposition , getDisplayName */
void TestProperty ( )
{
2001-03-03 09:32:02 +00:00
UCollator * col , * ruled ;
1999-08-16 21:50:52 +00:00
UChar * disName ;
2001-06-26 22:28:11 +00:00
int32_t len = 0 , i = 0 ;
1999-08-16 21:50:52 +00:00
UChar * source , * target ;
int32_t tempLength ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
2001-11-13 16:59:35 +00:00
/*
All the collations have the same version in an ICU
version .
ICU 2.0 currVersionArray = { 0x18 , 0xC0 , 0x02 , 0x02 } ;
2002-03-26 16:47:39 +00:00
ICU 2.1 currVersionArray = { 0x19 , 0x00 , 0x03 , 0x03 } ;
2002-07-20 04:38:14 +00:00
ICU 2.2 currVersionArray = { 0x21 , 0x40 , 0x04 , 0x04 } ;
2001-02-28 02:45:01 +00:00
*/
2002-07-20 04:38:14 +00:00
UVersionInfo currVersionArray = { 0x21 , 0x40 , 0x04 , 0x04 } ;
2000-05-15 19:48:56 +00:00
UVersionInfo versionArray ;
1999-08-16 21:50:52 +00:00
log_verbose ( " The property tests begin : \n " ) ;
log_verbose ( " Test ucol_strcoll : \n " ) ;
2001-03-20 20:11:48 +00:00
col = ucol_open ( " en_US " , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Default Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
2001-11-13 16:59:35 +00:00
2000-05-15 19:48:56 +00:00
ucol_getVersion ( col , versionArray ) ;
2000-08-09 22:49:33 +00:00
for ( i = 0 ; i < 4 ; + + i ) {
2001-11-13 16:59:35 +00:00
if ( versionArray [ i ] ! = currVersionArray [ i ] ) {
2000-05-15 19:48:56 +00:00
log_err ( " Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d \n " ,
versionArray [ 0 ] , versionArray [ 1 ] , versionArray [ 2 ] , versionArray [ 3 ] ) ;
2000-08-09 22:49:33 +00:00
break ;
2000-05-15 19:48:56 +00:00
}
2000-08-09 22:49:33 +00:00
}
2001-06-26 22:28:11 +00:00
1999-08-16 21:50:52 +00:00
source = ( UChar * ) malloc ( sizeof ( UChar ) * 12 ) ;
target = ( UChar * ) malloc ( sizeof ( UChar ) * 12 ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
u_uastrcpy ( source , " ab " ) ;
u_uastrcpy ( target , " abc " ) ;
doAssert ( ( ucol_strcoll ( col , source , u_strlen ( source ) , target , u_strlen ( target ) ) = = UCOL_LESS ) , " ab < abc comparison failed " ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
u_uastrcpy ( source , " ab " ) ;
u_uastrcpy ( target , " AB " ) ;
doAssert ( ( ucol_strcoll ( col , source , u_strlen ( source ) , target , u_strlen ( target ) ) = = UCOL_LESS ) , " ab < AB comparison failed " ) ;
2001-03-13 07:30:28 +00:00
/* u_uastrcpy(source, "black-bird");
u_uastrcpy ( target , " blackbird " ) ; */
u_uastrcpy ( target , " black-bird " ) ;
u_uastrcpy ( source , " blackbird " ) ;
1999-08-16 21:50:52 +00:00
doAssert ( ( ucol_strcoll ( col , source , u_strlen ( source ) , target , u_strlen ( target ) ) = = UCOL_GREATER ) ,
" black-bird > blackbird comparison failed " ) ;
u_uastrcpy ( source , " black bird " ) ;
u_uastrcpy ( target , " black-bird " ) ;
doAssert ( ( ucol_strcoll ( col , source , u_strlen ( source ) , target , u_strlen ( target ) ) = = UCOL_LESS ) ,
" black bird < black-bird comparison failed " ) ;
u_uastrcpy ( source , " Hello " ) ;
u_uastrcpy ( target , " hello " ) ;
doAssert ( ( ucol_strcoll ( col , source , u_strlen ( source ) , target , u_strlen ( target ) ) = = UCOL_GREATER ) ,
" Hello > hello comparison failed " ) ;
free ( source ) ;
free ( target ) ;
log_verbose ( " Test ucol_strcoll ends. \n " ) ;
log_verbose ( " testing ucol_getStrength() method ... \n " ) ;
doAssert ( ( ucol_getStrength ( col ) = = UCOL_TERTIARY ) , " collation object has the wrong strength " ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_PRIMARY ) , " collation object's strength is primary difference " ) ;
log_verbose ( " testing ucol_setStrength() method ... \n " ) ;
ucol_setStrength ( col , UCOL_SECONDARY ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_TERTIARY ) , " collation object's strength is secondary difference " ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_PRIMARY ) , " collation object's strength is primary difference " ) ;
doAssert ( ( ucol_getStrength ( col ) = = UCOL_SECONDARY ) , " collation object has the wrong strength " ) ;
2002-08-21 19:09:33 +00:00
# ifdef ICU_NORMALIZER_USE_DEPRECATES
1999-08-16 21:50:52 +00:00
log_verbose ( " testing ucol_setDecomposition() method ... \n " ) ;
2001-09-22 01:24:03 +00:00
ucol_setNormalization ( col , UNORM_NONE ) ;
doAssert ( ( ucol_getNormalization ( col ) ! = UNORM_NFC ) , " collation object's normalization mode is Canonical decomposition followed by canonical composition " ) ;
doAssert ( ( ucol_getNormalization ( col ) ! = UNORM_NFD ) , " collation object's normalization mode is canonical decomposition " ) ;
doAssert ( ( ucol_getNormalization ( col ) = = UNORM_NONE ) , " collation object has the wrong normalization mode " ) ;
2002-08-21 19:09:33 +00:00
# endif
1999-08-16 21:50:52 +00:00
log_verbose ( " Get display name for the default collation in German : \n " ) ;
len = ucol_getDisplayName ( " en_US " , " de_DE " , NULL , 0 , & status ) ;
1999-10-07 00:07:53 +00:00
if ( status = = U_BUFFER_OVERFLOW_ERROR ) {
status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
disName = ( UChar * ) malloc ( sizeof ( UChar ) * ( len + 1 ) ) ;
ucol_getDisplayName ( " en_US " , " de_DE " , disName , len + 1 , & status ) ;
2000-08-09 23:34:41 +00:00
log_verbose ( " the display name for default collation in german: %s \n " , austrdup ( disName ) ) ;
free ( disName ) ;
1999-08-16 21:50:52 +00:00
}
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: in getDisplayName: %s \n " , myErrorName ( status ) ) ;
return ;
}
log_verbose ( " Default collation getDisplayName ended. \n " ) ;
2001-03-03 09:32:02 +00:00
ruled = ucol_open ( " da_DK " , & status ) ;
1999-08-16 21:50:52 +00:00
log_verbose ( " ucol_getRules() testing ... \n " ) ;
2001-03-03 09:32:02 +00:00
ucol_getRules ( ruled , & tempLength ) ;
1999-08-16 21:50:52 +00:00
doAssert ( tempLength ! = 0 , " getRules() result incorrect " ) ;
log_verbose ( " getRules tests end. \n " ) ;
2001-03-01 20:17:12 +00:00
{
2002-07-29 21:04:18 +00:00
UChar * buffer = ( UChar * ) malloc ( 200000 * sizeof ( UChar ) ) ;
2001-03-01 20:17:12 +00:00
int32_t bufLen = 200000 ;
2001-05-03 00:08:09 +00:00
buffer [ 0 ] = ' \0 ' ;
2001-03-01 20:17:12 +00:00
log_verbose ( " ucol_getRulesEx() testing ... \n " ) ;
tempLength = ucol_getRulesEx ( col , UCOL_TAILORING_ONLY , buffer , bufLen ) ;
doAssert ( tempLength = = 0 , " getRulesEx() result incorrect " ) ;
log_verbose ( " getRules tests end. \n " ) ;
log_verbose ( " ucol_getRulesEx() testing ... \n " ) ;
tempLength = ucol_getRulesEx ( col , UCOL_FULL_RULES , buffer , bufLen ) ;
doAssert ( tempLength ! = 0 , " getRulesEx() result incorrect " ) ;
log_verbose ( " getRules tests end. \n " ) ;
2002-07-29 21:04:18 +00:00
free ( buffer ) ;
2001-03-01 20:17:12 +00:00
}
2001-03-03 09:32:02 +00:00
ucol_close ( ruled ) ;
1999-08-16 21:50:52 +00:00
ucol_close ( col ) ;
2001-03-01 20:17:12 +00:00
1999-08-16 21:50:52 +00:00
log_verbose ( " open an collator for french locale " ) ;
col = ucol_open ( " fr_FR " , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Creating French collation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
ucol_setStrength ( col , UCOL_PRIMARY ) ;
log_verbose ( " testing ucol_getStrength() method again ... \n " ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_TERTIARY ) , " collation object has the wrong strength " ) ;
doAssert ( ( ucol_getStrength ( col ) = = UCOL_PRIMARY ) , " collation object's strength is not primary difference " ) ;
log_verbose ( " testing French ucol_setStrength() method ... \n " ) ;
ucol_setStrength ( col , UCOL_TERTIARY ) ;
doAssert ( ( ucol_getStrength ( col ) = = UCOL_TERTIARY ) , " collation object's strength is not tertiary difference " ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_PRIMARY ) , " collation object's strength is primary difference " ) ;
doAssert ( ( ucol_getStrength ( col ) ! = UCOL_SECONDARY ) , " collation object's strength is secondary difference " ) ;
ucol_close ( col ) ;
log_verbose ( " Get display name for the french collation in english : \n " ) ;
len = ucol_getDisplayName ( " fr_FR " , " en_US " , NULL , 0 , & status ) ;
1999-10-07 00:07:53 +00:00
if ( status = = U_BUFFER_OVERFLOW_ERROR ) {
status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
disName = ( UChar * ) malloc ( sizeof ( UChar ) * ( len + 1 ) ) ;
ucol_getDisplayName ( " fr_FR " , " en_US " , disName , len + 1 , & status ) ;
2000-08-09 23:34:41 +00:00
log_verbose ( " the display name for french collation in english: %s \n " , austrdup ( disName ) ) ;
free ( disName ) ;
1999-08-16 21:50:52 +00:00
}
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: in getDisplayName: %s \n " , myErrorName ( status ) ) ;
return ;
}
log_verbose ( " Default collation getDisplayName ended. \n " ) ;
}
2001-03-14 00:23:10 +00:00
1999-08-16 21:50:52 +00:00
/* Test RuleBasedCollator and getRules*/
void TestRuleBasedColl ( )
{
UCollator * col1 , * col2 , * col3 , * col4 ;
2002-07-12 20:33:01 +00:00
UCollationElements * iter1 , * iter2 ;
1999-08-16 21:50:52 +00:00
UChar ruleset1 [ 60 ] ;
UChar ruleset2 [ 50 ] ;
2002-07-12 20:33:01 +00:00
UChar teststr [ 10 ] ;
UChar teststr2 [ 10 ] ;
1999-08-16 21:50:52 +00:00
const UChar * rule1 , * rule2 , * rule3 , * rule4 ;
int32_t tempLength ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
2001-03-03 09:32:02 +00:00
u_uastrcpy ( ruleset1 , " &9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E " ) ;
u_uastrcpy ( ruleset2 , " &9 < a, A < b, B < c, C < d, D, e, E " ) ;
1999-08-16 21:50:52 +00:00
2001-09-22 01:24:03 +00:00
col1 = ucol_openRules ( ruleset1 , u_strlen ( ruleset1 ) , UCOL_DEFAULT , UCOL_DEFAULT_STRENGTH , NULL , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " RuleBased Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
else
log_verbose ( " PASS: RuleBased Collator creation passed \n " ) ;
1999-10-07 00:07:53 +00:00
status = U_ZERO_ERROR ;
2001-09-22 01:24:03 +00:00
col2 = ucol_openRules ( ruleset2 , u_strlen ( ruleset2 ) , UCOL_DEFAULT , UCOL_DEFAULT_STRENGTH , NULL , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " RuleBased Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
else
log_verbose ( " PASS: RuleBased Collator creation passed \n " ) ;
1999-10-07 00:07:53 +00:00
status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
col3 = ucol_open ( NULL , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Default Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
else
log_verbose ( " PASS: Default Collator creation passed \n " ) ;
rule1 = ucol_getRules ( col1 , & tempLength ) ;
rule2 = ucol_getRules ( col2 , & tempLength ) ;
rule3 = ucol_getRules ( col3 , & tempLength ) ;
doAssert ( ( u_strcmp ( rule1 , rule2 ) ! = 0 ) , " Default collator getRules failed " ) ;
doAssert ( ( u_strcmp ( rule2 , rule3 ) ! = 0 ) , " Default collator getRules failed " ) ;
doAssert ( ( u_strcmp ( rule1 , rule3 ) ! = 0 ) , " Default collator getRules failed " ) ;
2001-09-22 01:24:03 +00:00
col4 = ucol_openRules ( rule2 , u_strlen ( rule2 ) , UCOL_DEFAULT , UCOL_DEFAULT_STRENGTH , NULL , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " RuleBased Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
rule4 = ucol_getRules ( col4 , & tempLength ) ;
doAssert ( ( u_strcmp ( rule2 , rule4 ) = = 0 ) , " Default collator getRules failed " ) ;
ucol_close ( col1 ) ;
ucol_close ( col2 ) ;
ucol_close ( col3 ) ;
ucol_close ( col4 ) ;
2002-07-12 20:33:01 +00:00
/* tests that modifier ! is always ignored */
u_uastrcpy ( ruleset1 , " !&a<b " ) ;
teststr [ 0 ] = 0x0e40 ;
teststr [ 1 ] = 0x0e01 ;
teststr [ 2 ] = 0x0e2d ;
col1 = ucol_openRules ( ruleset1 , u_strlen ( ruleset1 ) , UCOL_DEFAULT , UCOL_DEFAULT_STRENGTH , NULL , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " RuleBased Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
col2 = ucol_open ( " en_US " , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " en_US Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
iter1 = ucol_openElements ( col1 , teststr , 3 , & status ) ;
iter2 = ucol_openElements ( col2 , teststr , 3 , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ERROR: CollationElement iterator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
while ( TRUE ) {
/* testing with en since thai has its own tailoring */
uint32_t ce = ucol_next ( iter1 , & status ) ;
uint32_t ce2 = ucol_next ( iter2 , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " ERROR: CollationElement iterator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
if ( ce2 ! = ce ) {
log_err ( " ! modifier test failed " ) ;
}
if ( ce = = UCOL_NULLORDER ) {
break ;
}
}
ucol_closeElements ( iter1 ) ;
ucol_closeElements ( iter2 ) ;
ucol_close ( col1 ) ;
ucol_close ( col2 ) ;
/* test that we can start a rule without a & or < */
2002-08-01 20:54:18 +00:00
u_uastrcpy ( ruleset1 , " < z < a " ) ;
2002-07-12 20:33:01 +00:00
col1 = ucol_openRules ( ruleset1 , u_strlen ( ruleset1 ) , UCOL_DEFAULT , UCOL_DEFAULT_STRENGTH , NULL , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " RuleBased Collator creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
u_uastrcpy ( teststr , " z " ) ;
u_uastrcpy ( teststr2 , " a " ) ;
if ( ucol_greaterOrEqual ( col1 , teststr , 1 , teststr2 , 1 ) ) {
log_err ( " Rule \" z < a \" fails " ) ;
}
ucol_close ( col1 ) ;
1999-08-16 21:50:52 +00:00
}
void TestCompare ( )
{
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
UCollator * col ;
UChar * test1 ;
UChar * test2 ;
log_verbose ( " The compare tests begin : \n " ) ;
1999-10-07 00:07:53 +00:00
status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
col = ucol_open ( " en_US " , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ucal_open() collation creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
test1 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
test2 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
u_uastrcpy ( test1 , " Abcda " ) ;
u_uastrcpy ( test2 , " abcda " ) ;
log_verbose ( " Use tertiary comparison level testing .... \n " ) ;
doAssert ( ( ! ucol_equal ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" != \" abcda \" " ) ;
doAssert ( ( ucol_greater ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" >>> \" abcda \" " ) ;
doAssert ( ( ucol_greaterOrEqual ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" >>> \" abcda \" " ) ;
ucol_setStrength ( col , UCOL_SECONDARY ) ;
log_verbose ( " Use secondary comparison level testing .... \n " ) ;
doAssert ( ( ucol_equal ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
doAssert ( ( ! ucol_greater ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
doAssert ( ( ucol_greaterOrEqual ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
ucol_setStrength ( col , UCOL_PRIMARY ) ;
log_verbose ( " Use primary comparison level testing .... \n " ) ;
doAssert ( ( ucol_equal ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
doAssert ( ( ! ucol_greater ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
doAssert ( ( ucol_greaterOrEqual ( col , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
log_verbose ( " The compare tests end. \n " ) ;
ucol_close ( col ) ;
free ( test1 ) ;
free ( test2 ) ;
2000-12-13 01:26:07 +00:00
}
/*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
tests decomposition setting
*/
void TestDecomposition ( ) {
UErrorCode status = U_ZERO_ERROR ;
UCollator * en_US , * el_GR , * vi_VN ;
en_US = ucol_open ( " en_US " , & status ) ;
el_GR = ucol_open ( " el_GR " , & status ) ;
vi_VN = ucol_open ( " vi_VN " , & status ) ;
2001-02-22 00:37:21 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " ERROR: collation creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
2002-08-21 19:09:33 +00:00
if ( ucol_getAttribute ( vi_VN , UCOL_NORMALIZATION_MODE , & status ) ! = UCOL_ON | |
U_FAILURE ( status ) )
{
log_err ( " ERROR: vi_VN collation did not have cannonical decomposition for normalization! \n " ) ;
}
status = U_ZERO_ERROR ;
if ( ucol_getAttribute ( el_GR , UCOL_NORMALIZATION_MODE , & status ) ! = UCOL_ON | |
U_FAILURE ( status ) )
{
log_err ( " ERROR: el_GR collation did not have cannonical decomposition for normalization! \n " ) ;
}
status = U_ZERO_ERROR ;
if ( ucol_getAttribute ( en_US , UCOL_NORMALIZATION_MODE , & status ) ! = UCOL_OFF | |
U_FAILURE ( status ) )
{
log_err ( " ERROR: en_US collation had cannonical decomposition for normalization! \n " ) ;
}
# ifdef ICU_NORMALIZER_USE_DEPRECATES
2000-12-13 01:26:07 +00:00
/* there is no reason to have canonical decomposition in en_US OR default locale */
2001-09-22 01:24:03 +00:00
if ( ucol_getNormalization ( vi_VN ) ! = UNORM_NFD )
2000-12-13 01:26:07 +00:00
{
log_err ( " ERROR: vi_VN collation did not have cannonical decomposition for normalization! \n " ) ;
}
2001-09-22 01:24:03 +00:00
if ( ucol_getNormalization ( el_GR ) ! = UNORM_NFD )
2000-12-13 01:26:07 +00:00
{
log_err ( " ERROR: el_GR collation did not have cannonical decomposition for normalization! \n " ) ;
}
if ( ucol_getNormalization ( en_US ) ! = UNORM_NONE )
{
log_err ( " ERROR: en_US collation had cannonical decomposition for normalization! \n " ) ;
}
2002-08-21 19:09:33 +00:00
# endif
2000-12-13 01:26:07 +00:00
ucol_close ( en_US ) ;
ucol_close ( el_GR ) ;
ucol_close ( vi_VN ) ;
1999-08-16 21:50:52 +00:00
}
2001-02-23 18:26:51 +00:00
# define CLONETEST_COLLATOR_COUNT 3
void TestSafeClone ( ) {
UChar * test1 ;
UChar * test2 ;
UCollator * someCollators [ CLONETEST_COLLATOR_COUNT ] ;
2001-05-31 23:30:09 +00:00
UCollator * someClonedCollators [ CLONETEST_COLLATOR_COUNT ] ;
UCollator * col ;
2001-02-23 18:26:51 +00:00
UErrorCode err = U_ZERO_ERROR ;
2001-09-26 21:09:18 +00:00
int8_t testSize = 6 ; /* Leave this here to test buffer alingment in memory*/
2001-05-31 23:30:09 +00:00
uint8_t buffer [ CLONETEST_COLLATOR_COUNT ] [ U_COL_SAFECLONE_BUFFERSIZE ] ;
int32_t bufferSize = U_COL_SAFECLONE_BUFFERSIZE ;
2001-02-23 18:26:51 +00:00
int index ;
2002-05-23 00:11:42 +00:00
if ( U_COL_SAFECLONE_BUFFERSIZE < sizeof ( UCollator ) ) {
log_err ( " U_COL_SAFECLONE_BUFFERSIZE should be larger than sizeof(UCollator) \n " ) ;
return ;
}
2001-09-26 21:09:18 +00:00
test1 = ( UChar * ) malloc ( sizeof ( UChar ) * testSize ) ;
test2 = ( UChar * ) malloc ( sizeof ( UChar ) * testSize ) ;
2001-02-23 18:26:51 +00:00
u_uastrcpy ( test1 , " abCda " ) ;
u_uastrcpy ( test2 , " abcda " ) ;
/* one default collator & two complex ones */
2001-05-31 23:30:09 +00:00
someCollators [ 0 ] = ucol_open ( " en_US " , & err ) ;
someCollators [ 1 ] = ucol_open ( " ko " , & err ) ;
someCollators [ 2 ] = ucol_open ( " ja_JP " , & err ) ;
/* Check the various error & informational states: */
/* Null status - just returns NULL */
if ( 0 ! = ucol_safeClone ( someCollators [ 0 ] , buffer [ 0 ] , & bufferSize , 0 ) )
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with null status \n " ) ;
}
/* error status - should return 0 & keep error the same */
err = U_MEMORY_ALLOCATION_ERROR ;
if ( 0 ! = ucol_safeClone ( someCollators [ 0 ] , buffer [ 0 ] , & bufferSize , & err ) | | err ! = U_MEMORY_ALLOCATION_ERROR )
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with incoming error status \n " ) ;
}
err = U_ZERO_ERROR ;
/* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
if ( 0 ! = ucol_safeClone ( someCollators [ 0 ] , buffer [ 0 ] , 0 , & err ) | | err ! = U_ILLEGAL_ARGUMENT_ERROR )
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer \n " ) ;
}
err = U_ZERO_ERROR ;
/* buffer size pointer is 0 - fill in pbufferSize with a size */
bufferSize = 0 ;
if ( 0 ! = ucol_safeClone ( someCollators [ 0 ] , buffer [ 0 ] , & bufferSize , & err ) | | U_FAILURE ( err ) | | bufferSize < = 0 )
{
log_err ( " FAIL: Cloned Collator failed a sizing request ('preflighting') \n " ) ;
}
/* Verify our define is large enough */
if ( U_COL_SAFECLONE_BUFFERSIZE < bufferSize )
{
log_err ( " FAIL: Pre-calculated buffer size is too small \n " ) ;
}
/* Verify we can use this run-time calculated size */
if ( 0 = = ( col = ucol_safeClone ( someCollators [ 0 ] , buffer [ 0 ] , & bufferSize , & err ) ) | | U_FAILURE ( err ) )
{
log_err ( " FAIL: Collator can't be cloned with run-time size \n " ) ;
}
if ( col ) ucol_close ( col ) ;
/* size one byte too small - should allocate & let us know */
- - bufferSize ;
2002-08-21 19:09:33 +00:00
if ( 0 = = ( col = ucol_safeClone ( someCollators [ 0 ] , 0 , & bufferSize , & err ) ) | | err ! = U_SAFECLONE_ALLOCATED_WARNING )
2001-05-31 23:30:09 +00:00
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with too-small buffer size \n " ) ;
}
if ( col ) ucol_close ( col ) ;
err = U_ZERO_ERROR ;
bufferSize = U_COL_SAFECLONE_BUFFERSIZE ;
/* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
2002-08-21 19:09:33 +00:00
if ( 0 = = ( col = ucol_safeClone ( someCollators [ 0 ] , 0 , & bufferSize , & err ) ) | | err ! = U_SAFECLONE_ALLOCATED_WARNING )
2001-05-31 23:30:09 +00:00
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with null buffer pointer \n " ) ;
}
if ( col ) ucol_close ( col ) ;
err = U_ZERO_ERROR ;
/* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
if ( 0 ! = ucol_safeClone ( 0 , buffer [ 0 ] , & bufferSize , & err ) | | err ! = U_ILLEGAL_ARGUMENT_ERROR )
{
log_err ( " FAIL: Cloned Collator failed to deal correctly with null Collator pointer \n " ) ;
}
err = U_ZERO_ERROR ;
/* change orig & clone & make sure they are independent */
for ( index = 0 ; index < CLONETEST_COLLATOR_COUNT ; index + + )
{
bufferSize = U_COL_SAFECLONE_BUFFERSIZE ;
someClonedCollators [ index ] = ucol_safeClone ( someCollators [ index ] , buffer [ index ] , & bufferSize , & err ) ;
2001-02-23 18:26:51 +00:00
ucol_setStrength ( someClonedCollators [ index ] , UCOL_TERTIARY ) ;
ucol_setStrength ( someCollators [ index ] , UCOL_PRIMARY ) ;
2001-03-15 19:12:26 +00:00
ucol_setAttribute ( someClonedCollators [ index ] , UCOL_CASE_LEVEL , UCOL_OFF , & err ) ;
ucol_setAttribute ( someCollators [ index ] , UCOL_CASE_LEVEL , UCOL_OFF , & err ) ;
2001-02-23 18:26:51 +00:00
doAssert ( ( ucol_greater ( someClonedCollators [ index ] , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" abCda \" >>> \" abcda \" " ) ;
doAssert ( ( ucol_equal ( someCollators [ index ] , test1 , u_strlen ( test1 ) , test2 , u_strlen ( test2 ) ) ) , " Result should be \" abcda \" == \" abCda \" " ) ;
ucol_close ( someClonedCollators [ index ] ) ;
2001-05-31 23:30:09 +00:00
ucol_close ( someCollators [ index ] ) ;
}
2001-11-02 19:55:04 +00:00
free ( test1 ) ;
free ( test2 ) ;
2001-02-23 18:26:51 +00:00
}
1999-08-16 21:50:52 +00:00
/*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ctor - - Tests the getSortKey
*/
void TestSortKey ( )
{
1999-11-23 22:46:47 +00:00
uint8_t * sortk1 = NULL , * sortk2 = NULL , * sortk3 = NULL ;
uint8_t sortk2_compat [ ] = {
2002-06-13 18:35:27 +00:00
/* 2.2 key */
2002-07-02 22:36:04 +00:00
0x1D , 0x1F , 0x21 , 0x23 , 0x1D , 0x01 ,
0x09 , 0x01 , 0x09 , 0x01 , 0x1C , 0x01 ,
2002-06-13 18:35:27 +00:00
0x92 , 0x93 , 0x94 , 0x95 , 0x92 , 0x00
/* 2.0 key */
/*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x09, 0x01, 0x09, 0x01, 0x18, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
2001-04-19 20:29:34 +00:00
/* 1.8.1 key.*/
2001-09-21 21:22:44 +00:00
/*0x19, 0x1B, 0x1D, 0x1F, 0x19, 0x01, 0x0A, 0x01, 0x0A, 0x01, 0x92, 0x93, 0x94, 0x95, 0x92, 0x00*/
2001-03-13 07:30:28 +00:00
/*this is a 1.8 sortkey */
2001-04-19 20:29:34 +00:00
/*0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00*/
2001-03-13 07:30:28 +00:00
/*this is a 1.7 sortkey */
/*0x02, 0x54, 0x02, 0x55, 0x02, 0x56, 0x02, 0x57, 0x02, 0x54, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00*/
2000-12-06 00:54:21 +00:00
/* this is a 1.6 sortkey */
/*0x00, 0x53, 0x00, 0x54, 0x00, 0x55, 0x00, 0x56, 0x00, 0x53, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00*/
1999-11-23 22:46:47 +00:00
} ;
2001-11-15 17:46:51 +00:00
int32_t sortklen , osortklen ;
uint32_t toStringLen = 0 ;
1999-08-16 21:50:52 +00:00
UCollator * col ;
UChar * test1 , * test2 , * test3 ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
2001-11-14 19:32:46 +00:00
char toStringBuffer [ 256 ] , * resultP ;
2000-12-15 19:27:36 +00:00
2000-12-20 23:15:59 +00:00
uint8_t s1 [ ] = { 0x9f , 0x00 } ;
uint8_t s2 [ ] = { 0x61 , 0x00 } ;
2000-12-15 19:27:36 +00:00
int strcmpResult ;
2000-12-20 23:15:59 +00:00
strcmpResult = strcmp ( ( const char * ) s1 , ( const char * ) s2 ) ;
2000-12-15 19:27:36 +00:00
log_verbose ( " strcmp(0x9f..., 0x61...) = %d \n " , strcmpResult ) ;
if ( strcmpResult < = 0 ) {
log_err ( " ERR: expected strcmp( \" 9f 00 \" , \" 61 00 \" ) to be >=0 (GREATER).. got %d. Calling strcmp() for sortkeys may not work! \n " ,
strcmpResult ) ;
}
1999-08-16 21:50:52 +00:00
log_verbose ( " testing SortKey begins... \n " ) ;
2000-06-10 05:10:28 +00:00
/* this is supposed to open default date format, but later on it treats it like it is "en_US"
- very bad if you try to run the tests on machine where default locale is NOT " en_US " */
/* col = ucol_open(NULL, &status); */
col = ucol_open ( " en_US " , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collation creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
1999-11-23 22:46:47 +00:00
if ( ucol_getStrength ( col ) ! = UCOL_DEFAULT_STRENGTH )
2001-11-15 17:46:51 +00:00
{
2000-11-21 04:05:39 +00:00
log_err ( " ERROR: default collation did not have UCOL_DEFAULT_STRENGTH ! \n " ) ;
2001-11-15 17:46:51 +00:00
}
2001-04-19 20:29:34 +00:00
/* Need to use identical strength */
ucol_setAttribute ( col , UCOL_STRENGTH , UCOL_IDENTICAL , & status ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
test1 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
test2 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
test3 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
1999-11-23 22:46:47 +00:00
memset ( test1 , 0xFE , sizeof ( UChar ) * 6 ) ;
memset ( test2 , 0xFE , sizeof ( UChar ) * 6 ) ;
memset ( test3 , 0xFE , sizeof ( UChar ) * 6 ) ;
1999-08-16 21:50:52 +00:00
u_uastrcpy ( test1 , " Abcda " ) ;
u_uastrcpy ( test2 , " abcda " ) ;
u_uastrcpy ( test3 , " abcda " ) ;
log_verbose ( " Use tertiary comparison level testing .... \n " ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
sortklen = ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , NULL , 0 ) ;
sortk1 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortklen + 1 ) ) ;
1999-11-23 22:46:47 +00:00
memset ( sortk1 , 0xFE , sortklen ) ;
1999-08-16 21:50:52 +00:00
ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , sortk1 , sortklen + 1 ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
sortklen = ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , NULL , 0 ) ;
sortk2 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortklen + 1 ) ) ;
1999-11-23 22:46:47 +00:00
memset ( sortk2 , 0xFE , sortklen ) ;
1999-08-16 21:50:52 +00:00
ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , sortk2 , sortklen + 1 ) ;
1999-11-23 22:46:47 +00:00
osortklen = sortklen ;
1999-08-16 21:50:52 +00:00
sortklen = ucol_getSortKey ( col , test2 , u_strlen ( test3 ) , NULL , 0 ) ;
sortk3 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortklen + 1 ) ) ;
1999-11-23 22:46:47 +00:00
memset ( sortk3 , 0xFE , sortklen ) ;
1999-08-16 21:50:52 +00:00
ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , sortk3 , sortklen + 1 ) ;
1999-11-23 22:46:47 +00:00
doAssert ( ( sortklen = = osortklen ) , " Sortkey length should be the same (abcda, abcda) " ) ;
1999-08-16 21:50:52 +00:00
doAssert ( ( memcmp ( sortk1 , sortk2 , sortklen ) > 0 ) , " Result should be \" Abcda \" > \" abcda \" " ) ;
doAssert ( ( memcmp ( sortk2 , sortk1 , sortklen ) < 0 ) , " Result should be \" abcda \" < \" Abcda \" " ) ;
doAssert ( ( memcmp ( sortk2 , sortk3 , sortklen ) = = 0 ) , " Result should be \" abcda \" == \" abcda \" " ) ;
1999-11-23 22:46:47 +00:00
doAssert ( ( memcmp ( sortk2 , sortk2_compat , sortklen ) = = 0 ) , " Binary format for 'abcda' sortkey different! " ) ;
2001-11-14 19:32:46 +00:00
resultP = ucol_sortKeyToString ( col , sortk2_compat , toStringBuffer , & toStringLen ) ;
doAssert ( ( resultP ! = 0 ) , " sortKeyToString failed! " ) ;
1999-11-23 22:46:47 +00:00
# if 1 /* verobse log of sortkeys */
{
char junk2 [ 1000 ] ;
char junk3 [ 1000 ] ;
int i ;
strcpy ( junk2 , " abcda[2] " ) ;
strcpy ( junk3 , " abcda[3] " ) ;
for ( i = 0 ; i < sortklen ; i + + )
2000-11-21 04:05:39 +00:00
{
sprintf ( junk2 + strlen ( junk2 ) , " %02X " , ( int ) ( 0xFF & sortk2 [ i ] ) ) ;
sprintf ( junk3 + strlen ( junk3 ) , " %02X " , ( int ) ( 0xFF & sortk3 [ i ] ) ) ;
}
1999-11-23 22:46:47 +00:00
log_verbose ( " %s \n " , junk2 ) ;
log_verbose ( " %s \n " , junk3 ) ;
}
# endif
1999-08-16 21:50:52 +00:00
free ( sortk1 ) ;
free ( sortk2 ) ;
free ( sortk3 ) ;
log_verbose ( " Use secondary comparision level testing ... \n " ) ;
ucol_setStrength ( col , UCOL_SECONDARY ) ;
sortklen = ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , NULL , 0 ) ;
sortk1 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortklen + 1 ) ) ;
ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , sortk1 , sortklen + 1 ) ;
sortklen = ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , NULL , 0 ) ;
sortk2 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortklen + 1 ) ) ;
ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , sortk2 , sortklen + 1 ) ;
doAssert ( ! ( memcmp ( sortk1 , sortk2 , sortklen ) > 0 ) , " Result should be \" Abcda \" == \" abcda \" " ) ;
doAssert ( ! ( memcmp ( sortk2 , sortk1 , sortklen ) < 0 ) , " Result should be \" abcda \" == \" Abcda \" " ) ;
doAssert ( ( memcmp ( sortk1 , sortk2 , sortklen ) = = 0 ) , " Result should be \" abcda \" == \" abcda \" " ) ;
1999-11-23 22:46:47 +00:00
1999-08-16 21:50:52 +00:00
log_verbose ( " testing sortkey ends... \n " ) ;
ucol_close ( col ) ;
free ( test1 ) ;
free ( test2 ) ;
free ( test3 ) ;
free ( sortk1 ) ;
free ( sortk2 ) ;
}
void TestHashCode ( )
{
uint8_t * sortk1 , * sortk2 , * sortk3 ;
int32_t sortk1len , sortk2len , sortk3len ;
UCollator * col ;
UChar * test1 , * test2 , * test3 ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
log_verbose ( " testing getHashCode begins... \n " ) ;
col = ucol_open ( " en_US " , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collation creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
test1 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
test2 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
test3 = ( UChar * ) malloc ( sizeof ( UChar ) * 6 ) ;
u_uastrcpy ( test1 , " Abcda " ) ;
u_uastrcpy ( test2 , " abcda " ) ;
u_uastrcpy ( test3 , " abcda " ) ;
log_verbose ( " Use tertiary comparison level testing .... \n " ) ;
sortk1len = ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , NULL , 0 ) ;
sortk1 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortk1len + 1 ) ) ;
ucol_getSortKey ( col , test1 , u_strlen ( test1 ) , sortk1 , sortk1len + 1 ) ;
sortk2len = ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , NULL , 0 ) ;
sortk2 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortk2len + 1 ) ) ;
ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , sortk2 , sortk2len + 1 ) ;
sortk3len = ucol_getSortKey ( col , test2 , u_strlen ( test3 ) , NULL , 0 ) ;
sortk3 = ( uint8_t * ) malloc ( sizeof ( uint8_t ) * ( sortk3len + 1 ) ) ;
ucol_getSortKey ( col , test2 , u_strlen ( test2 ) , sortk3 , sortk3len + 1 ) ;
log_verbose ( " ucol_hashCode() testing ... \n " ) ;
doAssert ( ucol_keyHashCode ( sortk1 , sortk1len ) ! = ucol_keyHashCode ( sortk2 , sortk2len ) , " Hash test1 result incorrect " ) ;
doAssert ( ! ( ucol_keyHashCode ( sortk1 , sortk1len ) = = ucol_keyHashCode ( sortk2 , sortk2len ) ) , " Hash test2 result incorrect " ) ;
doAssert ( ucol_keyHashCode ( sortk2 , sortk2len ) = = ucol_keyHashCode ( sortk3 , sortk3len ) , " Hash result not equal " ) ;
log_verbose ( " hashCode tests end. \n " ) ;
ucol_close ( col ) ;
free ( sortk1 ) ;
free ( sortk2 ) ;
free ( sortk3 ) ;
2000-07-10 22:03:16 +00:00
free ( test1 ) ;
free ( test2 ) ;
free ( test3 ) ;
1999-08-16 21:50:52 +00:00
}
/*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Tests the UCollatorElements API .
*
*/
void TestElemIter ( )
{
2002-03-12 01:32:42 +00:00
int32_t offset ;
1999-08-16 21:50:52 +00:00
int32_t order1 , order2 , order3 ;
UChar * testString1 , * testString2 ;
UCollator * col ;
UCollationElements * iterator1 , * iterator2 , * iterator3 ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
log_verbose ( " testing UCollatorElements begins... \n " ) ;
2001-05-15 21:51:05 +00:00
col = ucol_open ( " en_US " , & status ) ;
2002-08-21 19:09:33 +00:00
ucol_setAttribute ( col , UCOL_NORMALIZATION_MODE , UCOL_OFF , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collation creation failed.: %s \n " , myErrorName ( status ) ) ;
return ;
}
testString1 = ( UChar * ) malloc ( sizeof ( UChar ) * 150 ) ;
testString2 = ( UChar * ) malloc ( sizeof ( UChar ) * 150 ) ;
u_uastrcpy ( testString1 , " XFILE What subset of all possible test cases has the highest probability of detecting the most errors? " ) ;
2001-03-15 02:35:49 +00:00
u_uastrcpy ( testString2 , " Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors? " ) ;
1999-08-16 21:50:52 +00:00
log_verbose ( " Constructors and comparison testing.... \n " ) ;
iterator1 = ucol_openElements ( col , testString1 , u_strlen ( testString1 ) , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collationElement iterator creation failed.: %s \n " , myErrorName ( status ) ) ;
ucol_close ( col ) ;
return ;
}
else { log_verbose ( " PASS: Default collationElement iterator1 creation passed \n " ) ; }
iterator2 = ucol_openElements ( col , testString1 , u_strlen ( testString1 ) , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collationElement iterator creation failed.: %s \n " , myErrorName ( status ) ) ;
ucol_close ( col ) ;
return ;
}
else { log_verbose ( " PASS: Default collationElement iterator2 creation passed \n " ) ; }
iterator3 = ucol_openElements ( col , testString2 , u_strlen ( testString2 ) , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " ERROR: Default collationElement iterator creation failed.: %s \n " , myErrorName ( status ) ) ;
ucol_close ( col ) ;
return ;
}
else { log_verbose ( " PASS: Default collationElement iterator3 creation passed \n " ) ; }
offset = ucol_getOffset ( iterator1 ) ;
ucol_setOffset ( iterator1 , 6 , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Error in setOffset for UCollatorElements iterator.: %s \n " , myErrorName ( status ) ) ;
return ;
}
if ( ucol_getOffset ( iterator1 ) = = 6 )
log_verbose ( " setOffset and getOffset working fine \n " ) ;
else {
log_err ( " error in set and get Offset got %d instead of 6 \n " , ucol_getOffset ( iterator1 ) ) ;
}
ucol_setOffset ( iterator1 , 0 , & status ) ;
order1 = ucol_next ( iterator1 , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Somehow ran out of memory stepping through the iterator1.: %s \n " , myErrorName ( status ) ) ;
return ;
}
order2 = ucol_getOffset ( iterator2 ) ;
doAssert ( ( order1 ! = order2 ) , " The first iterator advance failed " ) ;
order2 = ucol_next ( iterator2 , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Somehow ran out of memory stepping through the iterator2.: %s \n " , myErrorName ( status ) ) ;
return ;
}
order3 = ucol_next ( iterator3 , & status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) ) {
1999-08-16 21:50:52 +00:00
log_err ( " Somehow ran out of memory stepping through the iterator3.: %s \n " , myErrorName ( status ) ) ;
return ;
}
doAssert ( ( order1 = = order2 ) , " The second iterator advance failed should be the same as first one " ) ;
doAssert ( ( ( order1 & UCOL_PRIMARYMASK ) = = ( order3 & UCOL_PRIMARYMASK ) ) , " The primary orders should be identical " ) ;
doAssert ( ( ( order1 & UCOL_SECONDARYMASK ) = = ( order3 & UCOL_SECONDARYMASK ) ) , " The secondary orders should be identical " ) ;
doAssert ( ( ( order1 & UCOL_TERTIARYMASK ) = = ( order3 & UCOL_TERTIARYMASK ) ) , " The tertiary orders should be identical " ) ;
order1 = ucol_next ( iterator1 , & status ) ;
2001-05-15 21:51:05 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " Somehow ran out of memory stepping through the iterator2.: %s \n " , myErrorName ( status ) ) ;
return ;
}
1999-08-16 21:50:52 +00:00
order3 = ucol_next ( iterator3 , & status ) ;
2001-05-15 21:51:05 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " Somehow ran out of memory stepping through the iterator2.: %s \n " , myErrorName ( status ) ) ;
return ;
}
1999-08-16 21:50:52 +00:00
doAssert ( ( ( order1 & UCOL_PRIMARYMASK ) = = ( order3 & UCOL_PRIMARYMASK ) ) , " The primary orders should be identical " ) ;
doAssert ( ( ( order1 & UCOL_TERTIARYMASK ) ! = ( order3 & UCOL_TERTIARYMASK ) ) , " The tertiary orders should be different " ) ;
order1 = ucol_next ( iterator1 , & status ) ;
2001-05-15 21:51:05 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " Somehow ran out of memory stepping through the iterator2.: %s \n " , myErrorName ( status ) ) ;
return ;
}
1999-08-16 21:50:52 +00:00
order3 = ucol_next ( iterator3 , & status ) ;
2001-05-15 21:51:05 +00:00
if ( U_FAILURE ( status ) ) {
log_err ( " Somehow ran out of memory stepping through the iterator2.: %s \n " , myErrorName ( status ) ) ;
return ;
}
2002-06-13 18:35:27 +00:00
/* this here, my friends, is either pure lunacy or something so obsolete that even it's mother
* doesn ' t care about it . Essentialy , this test complains if secondary values for ' I ' and ' _ '
* are the same . According to the UCA , this is not true . Therefore , remove the test .
* Besides , if primary strengths for two code points are different , it doesn ' t matter one bit
* what is the relation between secondary or any other strengths .
* killed by weiv 06 / 11 / 2002.
*/
/*
1999-08-16 21:50:52 +00:00
doAssert ( ( ( order1 & UCOL_SECONDARYMASK ) ! = ( order3 & UCOL_SECONDARYMASK ) ) , " The secondary orders should be different " ) ;
2002-06-13 18:35:27 +00:00
*/
1999-08-16 21:50:52 +00:00
doAssert ( ( order1 ! = UCOL_NULLORDER ) , " Unexpected end of iterator reached " ) ;
free ( testString1 ) ;
free ( testString2 ) ;
ucol_closeElements ( iterator1 ) ;
ucol_closeElements ( iterator2 ) ;
ucol_closeElements ( iterator3 ) ;
ucol_close ( col ) ;
log_verbose ( " testing CollationElementIterator ends... \n " ) ;
}
2002-02-28 07:21:21 +00:00
void TestGetLocale ( ) {
UErrorCode status = U_ZERO_ERROR ;
const char * rules = " &a<x<y<z " ;
UChar rlz [ 256 ] = { 0 } ;
uint32_t rlzLen = u_unescape ( rules , rlz , 256 ) ;
2002-03-13 05:54:56 +00:00
UCollator * coll = NULL ;
const char * locale = NULL ;
int32_t i = 0 ;
2002-04-02 03:15:12 +00:00
static const struct {
const char * requestedLocale ;
const char * validLocale ;
const char * actualLocale ;
2002-03-13 05:54:56 +00:00
} testStruct [ ] = {
2002-11-23 00:52:08 +00:00
{ " sr_YU " , " sr_YU " , " ru " } ,
2002-03-13 05:54:56 +00:00
{ " sh_YU " , " sh_YU " , " sh " } ,
{ " en_US_CALIFORNIA " , " en_US " , " root " } ,
{ " fr_FR_NONEXISTANT " , " fr_FR " , " fr " }
} ;
2002-02-28 07:21:21 +00:00
2002-03-13 05:54:56 +00:00
/* test opening collators for different locales */
for ( i = 0 ; i < sizeof ( testStruct ) / sizeof ( testStruct [ 0 ] ) ; i + + ) {
status = U_ZERO_ERROR ;
coll = ucol_open ( testStruct [ i ] . requestedLocale , & status ) ;
if ( U_FAILURE ( status ) ) {
log_err ( " Failed to open collator for %s with %s \n " , testStruct [ i ] . requestedLocale , u_errorName ( status ) ) ;
ucol_close ( coll ) ;
continue ;
}
locale = ucol_getLocale ( coll , ULOC_REQUESTED_LOCALE , & status ) ;
if ( strcmp ( locale , testStruct [ i ] . requestedLocale ) ! = 0 ) {
log_err ( " [Coll %s]: Error in requested locale, expected %s, got %s \n " , testStruct [ i ] . requestedLocale , testStruct [ i ] . requestedLocale , locale ) ;
}
locale = ucol_getLocale ( coll , ULOC_VALID_LOCALE , & status ) ;
if ( strcmp ( locale , testStruct [ i ] . validLocale ) ! = 0 ) {
log_err ( " [Coll %s]: Error in valid locale, expected %s, got %s \n " , testStruct [ i ] . requestedLocale , testStruct [ i ] . validLocale , locale ) ;
}
locale = ucol_getLocale ( coll , ULOC_ACTUAL_LOCALE , & status ) ;
if ( strcmp ( locale , testStruct [ i ] . actualLocale ) ! = 0 ) {
log_err ( " [Coll %s]: Error in actual locale, expected %s, got %s \n " , testStruct [ i ] . requestedLocale , testStruct [ i ] . actualLocale , locale ) ;
}
ucol_close ( coll ) ;
}
2002-02-28 07:21:21 +00:00
2002-03-13 05:54:56 +00:00
/* completely non-existant locale for collator should get a default collator */
{
UCollator * defaultColl = ucol_open ( NULL , & status ) ;
coll = ucol_open ( " blahaha " , & status ) ;
if ( strcmp ( ucol_getLocale ( coll , ULOC_REQUESTED_LOCALE , & status ) , " blahaha " ) ) {
log_err ( " Nonexisting locale didn't preserve the requested locale \n " ) ;
}
if ( strcmp ( ucol_getLocale ( coll , ULOC_VALID_LOCALE , & status ) ,
ucol_getLocale ( defaultColl , ULOC_VALID_LOCALE , & status ) ) ) {
log_err ( " Valid locale for nonexisting locale locale collator differs "
" from valid locale for default collator \n " ) ;
}
if ( strcmp ( ucol_getLocale ( coll , ULOC_ACTUAL_LOCALE , & status ) ,
ucol_getLocale ( defaultColl , ULOC_ACTUAL_LOCALE , & status ) ) ) {
log_err ( " Actual locale for nonexisting locale locale collator differs "
" from actual locale for default collator \n " ) ;
}
ucol_close ( coll ) ;
ucol_close ( defaultColl ) ;
}
2002-02-28 07:21:21 +00:00
2002-03-13 05:54:56 +00:00
2002-02-28 07:21:21 +00:00
2002-03-13 05:54:56 +00:00
/* collator instantiated from rules should have all three locales NULL */
2002-02-28 07:21:21 +00:00
coll = ucol_openRules ( rlz , rlzLen , UCOL_DEFAULT , UCOL_DEFAULT , NULL , & status ) ;
2002-03-13 05:54:56 +00:00
locale = ucol_getLocale ( coll , ULOC_REQUESTED_LOCALE , & status ) ;
if ( locale ! = NULL ) {
log_err ( " For collator instantiated from rules, requested locale returned %s instead of NULL \n " , locale ) ;
}
locale = ucol_getLocale ( coll , ULOC_VALID_LOCALE , & status ) ;
if ( locale ! = NULL ) {
log_err ( " For collator instantiated from rules, valid locale returned %s instead of NULL \n " , locale ) ;
}
locale = ucol_getLocale ( coll , ULOC_ACTUAL_LOCALE , & status ) ;
if ( locale ! = NULL ) {
log_err ( " For collator instantiated from rules, actual locale returned %s instead of NULL \n " , locale ) ;
}
2002-02-28 07:21:21 +00:00
ucol_close ( coll ) ;
2002-03-13 05:54:56 +00:00
2002-02-28 07:21:21 +00:00
}
1999-08-16 21:50:52 +00:00
void TestGetAll ( )
{
int32_t i , count ;
count = ucol_countAvailable ( ) ;
/* use something sensible w/o hardcoding the count */
if ( count < 0 ) {
log_err ( " Error in countAvailable(), it returned %d \n " , count ) ;
}
else {
2000-08-11 04:29:07 +00:00
log_verbose ( " PASS: countAvailable() successful, it returned %d \n " , count ) ;
1999-08-16 21:50:52 +00:00
}
for ( i = 0 ; i < count ; i + + )
log_verbose ( " %s \n " , ucol_getAvailable ( i ) ) ;
}
2002-01-21 23:54:58 +00:00
2002-01-30 06:14:50 +00:00
struct teststruct {
const char * original ;
uint8_t key [ 256 ] ;
} ;
2002-02-08 04:53:41 +00:00
static int compare_teststruct ( const void * string1 , const void * string2 ) {
2002-02-20 21:59:36 +00:00
return ( strcmp ( ( const char * ) ( ( struct teststruct * ) string1 ) - > key , ( const char * ) ( ( struct teststruct * ) string2 ) - > key ) ) ;
2002-01-30 06:14:50 +00:00
}
2002-01-21 23:54:58 +00:00
void TestBounds ( ) {
UErrorCode status = U_ZERO_ERROR ;
2002-01-30 06:14:50 +00:00
UCollator * coll = ucol_open ( " sh " , & status ) ;
2002-01-21 23:54:58 +00:00
uint8_t sortkey [ 512 ] , lower [ 512 ] , upper [ 512 ] ;
UChar buffer [ 512 ] ;
const char * test [ ] = {
" John Smith " ,
" JOHN SMITH " ,
" john SMITH " ,
" j \\ u00F6hn sm \\ u00EFth " ,
" J \\ u00F6hn Sm \\ u00EFth " ,
2002-01-30 06:14:50 +00:00
" J \\ u00D6HN SM \\ u00CFTH " ,
" john smithsonian " ,
" John Smithsonian " ,
2002-01-21 23:54:58 +00:00
} ;
2002-01-30 06:14:50 +00:00
static struct teststruct tests [ ] = {
{ " \\ u010CAKI MIHALJ " } ,
{ " \\ u010CAKI MIHALJ " } ,
{ " \\ u010CAKI PIRO \\ u0160KA " } ,
{ " \\ u010CABAI ANDRIJA " } ,
{ " \\ u010CABAI LAJO \\ u0160 " } ,
{ " \\ u010CABAI MARIJA " } ,
{ " \\ u010CABAI STEVAN " } ,
{ " \\ u010CABAI STEVAN " } ,
{ " \\ u010CABARKAPA BRANKO " } ,
{ " \\ u010CABARKAPA MILENKO " } ,
{ " \\ u010CABARKAPA MIROSLAV " } ,
{ " \\ u010CABARKAPA SIMO " } ,
{ " \\ u010CABARKAPA STANKO " } ,
{ " \\ u010CABARKAPA TAMARA " } ,
{ " \\ u010CABARKAPA TOMA \\ u0160 " } ,
{ " \\ u010CABDARI \\ u0106 NIKOLA " } ,
{ " \\ u010CABDARI \\ u0106 ZORICA " } ,
{ " \\ u010CABI NANDOR " } ,
{ " \\ u010CABOVI \\ u0106 MILAN " } ,
{ " \\ u010CABRADI AGNEZIJA " } ,
{ " \\ u010CABRADI IVAN " } ,
{ " \\ u010CABRADI JELENA " } ,
{ " \\ u010CABRADI LJUBICA " } ,
{ " \\ u010CABRADI STEVAN " } ,
{ " \\ u010CABRDA MARTIN " } ,
{ " \\ u010CABRILO BOGDAN " } ,
{ " \\ u010CABRILO BRANISLAV " } ,
{ " \\ u010CABRILO LAZAR " } ,
{ " \\ u010CABRILO LJUBICA " } ,
{ " \\ u010CABRILO SPASOJA " } ,
{ " \\ u010CADE \\ u0160 ZDENKA " } ,
{ " \\ u010CADESKI BLAGOJE " } ,
{ " \\ u010CADOVSKI VLADIMIR " } ,
{ " \\ u010CAGLJEVI \\ u0106 TOMA " } ,
{ " \\ u010CAGOROVI \\ u0106 VLADIMIR " } ,
{ " \\ u010CAJA VANKA " } ,
{ " \\ u010CAJI \\ u0106 BOGOLJUB " } ,
{ " \\ u010CAJI \\ u0106 BORISLAV " } ,
{ " \\ u010CAJI \\ u0106 RADOSLAV " } ,
{ " \\ u010CAK \\ u0160IRAN MILADIN " } ,
{ " \\ u010CAKAN EUGEN " } ,
{ " \\ u010CAKAN EVGENIJE " } ,
{ " \\ u010CAKAN IVAN " } ,
{ " \\ u010CAKAN JULIJAN " } ,
{ " \\ u010CAKAN MIHAJLO " } ,
{ " \\ u010CAKAN STEVAN " } ,
{ " \\ u010CAKAN VLADIMIR " } ,
{ " \\ u010CAKAN VLADIMIR " } ,
{ " \\ u010CAKAN VLADIMIR " } ,
{ " \\ u010CAKARA ANA " } ,
{ " \\ u010CAKAREVI \\ u0106 MOMIR " } ,
{ " \\ u010CAKAREVI \\ u0106 NEDELJKO " } ,
{ " \\ u010CAKI \\ u0160ANDOR " } ,
{ " \\ u010CAKI AMALIJA " } ,
{ " \\ u010CAKI ANDRA \\ u0160 " } ,
{ " \\ u010CAKI LADISLAV " } ,
{ " \\ u010CAKI LAJO \\ u0160 " } ,
{ " \\ u010CAKI LASLO " } ,
} ;
int32_t i = 0 , j = 0 , k = 0 , buffSize = 0 , skSize = 0 , lowerSize = 0 , upperSize = 0 ;
int32_t arraySize = sizeof ( tests ) / sizeof ( tests [ 0 ] ) ;
for ( i = 0 ; i < arraySize ; i + + ) {
buffSize = u_unescape ( tests [ i ] . original , buffer , 512 ) ;
skSize = ucol_getSortKey ( coll , buffer , buffSize , tests [ i ] . key , 512 ) ;
}
qsort ( tests , arraySize , sizeof ( struct teststruct ) , compare_teststruct ) ;
for ( i = 0 ; i < arraySize - 1 ; i + + ) {
for ( j = i + 1 ; j < arraySize ; j + + ) {
lowerSize = ucol_getBound ( tests [ i ] . key , - 1 , UCOL_BOUND_LOWER , 1 , lower , 512 , & status ) ;
upperSize = ucol_getBound ( tests [ j ] . key , - 1 , UCOL_BOUND_UPPER , 1 , upper , 512 , & status ) ;
for ( k = i ; k < = j ; k + + ) {
2002-02-20 21:59:36 +00:00
if ( strcmp ( ( const char * ) lower , ( const char * ) tests [ k ] . key ) > 0 ) {
2002-01-30 06:14:50 +00:00
log_err ( " Problem with lower! j = %i (%s vs %s) \n " , k , tests [ k ] . original , tests [ i ] . original ) ;
}
2002-02-20 21:59:36 +00:00
if ( strcmp ( ( const char * ) upper , ( const char * ) tests [ k ] . key ) < = 0 ) {
2002-01-30 06:14:50 +00:00
log_err ( " Problem with upper! j = %i (%s vs %s) \n " , k , tests [ k ] . original , tests [ j ] . original ) ;
}
}
}
}
#if 0
for ( i = 0 ; i < 1000 ; i + + ) {
lowerRND = ( rand ( ) / ( RAND_MAX / arraySize ) ) ;
upperRND = lowerRND + ( rand ( ) / ( RAND_MAX / ( arraySize - lowerRND ) ) ) ;
lowerSize = ucol_getBound ( tests [ lowerRND ] . key , - 1 , UCOL_BOUND_LOWER , 1 , lower , 512 , & status ) ;
upperSize = ucol_getBound ( tests [ upperRND ] . key , - 1 , UCOL_BOUND_UPPER_LONG , 1 , upper , 512 , & status ) ;
for ( j = lowerRND ; j < = upperRND ; j + + ) {
if ( strcmp ( lower , tests [ j ] . key ) > 0 ) {
log_err ( " Problem with lower! j = %i (%s vs %s) \n " , j , tests [ j ] . original , tests [ lowerRND ] . original ) ;
}
if ( strcmp ( upper , tests [ j ] . key ) < = 0 ) {
log_err ( " Problem with upper! j = %i (%s vs %s) \n " , j , tests [ j ] . original , tests [ upperRND ] . original ) ;
}
}
}
# endif
2002-01-21 23:54:58 +00:00
for ( i = 0 ; i < sizeof ( test ) / sizeof ( test [ 0 ] ) ; i + + ) {
buffSize = u_unescape ( test [ i ] , buffer , 512 ) ;
skSize = ucol_getSortKey ( coll , buffer , buffSize , sortkey , 512 ) ;
2002-01-30 06:14:50 +00:00
lowerSize = ucol_getBound ( sortkey , skSize , UCOL_BOUND_LOWER , 1 , lower , 512 , & status ) ;
upperSize = ucol_getBound ( sortkey , skSize , UCOL_BOUND_UPPER_LONG , 1 , upper , 512 , & status ) ;
2002-01-21 23:54:58 +00:00
for ( j = i + 1 ; j < sizeof ( test ) / sizeof ( test [ 0 ] ) ; j + + ) {
buffSize = u_unescape ( test [ j ] , buffer , 512 ) ;
skSize = ucol_getSortKey ( coll , buffer , buffSize , sortkey , 512 ) ;
2002-02-20 21:59:36 +00:00
if ( strcmp ( ( const char * ) lower , ( const char * ) sortkey ) > 0 ) {
2002-01-30 06:14:50 +00:00
log_err ( " Problem with lower! i = %i, j = %i (%s vs %s) \n " , i , j , test [ i ] , test [ j ] ) ;
}
2002-02-20 21:59:36 +00:00
if ( strcmp ( ( const char * ) upper , ( const char * ) sortkey ) < = 0 ) {
2002-01-30 06:14:50 +00:00
log_err ( " Problem with upper! i = %i, j = %i (%s vs %s) \n " , i , j , test [ i ] , test [ j ] ) ;
2002-01-21 23:54:58 +00:00
}
}
}
ucol_close ( coll ) ;
2002-02-08 04:53:41 +00:00
}
2002-08-22 20:48:21 +00:00
static void doOverrunTest ( UCollator * coll , const UChar * uString , int32_t strLen ) {
2002-08-02 06:55:51 +00:00
int32_t skLen = 0 , skLen2 = 0 ;
uint8_t sortKey [ 256 ] ;
int32_t i , j ;
uint8_t filler = 0xFF ;
skLen = ucol_getSortKey ( coll , uString , strLen , NULL , 0 ) ;
for ( i = 0 ; i < skLen ; i + + ) {
uprv_memset ( sortKey , filler , 256 ) ;
skLen2 = ucol_getSortKey ( coll , uString , strLen , sortKey , i ) ;
if ( skLen ! = skLen2 ) {
log_err ( " For buffer size %i, got different sortkey length. Expected %i got %i \n " , i , skLen , skLen2 ) ;
}
for ( j = i ; j < 256 ; j + + ) {
if ( sortKey [ j ] ! = filler ) {
log_err ( " Something run over index %i \n " , j ) ;
break ;
}
}
}
}
/* j1865 reports that if a shorter buffer is passed to
* to get sort key , a buffer overrun happens in some
* cases . This test tries to check this .
*/
void TestSortKeyBufferOverrun ( void ) {
UErrorCode status = U_ZERO_ERROR ;
const char * cString = " A very Merry liTTle-lamB.. " ;
UChar uString [ 256 ] ;
int32_t strLen = 0 ;
UCollator * coll = ucol_open ( " root " , & status ) ;
2002-08-02 18:22:44 +00:00
strLen = u_unescape ( cString , uString , 256 ) ;
2002-08-02 06:55:51 +00:00
if ( U_SUCCESS ( status ) ) {
log_verbose ( " testing non ignorable \n " ) ;
ucol_setAttribute ( coll , UCOL_ALTERNATE_HANDLING , UCOL_NON_IGNORABLE , & status ) ;
doOverrunTest ( coll , uString , strLen ) ;
log_verbose ( " testing shifted \n " ) ;
ucol_setAttribute ( coll , UCOL_ALTERNATE_HANDLING , UCOL_SHIFTED , & status ) ;
doOverrunTest ( coll , uString , strLen ) ;
log_verbose ( " testing shifted quaternary \n " ) ;
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_QUATERNARY , & status ) ;
doOverrunTest ( coll , uString , strLen ) ;
log_verbose ( " testing with french secondaries \n " ) ;
ucol_setAttribute ( coll , UCOL_FRENCH_COLLATION , UCOL_ON , & status ) ;
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_TERTIARY , & status ) ;
ucol_setAttribute ( coll , UCOL_ALTERNATE_HANDLING , UCOL_NON_IGNORABLE , & status ) ;
doOverrunTest ( coll , uString , strLen ) ;
}
2002-08-03 05:53:19 +00:00
ucol_close ( coll ) ;
2002-08-02 06:55:51 +00:00
}
2002-08-21 19:09:33 +00:00
static void TestAttribute ( )
{
UErrorCode error = U_ZERO_ERROR ;
UCollator * coll = ucol_open ( NULL , & error ) ;
if ( U_FAILURE ( error ) ) {
log_err ( " Creation of default collator failed " ) ;
return ;
}
ucol_setAttribute ( coll , UCOL_FRENCH_COLLATION , UCOL_OFF , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_FRENCH_COLLATION , & error ) ! = UCOL_OFF | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the french collation failed " ) ;
}
ucol_setAttribute ( coll , UCOL_FRENCH_COLLATION , UCOL_ON , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_FRENCH_COLLATION , & error ) ! = UCOL_ON | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the french collation failed " ) ;
}
ucol_setAttribute ( coll , UCOL_ALTERNATE_HANDLING , UCOL_SHIFTED , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_ALTERNATE_HANDLING , & error ) ! = UCOL_SHIFTED | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the alternate handling failed " ) ;
}
ucol_setAttribute ( coll , UCOL_ALTERNATE_HANDLING , UCOL_NON_IGNORABLE , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_ALTERNATE_HANDLING , & error ) ! = UCOL_NON_IGNORABLE | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the alternate handling failed " ) ;
}
ucol_setAttribute ( coll , UCOL_CASE_FIRST , UCOL_LOWER_FIRST , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_CASE_FIRST , & error ) ! = UCOL_LOWER_FIRST | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the case first attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_CASE_FIRST , UCOL_UPPER_FIRST , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_CASE_FIRST , & error ) ! = UCOL_UPPER_FIRST | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the case first attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_CASE_LEVEL , UCOL_ON , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_CASE_LEVEL , & error ) ! = UCOL_ON | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the case level attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_CASE_LEVEL , UCOL_OFF , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_CASE_LEVEL , & error ) ! = UCOL_OFF | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the case level attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_NORMALIZATION_MODE , UCOL_ON , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_NORMALIZATION_MODE , & error ) ! = UCOL_ON | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the normalization on/off attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_NORMALIZATION_MODE , UCOL_OFF , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_NORMALIZATION_MODE , & error ) ! = UCOL_OFF | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the normalization on/off attribute failed " ) ;
}
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_PRIMARY , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_STRENGTH , & error ) ! = UCOL_PRIMARY | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the collation strength failed " ) ;
}
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_SECONDARY , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_STRENGTH , & error ) ! = UCOL_SECONDARY | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the collation strength failed " ) ;
}
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_TERTIARY , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_STRENGTH , & error ) ! = UCOL_TERTIARY | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the collation strength failed " ) ;
}
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_QUATERNARY , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_STRENGTH , & error ) ! = UCOL_QUATERNARY | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the collation strength failed " ) ;
}
ucol_setAttribute ( coll , UCOL_STRENGTH , UCOL_IDENTICAL , & error ) ;
if ( ucol_getAttribute ( coll , UCOL_STRENGTH , & error ) ! = UCOL_IDENTICAL | |
U_FAILURE ( error ) ) {
log_err ( " Setting and retrieving of the collation strength failed " ) ;
}
ucol_close ( coll ) ;
}
2002-09-17 06:27:51 +00:00
void TestGetTailoredSet ( ) {
struct {
2002-12-06 23:53:00 +00:00
const char * rules ;
const char * tests [ 20 ] ;
2002-09-17 06:27:51 +00:00
int32_t testsize ;
} setTest [ ] = {
{ " &a < \\ u212b " , { " \\ u212b " , " A \\ u030a " , " \\ u00c5 " } , 3 } ,
{ " & S < \\ u0161 <<< \\ u0160 " , { " \\ u0161 " , " s \\ u030C " , " \\ u0160 " , " S \\ u030C " } , 4 }
} ;
int32_t i = 0 , j = 0 ;
UErrorCode status = U_ZERO_ERROR ;
UParseError pError ;
UCollator * coll = NULL ;
UChar buff [ 1024 ] ;
int32_t buffLen = 0 ;
USet * set = NULL ;
for ( i = 0 ; i < sizeof ( setTest ) / sizeof ( setTest [ 0 ] ) ; i + + ) {
buffLen = u_unescape ( setTest [ i ] . rules , buff , 1024 ) ;
coll = ucol_openRules ( buff , buffLen , UCOL_DEFAULT , UCOL_DEFAULT , & pError , & status ) ;
if ( U_SUCCESS ( status ) ) {
set = ucol_getTailoredSet ( coll , & status ) ;
if ( uset_size ( set ) ! = setTest [ i ] . testsize ) {
log_err ( " Tailored set size different (%d) than expected (%d) \n " , uset_size ( set ) , setTest [ i ] . testsize ) ;
}
for ( j = 0 ; j < setTest [ i ] . testsize ; j + + ) {
buffLen = u_unescape ( setTest [ i ] . tests [ j ] , buff , 1024 ) ;
if ( ! uset_containsString ( set , buff , buffLen ) ) {
log_err ( " Tailored set doesn't contain %s... It should \n " , setTest [ i ] . tests [ j ] ) ;
}
}
uset_close ( set ) ;
} else {
log_err ( " Couldn't open collator with rules %s \n " , setTest [ i ] . rules ) ;
}
ucol_close ( coll ) ;
}
2002-09-17 19:45:50 +00:00
}
2002-09-20 17:54:45 +00:00
# endif /* #if !UCONFIG_NO_COLLATION */