X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..151279e3792e85d0417b499c229886b3af724f55:/icuSources/test/intltest/transapi.cpp?ds=sidebyside diff --git a/icuSources/test/intltest/transapi.cpp b/icuSources/test/intltest/transapi.cpp index 5146784c..24317ca8 100644 --- a/icuSources/test/intltest/transapi.cpp +++ b/icuSources/test/intltest/transapi.cpp @@ -1,8 +1,8 @@ -/******************************************************************** +/************************************************************************ * COPYRIGHT: - * Copyright (c) 2000-2003, International Business Machines Corporation and - * others. All Rights Reserved. - ********************************************************************/ + * Copyright (c) 2000-2009, International Business Machines Corporation + * and others. All Rights Reserved. + ************************************************************************/ /************************************************************************ * Date Name Description * 1/03/2000 Madhu Creation. @@ -16,15 +16,14 @@ #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 #include #include #include "unicode/rep.h" #include "unicode/locid.h" +#include "unicode/uniset.h" int32_t getInt(UnicodeString str) { @@ -60,6 +59,7 @@ TransliteratorAPITest::runIndexedTest(int32_t index, UBool exec, TESTCASE(11,TestClone); TESTCASE(12,TestNullTransliterator); TESTCASE(13,TestRegisterUnregister); + TESTCASE(14,TestUnicodeFunctor); default: name = ""; break; } } @@ -72,7 +72,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(); @@ -84,6 +84,9 @@ void TransliteratorAPITest::TestgetID() { for (i=0; i-1){ + continue; + } t = Transliterator::createInstance(ID, UTRANS_FORWARD, parseError, status); if(t == 0){ errln("FAIL: " + ID); @@ -116,7 +119,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"); @@ -135,7 +138,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; } @@ -191,7 +194,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(); @@ -240,7 +243,7 @@ void TransliteratorAPITest::TestGetDisplayName() { for (uint32_t i=0; itransliterate(temp, start, limit); doTest(t->getID() + ".transliterate(Replaceable, int32_t, int32_t, ):(" + start + "," + limit + ") for \n\t source: " + prettify(Data2[i+1]), temp, Data2[i+5]); status = U_ZERO_ERROR; + callEverything(t, __LINE__); delete t; t = NULL; } @@ -363,11 +368,13 @@ void TransliteratorAPITest::TestTransliterate2(){ } gotResBuf = temp = "try start greater than limit"; t->transliterate(gotResBuf, 10, 5); - if(gotResBuf == temp) + if(gotResBuf == temp) { logln("OK: start greater than limit value handled correctly"); - else + } else { errln("FAIL: start greater than limit value returned" + gotResBuf); + } + callEverything(t, __LINE__); delete t; } @@ -408,12 +415,16 @@ void TransliteratorAPITest::TestSimpleKeyboardTransliterator(){ UParseError parseError; Transliterator* t=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD, parseError, status); if(t == 0) { + UnicodeString context; + + if (parseError.preContext[0]) { + context += (UnicodeString)" at " + parseError.preContext; + } + if (parseError.postContext[0]) { + context += (UnicodeString)" | " + parseError.postContext; + } errln((UnicodeString)"FAIL: can't create Any-Hex, " + - (UnicodeString)u_errorName(status) + - (parseError.preContext[0] ? - ((UnicodeString)" at " + parseError.preContext + - (parseError.postContext[0] ? - ((UnicodeString)" | " + parseError.postContext) : (UnicodeString)"")) : (UnicodeString)"")); + (UnicodeString)u_errorName(status) + context); return; } UTransPosition index={19,20,20,20}; @@ -543,7 +554,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; @@ -552,7 +563,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); @@ -578,13 +589,11 @@ 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; ihandleTransliterate(replaceable, index, TRUE); + nullTrans->finishTransliteration(replaceable, index); if(index.start != limit){ errln("ERROR: NullTransliterator->handleTransliterate() failed"); } doTest((UnicodeString)"NullTransliterator->handleTransliterate", replaceable, s); + callEverything(nullTrans, __LINE__); delete nullTrans; @@ -634,9 +645,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; } @@ -650,18 +660,22 @@ 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; } + callEverything(s, __LINE__); + callEverything(t, __LINE__); delete s; /* Check inverse too @@ -677,7 +691,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; } @@ -761,18 +775,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; @@ -801,6 +815,7 @@ void TransliteratorAPITest::TestGetAdoptFilter(){ doTest("adoptFilter round trip", got, temp); t->adoptFilter(new TestFilter2); + callEverything(t, __LINE__); data="heelloe"; exp=UnicodeString("\\u0068eell\\u006Fe", ""); got = data; @@ -887,7 +902,83 @@ 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) ); +} + + +// +// callEverything call all of the const (non-destructive) methods on a +// 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; }} + +void TransliteratorAPITest::callEverything(const Transliterator *tr, int line) { + Transliterator *clonedTR = tr->clone(); + CEASSERT(clonedTR != NULL); + + int32_t maxcl = tr->getMaximumContextLength(); + CEASSERT(clonedTR->getMaximumContextLength() == maxcl); + + UnicodeString id; + UnicodeString clonedId; + id = tr->getID(); + clonedId = clonedTR->getID(); + CEASSERT(id == clonedId); + + const UnicodeFilter *filter = tr->getFilter(); + const UnicodeFilter *clonedFilter = clonedTR->getFilter(); + if (filter == NULL || clonedFilter == NULL) { + // If one filter is NULL they better both be NULL. + CEASSERT(filter == clonedFilter); + } else { + CEASSERT(filter != clonedFilter); + } + + UnicodeString rules; + UnicodeString clonedRules; + rules = tr->toRules(rules, FALSE); + clonedRules = clonedTR->toRules(clonedRules, FALSE); + CEASSERT(rules == clonedRules); + + UnicodeSet sourceSet; + UnicodeSet clonedSourceSet; + tr->getSourceSet(sourceSet); + clonedTR->getSourceSet(clonedSourceSet); + CEASSERT(clonedSourceSet == sourceSet); + + UnicodeSet targetSet; + UnicodeSet clonedTargetSet; + tr->getTargetSet(targetSet); + clonedTR->getTargetSet(clonedTargetSet); + CEASSERT(targetSet == clonedTargetSet); + + UClassID classID = tr->getDynamicClassID(); + CEASSERT(classID == clonedTR->getDynamicClassID()); + CEASSERT(classID != 0); + + 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 */