2001-03-21 20:09:56 +00:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (c) 1997-2003, International Business Machines Corporation and
|
2001-03-21 20:09:56 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 23:23:48 +00:00
|
|
|
************************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 03/15/2000 Madhu Creation.
|
|
|
|
************************************************************************/
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
#include "ittrans.h"
|
|
|
|
#include "unhxtrts.h"
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/translit.h"
|
|
|
|
#include "unicode/unifilt.h"
|
2002-03-19 17:50:15 +00:00
|
|
|
#include "unicode/uchar.h"
|
2000-03-28 23:23:48 +00:00
|
|
|
#include "intltest.h"
|
2003-02-14 01:44:49 +00:00
|
|
|
#include "unitohex.h"
|
2000-03-28 23:23:48 +00:00
|
|
|
/*converts a Unicodestring to integer*/
|
|
|
|
static int32_t getInt(UnicodeString str)
|
|
|
|
{
|
2000-08-16 15:30:13 +00:00
|
|
|
int32_t result = 0;
|
|
|
|
int32_t len = str.length();
|
|
|
|
int32_t i = 0;
|
|
|
|
for(i=0; i<len; i++) {
|
|
|
|
result = result*10+u_charDigitValue(str.char32At(i));
|
|
|
|
}
|
|
|
|
return result;
|
2000-03-28 23:23:48 +00:00
|
|
|
}
|
|
|
|
//---------------------------------------------
|
|
|
|
// runIndexedTest
|
|
|
|
//---------------------------------------------
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void UniToHexTransliteratorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
|
2000-03-28 23:23:48 +00:00
|
|
|
{
|
|
|
|
if (exec) logln((UnicodeString)"TestSuite UnicodeToHexadecimal Transliterator API ");
|
|
|
|
switch (index) {
|
2000-08-14 21:42:36 +00:00
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
case 0: name = "TestConstruction"; if (exec) TestConstruction(); break;
|
|
|
|
case 1: name = "TestCloneEqual"; if (exec) TestCloneEqual(); break;
|
|
|
|
case 2: name = "TestUpperCase"; if (exec) TestUpperCase(); break;
|
2000-08-14 21:42:36 +00:00
|
|
|
case 3: name = "TestPattern"; if (exec) TestPattern(); break;
|
2000-03-28 23:23:48 +00:00
|
|
|
case 4: name = "TestSimpleTransliterate"; if (exec) TestSimpleTransliterate(); break;
|
|
|
|
case 5: name = "TestTransliterate"; if (exec) TestTransliterate(); break;
|
|
|
|
default: name = ""; break; /*needed to end loop*/
|
|
|
|
}
|
|
|
|
}
|
2001-07-17 23:39:00 +00:00
|
|
|
|
|
|
|
// This test used to call handleTransliterate. That is a protected
|
|
|
|
// method that isn't supposed to be called externally. This method is
|
|
|
|
// a workaround to make it call the correct method.
|
|
|
|
static void pseudoHandleTransliterate(const Transliterator* t,
|
|
|
|
Replaceable& text,
|
|
|
|
UTransPosition& index,
|
|
|
|
UBool incremental) {
|
|
|
|
if (incremental) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
t->transliterate(text, index, status);
|
|
|
|
} else {
|
|
|
|
t->finishTransliteration(text, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
/**
|
|
|
|
* Used by TestConstruction() and TestTransliterate.
|
|
|
|
*/
|
2002-12-16 19:51:39 +00:00
|
|
|
int32_t gTestUniFilterClassID;
|
2000-03-28 23:23:48 +00:00
|
|
|
class TestUniFilter : public UnicodeFilter {
|
2002-12-16 19:51:39 +00:00
|
|
|
virtual UClassID getDynamicClassID() const { return &gTestUniFilterClassID; }
|
2002-02-07 01:07:55 +00:00
|
|
|
virtual UnicodeFunctor* clone() const {
|
2000-03-28 23:23:48 +00:00
|
|
|
return new TestUniFilter(*this);
|
|
|
|
}
|
2001-07-25 19:11:02 +00:00
|
|
|
virtual UBool contains(UChar32 c) const {
|
2000-06-27 18:20:33 +00:00
|
|
|
if(c==0x0063 || c==0x0061 || c==0x0043 || c==0x0041)
|
2000-03-28 23:23:48 +00:00
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-08-28 17:17:04 +00:00
|
|
|
// Stubs
|
|
|
|
virtual UnicodeString& toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
virtual UBool matchesIndexValue(uint8_t v) const {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
virtual void addMatchSetTo(UnicodeSet& toUnionTo) const {}
|
2000-03-28 23:23:48 +00:00
|
|
|
};
|
|
|
|
void UniToHexTransliteratorTest::TestConstruction(){
|
2000-08-14 21:42:36 +00:00
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
|
|
|
logln("Testing the construction UnicodeToHexTransliterator()");
|
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator();
|
|
|
|
if(trans1==0){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed Error=" + (UnicodeString)u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
delete trans1;
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
logln("Testing the cosntruction UnicodeToHexTransliterator(pattern, status)");
|
2000-03-28 23:23:48 +00:00
|
|
|
UnicodeString pattern("\\\\U+0000abc");
|
2000-08-14 21:42:36 +00:00
|
|
|
trans1=new UnicodeToHexTransliterator(pattern, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern =" + pattern + " Error=" + (UnicodeString)u_errorName(status));
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
delete trans1;
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
logln("Testing the cosntruction UnicodeToHexTransliterator(pattern, status) with illegal pattern");
|
|
|
|
UnicodeString pattern2("\\X+");
|
|
|
|
trans1=new UnicodeToHexTransliterator(pattern2, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
logln("OK: UnicodeToHexTransliterator construction for illegal pattern failed, as expected");
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
} else {
|
|
|
|
errln("Error: calling the UnicodeToHexTransliterator constructor with illegal pattern should fail");
|
|
|
|
}
|
2001-10-03 18:05:44 +00:00
|
|
|
delete trans1;
|
2000-08-14 21:42:36 +00:00
|
|
|
|
|
|
|
logln("Testing the construction UnicodeToHexTransliterator(pattern, isUppercase, adoptedFilter, status)");
|
2000-03-28 23:23:48 +00:00
|
|
|
trans1=new UnicodeToHexTransliterator(pattern, FALSE, NULL, status);
|
2000-08-14 21:42:36 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed. Error=" + (UnicodeString)u_errorName(status));
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logln("Testing the copy construction");
|
|
|
|
UnicodeToHexTransliterator *trans1copy=new UnicodeToHexTransliterator(*trans1);
|
|
|
|
if(trans1->toPattern() != trans1copy->toPattern() || trans1->isUppercase() != trans1copy->isUppercase() ||
|
|
|
|
trans1->getID() != trans1copy->getID()){
|
|
|
|
errln("Copy construction failed");
|
|
|
|
}
|
|
|
|
delete trans1copy;
|
|
|
|
delete trans1;
|
|
|
|
|
|
|
|
logln("Testing the construction UnicodeToHexTransliterator(pattern, isUppercase, adoptedFilter, status)");
|
|
|
|
trans1=new UnicodeToHexTransliterator(pattern, TRUE, new TestUniFilter, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed Error=" + (UnicodeString)u_errorName(status));
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logln("Testing the copy construction");
|
|
|
|
trans1copy=new UnicodeToHexTransliterator(*trans1);
|
|
|
|
if(trans1->toPattern() != trans1copy->toPattern() || trans1->isUppercase() != trans1copy->isUppercase() ||
|
|
|
|
trans1->getID() != trans1copy->getID() ||
|
|
|
|
trans1->getFilter() == NULL || trans1copy->getFilter() == NULL ){
|
|
|
|
errln("Copy construction failed");
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
delete trans1copy;
|
2000-08-14 21:42:36 +00:00
|
|
|
delete trans1;
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniToHexTransliteratorTest::TestCloneEqual(){
|
2000-08-14 21:42:36 +00:00
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
|
|
|
UnicodeToHexTransliterator *transdefault=new UnicodeToHexTransliterator();
|
|
|
|
UnicodeString pattern1("\\U##00");
|
|
|
|
UnicodeString pattern2("\\\\uni0000");
|
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator(pattern1, status);
|
2000-03-28 23:23:48 +00:00
|
|
|
if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
|
2000-08-14 21:42:36 +00:00
|
|
|
errln("UnicodeToHexTransliterator construction failed");
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UnicodeToHexTransliterator *trans2=new UnicodeToHexTransliterator(pattern2, status);
|
2000-03-28 23:23:48 +00:00
|
|
|
if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
|
2000-08-14 21:42:36 +00:00
|
|
|
errln("UnicodeToHexTransliterator construction failed");
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
logln("Testing the clone() API of the UnicodeToHexTransliterator");
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeToHexTransliterator *transdefaultclone=(UnicodeToHexTransliterator*)transdefault->clone();
|
|
|
|
UnicodeToHexTransliterator *trans1clone=(UnicodeToHexTransliterator*)trans1->clone();
|
|
|
|
UnicodeToHexTransliterator *trans2clone=(UnicodeToHexTransliterator*)trans2->clone();
|
|
|
|
if(transdefault->toPattern() != transdefaultclone->toPattern() ||
|
|
|
|
transdefault->isUppercase() != transdefaultclone->isUppercase() ||
|
|
|
|
trans1->toPattern() != trans1clone->toPattern() ||
|
|
|
|
trans1->isUppercase() != trans1clone->isUppercase() ||
|
|
|
|
trans2->toPattern() != trans2clone->toPattern() ||
|
|
|
|
trans2->isUppercase() != trans2clone->isUppercase() ||
|
|
|
|
transdefault->toPattern() == trans1->toPattern() ||
|
|
|
|
trans1->toPattern() == trans2clone->toPattern() ||
|
|
|
|
trans2->toPattern() == transdefault->toPattern() ) {
|
|
|
|
errln("Error: clone() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logln("Testing the =operator of the UnicodeToHexTransliterator");
|
2000-07-11 17:44:24 +00:00
|
|
|
UnicodeToHexTransliterator *transdefaultequal=new UnicodeToHexTransliterator();
|
|
|
|
UnicodeToHexTransliterator *trans1equal=new UnicodeToHexTransliterator();
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeToHexTransliterator *trans2equal=new UnicodeToHexTransliterator();
|
2000-07-11 17:44:24 +00:00
|
|
|
*transdefaultequal=*transdefault;
|
|
|
|
*trans1equal=*trans1;
|
|
|
|
*trans2equal=*trans2;
|
2000-06-28 22:26:59 +00:00
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
if(transdefault->toPattern() != transdefaultequal->toPattern() ||
|
|
|
|
transdefault->isUppercase() != transdefaultequal->isUppercase() ||
|
|
|
|
trans1->toPattern() != trans1equal->toPattern() ||
|
|
|
|
trans1->isUppercase() != trans1equal->isUppercase() ||
|
|
|
|
trans2->toPattern() != trans2equal->toPattern() ||
|
|
|
|
trans2->isUppercase() != trans2equal->isUppercase() ||
|
|
|
|
transdefault->toPattern() == trans1->toPattern() ||
|
|
|
|
trans1->toPattern() == trans2equal->toPattern() ||
|
|
|
|
trans2->toPattern() == transdefault->toPattern() ) {
|
|
|
|
errln("Error: equal() failed");
|
|
|
|
}
|
|
|
|
if(transdefaultclone->toPattern() != transdefaultequal->toPattern() ||
|
|
|
|
transdefaultclone->isUppercase() != transdefaultequal->isUppercase() ||
|
|
|
|
trans1equal->toPattern() != trans1clone->toPattern() ||
|
|
|
|
trans1equal->isUppercase() != trans1clone->isUppercase() ||
|
|
|
|
trans2clone->toPattern() != trans2equal->toPattern() ||
|
|
|
|
trans2clone->isUppercase() != trans2equal->isUppercase() ) {
|
|
|
|
errln("Error: equal() or clone() failed");
|
|
|
|
}
|
2001-10-03 18:05:44 +00:00
|
|
|
delete transdefaultclone;
|
|
|
|
delete trans1clone;
|
|
|
|
delete trans2clone;
|
2000-07-11 17:44:24 +00:00
|
|
|
delete transdefaultequal;
|
|
|
|
delete trans1equal;
|
|
|
|
delete trans2equal;
|
2000-08-14 21:42:36 +00:00
|
|
|
delete transdefault;
|
|
|
|
delete trans1;
|
|
|
|
delete trans2;
|
2000-03-28 23:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UniToHexTransliteratorTest::TestUpperCase(){
|
2000-08-14 21:42:36 +00:00
|
|
|
logln("Testing the isUppercase() and setUppercase() API of UnicodeToHexTransliterator");
|
2000-03-28 23:23:48 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeString str("abk");
|
|
|
|
/*default transliterator has upper case TRUE*/
|
|
|
|
UnicodeToHexTransliterator *transdefault=new UnicodeToHexTransliterator();
|
2000-03-28 23:23:48 +00:00
|
|
|
if(transdefault == 0){
|
2000-08-14 21:42:36 +00:00
|
|
|
errln("UnicodeToHexTransliterator construction failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect(*transdefault, "where uppercase=default", str, UnicodeString("\\u0061\\u0062\\u006B", ""));
|
|
|
|
|
|
|
|
UnicodeString pattern("\\\\u0000", "");
|
|
|
|
/*transliterator with Uppercase FALSE*/
|
2000-03-28 23:23:48 +00:00
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator(pattern, FALSE, NULL, status);
|
2000-08-14 21:42:36 +00:00
|
|
|
if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern =" + pattern);
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect(*trans1, "where uppercase=FALSE", str, UnicodeString("\\u0061\\u0062\\u006b", "")); /*doesn't display uppercase*/
|
|
|
|
|
|
|
|
if(transdefault->isUppercase() != TRUE || trans1->isUppercase() != FALSE ){
|
|
|
|
errln("isUpperCase() failed");
|
|
|
|
}
|
|
|
|
/*changing the outputhexdigits to lower case for the default transliterator*/
|
|
|
|
transdefault->setUppercase(trans1->isUppercase());
|
|
|
|
if(transdefault->isUppercase() != trans1->isUppercase() || transdefault->isUppercase() != FALSE){
|
|
|
|
errln("setUppercase() failed");
|
|
|
|
}
|
|
|
|
/*doesn't ouput uppercase hex, since transdefault's uppercase is set to FALSE using setUppercase*/
|
|
|
|
expect(*transdefault, "where uppercase=FALSE", str, UnicodeString("\\u0061\\u0062\\u006b", ""));
|
|
|
|
|
|
|
|
/*trying round trip*/
|
|
|
|
transdefault->setUppercase(TRUE);
|
|
|
|
if(transdefault->isUppercase() != TRUE || transdefault->isUppercase() == trans1->isUppercase() ){
|
|
|
|
errln("setUppercase() failed");
|
|
|
|
}
|
|
|
|
/*displays upper case since it is set to TRUE*/
|
|
|
|
expect(*transdefault, "where uppercase=TRUE", str, UnicodeString("\\u0061\\u0062\\u006B", ""));
|
|
|
|
|
|
|
|
delete transdefault;
|
|
|
|
delete trans1;
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
}
|
|
|
|
void UniToHexTransliteratorTest::TestPattern(){
|
2000-08-14 21:42:36 +00:00
|
|
|
logln("Testing the applyPattern() and toPattern() API of UnicodeToHexTransliterator");
|
2000-03-28 23:23:48 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-08-14 21:42:36 +00:00
|
|
|
/*default transliterator has pattern \\u0000*/
|
|
|
|
UnicodeToHexTransliterator *transdefault=new UnicodeToHexTransliterator();
|
2000-03-28 23:23:48 +00:00
|
|
|
if(transdefault == 0){
|
2000-08-14 21:42:36 +00:00
|
|
|
errln("UnicodeToHexTransliterator construction failed");
|
|
|
|
return;
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
UnicodeString defaultpattern=transdefault->toPattern();
|
2000-08-14 21:42:36 +00:00
|
|
|
|
|
|
|
UnicodeString pattern1("\\\\U+0000", "");
|
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator(pattern1, TRUE, NULL, status);
|
|
|
|
if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern =" + pattern1);
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
/*test toPattern() */
|
2000-08-14 21:42:36 +00:00
|
|
|
if(transdefault->toPattern() == trans1->toPattern() ||
|
|
|
|
transdefault->toPattern() != UnicodeString("\\\\u0000", "") ||
|
|
|
|
trans1->toPattern() != pattern1 ){
|
|
|
|
errln("Error: toPattern() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*apply patterns for transdefault*/
|
|
|
|
UnicodeString str("abKf");
|
|
|
|
expectPattern(*transdefault, pattern1, str, UnicodeString("\\U+0061\\U+0062\\U+004B\\U+0066", ""));
|
|
|
|
expectPattern(*transdefault, UnicodeString("\\U##00,", ""), str, UnicodeString("U61,U62,U4B,U66,", ""));
|
|
|
|
expectPattern(*transdefault, defaultpattern, str, UnicodeString("\\u0061\\u0062\\u004B\\u0066", ""));
|
|
|
|
expectPattern(*trans1, UnicodeString("\\uni0000", ""), str, UnicodeString("uni0061uni0062uni004Buni0066", ""));
|
|
|
|
expectPattern(*trans1, UnicodeString("\\\\S-0000-E", ""), str, UnicodeString("\\S-0061-E\\S-0062-E\\S-004B-E\\S-0066-E", ""));
|
|
|
|
expectPattern(*trans1, UnicodeString("\\u##0000", ""), str, UnicodeString("FAIL", ""));
|
|
|
|
expectPattern(*trans1, UnicodeString("\\*0000", ""), str, UnicodeString("*0061*0062*004B*0066", ""));
|
|
|
|
expectPattern(*trans1, UnicodeString("\\u####", ""), str, UnicodeString("FAIL", ""));
|
|
|
|
|
|
|
|
delete trans1;
|
|
|
|
delete transdefault;
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
void UniToHexTransliteratorTest::TestSimpleTransliterate(){
|
2000-08-14 21:42:36 +00:00
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
|
|
|
UnicodeString pattern1("\\\\U+0000", "");
|
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator(pattern1, TRUE, NULL, status);
|
|
|
|
if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern =" + pattern1);
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UTransPosition index={1,5,2,5};
|
|
|
|
UnicodeString source("Hello");
|
|
|
|
UnicodeString rsource(source);
|
|
|
|
UnicodeString expected("He\\U+006C\\U+006C\\U+006F", "");
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(trans1, rsource, index, FALSE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(trans1->getID() + ":handleTransliterator ", source + "-->" + rsource, rsource==expected, expected);
|
2000-03-28 23:23:48 +00:00
|
|
|
delete trans1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniToHexTransliteratorTest::TestTransliterate(){
|
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeString Data[]={
|
|
|
|
//pattern, source, index.contextStart, index.contextLimit, index.start, expectedResult, expectedResult using filter(a, b)
|
|
|
|
UnicodeString("U+##00", ""), UnicodeString("abc", ""), "1", "3", "2", UnicodeString("abU+63", ""), UnicodeString("abc", ""),
|
|
|
|
UnicodeString("\\\\u0000", ""), UnicodeString("abc", ""), "1", "2", "1", UnicodeString("a\\u0062c", ""), UnicodeString("a\\u0062c", ""),
|
|
|
|
UnicodeString("Uni0000", ""), UnicodeString("abc", ""), "1", "3", "2", UnicodeString("abUni0063", ""), UnicodeString("abc", ""),
|
|
|
|
UnicodeString("U[0000]", ""), UnicodeString("hello", ""), "0", "4", "2", UnicodeString("heU[006C]U[006C]o", ""), UnicodeString("heU[006C]U[006C]o", ""),
|
|
|
|
UnicodeString("prefix-0000-suffix", ""), UnicodeString("abc", ""), "1", "3", "1", UnicodeString("aprefix-0062-suffixprefix-0063-suffix", ""), UnicodeString("aprefix-0062-suffixc", ""),
|
|
|
|
UnicodeString("*##00*", ""), UnicodeString("hellothere", ""), "1", "8", "4", UnicodeString("hell*6F**74**68**65*re", ""), UnicodeString("hell*6F**74**68**65*re", ""),
|
|
|
|
|
|
|
|
};
|
2000-12-21 01:18:01 +00:00
|
|
|
uint32_t i;
|
2000-08-14 21:42:36 +00:00
|
|
|
for(i=0;i<sizeof(Data)/sizeof(Data[0]);i=i+7){
|
|
|
|
UnicodeToHexTransliterator *trans1=new UnicodeToHexTransliterator(Data[i+0], TRUE, NULL, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern =" + Data[i+0]);
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
expectTranslit(*trans1, "", Data[i+1], getInt(Data[i+2]), getInt(Data[i+3]), getInt(Data[i+4]), Data[i+5] );
|
|
|
|
delete trans1;
|
|
|
|
UnicodeToHexTransliterator *trans2=new UnicodeToHexTransliterator(Data[i+0], TRUE, new TestUniFilter, status);
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("UnicodeToHexTransliterator construction failed with pattern=" + Data[i+0] + "with filter(a,c)" );
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
expectTranslit(*trans2, " with filter(a,A,c,C)", Data[i+1], getInt(Data[i+2]), getInt(Data[i+3]), getInt(Data[i+4]), Data[i+6] );
|
|
|
|
delete trans2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
// Support methods
|
|
|
|
//======================================================================
|
|
|
|
|
|
|
|
void UniToHexTransliteratorTest::expectTranslit(const UnicodeToHexTransliterator& t,
|
2000-08-14 21:42:36 +00:00
|
|
|
const UnicodeString& message,
|
|
|
|
const UnicodeString& source,
|
|
|
|
int32_t start, int32_t limit, int32_t cursor,
|
|
|
|
const UnicodeString& expectedResult){
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
UTransPosition _index;
|
2000-08-12 01:24:04 +00:00
|
|
|
_index.contextStart=start;
|
|
|
|
_index.contextLimit= limit;
|
|
|
|
_index.start = cursor;
|
|
|
|
_index.limit = limit;
|
2000-06-27 19:06:15 +00:00
|
|
|
UTransPosition index = _index;
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeString rsource(source);
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(&t, rsource, index, FALSE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + ":handleTransliterator(increment=FALSE) " + message, source + "-->" + rsource, rsource==expectedResult, expectedResult);
|
|
|
|
|
|
|
|
UnicodeString rsource2(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(&t, rsource2, index, TRUE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + ":handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
/*ceates a copy constructor and checks the transliteration*/
|
2000-08-14 21:42:36 +00:00
|
|
|
UnicodeToHexTransliterator *copy=new UnicodeToHexTransliterator(t);
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(copy, rsource2, index, FALSE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "COPY:handleTransliterator(increment=FALSE) " + message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
|
|
|
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(copy, rsource2, index, TRUE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "COPY:handleTransliterator(increment=TRUE) " + message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
2000-03-28 23:23:48 +00:00
|
|
|
delete copy;
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
/*creates a clone and tests transliteration*/
|
|
|
|
UnicodeToHexTransliterator *clone=(UnicodeToHexTransliterator*)t.clone();
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(clone, rsource2, index, FALSE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "CLONE:handleTransliterator(increment=FALSE) "+ message,source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
|
|
|
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(clone, rsource2, index, TRUE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "CLONE:handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
2001-10-03 18:05:44 +00:00
|
|
|
delete clone;
|
2000-08-14 21:42:36 +00:00
|
|
|
|
|
|
|
/*Uses the assignment operator to create a transliterator and tests transliteration*/
|
|
|
|
UnicodeToHexTransliterator equal=t;
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(&equal, rsource2, index, FALSE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "=OPERATOR:handleTransliterator(increment=FALSE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
|
|
|
|
|
|
|
rsource2.remove();
|
|
|
|
rsource2.append(source);
|
|
|
|
index=_index;
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(&equal, rsource2, index, TRUE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + "=OPERATOR:handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void UniToHexTransliteratorTest::expectPattern(UnicodeToHexTransliterator& t,
|
2000-08-14 21:42:36 +00:00
|
|
|
const UnicodeString& pattern,
|
|
|
|
const UnicodeString& source,
|
|
|
|
const UnicodeString& expectedResult){
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
UErrorCode status=U_ZERO_ERROR;
|
2000-08-14 21:42:36 +00:00
|
|
|
t.applyPattern(pattern, status);
|
|
|
|
if(expectedResult == "FAIL"){
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
logln("OK: calling applyPattern() with illegal pattern failed as expected. Error=" + (UnicodeString)u_errorName(status));
|
|
|
|
status=U_ZERO_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2000-03-28 23:23:48 +00:00
|
|
|
else{
|
2000-08-14 21:42:36 +00:00
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("Error: applyPattern() failed with pattern =" + pattern + "--->" + (UnicodeString)u_errorName(status));
|
|
|
|
return;
|
|
|
|
}else {
|
|
|
|
if(t.toPattern() != pattern) {
|
|
|
|
errln("Error: applyPattern or toPatten failed. Expected: " + pattern + "Got: " + t.toPattern());
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
logln("OK: applyPattern passed. Testing transliteration");
|
|
|
|
expect(t, (UnicodeString)" with pattern "+pattern, source, expectedResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-28 23:23:48 +00:00
|
|
|
}
|
|
|
|
void UniToHexTransliteratorTest::expect(const UnicodeToHexTransliterator& t,
|
2000-08-14 21:42:36 +00:00
|
|
|
const UnicodeString& message,
|
2000-03-28 23:23:48 +00:00
|
|
|
const UnicodeString& source,
|
|
|
|
const UnicodeString& expectedResult) {
|
|
|
|
|
|
|
|
UnicodeString rsource(source);
|
|
|
|
t.transliterate(rsource);
|
|
|
|
expectAux(t.getID() + ":Replaceable " + message, source + "->" + rsource, rsource==expectedResult, expectedResult);
|
|
|
|
|
2000-08-14 21:42:36 +00:00
|
|
|
// Test handleTransliterate (incremental) transliteration --
|
2000-03-28 23:23:48 +00:00
|
|
|
rsource.remove();
|
2000-08-14 21:42:36 +00:00
|
|
|
rsource.append(source);
|
2000-08-12 01:24:04 +00:00
|
|
|
UTransPosition index;
|
|
|
|
index.contextStart=0;
|
|
|
|
index.contextLimit = source.length();
|
|
|
|
index.start =0;
|
|
|
|
index.limit=source.length();
|
2001-07-17 23:39:00 +00:00
|
|
|
pseudoHandleTransliterate(&t, rsource, index, TRUE);
|
2000-08-14 21:42:36 +00:00
|
|
|
expectAux(t.getID() + ":handleTransliterate " + message, source + "->" + rsource, rsource==expectedResult, expectedResult);
|
2000-03-28 23:23:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
void UniToHexTransliteratorTest::expectAux(const UnicodeString& tag,
|
2000-05-18 22:08:39 +00:00
|
|
|
const UnicodeString& summary, UBool pass,
|
2000-03-28 23:23:48 +00:00
|
|
|
const UnicodeString& expectedResult) {
|
|
|
|
if (pass) {
|
|
|
|
logln(UnicodeString("(")+tag+") " + prettify(summary));
|
|
|
|
} else {
|
|
|
|
errln(UnicodeString("FAIL: (")+tag+") "
|
|
|
|
+ prettify(summary)
|
|
|
|
+ ", expected " + prettify(expectedResult));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|