]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/transapi.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / intltest / transapi.cpp
index 9be3923f85ed9986fac4c45cef46cdd89e6483cf..4b59ecfde49231ec281599ee5e60a82e5a6e43e7 100644 (file)
@@ -1,6 +1,8 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /************************************************************************
  * COPYRIGHT: 
- * Copyright (c) 2000-2004, International Business Machines Corporation
+ * Copyright (c) 2000-2016, International Business Machines Corporation
  * and others. All Rights Reserved.
  ************************************************************************/
 /************************************************************************
 #include "transapi.h"
 #include "unicode/utypes.h"
 #include "unicode/translit.h"
-#include "rbt.h"
 #include "unicode/unifilt.h"
 #include "cpdtrans.h"
-#include "nultrans.h"
+#include "cmemory.h"
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -27,8 +28,6 @@
 #include "unicode/locid.h"
 #include "unicode/uniset.h"
 
-static const UVersionInfo ICU_31 = {3,1,0,0};
-
 int32_t getInt(UnicodeString str)
 {
     char buffer[20];
@@ -63,6 +62,7 @@ TransliteratorAPITest::runIndexedTest(int32_t index, UBool exec,
         TESTCASE(11,TestClone);
         TESTCASE(12,TestNullTransliterator);
         TESTCASE(13,TestRegisterUnregister);
+        TESTCASE(14,TestUnicodeFunctor);
         default: name = ""; break;
     }
 }
@@ -75,7 +75,7 @@ void TransliteratorAPITest::TestgetID() {
     UErrorCode status = U_ZERO_ERROR;
     Transliterator* t= Transliterator::createInstance(trans, UTRANS_FORWARD, parseError, status);
     if(t==0 || U_FAILURE(status)){
-        errln("FAIL: construction of Latin-Greek");
+        dataerrln("FAIL: construction of Latin-Greek -  %s",u_errorName(status));
         return;
     }else{
         ID= t->getID();
@@ -87,7 +87,7 @@ void TransliteratorAPITest::TestgetID() {
     for (i=0; i<Transliterator::countAvailableIDs(); i++){
         status = U_ZERO_ERROR;
         ID = (UnicodeString) Transliterator::getAvailableID(i);
-        if(ID.indexOf("Thai")>-1 && isICUVersionAtLeast(ICU_31)){
+        if(ID.indexOf("Thai")>-1){
             continue;
         }   
         t = Transliterator::createInstance(ID, UTRANS_FORWARD, parseError, status);
@@ -122,7 +122,7 @@ void TransliteratorAPITest::TestgetID() {
     Transliterator* t5=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
     if(t5 == 0)
         errln("FAIL: construction");
-    if(t1->getID() != t5->getID() || t5->getID() != t3->getID() || t1->getID() != t3->getID())
+    else if(t1->getID() != t5->getID() || t5->getID() != t3->getID() || t1->getID() != t3->getID())
         errln("FAIL: getID or clone failed");
 
 
@@ -141,7 +141,7 @@ void TransliteratorAPITest::TestgetInverse() {
      Transliterator* t2    = Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
      Transliterator* invt2 = Transliterator::createInstance("Devanagari-Latin", UTRANS_FORWARD, parseError, status);
      if(t1 == 0 || invt1 == 0 || t2 == 0 || invt2 == 0) {
-         errln("FAIL: in instantiation");
+         dataerrln("FAIL: in instantiation - %s", u_errorName(status));
          return;
      }
 
@@ -169,7 +169,7 @@ void TransliteratorAPITest::TestgetInverse() {
        "Any-Hex",
        "Hex-Any"
      };
-     for(uint32_t i=0; i<sizeof(TransID)/sizeof(TransID[0]); i=i+2){
+     for(uint32_t i=0; i<UPRV_LENGTHOF(TransID); i=i+2){
          Transliterator *trans1=Transliterator::createInstance(TransID[i], UTRANS_FORWARD, parseError, status);
          if(t1 == 0){
            errln("FAIL: in instantiation for" + TransID[i]);
@@ -197,7 +197,7 @@ void TransliteratorAPITest::TestClone(){
     t1=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
     t2=Transliterator::createInstance("Latin-Greek", UTRANS_FORWARD, parseError, status);
     if(t1 == 0 || t2 == 0){
-        errln("FAIL: construction");
+        dataerrln("FAIL: construction - %s", u_errorName(status));
         return;
     }
     t3=t1->clone();
@@ -243,10 +243,10 @@ void TransliteratorAPITest::TestGetDisplayName() {
     return;
 #else
 
-    for (uint32_t i=0; i<sizeof(dispNames)/sizeof(dispNames[0]); i=i+2 ) {
+    for (uint32_t i=0; i<UPRV_LENGTHOF(dispNames); i=i+2 ) {
         t = Transliterator::createInstance(dispNames[i+0], UTRANS_FORWARD, parseError, status);
         if(t==0){
-             errln("FAIL: construction: " + dispNames[i+0]);
+             dataerrln("FAIL: construction: " + dispNames[i+0] + " - " + u_errorName(status));
              status = U_ZERO_ERROR;
              continue;
         }
@@ -277,7 +277,7 @@ void TransliteratorAPITest::TestTransliterate1(){
          "Latin-Devanagari",CharsToUnicodeString("bha\\u0304rata"), CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924") ,
          "Latin-Devanagari",UnicodeString("kra ksha khra gra cra dya dhya",""), CharsToUnicodeString("\\u0915\\u094D\\u0930 \\u0915\\u094D\\u0936 \\u0916\\u094D\\u0930 \\u0917\\u094D\\u0930 \\u091a\\u094D\\u0930 \\u0926\\u094D\\u092F \\u0927\\u094D\\u092F") ,
 
-         "Devanagari-Latin",    CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"),        UnicodeString("bh\\u0101rata"),
+         "Devanagari-Latin",    CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"),        CharsToUnicodeString("bh\\u0101rata"),
      //  "Contracted-Expanded", CharsToUnicodeString("\\u00C0\\u00C1\\u0042"),               CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042") ,
      //  "Expanded-Contracted", CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042"), CharsToUnicodeString("\\u00C0\\u00C1\\u0042") ,
          //"Latin-Arabic",        "aap",                                 CharsToUnicodeString("\\u0627\\u06A4")     ,
@@ -292,11 +292,11 @@ void TransliteratorAPITest::TestTransliterate1(){
     UErrorCode status = U_ZERO_ERROR;
     UParseError parseError;
 
-    for(uint32_t i=0;i<sizeof(Data)/sizeof(Data[0]); i=i+3){
+    for(uint32_t i=0;i<UPRV_LENGTHOF(Data); i=i+3){
         t=Transliterator::createInstance(Data[i+0], UTRANS_FORWARD, parseError, status);
         if(t==0){
-            errln("FAIL: construction: " + Data[i+0] + " Error: "  + u_errorName(status));
-            errln("PreContext: " + prettify(parseError.preContext) + " PostContext: " + prettify( parseError.postContext) );
+            dataerrln("FAIL: construction: " + Data[i+0] + " Error: "  + u_errorName(status));
+            dataerrln("PreContext: " + prettify(parseError.preContext) + " PostContext: " + prettify( parseError.postContext) );
             status = U_ZERO_ERROR;
             continue;
         }
@@ -328,7 +328,7 @@ void TransliteratorAPITest::TestTransliterate2(){
          "Hex-Any",         CharsToUnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F\\u0021\\u0020"), "0", "5",  "hello", "hello! "  ,
        //  "Contracted-Expanded", CharsToUnicodeString("\\u00C0\\u00C1\\u0042"),        "1", "2",  CharsToUnicodeString("\\u0041\\u0301"), CharsToUnicodeString("\\u00C0\\u0041\\u0301\\u0042") ,
          "Devanagari-Latin",    CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), "0", "1",  "bha", CharsToUnicodeString("bha\\u093E\\u0930\\u0924") ,
-         "Devanagari-Latin",    CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), "1", "2",  "\\u0314\\u0101", CharsToUnicodeString("\\u092D\\u0314\\u0101\\u0930\\u0924")  
+         "Devanagari-Latin",    CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), "1", "2",  CharsToUnicodeString("\\u0314\\u0101"), CharsToUnicodeString("\\u092D\\u0314\\u0101\\u0930\\u0924")  
 
     };
     logln("\n   Testing transliterate(String, int, int, StringBuffer)");
@@ -339,10 +339,10 @@ void TransliteratorAPITest::TestTransliterate2(){
     UErrorCode status = U_ZERO_ERROR;
     UParseError parseError;
 
-    for(uint32_t i=0; i<sizeof(Data2)/sizeof(Data2[0]); i=i+6){
+    for(uint32_t i=0; i<UPRV_LENGTHOF(Data2); i=i+6){
         t=Transliterator::createInstance(Data2[i+0], UTRANS_FORWARD, parseError, status);
         if(t==0){
-            errln("FAIL: construction: " + prettify(Data2[i+0]));
+            dataerrln("FAIL: construction: " + prettify(Data2[i+0]) + " - " + u_errorName(status));
             continue;
         }
         start=getInt(Data2[i+2]);
@@ -402,7 +402,7 @@ void TransliteratorAPITest::TestTransliterate3(){
 
     if(t == 0)
         errln("FAIL : construction");
-    for(uint32_t i=0; i<sizeof(Data)/sizeof(Data[0]); i=i+3){
+    for(uint32_t i=0; i<UPRV_LENGTHOF(Data); i=i+3){
         start=getInt(Data[i+0]);
         limit=getInt(Data[i+1]);
         t->transliterate(rs, start, limit);
@@ -449,7 +449,7 @@ void TransliteratorAPITest::TestSimpleKeyboardTransliterator(){
         {-1, 16, 14, 16},   //invalid since START<0
         {3,  50, 2,  50}    //invalid since LIMIT>text.length()
     };
-    for(uint32_t i=0; i<sizeof(index1)/sizeof(index1[0]); i++){
+    for(uint32_t i=0; i<UPRV_LENGTHOF(index1); i++){
         status=U_ZERO_ERROR;
         t->transliterate(rs, index1[i], insertion, status);
         if(status == U_ILLEGAL_ARGUMENT_ERROR)
@@ -510,19 +510,20 @@ void TransliteratorAPITest::TestKeyboardTransliterator1(){
     status=U_ZERO_ERROR;
     index.contextStart = index.contextLimit = index.start = index.limit = 0;
     logln("Testing transliterate(Replaceable, int32_t, UChar, UErrorCode)");
-    for(i=10; i<sizeof(Data)/sizeof(Data[0]); i=i+2){
+    for(i=10; i<UPRV_LENGTHOF(Data); i=i+2){
         UnicodeString log;
-         if (Data[i+0] != "") {
-           log = s + " + " + Data[i+0] + " -> ";
-           UChar c=Data[i+0].charAt(0);
-           t->transliterate(s, index, c, status);
-           if(U_FAILURE(status))
+        if (Data[i+0] != "") {
+            log = s + " + " + Data[i+0] + " -> ";
+            UChar c=Data[i+0].charAt(0);
+            t->transliterate(s, index, c, status);
+            if(U_FAILURE(status)) {
                errln("FAIL: " + t->getID()+ ".transliterate(Replaceable, int32_t[], UChar, UErrorCode)-->" + (UnicodeString)u_errorName(status));
                continue;
-         }else {
+            }
+        } else {
            log = s + " => ";
            t->finishTransliteration(s, index);
-         }
+        }
         // Show the start index '{' and the cursor '|'
         displayOutput(s, Data[i+1], log, index); 
     }
@@ -557,7 +558,7 @@ void TransliteratorAPITest::TestKeyboardTransliterator2(){
     rs="Initial String: add--";
     t=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD, parseError, status);
     if(t == 0)
-        errln("FAIL : construction");
+        dataerrln("FAIL : construction - %s", u_errorName(status));
     else {
         keyboardAux(t, Data, rs, 0, 20);
         delete t;
@@ -566,7 +567,7 @@ void TransliteratorAPITest::TestKeyboardTransliterator2(){
     rs="Hindi --";
     t=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
     if(t == 0)
-        errln("FAIL : construction");
+        dataerrln("FAIL : construction - %s", u_errorName(status));
     else
         keyboardAux(t, Data, rs, 20, 40);
 
@@ -592,14 +593,12 @@ void TransliteratorAPITest::TestKeyboardTransliterator3(){
     UTransPosition index={0, 0, 0, 0};
     logln("Testing transliterate(Replaceable, int32_t, UErrorCode)");
     Transliterator *t=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD, parseError, status);
-    if(U_FAILURE(status)) {
+    if(t == 0 || U_FAILURE(status)) {
       errln("Error creating transliterator %s", u_errorName(status));
       delete t;
       return;
     }
-    if(t == 0)
-        errln("FAIL : construction");
-    for(uint32_t i=0; i<sizeof(Data)/sizeof(Data[0]); i=i+4){
+    for(uint32_t i=0; i<UPRV_LENGTHOF(Data); i=i+4){
         UnicodeString log;
         index.contextStart=getInt(Data[i+0]);
         index.contextLimit=index.limit=getInt(Data[i+1]);
@@ -618,8 +617,9 @@ void TransliteratorAPITest::TestKeyboardTransliterator3(){
     delete t;
 }
 void TransliteratorAPITest::TestNullTransliterator(){
+    UErrorCode status=U_ZERO_ERROR;
     UnicodeString s("Transliterate using null transliterator");
-    NullTransliterator *nullTrans=new NullTransliterator();
+    Transliterator *nullTrans=Transliterator::createInstance("Any-Null", UTRANS_FORWARD, status);
     int32_t transLimit;
     int32_t start=0;
     int32_t limit=s.length();
@@ -636,7 +636,7 @@ void TransliteratorAPITest::TestNullTransliterator(){
     index.contextLimit = limit;
     index.start = 0;
     index.limit = limit;
-    nullTrans->handleTransliterate(replaceable, index, TRUE);
+    nullTrans->finishTransliteration(replaceable, index);
     if(index.start != limit){
         errln("ERROR: NullTransliterator->handleTransliterate() failed");
     }
@@ -649,9 +649,8 @@ void TransliteratorAPITest::TestNullTransliterator(){
 void TransliteratorAPITest::TestRegisterUnregister(){
    
    UErrorCode status=U_ZERO_ERROR;
-   UParseError parseError;
     /* Make sure it doesn't exist */
-   if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, parseError, status) != NULL) {
+   if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, status) != NULL) {
       errln("FAIL: TestA-TestB already registered\n");
       return;
    }
@@ -665,14 +664,16 @@ void TransliteratorAPITest::TestRegisterUnregister(){
    status =U_ZERO_ERROR;
 
    /* Create it */
-   Transliterator *t = new RuleBasedTransliterator("TestA-TestB",
+   UParseError parseError;
+   Transliterator *t = Transliterator::createFromRules("TestA-TestB",
                                                    "a<>b",
+                                                   UTRANS_FORWARD, parseError,
                                                    status);
    /* Register it */
    Transliterator::registerInstance(t);
 
    /* Now check again -- should exist now*/
-   Transliterator *s = Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, parseError, status);
+   Transliterator *s = Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, status);
    if (s == NULL) {
       errln("FAIL: TestA-TestB not registered\n");
       return;
@@ -694,7 +695,7 @@ void TransliteratorAPITest::TestRegisterUnregister(){
    /*unregister the instance*/
    Transliterator::unregister("TestA-TestB");
    /* now Make sure it doesn't exist */
-   if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, parseError, status) != NULL) {
+   if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, status) != NULL) {
       errln("FAIL: TestA-TestB isn't unregistered\n");
       return;
    }
@@ -711,7 +712,7 @@ int gTestFilter3ClassID = 0;
  */
 class TestFilter1 : public UnicodeFilter {
     UClassID getDynamicClassID()const { return &gTestFilter1ClassID; }
-    virtual UnicodeFunctor* clone() const {
+    virtual TestFilter1* clone() const {
         return new TestFilter1(*this);
     }
     virtual UBool contains(UChar32 c) const {
@@ -732,7 +733,7 @@ class TestFilter1 : public UnicodeFilter {
 };
 class TestFilter2 : public UnicodeFilter {
     UClassID getDynamicClassID()const { return &gTestFilter2ClassID; }
-    virtual UnicodeFunctor* clone() const {
+    virtual TestFilter2* clone() const {
         return new TestFilter2(*this);
     }
     virtual UBool contains(UChar32 c) const {
@@ -753,7 +754,7 @@ class TestFilter2 : public UnicodeFilter {
 };
 class TestFilter3 : public UnicodeFilter {
     UClassID getDynamicClassID()const { return &gTestFilter3ClassID; }
-    virtual UnicodeFunctor* clone() const {
+    virtual TestFilter3* clone() const {
         return new TestFilter3(*this);
     }
     virtual UBool contains(UChar32 c) const {
@@ -778,18 +779,18 @@ void TransliteratorAPITest::TestGetAdoptFilter(){
     UErrorCode status = U_ZERO_ERROR;
     UParseError parseError;
     Transliterator *t=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD, parseError, status);
-    if(U_FAILURE(status)) {
-      errln("Error creating transliterator %s", u_errorName(status));
-      delete t;
-      return;
+    if(t == 0 || U_FAILURE(status)) {
+        errln("Error creating transliterator %s", u_errorName(status));
+        delete t;
+        return;
     }
-    if(t == 0)
-        errln("FAIL : construction");
     const UnicodeFilter *u=t->getFilter();
     if(u != NULL){
-      errln("FAIL: getFilter failed. Didn't return null when the transliterator used no filtering");
+        errln("FAIL: getFilter failed. Didn't return null when the transliterator used no filtering");
+        delete t;
+        return;
     }
-          
+
     UnicodeString got, temp, message;
     UnicodeString data="ABCabcbbCBa";
     temp = data;
@@ -905,7 +906,7 @@ void TransliteratorAPITest::doTest(const UnicodeString& message, const UnicodeSt
     if (prettify(result) == prettify(expected)) 
         logln((UnicodeString)"Ok: " + prettify(message) + " passed \"" + prettify(expected) + "\"");
     else 
-        errln((UnicodeString)"FAIL:" + message + " failed  Got-->" + prettify(result)+ ", Expected--> " + prettify(expected) );
+        dataerrln((UnicodeString)"FAIL:" + message + " failed  Got-->" + prettify(result)+ ", Expected--> " + prettify(expected) );
 }
 
 
@@ -914,8 +915,12 @@ void TransliteratorAPITest::doTest(const UnicodeString& message, const UnicodeSt
 //                    transliterator, just to verify that they don't fail in some
 //                    destructive way.
 //
-#define CEASSERT(a) {if (!(a)) { \
-errln("FAIL at line %d from line %d: %s", __LINE__, line, #a);  return; }}
+#define CEASSERT(a) UPRV_BLOCK_MACRO_BEGIN { \
+    if (!(a)) { \
+        errln("FAIL at line %d from line %d: %s", __LINE__, line, #a); \
+        return; \
+    } \
+} UPRV_BLOCK_MACRO_END
 
 void TransliteratorAPITest::callEverything(const Transliterator *tr, int line) {
     Transliterator *clonedTR = tr->clone();
@@ -964,7 +969,24 @@ void TransliteratorAPITest::callEverything(const Transliterator *tr, int line) {
     delete clonedTR;
 }
 
+static const int MyUnicodeFunctorTestClassID = 0;
+class MyUnicodeFunctorTestClass : public UnicodeFunctor {
+public:
+    virtual UnicodeFunctor* clone() const {return NULL;}
+    static UClassID getStaticClassID(void) {return (UClassID)&MyUnicodeFunctorTestClassID;}
+    virtual UClassID getDynamicClassID(void) const {return getStaticClassID();}
+    virtual void setData(const TransliterationRuleData*) {}
+};
 
+void TransliteratorAPITest::TestUnicodeFunctor() {
+    MyUnicodeFunctorTestClass myClass;
+    if (myClass.toMatcher() != NULL) {
+        errln("FAIL: UnicodeFunctor::toMatcher did not return NULL");
+    }
+    if (myClass.toReplacer() != NULL) {
+        errln("FAIL: UnicodeFunctor::toReplacer did not return NULL");
+    }
+}
 
 
 #endif /* #if !UCONFIG_NO_TRANSLITERATION */