Added extra tests for code coverage.

X-SVN-Rev: 5738
This commit is contained in:
Syn Wee Quek 2001-09-11 21:40:05 +00:00
parent 27054a379d
commit ecb3821442
6 changed files with 257 additions and 58 deletions

View file

@ -66,15 +66,14 @@ USearchAttributeValue SearchIterator::getAttribute(
USearchAttribute attribute) const
{
switch (attribute) {
case USEARCH_ATTRIBUTE_COUNT :
return USEARCH_DEFAULT;
case USEARCH_OVERLAP :
return (m_search_->isOverlap == TRUE ? USEARCH_ON : USEARCH_OFF);
case USEARCH_CANONICAL_MATCH :
return (m_search_->isCanonicalMatch == TRUE ? USEARCH_ON :
USEARCH_OFF);
default :
return USEARCH_DEFAULT;
}
return USEARCH_DEFAULT;
}
UTextOffset SearchIterator::getMatchedStart() const
@ -234,6 +233,7 @@ UTextOffset SearchIterator::previous(UErrorCode &status)
offset = m_search_->textLength;
m_search_->isForwardSearching = FALSE;
m_search_->reset = FALSE;
setOffset(offset, status);
}
else {
offset = getOffset();

View file

@ -903,7 +903,6 @@ UBool checkNextExactContractionMatch(UStringSearch *strsrch,
uint32_t *patternce = strsrch->pattern.CE;
int32_t patterncelength = strsrch->pattern.CELength;
int32_t count = 0;
int32_t textlength = strsrch->search->textLength;
while (count < patterncelength) {
uint32_t ce = getCE(strsrch, ucol_next(coleiter, status));
if (ce == UCOL_IGNORABLE) {
@ -1232,8 +1231,7 @@ inline uint32_t getPreviousSafeOffset(const UCollator *collator,
* @param coleiter collation element iterator for safe text
*/
inline void cleanUpSafeText(const UStringSearch *strsrch, UChar *safetext,
UChar *safebuffer,
UCollationElements *coleiter)
UChar *safebuffer)
{
if (safetext != safebuffer && safetext != strsrch->canonicalSuffixAccents)
{
@ -1294,10 +1292,10 @@ UTextOffset doNextCanonicalSuffixMatch(UStringSearch *strsrch,
if (textce == UCOL_NULLORDER) {
// check if we have passed the safe buffer
if (coleiter == strsrch->textIter) {
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
return USEARCH_DONE;
}
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
safetext = safebuffer;
coleiter = strsrch->textIter;
ucol_setOffset(coleiter, safeoffset, status);
@ -1310,13 +1308,13 @@ UTextOffset doNextCanonicalSuffixMatch(UStringSearch *strsrch,
UTextOffset failedoffset = getColElemIterOffset(coleiter, FALSE);
if (isSafe && failedoffset >= safelength) {
// alas... no hope. failed at rearranged accent set
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
return USEARCH_DONE;
}
else {
if (isSafe) {
failedoffset += safeoffset;
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
}
// try rearranging the front accents
@ -1337,7 +1335,7 @@ UTextOffset doNextCanonicalSuffixMatch(UStringSearch *strsrch,
UTextOffset result = getColElemIterOffset(coleiter, FALSE);
// sets the text iterator here with the correct expansion and offset
int32_t leftoverces = getExpansionPrefix(coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
if (result >= safelength) {
result = textoffset;
}
@ -1809,13 +1807,12 @@ UTextOffset doPreviousCanonicalSuffixMatch(UStringSearch *strsrch,
unorm_normalize(text + offset, end - offset, UNORM_NFD, 0, accents,
INITIAL_ARRAY_SIZE_, status);
UTextOffset accentsindex[INITIAL_ARRAY_SIZE_];
UTextOffset accentsize = getUnblockedAccentIndex(accents,
UTextOffset accentsindex[INITIAL_ARRAY_SIZE_];
UTextOffset accentsize = getUnblockedAccentIndex(accents,
accentsindex);
UTextOffset count = (2 << (accentsize - 1)) - 2;
UChar buffer[INITIAL_ARRAY_SIZE_];
const UCollator *collator = strsrch->collator;
UCollationElements *coleiter = strsrch->utilIter;
UTextOffset count = (2 << (accentsize - 1)) - 2;
UChar buffer[INITIAL_ARRAY_SIZE_];
UCollationElements *coleiter = strsrch->utilIter;
while (U_SUCCESS(*status) && count > 0) {
UChar *rearrange = strsrch->canonicalSuffixAccents;
// copy the base characters
@ -1914,10 +1911,10 @@ UTextOffset doPreviousCanonicalPrefixMatch(UStringSearch *strsrch,
if (textce == UCOL_NULLORDER) {
// check if we have passed the safe buffer
if (coleiter == strsrch->textIter) {
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
return USEARCH_DONE;
}
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
safetext = safebuffer;
coleiter = strsrch->textIter;
ucol_setOffset(coleiter, safeoffset, status);
@ -1930,13 +1927,13 @@ UTextOffset doPreviousCanonicalPrefixMatch(UStringSearch *strsrch,
UTextOffset failedoffset = ucol_getOffset(coleiter);
if (isSafe && failedoffset <= prefixlength) {
// alas... no hope. failed at rearranged accent set
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
return USEARCH_DONE;
}
else {
if (isSafe) {
failedoffset = safeoffset - failedoffset;
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
}
// try rearranging the end accents
@ -1957,7 +1954,7 @@ UTextOffset doPreviousCanonicalPrefixMatch(UStringSearch *strsrch,
UTextOffset result = ucol_getOffset(coleiter);
// sets the text iterator here with the correct expansion and offset
int32_t leftoverces = getExpansionSuffix(coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer, coleiter);
cleanUpSafeText(strsrch, safetext, safebuffer);
if (result <= prefixlength) {
result = textoffset;
}
@ -2210,24 +2207,20 @@ U_CAPI UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
}
if (U_SUCCESS(*status)) {
UCollator *collator = ucol_open(locale, status);
UStringSearch *result;
UCollator *collator = ucol_open(locale, status);
if (U_SUCCESS(*status)) {
UStringSearch *result = usearch_openFromCollator(pattern,
patternlength, text, textlength,
collator, breakiter, status);
if (U_FAILURE(*status)) {
return NULL;
if (result == NULL || U_FAILURE(*status)) {
ucol_close(collator);
}
else {
result->ownCollator = TRUE;
}
return result;
}
result = usearch_openFromCollator(pattern, patternlength, text,
textlength, collator, breakiter,
status);
if (result == NULL) {
ucol_close(collator);
}
else {
result->ownCollator = TRUE;
}
return result;
}
return NULL;
}
@ -2565,7 +2558,7 @@ U_CAPI void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
int32_t patternlength,
UErrorCode *status)
{
if (pattern == NULL) {
if (strsrch == NULL || pattern == NULL) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
}
if (U_SUCCESS(*status)) {
@ -2746,6 +2739,7 @@ U_CAPI UTextOffset U_EXPORT2 usearch_previous(UStringSearch *strsrch,
offset = search->textLength;
search->isForwardSearching = FALSE;
search->reset = FALSE;
ucol_setOffset(strsrch->textIter, offset, status);
}
else {
offset = usearch_getOffset(strsrch);

View file

@ -197,7 +197,8 @@ static const SearchData SUPPLEMENTARY[] = {
{NULL, NULL, NULL, UCOL_TERTIARY, NULL, {-1}, {0}}
};
static const char *CONTRACTIONRULE = "&z < AB < X\\u0300 < ABC < X\\u0300\\u0315";
static const char *CONTRACTIONRULE =
"&z = ab/c < AB < X\\u0300 < ABC < X\\u0300\\u0315";
static const SearchData CONTRACTION[] = {
/* common discontiguous */
@ -222,6 +223,7 @@ static const SearchData CONTRACTION[] = {
/* blocked discontiguous */
{"X\\u0300\\u031A\\u0315D", "\\u031A\\u0315D", NULL, UCOL_TERTIARY, NULL,
{-1}, {0}},
{"ab", "z", NULL, UCOL_TERTIARY, NULL, {0, -1}, {2}},
{NULL, NULL, NULL, UCOL_TERTIARY, NULL, {-1}, {0}}
};
@ -435,6 +437,7 @@ static const SearchData CONTRACTIONCANONICAL[] = {
/* blocked discontiguous */
{"X\\u0300\\u031A\\u0315D", "\\u031A\\u0315D", NULL, UCOL_TERTIARY, NULL,
{1, -1}, {4}},
{"ab", "z", NULL, UCOL_TERTIARY, NULL, {0, -1}, {2}},
{NULL, NULL, NULL, UCOL_TERTIARY, NULL, {-1}, {0}}
};

View file

@ -284,6 +284,7 @@ static UBool assertEqualWithUStringSearch( UStringSearch *strsrch,
const SearchData search)
{
int count = 0;
int matchlimit = 0;
UErrorCode status = U_ZERO_ERROR;
UTextOffset matchindex = search.offset[count];
int32_t textlength;
@ -334,7 +335,9 @@ static UBool assertEqualWithUStringSearch( UStringSearch *strsrch,
}
/* start of preceding matches */
count = count == 0 ? 0 : count - 1;
matchlimit = count;
matchindex = search.offset[count];
while (U_SUCCESS(status) && matchindex >= 0) {
uint32_t matchlength = search.size[count];
usearch_previous(strsrch, &status);
@ -373,6 +376,7 @@ static UBool assertEqualWithUStringSearch( UStringSearch *strsrch,
usearch_getMatchedLength(strsrch));
return FALSE;
}
return TRUE;
}
@ -539,6 +543,9 @@ static void TestBreakIterator() {
int count = 0;
open();
if (usearch_getBreakIterator(NULL) != NULL) {
log_err("Expected NULL breakiterator from NULL string search\n");
}
u_unescape(BREAKITERATOR[0].text, text, 128);
u_unescape(BREAKITERATOR[0].pattern, pattern, 32);
strsrch = usearch_openFromCollator(pattern, -1, text, -1, EN_US_, NULL,
@ -716,6 +723,9 @@ static void TestCollator()
UStringSearch *strsrch;
open();
if (usearch_getCollator(NULL) != NULL) {
log_err("Expected NULL collator from NULL string search\n");
}
u_unescape(COLLATOR[0].text, text, 128);
u_unescape(COLLATOR[0].pattern, pattern, 32);
@ -772,6 +782,14 @@ static void TestPattern()
UErrorCode status = U_ZERO_ERROR;
open();
if (usearch_getPattern(NULL, &templength) != NULL) {
log_err("Error NULL string search expected returning NULL pattern\n");
}
usearch_setPattern(NULL, pattern, 3, &status);
if (U_SUCCESS(status)) {
log_err("Error expected setting pattern in NULL strings search\n");
}
status = U_ZERO_ERROR;
u_unescape(PATTERN[0].text, text, 128);
u_unescape(PATTERN[0].pattern, pattern, 32);
@ -779,6 +797,17 @@ static void TestPattern()
strsrch = usearch_openFromCollator(pattern, -1, text, -1, EN_US_,
NULL, &status);
status = U_ZERO_ERROR;
usearch_setPattern(strsrch, NULL, 3, &status);
if (U_SUCCESS(status)) {
log_err("Error expected setting NULL pattern in strings search\n");
}
status = U_ZERO_ERROR;
usearch_setPattern(strsrch, pattern, 0, &status);
if (U_SUCCESS(status)) {
log_err("Error expected setting pattern with length 0 in strings search\n");
}
status = U_ZERO_ERROR;
if (U_FAILURE(status)) {
log_err("Error opening string search %s\n", u_errorName(status));
goto ENDTESTPATTERN;
@ -851,6 +880,17 @@ static void TestText()
u_unescape(TEXT[0].pattern, pattern, 32);
open();
if (usearch_getText(NULL, &templength) != NULL) {
log_err("Error NULL string search should return NULL text\n");
}
usearch_setText(NULL, text, 10, &status);
if (U_SUCCESS(status)) {
log_err("Error NULL string search should have an error when setting text\n");
}
status = U_ZERO_ERROR;
strsrch = usearch_openFromCollator(pattern, -1, text, -1, EN_US_,
NULL, &status);
@ -923,6 +963,9 @@ static void TestGetSetOffset()
UStringSearch *strsrch;
open();
if (usearch_getOffset(NULL) != USEARCH_DONE) {
log_err("usearch_getOffset(NULL) expected USEARCH_DONE\n");
}
strsrch = usearch_openFromCollator(pattern, 16, text, 32, EN_US_, NULL,
&status);
/* testing out of bounds error */
@ -960,6 +1003,21 @@ static void TestGetSetOffset()
usearch_getMatchedLength(strsrch));
return;
}
usearch_setOffset(strsrch, matchindex + matchlength, &status);
usearch_previous(strsrch, &status);
if (matchindex != usearch_getMatchedStart(strsrch) ||
matchlength != (uint32_t)usearch_getMatchedLength(strsrch)) {
char *str = toCharString(usearch_getText(strsrch,
&textlength));
log_err("Text: %s\n", str);
str = toCharString(usearch_getPattern(strsrch, &textlength));
log_err("Pattern: %s\n", str);
log_err("Error match found at %d %d\n",
usearch_getMatchedStart(strsrch),
usearch_getMatchedLength(strsrch));
return;
}
usearch_setOffset(strsrch, matchindex + matchlength, &status);
matchindex = search.offset[count + 1] == -1 ? -1 :
search.offset[count + 2];
if (search.offset[count + 1] != -1) {
@ -997,6 +1055,12 @@ static void TestGetSetAttribute()
UStringSearch *strsrch;
open();
if (usearch_getAttribute(NULL, USEARCH_OVERLAP) != USEARCH_DEFAULT ||
usearch_getAttribute(NULL, USEARCH_CANONICAL_MATCH) !=
USEARCH_DEFAULT) {
log_err(
"Attributes for NULL string search should be USEARCH_DEFAULT\n");
}
strsrch = usearch_openFromCollator(pattern, 16, text, 32, EN_US_, NULL,
&status);
if (U_FAILURE(status)) {
@ -1075,6 +1139,13 @@ static void TestGetMatch()
UChar matchtext[128];
open();
if (usearch_getMatchedStart(NULL) != USEARCH_DONE ||
usearch_getMatchedLength(NULL) != USEARCH_DONE) {
log_err(
"Expected start and length of NULL string search should be USEARCH_DONE\n");
}
u_unescape(search.text, text, 128);
u_unescape(search.pattern, pattern, 32);
strsrch = usearch_openFromCollator(pattern, -1, text, -1, EN_US_,
@ -1164,6 +1235,11 @@ static void TestSetMatch()
UChar pattern[32];
UStringSearch *strsrch;
UErrorCode status = U_ZERO_ERROR;
if (usearch_first(NULL, &status) != USEARCH_DONE ||
usearch_last(NULL, &status) != USEARCH_DONE) {
log_err("Error getting the first and last match of a NULL string search\n");
}
u_unescape(search.text, text, 128);
u_unescape(search.pattern, pattern, 32);
strsrch = usearch_openFromCollator(pattern, -1, text, -1, EN_US_,
@ -1226,13 +1302,14 @@ static void TestSetMatch()
static void TestReset()
{
UErrorCode status = U_ZERO_ERROR;
UChar text[128];
UChar pattern[32];
UErrorCode status = U_ZERO_ERROR;
UChar text[] = {0x66, 0x69, 0x73, 0x68, 0x20,
0x66, 0x69, 0x73, 0x68};
UChar pattern[] = {0x73};
UStringSearch *strsrch;
open();
strsrch = usearch_openFromCollator(pattern, 16, text, 32,
strsrch = usearch_openFromCollator(pattern, 1, text, 9,
EN_US_, NULL, &status);
if (U_FAILURE(status)) {
log_err("Error opening string search %s\n", u_errorName(status));
@ -1244,7 +1321,7 @@ static void TestReset()
usearch_setAttribute(strsrch, USEARCH_OVERLAP, USEARCH_ON, &status);
usearch_setAttribute(strsrch, USEARCH_CANONICAL_MATCH, USEARCH_ON,
&status);
usearch_setOffset(strsrch, 10, &status);
usearch_setOffset(strsrch, 9, &status);
if (U_FAILURE(status)) {
log_err("Error setting attributes and offsets\n");
}
@ -1258,6 +1335,11 @@ static void TestReset()
usearch_getMatchedStart(strsrch) != USEARCH_DONE) {
log_err("Error resetting string search\n");
}
usearch_previous(strsrch, &status);
if (usearch_getMatchedStart(strsrch) != 7 ||
usearch_getMatchedLength(strsrch) != 1) {
log_err("Error resetting string search\n");
}
}
usearch_close(strsrch);
close();

View file

@ -113,6 +113,7 @@ void StringSearchTest::runIndexedTest(int32_t index, UBool exec,
CASE(30, TestGetSetOffsetCanonical)
CASE(31, TestSupplementaryCanonical)
CASE(32, TestContractionCanonical)
CASE(33, TestSearchIterator)
default: name = ""; break;
}
}
@ -547,15 +548,9 @@ void StringSearchTest::TestInitialization()
{
errln("Error copying StringSearch");
}
delete result;
delete copy;
/* testing if an extremely large pattern will fail the initialization */
for (int count = 0; count < 512; count ++) {
pattern.append(temp);
}
result = new StringSearch(pattern, text, m_en_us_, NULL, status);
copy = new StringSearch(*result);
copy = (StringSearch *)result->safeClone();
if (*(copy->getCollator()) != *(result->getCollator()) ||
copy->getBreakIterator() != result->getBreakIterator() ||
copy->getMatchedLength() != result->getMatchedLength() ||
@ -563,8 +558,31 @@ void StringSearchTest::TestInitialization()
copy->getOffset() != result->getOffset() ||
copy->getPattern() != result->getPattern() ||
copy->getText() != result->getText() ||
*(copy) != *(result))
{
*(copy) != *(result)) {
errln("Error copying StringSearch");
}
delete result;
/* testing if an extremely large pattern will fail the initialization */
for (int count = 0; count < 512; count ++) {
pattern.append(temp);
}
result = new StringSearch(pattern, text, m_en_us_, NULL, status);
if (*result != *result) {
errln("Error: string search object expected to match itself");
}
if (*result == *copy) {
errln("Error: string search objects are not expected to match");
}
*copy = *result;
if (*(copy->getCollator()) != *(result->getCollator()) ||
copy->getBreakIterator() != result->getBreakIterator() ||
copy->getMatchedLength() != result->getMatchedLength() ||
copy->getMatchedStart() != result->getMatchedStart() ||
copy->getOffset() != result->getOffset() ||
copy->getPattern() != result->getPattern() ||
copy->getText() != result->getText() ||
*(copy) != *(result)) {
errln("Error copying StringSearch");
}
if (U_FAILURE(status)) {
@ -1015,7 +1033,8 @@ void StringSearchTest::TestText()
u_unescape(TEXT[0].text, temp, 128);
text.setTo(temp, u_strlen(temp));
strsrch->setText(text, status);
StringCharacterIterator chariter(text);
strsrch->setText(chariter, status);
if (text != strsrch->getText()) {
errln("Error setting text");
delete strsrch;
@ -1309,8 +1328,8 @@ void StringSearchTest::TestSetMatch()
void StringSearchTest::TestReset()
{
UErrorCode status = U_ZERO_ERROR;
UnicodeString text("text text text");
UnicodeString pattern("pattern");
UnicodeString text("fish fish");
UnicodeString pattern("s");
StringSearch *strsrch = new StringSearch(pattern, text, m_en_us_, NULL,
status);
if (U_FAILURE(status)) {
@ -1322,7 +1341,7 @@ void StringSearchTest::TestReset()
}
strsrch->setAttribute(USEARCH_OVERLAP, USEARCH_ON, status);
strsrch->setAttribute(USEARCH_CANONICAL_MATCH, USEARCH_ON, status);
strsrch->setOffset(10, status);
strsrch->setOffset(9, status);
if (U_FAILURE(status)) {
errln("Error setting attributes and offsets");
}
@ -1334,6 +1353,11 @@ void StringSearchTest::TestReset()
strsrch->getMatchedStart() != USEARCH_DONE) {
errln("Error resetting string search");
}
strsrch->previous(status);
if (strsrch->getMatchedStart() != 7 ||
strsrch->getMatchedLength() != 1) {
errln("Error resetting string search\n");
}
}
delete strsrch;
}
@ -1939,5 +1963,100 @@ void StringSearchTest::TestContractionCanonical()
delete collator;
}
class TempSearch : public SearchIterator
{
public:
TempSearch();
TempSearch(TempSearch &search);
~TempSearch();
void setOffset(UTextOffset position, UErrorCode &status);
UTextOffset getOffset() const;
SearchIterator* safeClone() const;
protected:
UTextOffset handleNext(UTextOffset position, UErrorCode &status);
UTextOffset handlePrev(UTextOffset position, UErrorCode &status);
};
TempSearch::TempSearch() : SearchIterator()
{
}
TempSearch::TempSearch(TempSearch &search) : SearchIterator(search)
{
}
TempSearch::~TempSearch()
{
}
void TempSearch::setOffset(UTextOffset position, UErrorCode &status)
{
}
UTextOffset TempSearch::getOffset() const
{
return USEARCH_DONE;
}
SearchIterator * TempSearch::safeClone() const
{
return NULL;
}
UTextOffset TempSearch::handleNext(UTextOffset position, UErrorCode &status)
{
return USEARCH_DONE;
}
UTextOffset TempSearch::handlePrev(UTextOffset position, UErrorCode &status)
{
return USEARCH_DONE;
}
void StringSearchTest::TestSearchIterator()
{
TempSearch search;
if (search.getBreakIterator() != NULL ||
search.getAttribute(USEARCH_OVERLAP) != USEARCH_OFF ||
search.getAttribute(USEARCH_CANONICAL_MATCH) != USEARCH_OFF ||
search.getMatchedStart() != USEARCH_DONE ||
search.getMatchedLength() != 0 || search.getText().length() != 0) {
errln("Error subclassing SearchIterator, default constructor failed");
return;
}
if (search.getAttribute(USEARCH_ATTRIBUTE_COUNT) != USEARCH_DEFAULT) {
errln("Error getting illegal attribute failed");
return;
}
UnicodeString text("abc");
StringCharacterIterator striter(text);
UErrorCode status = U_ZERO_ERROR;
search.setText(text, status);
TempSearch search2;
search2.setText(striter, status);
if (U_FAILURE(status) || search != search2) {
errln("Error setting text");
return;
}
if (search != search) {
errln("Error: search object has to be equals to itself");
return;
}
TempSearch search3(search);
if (search != search3) {
errln("Error: search object has to be equals to its copy");
return;
}
search.setAttribute(USEARCH_OVERLAP, USEARCH_ON, status);
if (U_FAILURE(status) ||
search.getAttribute(USEARCH_OVERLAP) != USEARCH_ON) {
errln("Error setting overlap attribute");
}
search.reset();
if (search.getAttribute(USEARCH_OVERLAP) != USEARCH_OFF) {
errln("Error resetting search");
}
}

View file

@ -77,6 +77,7 @@ private:
void TestGetSetOffsetCanonical();
void TestSupplementaryCanonical();
void TestContractionCanonical();
void TestSearchIterator();
};
#endif