X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..340931cb2e044a2141d11567dd0f782524e32994:/icuSources/test/cintltst/cmsccoll.c diff --git a/icuSources/test/cintltst/cmsccoll.c b/icuSources/test/cintltst/cmsccoll.c index 28d3fb62..e5d3d0d8 100644 --- a/icuSources/test/cintltst/cmsccoll.c +++ b/icuSources/test/cintltst/cmsccoll.c @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /******************************************************************** * COPYRIGHT: - * Copyright (c) 2001-2004, International Business Machines Corporation and + * Copyright (c) 2001-2016, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************* @@ -28,19 +30,21 @@ #include "unicode/ustring.h" #include "string.h" #include "ucol_imp.h" -#include "ucol_tok.h" #include "cmemory.h" #include "cstring.h" #include "uassert.h" #include "unicode/parseerr.h" #include "unicode/ucnv.h" +#include "unicode/ures.h" +#include "unicode/uscript.h" +#include "unicode/utf16.h" #include "uparse.h" +#include "putilimp.h" -#define LEN(a) (sizeof(a)/sizeof(a[0])) #define MAX_TOKEN_LEN 16 -typedef int tst_strcoll(void *collator, const int object, +typedef UCollationResult tst_strcoll(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen); @@ -89,7 +93,7 @@ static void IncompleteCntTest(void) coll = ucol_openRules(temp, u_strlen(temp), UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL,&status); if(U_SUCCESS(status)) { - size = sizeof(cnt1)/sizeof(cnt1[0]); + size = UPRV_LENGTHOF(cnt1); for(i = 0; i < size-1; i++) { for(j = i+1; j < size; j++) { UCollationElements *iter; @@ -115,7 +119,7 @@ static void IncompleteCntTest(void) coll = ucol_openRules(temp, u_strlen(temp), UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status); if(U_SUCCESS(status)) { - size = sizeof(cnt2)/sizeof(cnt2[0]); + size = UPRV_LENGTHOF(cnt2); for(i = 0; i < size-1; i++) { for(j = i+1; j < size; j++) { UCollationElements *iter; @@ -153,7 +157,7 @@ const static char shifted[][20] = { }; const static UCollationResult shiftedTert[] = { - 0, + UCOL_EQUAL, UCOL_EQUAL, UCOL_EQUAL, UCOL_LESS, @@ -189,7 +193,7 @@ static void BlackBirdTest(void) { ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status); if(U_SUCCESS(status)) { - size = sizeof(nonignorable)/sizeof(nonignorable[0]); + size = UPRV_LENGTHOF(nonignorable); for(i = 0; i < size-1; i++) { for(j = i+1; j < size; j++) { u_uastrcpy(t1, nonignorable[i]); @@ -203,7 +207,7 @@ static void BlackBirdTest(void) { ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status); if(U_SUCCESS(status)) { - size = sizeof(shifted)/sizeof(shifted[0]); + size = UPRV_LENGTHOF(shifted); for(i = 0; i < size-1; i++) { for(j = i+1; j < size; j++) { u_uastrcpy(t1, shifted[i]); @@ -215,7 +219,7 @@ static void BlackBirdTest(void) { ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status); if(U_SUCCESS(status)) { - size = sizeof(shifted)/sizeof(shifted[0]); + size = UPRV_LENGTHOF(shifted); for(i = 1; i < size; i++) { u_uastrcpy(t1, shifted[i-1]); u_uastrcpy(t2, shifted[i]); @@ -260,7 +264,7 @@ static void FunkyATest(void) UCollator *myCollation; myCollation = ucol_open("en_US", &status); if(U_FAILURE(status)){ - log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status)); + log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status)); return; } log_verbose("Testing some A letters, for some reason\n"); @@ -349,19 +353,19 @@ static void PrintMarkDavis(void) } fprintf(stderr, "\n"); - for(h = 0; h0x0400 && *p<0x0500)?0x00e1:0x491; - UChar preQ = (*p>0x0400 && *p<0x0500)?0x0041:0x413; -*/ - /*log_verbose("Testing primary\n");*/ +const static char chTest[][20] = { + "c", + "C", + "ca", "cb", "cx", "cy", "CZ", + "c\\u030C", "C\\u030C", + "h", + "H", + "ha", "Ha", "harly", "hb", "HB", "hx", "HX", "hy", "HY", + "ch", "cH", "Ch", "CH", + "cha", "charly", "che", "chh", "chch", "chr", + "i", "I", "iarly", + "r", "R", + "r\\u030C", "R\\u030C", + "s", + "S", + "s\\u030C", "S\\u030C", + "z", "Z", + "z\\u030C", "Z\\u030C" +}; - doTest(col, p, q, UCOL_LESS); -/* - UCollationResult result = ucol_strcoll(col,p,u_strlen(p),q,u_strlen(q)); +static void TestChMove(void) { + UChar t1[256] = {0}; + UChar t2[256] = {0}; + + uint32_t i = 0, j = 0; + uint32_t size = 0; + UErrorCode status = U_ZERO_ERROR; + + UCollator *coll = ucol_open("cs", &status); - if(result!=UCOL_LESS){ - aescstrdup(p,utfSource,256); - aescstrdup(q,utfTarget,256); - fprintf(file,"Primary failed source: %s target: %s \n", utfSource,utfTarget); + if(U_SUCCESS(status)) { + size = UPRV_LENGTHOF(chTest); + for(i = 0; i < size-1; i++) { + for(j = i+1; j < size; j++) { + u_unescape(chTest[i], t1, 256); + u_unescape(chTest[j], t2, 256); + doTest(coll, t1, t2, UCOL_LESS); + } + } } -*/ - source[0] = preP; - u_strcpy(source+1,p); - target[0] = preQ; - u_strcpy(target+1,q); - doTest(col, source, target, UCOL_LESS); -/* - fprintf(file,"Primary swamps 2nd failed source: %s target: %s \n", utfSource,utfTarget); -*/ + else { + log_data_err("Can't open collator"); + } + ucol_close(coll); } -static void testSecondary(UCollator* col, const UChar* p,const UChar* q){ - UChar source[256] = { '\0'}; - UChar target[256] = { '\0'}; - /*log_verbose("Testing secondary\n");*/ - doTest(col, p, q, UCOL_LESS); -/* - fprintf(file,"secondary failed source: %s target: %s \n", utfSource,utfTarget); -*/ - source[0] = 0x0053; - u_strcpy(source+1,p); - target[0]= 0x0073; - u_strcpy(target+1,q); - doTest(col, source, target, UCOL_LESS); /* - fprintf(file,"secondary swamps 3rd failed source: %s target: %s \n",utfSource,utfTarget); +const static char impTest[][20] = { + "\\u4e00", + "a", + "A", + "b", + "B", + "\\u4e01" +}; */ - u_strcpy(source,p); - source[u_strlen(p)] = 0x62; - source[u_strlen(p)+1] = 0; - +static void TestImplicitTailoring(void) { + static const struct { + const char *rules; + const char *data[10]; + const uint32_t len; + } tests[] = { + { + /* Tailor b and c before U+4E00. */ + "&[before 1]\\u4e00 < b < c " + /* Now, before U+4E00 is c; put d and e after that. */ + "&[before 1]\\u4e00 < d < e", + { "b", "c", "d", "e", "\\u4e00"}, 5 }, + { "&\\u4e00 < a <<< A < b <<< B", { "\\u4e00", "a", "A", "b", "B", "\\u4e01"}, 6 }, + { "&[before 1]\\u4e00 < \\u4e01 < \\u4e02", { "\\u4e01", "\\u4e02", "\\u4e00"}, 3}, + { "&[before 1]\\u4e01 < \\u4e02 < \\u4e03", { "\\u4e02", "\\u4e03", "\\u4e01"}, 3} + }; - u_strcpy(target,q); - target[u_strlen(q)] = 0x61; - target[u_strlen(q)+1] = 0; + int32_t i = 0; - doTest(col, source, target, UCOL_GREATER); + for(i = 0; i < UPRV_LENGTHOF(tests); i++) { + genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len); + } /* - fprintf(file,"secondary is swamped by 1 failed source: %s target: %s \n",utfSource,utfTarget); -*/ -} + UChar t1[256] = {0}; + UChar t2[256] = {0}; -static void testTertiary(UCollator* col, const UChar* p,const UChar* q){ - UChar source[256] = { '\0'}; - UChar target[256] = { '\0'}; + const char *rule = "&\\u4e00 < a <<< A < b <<< B"; - /*log_verbose("Testing tertiary\n");*/ + uint32_t i = 0, j = 0; + uint32_t size = 0; + uint32_t ruleLen = 0; + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = NULL; + ruleLen = u_unescape(rule, t1, 256); - doTest(col, p, q, UCOL_LESS); -/* - fprintf(file,"Tertiary failed source: %s target: %s \n",utfSource,utfTarget); -*/ - source[0] = 0x0020; - u_strcpy(source+1,p); - target[0]= 0x002D; - u_strcpy(target+1,q); + coll = ucol_openRules(t1, ruleLen, UCOL_OFF, UCOL_TERTIARY,NULL, &status); - doTest(col, source, target, UCOL_LESS); -/* - fprintf(file,"Tertiary swamps 4th failed source: %s target: %s \n", utfSource,utfTarget); -*/ + if(U_SUCCESS(status)) { + size = UPRV_LENGTHOF(impTest); + for(i = 0; i < size-1; i++) { + for(j = i+1; j < size; j++) { + u_unescape(impTest[i], t1, 256); + u_unescape(impTest[j], t2, 256); + doTest(coll, t1, t2, UCOL_LESS); + } + } + } + else { + log_err("Can't open collator"); + } + ucol_close(coll); + */ +} + +static void TestFCDProblem(void) { + UChar t1[256] = {0}; + UChar t2[256] = {0}; + + const char *s1 = "\\u0430\\u0306\\u0325"; + const char *s2 = "\\u04D1\\u0325"; - u_strcpy(source,p); - source[u_strlen(p)] = 0xE0; - source[u_strlen(p)+1] = 0; + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = ucol_open("", &status); + u_unescape(s1, t1, 256); + u_unescape(s2, t2, 256); - u_strcpy(target,q); - target[u_strlen(q)] = 0x61; - target[u_strlen(q)+1] = 0; + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status); + doTest(coll, t1, t2, UCOL_EQUAL); - doTest(col, source, target, UCOL_GREATER); + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); + doTest(coll, t1, t2, UCOL_EQUAL); -/* - fprintf(file,"Tertiary is swamped by 3rd failed source: %s target: %s \n",utfSource,utfTarget); -*/ + ucol_close(coll); } -static void testEquality(UCollator* col, const UChar* p,const UChar* q){ /* - UChar source[256] = { '\0'}; - UChar target[256] = { '\0'}; +The largest normalization form is 18 for NFKC/NFKD, 4 for NFD and 3 for NFC +We're only using NFC/NFD in this test. */ +#define NORM_BUFFER_TEST_LEN 18 +typedef struct { + UChar32 u; + UChar NFC[NORM_BUFFER_TEST_LEN]; + UChar NFD[NORM_BUFFER_TEST_LEN]; +} tester; - doTest(col, p, q, UCOL_EQUAL); -/* - fprintf(file,"Primary failed source: %s target: %s \n", utfSource,utfTarget); -*/ -} +static void TestComposeDecompose(void) { + /* [[:NFD_Inert=false:][:NFC_Inert=false:]] */ + static const UChar UNICODESET_STR[] = { + 0x5B,0x5B,0x3A,0x4E,0x46,0x44,0x5F,0x49,0x6E,0x65,0x72,0x74,0x3D,0x66,0x61, + 0x6C,0x73,0x65,0x3A,0x5D,0x5B,0x3A,0x4E,0x46,0x43,0x5F,0x49,0x6E,0x65,0x72, + 0x74,0x3D,0x66,0x61,0x6C,0x73,0x65,0x3A,0x5D,0x5D,0 + }; + int32_t noOfLoc; + int32_t i = 0, j = 0; -static void testCollator(UCollator *coll, UErrorCode *status) { - const UChar *rules = NULL, *current = NULL; - int32_t ruleLen = 0; - uint32_t strength = 0; - uint32_t chOffset = 0; uint32_t chLen = 0; - uint32_t exOffset = 0; uint32_t exLen = 0; - uint32_t prefixOffset = 0; uint32_t prefixLen = 0; - uint32_t firstEx = 0; -/* uint32_t rExpsLen = 0; */ - uint32_t firstLen = 0; - UBool varT = FALSE; UBool top_ = TRUE; - uint16_t specs = 0; - UBool startOfRules = TRUE; - UBool lastReset = FALSE; - UBool before = FALSE; - uint32_t beforeStrength = 0; - UColTokenParser src; - UColOptionSet opts; - - UChar first[256]; - UChar second[256]; - UChar tempB[256]; - uint32_t tempLen; - UChar *rulesCopy = NULL; - UParseError parseError; - src.opts = &opts; - - rules = ucol_getRules(coll, &ruleLen); - if(U_SUCCESS(*status) && ruleLen > 0) { - rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); - uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar)); - src.current = src.source = rulesCopy; - src.end = rulesCopy+ruleLen; - src.extraCurrent = src.end; - src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE; - *first = *second = 0; - - while ((current = ucol_tok_parseNextToken(&src, startOfRules,&parseError, status)) != NULL) { - strength = src.parsedToken.strength; - chOffset = src.parsedToken.charsOffset; - chLen = src.parsedToken.charsLen; - exOffset = src.parsedToken.extensionOffset; - exLen = src.parsedToken.extensionLen; - prefixOffset = src.parsedToken.prefixOffset; - prefixLen = src.parsedToken.prefixLen; - specs = src.parsedToken.flags; - - startOfRules = FALSE; - varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0); - top_ = (UBool)((specs & UCOL_TOK_TOP) != 0); - if(top_) { /* if reset is on top, the sequence is broken. We should have an empty string */ - second[0] = 0; - } else { - u_strncpy(second,rulesCopy+chOffset, chLen); - second[chLen] = 0; - - if(exLen > 0 && firstEx == 0) { - u_strncat(first, rulesCopy+exOffset, exLen); - first[firstLen+exLen] = 0; - } + UErrorCode status = U_ZERO_ERROR; + const char *locName = NULL; + uint32_t nfcSize; + uint32_t nfdSize; + tester **t; + uint32_t noCases = 0; + UCollator *coll = NULL; + UChar32 u = 0; + UChar comp[NORM_BUFFER_TEST_LEN]; + uint32_t len = 0; + UCollationElements *iter; + USet *charsToTest = uset_openPattern(UNICODESET_STR, -1, &status); + int32_t charsToTestSize; - if(lastReset == TRUE && prefixLen != 0) { - u_strncpy(first+prefixLen, first, firstLen); - u_strncpy(first, rulesCopy+prefixOffset, prefixLen); - first[firstLen+prefixLen] = 0; - firstLen = firstLen+prefixLen; - } + noOfLoc = uloc_countAvailable(); - if(before == TRUE) { /* swap first and second */ - u_strcpy(tempB, first); - u_strcpy(first, second); - u_strcpy(second, tempB); + coll = ucol_open("", &status); + if (U_FAILURE(status)) { + log_data_err("Error opening collator -> %s (Are you missing data?)\n", u_errorName(status)); + uset_close(charsToTest); + return; + } + charsToTestSize = uset_size(charsToTest); + if (charsToTestSize <= 0) { + log_err("Set was zero. Missing data?\n"); + uset_close(charsToTest); + return; + } + t = (tester **)malloc(charsToTestSize * sizeof(tester *)); + t[0] = (tester *)malloc(sizeof(tester)); + log_verbose("Testing UCA extensively for %d characters\n", charsToTestSize); - tempLen = firstLen; - firstLen = chLen; - chLen = tempLen; + for(u = 0; u < charsToTestSize; u++) { + UChar32 ch = uset_charAt(charsToTest, u); + len = 0; + U16_APPEND_UNSAFE(comp, len, ch); + nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status); + nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status); - tempLen = firstEx; - firstEx = exLen; - exLen = tempLen; - if(beforeStrength < strength) { - strength = beforeStrength; - } + if(nfcSize != nfdSize || (uprv_memcmp(t[noCases]->NFC, t[noCases]->NFD, nfcSize * sizeof(UChar)) != 0) + || (len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0))) { + t[noCases]->u = ch; + if(len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0)) { + u_strncpy(t[noCases]->NFC, comp, len); + t[noCases]->NFC[len] = 0; + } + noCases++; + t[noCases] = (tester *)malloc(sizeof(tester)); + uprv_memset(t[noCases], 0, sizeof(tester)); } - } - lastReset = FALSE; + } + log_verbose("Testing %d/%d of possible test cases\n", noCases, charsToTestSize); + uset_close(charsToTest); + charsToTest = NULL; - switch(strength){ - case UCOL_IDENTICAL: - testEquality(coll,first,second); - break; - case UCOL_PRIMARY: - testPrimary(coll,first,second); - break; - case UCOL_SECONDARY: - testSecondary(coll,first,second); - break; - case UCOL_TERTIARY: - testTertiary(coll,first,second); - break; - case UCOL_TOK_RESET: - lastReset = TRUE; - before = (UBool)((specs & UCOL_TOK_BEFORE) != 0); - if(before) { - beforeStrength = (specs & UCOL_TOK_BEFORE)-1; + for(u=0; u<(UChar32)noCases; u++) { + if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) { + log_err("Failure: codePoint %05X fails TestComposeDecompose in the UCA\n", t[u]->u); + doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL); } - break; - default: - break; - } - - if(before == TRUE && strength != UCOL_TOK_RESET) { /* first and second were swapped */ - before = FALSE; - } else { - firstLen = chLen; - firstEx = exLen; - u_strcpy(first, second); + } + /* + for(u = 0; u < charsToTestSize; u++) { + if(!(u&0xFFFF)) { + log_verbose("%08X ", u); } + uprv_memset(t[noCases], 0, sizeof(tester)); + t[noCases]->u = u; + len = 0; + U16_APPEND_UNSAFE(comp, len, u); + comp[len] = 0; + nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status); + nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status); + doTest(coll, comp, t[noCases]->NFD, UCOL_EQUAL); + doTest(coll, comp, t[noCases]->NFC, UCOL_EQUAL); } - free(rulesCopy); - } -} + */ -static int ucaTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) { - UCollator *UCA = (UCollator *)collator; - return ucol_strcoll(UCA, source, sLen, target, tLen); -} + ucol_close(coll); -/* -static int winTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) { -#ifdef WIN32 - LCID lcid = (LCID)collator; - return CompareString(lcid, 0, source, sLen, target, tLen); -#else - return 0; -#endif -} -*/ + log_verbose("Testing locales, number of cases = %i\n", noCases); + for(i = 0; iNFD, u_strlen(t[u]->NFD), &status); - return func(collator, opts, source, sLen+1, target, tLen+1); + for(u=0; u<(UChar32)noCases; u++) { + if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) { + log_err("Failure: codePoint %05X fails TestComposeDecompose for locale %s\n", t[u]->u, cName); + doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL); + log_verbose("Testing NFC\n"); + ucol_setText(iter, t[u]->NFC, u_strlen(t[u]->NFC), &status); + backAndForth(iter); + log_verbose("Testing NFD\n"); + ucol_setText(iter, t[u]->NFD, u_strlen(t[u]->NFD), &status); + backAndForth(iter); + } + } + ucol_closeElements(iter); + ucol_close(coll); + } + } + for(u = 0; u <= (UChar32)noCases; u++) { + free(t[u]); + } + free(t); } -static UCollationResult swampLater(tst_strcoll* func, void *collator, int opts, - UChar s1, UChar s2, - const UChar *s, const uint32_t sLen, - const UChar *t, const uint32_t tLen) { - UChar source[256] = {0}; - UChar target[256] = {0}; - - u_strcpy(source, s); - source[sLen] = s1; - u_strcpy(target, t); - target[tLen] = s2; +static void TestEmptyRule(void) { + UErrorCode status = U_ZERO_ERROR; + UChar rulez[] = { 0 }; + UCollator *coll = ucol_openRules(rulez, 0, UCOL_OFF, UCOL_TERTIARY,NULL, &status); - return func(collator, opts, source, sLen+1, target, tLen+1); + ucol_close(coll); } -static uint32_t probeStrength(tst_strcoll* func, void *collator, int opts, - const UChar *s, const uint32_t sLen, - const UChar *t, const uint32_t tLen, - UCollationResult result) { - /*UChar fPrimary = 0x6d;*/ - /*UChar sPrimary = 0x6e;*/ - UChar fSecondary = 0x310d; - UChar sSecondary = 0x31a3; - UChar fTertiary = 0x310f; - UChar sTertiary = 0x31b7; - - UCollationResult oposite; - if(result == UCOL_EQUAL) { - return UCOL_IDENTICAL; - } else if(result == UCOL_GREATER) { - oposite = UCOL_LESS; - } else { - oposite = UCOL_GREATER; +static void TestUCARules(void) { + UErrorCode status = U_ZERO_ERROR; + UChar b[256]; + UChar *rules = b; + uint32_t ruleLen = 0; + UCollator *UCAfromRules = NULL; + UCollator *coll = ucol_open("", &status); + if(status == U_FILE_ACCESS_ERROR) { + log_data_err("Is your data around?\n"); + return; + } else if(U_FAILURE(status)) { + log_err("Error opening collator\n"); + return; } + ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, 256); - if(swampEarlier(func, collator, opts, sSecondary, fSecondary, s, sLen, t, tLen) == result) { - return UCOL_PRIMARY; - } else if((swampEarlier(func, collator, opts, sTertiary, 0x310f, s, sLen, t, tLen) == result) && - (swampEarlier(func, collator, opts, 0x310f, sTertiary, s, sLen, t, tLen) == result)) { - return UCOL_SECONDARY; - } else if((swampLater(func, collator, opts, sTertiary, fTertiary, s, sLen, t, tLen) == result) && - (swampLater(func, collator, opts, fTertiary, sTertiary, s, sLen, t, tLen) == result)) { - return UCOL_TERTIARY; - } else if((swampLater(func, collator, opts, sTertiary, 0x310f, s, sLen, t, tLen) == oposite) && - (swampLater(func, collator, opts, fTertiary, sTertiary, s, sLen, t, tLen) == oposite)) { - return UCOL_QUATERNARY; - } else { - return UCOL_IDENTICAL; + log_verbose("TestUCARules\n"); + if(ruleLen > 256) { + rules = (UChar *)malloc((ruleLen+1)*sizeof(UChar)); + ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, ruleLen); } -} - -static char *getRelationSymbol(UCollationResult res, uint32_t strength, char *buffer) { - uint32_t i = 0; - - if(res == UCOL_EQUAL || strength == 0xdeadbeef) { - buffer[0] = '='; - buffer[1] = '='; - buffer[2] = '\0'; - } else if(res == UCOL_GREATER) { - for(i = 0; i maxOutputLength) { - maxOutputLength = outputLength; - U_ASSERT(outputLength < sizeof(output)); - } - - log_verbose("%s\n", output); - +static void TestBefore(void) { + const static char *data[] = { + "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0", "A", + "\\u0113", "\\u00e9", "\\u011b", "\\u00e8", "E", + "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec", "I", + "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2", "O", + "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9", "U", + "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc", "\\u00fc" + }; + genericRulesStarter( + "&[before 1]a<\\u0101<\\u00e1<\\u01ce<\\u00e0" + "&[before 1]e<\\u0113<\\u00e9<\\u011b<\\u00e8" + "&[before 1]i<\\u012b<\\u00ed<\\u01d0<\\u00ec" + "&[before 1]o<\\u014d<\\u00f3<\\u01d2<\\u00f2" + "&[before 1]u<\\u016b<\\u00fa<\\u01d4<\\u00f9" + "&u<\\u01d6<\\u01d8<\\u01da<\\u01dc<\\u00fc", + data, UPRV_LENGTHOF(data)); } -/* -static void printOutRules(const UChar *rules) { - uint32_t len = u_strlen(rules); - uint32_t i = 0; - char toPrint; - uint32_t line = 0; - - fprintf(stdout, "Rules:"); - - for(i = 0; i=0x20) { - toPrint = (char)rules[i]; - if(toPrint == '&') { - line = 1; - fprintf(stdout, "\n&"); - } else if(toPrint == ';') { - fprintf(stdout, "<<"); - line+=2; - } else if(toPrint == ',') { - fprintf(stdout, "<<<"); - line+=3; - } else { - fprintf(stdout, "%c", toPrint); - line++; - } - } else if(rules[i]<0x3400 || rules[i]>=0xa000) { - fprintf(stdout, "\\u%04X", rules[i]); - line+=6; - } - if(line>72) { - fprintf(stdout, "\n"); - line = 0; - } - } +#if 0 +/* superceded by TestBeforePinyin */ +static void TestJ784(void) { + const static char *data[] = { + "A", "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0", + "E", "\\u0113", "\\u00e9", "\\u011b", "\\u00e8", + "I", "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec", + "O", "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2", + "U", "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9", + "\\u00fc", + "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc" + }; + genericLocaleStarter("zh", data, UPRV_LENGTHOF(data)); +} +#endif - log_verbose("\n"); +static void TestUpperCaseFirst(void) { + const static char *data[] = { + "I", + "i", + "Y", + "y" + }; + genericLocaleStarter("da", data, UPRV_LENGTHOF(data)); +} +static void TestJ815(void) { + const static char *data[] = { + "aa", + "Aa", + "ab", + "Ab", + "ad", + "Ad", + "ae", + "Ae", + "\\u00e6", + "\\u00c6", + "af", + "Af", + "b", + "B" + }; + genericLocaleStarter("fr", data, UPRV_LENGTHOF(data)); + genericRulesStarter("[backwards 2]&A<<\\u00e6/e<<<\\u00c6/E", data, UPRV_LENGTHOF(data)); } -*/ -static uint32_t testSwitch(tst_strcoll* func, void *collator, int opts, uint32_t strength, const UChar *first, const UChar *second, const char* msg, UBool error) { - uint32_t diffs = 0; - UCollationResult realResult; - uint32_t realStrength; - uint32_t sLen = u_strlen(first); - uint32_t tLen = u_strlen(second); +static void TestCase(void) +{ + const static UChar gRules[MAX_TOKEN_LEN] = + /*" & 0 < 1,\u2461 0) { - rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); - uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar)); - src.current = src.source = rulesCopy; - src.end = rulesCopy+ruleLen; - src.extraCurrent = src.end; - src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE; - *first = *second = 0; - - while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,status)) != NULL) { - strength = src.parsedToken.strength; - chOffset = src.parsedToken.charsOffset; - chLen = src.parsedToken.charsLen; - exOffset = src.parsedToken.extensionOffset; - exLen = src.parsedToken.extensionLen; - prefixOffset = src.parsedToken.prefixOffset; - prefixLen = src.parsedToken.prefixLen; - specs = src.parsedToken.flags; - - startOfRules = FALSE; - varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0); - top_ = (UBool)((specs & UCOL_TOK_TOP) != 0); - - u_strncpy(second,rulesCopy+chOffset, chLen); - second[chLen] = 0; - secondLen = chLen; - - if(exLen > 0) { - u_strncat(first, rulesCopy+exOffset, exLen); - first[firstLen+exLen] = 0; - firstLen += exLen; - } + if(U_FAILURE(status)){ + log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status)); + return; + } + log_verbose("Testing different case settings\n"); + ucol_setStrength(myCollation, UCOL_TERTIARY); - if(strength != UCOL_TOK_RESET) { - if((*first<0x3400 || *first>=0xa000) && (*second<0x3400 || *second>=0xa000)) { - UCAdiff += testSwitch(&ucaTest, (void *)UCA, 0, strength, first, second, refName, error); - /*Windiff += testSwitch(&winTest, (void *)lcid, 0, strength, first, second, "Win32");*/ + for(k = 0; k<4; k++) { + ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status); + ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status); + log_verbose("Case first = %d, Case level = %d\n", caseTestAttributes[k][0], caseTestAttributes[k][1]); + for (i = 0; i < 3 ; i++) { + for(j = i+1; j<4; j++) { + doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]); } } - - - firstLen = chLen; - u_strcpy(first, second); - - } - if(UCAdiff != 0 && Windiff != 0) { - log_verbose("\n"); } - if(UCAdiff == 0) { - log_verbose("No immediate difference with %s!\n", refName); - } - if(Windiff == 0) { - log_verbose("No immediate difference with Win32!\n"); + ucol_close(myCollation); + + myCollation = ucol_openRules(gRules, u_strlen(gRules), UCOL_OFF, UCOL_TERTIARY,NULL, &status); + if(U_FAILURE(status)){ + log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status)); + return; } - free(rulesCopy); - } -} + log_verbose("Testing different case settings with custom rules\n"); + ucol_setStrength(myCollation, UCOL_TERTIARY); -/* - * Takes two CEs (lead and continuation) and - * compares them as CEs should be compared: - * primary vs. primary, secondary vs. secondary - * tertiary vs. tertiary - */ -static int32_t compareCEs(uint32_t s1, uint32_t s2, - uint32_t t1, uint32_t t2) { - uint32_t s = 0, t = 0; - if(s1 == t1 && s2 == t2) { - return 0; - } - s = (s1 & 0xFFFF0000)|((s2 & 0xFFFF0000)>>16); - t = (t1 & 0xFFFF0000)|((t2 & 0xFFFF0000)>>16); - if(s < t) { - return -1; - } else if(s > t) { - return 1; - } else { - s = (s1 & 0x0000FF00) | (s2 & 0x0000FF00)>>8; - t = (t1 & 0x0000FF00) | (t2 & 0x0000FF00)>>8; - if(s < t) { - return -1; - } else if(s > t) { - return 1; - } else { - s = (s1 & 0x000000FF)<<8 | (s2 & 0x000000FF); - t = (t1 & 0x000000FF)<<8 | (t2 & 0x000000FF); - if(s < t) { - return -1; - } else { - return 1; + for(k = 0; k<4; k++) { + ucol_setAttribute(myCollation, UCOL_CASE_FIRST, caseTestAttributes[k][0], &status); + ucol_setAttribute(myCollation, UCOL_CASE_LEVEL, caseTestAttributes[k][1], &status); + for (i = 0; i < 3 ; i++) { + for(j = i+1; j<4; j++) { + log_verbose("k:%d, i:%d, j:%d\n", k, i, j); + doTest(myCollation, testCase[i], testCase[j], caseTestResults[k][3*i+j-1]); + iter=ucol_openElements(myCollation, testCase[i], u_strlen(testCase[i]), &status); + backAndForth(iter); + ucol_closeElements(iter); + iter=ucol_openElements(myCollation, testCase[j], u_strlen(testCase[j]), &status); + backAndForth(iter); + ucol_closeElements(iter); + } } } - } -} + ucol_close(myCollation); + { + const static char *lowerFirst[] = { + "h", + "H", + "ch", + "Ch", + "CH", + "cha", + "chA", + "Cha", + "ChA", + "CHa", + "CHA", + "i", + "I" + }; + + const static char *upperFirst[] = { + "H", + "h", + "CH", + "Ch", + "ch", + "CHA", + "CHa", + "ChA", + "Cha", + "chA", + "cha", + "I", + "i" + }; + log_verbose("mixed case test\n"); + log_verbose("lower first, case level off\n"); + genericRulesStarter("[caseFirst lower]&Himage + UCA->image->UCAConsts); - uint32_t UCOL_RESET_TOP_VALUE = consts->UCA_LAST_NON_VARIABLE[0], /*UCOL_RESET_TOP_CONT = consts->UCA_LAST_NON_VARIABLE[1], */ - UCOL_NEXT_TOP_VALUE = consts->UCA_FIRST_IMPLICIT[0], UCOL_NEXT_TOP_CONT = consts->UCA_FIRST_IMPLICIT[1]; - - baseCE=baseContCE=nextCE=nextContCE=currCE=currContCE=lastCE=lastContCE = UCOL_NOT_FOUND; - - src.opts = &opts; - - rules = ucol_getRules(coll, &ruleLen); - - src.invUCA = ucol_initInverseUCA(status); - - if(indirectBoundariesSet == FALSE) { - /* UCOL_RESET_TOP_VALUE */ - setIndirectBoundaries(0, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT); - /* UCOL_FIRST_PRIMARY_IGNORABLE */ - setIndirectBoundaries(1, consts->UCA_FIRST_PRIMARY_IGNORABLE, 0); - /* UCOL_LAST_PRIMARY_IGNORABLE */ - setIndirectBoundaries(2, consts->UCA_LAST_PRIMARY_IGNORABLE, 0); - /* UCOL_FIRST_SECONDARY_IGNORABLE */ - setIndirectBoundaries(3, consts->UCA_FIRST_SECONDARY_IGNORABLE, 0); - /* UCOL_LAST_SECONDARY_IGNORABLE */ - setIndirectBoundaries(4, consts->UCA_LAST_SECONDARY_IGNORABLE, 0); - /* UCOL_FIRST_TERTIARY_IGNORABLE */ - setIndirectBoundaries(5, consts->UCA_FIRST_TERTIARY_IGNORABLE, 0); - /* UCOL_LAST_TERTIARY_IGNORABLE */ - setIndirectBoundaries(6, consts->UCA_LAST_TERTIARY_IGNORABLE, 0); - /* UCOL_FIRST_VARIABLE */ - setIndirectBoundaries(7, consts->UCA_FIRST_VARIABLE, 0); - /* UCOL_LAST_VARIABLE */ - setIndirectBoundaries(8, consts->UCA_LAST_VARIABLE, 0); - /* UCOL_FIRST_NON_VARIABLE */ - setIndirectBoundaries(9, consts->UCA_FIRST_NON_VARIABLE, 0); - /* UCOL_LAST_NON_VARIABLE */ - setIndirectBoundaries(10, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT); - /* UCOL_FIRST_IMPLICIT */ - setIndirectBoundaries(11, consts->UCA_FIRST_IMPLICIT, 0); - /* UCOL_LAST_IMPLICIT */ - setIndirectBoundaries(12, consts->UCA_LAST_IMPLICIT, consts->UCA_FIRST_TRAILING); - /* UCOL_FIRST_TRAILING */ - setIndirectBoundaries(13, consts->UCA_FIRST_TRAILING, 0); - /* UCOL_LAST_TRAILING */ - setIndirectBoundaries(14, consts->UCA_LAST_TRAILING, 0); - ucolIndirectBoundaries[14].limitCE = (consts->UCA_PRIMARY_SPECIAL_MIN<<24); - indirectBoundariesSet = TRUE; - } +static void TestIncrementalNormalize(void) { + + /*UChar baseA =0x61;*/ + UChar baseA =0x41; +/* UChar baseB = 0x42;*/ + static const UChar ccMix[] = {0x316, 0x321, 0x300}; + /*UChar ccMix[] = {0x61, 0x61, 0x61};*/ + /* + 0x316 is combining grave accent below, cc=220 + 0x321 is combining palatalized hook below, cc=202 + 0x300 is combining grave accent, cc=230 + */ +#define MAXSLEN 2000 + /*int maxSLen = 64000;*/ + int sLen; + int i; - if(U_SUCCESS(*status) && ruleLen > 0) { - rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); - uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar)); - src.current = src.source = rulesCopy; - src.end = rulesCopy+ruleLen; - src.extraCurrent = src.end; - src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE; - - while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,status)) != NULL) { - strength = src.parsedToken.strength; - chOffset = src.parsedToken.charsOffset; - chLen = src.parsedToken.charsLen; - exOffset = src.parsedToken.extensionOffset; - exLen = src.parsedToken.extensionLen; - prefixOffset = src.parsedToken.prefixOffset; - prefixLen = src.parsedToken.prefixLen; - specs = src.parsedToken.flags; - - startOfRules = FALSE; - varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0); - top_ = (UBool)((specs & UCOL_TOK_TOP) != 0); - - uprv_init_collIterate(coll, rulesCopy+chOffset, chLen, &c); - - currCE = ucol_getNextCE(coll, &c, status); - if(currCE == 0 && UCOL_ISTHAIPREVOWEL(*(rulesCopy+chOffset))) { - log_verbose("Thai prevowel detected. Will pick next CE\n"); - currCE = ucol_getNextCE(coll, &c, status); - } + UCollator *coll; + UErrorCode status = U_ZERO_ERROR; + UCollationResult result; - currContCE = ucol_getNextCE(coll, &c, status); - if(!isContinuation(currContCE)) { - currContCE = 0; - } + int32_t myQ = getTestOption(QUICK_OPTION); - /* we need to repack CEs here */ + if(getTestOption(QUICK_OPTION) < 0) { + setTestOption(QUICK_OPTION, 1); + } - if(strength == UCOL_TOK_RESET) { - before = (UBool)((specs & UCOL_TOK_BEFORE) != 0); - if(top_ == TRUE) { - int32_t index = src.parsedToken.indirectIndex; + { + /* Test 1. Run very long unnormalized strings, to force overflow of*/ + /* most buffers along the way.*/ + UChar strA[MAXSLEN+1]; + UChar strB[MAXSLEN+1]; - nextCE = baseCE = currCE = ucolIndirectBoundaries[index].startCE; - nextContCE = baseContCE = currContCE = ucolIndirectBoundaries[index].startContCE; - } else { - nextCE = baseCE = currCE; - nextContCE = baseContCE = currContCE; - } - maxStrength = UCOL_IDENTICAL; - } else { - if(strength < maxStrength) { - maxStrength = strength; - if(baseCE == UCOL_RESET_TOP_VALUE) { - log_verbose("Resetting to [top]\n"); - nextCE = UCOL_NEXT_TOP_VALUE; - nextContCE = UCOL_NEXT_TOP_CONT; - } else { - result = ucol_inv_getNextCE(&src, baseCE & 0xFFFFFF3F, baseContCE, &nextCE, &nextContCE, maxStrength); - } - if(result < 0) { - if(ucol_isTailored(coll, *(rulesCopy+oldOffset), status)) { - log_verbose("Reset is tailored codepoint %04X, don't know how to continue, taking next test\n", *(rulesCopy+oldOffset)); - return; - } else { - log_err("couldn't find the CE\n"); - return; - } - } + coll = ucol_open("en_US", &status); + if(status == U_FILE_ACCESS_ERROR) { + log_data_err("Is your data around?\n"); + return; + } else if(U_FAILURE(status)) { + log_err("Error opening collator\n"); + return; } + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); - currCE &= 0xFFFFFF3F; - currContCE &= 0xFFFFFFBF; - - if(maxStrength == UCOL_IDENTICAL) { - if(baseCE != currCE || baseContCE != currContCE) { - log_err("current CE (initial strength UCOL_EQUAL)\n"); - } - } else { - if(strength == UCOL_IDENTICAL) { - if(lastCE != currCE || lastContCE != currContCE) { - log_err("current CE (initial strength UCOL_EQUAL)\n"); - } - } else { - if(compareCEs(currCE, currContCE, nextCE, nextContCE) > 0) { - /*if(currCE > nextCE || (currCE == nextCE && currContCE >= nextContCE)) {*/ - log_err("current CE is not less than base CE\n"); - } - if(!before) { - if(compareCEs(currCE, currContCE, lastCE, lastContCE) < 0) { - /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/ - log_err("sequence of generated CEs is broken\n"); - } - } else { - before = FALSE; - if(compareCEs(currCE, currContCE, lastCE, lastContCE) > 0) { - /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/ - log_err("sequence of generated CEs is broken\n"); - } + /*for (sLen = 257; sLenimage->jamoSpecial == TRUE) { - log_err("%s has special JAMOs\n", locName); + { + static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42, 0x00}; + static const UChar strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00, 0x00}; + char sortKeyA[50]; + char sortKeyAz[50]; + char sortKeyB[50]; + char sortKeyBz[50]; + int r; + + result = ucol_strcoll(coll, strA, 6, strB, 6); + if (result != UCOL_GREATER) { + log_err("ERROR 1 in test 5\n"); + } + result = ucol_strcoll(coll, strA, -1, strB, -1); + if (result != UCOL_EQUAL) { + log_err("ERROR 2 in test 5\n"); } - ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status); - testCollator(coll, &status); - testCEs(coll, &status); - ucol_close(coll); - } - } - } - for(i = 0; i %s\n", u_errorName(status)); + return; + } + while (count < 149) { + secstr[count] = 0x0020; /* [06, 05, 05] */ + tertstr[count] = 0x0020; + count ++; } + /* top down compression ----------------------------------- */ + secstr[count] = 0x0332; /* [, 87, 05] */ + tertstr[count] = 0x3000; /* [06, 05, 07] */ - for(u = 0; u < 0x30000; u++) { - len = 0; - UTF_APPEND_CHAR_UNSAFE(comp, len, u); - nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status); - nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status); + /* no compression secstr should have 150 secondary bytes, tertstr should + have 150 tertiary bytes. + with correct compression, secstr should have 6 secondary + bytes (149/33 rounded up + accent), tertstr should have > 2 tertiary bytes */ + resultlen = ucol_getSortKey(coll, secstr, 150, result, UPRV_LENGTHOF(result)); + (void)resultlen; /* Suppress set but not used warning. */ + tempptr = (uint8_t *)uprv_strchr((char *)result, 1) + 1; + while (*(tempptr + 1) != 1) { + /* the last secondary collation element is not checked since it is not + part of the compression */ + if (*tempptr < SEC_COMMON_MIDDLE) { + log_err("Secondary top down compression overlapped\n"); + } + tempptr ++; + } - if(nfcSize != nfdSize || (uprv_memcmp(t[noCases]->NFC, t[noCases]->NFD, nfcSize * sizeof(UChar)) != 0) - || (len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0))) { - t[noCases]->u = u; - if(len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0)) { - u_strncpy(t[noCases]->NFC, comp, len); - t[noCases]->NFC[len] = 0; - } - noCases++; - t[noCases] = (tester *)malloc(sizeof(tester)); - uprv_memset(t[noCases], 0, sizeof(tester)); + /* tertiary top/bottom/common for en_US is similar to the secondary + top/bottom/common */ + resultlen = ucol_getSortKey(coll, tertstr, 150, result, UPRV_LENGTHOF(result)); + tempptr = (uint8_t *)uprv_strrchr((char *)result, 1) + 1; + while (*(tempptr + 1) != 0) { + /* the last secondary collation element is not checked since it is not + part of the compression */ + if (*tempptr < TER_ONLY_COMMON_MIDDLE) { + log_err("Tertiary top down compression overlapped\n"); } + tempptr ++; } - for(u=0; u<(UChar32)noCases; u++) { - if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) { - log_err("Failure: codePoint %05X fails TestComposeDecompose in the UCA\n", t[u]->u); - doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL); - } + /* bottom up compression ------------------------------------- */ + secstr[count] = 0; + tertstr[count] = 0; + resultlen = ucol_getSortKey(coll, secstr, 150, result, UPRV_LENGTHOF(result)); + tempptr = (uint8_t *)uprv_strchr((char *)result, 1) + 1; + while (*(tempptr + 1) != 1) { + /* the last secondary collation element is not checked since it is not + part of the compression */ + if (*tempptr > SEC_COMMON_MIDDLE) { + log_err("Secondary bottom up compression overlapped\n"); + } + tempptr ++; } - /* - for(u = 0; u < 0x30000; u++) { - if(!(u&0xFFFF)) { - log_verbose("%08X ", u); - } - uprv_memset(t[noCases], 0, sizeof(tester)); - t[noCases]->u = u; - len = 0; - UTF_APPEND_CHAR_UNSAFE(comp, len, u); - comp[len] = 0; - nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status); - nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status); - doTest(coll, comp, t[noCases]->NFD, UCOL_EQUAL); - doTest(coll, comp, t[noCases]->NFC, UCOL_EQUAL); + + /* tertiary top/bottom/common for en_US is similar to the secondary + top/bottom/common */ + resultlen = ucol_getSortKey(coll, tertstr, 150, result, UPRV_LENGTHOF(result)); + tempptr = (uint8_t *)uprv_strrchr((char *)result, 1) + 1; + while (*(tempptr + 1) != 0) { + /* the last secondary collation element is not checked since it is not + part of the compression */ + if (*tempptr > TER_ONLY_COMMON_MIDDLE) { + log_err("Tertiary bottom up compression overlapped\n"); + } + tempptr ++; } - */ ucol_close(coll); +} - log_verbose("Testing locales, number of cases = %i\n", noCases); - for(i = 0; iNFD, u_strlen(t[u]->NFD), &status); + // Most of the following are commented out because UCA 8.0 + // drops most of the Cyrillic contractions from the default order. + // See CLDR ticket #7246 "root collation: remove Cyrillic contractions". - for(u=0; u<(UChar32)noCases; u++) { - if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) { - log_err("Failure: codePoint %05X fails TestComposeDecompose for locale %s\n", t[u]->u, cName); - doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL); - log_verbose("Testing NFC\n"); - ucol_setText(iter, t[u]->NFC, u_strlen(t[u]->NFC), &status); - backAndForth(iter); - log_verbose("Testing NFD\n"); - ucol_setText(iter, t[u]->NFD, u_strlen(t[u]->NFD), &status); - backAndForth(iter); - } - } - ucol_closeElements(iter); - ucol_close(coll); - } - } - for(u = 0; u <= (UChar32)noCases; u++) { - free(t[u]); - } - free(t); + // genericLocaleStarter("root", test, 3); + // genericRulesStarter("&\\u0410 = \\u0410", test, 3); + // genericRulesStarter("&Z < \\u0410", test, 3); + genericRulesStarter("&\\u0410 = \\u0410 < \\u04d0", test, 3); + genericRulesStarter("&Z < \\u0410 < \\u04d0", test, 3); + // genericRulesStarter("&\\u0410 = \\u0410 < \\u0410\\u0301", test, 3); + // genericRulesStarter("&Z < \\u0410 < \\u0410\\u0301", test, 3); } -static void TestEmptyRule(void) { - UErrorCode status = U_ZERO_ERROR; - UChar rulez[] = { 0 }; - UCollator *coll = ucol_openRules(rulez, 0, UCOL_OFF, UCOL_TERTIARY,NULL, &status); +static void TestSuppressContractions(void) { - ucol_close(coll); + static const char *testNoCont2[] = { + "\\u0410\\u0302a", + "\\u0410\\u0306b", + "\\u0410c" + }; + static const char *testNoCont[] = { + "a\\u0410", + "A\\u0410\\u0306", + "\\uFF21\\u0410\\u0302" + }; + + genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont, 3); + genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont2, 3); } -static void TestUCARules(void) { - UErrorCode status = U_ZERO_ERROR; - UChar b[256]; - UChar *rules = b; - uint32_t ruleLen = 0; - UCollator *UCAfromRules = NULL; - UCollator *coll = ucol_open("", &status); - if(status == U_FILE_ACCESS_ERROR) { - log_data_err("Is your data around?\n"); - return; - } else if(U_FAILURE(status)) { - log_err("Error opening collator\n"); +static void TestContraction(void) { + const static char *testrules[] = { + "&A = AB / B", + "&A = A\\u0306/\\u0306", + "&c = ch / h" + }; + const static UChar testdata[][2] = { + {0x0041 /* 'A' */, 0x0042 /* 'B' */}, + {0x0041 /* 'A' */, 0x0306 /* combining breve */}, + {0x0063 /* 'c' */, 0x0068 /* 'h' */} + }; + const static UChar testdata2[][2] = { + {0x0063 /* 'c' */, 0x0067 /* 'g' */}, + {0x0063 /* 'c' */, 0x0068 /* 'h' */}, + {0x0063 /* 'c' */, 0x006C /* 'l' */} + }; +#if 0 + /* + * These pairs of rule strings are not guaranteed to yield the very same mappings. + * In fact, LDML 24 recommends an improved way of creating mappings + * which always yields different mappings for such pairs. See + * http://www.unicode.org/reports/tr35/tr35-33/tr35-collation.html#Orderings + */ + const static char *testrules3[] = { + "&z < xyz &xyzw << B", + "&z < xyz &xyz << B / w", + "&z < ch &achm << B", + "&z < ch &a << B / chm", + "&\\ud800\\udc00w << B", + "&\\ud800\\udc00 << B / w", + "&a\\ud800\\udc00m << B", + "&a << B / \\ud800\\udc00m", + }; +#endif + + UErrorCode status = U_ZERO_ERROR; + UCollator *coll; + UChar rule[256] = {0}; + uint32_t rlen = 0; + int i; + + for (i = 0; i < UPRV_LENGTHOF(testrules); i ++) { + UCollationElements *iter1; + int j = 0; + log_verbose("Rule %s for testing\n", testrules[i]); + rlen = u_unescape(testrules[i], rule, 32); + coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); + if (U_FAILURE(status)) { + log_err_status(status, "Collator creation failed %s -> %s\n", testrules[i], u_errorName(status)); + return; + } + iter1 = ucol_openElements(coll, testdata[i], 2, &status); + if (U_FAILURE(status)) { + log_err("Collation iterator creation failed\n"); + return; + } + while (j < 2) { + UCollationElements *iter2 = ucol_openElements(coll, + &(testdata[i][j]), + 1, &status); + int32_t ce; + if (U_FAILURE(status)) { + log_err("Collation iterator creation failed\n"); + return; + } + ce = ucol_next(iter2, &status); + while (ce != UCOL_NULLORDER) { + if (ucol_next(iter1, &status) != ce) { + log_err("Collation elements in contraction split does not match\n"); + return; + } + ce = ucol_next(iter2, &status); + } + j ++; + ucol_closeElements(iter2); + } + if (ucol_next(iter1, &status) != UCOL_NULLORDER) { + log_err("Collation elements not exhausted\n"); + return; + } + ucol_closeElements(iter1); + ucol_close(coll); + } + + rlen = u_unescape("& a < b < c < ch < d & c = ch / h", rule, 256); + coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); + if (ucol_strcoll(coll, testdata2[0], 2, testdata2[1], 2) != UCOL_LESS) { + log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n", + testdata2[0][0], testdata2[0][1], testdata2[1][0], + testdata2[1][1]); + return; + } + if (ucol_strcoll(coll, testdata2[1], 2, testdata2[2], 2) != UCOL_LESS) { + log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n", + testdata2[1][0], testdata2[1][1], testdata2[2][0], + testdata2[2][1]); + return; + } + ucol_close(coll); +#if 0 /* see above */ + for (i = 0; i < UPRV_LENGTHOF(testrules3); i += 2) { + log_verbose("testrules3 i==%d \"%s\" vs. \"%s\"\n", i, testrules3[i], testrules3[i + 1]); + UCollator *coll1, + *coll2; + UCollationElements *iter1, + *iter2; + UChar ch = 0x0042 /* 'B' */; + uint32_t ce; + rlen = u_unescape(testrules3[i], rule, 32); + coll1 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); + rlen = u_unescape(testrules3[i + 1], rule, 32); + coll2 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); + if (U_FAILURE(status)) { + log_err("Collator creation failed %s\n", testrules[i]); + return; + } + iter1 = ucol_openElements(coll1, &ch, 1, &status); + iter2 = ucol_openElements(coll2, &ch, 1, &status); + if (U_FAILURE(status)) { + log_err("Collation iterator creation failed\n"); + return; + } + ce = ucol_next(iter1, &status); + if (U_FAILURE(status)) { + log_err("Retrieving ces failed\n"); + return; + } + while (ce != UCOL_NULLORDER) { + uint32_t ce2 = (uint32_t)ucol_next(iter2, &status); + if (ce == ce2) { + log_verbose("CEs match: %08x\n", ce); + } else { + log_err("CEs do not match: %08x vs. %08x\n", ce, ce2); + return; + } + ce = ucol_next(iter1, &status); + if (U_FAILURE(status)) { + log_err("Retrieving ces failed\n"); + return; + } + } + if (ucol_next(iter2, &status) != UCOL_NULLORDER) { + log_err("CEs not exhausted\n"); + return; + } + ucol_closeElements(iter1); + ucol_closeElements(iter2); + ucol_close(coll1); + ucol_close(coll2); + } +#endif +} + +static void TestExpansion(void) { + const static char *testrules[] = { +#if 0 + /* + * This seems to have tested that M was not mapped to an expansion. + * I believe the old builder just did that because it computed the extension CEs + * at the very end, which was a bug. + * Among other problems, it violated the core tailoring principle + * by making an earlier rule depend on a later one. + * And, of course, if M did not get an expansion, then it was primary different from K, + * unlike what the rule &K< %s\n", testrules[i], u_errorName(status)); + return; + } + + for (j = 0; j < 5; j ++) { + doTest(coll, testdata[j], testdata[j + 1], UCOL_LESS); + } + ucol_close(coll); + } +} + +#if 0 +/* this test tests the current limitations of the engine */ +/* it always fail, so it is disabled by default */ +static void TestLimitations(void) { + /* recursive expansions */ + { + static const char *rule = "&a=b/c&d=c/e"; + static const char *tlimit01[] = {"add","b","adf"}; + static const char *tlimit02[] = {"aa","b","af"}; + log_verbose("recursive expansions\n"); + genericRulesStarter(rule, tlimit01, UPRV_LENGTHOF(tlimit01)); + genericRulesStarter(rule, tlimit02, UPRV_LENGTHOF(tlimit02)); + } + /* contractions spanning expansions */ + { + static const char *rule = "&a<< %08x\n", varTopOriginal); + ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); + + varTop1 = ucol_setVariableTop(coll, &space, 1, &status); + varTop2 = ucol_getVariableTop(coll, &status); + log_verbose("ucol_setVariableTop(space) -> %08x\n", varTop1); + if(U_FAILURE(status) || varTop1 != varTop2 || + !ucol_equal(coll, &nul, 0, &space, 1) || + ucol_equal(coll, &nul, 0, &dot, 1) || + ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &space, 1, &dot, 1)) { + log_err("ucol_setVariableTop(space) did not work - %s\n", u_errorName(status)); + } + + varTop1 = ucol_setVariableTop(coll, &dot, 1, &status); + varTop2 = ucol_getVariableTop(coll, &status); + log_verbose("ucol_setVariableTop(dot) -> %08x\n", varTop1); + if(U_FAILURE(status) || varTop1 != varTop2 || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &dot, 1, °ree, 1)) { + log_err("ucol_setVariableTop(dot) did not work - %s\n", u_errorName(status)); + } + + varTop1 = ucol_setVariableTop(coll, °ree, 1, &status); + varTop2 = ucol_getVariableTop(coll, &status); + log_verbose("ucol_setVariableTop(degree) -> %08x\n", varTop1); + if(U_FAILURE(status) || varTop1 != varTop2 || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + !ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, °ree, 1, &dollar, 1)) { + log_err("ucol_setVariableTop(degree) did not work - %s\n", u_errorName(status)); + } + + varTop1 = ucol_setVariableTop(coll, &dollar, 1, &status); + varTop2 = ucol_getVariableTop(coll, &status); + log_verbose("ucol_setVariableTop(dollar) -> %08x\n", varTop1); + if(U_FAILURE(status) || varTop1 != varTop2 || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + !ucol_equal(coll, &nul, 0, °ree, 1) || + !ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &dollar, 1, &zero, 1)) { + log_err("ucol_setVariableTop(dollar) did not work - %s\n", u_errorName(status)); + } + + log_verbose("Testing setting variable top to contractions\n"); + { + UChar first[4] = { 0 }; + first[0] = 0x0040; + first[1] = 0x0050; + first[2] = 0x0000; + + status = U_ZERO_ERROR; + ucol_setVariableTop(coll, first, -1, &status); + + if(U_SUCCESS(status)) { + log_err("Invalid contraction succeded in setting variable top!\n"); + } + + } + + log_verbose("Test restoring variable top\n"); + + status = U_ZERO_ERROR; + ucol_restoreVariableTop(coll, varTopOriginal, &status); + if(varTopOriginal != ucol_getVariableTop(coll, &status)) { + log_err("Couldn't restore old variable top\n"); + } + + log_verbose("Testing calling with error set\n"); + + status = U_INTERNAL_PROGRAM_ERROR; + varTop1 = ucol_setVariableTop(coll, &space, 1, &status); + varTop2 = ucol_getVariableTop(coll, &status); + ucol_restoreVariableTop(coll, varTop2, &status); + varTop1 = ucol_setVariableTop(NULL, &dot, 1, &status); + varTop2 = ucol_getVariableTop(NULL, &status); + ucol_restoreVariableTop(NULL, varTop2, &status); + if(status != U_INTERNAL_PROGRAM_ERROR) { + log_err("Bad reaction to passed error!\n"); + } + ucol_close(coll); + } else { + log_data_err("Couldn't open UCA collator\n"); + } +} + +static void TestMaxVariable(void) { + UErrorCode status = U_ZERO_ERROR; + UColReorderCode oldMax, max; + UCollator *coll; + + static const UChar nul = 0; + static const UChar space = 0x20; + static const UChar dot = 0x2e; /* punctuation */ + static const UChar degree = 0xb0; /* symbol */ + static const UChar dollar = 0x24; /* currency symbol */ + static const UChar zero = 0x30; /* digit */ + + coll = ucol_open("", &status); + if(U_FAILURE(status)) { + log_data_err("Couldn't open root collator\n"); return; } - ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, 256); - log_verbose("TestUCARules\n"); - if(ruleLen > 256) { - rules = (UChar *)malloc((ruleLen+1)*sizeof(UChar)); - ruleLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rules, ruleLen); + oldMax = ucol_getMaxVariable(coll); + log_verbose("ucol_getMaxVariable(root) -> %04x\n", oldMax); + ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); + + ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SPACE, &status); + max = ucol_getMaxVariable(coll); + log_verbose("ucol_setMaxVariable(space) -> %04x\n", max); + if(U_FAILURE(status) || max != UCOL_REORDER_CODE_SPACE || + !ucol_equal(coll, &nul, 0, &space, 1) || + ucol_equal(coll, &nul, 0, &dot, 1) || + ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &space, 1, &dot, 1)) { + log_err("ucol_setMaxVariable(space) did not work - %s\n", u_errorName(status)); } - log_verbose("Rules length is %d\n", ruleLen); - UCAfromRules = ucol_openRules(rules, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status); + + ucol_setMaxVariable(coll, UCOL_REORDER_CODE_PUNCTUATION, &status); + max = ucol_getMaxVariable(coll); + log_verbose("ucol_setMaxVariable(punctuation) -> %04x\n", max); + if(U_FAILURE(status) || max != UCOL_REORDER_CODE_PUNCTUATION || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &dot, 1, °ree, 1)) { + log_err("ucol_setMaxVariable(punctuation) did not work - %s\n", u_errorName(status)); + } + + ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SYMBOL, &status); + max = ucol_getMaxVariable(coll); + log_verbose("ucol_setMaxVariable(symbol) -> %04x\n", max); + if(U_FAILURE(status) || max != UCOL_REORDER_CODE_SYMBOL || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + !ucol_equal(coll, &nul, 0, °ree, 1) || + ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, °ree, 1, &dollar, 1)) { + log_err("ucol_setMaxVariable(symbol) did not work - %s\n", u_errorName(status)); + } + + ucol_setMaxVariable(coll, UCOL_REORDER_CODE_CURRENCY, &status); + max = ucol_getMaxVariable(coll); + log_verbose("ucol_setMaxVariable(currency) -> %04x\n", max); + if(U_FAILURE(status) || max != UCOL_REORDER_CODE_CURRENCY || + !ucol_equal(coll, &nul, 0, &space, 1) || + !ucol_equal(coll, &nul, 0, &dot, 1) || + !ucol_equal(coll, &nul, 0, °ree, 1) || + !ucol_equal(coll, &nul, 0, &dollar, 1) || + ucol_equal(coll, &nul, 0, &zero, 1) || + ucol_greaterOrEqual(coll, &dollar, 1, &zero, 1)) { + log_err("ucol_setMaxVariable(currency) did not work - %s\n", u_errorName(status)); + } + + log_verbose("Test restoring maxVariable\n"); + status = U_ZERO_ERROR; + ucol_setMaxVariable(coll, oldMax, &status); + if(oldMax != ucol_getMaxVariable(coll)) { + log_err("Couldn't restore old maxVariable\n"); + } + + log_verbose("Testing calling with error set\n"); + status = U_INTERNAL_PROGRAM_ERROR; + ucol_setMaxVariable(coll, UCOL_REORDER_CODE_SPACE, &status); + max = ucol_getMaxVariable(coll); + if(max != oldMax || status != U_INTERNAL_PROGRAM_ERROR) { + log_err("Bad reaction to passed error!\n"); + } + ucol_close(coll); +} + +static void TestNonChars(void) { + static const char *test[] = { + "\\u0000", /* ignorable */ + "\\uFFFE", /* special merge-sort character with minimum non-ignorable weights */ + "\\uFDD0", "\\uFDEF", + "\\U0001FFFE", "\\U0001FFFF", /* UCA 6.0: noncharacters are treated like unassigned, */ + "\\U0002FFFE", "\\U0002FFFF", /* not like ignorable. */ + "\\U0003FFFE", "\\U0003FFFF", + "\\U0004FFFE", "\\U0004FFFF", + "\\U0005FFFE", "\\U0005FFFF", + "\\U0006FFFE", "\\U0006FFFF", + "\\U0007FFFE", "\\U0007FFFF", + "\\U0008FFFE", "\\U0008FFFF", + "\\U0009FFFE", "\\U0009FFFF", + "\\U000AFFFE", "\\U000AFFFF", + "\\U000BFFFE", "\\U000BFFFF", + "\\U000CFFFE", "\\U000CFFFF", + "\\U000DFFFE", "\\U000DFFFF", + "\\U000EFFFE", "\\U000EFFFF", + "\\U000FFFFE", "\\U000FFFFF", + "\\U0010FFFE", "\\U0010FFFF", + "\\uFFFF" /* special character with maximum primary weight */ + }; + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = ucol_open("en_US", &status); + + log_verbose("Test non characters\n"); + if(U_SUCCESS(status)) { - ucol_close(UCAfromRules); + genericOrderingTestWithResult(coll, test, 35, UCOL_LESS); } else { - log_verbose("Unable to create a collator from UCARules!\n"); + log_err_status(status, "Unable to open collator\n"); } -/* - u_unescape(blah, b, 256); - ucol_getSortKey(coll, b, 1, res, 256); -*/ + ucol_close(coll); - if(rules != b) { - free(rules); - } } +static void TestExtremeCompression(void) { + static char *test[4]; + int32_t j = 0, i = 0; + + for(i = 0; i<4; i++) { + test[i] = (char *)malloc(2048*sizeof(char)); + } -/* Pinyin tonal order */ -/* - A < .. (\u0101) < .. (\u00e1) < .. (\u01ce) < .. (\u00e0) - (w/macron)< (w/acute)< (w/caron)< (w/grave) - E < .. (\u0113) < .. (\u00e9) < .. (\u011b) < .. (\u00e8) - I < .. (\u012b) < .. (\u00ed) < .. (\u01d0) < .. (\u00ec) - O < .. (\u014d) < .. (\u00f3) < .. (\u01d2) < .. (\u00f2) - U < .. (\u016b) < .. (\u00fa) < .. (\u01d4) < .. (\u00f9) - < .. (\u01d6) < .. (\u01d8) < .. (\u01da) < .. (\u01dc) < -.. (\u00fc) + for(j = 20; j < 500; j++) { + for(i = 0; i<4; i++) { + uprv_memset(test[i], 'a', (j-1)*sizeof(char)); + test[i][j-1] = (char)('a'+i); + test[i][j] = 0; + } + genericLocaleStarter("en_US", (const char **)test, 4); + } -However, in testing we got the following order: - A < .. (\u00e1) < .. (\u00e0) < .. (\u01ce) < .. (\u0101) - (w/acute)< (w/grave)< (w/caron)< (w/macron) - E < .. (\u00e9) < .. (\u00e8) < .. (\u00ea) < .. (\u011b) < -.. (\u0113) - I < .. (\u00ed) < .. (\u00ec) < .. (\u01d0) < .. (\u012b) - O < .. (\u00f3) < .. (\u00f2) < .. (\u01d2) < .. (\u014d) - U < .. (\u00fa) < .. (\u00f9) < .. (\u01d4) < .. (\u00fc) < -.. (\u01d8) - < .. (\u01dc) < .. (\u01da) < .. (\u01d6) < .. (\u016b) -*/ -static void TestBefore(void) { - const static char *data[] = { - "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0", "A", - "\\u0113", "\\u00e9", "\\u011b", "\\u00e8", "E", - "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec", "I", - "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2", "O", - "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9", "U", - "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc", "\\u00fc" - }; - genericRulesStarter( - "&[before 1]a<\\u0101<\\u00e1<\\u01ce<\\u00e0" - "&[before 1]e<\\u0113<\\u00e9<\\u011b<\\u00e8" - "&[before 1]i<\\u012b<\\u00ed<\\u01d0<\\u00ec" - "&[before 1]o<\\u014d<\\u00f3<\\u01d2<\\u00f2" - "&[before 1]u<\\u016b<\\u00fa<\\u01d4<\\u00f9" - "&u<\\u01d6<\\u01d8<\\u01da<\\u01dc<\\u00fc", - data, sizeof(data)/sizeof(data[0])); + for(i = 0; i<4; i++) { + free(test[i]); + } } #if 0 -/* superceded by TestBeforePinyin */ -static void TestJ784(void) { - const static char *data[] = { - "A", "\\u0101", "\\u00e1", "\\u01ce", "\\u00e0", - "E", "\\u0113", "\\u00e9", "\\u011b", "\\u00e8", - "I", "\\u012b", "\\u00ed", "\\u01d0", "\\u00ec", - "O", "\\u014d", "\\u00f3", "\\u01d2", "\\u00f2", - "U", "\\u016b", "\\u00fa", "\\u01d4", "\\u00f9", - "\\u00fc", - "\\u01d6", "\\u01d8", "\\u01da", "\\u01dc" +static void TestExtremeCompression(void) { + static char *test[4]; + int32_t j = 0, i = 0; + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = ucol_open("en_US", status); + for(i = 0; i<4; i++) { + test[i] = (char *)malloc(2048*sizeof(char)); + } + for(j = 10; j < 2048; j++) { + for(i = 0; i<4; i++) { + uprv_memset(test[i], 'a', (j-2)*sizeof(char)); + test[i][j-1] = (char)('a'+i); + test[i][j] = 0; + } + } + genericLocaleStarter("en_US", (const char **)test, 4); + + for(j = 10; j < 2048; j++) { + for(i = 0; i<1; i++) { + uprv_memset(test[i], 'a', (j-1)*sizeof(char)); + test[i][j] = 0; + } + } + for(i = 0; i<4; i++) { + free(test[i]); + } +} +#endif + +static void TestSurrogates(void) { + static const char *test[] = { + "z","\\ud900\\udc25", "\\ud805\\udc50", + "\\ud800\\udc00y", "\\ud800\\udc00r", + "\\ud800\\udc00f", "\\ud800\\udc00", + "\\ud800\\udc00c", "\\ud800\\udc00b", + "\\ud800\\udc00fa", "\\ud800\\udc00fb", + "\\ud800\\udc00a", + "c", "b" + }; + + static const char *rule = + "&z < \\ud900\\udc25 < \\ud805\\udc50" + "< \\ud800\\udc00y < \\ud800\\udc00r" + "< \\ud800\\udc00f << \\ud800\\udc00" + "< \\ud800\\udc00fa << \\ud800\\udc00fb" + "< \\ud800\\udc00a < c < b" ; + + genericRulesStarter(rule, test, 14); +} + +/* This is a test for prefix implementation, used by JIS X 4061 collation rules */ +static void TestPrefix(void) { + uint32_t i; + + static const struct { + const char *rules; + const char *data[50]; + const uint32_t len; + } tests[] = { + { "&z <<< z|a", + {"zz", "za"}, 2 }, + + { "&z <<< z| a", + {"zz", "za"}, 2 }, + { "[strength I]" + "&a=\\ud900\\udc25" + "&z<<<\\ud900\\udc25|a", + {"aa", "az", "\\ud900\\udc25z", "\\ud900\\udc25a", "zz"}, 4 }, }; - genericLocaleStarter("zh", data, sizeof(data)/sizeof(data[0])); + + + for(i = 0; i s2 +weiv will check - could be a problem with utf-8 iterator +cycheng@ca.ibm.c... but if we use the utf-8 iterator, i.e. s1 = efbfbc62 and s2 = eda08021, we have s1 < s2 +weiv hmmm +cycheng@ca.ibm.c... note that we have a standalone high surrogate +weiv that doesn't sound right +cycheng@ca.ibm.c... we got the same inconsistent results on AIX and Win2000 +weiv so you have two strings, you convert them to utf-8 and to utf-16BE +cycheng@ca.ibm.c... yes +weiv and then do the comparison +cycheng@ca.ibm.c... in one case, the input strings are in utf8, and in the other case the input strings are in utf-16be +weiv utf-16 strings look like a little endian ones in the example you sent me +weiv It could be a bug - let me try to test it out +cycheng@ca.ibm.c... ok +cycheng@ca.ibm.c... we can wait till the conf. call +cycheng@ca.ibm.c... next weke +weiv that would be great +weiv hmmm +weiv I might be wrong +weiv let me play with it some more +cycheng@ca.ibm.c... ok +cycheng@ca.ibm.c... also please check s3 = 0x0e3a0062 and s4 = 0x0e400021. both are in utf-16be +cycheng@ca.ibm.c... seems with icu 2.2 we have s3 > s4, but not in icu 2.4 that's built for db2 +cycheng@ca.ibm.c... also s1 & s2 that I sent you earlier are also in utf-16be +weiv ok +cycheng@ca.ibm.c... i ask sherman to send you more inconsistent data +weiv thanks +cycheng@ca.ibm.c... the 4 strings we sent are just samples +*/ +#if 0 +static void Alexis(void) { + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = ucol_open("", &status); - coll = ucol_open("en_US", &status); - if(status == U_FILE_ACCESS_ERROR) { - log_data_err("Is your data around?\n"); - return; - } else if(U_FAILURE(status)) { - log_err("Error opening collator\n"); - return; - } - ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); - /*for (sLen = 257; sLenimage)->jamoSpecial = TRUE; /* don't try this at home */ - genericOrderingTest(coll, koreanData, sizeof(koreanData)/sizeof(koreanData[0])); + uint8_t key[256]; - ucol_close(coll); + for(i = 0; i < UPRV_LENGTHOF(cases); i++) { + uint32_t state[2] = {0, 0}; + length = u_unescape(cases[i], currCase, 256); + uiter_setString(&iter, currCase, length); + pKeyLen = ucol_nextSortKeyPart(coll, &iter, state, key, 256, &status); + (void)pKeyLen; /* Suppress set but not used warning. */ - log_verbose("Using ko__LOTUS locale\n"); - genericLocaleStarter("ko__LOTUS", koreanData, sizeof(koreanData)/sizeof(koreanData[0])); + log_verbose("Done\n"); + + } + ucol_close(coll); } -static void TestCompressOverlap(void) { - UChar secstr[150]; - UChar tertstr[150]; - UErrorCode status = U_ZERO_ERROR; - UCollator *coll; - char result[200]; - uint32_t resultlen; - int count = 0; - char *tempptr; +static void TestSettings(void) { + static const char* cases[] = { + "apple", + "Apple" + }; - coll = ucol_open("", &status); + static const char* locales[] = { + "", + "en" + }; - if (U_FAILURE(status)) { - log_err("Collator can't be created\n"); - return; - } - while (count < 149) { - secstr[count] = 0x0020; /* [06, 05, 05] */ - tertstr[count] = 0x0020; - count ++; + UErrorCode status = U_ZERO_ERROR; + + int32_t i = 0, j = 0; + + UChar source[256], target[256]; + int32_t sLen = 0, tLen = 0; + + UCollator *collateObject = NULL; + for(i = 0; i < UPRV_LENGTHOF(locales); i++) { + collateObject = ucol_open(locales[i], &status); + ucol_setStrength(collateObject, UCOL_PRIMARY); + ucol_setAttribute(collateObject, UCOL_CASE_LEVEL , UCOL_OFF, &status); + for(j = 1; j < UPRV_LENGTHOF(cases); j++) { + sLen = u_unescape(cases[j-1], source, 256); + source[sLen] = 0; + tLen = u_unescape(cases[j], target, 256); + source[tLen] = 0; + doTest(collateObject, source, target, UCOL_EQUAL); } + ucol_close(collateObject); + } +} - /* top down compression ----------------------------------- */ - secstr[count] = 0x0332; /* [, 87, 05] */ - tertstr[count] = 0x3000; /* [06, 05, 07] */ +static int32_t TestEqualsForCollator(const char* locName, UCollator *source, UCollator *target) { + UErrorCode status = U_ZERO_ERROR; + int32_t errorNo = 0; + const UChar *sourceRules = NULL; + int32_t sourceRulesLen = 0; + UParseError parseError; + UColAttributeValue french = UCOL_OFF; - /* no compression secstr should have 150 secondary bytes, tertstr should - have 150 tertiary bytes. - with correct overlapping compression, secstr should have 4 secondary - bytes, tertstr should have > 2 tertiary bytes */ - resultlen = ucol_getSortKey(coll, secstr, 150, (uint8_t *)result, 250); - tempptr = uprv_strchr(result, 1) + 1; - while (*(tempptr + 1) != 1) { - /* the last secondary collation element is not checked since it is not - part of the compression */ - if (*tempptr < UCOL_COMMON_TOP2 - UCOL_TOP_COUNT2) { - log_err("Secondary compression overlapped\n"); + if(!ucol_equals(source, target)) { + log_err("Same collators, different address not equal\n"); + errorNo++; + } + ucol_close(target); + if(uprv_strcmp(locName, ucol_getLocaleByType(source, ULOC_ACTUAL_LOCALE, &status)) == 0) { + target = ucol_safeClone(source, NULL, NULL, &status); + if(U_FAILURE(status)) { + log_err("Error creating clone\n"); + errorNo++; + return errorNo; } - tempptr ++; + if(!ucol_equals(source, target)) { + log_err("Collator different from it's clone\n"); + errorNo++; + } + french = ucol_getAttribute(source, UCOL_FRENCH_COLLATION, &status); + if(french == UCOL_ON) { + ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_OFF, &status); + } else { + ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_ON, &status); + } + if(U_FAILURE(status)) { + log_err("Error setting attributes\n"); + errorNo++; + return errorNo; + } + if(ucol_equals(source, target)) { + log_err("Collators same even when options changed\n"); + errorNo++; + } + ucol_close(target); + + sourceRules = ucol_getRules(source, &sourceRulesLen); + target = ucol_openRules(sourceRules, sourceRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status); + if(U_FAILURE(status)) { + log_err("Error instantiating target from rules - %s\n", u_errorName(status)); + errorNo++; + return errorNo; + } + /* Note: The tailoring rule string is an optional data item. */ + if(!ucol_equals(source, target) && sourceRulesLen != 0) { + log_err("Collator different from collator that was created from the same rules\n"); + errorNo++; + } + ucol_close(target); } + return errorNo; +} - /* tertiary top/bottom/common for en_US is similar to the secondary - top/bottom/common */ - resultlen = ucol_getSortKey(coll, tertstr, 150, (uint8_t *)result, 250); - tempptr = uprv_strrchr(result, 1) + 1; - while (*(tempptr + 1) != 0) { - /* the last secondary collation element is not checked since it is not - part of the compression */ - if (*tempptr < coll->tertiaryTop - coll->tertiaryTopCount) { - log_err("Tertiary compression overlapped\n"); - } - tempptr ++; + +static void TestEquals(void) { + /* ucol_equals is not currently a public API. There is a chance that it will become + * something like this. + */ + /* test whether the two collators instantiated from the same locale are equal */ + UErrorCode status = U_ZERO_ERROR; + UParseError parseError; + int32_t noOfLoc = uloc_countAvailable(); + const char *locName = NULL; + UCollator *source = NULL, *target = NULL; + int32_t i = 0; + + const char* rules[] = { + "&l < lj <<< Lj <<< LJ", + "&n < nj <<< Nj <<< NJ", + "&ae <<< \\u00e4", + "&AE <<< \\u00c4" + }; + /* + const char* badRules[] = { + "&l <<< Lj", + "&n < nj <<< nJ <<< NJ", + "&a <<< \\u00e4", + "&AE <<< \\u00c4 <<< x" + }; + */ + + UChar sourceRules[1024], targetRules[1024]; + int32_t sourceRulesSize = 0, targetRulesSize = 0; + int32_t rulesSize = UPRV_LENGTHOF(rules); + + for(i = 0; i < rulesSize; i++) { + sourceRulesSize += u_unescape(rules[i], sourceRules+sourceRulesSize, 1024 - sourceRulesSize); + targetRulesSize += u_unescape(rules[rulesSize-i-1], targetRules+targetRulesSize, 1024 - targetRulesSize); } - /* bottom up compression ------------------------------------- */ - secstr[count] = 0; - tertstr[count] = 0; - resultlen = ucol_getSortKey(coll, secstr, 150, (uint8_t *)result, 250); - tempptr = uprv_strchr(result, 1) + 1; - while (*(tempptr + 1) != 1) { - /* the last secondary collation element is not checked since it is not - part of the compression */ - if (*tempptr > UCOL_COMMON_BOT2 + UCOL_BOT_COUNT2) { - log_err("Secondary compression overlapped\n"); - } - tempptr ++; + source = ucol_openRules(sourceRules, sourceRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status); + if(status == U_FILE_ACCESS_ERROR) { + log_data_err("Is your data around?\n"); + return; + } else if(U_FAILURE(status)) { + log_err("Error opening collator\n"); + return; + } + target = ucol_openRules(targetRules, targetRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status); + if(!ucol_equals(source, target)) { + log_err("Equivalent collators not equal!\n"); } + ucol_close(source); + ucol_close(target); - /* tertiary top/bottom/common for en_US is similar to the secondary - top/bottom/common */ - resultlen = ucol_getSortKey(coll, tertstr, 150, (uint8_t *)result, 250); - tempptr = uprv_strrchr(result, 1) + 1; - while (*(tempptr + 1) != 0) { - /* the last secondary collation element is not checked since it is not - part of the compression */ - if (*tempptr > coll->tertiaryBottom + coll->tertiaryBottomCount) { - log_err("Tertiary compression overlapped\n"); + source = ucol_open("root", &status); + target = ucol_open("root", &status); + log_verbose("Testing root\n"); + if(!ucol_equals(source, source)) { + log_err("Same collator not equal\n"); + } + if(TestEqualsForCollator("root", source, target)) { + log_err("Errors for root\n"); + } + ucol_close(source); + + for(i = 0; i %s\n", u_errorName(status)); + } else { + ucol_close(coll); + } } -static void TestSuppressContractions(void) { +/** + * Test for CollationElementIterator previous and next for the whole set of + * unicode characters with normalization on. + */ +static void TestNumericCollation(void) +{ + UErrorCode status = U_ZERO_ERROR; - static const char *testNoCont2[] = { - "\\u0410\\u0302a", - "\\u0410\\u0306b", - "\\u0410c" - }; - static const char *testNoCont[] = { - "a\\u0410", - "A\\u0410\\u0306", - "\\uFF21\\u0410\\u0302" - }; + const static char *basicTestStrings[]={ + "hello1", + "hello2", + "hello2002", + "hello2003", + "hello123456", + "hello1234567", + "hello10000000", + "hello100000000", + "hello1000000000", + "hello10000000000", + }; - genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont, 3); - genericRulesStarter("[suppressContractions [\\u0400-\\u047f]]", testNoCont2, 3); -} + const static char *preZeroTestStrings[]={ + "avery10000", + "avery010000", + "avery0010000", + "avery00010000", + "avery000010000", + "avery0000010000", + "avery00000010000", + "avery000000010000", + }; -static void TestContraction(void) { - const static char *testrules[] = { - "&A = AB / B", - "&A = A\\u0306/\\u0306", - "&c = ch / h" + const static char *thirtyTwoBitNumericStrings[]={ + "avery42949672960", + "avery42949672961", + "avery42949672962", + "avery429496729610" }; - const static UChar testdata[][2] = { - {0x0041 /* 'A' */, 0x0042 /* 'B' */}, - {0x0041 /* 'A' */, 0x0306 /* combining breve */}, - {0x0063 /* 'c' */, 0x0068 /* 'h' */} + + const static char *longNumericStrings[]={ + /* Some of these sort out of the order that would expected if digits-as-numbers handled arbitrarily-long digit strings. + In fact, a single collation element can represent a maximum of 254 digits as a number. Digit strings longer than that + are treated as multiple collation elements. */ + "num9234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123z", /*253digits, num + 9.23E252 + z */ + "num10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*254digits, num + 1.00E253 */ + "num100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*255digits, num + 1.00E253 + 0, out of numeric order but expected */ + "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 1.23E253 */ + "num123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", /*255digits, num + 1.23E253 + 5 */ + "num1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456", /*256digits, num + 1.23E253 + 56 */ + "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567", /*257digits, num + 1.23E253 + 567 */ + "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 1.23E253 + a, out of numeric order but expected */ + "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 9.23E253, out of numeric order but expected */ + "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 9.23E253 + a, out of numeric order but expected */ }; - const static UChar testdata2[][2] = { - {0x0063 /* 'c' */, 0x0067 /* 'g' */}, - {0x0063 /* 'c' */, 0x0068 /* 'h' */}, - {0x0063 /* 'c' */, 0x006C /* 'l' */} + + const static char *supplementaryDigits[] = { + "\\uD835\\uDFCE", /* 0 */ + "\\uD835\\uDFCF", /* 1 */ + "\\uD835\\uDFD0", /* 2 */ + "\\uD835\\uDFD1", /* 3 */ + "\\uD835\\uDFCF\\uD835\\uDFCE", /* 10 */ + "\\uD835\\uDFCF\\uD835\\uDFCF", /* 11 */ + "\\uD835\\uDFCF\\uD835\\uDFD0", /* 12 */ + "\\uD835\\uDFD0\\uD835\\uDFCE", /* 20 */ + "\\uD835\\uDFD0\\uD835\\uDFCF", /* 21 */ + "\\uD835\\uDFD0\\uD835\\uDFD0" /* 22 */ }; - const static char *testrules3[] = { - "&z < xyz &xyzw << B", - "&z < xyz &xyz << B / w", - "&z < ch &achm << B", - "&z < ch &a << B / chm", - "&\\ud800\\udc00w << B", - "&\\ud800\\udc00 << B / w", - "&a\\ud800\\udc00m << B", - "&a << B / \\ud800\\udc00m", + + const static char *foreignDigits[] = { + "\\u0661", + "\\u0662", + "\\u0663", + "\\u0661\\u0660", + "\\u0661\\u0662", + "\\u0661\\u0663", + "\\u0662\\u0660", + "\\u0662\\u0662", + "\\u0662\\u0663", + "\\u0663\\u0660", + "\\u0663\\u0662", + "\\u0663\\u0663" }; - UErrorCode status = U_ZERO_ERROR; - UCollator *coll; - UChar rule[256] = {0}; - uint32_t rlen = 0; - int i; + const static char *evenZeroes[] = { + "2000", + "2001", + "2002", + "2003" + }; - for (i = 0; i < sizeof(testrules) / sizeof(testrules[0]); i ++) { - UCollationElements *iter1; - int j = 0; - log_verbose("Rule %s for testing\n", testrules[i]); - rlen = u_unescape(testrules[i], rule, 32); - coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); - if (U_FAILURE(status)) { - log_err("Collator creation failed %s\n", testrules[i]); - return; - } - iter1 = ucol_openElements(coll, testdata[i], 2, &status); - if (U_FAILURE(status)) { - log_err("Collation iterator creation failed\n"); - return; - } - while (j < 2) { - UCollationElements *iter2 = ucol_openElements(coll, - &(testdata[i][j]), - 1, &status); - uint32_t ce; - if (U_FAILURE(status)) { - log_err("Collation iterator creation failed\n"); - return; - } - ce = ucol_next(iter2, &status); - while (ce != UCOL_NULLORDER) { - if ((uint32_t)ucol_next(iter1, &status) != ce) { - log_err("Collation elements in contraction split does not match\n"); - return; - } - ce = ucol_next(iter2, &status); - } - j ++; - ucol_closeElements(iter2); - } - if (ucol_next(iter1, &status) != UCOL_NULLORDER) { - log_err("Collation elements not exhausted\n"); - return; - } - ucol_closeElements(iter1); - ucol_close(coll); - } + UColAttribute att = UCOL_NUMERIC_COLLATION; + UColAttributeValue val = UCOL_ON; - rlen = u_unescape("& a < b < c < ch < d & c = ch / h", rule, 256); - coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); - if (ucol_strcoll(coll, testdata2[0], 2, testdata2[1], 2) != UCOL_LESS) { - log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n", - testdata2[0][0], testdata2[0][1], testdata2[1][0], - testdata2[1][1]); + /* Open our collator. */ + UCollator* coll = ucol_open("root", &status); + if (U_FAILURE(status)){ + log_err_status(status, "ERROR: in using ucol_open() -> %s\n", + myErrorName(status)); return; } - if (ucol_strcoll(coll, testdata2[1], 2, testdata2[2], 2) != UCOL_LESS) { - log_err("Expected \\u%04x\\u%04x < \\u%04x\\u%04x\n", - testdata2[1][0], testdata2[1][1], testdata2[2][0], - testdata2[2][1]); + genericLocaleStarterWithOptions("root", basicTestStrings, UPRV_LENGTHOF(basicTestStrings), &att, &val, 1); + genericLocaleStarterWithOptions("root", thirtyTwoBitNumericStrings, UPRV_LENGTHOF(thirtyTwoBitNumericStrings), &att, &val, 1); + genericLocaleStarterWithOptions("root", longNumericStrings, UPRV_LENGTHOF(longNumericStrings), &att, &val, 1); + genericLocaleStarterWithOptions("en_US", foreignDigits, UPRV_LENGTHOF(foreignDigits), &att, &val, 1); + genericLocaleStarterWithOptions("root", supplementaryDigits, UPRV_LENGTHOF(supplementaryDigits), &att, &val, 1); + genericLocaleStarterWithOptions("root", evenZeroes, UPRV_LENGTHOF(evenZeroes), &att, &val, 1); + + /* Setting up our collator to do digits. */ + ucol_setAttribute(coll, UCOL_NUMERIC_COLLATION, UCOL_ON, &status); + if (U_FAILURE(status)){ + log_err("ERROR: in setting UCOL_NUMERIC_COLLATION as an attribute\n %s\n", + myErrorName(status)); return; } + + /* + Testing that prepended zeroes still yield the correct collation behavior. + We expect that every element in our strings array will be equal. + */ + genericOrderingTestWithResult(coll, preZeroTestStrings, UPRV_LENGTHOF(preZeroTestStrings), UCOL_EQUAL); + ucol_close(coll); +} - for (i = 0; i < sizeof(testrules3) / sizeof(testrules3[0]); i += 2) { - UCollator *coll1, - *coll2; - UCollationElements *iter1, - *iter2; - UChar ch = 0x0042 /* 'B' */; - uint32_t ce; - rlen = u_unescape(testrules3[i], rule, 32); - coll1 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); - rlen = u_unescape(testrules3[i + 1], rule, 32); - coll2 = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); - if (U_FAILURE(status)) { - log_err("Collator creation failed %s\n", testrules[i]); - return; - } - iter1 = ucol_openElements(coll1, &ch, 1, &status); - iter2 = ucol_openElements(coll2, &ch, 1, &status); - if (U_FAILURE(status)) { - log_err("Collation iterator creation failed\n"); - return; - } - ce = ucol_next(iter1, &status); - if (U_FAILURE(status)) { - log_err("Retrieving ces failed\n"); - return; - } - while (ce != UCOL_NULLORDER) { - if (ce != (uint32_t)ucol_next(iter2, &status)) { - log_err("CEs does not match\n"); - return; - } - ce = ucol_next(iter1, &status); - if (U_FAILURE(status)) { - log_err("Retrieving ces failed\n"); - return; - } - } - if (ucol_next(iter2, &status) != UCOL_NULLORDER) { - log_err("CEs not exhausted\n"); - return; +static void TestTibetanConformance(void) +{ + const char* test[] = { + "\\u0FB2\\u0591\\u0F71\\u0061", + "\\u0FB2\\u0F71\\u0061" + }; + + UErrorCode status = U_ZERO_ERROR; + UCollator *coll = ucol_open("", &status); + UChar source[100]; + UChar target[100]; + int result; + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); + if (U_SUCCESS(status)) { + u_unescape(test[0], source, 100); + u_unescape(test[1], target, 100); + doTest(coll, source, target, UCOL_EQUAL); + result = ucol_strcoll(coll, source, -1, target, -1); + log_verbose("result %d\n", result); + if (UCOL_EQUAL != result) { + log_err("Tibetan comparison error\n"); } - ucol_closeElements(iter1); - ucol_closeElements(iter2); - ucol_close(coll1); - ucol_close(coll2); } + ucol_close(coll); + + genericLocaleStarterWithResult("", test, 2, UCOL_EQUAL); } -static void TestExpansion(void) { - const static char *testrules[] = { - "&J << K / B & K << M", - "&J << K / B << M" - }; - const static UChar testdata[][3] = { - {0x004A /*'J'*/, 0x0041 /*'A'*/, 0}, - {0x004D /*'M'*/, 0x0041 /*'A'*/, 0}, - {0x004B /*'K'*/, 0x0041 /*'A'*/, 0}, - {0x004B /*'K'*/, 0x0043 /*'C'*/, 0}, - {0x004A /*'J'*/, 0x0043 /*'C'*/, 0}, - {0x004D /*'M'*/, 0x0043 /*'C'*/, 0} - }; +static void TestPinyinProblem(void) { + static const char *test[] = { "\\u4E56\\u4E56\\u7761", "\\u4E56\\u5B69\\u5B50" }; + genericLocaleStarter("zh__PINYIN", test, UPRV_LENGTHOF(test)); +} - UErrorCode status = U_ZERO_ERROR; - UCollator *coll; - UChar rule[256] = {0}; - uint32_t rlen = 0; - int i; +/** + * Iterate through the given iterator, checking to see that all the strings + * in the expected array are present. + * @param expected array of strings we expect to see, or NULL + * @param expectedCount number of elements of expected, or 0 + */ +static int32_t checkUEnumeration(const char* msg, + UEnumeration* iter, + const char** expected, + int32_t expectedCount) { + UErrorCode ec = U_ZERO_ERROR; + int32_t i = 0, n, j, bit; + int32_t seenMask = 0; - for (i = 0; i < sizeof(testrules) / sizeof(testrules[0]); i ++) { - int j = 0; - log_verbose("Rule %s for testing\n", testrules[i]); - rlen = u_unescape(testrules[i], rule, 32); - coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status); - if (U_FAILURE(status)) { - log_err("Collator creation failed %s\n", testrules[i]); - return; + U_ASSERT(expectedCount >= 0 && expectedCount < 31); /* [sic] 31 not 32 */ + n = uenum_count(iter, &ec); + if (!assertSuccess("count", &ec)) return -1; + log_verbose("%s = [", msg); + for (;; ++i) { + const char* s = uenum_next(iter, NULL, &ec); + if (!assertSuccess("snext", &ec) || s == NULL) break; + if (i != 0) log_verbose(","); + log_verbose("%s", s); + /* check expected list */ + for (j=0, bit=1; j %s (Are you missing data?)\n", u_errorName(ec)); + } +#endif - log_verbose("variable top\n"); - genericRulesStarterWithOptions(rule, tlimit03, sizeof(tlimit03)/sizeof(tlimit03[0]), att, valOn, sizeof(att)/sizeof(att[0])); - genericRulesStarterWithOptions(rule, tlimit01, sizeof(tlimit01)/sizeof(tlimit01[0]), att, valOn, sizeof(att)/sizeof(att[0])); - genericRulesStarterWithOptions(rule, tlimit02, sizeof(tlimit02)/sizeof(tlimit02[0]), att, valOn, sizeof(att)/sizeof(att[0])); - genericRulesStarterWithOptions(rule, tlimit01, sizeof(tlimit01)/sizeof(tlimit01[0]), att, valOff, sizeof(att)/sizeof(att[0])); - genericRulesStarterWithOptions(rule, tlimit02, sizeof(tlimit02)/sizeof(tlimit02[0]), att, valOff, sizeof(att)/sizeof(att[0])); + e = ucol_getKeywords(&ec); + if (e != NULL) { + assertSuccess("ucol_getKeywords", &ec); + assertTrue("ucol_getKeywords!=0", e!=0); + n = checkUEnumeration("ucol_getKeywords", e, KW, UPRV_LENGTHOF(KW)); + /* Don't need to check n because we check list */ + uenum_close(e); + } else { + log_data_err("Error calling ucol_getKeywords() -> %s (Are you missing data?)\n", u_errorName(ec)); + } - } - /* case level */ - { - static const char *rule = "&c %s (Are you missing data?)\n", u_errorName(ec)); + } + + /* Try setting a warning before calling ucol_getKeywordValues */ + ec = U_USING_FALLBACK_WARNING; + e = ucol_getKeywordValues(KW[0], &ec); + if (assertSuccess("ucol_getKeywordValues [with warning code set]", &ec)) { + assertTrue("ucol_getKeywordValues!=0 [with warning code set]", e!=0); + n = checkUEnumeration("ucol_getKeywordValues [with warning code set]", e, KWVAL, UPRV_LENGTHOF(KWVAL)); + /* Don't need to check n because we check list */ + uenum_close(e); + } + /* +U_DRAFT int32_t U_EXPORT2 +ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity, + const char* locale, UBool* isAvailable, + UErrorCode* status); } -#endif +*/ + n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de", + &isAvailable, &ec); + if (assertSuccess("getFunctionalEquivalent", &ec)) { + assertEquals("getFunctionalEquivalent(de)", "root", loc); + assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE", + isAvailable == TRUE); + } -static void TestBocsuCoverage(void) { - UErrorCode status = U_ZERO_ERROR; - const char *testString = "\\u0041\\u0441\\u4441\\U00044441\\u4441\\u0441\\u0041"; - UChar test[256] = {0}; - uint32_t tlen = u_unescape(testString, test, 32); - uint8_t key[256] = {0}; - uint32_t klen = 0; + n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de_DE", + &isAvailable, &ec); + if (assertSuccess("getFunctionalEquivalent", &ec)) { + assertEquals("getFunctionalEquivalent(de_DE)", "root", loc); + assertTrue("getFunctionalEquivalent(de_DE).isAvailable==FALSE", + isAvailable == FALSE); + } +} - UCollator *coll = ucol_open("", &status); - if(U_SUCCESS(status)) { - ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &status); +/* supercedes TestJ784 */ +static void TestBeforePinyin(void) { + const static char rules[] = { + "&[before 2]A<<\\u0101<<<\\u0100<<\\u00E1<<<\\u00C1<<\\u01CE<<<\\u01CD<<\\u00E0<<<\\u00C0" + "&[before 2]e<<\\u0113<<<\\u0112<<\\u00E9<<<\\u00C9<<\\u011B<<<\\u011A<<\\u00E8<<<\\u00C8" + "&[before 2]i<<\\u012B<<<\\u012A<<\\u00ED<<<\\u00CD<<\\u01D0<<<\\u01CF<<\\u00EC<<<\\u00CC" + "&[before 2]o<<\\u014D<<<\\u014C<<\\u00F3<<<\\u00D3<<\\u01D2<<<\\u01D1<<\\u00F2<<<\\u00D2" + "&[before 2]u<<\\u016B<<<\\u016A<<\\u00FA<<<\\u00DA<<\\u01D4<<<\\u01D3<<\\u00F9<<<\\u00D9" + "&U<<\\u01D6<<<\\u01D5<<\\u01D8<<<\\u01D7<<\\u01DA<<<\\u01D9<<\\u01DC<<<\\u01DB<<\\u00FC" + }; + + const static char *test[] = { + "l\\u0101", + "la", + "l\\u0101n", + "lan ", + "l\\u0113", + "le", + "l\\u0113n", + "len" + }; + + const static char *test2[] = { + "x\\u0101", + "x\\u0100", + "X\\u0101", + "X\\u0100", + "x\\u00E1", + "x\\u00C1", + "X\\u00E1", + "X\\u00C1", + "x\\u01CE", + "x\\u01CD", + "X\\u01CE", + "X\\u01CD", + "x\\u00E0", + "x\\u00C0", + "X\\u00E0", + "X\\u00C0", + "xa", + "xA", + "Xa", + "XA", + "x\\u0101x", + "x\\u0100x", + "x\\u00E1x", + "x\\u00C1x", + "x\\u01CEx", + "x\\u01CDx", + "x\\u00E0x", + "x\\u00C0x", + "xax", + "xAx" + }; - klen = ucol_getSortKey(coll, test, tlen, key, 256); + const static char *test3[] = { // rdar://53741390 + "\\u85CF", // 藏 cáng + "\\u92BA", // 銺 zàng + "\\u85CF\\u6587", // 藏文 zàngwén + "\\u85CF\\u8BED", // 藏语 zàngyǔ + "\\u81D3", // 臓 zàng + }; - ucol_close(coll); - } else { - log_data_err("Couldn't open UCA\n"); - } + genericRulesStarter(rules, test, UPRV_LENGTHOF(test)); + genericLocaleStarter("zh", test, UPRV_LENGTHOF(test)); + genericRulesStarter(rules, test2, UPRV_LENGTHOF(test2)); + genericLocaleStarter("zh", test2, UPRV_LENGTHOF(test2)); + genericLocaleStarter("zh", test3, UPRV_LENGTHOF(test3)); } -static void TestVariableTopSetting(void) { - UErrorCode status = U_ZERO_ERROR; - const UChar *current = NULL; - uint32_t varTopOriginal = 0, varTop1, varTop2; - UCollator *coll = ucol_open("", &status); - if(U_SUCCESS(status)) { +static void TestBeforeTightening(void) { + static const struct { + const char *rules; + UErrorCode expectedStatus; + } tests[] = { + { "&[before 1]a 0*/ - log_verbose("Slide variable top over UCARules\n"); - rulesLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rulesCopy, 0); - rulesCopy = (UChar *)malloc((rulesLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); - rulesLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rulesCopy, rulesLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE); - - if(U_SUCCESS(status) && rulesLen > 0) { - ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); - src.current = src.source = rulesCopy; - src.end = rulesCopy+rulesLen; - src.extraCurrent = src.end; - src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE; - - while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,&status)) != NULL) { - strength = src.parsedToken.strength; - chOffset = src.parsedToken.charsOffset; - chLen = src.parsedToken.charsLen; - exOffset = src.parsedToken.extensionOffset; - exLen = src.parsedToken.extensionLen; - prefixOffset = src.parsedToken.prefixOffset; - prefixLen = src.parsedToken.prefixLen; - specs = src.parsedToken.flags; - - startOfRules = FALSE; - if(0) { - log_verbose("%04X %d ", *(rulesCopy+chOffset), chLen); - } - if(strength == UCOL_PRIMARY) { - status = U_ZERO_ERROR; - varTopOriginal = ucol_getVariableTop(coll, &status); - varTop1 = ucol_setVariableTop(coll, rulesCopy+oldChOffset, oldChLen, &status); - if(U_FAILURE(status)) { - char buffer[256]; - char *buf = buffer; - uint32_t i = 0, j; - uint32_t CE = UCOL_NO_MORE_CES; - - /* before we start screaming, let's see if there is a problem with the rules */ - collIterate s; - uprv_init_collIterate(coll, rulesCopy+oldChOffset, oldChLen, &s); - - CE = ucol_getNextCE(coll, &s, &status); - - for(i = 0; i < oldChLen; i++) { - j = sprintf(buf, "%04X ", *(rulesCopy+oldChOffset+i)); - buf += j; - } - if(status == U_PRIMARY_TOO_LONG_ERROR) { - log_verbose("= Expected failure for %s =", buffer); - } else { - if(s.pos == s.endp) { - log_err("Unexpected failure setting variable top at offset %d. Error %s. Codepoints: %s\n", - oldChOffset, u_errorName(status), buffer); - } else { - log_verbose("There is a goofy contraction in UCA rules that does not appear in the fractional UCA. Codepoints: %s\n", - buffer); - } - } - } - varTop2 = ucol_getVariableTop(coll, &status); - if((varTop1 & 0xFFFF0000) != (varTop2 & 0xFFFF0000)) { - log_err("cannot retrieve set varTop value!\n"); - continue; - } + UErrorCode status = U_ZERO_ERROR; + UChar rlz[RULE_BUFFER_LEN] = { 0 }; + uint32_t rlen = 0; - if((varTop1 & 0xFFFF0000) > 0 && oldExLen == 0) { - - u_strncpy(first, rulesCopy+oldChOffset, oldChLen); - u_strncpy(first+oldChLen, rulesCopy+chOffset, chLen); - u_strncpy(first+oldChLen+chLen, rulesCopy+oldChOffset, oldChLen); - first[2*oldChLen+chLen] = 0; - - if(oldExLen == 0) { - u_strncpy(second, rulesCopy+chOffset, chLen); - second[chLen] = 0; - } else { /* This is skipped momentarily, but should work once UCARules are fully UCA conformant */ - u_strncpy(second, rulesCopy+oldExOffset, oldExLen); - u_strncpy(second+oldChLen, rulesCopy+chOffset, chLen); - u_strncpy(second+oldChLen+chLen, rulesCopy+oldExOffset, oldExLen); - second[2*oldExLen+chLen] = 0; - } - result = ucol_strcoll(coll, first, -1, second, -1); - if(result == UCOL_EQUAL) { - doTest(coll, first, second, UCOL_EQUAL); - } else { - log_verbose("Suspicious strcoll result for %04X and %04X\n", *(rulesCopy+oldChOffset), *(rulesCopy+chOffset)); - } - } - } - if(strength != UCOL_TOK_RESET) { - oldChOffset = chOffset; - oldChLen = chLen; - oldExOffset = exOffset; - oldExLen = exLen; - } - } - status = U_ZERO_ERROR; - } - else { - log_err("Unexpected failure getting rules %s\n", u_errorName(status)); - return; - } - if (U_FAILURE(status)) { - log_err("Error parsing rules %s\n", u_errorName(status)); - return; - } - status = U_ZERO_ERROR; - } + UCollator *coll = NULL; - QUICK = myQ; - log_verbose("Testing setting variable top to contractions\n"); - { - /* uint32_t tailoredCE = UCOL_NOT_FOUND; */ - /*UChar *conts = (UChar *)((uint8_t *)coll->image + coll->image->UCAConsts+sizeof(UCAConstants));*/ - UChar *conts = (UChar *)((uint8_t *)coll->image + coll->image->contractionUCACombos); - while(*conts != 0) { - if(*(conts+2) == 0) { - varTop1 = ucol_setVariableTop(coll, conts, -1, &status); - } else { - varTop1 = ucol_setVariableTop(coll, conts, 3, &status); - } - if(U_FAILURE(status)) { - log_err("Couldn't set variable top to a contraction %04X %04X %04X\n", - *conts, *(conts+1), *(conts+2)); + for(i = 0; i < UPRV_LENGTHOF(tests); i++) { + rlen = u_unescape(tests[i].rules, rlz, RULE_BUFFER_LEN); + coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status); + if(status != tests[i].expectedStatus) { + log_err_status(status, "Opening a collator with rules %s returned error code %s, expected %s\n", + tests[i].rules, u_errorName(status), u_errorName(tests[i].expectedStatus)); + } + ucol_close(coll); status = U_ZERO_ERROR; - } - conts+=3; } - status = U_ZERO_ERROR; +} - first[0] = 0x0040; - first[1] = 0x0050; - first[2] = 0x0000; +/* +&m < a +&[before 1] a < x <<< X << q <<< Q < z +assert: m <<< M < x <<< X << q <<< Q < z < a < n - ucol_setVariableTop(coll, first, -1, &status); +&m < a +&[before 2] a << x <<< X << q <<< Q < z +assert: m <<< M < x <<< X << q <<< Q << a < z < n - if(U_SUCCESS(status)) { - log_err("Invalid contraction succeded in setting variable top!\n"); - } +&m < a +&[before 3] a <<< x <<< X << q <<< Q < z +assert: m <<< M < x <<< X <<< a << q <<< Q < z < n - } - log_verbose("Test restoring variable top\n"); +&m << a +&[before 1] a < x <<< X << q <<< Q < z +assert: x <<< X << q <<< Q < z < m <<< M << a < n - status = U_ZERO_ERROR; - ucol_restoreVariableTop(coll, varTopOriginal, &status); - if(varTopOriginal != ucol_getVariableTop(coll, &status)) { - log_err("Couldn't restore old variable top\n"); - } +&m << a +&[before 2] a << x <<< X << q <<< Q < z +assert: m <<< M << x <<< X << q <<< Q << a < z < n - log_verbose("Testing calling with error set\n"); +&m << a +&[before 3] a <<< x <<< X << q <<< Q < z +assert: m <<< M << x <<< X <<< a << q <<< Q < z < n - status = U_INTERNAL_PROGRAM_ERROR; - varTop1 = ucol_setVariableTop(coll, first, 1, &status); - varTop2 = ucol_getVariableTop(coll, &status); - ucol_restoreVariableTop(coll, varTop2, &status); - varTop1 = ucol_setVariableTop(NULL, first, 1, &status); - varTop2 = ucol_getVariableTop(NULL, &status); - ucol_restoreVariableTop(NULL, varTop2, &status); - if(status != U_INTERNAL_PROGRAM_ERROR) { - log_err("Bad reaction to passed error!\n"); - } - free(rulesCopy); - ucol_close(coll); - } else { - log_data_err("Couldn't open UCA collator\n"); - } -} +&m <<< a +&[before 1] a < x <<< X << q <<< Q < z +assert: x <<< X << q <<< Q < z < n < m <<< a <<< M -static void TestNonChars(void) { - static const char *test[] = { - "\\u0000", - "\\uFFFE", "\\uFFFF", - "\\U0001FFFE", "\\U0001FFFF", - "\\U0002FFFE", "\\U0002FFFF", - "\\U0003FFFE", "\\U0003FFFF", - "\\U0004FFFE", "\\U0004FFFF", - "\\U0005FFFE", "\\U0005FFFF", - "\\U0006FFFE", "\\U0006FFFF", - "\\U0007FFFE", "\\U0007FFFF", - "\\U0008FFFE", "\\U0008FFFF", - "\\U0009FFFE", "\\U0009FFFF", - "\\U000AFFFE", "\\U000AFFFF", - "\\U000BFFFE", "\\U000BFFFF", - "\\U000CFFFE", "\\U000CFFFF", - "\\U000DFFFE", "\\U000DFFFF", - "\\U000EFFFE", "\\U000EFFFF", - "\\U000FFFFE", "\\U000FFFFF", - "\\U0010FFFE", "\\U0010FFFF" - }; - UErrorCode status = U_ZERO_ERROR; - UCollator *coll = ucol_open("en_US", &status); +&m <<< a +&[before 2] a << x <<< X << q <<< Q < z +assert: x <<< X << q <<< Q << m <<< a <<< M < z < n - log_verbose("Test non characters\n"); +&m <<< a +&[before 3] a <<< x <<< X << q <<< Q < z +assert: m <<< x <<< X <<< a <<< M << q <<< Q < z < n - if(U_SUCCESS(status)) { - genericOrderingTestWithResult(coll, test, 35, UCOL_EQUAL); - } else { - log_err("Unable to open collator\n"); - } - ucol_close(coll); -} +&[before 1] s < x <<< X << q <<< Q < z +assert: r <<< R < x <<< X << q <<< Q < z < s < n -static void TestExtremeCompression(void) { - static char *test[4]; - int32_t j = 0, i = 0; +&[before 2] s << x <<< X << q <<< Q < z +assert: r <<< R < x <<< X << q <<< Q << s < z < n - for(i = 0; i<4; i++) { - test[i] = (char *)malloc(2048*sizeof(char)); - } +&[before 3] s <<< x <<< X << q <<< Q < z +assert: r <<< R < x <<< X <<< s << q <<< Q < z < n - for(j = 20; j < 500; j++) { - for(i = 0; i<4; i++) { - uprv_memset(test[i], 'a', (j-1)*sizeof(char)); - test[i][j-1] = (char)('a'+i); - test[i][j] = 0; - } - genericLocaleStarter("en_US", (const char **)test, 4); - } +&[before 1] \u24DC < x <<< X << q <<< Q < z +assert: x <<< X << q <<< Q < z < n < m <<< \u24DC <<< M + +&[before 2] \u24DC << x <<< X << q <<< Q < z +assert: x <<< X << q <<< Q << m <<< \u24DC <<< M < z < n + +&[before 3] \u24DC <<< x <<< X << q <<< Q < z +assert: m <<< x <<< X <<< \u24DC <<< M << q <<< Q < z < n +*/ - for(i = 0; i<4; i++) { - free(test[i]); - } -} #if 0 -static void TestExtremeCompression(void) { - static char *test[4]; - int32_t j = 0, i = 0; - UErrorCode status = U_ZERO_ERROR; - UCollator *coll = ucol_open("en_US", status); - for(i = 0; i<4; i++) { - test[i] = (char *)malloc(2048*sizeof(char)); - } - for(j = 10; j < 2048; j++) { - for(i = 0; i<4; i++) { - uprv_memset(test[i], 'a', (j-2)*sizeof(char)); - test[i][j-1] = (char)('a'+i); - test[i][j] = 0; +/* requires features not yet supported */ +static void TestMoreBefore(void) { + static const struct { + const char* rules; + const char* order[16]; + int32_t size; + } tests[] = { + { "&m < a &[before 1] a < x <<< X << q <<< Q < z", + { "m","M","x","X","q","Q","z","a","n" }, 9}, + { "&m < a &[before 2] a << x <<< X << q <<< Q < z", + { "m","M","x","X","q","Q","a","z","n" }, 9}, + { "&m < a &[before 3] a <<< x <<< X << q <<< Q < z", + { "m","M","x","X","a","q","Q","z","n" }, 9}, + { "&m << a &[before 1] a < x <<< X << q <<< Q < z", + { "x","X","q","Q","z","m","M","a","n" }, 9}, + { "&m << a &[before 2] a << x <<< X << q <<< Q < z", + { "m","M","x","X","q","Q","a","z","n" }, 9}, + { "&m << a &[before 3] a <<< x <<< X << q <<< Q < z", + { "m","M","x","X","a","q","Q","z","n" }, 9}, + { "&m <<< a &[before 1] a < x <<< X << q <<< Q < z", + { "x","X","q","Q","z","n","m","a","M" }, 9}, + { "&m <<< a &[before 2] a << x <<< X << q <<< Q < z", + { "x","X","q","Q","m","a","M","z","n" }, 9}, + { "&m <<< a &[before 3] a <<< x <<< X << q <<< Q < z", + { "m","x","X","a","M","q","Q","z","n" }, 9}, + { "&[before 1] s < x <<< X << q <<< Q < z", + { "r","R","x","X","q","Q","z","s","n" }, 9}, + { "&[before 2] s << x <<< X << q <<< Q < z", + { "r","R","x","X","q","Q","s","z","n" }, 9}, + { "&[before 3] s <<< x <<< X << q <<< Q < z", + { "r","R","x","X","s","q","Q","z","n" }, 9}, + { "&[before 1] \\u24DC < x <<< X << q <<< Q < z", + { "x","X","q","Q","z","n","m","\\u24DC","M" }, 9}, + { "&[before 2] \\u24DC << x <<< X << q <<< Q < z", + { "x","X","q","Q","m","\\u24DC","M","z","n" }, 9}, + { "&[before 3] \\u24DC <<< x <<< X << q <<< Q < z", + { "m","x","X","\\u24DC","M","q","Q","z","n" }, 9} + }; + + int32_t i = 0; + + for(i = 0; i < UPRV_LENGTHOF(tests); i++) { + genericRulesStarter(tests[i].rules, tests[i].order, tests[i].size); } - } - genericLocaleStarter("en_US", (const char **)test, 4); +} +#endif + +static void TestTailorNULL( void ) { + const static char* rule = "&a <<< '\\u0000'"; + UErrorCode status = U_ZERO_ERROR; + UChar rlz[RULE_BUFFER_LEN] = { 0 }; + uint32_t rlen = 0; + UChar a = 1, null = 0; + UCollationResult res = UCOL_EQUAL; + + UCollator *coll = NULL; + + + rlen = u_unescape(rule, rlz, RULE_BUFFER_LEN); + coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status); + + if(U_FAILURE(status)) { + log_err_status(status, "Could not open default collator! -> %s\n", u_errorName(status)); + } else { + res = ucol_strcoll(coll, &a, 1, &null, 1); - for(j = 10; j < 2048; j++) { - for(i = 0; i<1; i++) { - uprv_memset(test[i], 'a', (j-1)*sizeof(char)); - test[i][j] = 0; + if(res != UCOL_LESS) { + log_err("NULL was not tailored properly!\n"); + } } - } - for(i = 0; i<4; i++) { - free(test[i]); - } -} -#endif - -static void TestSurrogates(void) { - static const char *test[] = { - "z","\\ud900\\udc25", "\\ud805\\udc50", - "\\ud800\\udc00y", "\\ud800\\udc00r", - "\\ud800\\udc00f", "\\ud800\\udc00", - "\\ud800\\udc00c", "\\ud800\\udc00b", - "\\ud800\\udc00fa", "\\ud800\\udc00fb", - "\\ud800\\udc00a", - "c", "b" - }; - static const char *rule = - "&z < \\ud900\\udc25 < \\ud805\\udc50" - "< \\ud800\\udc00y < \\ud800\\udc00r" - "< \\ud800\\udc00f << \\ud800\\udc00" - "< \\ud800\\udc00fa << \\ud800\\udc00fb" - "< \\ud800\\udc00a < c < b" ; + ucol_close(coll); +} - genericRulesStarter(rule, test, 14); +static void +TestUpperFirstQuaternary(void) +{ + const char* tests[] = { "B", "b", "Bb", "bB" }; + UColAttribute att[] = { UCOL_STRENGTH, UCOL_CASE_FIRST }; + UColAttributeValue attVals[] = { UCOL_QUATERNARY, UCOL_UPPER_FIRST }; + genericLocaleStarterWithOptions("root", tests, UPRV_LENGTHOF(tests), att, attVals, UPRV_LENGTHOF(att)); } -/* This is a test for prefix implementation, used by JIS X 4061 collation rules */ -static void TestPrefix(void) { - uint32_t i; +static void +TestJ4960(void) +{ + const char* tests[] = { "\\u00e2T", "aT" }; + UColAttribute att[] = { UCOL_STRENGTH, UCOL_CASE_LEVEL }; + UColAttributeValue attVals[] = { UCOL_PRIMARY, UCOL_ON }; + const char* tests2[] = { "a", "A" }; + const char* rule = "&[first tertiary ignorable]=A=a"; + UColAttribute att2[] = { UCOL_CASE_LEVEL }; + UColAttributeValue attVals2[] = { UCOL_ON }; + /* Test whether we correctly ignore primary ignorables on case level when */ + /* we have only primary & case level */ + genericLocaleStarterWithOptionsAndResult("root", tests, UPRV_LENGTHOF(tests), att, attVals, UPRV_LENGTHOF(att), UCOL_EQUAL); + /* Test whether ICU4J will make case level for sortkeys that have primary strength */ + /* and case level */ + genericLocaleStarterWithOptions("root", tests2, UPRV_LENGTHOF(tests2), att, attVals, UPRV_LENGTHOF(att)); + /* Test whether completely ignorable letters have case level info (they shouldn't) */ + genericRulesStarterWithOptionsAndResult(rule, tests2, UPRV_LENGTHOF(tests2), att2, attVals2, UPRV_LENGTHOF(att2), UCOL_EQUAL); +} - static struct { - const char *rules; - const char *data[50]; - const uint32_t len; - } tests[] = { - { "&z <<< z|a", - {"zz", "za"}, 2 }, +static void +TestJ5223(void) +{ + static const char *test = "this is a test string"; + UChar ustr[256]; + int32_t ustr_length = u_unescape(test, ustr, 256); + unsigned char sortkey[256]; + int32_t sortkey_length; + UErrorCode status = U_ZERO_ERROR; + static UCollator *coll = NULL; + coll = ucol_open("root", &status); + if(U_FAILURE(status)) { + log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status)); + return; + } + ucol_setStrength(coll, UCOL_PRIMARY); + ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status); + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); + if (U_FAILURE(status)) { + log_err("Failed setting atributes\n"); + return; + } + sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, NULL, 0); + if (sortkey_length > 256) return; - { "&z <<< z| a", - {"zz", "za"}, 2 }, - { "[strength I]" - "&a=\\ud900\\udc25" - "&z<<<\\ud900\\udc25|a", - {"aa", "az", "\\ud900\\udc25z", "\\ud900\\udc25a", "zz"}, 4 }, - }; + /* we mark the position where the null byte should be written in advance */ + sortkey[sortkey_length-1] = 0xAA; + /* we set the buffer size one byte higher than needed */ + sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey, + sortkey_length+1); - for(i = 0; i<(sizeof(tests)/sizeof(tests[0])); i++) { - genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len); + /* no error occurs (for me) */ + if (sortkey[sortkey_length-1] == 0xAA) { + log_err("Hit bug at first try\n"); } -} -/* This test uses data suplied by Masashiko Maedera to test the implementation */ -/* JIS X 4061 collation order implementation */ -static void TestNewJapanese(void) { + /* we mark the position where the null byte should be written again */ + sortkey[sortkey_length-1] = 0xAA; - static const char *test1[] = { - "\\u30b7\\u30e3\\u30fc\\u30ec", - "\\u30b7\\u30e3\\u30a4", - "\\u30b7\\u30e4\\u30a3", - "\\u30b7\\u30e3\\u30ec", - "\\u3061\\u3087\\u3053", - "\\u3061\\u3088\\u3053", - "\\u30c1\\u30e7\\u30b3\\u30ec\\u30fc\\u30c8", - "\\u3066\\u30fc\\u305f", - "\\u30c6\\u30fc\\u30bf", - "\\u30c6\\u30a7\\u30bf", - "\\u3066\\u3048\\u305f", - "\\u3067\\u30fc\\u305f", - "\\u30c7\\u30fc\\u30bf", - "\\u30c7\\u30a7\\u30bf", - "\\u3067\\u3048\\u305f", - "\\u3066\\u30fc\\u305f\\u30fc", - "\\u30c6\\u30fc\\u30bf\\u30a1", - "\\u30c6\\u30a7\\u30bf\\u30fc", - "\\u3066\\u3047\\u305f\\u3041", - "\\u3066\\u3048\\u305f\\u30fc", - "\\u3067\\u30fc\\u305f\\u30fc", - "\\u30c7\\u30fc\\u30bf\\u30a1", - "\\u3067\\u30a7\\u305f\\u30a1", - "\\u30c7\\u3047\\u30bf\\u3041", - "\\u30c7\\u30a8\\u30bf\\u30a2", - "\\u3072\\u3086", - "\\u3073\\u3085\\u3042", - "\\u3074\\u3085\\u3042", - "\\u3073\\u3085\\u3042\\u30fc", - "\\u30d3\\u30e5\\u30a2\\u30fc", - "\\u3074\\u3085\\u3042\\u30fc", - "\\u30d4\\u30e5\\u30a2\\u30fc", - "\\u30d2\\u30e5\\u30a6", - "\\u30d2\\u30e6\\u30a6", - "\\u30d4\\u30e5\\u30a6\\u30a2", - "\\u3073\\u3085\\u30fc\\u3042\\u30fc", - "\\u30d3\\u30e5\\u30fc\\u30a2\\u30fc", - "\\u30d3\\u30e5\\u30a6\\u30a2\\u30fc", - "\\u3072\\u3085\\u3093", - "\\u3074\\u3085\\u3093", - "\\u3075\\u30fc\\u308a", - "\\u30d5\\u30fc\\u30ea", - "\\u3075\\u3045\\u308a", - "\\u3075\\u30a5\\u308a", - "\\u3075\\u30a5\\u30ea", - "\\u30d5\\u30a6\\u30ea", - "\\u3076\\u30fc\\u308a", - "\\u30d6\\u30fc\\u30ea", - "\\u3076\\u3045\\u308a", - "\\u30d6\\u30a5\\u308a", - "\\u3077\\u3046\\u308a", - "\\u30d7\\u30a6\\u30ea", - "\\u3075\\u30fc\\u308a\\u30fc", - "\\u30d5\\u30a5\\u30ea\\u30fc", - "\\u3075\\u30a5\\u308a\\u30a3", - "\\u30d5\\u3045\\u308a\\u3043", - "\\u30d5\\u30a6\\u30ea\\u30fc", - "\\u3075\\u3046\\u308a\\u3043", - "\\u30d6\\u30a6\\u30ea\\u30a4", - "\\u3077\\u30fc\\u308a\\u30fc", - "\\u3077\\u30a5\\u308a\\u30a4", - "\\u3077\\u3046\\u308a\\u30fc", - "\\u30d7\\u30a6\\u30ea\\u30a4", - "\\u30d5\\u30fd", - "\\u3075\\u309e", - "\\u3076\\u309d", - "\\u3076\\u3075", - "\\u3076\\u30d5", - "\\u30d6\\u3075", - "\\u30d6\\u30d5", - "\\u3076\\u309e", - "\\u3076\\u3077", - "\\u30d6\\u3077", - "\\u3077\\u309d", - "\\u30d7\\u30fd", - "\\u3077\\u3075", -}; + /* this time we set the buffer size to the exact amount needed */ + sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, sortkey, + sortkey_length); - static const char *test2[] = { - "\\u306f\\u309d", /* H\\u309d */ - /*"\\u30cf\\u30fd",*/ /* K\\u30fd */ - "\\u306f\\u306f", /* HH */ - "\\u306f\\u30cf", /* HK */ - "\\u30cf\\u30cf", /* KK */ - "\\u306f\\u309e", /* H\\u309e */ - "\\u30cf\\u30fe", /* K\\u30fe */ - "\\u306f\\u3070", /* HH\\u309b */ - "\\u30cf\\u30d0", /* KK\\u309b */ - "\\u306f\\u3071", /* HH\\u309c */ - "\\u30cf\\u3071", /* KH\\u309c */ - "\\u30cf\\u30d1", /* KK\\u309c */ - "\\u3070\\u309d", /* H\\u309b\\u309d */ - "\\u30d0\\u30fd", /* K\\u309b\\u30fd */ - "\\u3070\\u306f", /* H\\u309bH */ - "\\u30d0\\u30cf", /* K\\u309bK */ - "\\u3070\\u309e", /* H\\u309b\\u309e */ - "\\u30d0\\u30fe", /* K\\u309b\\u30fe */ - "\\u3070\\u3070", /* H\\u309bH\\u309b */ - "\\u30d0\\u3070", /* K\\u309bH\\u309b */ - "\\u30d0\\u30d0", /* K\\u309bK\\u309b */ - "\\u3070\\u3071", /* H\\u309bH\\u309c */ - "\\u30d0\\u30d1", /* K\\u309bK\\u309c */ - "\\u3071\\u309d", /* H\\u309c\\u309d */ - "\\u30d1\\u30fd", /* K\\u309c\\u30fd */ - "\\u3071\\u306f", /* H\\u309cH */ - "\\u30d1\\u30cf", /* K\\u309cK */ - "\\u3071\\u3070", /* H\\u309cH\\u309b */ - "\\u3071\\u30d0", /* H\\u309cK\\u309b */ - "\\u30d1\\u30d0", /* K\\u309cK\\u309b */ - "\\u3071\\u3071", /* H\\u309cH\\u309c */ - "\\u30d1\\u30d1", /* K\\u309cK\\u309c */ - }; - /* - static const char *test3[] = { - "\\u221er\\u221e", - "\\u221eR#", - "\\u221et\\u221e", - "#r\\u221e", - "#R#", - "#t%", - "#T%", - "8t\\u221e", - "8T\\u221e", - "8t#", - "8T#", - "8t%", - "8T%", - "8t8", - "8T8", - "\\u03c9r\\u221e", - "\\u03a9R%", - "rr\\u221e", - "rR\\u221e", - "Rr\\u221e", - "RR\\u221e", - "RT%", - "rt8", - "tr\\u221e", - "tr8", - "TR8", - "tt8", - "\\u30b7\\u30e3\\u30fc\\u30ec", - }; - */ - static const UColAttribute att[] = { UCOL_STRENGTH }; - static const UColAttributeValue val[] = { UCOL_QUATERNARY }; + /* now the trailing null byte is not written */ + if (sortkey[sortkey_length-1] == 0xAA) { + log_err("Hit bug at second try\n"); + } - static const UColAttribute attShifted[] = { UCOL_STRENGTH, UCOL_ALTERNATE_HANDLING}; - static const UColAttributeValue valShifted[] = { UCOL_QUATERNARY, UCOL_SHIFTED }; + ucol_close(coll); +} + +/* Regression test for Thai partial sort key problem */ +static void +TestJ5232(void) +{ + const static char *test[] = { + "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e47\\u0e21", + "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e48\\u0e21" + }; - genericLocaleStarterWithOptions("ja", test1, sizeof(test1)/sizeof(test1[0]), att, val, 1); - genericLocaleStarterWithOptions("ja", test2, sizeof(test2)/sizeof(test2[0]), att, val, 1); - /*genericLocaleStarter("ja", test3, sizeof(test3)/sizeof(test3[0]));*/ - genericLocaleStarterWithOptions("ja", test1, sizeof(test1)/sizeof(test1[0]), attShifted, valShifted, 2); - genericLocaleStarterWithOptions("ja", test2, sizeof(test2)/sizeof(test2[0]), attShifted, valShifted, 2); + genericLocaleStarter("th", test, UPRV_LENGTHOF(test)); } -static void TestStrCollIdenticalPrefix(void) { - const char* rule = "&\\ud9b0\\udc70=\\ud9b0\\udc71"; - const char* test[] = { - "ab\\ud9b0\\udc70", - "ab\\ud9b0\\udc71" - }; - genericRulesTestWithResult(rule, test, sizeof(test)/sizeof(test[0]), UCOL_EQUAL); +static void +TestJ5367(void) +{ + const static char *test[] = { "a", "y" }; + const char* rules = "&Ny << Y &[first secondary ignorable] <<< a"; + genericRulesStarter(rules, test, UPRV_LENGTHOF(test)); } -/* Contractions should have all their canonically equivalent */ -/* strings included */ -static void TestContractionClosure(void) { - static struct { - const char *rules; - const char *data[50]; - const uint32_t len; - } tests[] = { - { "&b=\\u00e4\\u00e4", - { "b", "\\u00e4\\u00e4", "a\\u0308a\\u0308", "\\u00e4a\\u0308", "a\\u0308\\u00e4" }, 5}, - { "&b=\\u00C5", - { "b", "\\u00C5", "A\\u030A", "\\u212B" }, 4}, - }; - uint32_t i; +static void +TestVI5913(void) +{ + UErrorCode status = U_ZERO_ERROR; + int32_t i, j; + UCollator *coll =NULL; + uint8_t resColl[100], expColl[100]; + int32_t rLen, tLen, ruleLen, sLen, kLen; + UChar rule[256]={0x26, 0x62, 0x3c, 0x1FF3, 0}; /* &b<0x1FF3-omega with Ypogegrammeni*/ + UChar rule2[256]={0x26, 0x7a, 0x3c, 0x0161, 0}; /* &z %s\n", u_errorName(status)); + return; + } + log_verbose("\n\nVI collation:"); + if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[2], u_strlen(tData[2])) ) { + log_err("\\u1EAC not equals to \\u1EA0+\\u0302\n"); + } + if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[3], u_strlen(tData[3])) ) { + log_err("\\u1EAC not equals to \\u00c2+\\u0323\n"); + } + if ( !ucol_equal(coll, tData[5], u_strlen(tData[5]), tData[4], u_strlen(tData[4])) ) { + log_err("\\u1ED8 not equals to \\u1ECC+\\u0302\n"); + } + if ( !ucol_equal(coll, tData[7], u_strlen(tData[7]), tData[6], u_strlen(tData[6])) ) { + log_err("\\u1EB7 not equals to \\u1EA1+\\u0306\n"); + } + + for (j=0; j<8; j++) { + tLen = u_strlen(tData[j]); + log_verbose("\n Data :%s \tlen: %d key: ", tData[j], tLen); + rLen = ucol_getSortKey(coll, tData[j], tLen, resColl, 100); + for(i = 0; i %s\n", u_errorName(status)); + return; + } + tLen = u_strlen(tData1[0]); + rLen = ucol_getSortKey(coll, tData1[0], tLen, resColl, 100); + if (rLen != UPRV_LENGTHOF(lastPrimaryIgnCE) || uprv_memcmp(resColl, lastPrimaryIgnCE, rLen) != 0) { + log_err("Bad result for &[lpi]< %s\n", u_errorName(status)); + log_info(" offset=%d \"%s\" | \"%s\"\n", + parseError.offset, aescstrdup(parseError.preContext, -1), aescstrdup(parseError.postContext, -1)); + return; + } + tLen = u_strlen(tData2[0]); + rLen = ucol_getSortKey(coll, tData2[0], tLen, resColl, 100); + if (rLen != UPRV_LENGTHOF(lastSecondaryIgnCE) || uprv_memcmp(resColl, lastSecondaryIgnCE, rLen) != 0) { + log_err("Bad result for &[lsi]<< %s\n", u_errorName(status)); + return; } + for (j=0; j<11; j++) { + tLen = u_strlen(tData1[j]); + rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100); + if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) { + log_err("\n Expecting greater key than previous test case: Data[%d] :%s.", + j, tData1[j]); + } + log_verbose("\n Data[%d] :%s \tlen: %d key: ", j, tData1[j], rLen); + for(i = 0; i0) && (strcmp((char *)resColl, (char *)prevColl)<0)) { + log_err("\n Expecting greater key than previous test case: Data[%d] :%s.", + j, tData1[j]); + } + log_verbose("\n Data[%d] :%s \tlen: %d key: ", j, tData1[j], rLen); + for(i = 0; i0) && (strcmp((char *)resColl, (char *)prevColl)<0)) { + log_err("\n Expecting greater key than previous test case: Data[%d] :%s.", + j, tData1[j]); + } + log_verbose("\n Data[%d] :%s \tlen: %d key: ", j, tData1[j], rLen); + for(i = 0; i0) && (j!=3) && (strcmp((char *)resColl, (char *)prevColl)<0)) { + log_err("\n Expecting greater key than previous test case: Data[%d] :%s.", + j, tData1[j]); + } + if ((j==3)&&(strcmp((char *)resColl, (char *)prevColl)>0)) { + log_err("\n Expecting smaller key than previous test case: Data[%d] :%s.", + j, tData1[j]); + } + log_verbose("\n Data[%d] :%s \tlen: %d key: ", j, tData1[j], rLen); + for(i = 0; i %s\n", u_errorName(status)); + return; + } + ucol_setStrength(coll, UCOL_PRIMARY); + ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status); + ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); + if (U_FAILURE(status)) { + log_err("Failed setting atributes\n"); + return; + } - coll = ucol_openRules(string, uStringLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status); + sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, shortKeyBuf, sizeof(shortKeyBuf)); + if (sortkey_length != 4) { + log_err("expecting length of sortKey is 4 got:%d ", sortkey_length); + } + log_verbose("length of sortKey is %d", sortkey_length); ucol_close(coll); - } +} +#define TSKC_DATA_SIZE 5 +#define TSKC_BUF_SIZE 50 +static void +TestSortKeyConsistency(void) +{ + UErrorCode icuRC = U_ZERO_ERROR; + UCollator* ucol; + UChar data[] = { 0xFFFD, 0x0006, 0x0006, 0x0006, 0xFFFD}; -} + uint8_t bufFull[TSKC_DATA_SIZE][TSKC_BUF_SIZE]; + uint8_t bufPart[TSKC_DATA_SIZE][TSKC_BUF_SIZE]; + int32_t i, j, i2; -/* -[last variable] last variable value -[last primary ignorable] largest CE for primary ignorable -[last secondary ignorable] largest CE for secondary ignorable -[last tertiary ignorable] largest CE for tertiary ignorable -[top] guaranteed to be above all implicit CEs, for now and in the future (in 1.8) -*/ + ucol = ucol_openFromShortString("LEN_S4", FALSE, NULL, &icuRC); + if (U_FAILURE(icuRC)) + { + log_err_status(icuRC, "ucol_openFromShortString failed -> %s\n", u_errorName(icuRC)); + return; + } -static void TestRuleOptions(void) { - /* values here are hardcoded and are correct for the current UCA - * when the UCA changes, one might be forced to change these - * values. (\\u02d0, \\U00010FFFC etc...) - */ - static struct { - const char *rules; - const char *data[50]; - const uint32_t len; - } tests[] = { - /* - all befores here amount to zero */ - { "&[before 3][first tertiary ignorable]<< %s\n", u_errorName(status)); + return; + } - { "&[last variable] s2 -weiv will check - could be a problem with utf-8 iterator -cycheng@ca.ibm.c... but if we use the utf-8 iterator, i.e. s1 = efbfbc62 and s2 = eda08021, we have s1 < s2 -weiv hmmm -cycheng@ca.ibm.c... note that we have a standalone high surrogate -weiv that doesn't sound right -cycheng@ca.ibm.c... we got the same inconsistent results on AIX and Win2000 -weiv so you have two strings, you convert them to utf-8 and to utf-16BE -cycheng@ca.ibm.c... yes -weiv and then do the comparison -cycheng@ca.ibm.c... in one case, the input strings are in utf8, and in the other case the input strings are in utf-16be -weiv utf-16 strings look like a little endian ones in the example you sent me -weiv It could be a bug - let me try to test it out -cycheng@ca.ibm.c... ok -cycheng@ca.ibm.c... we can wait till the conf. call -cycheng@ca.ibm.c... next weke -weiv that would be great -weiv hmmm -weiv I might be wrong -weiv let me play with it some more -cycheng@ca.ibm.c... ok -cycheng@ca.ibm.c... also please check s3 = 0x0e3a0062 and s4 = 0x0e400021. both are in utf-16be -cycheng@ca.ibm.c... seems with icu 2.2 we have s3 > s4, but not in icu 2.4 that's built for db2 -cycheng@ca.ibm.c... also s1 & s2 that I sent you earlier are also in utf-16be -weiv ok -cycheng@ca.ibm.c... i ask sherman to send you more inconsistent data -weiv thanks -cycheng@ca.ibm.c... the 4 strings we sent are just samples -*/ -#if 0 -static void Alexis(void) { - UErrorCode status = U_ZERO_ERROR; - UCollator *coll = ucol_open("", &status); +static int nRangeTestcasesSupplemental = UPRV_LENGTHOF(rangeTestcasesSupplemental); +const static OneTestCase rangeTestcasesQwerty[] = { + { {0x0071}, {0x0077}, UCOL_LESS }, /* "q" < "w" */ + { {0x0077}, {0x0065}, UCOL_LESS }, /* "w" < "e" */ - const char utf16be[2][4] = { - { (char)0xd8, (char)0x00, (char)0x00, (char)0x21 }, - { (char)0xff, (char)0xfc, (char)0x00, (char)0x62 } - }; + { {0x0079}, {0x0075}, UCOL_LESS }, /* "y" < "u" */ + { {0x0071}, {0x0075}, UCOL_LESS }, /* "q" << "u" */ - const char utf8[2][4] = { - { (char)0xed, (char)0xa0, (char)0x80, (char)0x21 }, - { (char)0xef, (char)0xbf, (char)0xbc, (char)0x62 }, - }; + { {0x0074}, {0x0069}, UCOL_LESS }, /* "t" << "i" */ + { {0x006f}, {0x0070}, UCOL_LESS }, /* "o" << "p" */ - UCharIterator iterU161, iterU162; - UCharIterator iterU81, iterU82; + { {0x0079}, {0x0065}, UCOL_LESS }, /* "y" < "e" */ + { {0x0069}, {0x0075}, UCOL_LESS }, /* "i" < "u" */ - UCollationResult resU16, resU8; + { {0x0071, 0x0075, 0x0065, 0x0073, 0x0074}, + {0x0077, 0x0065, 0x0072, 0x0065}, UCOL_LESS }, /* "quest" < "were" */ + { {0x0071, 0x0075, 0x0061, 0x0063, 0x006b}, + {0x0071, 0x0075, 0x0065, 0x0073, 0x0074}, UCOL_LESS }, /* "quack" < "quest" */ +}; - uiter_setUTF16BE(&iterU161, utf16be[0], 4); - uiter_setUTF16BE(&iterU162, utf16be[1], 4); +static int nRangeTestcasesQwerty = UPRV_LENGTHOF(rangeTestcasesQwerty); - uiter_setUTF8(&iterU81, utf8[0], 4); - uiter_setUTF8(&iterU82, utf8[1], 4); +static void TestSameStrengthList(void) +{ + const char* strRules[] = { + /* Normal */ + "&a= 0 && i <= TST_UCOL_MAX_INPUT) { - log_verbose("%08X\t%08X\n", i, uprv_uca_getImplicitFromRaw(i)); + length = ucol_getEquivalentReorderCodes( + USCRIPT_MEROITIC_CURSIVE, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status); + if (U_FAILURE(status) || length != 2) { + log_err("ERROR/Merc: retrieved equivalent script length wrong: " + "expected 2, was = %d\n", length); + } + length = ucol_getEquivalentReorderCodes( + USCRIPT_MEROITIC_HIEROGLYPHS, equivalentScripts, UPRV_LENGTHOF(equivalentScripts), &status); + if (U_FAILURE(status) || length != 2) { + log_err("ERROR/Mero: retrieved equivalent script length wrong: " + "expected 2, was = %d\n", length); } } -static void TestImplicitGeneration(void) { +static void TestReorderingAcrossCloning(void) +{ UErrorCode status = U_ZERO_ERROR; - UChar32 last = 0; - UChar32 current; - UChar32 i = 0, j = 0; - UChar32 roundtrip = 0; - UChar32 lastBottom = 0; - UChar32 currentBottom = 0; - UChar32 lastTop = 0; - UChar32 currentTop = 0; - - UCollator *coll = ucol_open("root", &status); + UCollator *myCollation; + int32_t reorderCodes[3] = {USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION}; + UCollator *clonedCollation; + int32_t retrievedReorderCodesLength; + int32_t retrievedReorderCodes[10]; + int loopIndex; + + log_verbose("Testing non-lead bytes in a sort key with and without reordering\n"); + + /* build collator tertiary */ + myCollation = ucol_open("", &status); + ucol_setStrength(myCollation, UCOL_TERTIARY); if(U_FAILURE(status)) { - log_err("Couldn't open UCA\n"); + log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status)); return; } - uprv_uca_getRawFromImplicit(0xE20303E7); - - for (i = 0; i <= TST_UCOL_MAX_INPUT; ++i) { - current = uprv_uca_getImplicitFromRaw(i) & fourBytes; - - /* check that it round-trips AND that all intervening ones are illegal*/ - roundtrip = uprv_uca_getRawFromImplicit(current); - if (roundtrip != i) { - log_err("No roundtrip %08X\n", i); - } - if (last != 0) { - for (j = last + 1; j < current; ++j) { - roundtrip = uprv_uca_getRawFromImplicit(j); - /* raise an error if it *doesn't* find an error*/ - if (roundtrip != -1) { - log_err("Fails to recognize illegal %08X\n", j); - } - } - } - /* now do other consistency checks*/ - lastBottom = last & bottomByte; - currentBottom = current & bottomByte; - lastTop = last & topByte; - currentTop = current & topByte; - - /* print out some values for spot-checking*/ - if (lastTop != currentTop || i == 0x10000 || i == 0x110000) { - showImplicit(i-3); - showImplicit(i-2); - showImplicit(i-1); - showImplicit(i); - showImplicit(i+1); - showImplicit(i+2); - } - last = current; - - if(uprv_uca_getCodePointFromRaw(uprv_uca_getRawFromCodePoint(i)) != i) { - log_err("No raw <-> code point roundtrip for 0x%08X\n", i); + /* set the reorderding */ + ucol_setReorderCodes(myCollation, reorderCodes, UPRV_LENGTHOF(reorderCodes), &status); + if (U_FAILURE(status)) { + log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status)); + return; + } + + /* clone the collator */ + clonedCollation = ucol_safeClone(myCollation, NULL, NULL, &status); + if (U_FAILURE(status)) { + log_err_status(status, "ERROR: cloning collator: %s\n", myErrorName(status)); + return; + } + + /* get the reordering */ + retrievedReorderCodesLength = ucol_getReorderCodes(clonedCollation, retrievedReorderCodes, UPRV_LENGTHOF(retrievedReorderCodes), &status); + if (U_FAILURE(status)) { + log_err_status(status, "ERROR: getting reorder codes: %s\n", myErrorName(status)); + return; + } + if (retrievedReorderCodesLength != UPRV_LENGTHOF(reorderCodes)) { + log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, UPRV_LENGTHOF(reorderCodes)); + return; + } + for (loopIndex = 0; loopIndex < retrievedReorderCodesLength; loopIndex++) { + if (retrievedReorderCodes[loopIndex] != reorderCodes[loopIndex]) { + log_err_status(status, "ERROR: retrieved reorder code doesn't match set reorder code at index %d\n", loopIndex); + return; } } - showImplicit(TST_UCOL_MAX_INPUT-2); - showImplicit(TST_UCOL_MAX_INPUT-1); - showImplicit(TST_UCOL_MAX_INPUT); - ucol_close(coll); + + /*uprv_free(buffer);*/ + ucol_close(myCollation); + ucol_close(clonedCollation); } -/** - * Iterate through the given iterator, checking to see that all the strings - * in the expected array are present. - * @param expected array of strings we expect to see, or NULL - * @param expectedCount number of elements of expected, or 0 +/* + * Utility function to test one collation reordering test case set. + * @param testcases Array of test cases. + * @param n_testcases Size of the array testcases. + * @param reorderTokens Array of reordering codes. + * @param reorderTokensLen Size of the array reorderTokens. */ -static int32_t checkUEnumeration(const char* msg, - UEnumeration* iter, - const char** expected, - int32_t expectedCount) { - UErrorCode ec = U_ZERO_ERROR; - int32_t i = 0, n, j, bit; - int32_t seenMask = 0; +static void doTestOneReorderingAPITestCase(const OneTestCase testCases[], uint32_t testCasesLen, const int32_t reorderTokens[], int32_t reorderTokensLen) +{ + uint32_t testCaseNum; + UErrorCode status = U_ZERO_ERROR; + UCollator *myCollation; - U_ASSERT(expectedCount >= 0 && expectedCount < 31); /* [sic] 31 not 32 */ - n = uenum_count(iter, &ec); - if (!assertSuccess("count", &ec)) return -1; - log_verbose("%s = [", msg); - for (;; ++i) { - const char* s = uenum_next(iter, NULL, &ec); - if (!assertSuccess("snext", &ec) || s == NULL) break; - if (i != 0) log_verbose(","); - log_verbose("%s", s); - /* check expected list */ - for (j=0, bit=1; j 128 collation elements */ + 0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20, + 0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20, + 0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20, + 0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20, + 0x42,0xC1,0x43,0xC9,0x44,0xCD,0x46,0xD3,0x47,0xDA,0x48,0xC0,0x4A,0xC8,0x4B,0xCC,0x4C,0xD2,0x4D,0xD9,0x4E,0xC2,0x50,0xCA,0x20 +}; + +/* 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' repeated 12 times */ +static const UChar longUpperStr3[]= { /* 324 chars */ + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20, + 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x20 +}; + +typedef struct { + const UChar * longUpperStrPtr; + int32_t longUpperStrLen; +} LongUpperStrItem; + +/* String pointers must be in reverse collation order of the corresponding strings */ +static const LongUpperStrItem longUpperStrItems[] = { + { longUpperStr1, UPRV_LENGTHOF(longUpperStr1) }, + { longUpperStr2, UPRV_LENGTHOF(longUpperStr2) }, + { longUpperStr3, UPRV_LENGTHOF(longUpperStr3) }, + { NULL, 0 } +}; + +enum { kCollKeyLenMax = 850 }; /* may change with collation changes */ + +/* Text fix for #8445; without fix, could have crash due to stack or heap corruption */ +static void TestCaseLevelBufferOverflow(void) +{ UErrorCode status = U_ZERO_ERROR; - UChar rlz[RULE_BUFFER_LEN] = { 0 }; - uint32_t rlen = 0; - UChar a = 1, null = 0; - UCollationResult res = UCOL_EQUAL; + UCollator * ucol = ucol_open("root", &status); + if ( U_SUCCESS(status) ) { + ucol_setAttribute(ucol, UCOL_CASE_LEVEL, UCOL_ON, &status); + if ( U_SUCCESS(status) ) { + const LongUpperStrItem * itemPtr; + uint8_t sortKeyA[kCollKeyLenMax], sortKeyB[kCollKeyLenMax]; + for ( itemPtr = longUpperStrItems; itemPtr->longUpperStrPtr != NULL; itemPtr++ ) { + int32_t sortKeyLen; + if (itemPtr > longUpperStrItems) { + uprv_strcpy((char *)sortKeyB, (char *)sortKeyA); + } + sortKeyLen = ucol_getSortKey(ucol, itemPtr->longUpperStrPtr, itemPtr->longUpperStrLen, sortKeyA, kCollKeyLenMax); + if (sortKeyLen <= 0 || sortKeyLen > kCollKeyLenMax) { + log_err("ERROR sort key length from ucol_getSortKey is %d\n", sortKeyLen); + break; + } + if ( itemPtr > longUpperStrItems ) { + int compareResult = uprv_strcmp((char *)sortKeyA, (char *)sortKeyB); + if (compareResult >= 0) { + log_err("ERROR in sort key comparison result, expected -1, got %d\n", compareResult); + } + } + } + } else { + log_err_status(status, "ERROR in ucol_setAttribute UCOL_CASE_LEVEL on: %s\n", myErrorName(status)); + } + ucol_close(ucol); + } else { + log_err_status(status, "ERROR in ucol_open for root: %s\n", myErrorName(status)); + } +} - UCollator *coll = NULL; +/* Test for #10595 */ +static const UChar testJapaneseName[] = {0x4F50, 0x3005, 0x6728, 0x002C, 0x6B66, 0}; /* Sa sa Ki, Takeshi */ +#define KEY_PART_SIZE 16 +static void TestNextSortKeyPartJaIdentical(void) +{ + UErrorCode status = U_ZERO_ERROR; + UCollator *coll; + uint8_t keyPart[KEY_PART_SIZE]; + UCharIterator iter; + uint32_t state[2] = {0, 0}; + int32_t keyPartLen; + + coll = ucol_open("ja", &status); + ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_IDENTICAL, &status); + if (U_FAILURE(status)) { + log_err_status(status, "ERROR: in creation of Japanese collator with identical strength: %s\n", myErrorName(status)); + return; + } - rlen = u_unescape(rule, rlz, RULE_BUFFER_LEN); - coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status); - res = ucol_strcoll(coll, &a, 1, &null, 1); - if(res != UCOL_LESS) { - log_err("NULL was not tailored properly!\n"); + uiter_setString(&iter, testJapaneseName, 5); + keyPartLen = KEY_PART_SIZE; + while (keyPartLen == KEY_PART_SIZE) { + keyPartLen = ucol_nextSortKeyPart(coll, &iter, state, keyPart, KEY_PART_SIZE, &status); + if (U_FAILURE(status)) { + log_err_status(status, "ERROR: in iterating next sort key part: %s\n", myErrorName(status)); + break; + } } + ucol_close(coll); } @@ -4639,6 +5888,7 @@ void addMiscCollTest(TestNode** root) TEST(TestExtremeCompression); TEST(TestSurrogates); TEST(TestVariableTopSetting); + TEST(TestMaxVariable); TEST(TestBocsuCoverage); TEST(TestCyrillicTailoring); TEST(TestCase); @@ -4646,19 +5896,14 @@ void addMiscCollTest(TestNode** root) TEST(BlackBirdTest); TEST(FunkyATest); TEST(BillFairmanTest); - TEST(RamsRulesTest); - TEST(IsTailoredTest); - TEST(TestCollations); TEST(TestChMove); TEST(TestImplicitTailoring); TEST(TestFCDProblem); TEST(TestEmptyRule); /*TEST(TestJ784);*/ /* 'zh' locale has changed - now it is getting tested by TestBeforePinyin */ TEST(TestJ815); - /*TEST(TestJ831);*/ /* we changed lv locale */ + TEST(TestUpperCaseFirst); TEST(TestBefore); - TEST(TestRedundantRules); - TEST(TestExpansionSyntax); TEST(TestHangulTailoring); TEST(TestUCARules); TEST(TestIncrementalNormalize); @@ -4680,13 +5925,58 @@ void addMiscCollTest(TestNode** root) TEST(TestNumericCollation); TEST(TestTibetanConformance); TEST(TestPinyinProblem); - TEST(TestImplicitGeneration); TEST(TestSeparateTrees); TEST(TestBeforePinyin); TEST(TestBeforeTightening); /*TEST(TestMoreBefore);*/ TEST(TestTailorNULL); + TEST(TestUpperFirstQuaternary); + TEST(TestJ4960); + TEST(TestJ5223); + TEST(TestJ5232); + TEST(TestJ5367); + TEST(TestHiragana); + TEST(TestSortKeyConsistency); + TEST(TestVI5913); /* VI, RO tailored rules */ + TEST(TestCroatianSortKey); + TEST(TestTailor6179); + TEST(TestUCAPrecontext); + TEST(TestOutOfBuffer5468); + TEST(TestSameStrengthList); + + TEST(TestSameStrengthListQuoted); + TEST(TestSameStrengthListSupplemental); + TEST(TestSameStrengthListQwerty); + TEST(TestSameStrengthListQuotedQwerty); + TEST(TestSameStrengthListRanges); + TEST(TestSameStrengthListSupplementalRanges); + TEST(TestSpecialCharacters); + TEST(TestPrivateUseCharacters); + TEST(TestPrivateUseCharactersInList); + TEST(TestPrivateUseCharactersInRange); + TEST(TestInvalidListsAndRanges); + TEST(TestImportRulesDeWithPhonebook); + /* TEST(TestImportRulesFiWithEor); EOR rules removed from CLDR 21 */ + /* TEST(TestImportRulesCJKWithUnihan); */ + TEST(TestImport); + TEST(TestImportWithType); + + TEST(TestBeforeRuleWithScriptReordering); + TEST(TestNonLeadBytesDuringCollationReordering); + TEST(TestReorderingAPI); + TEST(TestReorderingAPIWithRuleCreatedCollator); + TEST(TestEquivalentReorderingScripts); + TEST(TestGreekFirstReorder); + TEST(TestGreekLastReorder); + TEST(TestNonScriptReorder); + TEST(TestHaniReorder); + TEST(TestHaniReorderWithOtherRules); + TEST(TestMultipleReorder); + TEST(TestReorderingAcrossCloning); + TEST(TestReorderWithNumericCollation); + + TEST(TestCaseLevelBufferOverflow); + TEST(TestNextSortKeyPartJaIdentical); } #endif /* #if !UCONFIG_NO_COLLATION */ -