]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/srchtest.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / intltest / srchtest.cpp
index 6899f4bb2587687bdfdab6293fc2808538d2b416..22c12906c6eff2ad5faa557bc7ba70f47b923bcc 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *****************************************************************************
-* Copyright (C) 2001-2004, International Business Machines orporation  
+* Copyright (C) 2001-2008, International Business Machines orporation  
 * and others. All Rights Reserved.
 ****************************************************************************/
 
         if (exec) {                   \
             logln(#test "---");       \
             logln((UnicodeString)""); \
-            test();                   \
+            if(areBroken) {           \
+                  errln(__FILE__ " cannot test - failed to create collator.");  \
+            } else {                  \
+                test();               \
+            }                         \
         }                             \
         break;
 
 // 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);
@@ -75,10 +83,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_;
@@ -87,6 +97,7 @@ StringSearchTest::~StringSearchTest()
     delete m_en_wordbreaker_;
     delete m_en_characterbreaker_;
 #endif
+#endif
 }
 
 // public methods ----------------------------------------------------------
@@ -94,12 +105,12 @@ 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 &&
         m_en_characterbreaker_ == NULL && exec) {
-      errln(__FILE__ " cannot test - failed to create collator.");
-      name = "";
-      return;
+        areBroken = TRUE;
     }
 
     switch (index) {
@@ -150,10 +161,16 @@ void StringSearchTest::runIndexedTest(int32_t index, UBool exec,
         CASE(32, TestContractionCanonical)
         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)
@@ -251,8 +268,8 @@ UBool StringSearchTest::assertEqualWithStringSearch(StringSearch *strsrch,
             char *str = toCharString(strsrch->getText());
             errln("Text: %s", str);
             str = toCharString(strsrch->getPattern());
-            errln("Pattern: %s", str);
-            errln("Error following match found at %d %d", 
+            infoln("Pattern: %s", str);
+            infoln("Error following match found at %d %d", 
                     strsrch->getMatchedStart(), strsrch->getMatchedLength());
             return FALSE;
         }
@@ -370,7 +387,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;
@@ -390,6 +407,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) {
@@ -410,22 +428,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, 
@@ -676,7 +699,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 ++;
     }
@@ -693,14 +716,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 ++;
     }
@@ -708,7 +731,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 ++;
     }
@@ -719,7 +742,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 ++;
     }
@@ -805,7 +828,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;
@@ -813,7 +836,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 ++;
     }
@@ -833,7 +856,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 ++;
     }
@@ -1540,6 +1563,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()
 {
@@ -1776,6 +1836,8 @@ void StringSearchTest::TestCollatorCanonical()
         if (tailored != NULL) {
             delete tailored;
         }
+
+        return;
     }
         
     strsrch->setCollator(m_en_us_, status);
@@ -1938,6 +2000,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);
@@ -1981,7 +2047,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];
@@ -1989,7 +2055,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;
                 }
             }
             
@@ -2003,9 +2069,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;
 }
     
@@ -2272,4 +2341,39 @@ void StringSearchTest::TestSubclass()
     }
 }
 
+class StubSearchIterator:public SearchIterator{
+public:
+    StubSearchIterator(){}
+    virtual void setOffset(int32_t , UErrorCode &) {};
+    virtual int32_t getOffset(void) const {return 0;};
+    virtual SearchIterator* safeClone(void) const {return NULL;};
+    virtual int32_t handleNext(int32_t , UErrorCode &){return 0;};
+    virtual int32_t handlePrev(int32_t , UErrorCode &) {return 0;};
+    virtual UClassID getDynamicClassID() const {
+        static char classID = 0;
+        return (UClassID)&classID; 
+    }
+};
+
+void StringSearchTest::TestCoverage(){
+    StubSearchIterator stub1, stub2;
+    UErrorCode status = U_ZERO_ERROR;
+
+    if (stub1 != stub2){
+        errln("new StubSearchIterator should be equal");
+    }
+
+    stub2.setText(UnicodeString("ABC"), status);
+    if (U_FAILURE(status)) {
+        errln("Error: SearchIterator::SetText");
+    }
+
+    stub1 = stub2;
+    if (stub1 != stub2){
+        errln("SearchIterator::operator =  assigned object should be equal");
+    }
+}
+
+#endif /* !UCONFIG_NO_BREAK_ITERATION */
+
 #endif /* #if !UCONFIG_NO_COLLATION */