ICU-2546 more tests for mergeSortKeys

X-SVN-Rev: 11868
This commit is contained in:
Vladimir Weinstein 2003-05-09 20:10:44 +00:00
parent 385ad3802c
commit 12929b76f3
4 changed files with 168 additions and 108 deletions

View File

@ -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

View File

@ -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; i<casesSize; i++) {
sortkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
mergedPrefixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
mergedSuffixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
}
unescapedLen = u_unescape(prefix, buffer, 256);
prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
unescapedLen = u_unescape(suffix, buffer, 256);
suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
log_verbose("Massaging data with prefixes and different strengths\n");
strength = UCOL_PRIMARY;
while(strength <= UCOL_IDENTICAL) {
log_verbose("Strength %s\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status);
for(i = 0; i<casesSize; i++) {
unescapedLen = u_unescape(cases[i], buffer, 256);
sortKeysLen[i] = ucol_getSortKey(coll, buffer, unescapedLen, sortkeys[i], 256);
ucol_mergeSortkeys(prefixKey, prefixKeyLen, sortkeys[i], sortKeysLen[i], mergedPrefixkeys[i], 256);
ucol_mergeSortkeys(sortkeys[i], sortKeysLen[i], suffixKey, suffixKeyLen, mergedSuffixkeys[i], 256);
if(i>0) {
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; i<casesSize; i++) {
free(sortkeys[i]);
free(mergedPrefixkeys[i]);
free(mergedSuffixkeys[i]);
}
free(sortkeys);
free(mergedPrefixkeys);
free(mergedSuffixkeys);
free(sortKeysLen);
ucol_close(coll);
/* need to finish this up */
}
#endif /* #if !UCONFIG_NO_COLLATION */

View File

@ -94,6 +94,18 @@
*/
void TestGetTailoredSet(void);
/**
* Test mergeSortKeys
*/
void TestMergeSortKeys(void);
/**
* utility function, defined in cmsccoll.c
*/
void genericLocaleStarter(const char *locale, const char *s[], uint32_t size);
const char * strengthsC[];
#endif /* #if !UCONFIG_NO_COLLATION */
#endif

View File

@ -401,7 +401,7 @@ UColAttributeValue strengths[] = {
UCOL_IDENTICAL
};
static const char * strengthsC[] = {
const char * strengthsC[] = {
"UCOL_PRIMARY",
"UCOL_SECONDARY",
"UCOL_TERTIARY",
@ -1462,7 +1462,7 @@ static void genericOrderingTest(UCollator *coll, const char *s[], uint32_t size)
genericOrderingTestWithResult(coll, s, size, UCOL_LESS);
}
static void genericLocaleStarter(const char *locale, const char *s[], uint32_t size) {
void genericLocaleStarter(const char *locale, const char *s[], uint32_t size) {
UErrorCode status = U_ZERO_ERROR;
UCollator *coll = ucol_open(locale, &status);
@ -3521,110 +3521,6 @@ static void TestPrefixCompose(void) {
}
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));
}
static 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; i<casesSize; i++) {
sortkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
mergedPrefixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
mergedSuffixkeys[i] = (uint8_t *)malloc(256*sizeof(uint8_t));
}
unescapedLen = u_unescape(prefix, buffer, 256);
prefixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, prefixKey, 256);
unescapedLen = u_unescape(suffix, buffer, 256);
suffixKeyLen = ucol_getSortKey(coll, buffer, unescapedLen, suffixKey, 256);
log_verbose("Massaging data with prefixes and different strengths\n");
strength = UCOL_PRIMARY;
while(strength <= UCOL_IDENTICAL) {
log_verbose("Strength %s\n", strengthsC[strength<=UCOL_QUATERNARY?strength:4]);
ucol_setAttribute(coll, UCOL_STRENGTH, strength, &status);
for(i = 0; i<casesSize; i++) {
unescapedLen = u_unescape(cases[i], buffer, 256);
sortKeysLen[i] = ucol_getSortKey(coll, buffer, unescapedLen, sortkeys[i], 256);
ucol_mergeSortkeys(prefixKey, prefixKeyLen, sortkeys[i], sortKeysLen[i], mergedPrefixkeys[i], 256);
ucol_mergeSortkeys(sortkeys[i], sortKeysLen[i], suffixKey, suffixKeyLen, mergedSuffixkeys[i], 256);
if(i>0) {
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<casesSize; i++) {
free(sortkeys[i]);
free(mergedPrefixkeys[i]);
free(mergedSuffixkeys[i]);
}
free(sortkeys);
free(mergedPrefixkeys);
free(mergedSuffixkeys);
free(sortKeysLen);
ucol_close(coll);
/* need to finish this up */
}
/*
[last variable] last variable value
[last primary ignorable] largest CE for primary ignorable
@ -4126,7 +4022,6 @@ void addMiscCollTest(TestNode** root)
addTest(root, &TestRuleOptions, "tscoll/cmsccoll/TestRuleOptions");
addTest(root, &TestBeforePrefixFailure, "tscoll/cmsccoll/TestBeforePrefixFailure");
addTest(root, &TestContractionClosure, "tscoll/cmsccoll/TestContractionClosure");
addTest(root, &TestMergeSortKeys, "tscoll/cmsccoll/TestMergeSortKeys");
addTest(root, &TestPrefixCompose, "tscoll/cmsccoll/TestPrefixCompose");
addTest(root, &TestStrCollIdenticalPrefix, "tscoll/cmsccoll/TestStrCollIdenticalPrefix");
addTest(root, &TestPrefix, "tscoll/cmsccoll/TestPrefix");