]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/tscoll.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / tscoll.cpp
index 9a44b5a053e513d71b36eafe596d590ccd541141..872a78b950431d5ec2b3f5265562ce405963a0ea 100644 (file)
@@ -1,6 +1,6 @@
 /********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2003, International Business Machines Corporation and
+ * Copyright (c) 1997-2011, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
 
 #if !UCONFIG_NO_COLLATION
 
+#include "unicode/localpointer.h"
 #include "unicode/uchar.h"
-
+#include "unicode/ustring.h"
 
 #include "dadrcoll.h"
 
 #include "encoll.h"
 #include "frcoll.h"
 #include "decoll.h"
-#include "dacoll.h"
 #include "escoll.h"
 #include "ficoll.h"
 #include "jacoll.h"
 #include "regcoll.h"
 #include "currcoll.h"
 #include "itercoll.h"
-//#include "capicoll.h"   // CollationCAPITest
 #include "tstnorm.h"
 #include "normconf.h"
 #include "thcoll.h"
 #include "srchtest.h"
+#include "ssearch.h"
 #include "cntabcol.h"
 #include "lcukocol.h"
 #include "ucaconf.h"
 #include "svccoll.h"
+#include "cmemory.h"
+#include "alphaindextst.h"
+//#include "rndmcoll.h"
+
+// Set to 1 to test offsets in backAndForth()
+#define TEST_OFFSETS 0
+
+#define TESTCLASS(n,classname)        \
+    case n:                           \
+        name = #classname;            \
+        if (exec) {                   \
+            logln(#classname "---");  \
+            logln("");                \
+            classname t;              \
+            callTest(t, par);         \
+        }                             \
+        break
 
 void IntlTestCollator::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
 {
-    if (exec)
-    {
+    if (exec) {
         logln("TestSuite Collator: ");
     }
 
-    switch (index)
-    {
-    case 0:
-        name = "CollationEnglishTest";
-
-        if (exec)
-        {
-            logln("CollationEnglishtest---");
-            logln("");
-            CollationEnglishTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 1:
-        name = "CollationFrenchTest";
-        
-        if (exec)
-        {
-            logln("CollationFrenchtest---");
-            logln("");
-            CollationFrenchTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 2:
-        name = "CollationGermanTest";
-        
-        if (exec)
-        {
-            logln("CollationGermantest---");
-            logln("");
-
-            CollationGermanTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 3:
-        name = "CollationDanishTest";
-
-        if (exec)
-        {
-            logln("CollationDanishtest---");
-            logln("");
-            
-            CollationDanishTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 4:
-        name = "CollationSpanishTest";
-        
-        if (exec)
-        {
-            logln("CollationSpanishtest---");
-            logln("");
-
-            CollationSpanishTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 5:
-        name = "CollationFinnishTest"; 
-
-        if (exec)
-        {
-            logln("CollationFinnishtest---");
-            
-            CollationFinnishTest test;
-            callTest( test, par ); 
-        }
-        break;
-
-    case 6:
-        name = "CollationKanaTest"; 
-
-        if (exec)
-        {
-            logln("CollationKanatest---");
-            
-            CollationKanaTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 7:
-        name = "CollationTurkishTest";
-        
-        if (exec)
-        {
-            logln("CollationTurkishtest---");
-            logln("");
-
-            CollationTurkishTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 8:
-        name = "CollationDummyTest"; 
-
-        if (exec)
-        {
-            logln("CollationDummyTest---");
-            
-            CollationDummyTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 9:
-        name = "G7CollationTest";
-        
-        if (exec)
-        {
-            logln("G7CollationTest---");
-            
-            G7CollationTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 10:
-        name = "CollationMonkeyTest";
-        
-        if (exec)
-        {
-            logln("CollationMonkeyTest---");
-            
-            CollationMonkeyTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 11:
-        name = "CollationAPITest";
-        
-        if (exec)
-        {
-            logln("CollationAPITest---");
-            logln("");
-            
-            CollationAPITest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 12:
-        name = "CollationRegressionTest"; 
-
-        if (exec)
-        {
-            logln("CollationRegressionTest---");
-            
-            CollationRegressionTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 13:
-        name = "CollationCurrencyTest"; 
-
-        if (exec)
-        {
-            logln("CollationCurrencyTest---");
-            logln("");
-            
-            CollationCurrencyTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 14:
-        name = "CollationIteratorTest"; 
-
-        if (exec)
-        {
-            logln("CollationIteratorTest---");
-            
-            CollationIteratorTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 15: 
-      /*        name = "CollationCAPITest"; 
-        if (exec) {
-            logln("Collation C API test---"); logln("");
-            CollationCAPITest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 16: */
-        name = "CollationThaiTest"; 
-        if (exec) {
-            logln("CollationThaiTest---"); 
-            
-            CollationThaiTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 16: 
-        name = "LotusCollationTest";
-
-        name = "LotusCollationKoreanTest"; 
-        if (exec) {
-            logln("LotusCollationKoreanTest---"); logln("");
-            LotusCollationKoreanTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 17:
-        name = "StringSearchTest"; 
-        if (exec) {
-            logln("StringSearchTest---"); 
-            
-            StringSearchTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 18: 
-        name = "ContractionTableTest";
-
-        name = "ContractionTableTest"; 
-        if (exec) {
-            logln("ContractionTableTest---"); logln("");
-            ContractionTableTest test;
-            callTest( test, par );
-        }
-        break;
-
-    case 19:
-      name = "DataDrivenTest";
-      if (exec) {
-        logln("DataDrivenTest---"); logln("");
-        DataDrivenCollatorTest test;
-        callTest( test, par );
-      }
-      break;
-
-    case 20:
-      name = "UCAConformanceTest";
-      if (exec) {
-        logln("UCAConformanceTest---"); logln("");
-        UCAConformanceTest test;
-        callTest( test, par );
-      }
-      break;
-
-    case 21:
-      name = "CollationServiceTest";
-      if (exec) {
-        logln("CollationServiceTest---"); logln("");
-        CollationServiceTest test;
-        callTest( test, par );
-      }
-      break;
-
-    default: name = ""; break;
+    switch (index) {
+      TESTCLASS(0, CollationEnglishTest);
+      TESTCLASS(1, CollationFrenchTest);
+      TESTCLASS(2, CollationGermanTest);
+      TESTCLASS(3, CollationSpanishTest);
+      TESTCLASS(4, CollationKanaTest);
+      TESTCLASS(5, CollationTurkishTest);
+      TESTCLASS(6, CollationDummyTest);
+      TESTCLASS(7, G7CollationTest);
+      TESTCLASS(8, CollationMonkeyTest);
+      TESTCLASS(9, CollationAPITest);
+      TESTCLASS(10, CollationRegressionTest);
+      TESTCLASS(11, CollationCurrencyTest);
+      TESTCLASS(12, CollationIteratorTest);
+      TESTCLASS(13, CollationThaiTest);
+      TESTCLASS(14, LotusCollationKoreanTest);
+      TESTCLASS(15, StringSearchTest);
+      TESTCLASS(16, ContractionTableTest);
+#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
+      TESTCLASS(17, DataDrivenCollatorTest);
+#endif
+      TESTCLASS(18, UCAConformanceTest);
+      TESTCLASS(19, CollationServiceTest);
+      TESTCLASS(20, CollationFinnishTest); // removed by weiv - we have changed Finnish collation
+      //TESTCLASS(21, RandomCollatorTest); // See ticket 5747 about reenabling this test.
+      TESTCLASS(21, SSearchTest);
+#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
+      TESTCLASS(22, AlphabeticIndexTest);
+#endif
+
+      default: name = ""; break;
     }
 }
 
@@ -386,7 +166,7 @@ IntlTestCollator::doTestVariant(Collator* col, const UnicodeString &source, cons
     const UChar* trg = target.getBuffer();
     UCollationResult compareResultIter = (UCollationResult)result;
 
-    if(!quick) {
+    {
       UCharIterator sIter, tIter;
       uiter_setString(&sIter, src, sLen);
       uiter_setString(&tIter, trg, tLen);
@@ -434,7 +214,7 @@ IntlTestCollator::doTestVariant(Collator* col, const UnicodeString &source, cons
     }
 
     /* testing the partial sortkeys */
-    if(1) { /*!QUICK*/
+    { /*!QUICK*/
       int32_t partialSizes[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
       int32_t partialSizesSize = 1;
       if(!quick) {
@@ -490,13 +270,12 @@ IntlTestCollator::doTest(Collator* col, const UnicodeString &source, const Unico
     }
 
     UErrorCode status = U_ZERO_ERROR;
-    CollationElementIterator* c = ((RuleBasedCollator *)col)->createCollationElementIterator( source );
+    LocalPointer<CollationElementIterator> c(((RuleBasedCollator *)col)->createCollationElementIterator(source));
     logln("Testing iterating source: "+source);
     backAndForth(*c);
     c->setText(target, status);
     logln("Testing iterating target: "+target);
     backAndForth(*c);
-    delete c;
   }
 }
 
@@ -611,8 +390,10 @@ UnicodeString &IntlTestCollator::prettify(const CollationKey &source, UnicodeStr
 
     for (i = 0; i < byteCount; i += 1)
     {
+        if (i != 0) {
+            target += " ";
+        }
         appendHex(bytes[i], 2, target);
-        target += " ";
     }
 
     target += "]";
@@ -624,7 +405,7 @@ void IntlTestCollator::backAndForth(CollationElementIterator &iter)
 {
     // Run through the iterator forwards and stick it into an array
     int32_t orderLength = 0;
-    int32_t *orders = getOrders(iter, orderLength);
+    LocalArray<Order> orders(getOrders(iter, orderLength));
     UErrorCode status = U_ZERO_ERROR;
 
     // Now go through it backwards and make sure we get the same values
@@ -636,26 +417,48 @@ void IntlTestCollator::backAndForth(CollationElementIterator &iter)
 
     while ((o = iter.previous(status)) != CollationElementIterator::NULLORDER)
     {
-        if (o != orders[--index])
-        {
+        /*int32_t offset = */iter.getOffset();
+
+        if (index == 0) {
+          if(o == 0) {
+            continue;
+          } else { // this is an error, orders exhausted but there are non-ignorable CEs from
+            // going backwards
+            errln("Backward iteration returned a non ignorable after orders are exhausted");
+            break;
+          }
+        }
+
+        index -= 1;
+        if (o != orders[index].order) {
             if (o == 0)
-                index ++;
-            else
-            {
-                while (index > 0 && orders[--index] == 0)
-                {
+                index += 1;
+            else {
+                while (index > 0 && orders[--index].order == 0) {
+                  // nothing...
                 }
-                if (o != orders[index])
-                {
-                    errln("Mismatch at index %d: 0x%X vs 0x%X", index,
-                        orders[index], o);
-                    break;
+
+                if (o != orders[index].order) {
+                    errln("Mismatched order at index %d: 0x%0:8X vs. 0x%0:8X", index,
+                    orders[index].order, o);
+                //break;
+                  return;
                 }
             }
         }
+
+#if TEST_OFFSETS
+        if (offset != orders[index].offset) {
+          errln("Mismatched offset at index %d: %d vs. %d", index,
+            orders[index].offset, offset);
+       //break;
+         return;
+        }
+#endif
+
     }
 
-    while (index != 0 && orders[index - 1] == 0)
+    while (index != 0 && orders[index - 1].order == 0)
     {
       index --;
     }
@@ -688,8 +491,6 @@ void IntlTestCollator::backAndForth(CollationElementIterator &iter)
         }
         errln("");
     }
-
-    delete[] orders;
 }
 
 
@@ -697,12 +498,13 @@ void IntlTestCollator::backAndForth(CollationElementIterator &iter)
  * Return an integer array containing all of the collation orders
  * returned by calls to next on the specified iterator
  */
-int32_t *IntlTestCollator::getOrders(CollationElementIterator &iter, int32_t &orderLength)
+IntlTestCollator::Order *IntlTestCollator::getOrders(CollationElementIterator &iter, int32_t &orderLength)
 {
     int32_t maxSize = 100;
     int32_t size = 0;
-    int32_t *orders = new int32_t[maxSize];
+    LocalArray<Order> orders(new Order[maxSize]);
     UErrorCode status = U_ZERO_ERROR;
+    int32_t offset = iter.getOffset();
 
     int32_t order;
     while ((order = iter.next(status)) != CollationElementIterator::NULLORDER)
@@ -710,27 +512,29 @@ int32_t *IntlTestCollator::getOrders(CollationElementIterator &iter, int32_t &or
         if (size == maxSize)
         {
             maxSize *= 2;
-            int32_t *temp = new int32_t[maxSize];
+            Order *temp = new Order[maxSize];
 
-            uprv_memcpy(temp, orders, size * sizeof(int32_t));
-            delete[] orders;
-            orders = temp;
+            uprv_memcpy(temp, orders.getAlias(), size * sizeof(Order));
+            orders.adoptInstead(temp);
         }
 
-        orders[size++] = order;
+        orders[size].order  = order;
+        orders[size].offset = offset;
+
+        offset = iter.getOffset();
+        size += 1;
     }
 
     if (maxSize > size)
     {
-        int32_t *temp = new int32_t[size];
+        Order *temp = new Order[size];
 
-        uprv_memcpy(temp, orders, size * sizeof(int32_t));
-        delete[] orders;
-        orders = temp;
+        uprv_memcpy(temp, orders.getAlias(), size * sizeof(Order));
+        orders.adoptInstead(temp);
     }
 
     orderLength = size;
-    return orders;
+    return orders.orphan();
 }
 
 #endif /* #if !UCONFIG_NO_COLLATION */