From 12929b76f32eed09634c8da2dad203e54de18468 Mon Sep 17 00:00:00 2001 From: Vladimir Weinstein Date: Fri, 9 May 2003 20:10:44 +0000 Subject: [PATCH] ICU-2546 more tests for mergeSortKeys X-SVN-Rev: 11868 --- icu4c/source/i18n/unicode/ucol.h | 2 + icu4c/source/test/cintltst/capitst.c | 153 +++++++++++++++++++++++++- icu4c/source/test/cintltst/capitst.h | 12 ++ icu4c/source/test/cintltst/cmsccoll.c | 109 +----------------- 4 files changed, 168 insertions(+), 108 deletions(-) diff --git a/icu4c/source/i18n/unicode/ucol.h b/icu4c/source/i18n/unicode/ucol.h index 3c3a56f648..5108fc05f3 100644 --- a/icu4c/source/i18n/unicode/ucol.h +++ b/icu4c/source/i18n/unicode/ucol.h @@ -614,6 +614,8 @@ ucol_getVersion(const UCollator* coll, UVersionInfo info); * 191B1D 02 1F212301 050505 02 050505 01 910505 02 910505 00 * This allows for concatenating of first and last names for sorting, among other things. * If the destination buffer is not big enough, the results are undefined. + * If any of source lengths are zero or any of source pointers are NULL/undefined, + * result is of size zero. * @param src1 pointer to the first sortkey * @param src1Length length of the first sortkey * @param src2 pointer to the second sortkey diff --git a/icu4c/source/test/cintltst/capitst.c b/icu4c/source/test/cintltst/capitst.c index d4ee2e9a72..9b91fc779b 100644 --- a/icu4c/source/test/cintltst/capitst.c +++ b/icu4c/source/test/cintltst/capitst.c @@ -50,6 +50,7 @@ void addCollAPITest(TestNode** root) addTest(root, &TestSortKeyBufferOverrun, "tscoll/capitst/TestSortKeyBufferOverrun"); addTest(root, &TestAttribute, "tscoll/capitst/TestAttribute"); addTest(root, &TestGetTailoredSet, "tscoll/capitst/TestGetTailoredSet"); + addTest(root, &TestMergeSortKeys, "tscoll/capitst/TestMergeSortKeys"); } @@ -676,7 +677,7 @@ void TestSafeClone() { */ void TestSortKey() { - uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL; + uint8_t *sortk1 = NULL, *sortk2 = NULL, *sortk3 = NULL, *sortkEmpty = NULL; uint8_t sortk2_compat[] = { /* 2.2 key */ 0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01, @@ -811,6 +812,22 @@ void TestSortKey() doAssert( !(memcmp(sortk1, sortk2, sortklen) > 0), "Result should be \"Abcda\" == \"abcda\""); doAssert( !(memcmp(sortk2, sortk1, sortklen) < 0), "Result should be \"abcda\" == \"Abcda\""); doAssert( (memcmp(sortk1, sortk2, sortklen) == 0), "Result should be \"abcda\" == \"abcda\""); + + log_verbose("getting sortkey for an empty string\n"); + ucol_setAttribute(col, UCOL_STRENGTH, UCOL_TERTIARY, &status); + sortklen = ucol_getSortKey(col, test1, 0, NULL, 0); + sortkEmpty = (uint8_t*)malloc(sizeof(uint8_t) * sortklen+1); + sortklen = ucol_getSortKey(col, test1, 0, sortkEmpty, sortklen+1); + if(sortklen != 3 || sortkEmpty[0] != 1 || sortkEmpty[0] != 1 || sortkEmpty[2] != 0) { + log_err("Empty string generated wrong sortkey!\n"); + } + free(sortkEmpty); + + log_verbose("testing passing invalid string\n"); + sortklen = ucol_getSortKey(col, NULL, 0, NULL, 0); + if(sortklen != 0) { + log_err("Invalid string didn't return sortkey size of 0\n"); + } log_verbose("testing sortkey ends...\n"); @@ -1472,4 +1489,138 @@ void TestGetTailoredSet() { } } +static int tMemCmp(const uint8_t *first, const uint8_t *second) { + int32_t firstLen = uprv_strlen((const char *)first); + int32_t secondLen = uprv_strlen((const char *)second); + return uprv_memcmp(first, second, uprv_min(firstLen, secondLen)); +} + +void TestMergeSortKeys(void) { + UErrorCode status = U_ZERO_ERROR; + + const char* cases[] = { + "abc", + "abcd", + "abcde" + }; + uint32_t casesSize = sizeof(cases)/sizeof(cases[0]); + const char* prefix = "foo"; + const char* suffix = "egg"; + char outBuff1[256], outBuff2[256]; + + uint8_t **sortkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *)); + uint8_t **mergedPrefixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *)); + uint8_t **mergedSuffixkeys = (uint8_t **)malloc(casesSize*sizeof(uint8_t *)); + uint32_t *sortKeysLen = (uint32_t *)malloc(casesSize*sizeof(uint32_t)); + uint8_t prefixKey[256], suffixKey[256]; + uint32_t prefixKeyLen = 0, suffixKeyLen = 0, i = 0; + UChar buffer[256]; + uint32_t unescapedLen = 0, l1 = 0, l2 = 0; + UColAttributeValue strength; + + UCollator *coll = ucol_open("en", &status); + log_verbose("ucol_mergeSortkeys test\n"); + log_verbose("Testing order of the test cases\n"); + genericLocaleStarter("en", cases, casesSize); + + for(i = 0; i0) { + if(tMemCmp(mergedPrefixkeys[i-1], mergedPrefixkeys[i]) >= 0) { + log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]); + log_err("%s\n%s\n", + ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, &l1), + ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, &l2)); + } + if(tMemCmp(mergedSuffixkeys[i-1], mergedSuffixkeys[i]) >= 0) { + log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]); + log_err("%s\n%s\n", + ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, &l1), + ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, &l2)); + } + } + } + if(strength == UCOL_QUATERNARY) { + strength = UCOL_IDENTICAL; + } else { + strength++; + } + } + + { + uint8_t smallBuf[3]; + uint32_t reqLen = 0; + log_verbose("testing buffer overflow\n"); + reqLen = ucol_mergeSortkeys(prefixKey, prefixKeyLen, suffixKey, suffixKeyLen, smallBuf, 3); + if(reqLen != (prefixKeyLen+suffixKeyLen-1)) { + log_err("Wrong preflight size for merged sortkey\n"); + } + } + + { + UChar empty = 0; + uint8_t emptyKey[20], abcKey[50], mergedKey[100]; + int32_t emptyKeyLen = 0, abcKeyLen = 0, mergedKeyLen = 0; + + log_verbose("testing merging with sortkeys generated for empty strings\n"); + emptyKeyLen = ucol_getSortKey(coll, &empty, 0, emptyKey, 20); + unescapedLen = u_unescape(cases[0], buffer, 256); + abcKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, abcKey, 50); + mergedKeyLen = ucol_mergeSortkeys(emptyKey, emptyKeyLen, abcKey, abcKeyLen, mergedKey, 100); + if(mergedKey[0] != 2) { + log_err("Empty sortkey didn't produce a level separator\n"); + } + /* try with zeros */ + mergedKeyLen = ucol_mergeSortkeys(emptyKey, 0, abcKey, abcKeyLen, mergedKey, 100); + if(mergedKeyLen != 0 || mergedKey[0] != 0) { + log_err("Empty key didn't produce null mergedKey\n"); + } + mergedKeyLen = ucol_mergeSortkeys(abcKey, abcKeyLen, emptyKey, 0, mergedKey, 100); + if(mergedKeyLen != 0 || mergedKey[0] != 0) { + log_err("Empty key didn't produce null mergedKey\n"); + } + + + + + + } + + + + + + for(i = 0; i0) { - if(tMemCmp(mergedPrefixkeys[i-1], mergedPrefixkeys[i]) >= 0) { - log_err("Error while comparing prefixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]); - log_err("%s\n%s\n", - ucol_sortKeyToString(coll, mergedPrefixkeys[i-1], outBuff1, &l1), - ucol_sortKeyToString(coll, mergedPrefixkeys[i], outBuff2, &l2)); - } - if(tMemCmp(mergedSuffixkeys[i-1], mergedSuffixkeys[i]) >= 0) { - log_err("Error while comparing suffixed keys @ strength %s:\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]); - log_err("%s\n%s\n", - ucol_sortKeyToString(coll, mergedSuffixkeys[i-1], outBuff1, &l1), - ucol_sortKeyToString(coll, mergedSuffixkeys[i], outBuff2, &l2)); - } - } - } - if(strength == UCOL_QUATERNARY) { - strength = UCOL_IDENTICAL; - } else { - strength++; - } - } - - { - uint8_t smallBuf[3]; - uint32_t reqLen = 0; - log_verbose("testing buffer overflow\n"); - reqLen = ucol_mergeSortkeys(prefixKey, prefixKeyLen, suffixKey, suffixKeyLen, smallBuf, 3); - if(reqLen != (prefixKeyLen+suffixKeyLen-1)) { - log_err("Wrong preflight size for merged sortkey\n"); - } - } - - - for(i = 0; i