]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/srchtest.cpp
ICU-531.31.tar.gz
[apple/icu.git] / icuSources / test / intltest / srchtest.cpp
index f768c0d2f8e581f1fc45ef560183b13e039d1ff2..a748ac707465a30a1b540022caa56b0a569c4005 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *****************************************************************************
-* Copyright (C) 2001-2006, International Business Machines orporation  
+* Copyright (C) 2001-2011, International Business Machines orporation  
 * and others. All Rights Reserved.
 ****************************************************************************/
 
@@ -9,7 +9,9 @@
 #if !UCONFIG_NO_COLLATION
 
 #include "srchtest.h"
+#if !UCONFIG_NO_BREAK_ITERATION
 #include "../cintltst/usrchdat.c"
+#endif
 #include "unicode/stsearch.h"
 #include "unicode/ustring.h"
 #include "unicode/schriter.h"
@@ -25,7 +27,7 @@
             logln(#test "---");       \
             logln((UnicodeString)""); \
             if(areBroken) {           \
-                  errln(__FILE__ " cannot test - failed to create collator.");  \
+                  dataerrln(__FILE__ " cannot test - failed to create collator.");  \
             } else {                  \
                 test();               \
             }                         \
 
 // public contructors and destructors --------------------------------------
 
-StringSearchTest::StringSearchTest() :
+StringSearchTest::StringSearchTest() 
+#if !UCONFIG_NO_BREAK_ITERATION
+:
     m_en_wordbreaker_(NULL), m_en_characterbreaker_(NULL)
+#endif
 {
+#if !UCONFIG_NO_BREAK_ITERATION
     UErrorCode    status = U_ZERO_ERROR;
     
     m_en_us_ = (RuleBasedCollator *)Collator::createInstance("en_US", status);
@@ -79,10 +85,12 @@ StringSearchTest::StringSearchTest() :
     m_en_characterbreaker_ = BreakIterator::createCharacterInstance(
                                                     Locale::getEnglish(), status);
 #endif
+#endif
 }
 
 StringSearchTest::~StringSearchTest() 
 {
+#if !UCONFIG_NO_BREAK_ITERATION
     delete m_en_us_;
     delete m_fr_fr_;
     delete m_de_;
@@ -91,6 +99,7 @@ StringSearchTest::~StringSearchTest()
     delete m_en_wordbreaker_;
     delete m_en_characterbreaker_;
 #endif
+#endif
 }
 
 // public methods ----------------------------------------------------------
@@ -98,6 +107,7 @@ StringSearchTest::~StringSearchTest()
 void StringSearchTest::runIndexedTest(int32_t index, UBool exec, 
                                       const char* &name, char* ) 
 {
+#if !UCONFIG_NO_BREAK_ITERATION
     UBool areBroken = FALSE;
     if (m_en_us_ == NULL && m_fr_fr_ == NULL && m_de_ == NULL &&
         m_es_ == NULL && m_en_wordbreaker_ == NULL &&
@@ -106,7 +116,9 @@ void StringSearchTest::runIndexedTest(int32_t index, UBool exec,
     }
 
     switch (index) {
+#if !UCONFIG_NO_FILE_IO
         CASE(0, TestOpenClose)
+#endif
         CASE(1, TestInitialization)
         CASE(2, TestBasic)
         CASE(3, TestNormExact)
@@ -154,10 +166,15 @@ void StringSearchTest::runIndexedTest(int32_t index, UBool exec,
         CASE(33, TestUClassID)
         CASE(34, TestSubclass)
         CASE(35, TestCoverage)
+        CASE(36, TestDiacriticMatch)
         default: name = ""; break;
     }
+#else
+    name="";
+#endif
 }
 
+#if !UCONFIG_NO_BREAK_ITERATION
 // private methods ------------------------------------------------------
 
 RuleBasedCollator * StringSearchTest::getCollator(const char *collator)
@@ -237,18 +254,26 @@ Collator::ECollationStrength StringSearchTest::getECollationStrength(
 UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
                                                     const SearchData *search)
 {
-    int           count       = 0;
-    UErrorCode    status      = U_ZERO_ERROR;
-    int32_t   matchindex  = search->offset[count];
-    UnicodeString matchtext;
-    
+    int32_t         count       = 0;
+    UErrorCode      status      = U_ZERO_ERROR;
+    int32_t         matchindex  = search->offset[count];
+    UnicodeString   matchtext;
+    int32_t         matchlength;
+
+    strsrch->setAttribute(USEARCH_ELEMENT_COMPARISON, search->elemCompare, status);
+    if (U_FAILURE(status)) {
+        errln("Error setting USEARCH_ELEMENT_COMPARISON attribute %s", u_errorName(status));
+        return FALSE;
+    }   
+
     if (strsrch->getMatchedStart() != USEARCH_DONE ||
         strsrch->getMatchedLength() != 0) {
         errln("Error with the initialization of match start and length");
     }
-    // start of following matches 
+
+    // start of next matches
     while (U_SUCCESS(status) && matchindex >= 0) {
-        int32_t matchlength = search->size[count];
+        matchlength = search->size[count];
         strsrch->next(status);
         if (matchindex != strsrch->getMatchedStart() || 
             matchlength != strsrch->getMatchedLength()) {
@@ -256,8 +281,9 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
             errln("Text: %s", str);
             str = toCharString(strsrch->getPattern());
             errln("Pattern: %s", str);
-            errln("Error following match found at %d %d", 
-                    strsrch->getMatchedStart(), strsrch->getMatchedLength());
+            errln("Error next match found at %d (len:%d); expected %d (len:%d)", 
+                    strsrch->getMatchedStart(), strsrch->getMatchedLength(),
+                    matchindex, matchlength);
             return FALSE;
         }
         count ++;
@@ -269,7 +295,7 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
                                               matchindex + matchlength,
                                               matchtext, 0, 
                                               matchtext.length())) {
-            errln("Error getting following matched text");
+            errln("Error getting next matched text");
         }
 
         matchindex = search->offset[count];
@@ -281,15 +307,16 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
             errln("Text: %s", str);
             str = toCharString(strsrch->getPattern());
             errln("Pattern: %s", str);
-            errln("Error following match found at %d %d", 
+            errln("Error next match found at %d (len:%d); expected <NO MATCH>", 
                     strsrch->getMatchedStart(), strsrch->getMatchedLength());
             return FALSE;
     }
-    // start of preceding matches 
+
+    // start of previous matches
     count = count == 0 ? 0 : count - 1;
     matchindex = search->offset[count];
     while (U_SUCCESS(status) && matchindex >= 0) {
-        int32_t matchlength = search->size[count];
+        matchlength = search->size[count];
         strsrch->previous(status);
         if (matchindex != strsrch->getMatchedStart() || 
             matchlength != strsrch->getMatchedLength()) {
@@ -297,8 +324,9 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
             errln("Text: %s", str);
             str = toCharString(strsrch->getPattern());
             errln("Pattern: %s", str);
-            errln("Error following match found at %d %d", 
-                    strsrch->getMatchedStart(), strsrch->getMatchedLength());
+            errln("Error previous match found at %d (len:%d); expected %d (len:%d)",
+                    strsrch->getMatchedStart(), strsrch->getMatchedLength(),
+                    matchindex, matchlength);
             return FALSE;
         }
         
@@ -309,7 +337,7 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
                                               matchindex + matchlength,
                                               matchtext, 0, 
                                               matchtext.length())) {
-            errln("Error getting following matched text");
+            errln("Error getting previous matched text");
         }
 
         matchindex = count > 0 ? search->offset[count - 1] : -1;
@@ -319,13 +347,113 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
     if (strsrch->getMatchedStart() != USEARCH_DONE ||
         strsrch->getMatchedLength() != 0) {
         char *str = toCharString(strsrch->getText());
-            errln("Text: %s", str);
+        errln("Text: %s", str);
+        str = toCharString(strsrch->getPattern());
+        errln("Pattern: %s", str);
+        errln("Error previous match found at %d (len:%d); expected <NO MATCH>", 
+                strsrch->getMatchedStart(), strsrch->getMatchedLength());
+        return FALSE;
+    }
+
+    int32_t nextStart;
+    UBool isOverlap = (strsrch->getAttribute(USEARCH_OVERLAP) == USEARCH_ON);
+
+    // start of following matches
+    count = 0;
+    matchindex = search->offset[count];
+    nextStart = 0;
+
+    while (TRUE) {
+        strsrch->following(nextStart, status);
+
+        if (matchindex < 0) {
+            if (strsrch->getMatchedStart() != USEARCH_DONE ||
+                    strsrch->getMatchedLength() != 0) {
+                char *str = toCharString(strsrch->getText());
+                errln("Text: %s", str);
+                str = toCharString(strsrch->getPattern());
+                errln("Pattern: %s", str);
+                errln("Error following match starting at %d (overlap:%d) found at %d (len:%d); expected <NO MATCH>",
+                        nextStart, isOverlap,
+                        strsrch->getMatchedStart(), strsrch->getMatchedLength());
+                return FALSE;
+            }
+            // no more matches
+            break;
+        }
+
+        matchlength = search->size[count];
+        if (strsrch->getMatchedStart() != matchindex
+                || strsrch->getMatchedLength() != matchlength
+                || U_FAILURE(status)) {
+            char *str = toCharString(strsrch->getText());
+            errln("Text: %s\n", str);
             str = toCharString(strsrch->getPattern());
-            errln("Pattern: %s", str);
-            errln("Error following match found at %d %d", 
-                    strsrch->getMatchedStart(), strsrch->getMatchedLength());
+            errln("Pattern: %s\n", str);
+            errln("Error following match starting at %d (overlap: %d) found at %d (len:%d); expected %d (len:%d)\n",
+                        nextStart, isOverlap,
+                        strsrch->getMatchedStart(), strsrch->getMatchedLength(),
+                        matchindex, matchlength);
             return FALSE;
+        }
+
+        if (isOverlap || strsrch->getMatchedLength() == 0) {
+            nextStart = strsrch->getMatchedStart() + 1;
+        } else {
+            nextStart = strsrch->getMatchedStart() + strsrch->getMatchedLength();
+        }
+
+        count++;
+        matchindex = search->offset[count];
+    }
+
+    // start preceding matches
+    count = -1; // last non-negative offset index, could be -1 if no match
+    while (search->offset[count + 1] >= 0) {
+        count++;
     }
+    nextStart = strsrch->getText().length();
+
+    while (TRUE) {
+        strsrch->preceding(nextStart, status);
+
+        if (count < 0) {
+            if (strsrch->getMatchedStart() != USEARCH_DONE || strsrch->getMatchedLength() != 0) {
+                char *str = toCharString(strsrch->getText());
+                errln("Text: %s\n", str);
+                str = toCharString(strsrch->getPattern());
+                errln("Pattern: %s\n", str);
+                errln("Error preceding match starting at %d (overlap: %d) found at %d (len:%d); expected <NO MATCH>\n",
+                            nextStart, isOverlap,
+                            strsrch->getMatchedStart(), 
+                            strsrch->getMatchedLength());
+                return FALSE;
+            }
+            // no more matches
+            break;
+        }
+
+        matchindex = search->offset[count];
+        matchlength = search->size[count];
+        if (strsrch->getMatchedStart() != matchindex
+                || strsrch->getMatchedLength() != matchlength
+                || U_FAILURE(status)) {
+            char *str = toCharString(strsrch->getText());
+            errln("Text: %s\n", str);
+            str = toCharString(strsrch->getPattern());
+            errln("Pattern: %s\n", str);
+            errln("Error preceding match starting at %d (overlap: %d) found at %d (len:%d); expected %d (len:%d)\n",
+                        nextStart, isOverlap,
+                        strsrch->getMatchedStart(), strsrch->getMatchedLength(),
+                        matchindex, matchlength);
+            return FALSE;
+        }
+
+        nextStart = matchindex;
+        count--;
+    }
+
+    strsrch->setAttribute(USEARCH_ELEMENT_COMPARISON, USEARCH_STANDARD_ELEMENT_COMPARISON, status);
     return TRUE;
 }
     
@@ -374,7 +502,7 @@ UBool StringSearchTest::assertEqual(const SearchData *search)
     if( strsrch2 == strsrch || *strsrch2 != *strsrch ||
         !assertEqualWithStringSearch(strsrch2, search)
     ) {
-        errln("failure with StringSearch.clone()");
+        infoln("failure with StringSearch.clone()");
         collator->setStrength(getECollationStrength(UCOL_TERTIARY));
         delete strsrch;
         delete strsrch2;
@@ -394,6 +522,7 @@ UBool StringSearchTest::assertCanonicalEqual(const SearchData *search)
     BreakIterator *breaker  = getBreakIterator(search->breaker);
     StringSearch  *strsrch; 
     UChar          temp[128];
+    UBool          result = TRUE;
     
 #if UCONFIG_NO_BREAK_ITERATION
     if(search->breaker) {
@@ -414,22 +543,27 @@ UBool StringSearchTest::assertCanonicalEqual(const SearchData *search)
     }
 #endif
     collator->setStrength(getECollationStrength(search->strength));
+    collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
     strsrch = new StringSearch(pattern, text, (RuleBasedCollator *)collator, 
                                breaker, status);
     strsrch->setAttribute(USEARCH_CANONICAL_MATCH, USEARCH_ON, status);
     if (U_FAILURE(status)) {
         errln("Error opening string search %s", u_errorName(status));
-        return FALSE;
+        result = FALSE;
+        goto bail;
     }   
     
     if (!assertEqualWithStringSearch(strsrch, search)) {
-        collator->setStrength(getECollationStrength(UCOL_TERTIARY));
-        delete strsrch;
-        return FALSE;
+        result = FALSE;
+        goto bail;
     }
+
+bail:
     collator->setStrength(getECollationStrength(UCOL_TERTIARY));
+    collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, status);
     delete strsrch;
-    return TRUE;
+
+    return result;
 }
    
 UBool StringSearchTest::assertEqualWithAttribute(const SearchData *search, 
@@ -680,7 +814,7 @@ void StringSearchTest::TestBasic()
     while (BASIC[count].text != NULL) {
         //printf("count %d", count);
         if (!assertEqual(&BASIC[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
@@ -697,14 +831,14 @@ void StringSearchTest::TestNormExact()
     }
     while (BASIC[count].text != NULL) {
         if (!assertEqual(&BASIC[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
     count = 0;
     while (NORMEXACT[count].text != NULL) {
         if (!assertEqual(&NORMEXACT[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
@@ -712,7 +846,7 @@ void StringSearchTest::TestNormExact()
     count = 0;
     while (NONNORMEXACT[count].text != NULL) {
         if (!assertEqual(&NONNORMEXACT[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
@@ -723,7 +857,7 @@ void StringSearchTest::TestStrength()
     int count = 0;
     while (STRENGTH[count].text != NULL) {
         if (!assertEqual(&STRENGTH[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
@@ -809,7 +943,7 @@ void StringSearchTest::TestBreakIterator()
         }
         strsrch->reset();
         if (!assertEqualWithStringSearch(strsrch, search)) {
-             errln("Error at test number %d", count);
+             infoln("Error at test number %d", count);
         }
         delete strsrch;
         count += 2;
@@ -817,7 +951,7 @@ void StringSearchTest::TestBreakIterator()
     count = 0;
     while (BREAKITERATOREXACT[count].text != NULL) {
          if (!assertEqual(&BREAKITERATOREXACT[count])) {
-             errln("Error at test number %d", count);
+             infoln("Error at test number %d", count);
          }
          count ++;
     }
@@ -837,7 +971,7 @@ void StringSearchTest::TestVariable()
     while (VARIABLE[count].text != NULL) {
         logln("variable %d", count);
         if (!assertEqual(&VARIABLE[count])) {
-            errln("Error at test number %d", count);
+            infoln("Error at test number %d", count);
         }
         count ++;
     }
@@ -940,9 +1074,7 @@ void StringSearchTest::TestCollator()
     if (U_FAILURE(status)) {
         errln("Error opening rule based collator %s", u_errorName(status));
         delete strsrch;
-        if (tailored != NULL) {
-            delete tailored;
-        }
+        delete tailored;
         return;
     }
 
@@ -950,16 +1082,12 @@ void StringSearchTest::TestCollator()
     if (U_FAILURE(status) || (*strsrch->getCollator()) != (*tailored)) {
         errln("Error setting rule based collator");
         delete strsrch;
-        if (tailored != NULL) {
-            delete tailored;
-        }
+        delete tailored;
     }
     strsrch->reset();
     if (!assertEqualWithStringSearch(strsrch, &COLLATOR[1])) {
         delete strsrch;
-        if (tailored != NULL) {
-            delete tailored;
-        }
+        delete tailored;
         return;
     }
         
@@ -968,17 +1096,13 @@ void StringSearchTest::TestCollator()
     if (U_FAILURE(status) || (*strsrch->getCollator()) != (*m_en_us_)) {
         errln("Error setting rule based collator");
         delete strsrch;
-        if (tailored != NULL) {
-            delete tailored;
-        }
+        delete tailored;
     }
     if (!assertEqualWithStringSearch(strsrch, &COLLATOR[0])) {
        errln("Error searching collator test");
     }
     delete strsrch;
-    if (tailored != NULL) {
-        delete tailored;
-    }
+    delete tailored;
 }
 
 void StringSearchTest::TestPattern()
@@ -1544,6 +1668,43 @@ void StringSearchTest::TestIgnorable()
     delete strsrch;
     delete collator;
 }
+
+void StringSearchTest::TestDiacriticMatch()
+{
+       UChar temp[128];
+    UErrorCode status = U_ZERO_ERROR;
+    int        count  = 0;
+    RuleBasedCollator* coll = NULL;
+    StringSearch *strsrch = NULL;
+
+    UnicodeString pattern("pattern");
+    UnicodeString text("text");
+    
+    const SearchData *search; 
+    
+    search = &(DIACRITICMATCH[count]);
+    while (search->text != NULL) {
+               coll = getCollator(search->collator);
+       coll->setStrength(getECollationStrength(search->strength));
+       strsrch = new StringSearch(pattern, text, coll, getBreakIterator(search->breaker), status);
+       if (U_FAILURE(status)) {
+               errln("Error opening string search %s", u_errorName(status));
+               return;
+           }  
+        u_unescape(search->text, temp, 128);
+        text.setTo(temp, u_strlen(temp));
+        u_unescape(search->pattern, temp, 128);
+        pattern.setTo(temp, u_strlen(temp));
+        strsrch->setText(text, status);
+        strsrch->setPattern(pattern, status);
+        if (!assertEqualWithStringSearch(strsrch, search)) {
+            errln("Error at test number %d", count);
+        }
+        search = &(DIACRITICMATCH[++count]);
+        delete strsrch;
+    }
+    
+}
  
 void StringSearchTest::TestCanonical()
 {
@@ -1780,6 +1941,8 @@ void StringSearchTest::TestCollatorCanonical()
         if (tailored != NULL) {
             delete tailored;
         }
+
+        return;
     }
         
     strsrch->setCollator(m_en_us_, status);
@@ -1942,6 +2105,10 @@ void StringSearchTest::TestGetSetOffsetCanonical()
     UnicodeString  pattern("pattern");
     StringSearch  *strsrch = new StringSearch(pattern, text, m_en_us_, NULL, 
                                               status);
+    Collator *collator = strsrch->getCollator();
+
+    collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
+
     strsrch->setAttribute(USEARCH_CANONICAL_MATCH, USEARCH_ON, status);
     /* testing out of bounds error */
     strsrch->setOffset(-1, status);
@@ -1985,7 +2152,7 @@ void StringSearchTest::TestGetSetOffsetCanonical()
                 errln("Error match found at %d %d", 
                       strsrch->getMatchedStart(), 
                       strsrch->getMatchedLength());
-                return;
+                goto bail;
             }
             matchindex = search.offset[count + 1] == -1 ? -1 : 
                          search.offset[count + 2];
@@ -1993,7 +2160,7 @@ void StringSearchTest::TestGetSetOffsetCanonical()
                 strsrch->setOffset(search.offset[count + 1] + 1, status);
                 if (strsrch->getOffset() != search.offset[count + 1] + 1) {
                     errln("Error setting offset");
-                    return;
+                    goto bail;
                 }
             }
             
@@ -2007,9 +2174,12 @@ void StringSearchTest::TestGetSetOffsetCanonical()
             errln("Pattern: %s", str);
             errln("Error match found at %d %d", strsrch->getMatchedStart(), 
                    strsrch->getMatchedLength());
-            return;
+            goto bail;
         }
     }
+
+bail:
+    collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, status);
     delete strsrch;
 }
     
@@ -2309,4 +2479,6 @@ void StringSearchTest::TestCoverage(){
     }
 }
 
+#endif /* !UCONFIG_NO_BREAK_ITERATION */
+
 #endif /* #if !UCONFIG_NO_COLLATION */