2004-11-01 18:56:24 +00:00
/************************************************************************
2000-03-10 00:42:27 +00:00
* COPYRIGHT :
2009-06-12 19:34:21 +00:00
* Copyright ( c ) 2000 - 2009 , International Business Machines Corporation
2004-11-01 18:56:24 +00:00
* and others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-03-10 00:42:27 +00:00
/************************************************************************
2000-01-18 02:34:41 +00:00
* Date Name Description
* 1 / 03 / 2000 Madhu Creation .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-09-21 00:43:14 +00:00
# include "unicode/utypes.h"
# if !UCONFIG_NO_TRANSLITERATION
2000-01-18 02:34:41 +00:00
# include "ittrans.h"
# include "transapi.h"
# include "unicode/utypes.h"
# include "unicode/translit.h"
# include "unicode/unifilt.h"
2003-02-14 01:44:49 +00:00
# include "cpdtrans.h"
2000-01-18 02:34:41 +00:00
# include <string.h>
# include <stdio.h>
2000-08-24 01:12:12 +00:00
# include <stdlib.h>
2000-01-18 02:34:41 +00:00
# include "unicode/rep.h"
2001-08-31 18:02:09 +00:00
# include "unicode/locid.h"
2003-12-09 01:41:43 +00:00
# include "unicode/uniset.h"
2004-08-02 20:06:55 +00:00
2000-01-18 02:34:41 +00:00
int32_t getInt ( UnicodeString str )
{
2000-04-04 16:30:21 +00:00
char buffer [ 20 ] ;
2000-08-10 00:28:31 +00:00
int len = str . length ( ) ;
2000-04-04 16:30:21 +00:00
if ( len > = 20 ) {
len = 19 ;
}
str . extract ( 0 , len , buffer , " " ) ;
buffer [ len ] = 0 ;
2000-08-10 00:28:31 +00:00
return atoi ( buffer ) ;
2000-01-18 02:34:41 +00:00
}
2000-04-04 16:30:21 +00:00
2000-01-18 02:34:41 +00:00
//---------------------------------------------
// runIndexedTest
//---------------------------------------------
2001-10-26 05:42:31 +00:00
void
TransliteratorAPITest : : runIndexedTest ( int32_t index , UBool exec ,
const char * & name , char * /*par*/ ) {
2000-01-18 02:34:41 +00:00
switch ( index ) {
2001-10-26 05:42:31 +00:00
TESTCASE ( 0 , TestgetInverse ) ;
TESTCASE ( 1 , TestgetID ) ;
TESTCASE ( 2 , TestGetDisplayName ) ;
TESTCASE ( 3 , TestTransliterate1 ) ;
TESTCASE ( 4 , TestTransliterate2 ) ;
TESTCASE ( 5 , TestTransliterate3 ) ;
TESTCASE ( 6 , TestSimpleKeyboardTransliterator ) ;
TESTCASE ( 7 , TestKeyboardTransliterator1 ) ;
TESTCASE ( 8 , TestKeyboardTransliterator2 ) ;
TESTCASE ( 9 , TestKeyboardTransliterator3 ) ;
TESTCASE ( 10 , TestGetAdoptFilter ) ;
TESTCASE ( 11 , TestClone ) ;
TESTCASE ( 12 , TestNullTransliterator ) ;
TESTCASE ( 13 , TestRegisterUnregister ) ;
2005-01-08 06:50:46 +00:00
TESTCASE ( 14 , TestUnicodeFunctor ) ;
2001-10-26 05:42:31 +00:00
default : name = " " ; break ;
2000-01-18 02:34:41 +00:00
}
}
void TransliteratorAPITest : : TestgetID ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString trans = " Latin-Greek " ;
UnicodeString ID ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
UErrorCode status = U_ZERO_ERROR ;
Transliterator * t = Transliterator : : createInstance ( trans , UTRANS_FORWARD , parseError , status ) ;
if ( t = = 0 | | U_FAILURE ( status ) ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: construction of Latin-Greek - %s " , u_errorName ( status ) ) ;
2001-10-03 18:05:44 +00:00
return ;
2001-08-31 03:24:28 +00:00
} else {
2000-08-10 00:28:31 +00:00
ID = t - > getID ( ) ;
if ( ID ! = trans )
2000-01-18 02:34:41 +00:00
errln ( " FAIL: getID returned " + ID + " instead of Latin-Greek " ) ;
2001-10-03 18:05:44 +00:00
delete t ;
2000-08-10 00:28:31 +00:00
}
int i ;
for ( i = 0 ; i < Transliterator : : countAvailableIDs ( ) ; i + + ) {
2001-11-28 03:51:58 +00:00
status = U_ZERO_ERROR ;
2001-10-03 18:05:44 +00:00
ID = ( UnicodeString ) Transliterator : : getAvailableID ( i ) ;
2005-01-08 06:50:46 +00:00
if ( ID . indexOf ( " Thai " ) > - 1 ) {
2004-08-02 20:06:55 +00:00
continue ;
}
2001-10-03 18:05:44 +00:00
t = Transliterator : : createInstance ( ID , UTRANS_FORWARD , parseError , status ) ;
if ( t = = 0 ) {
errln ( " FAIL: " + ID ) ;
continue ;
}
if ( ID ! = t - > getID ( ) )
errln ( " FAIL: getID() returned " + t - > getID ( ) + " instead of " + ID ) ;
delete t ;
2000-08-10 00:28:31 +00:00
}
ID = ( UnicodeString ) Transliterator : : getAvailableID ( i ) ;
if ( ID ! = ( UnicodeString ) Transliterator : : getAvailableID ( 0 ) ) {
errln ( " FAIL: calling getAvailableID(index > coundAvailableIDs) should make index=0 \n " ) ;
}
2000-01-18 02:34:41 +00:00
2001-08-31 03:24:28 +00:00
Transliterator * t1 = Transliterator : : createInstance ( " Latin-Devanagari " , UTRANS_FORWARD , parseError , status ) ;
2001-10-26 05:42:31 +00:00
Transliterator * t2 = Transliterator : : createInstance ( " Latin-Greek " , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t1 = = 0 | | t2 = = 0 ) {
errln ( " FAIL: construction " ) ;
return ;
}
Transliterator * t3 = t1 - > clone ( ) ;
Transliterator * t4 = t2 - > clone ( ) ;
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
if ( t1 - > getID ( ) ! = t3 - > getID ( ) | | t2 - > getID ( ) ! = t4 - > getID ( ) | |
t1 - > getID ( ) = = t4 - > getID ( ) | | t2 - > getID ( ) = = t3 - > getID ( ) | |
t1 - > getID ( ) = = t4 - > getID ( ) )
errln ( " FAIL: getID or clone failed " ) ;
2000-01-18 02:34:41 +00:00
2001-08-31 03:24:28 +00:00
Transliterator * t5 = Transliterator : : createInstance ( " Latin-Devanagari " , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t5 = = 0 )
errln ( " FAIL: construction " ) ;
2007-08-31 04:58:52 +00:00
else if ( t1 - > getID ( ) ! = t5 - > getID ( ) | | t5 - > getID ( ) ! = t3 - > getID ( ) | | t1 - > getID ( ) ! = t3 - > getID ( ) )
2000-08-10 00:28:31 +00:00
errln ( " FAIL: getID or clone failed " ) ;
2000-08-14 21:42:36 +00:00
2000-07-13 01:05:50 +00:00
2000-08-10 00:28:31 +00:00
delete t1 ;
delete t2 ;
delete t3 ;
delete t4 ;
delete t5 ;
}
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestgetInverse ( ) {
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
2001-10-26 05:42:31 +00:00
Transliterator * t1 = Transliterator : : createInstance ( " Katakana-Latin " , UTRANS_FORWARD , parseError , status ) ;
Transliterator * invt1 = Transliterator : : createInstance ( " Latin-Katakana " , UTRANS_FORWARD , parseError , status ) ;
2001-08-31 03:24:28 +00:00
Transliterator * t2 = Transliterator : : createInstance ( " Latin-Devanagari " , UTRANS_FORWARD , parseError , status ) ;
Transliterator * invt2 = Transliterator : : createInstance ( " Devanagari-Latin " , UTRANS_FORWARD , parseError , status ) ;
2000-08-14 21:42:36 +00:00
if ( t1 = = 0 | | invt1 = = 0 | | t2 = = 0 | | invt2 = = 0 ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: in instantiation - %s " , u_errorName ( status ) ) ;
2000-08-14 21:42:36 +00:00
return ;
}
2000-01-18 02:34:41 +00:00
2001-08-31 03:24:28 +00:00
Transliterator * inverse1 = t1 - > createInverse ( status ) ;
Transliterator * inverse2 = t2 - > createInverse ( status ) ;
2000-08-14 21:42:36 +00:00
if ( inverse1 - > getID ( ) ! = invt1 - > getID ( ) | | inverse2 - > getID ( ) ! = invt2 - > getID ( )
| | inverse1 - > getID ( ) = = invt2 - > getID ( ) | | inverse2 - > getID ( ) = = invt1 - > getID ( ) )
errln ( " FAIL: getInverse() " ) ;
2000-01-18 02:34:41 +00:00
2000-08-14 21:42:36 +00:00
UnicodeString TransID [ ] = {
" Halfwidth-Fullwidth " ,
" Fullwidth-Halfwidth " ,
" Greek-Latin " ,
" Latin-Greek " ,
2001-10-26 05:42:31 +00:00
//"Arabic-Latin", // removed in 2.0
//"Latin-Arabic",
" Katakana-Latin " ,
" Latin-Katakana " ,
//"Hebrew-Latin", // removed in 2.0
//"Latin-Hebrew",
2000-08-14 21:42:36 +00:00
" Cyrillic-Latin " ,
" Latin-Cyrillic " ,
" Devanagari-Latin " ,
" Latin-Devanagari " ,
2001-08-01 17:37:08 +00:00
" Any-Hex " ,
" Hex-Any "
2000-08-14 21:42:36 +00:00
} ;
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( TransID ) / sizeof ( TransID [ 0 ] ) ; i = i + 2 ) {
2001-10-03 18:05:44 +00:00
Transliterator * trans1 = Transliterator : : createInstance ( TransID [ i ] , UTRANS_FORWARD , parseError , status ) ;
2000-08-14 21:42:36 +00:00
if ( t1 = = 0 ) {
errln ( " FAIL: in instantiation for " + TransID [ i ] ) ;
continue ;
2000-01-18 02:34:41 +00:00
}
2001-10-03 18:05:44 +00:00
Transliterator * inver1 = trans1 - > createInverse ( status ) ;
if ( inver1 - > getID ( ) ! = TransID [ i + 1 ] )
errln ( " FAIL :getInverse() for " + TransID [ i ] + " returned " + inver1 - > getID ( ) + " instead of " + TransID [ i + 1 ] ) ;
delete inver1 ;
delete trans1 ;
2000-08-14 21:42:36 +00:00
}
delete t1 ;
delete t2 ;
delete invt1 ;
delete invt2 ;
2001-10-03 18:05:44 +00:00
delete inverse1 ;
delete inverse2 ;
2000-08-14 21:42:36 +00:00
}
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestClone ( ) {
Transliterator * t1 , * t2 , * t3 , * t4 ;
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
t1 = Transliterator : : createInstance ( " Latin-Devanagari " , UTRANS_FORWARD , parseError , status ) ;
2001-10-26 05:42:31 +00:00
t2 = Transliterator : : createInstance ( " Latin-Greek " , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t1 = = 0 | | t2 = = 0 ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: construction - %s " , u_errorName ( status ) ) ;
2000-08-10 00:28:31 +00:00
return ;
}
t3 = t1 - > clone ( ) ;
t4 = t2 - > clone ( ) ;
if ( t1 - > getID ( ) ! = t3 - > getID ( ) | | t2 - > getID ( ) ! = t4 - > getID ( ) )
errln ( " FAIL: clone or getID failed " ) ;
if ( t1 - > getID ( ) = = t4 - > getID ( ) | | t2 - > getID ( ) = = t3 - > getID ( ) | | t1 - > getID ( ) = = t4 - > getID ( ) )
errln ( " FAIL: clone or getID failed " ) ;
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
delete t1 ;
delete t2 ;
2001-10-03 18:05:44 +00:00
delete t3 ;
delete t4 ;
2000-01-18 02:34:41 +00:00
}
void TransliteratorAPITest : : TestGetDisplayName ( ) {
UnicodeString dispNames [ ] = {
2000-08-10 00:28:31 +00:00
//ID, displayName
2001-10-26 05:42:31 +00:00
//"CurlyQuotes-StraightQuotes" ,"CurlyQuotes to StraightQuotes",
2001-08-01 17:37:08 +00:00
" Any-Hex " , " Any to Hex Escape " ,
2000-08-10 00:28:31 +00:00
" Halfwidth-Fullwidth " , " Halfwidth to Fullwidth " ,
2001-10-26 05:42:31 +00:00
//"Latin-Arabic" ,"Latin to Arabic" ,
2000-08-10 00:28:31 +00:00
" Latin-Devanagari " , " Latin to Devanagari " ,
" Greek-Latin " , " Greek to Latin " ,
2001-10-26 05:42:31 +00:00
//"Arabic-Latin" ,"Arabic to Latin" ,
2001-08-01 17:37:08 +00:00
" Hex-Any " , " Hex Escape to Any " ,
2000-08-10 00:28:31 +00:00
" Cyrillic-Latin " , " Cyrillic to Latin " ,
" Latin-Greek " , " Latin to Greek " ,
2001-10-26 05:42:31 +00:00
" Latin-Katakana " , " Latin to Katakana " ,
//"Latin-Hebrew" ,"Latin to Hebrew" ,
" Katakana-Latin " , " Katakana to Latin "
2000-08-10 00:28:31 +00:00
} ;
UnicodeString name = " " ;
2000-08-14 21:42:36 +00:00
Transliterator * t ;
2000-08-10 00:28:31 +00:00
UnicodeString message ;
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
2002-10-02 17:19:18 +00:00
# if UCONFIG_NO_FORMATTING
logln ( " Skipping, UCONFIG_NO_FORMATTING is set \n " ) ;
return ;
# else
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( dispNames ) / sizeof ( dispNames [ 0 ] ) ; i = i + 2 ) {
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( dispNames [ i + 0 ] , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t = = 0 ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: construction: " + dispNames [ i + 0 ] + " - " + u_errorName ( status ) ) ;
2001-08-31 03:24:28 +00:00
status = U_ZERO_ERROR ;
2000-08-10 00:28:31 +00:00
continue ;
2000-01-18 02:34:41 +00:00
}
2000-08-10 00:28:31 +00:00
t - > getDisplayName ( t - > getID ( ) , name ) ;
message = " Display name for ID: " + t - > getID ( ) ;
// doTest(message, name, dispNames[i+1]); //!!! This will obviously fail for any locale other than english and its children!!!
name = " " ;
2002-08-21 00:16:30 +00:00
t - > getDisplayName ( t - > getID ( ) , Locale : : getUS ( ) , name ) ;
2000-08-10 00:28:31 +00:00
message . remove ( ) ;
message . append ( " Display name for on english locale ID: " ) ;
message . append ( t - > getID ( ) ) ;
// message="Display name for on english locale ID:" + t->getID();
doTest ( message , name , dispNames [ i + 1 ] ) ;
name = " " ;
2000-08-14 21:42:36 +00:00
delete t ;
2000-08-10 00:28:31 +00:00
}
2002-10-02 17:19:18 +00:00
# endif
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
}
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestTransliterate1 ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString Data [ ] = {
//ID, input string, transliterated string
2001-08-31 03:24:28 +00:00
" Any-Hex " , " hello " , UnicodeString ( " \\ u0068 \\ u0065 \\ u006C \\ u006C \\ u006F " , " " ) ,
2001-08-01 17:37:08 +00:00
" Hex-Any " , UnicodeString ( " \\ u0068 \\ u0065 \\ u006C \\ u006C \\ u006F " , " " ) , " hello " ,
2001-11-11 02:45:34 +00:00
" Latin-Devanagari " , CharsToUnicodeString ( " bha \\ u0304rata " ) , CharsToUnicodeString ( " \\ u092D \\ u093E \\ u0930 \\ u0924 " ) ,
2001-10-04 01:42:16 +00:00
" 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 " ) ,
2001-08-31 03:24:28 +00:00
2008-06-17 00:55:35 +00:00
" Devanagari-Latin " , CharsToUnicodeString ( " \\ u092D \\ u093E \\ u0930 \\ u0924 " ) , CharsToUnicodeString ( " bh \\ u0101rata " ) ,
2000-08-10 00:28:31 +00:00
// "Contracted-Expanded", CharsToUnicodeString("\\u00C0\\u00C1\\u0042"), CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042") ,
// "Expanded-Contracted", CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042"), CharsToUnicodeString("\\u00C0\\u00C1\\u0042") ,
2001-10-26 05:42:31 +00:00
//"Latin-Arabic", "aap", CharsToUnicodeString("\\u0627\\u06A4") ,
//"Arabic-Latin", CharsToUnicodeString("\\u0627\\u06A4"), "aap"
2000-08-10 00:28:31 +00:00
} ;
UnicodeString gotResult ;
UnicodeString temp ;
UnicodeString message ;
2000-08-14 21:42:36 +00:00
Transliterator * t ;
2000-08-10 00:28:31 +00:00
logln ( " Testing transliterate " ) ;
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
2000-08-10 00:28:31 +00:00
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( Data ) / sizeof ( Data [ 0 ] ) ; i = i + 3 ) {
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( Data [ i + 0 ] , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t = = 0 ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: construction: " + Data [ i + 0 ] + " Error: " + u_errorName ( status ) ) ;
dataerrln ( " PreContext: " + prettify ( parseError . preContext ) + " PostContext: " + prettify ( parseError . postContext ) ) ;
2001-08-31 03:24:28 +00:00
status = U_ZERO_ERROR ;
2000-08-10 00:28:31 +00:00
continue ;
}
gotResult = Data [ i + 1 ] ;
t - > transliterate ( gotResult ) ;
2001-08-31 03:24:28 +00:00
message = t - > getID ( ) + " ->transliterate(UnicodeString, UnicodeString) for \n \t Source: " + prettify ( Data [ i + 1 ] ) ;
2000-08-10 00:28:31 +00:00
doTest ( message , gotResult , Data [ i + 2 ] ) ;
//doubt here
temp = Data [ i + 1 ] ;
t - > transliterate ( temp ) ;
message . remove ( ) ;
message . append ( t - > getID ( ) ) ;
message . append ( " ->transliterate(Replaceable) for \n \t Source: " ) ;
message . append ( Data [ i ] [ 1 ] ) ;
doTest ( message , temp , Data [ i + 2 ] ) ;
2000-01-18 02:34:41 +00:00
2003-12-09 01:41:43 +00:00
callEverything ( t , __LINE__ ) ;
2000-08-14 21:42:36 +00:00
delete t ;
}
2000-01-18 02:34:41 +00:00
}
2000-08-10 00:28:31 +00:00
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestTransliterate2 ( ) {
2000-08-10 00:28:31 +00:00
//testing tranliterate(String text, int start, int limit, StringBuffer result)
UnicodeString Data2 [ ] = {
//ID, input string, start, limit, transliterated string
2001-08-01 17:37:08 +00:00
" Any-Hex " , " hello! How are you? " , " 0 " , " 5 " , UnicodeString ( " \\ u0068 \\ u0065 \\ u006C \\ u006C \\ u006F " , " " ) , UnicodeString ( " \\ u0068 \\ u0065 \\ u006C \\ u006C \\ u006F! How are you? " , " " ) ,
" Any-Hex " , " hello! How are you? " , " 7 " , " 12 " , UnicodeString ( " \\ u0048 \\ u006F \\ u0077 \\ u0020 \\ u0061 " , " " ) , UnicodeString ( " hello! \\ u0048 \\ u006F \\ u0077 \\ u0020 \\ u0061re you? " , " " ) ,
" Hex-Any " , CharsToUnicodeString ( " \\ u0068 \\ u0065 \\ u006C \\ u006C \\ u006F \\ u0021 \\ u0020 " ) , " 0 " , " 5 " , " hello " , " hello! " ,
2000-08-10 00:28:31 +00:00
// "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 " ) ,
2008-06-17 00:55:35 +00:00
" Devanagari-Latin " , CharsToUnicodeString ( " \\ u092D \\ u093E \\ u0930 \\ u0924 " ) , " 1 " , " 2 " , CharsToUnicodeString ( " \\ u0314 \\ u0101 " ) , CharsToUnicodeString ( " \\ u092D \\ u0314 \\ u0101 \\ u0930 \\ u0924 " )
2000-08-10 00:28:31 +00:00
} ;
logln ( " \n Testing transliterate(String, int, int, StringBuffer) " ) ;
Transliterator * t ;
UnicodeString gotResBuf ;
UnicodeString temp ;
int32_t start , limit ;
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( Data2 ) / sizeof ( Data2 [ 0 ] ) ; i = i + 6 ) {
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( Data2 [ i + 0 ] , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t = = 0 ) {
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL: construction: " + prettify ( Data2 [ i + 0 ] ) + " - " + u_errorName ( status ) ) ;
2000-08-10 00:28:31 +00:00
continue ;
}
start = getInt ( Data2 [ i + 2 ] ) ;
limit = getInt ( Data2 [ i + 3 ] ) ;
Data2 [ i + 1 ] . extractBetween ( start , limit , gotResBuf ) ;
t - > transliterate ( gotResBuf ) ;
// errln("FAIL: calling transliterate on " + t->getID());
2001-08-31 03:24:28 +00:00
doTest ( t - > getID ( ) + " .transliterate(UnicodeString, int32_t, int32_t, UnicodeString):( " + start + " , " + limit + " ) for \n \t source: " + prettify ( Data2 [ i + 1 ] ) , gotResBuf , Data2 [ i + 4 ] ) ;
2000-08-10 00:28:31 +00:00
temp = Data2 [ i + 1 ] ;
t - > transliterate ( temp , start , limit ) ;
2001-08-31 03:24:28 +00:00
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 ;
2003-12-09 01:41:43 +00:00
callEverything ( t , __LINE__ ) ;
2001-10-03 18:05:44 +00:00
delete t ;
t = NULL ;
2000-08-10 00:28:31 +00:00
}
2000-01-18 02:34:41 +00:00
2001-08-31 03:24:28 +00:00
status = U_ZERO_ERROR ;
2000-08-10 00:28:31 +00:00
logln ( " \n Try calling transliterate with illegal start and limit values " ) ;
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2003-06-06 04:54:34 +00:00
if ( U_FAILURE ( status ) ) {
errln ( " Error creating transliterator %s " , u_errorName ( status ) ) ;
delete t ;
return ;
}
2000-08-10 00:28:31 +00:00
gotResBuf = temp = " try start greater than limit " ;
t - > transliterate ( gotResBuf , 10 , 5 ) ;
2003-12-09 01:41:43 +00:00
if ( gotResBuf = = temp ) {
2000-08-10 00:28:31 +00:00
logln ( " OK: start greater than limit value handled correctly " ) ;
2003-12-09 01:41:43 +00:00
} else {
2000-08-10 00:28:31 +00:00
errln ( " FAIL: start greater than limit value returned " + gotResBuf ) ;
2003-12-09 01:41:43 +00:00
}
2000-01-18 02:34:41 +00:00
2003-12-09 01:41:43 +00:00
callEverything ( t , __LINE__ ) ;
2000-08-10 00:28:31 +00:00
delete t ;
2000-01-18 02:34:41 +00:00
}
void TransliteratorAPITest : : TestTransliterate3 ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString rs = " This is the replaceable String " ;
UnicodeString Data [ ] = {
" 0 " , " 0 " , " This is the replaceable String " ,
" 2 " , " 3 " , UnicodeString ( " Th \\ u0069s is the replaceable String " , " " ) ,
" 21 " , " 23 " , UnicodeString ( " Th \\ u0069s is the repl \\ u0061 \\ u0063eable String " , " " ) ,
" 14 " , " 17 " , UnicodeString ( " Th \\ u0069s is t \\ u0068 \\ u0065 \\ u0020repl \\ u0061 \\ u0063eable String " , " " ) ,
} ;
int start , limit ;
UnicodeString message ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
UErrorCode status = U_ZERO_ERROR ;
Transliterator * t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2003-06-06 04:54:34 +00:00
if ( U_FAILURE ( status ) ) {
errln ( " Error creating transliterator %s " , u_errorName ( status ) ) ;
delete t ;
return ;
}
2000-08-10 00:28:31 +00:00
if ( t = = 0 )
errln ( " FAIL : construction " ) ;
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( Data ) / sizeof ( Data [ 0 ] ) ; i = i + 3 ) {
2000-08-10 00:28:31 +00:00
start = getInt ( Data [ i + 0 ] ) ;
limit = getInt ( Data [ i + 1 ] ) ;
t - > transliterate ( rs , start , limit ) ;
message = t - > getID ( ) + " .transliterate(ReplaceableString, start, limit):( " + start + " , " + limit + " ): " ;
doTest ( message , rs , Data [ i + 2 ] ) ;
}
2001-10-03 18:05:44 +00:00
delete t ;
2000-01-18 02:34:41 +00:00
}
void TransliteratorAPITest : : TestSimpleKeyboardTransliterator ( ) {
2000-08-10 00:28:31 +00:00
logln ( " simple call to transliterate " ) ;
UErrorCode status = U_ZERO_ERROR ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
Transliterator * t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2001-09-17 20:59:42 +00:00
if ( t = = 0 ) {
2003-06-23 23:16:10 +00:00
UnicodeString context ;
if ( parseError . preContext [ 0 ] ) {
context + = ( UnicodeString ) " at " + parseError . preContext ;
}
if ( parseError . postContext [ 0 ] ) {
context + = ( UnicodeString ) " | " + parseError . postContext ;
}
2001-09-17 20:59:42 +00:00
errln ( ( UnicodeString ) " FAIL: can't create Any-Hex, " +
2003-06-23 23:16:10 +00:00
( UnicodeString ) u_errorName ( status ) + context ) ;
2001-09-17 20:59:42 +00:00
return ;
}
2000-08-10 00:28:31 +00:00
UTransPosition index = { 19 , 20 , 20 , 20 } ;
UnicodeString rs = " Transliterate this-'' " ;
UnicodeString insertion = " abc " ;
UnicodeString expected = UnicodeString ( " Transliterate this-' \\ u0061 \\ u0062 \\ u0063' " , " " ) ;
t - > transliterate ( rs , index , insertion , status ) ;
if ( U_FAILURE ( status ) )
errln ( " FAIL: " + t - > getID ( ) + " .translitere(Replaceable, int[], UnicodeString, UErrorCode)--> " + ( UnicodeString ) u_errorName ( status ) ) ;
t - > finishTransliteration ( rs , index ) ;
UnicodeString message = " transliterate " ;
doTest ( message , rs , expected ) ;
logln ( " try calling transliterate with invalid index values " ) ;
UTransPosition index1 [ ] = {
//START, LIMIT, CURSOR
{ 10 , 10 , 12 , 10 } , //invalid since CURSOR>LIMIT valid:-START <= CURSOR <= LIMIT
{ 17 , 16 , 17 , 17 } , //invalid since START>LIMIT valid:-0<=START<=LIMIT
{ - 1 , 16 , 14 , 16 } , //invalid since START<0
{ 3 , 50 , 2 , 50 } //invalid since LIMIT>text.length()
} ;
2000-08-29 23:53:37 +00:00
for ( uint32_t i = 0 ; i < sizeof ( index1 ) / sizeof ( index1 [ 0 ] ) ; i + + ) {
2000-08-10 00:28:31 +00:00
status = U_ZERO_ERROR ;
t - > transliterate ( rs , index1 [ i ] , insertion , status ) ;
if ( status = = U_ILLEGAL_ARGUMENT_ERROR )
logln ( " OK: invalid index values handled correctly " ) ;
else
errln ( " FAIL: invalid index values didn't throw U_ILLEGAL_ARGUMENT_ERROR throw " + ( UnicodeString ) u_errorName ( status ) ) ;
}
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
delete t ;
}
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestKeyboardTransliterator1 ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString Data [ ] = {
//insertion, buffer
" a " , UnicodeString ( " \\ u0061 " , " " ) ,
" bbb " , UnicodeString ( " \\ u0061 \\ u0062 \\ u0062 \\ u0062 " , " " ) ,
" ca " , UnicodeString ( " \\ u0061 \\ u0062 \\ u0062 \\ u0062 \\ u0063 \\ u0061 " , " " ) ,
" " , UnicodeString ( " \\ u0061 \\ u0062 \\ u0062 \\ u0062 \\ u0063 \\ u0061 \\ u0020 " , " " ) ,
" " , UnicodeString ( " \\ u0061 \\ u0062 \\ u0062 \\ u0062 \\ u0063 \\ u0061 \\ u0020 " , " " ) ,
" a " , UnicodeString ( " \\ u0061 " , " " ) ,
" b " , UnicodeString ( " \\ u0061 \\ u0062 " , " " ) ,
" z " , UnicodeString ( " \\ u0061 \\ u0062 \\ u007A " , " " ) ,
" " , UnicodeString ( " \\ u0061 \\ u0062 \\ u007A " , " " )
} ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
UErrorCode status = U_ZERO_ERROR ;
Transliterator * t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2003-06-06 04:54:34 +00:00
if ( U_FAILURE ( status ) ) {
errln ( " Error creating transliterator %s " , u_errorName ( status ) ) ;
delete t ;
return ;
}
2000-08-10 00:28:31 +00:00
//keyboardAux(t, Data);
UTransPosition index = { 0 , 0 , 0 , 0 } ;
UnicodeString s ;
2000-08-29 23:53:37 +00:00
uint32_t i ;
2000-08-10 00:28:31 +00:00
logln ( " Testing transliterate(Replaceable, int32_t, UnicodeString, UErrorCode) " ) ;
for ( i = 0 ; i < 10 ; i = i + 2 ) {
UnicodeString log ;
if ( Data [ i + 0 ] ! = " " ) {
log = s + " + " + Data [ i + 0 ] + " -> " ;
t - > transliterate ( s , index , Data [ i + 0 ] , status ) ;
if ( U_FAILURE ( status ) ) {
errln ( " FAIL: " + t - > getID ( ) + " .transliterate(Replaceable, int32_t[], UnicodeString, UErrorCode)--> " + ( UnicodeString ) u_errorName ( status ) ) ;
continue ;
2000-01-18 02:34:41 +00:00
}
2000-08-10 00:28:31 +00:00
} else {
log = s + " => " ;
t - > finishTransliteration ( s , index ) ;
}
// Show the start index '{' and the cursor '|'
displayOutput ( s , Data [ i + 1 ] , log , index ) ;
}
s = " " ;
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 ) {
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 ) )
errln ( " FAIL: " + t - > getID ( ) + " .transliterate(Replaceable, int32_t[], UChar, UErrorCode)--> " + ( UnicodeString ) u_errorName ( status ) ) ;
continue ;
} else {
log = s + " => " ;
t - > finishTransliteration ( s , index ) ;
}
// Show the start index '{' and the cursor '|'
displayOutput ( s , Data [ i + 1 ] , log , index ) ;
}
delete t ;
2000-01-18 02:34:41 +00:00
}
2000-08-10 00:28:31 +00:00
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestKeyboardTransliterator2 ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString Data [ ] = {
//insertion, buffer, index[START], index[LIMIT], index[CURSOR]
2001-08-01 17:37:08 +00:00
//data for Any-Hex
2000-08-10 00:28:31 +00:00
" abc " , UnicodeString ( " Initial String: add- \\ u0061 \\ u0062 \\ u0063- " , " " ) , " 19 " , " 20 " , " 20 " ,
" a " , UnicodeString ( " In \\ u0069 \\ u0061tial String: add- \\ u0061 \\ u0062 \\ u0063- " , " " ) , " 2 " , " 3 " , " 2 " ,
" b " , UnicodeString ( " \\ u0062In \\ u0069 \\ u0061tial String: add- \\ u0061 \\ u0062 \\ u0063- " , " " ) , " 0 " , " 0 " , " 0 " ,
2001-10-04 01:42:16 +00:00
" " , UnicodeString ( " \\ u0062In \\ u0069 \\ u0061tial String: add- \\ u0061 \\ u0062 \\ u0063- " , " " ) , " 0 " , " 0 " , " 0 " ,
2000-08-10 00:28:31 +00:00
//data for Latin-Devanagiri
2001-11-11 02:45:34 +00:00
CharsToUnicodeString ( " a \\ u0304 " ) , CharsToUnicodeString ( " Hindi - \\ u0906- " ) , " 6 " , " 7 " , " 6 " ,
CharsToUnicodeString ( " ma \\ u0304 " ) , CharsToUnicodeString ( " Hindi - \\ u0906 \\ u092E \\ u093E- " ) , " 7 " , " 8 " , " 7 " ,
CharsToUnicodeString ( " ra \\ u0304 " ) , CharsToUnicodeString ( " Hi \\ u0930 \\ u093Endi - \\ u0906 \\ u092E \\ u093E- " ) , " 1 " , " 2 " , " 2 " ,
2001-10-04 01:42:16 +00:00
CharsToUnicodeString ( " " ) , CharsToUnicodeString ( " Hi \\ u0930 \\ u093Endi - \\ u0906 \\ u092E \\ u093E- " ) , " 1 " , " 2 " , " 2 "
2000-08-10 00:28:31 +00:00
//data for contracted-Expanded
// CharsToUnicodeString("\\u00C1"), CharsToUnicodeString("Ad\\u0041\\u0301d here:"), "1", "2", "1" ,
// CharsToUnicodeString("\\u00C0"), CharsToUnicodeString("Ad\\u0041\\u0301d here:\\u0041\\u0300"), "11", "11", "11",
// "", CharsToUnicodeString("Ad\\u0041\\u0301d here:\\u0041\\u0300"), "11", "11", "11",
} ;
Transliterator * t ;
UnicodeString rs ;
UnicodeString dataStr ;
logln ( " Testing transliterate(Replaceable, int32_t, UnicodeString, UErrorCode) " ) ;
2001-08-31 03:24:28 +00:00
UErrorCode status = U_ZERO_ERROR ;
UParseError parseError ;
2000-08-10 00:28:31 +00:00
rs = " Initial String: add-- " ;
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t = = 0 )
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL : construction - %s " , u_errorName ( status ) ) ;
2000-08-10 00:28:31 +00:00
else {
keyboardAux ( t , Data , rs , 0 , 20 ) ;
delete t ;
}
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
rs = " Hindi -- " ;
2001-08-31 03:24:28 +00:00
t = Transliterator : : createInstance ( " Latin-Devanagari " , UTRANS_FORWARD , parseError , status ) ;
2000-08-10 00:28:31 +00:00
if ( t = = 0 )
2009-06-12 19:34:21 +00:00
dataerrln ( " FAIL : construction - %s " , u_errorName ( status ) ) ;
2000-08-10 00:28:31 +00:00
else
keyboardAux ( t , Data , rs , 20 , 40 ) ;
// rs="Add here:";
// t=Transliterator::createInstance("Contracted-Expanded");
// keyboardAux(t, Data, rs, 35, 55);
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
delete t ;
}
2000-01-18 02:34:41 +00:00
void TransliteratorAPITest : : TestKeyboardTransliterator3 ( ) {
2000-08-10 00:28:31 +00:00
UnicodeString s = " This is the main string " ;
UnicodeString Data [ ] = {
" 0 " , " 0 " , " 0 " , " This is the main string " ,
" 1 " , " 3 " , " 2 " , UnicodeString ( " Th \\ u0069s is the main string " , " " ) ,
" 20 " , " 21 " , " 20 " , UnicodeString ( " Th \\ u0069s is the mai \\ u006E string " , " " )
} ;
UErrorCode status = U_ZERO_ERROR ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
2000-08-10 00:28:31 +00:00
UTransPosition index = { 0 , 0 , 0 , 0 } ;
logln ( " Testing transliterate(Replaceable, int32_t, UErrorCode) " ) ;
2001-08-31 03:24:28 +00:00
Transliterator * t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2007-08-31 04:58:52 +00:00
if ( t = = 0 | | U_FAILURE ( status ) ) {
2003-06-06 04:54:34 +00:00
errln ( " Error creating transliterator %s " , u_errorName ( status ) ) ;
delete t ;
return ;
}
2000-12-21 01:18:01 +00:00
for ( uint32_t i = 0 ; i < sizeof ( Data ) / sizeof ( Data [ 0 ] ) ; i = i + 4 ) {
2000-08-10 00:28:31 +00:00
UnicodeString log ;
index . contextStart = getInt ( Data [ i + 0 ] ) ;
2000-06-27 20:07:59 +00:00
index . contextLimit = index . limit = getInt ( Data [ i + 1 ] ) ;
2000-06-27 19:34:03 +00:00
index . start = getInt ( Data [ i + 2 ] ) ;
2000-01-18 20:24:35 +00:00
t - > transliterate ( s , index , status ) ;
2000-01-18 20:04:30 +00:00
if ( U_FAILURE ( status ) ) {
2000-01-18 20:24:35 +00:00
errln ( " FAIL: " + t - > getID ( ) + " .transliterate(Replaceable, int32_t[], UErrorCode)--> " + ( UnicodeString ) u_errorName ( status ) ) ;
2000-08-10 00:28:31 +00:00
continue ;
}
t - > finishTransliteration ( s , index ) ;
log = s + " => " ;
// Show the start index '{' and the cursor '|'
2000-01-18 02:34:41 +00:00
displayOutput ( s , Data [ i + 3 ] , log , index ) ;
2000-08-10 00:28:31 +00:00
}
2000-01-18 02:34:41 +00:00
2000-08-10 00:28:31 +00:00
delete t ;
2000-01-18 02:34:41 +00:00
}
2000-06-27 18:20:33 +00:00
void TransliteratorAPITest : : TestNullTransliterator ( ) {
2007-06-11 02:32:47 +00:00
UErrorCode status = U_ZERO_ERROR ;
2000-06-27 18:20:33 +00:00
UnicodeString s ( " Transliterate using null transliterator " ) ;
2007-06-11 02:32:47 +00:00
Transliterator * nullTrans = Transliterator : : createInstance ( " Any-Null " , UTRANS_FORWARD , status ) ;
2000-06-27 18:20:33 +00:00
int32_t transLimit ;
int32_t start = 0 ;
int32_t limit = s . length ( ) ;
UnicodeString replaceable = s ;
transLimit = nullTrans - > transliterate ( replaceable , start , limit ) ;
if ( transLimit ! = limit ) {
errln ( " ERROR: NullTransliterator->transliterate() failed " ) ;
}
doTest ( ( UnicodeString ) " nulTrans->transliterate " , replaceable , s ) ;
replaceable . remove ( ) ;
replaceable . append ( s ) ;
2000-08-12 01:03:01 +00:00
UTransPosition index ;
index . contextStart = start ;
index . contextLimit = limit ;
index . start = 0 ;
index . limit = limit ;
2007-06-11 02:32:47 +00:00
nullTrans - > finishTransliteration ( replaceable , index ) ;
2000-06-27 19:34:03 +00:00
if ( index . start ! = limit ) {
2000-06-27 18:20:33 +00:00
errln ( " ERROR: NullTransliterator->handleTransliterate() failed " ) ;
}
doTest ( ( UnicodeString ) " NullTransliterator->handleTransliterate " , replaceable , s ) ;
2003-12-09 01:41:43 +00:00
callEverything ( nullTrans , __LINE__ ) ;
2000-07-13 01:05:50 +00:00
delete nullTrans ;
2000-06-27 18:20:33 +00:00
}
2000-07-24 17:09:07 +00:00
void TransliteratorAPITest : : TestRegisterUnregister ( ) {
UErrorCode status = U_ZERO_ERROR ;
/* Make sure it doesn't exist */
2007-06-11 02:32:47 +00:00
if ( Transliterator : : createInstance ( " TestA-TestB " , UTRANS_FORWARD , status ) ! = NULL ) {
2000-07-24 17:09:07 +00:00
errln ( " FAIL: TestA-TestB already registered \n " ) ;
return ;
}
/* Check inverse too
if ( Transliterator : : createInstance ( " TestA-TestB " ,
( UTransDirection ) UTRANS_REVERSE ) ! = NULL ) {
errln ( " FAIL: TestA-TestB inverse already registered \n " ) ;
return ;
}
*/
2001-08-31 03:24:28 +00:00
status = U_ZERO_ERROR ;
2000-07-24 17:09:07 +00:00
/* Create it */
2007-06-11 06:05:41 +00:00
UParseError parseError ;
Transliterator * t = Transliterator : : createFromRules ( " TestA-TestB " ,
2000-07-24 17:09:07 +00:00
" a<>b " ,
2007-06-11 06:05:41 +00:00
UTRANS_FORWARD , parseError ,
2000-07-24 17:09:07 +00:00
status ) ;
/* Register it */
2001-08-15 19:06:40 +00:00
Transliterator : : registerInstance ( t ) ;
2000-07-24 17:09:07 +00:00
/* Now check again -- should exist now*/
2007-06-11 02:32:47 +00:00
Transliterator * s = Transliterator : : createInstance ( " TestA-TestB " , UTRANS_FORWARD , status ) ;
2000-07-24 17:09:07 +00:00
if ( s = = NULL ) {
errln ( " FAIL: TestA-TestB not registered \n " ) ;
return ;
}
2003-12-09 01:41:43 +00:00
callEverything ( s , __LINE__ ) ;
callEverything ( t , __LINE__ ) ;
2000-07-24 17:09:07 +00:00
delete s ;
/* Check inverse too
s = Transliterator : : createInstance ( " TestA-TestB " ,
( UTransDirection ) UTRANS_REVERSE ) ;
if ( s = = NULL ) {
errln ( " FAIL: TestA-TestB inverse not registered \n " ) ;
return ;
}
delete s ;
*/
/*unregister the instance*/
Transliterator : : unregister ( " TestA-TestB " ) ;
/* now Make sure it doesn't exist */
2007-06-11 02:32:47 +00:00
if ( Transliterator : : createInstance ( " TestA-TestB " , UTRANS_FORWARD , status ) ! = NULL ) {
2000-07-24 17:09:07 +00:00
errln ( " FAIL: TestA-TestB isn't unregistered \n " ) ;
return ;
}
}
2002-12-16 19:51:39 +00:00
int gTestFilter1ClassID = 0 ;
int gTestFilter2ClassID = 0 ;
int gTestFilter3ClassID = 0 ;
2000-07-24 17:09:07 +00:00
2000-01-18 02:34:41 +00:00
/**
* Used by TestFiltering ( ) .
*/
class TestFilter1 : public UnicodeFilter {
2002-12-16 19:51:39 +00:00
UClassID getDynamicClassID ( ) const { return & gTestFilter1ClassID ; }
2002-02-07 01:07:55 +00:00
virtual UnicodeFunctor * clone ( ) const {
2000-01-18 02:34:41 +00:00
return new TestFilter1 ( * this ) ;
}
2001-07-25 19:11:02 +00:00
virtual UBool contains ( UChar32 c ) const {
2000-03-31 21:45:17 +00:00
if ( c = = 0x63 | | c = = 0x61 | | c = = 0x43 | | c = = 0x41 )
2000-01-18 02:34:41 +00:00
return FALSE ;
else
return TRUE ;
}
2002-08-28 17:17:04 +00:00
// Stubs
virtual UnicodeString & toPattern ( UnicodeString & result ,
2003-06-09 04:57:01 +00:00
UBool /*escapeUnprintable*/ ) const {
2002-08-28 17:17:04 +00:00
return result ;
}
2003-06-09 04:57:01 +00:00
virtual UBool matchesIndexValue ( uint8_t /*v*/ ) const {
2002-08-28 17:17:04 +00:00
return FALSE ;
}
2003-06-09 04:57:01 +00:00
virtual void addMatchSetTo ( UnicodeSet & /*toUnionTo*/ ) const { }
2000-01-18 02:34:41 +00:00
} ;
class TestFilter2 : public UnicodeFilter {
2002-12-16 19:51:39 +00:00
UClassID getDynamicClassID ( ) const { return & gTestFilter2ClassID ; }
2002-02-07 01:07:55 +00:00
virtual UnicodeFunctor * clone ( ) const {
2000-01-18 02:34:41 +00:00
return new TestFilter2 ( * this ) ;
}
2001-07-25 19:11:02 +00:00
virtual UBool contains ( UChar32 c ) const {
2000-03-31 21:45:17 +00:00
if ( c = = 0x65 | | c = = 0x6c )
2000-01-18 02:34:41 +00:00
return FALSE ;
else
return TRUE ;
}
2002-08-28 17:17:04 +00:00
// Stubs
virtual UnicodeString & toPattern ( UnicodeString & result ,
2003-06-09 04:57:01 +00:00
UBool /*escapeUnprintable*/ ) const {
2002-08-28 17:17:04 +00:00
return result ;
}
2003-06-09 04:57:01 +00:00
virtual UBool matchesIndexValue ( uint8_t /*v*/ ) const {
2002-08-28 17:17:04 +00:00
return FALSE ;
}
2003-06-09 04:57:01 +00:00
virtual void addMatchSetTo ( UnicodeSet & /*toUnionTo*/ ) const { }
2000-01-18 02:34:41 +00:00
} ;
class TestFilter3 : public UnicodeFilter {
2002-12-16 19:51:39 +00:00
UClassID getDynamicClassID ( ) const { return & gTestFilter3ClassID ; }
2002-02-07 01:07:55 +00:00
virtual UnicodeFunctor * clone ( ) const {
2000-01-18 02:34:41 +00:00
return new TestFilter3 ( * this ) ;
}
2001-07-25 19:11:02 +00:00
virtual UBool contains ( UChar32 c ) const {
2000-03-31 21:45:17 +00:00
if ( c = = 0x6f | | c = = 0x77 )
2000-01-18 02:34:41 +00:00
return FALSE ;
else
return TRUE ;
}
2002-08-28 17:17:04 +00:00
// Stubs
virtual UnicodeString & toPattern ( UnicodeString & result ,
2003-06-09 04:57:01 +00:00
UBool /*escapeUnprintable*/ ) const {
2002-08-28 17:17:04 +00:00
return result ;
}
2003-06-09 04:57:01 +00:00
virtual UBool matchesIndexValue ( uint8_t /*v*/ ) const {
2002-08-28 17:17:04 +00:00
return FALSE ;
}
2003-06-09 04:57:01 +00:00
virtual void addMatchSetTo ( UnicodeSet & /*toUnionTo*/ ) const { }
2000-01-18 02:34:41 +00:00
} ;
void TransliteratorAPITest : : TestGetAdoptFilter ( ) {
2001-09-20 01:11:24 +00:00
UErrorCode status = U_ZERO_ERROR ;
2001-08-31 03:24:28 +00:00
UParseError parseError ;
Transliterator * t = Transliterator : : createInstance ( " Any-Hex " , UTRANS_FORWARD , parseError , status ) ;
2007-08-31 04:58:52 +00:00
if ( t = = 0 | | U_FAILURE ( status ) ) {
errln ( " Error creating transliterator %s " , u_errorName ( status ) ) ;
delete t ;
return ;
2003-06-06 04:54:34 +00:00
}
2000-01-18 02:34:41 +00:00
const UnicodeFilter * u = t - > getFilter ( ) ;
if ( u ! = NULL ) {
2007-08-31 04:58:52 +00:00
errln ( " FAIL: getFilter failed. Didn't return null when the transliterator used no filtering " ) ;
delete t ;
return ;
2000-01-18 02:34:41 +00:00
}
2007-08-31 04:58:52 +00:00
2000-01-18 02:34:41 +00:00
UnicodeString got , temp , message ;
UnicodeString data = " ABCabcbbCBa " ;
2000-01-19 19:02:10 +00:00
temp = data ;
2000-08-10 00:28:31 +00:00
t - > transliterate ( temp ) ;
t - > adoptFilter ( new TestFilter1 ) ;
2000-01-18 02:34:41 +00:00
2000-01-19 19:02:10 +00:00
got = data ;
2000-08-10 00:28:31 +00:00
t - > transliterate ( got ) ;
2000-08-02 19:06:18 +00:00
UnicodeString exp = UnicodeString ( " A \\ u0042Ca \\ u0062c \\ u0062 \\ u0062C \\ u0042a " , " " ) ;
2000-08-10 00:28:31 +00:00
message = " transliteration after adoptFilter(a,A,c,C) " ;
2000-01-18 02:34:41 +00:00
doTest ( message , got , exp ) ;
logln ( " Testing round trip " ) ;
t - > adoptFilter ( ( UnicodeFilter * ) u ) ;
if ( t - > getFilter ( ) = = NULL )
logln ( " OK: adoptFilter and getFilter round trip worked " ) ;
else
errln ( " FAIL: adoptFilter or getFilter round trip failed " ) ;
2000-01-19 19:02:10 +00:00
got = data ;
t - > transliterate ( got ) ;
2000-08-02 19:06:18 +00:00
exp = UnicodeString ( " \\ u0041 \\ u0042 \\ u0043 \\ u0061 \\ u0062 \\ u0063 \\ u0062 \\ u0062 \\ u0043 \\ u0042 \\ u0061 " , " " ) ;
2000-01-18 02:34:41 +00:00
message = " transliteration after adopting null filter " ;
doTest ( message , got , exp ) ;
message = " adoptFilter round trip " ;
2000-08-10 00:28:31 +00:00
doTest ( " adoptFilter round trip " , got , temp ) ;
2000-01-18 02:34:41 +00:00
t - > adoptFilter ( new TestFilter2 ) ;
2003-12-09 01:41:43 +00:00
callEverything ( t , __LINE__ ) ;
2000-01-18 02:34:41 +00:00
data = " heelloe " ;
2000-08-10 00:28:31 +00:00
exp = UnicodeString ( " \\ u0068eell \\ u006Fe " , " " ) ;
2000-01-19 19:02:10 +00:00
got = data ;
2000-08-10 00:28:31 +00:00
t - > transliterate ( got ) ;
message = " transliteration using (e,l) filter " ;
2000-01-18 02:34:41 +00:00
doTest ( " transliteration using (e,l) filter " , got , exp ) ;
2000-08-10 00:28:31 +00:00
data = " are well " ;
exp = UnicodeString ( " \\ u0061 \\ u0072e \\ u0020 \\ u0077ell " , " " ) ;
2000-01-19 19:02:10 +00:00
got = data ;
t - > transliterate ( got ) ;
2000-08-10 00:28:31 +00:00
doTest ( message , got , exp ) ;
2000-01-18 02:34:41 +00:00
t - > adoptFilter ( new TestFilter3 ) ;
2000-08-10 00:28:31 +00:00
data = " ho, wow! " ;
exp = UnicodeString ( " \\ u0068o \\ u002C \\ u0020wow \\ u0021 " , " " ) ;
2000-01-19 19:02:10 +00:00
got = data ;
t - > transliterate ( got ) ;
2000-08-10 00:28:31 +00:00
message = " transliteration using (o,w) filter " ;
2000-01-18 02:34:41 +00:00
doTest ( " transliteration using (o,w) filter " , got , exp ) ;
data = " owl " ;
2000-08-10 00:28:31 +00:00
exp = UnicodeString ( " ow \\ u006C " , " " ) ;
2000-01-19 19:02:10 +00:00
got = data ;
t - > transliterate ( got ) ;
2000-08-10 00:28:31 +00:00
doTest ( " transliteration using (o,w) filter " , got , exp ) ;
2004-10-29 00:24:42 +00:00
2000-08-10 00:28:31 +00:00
delete t ;
2000-01-18 02:34:41 +00:00
}
void TransliteratorAPITest : : keyboardAux ( Transliterator * t , UnicodeString DATA [ ] , UnicodeString & s , int32_t begin , int32_t end ) {
2000-08-10 00:28:31 +00:00
UTransPosition index = { 0 , 0 , 0 , 0 } ;
UErrorCode status = U_ZERO_ERROR ;
for ( int32_t i = begin ; i < end ; i = i + 5 ) {
UnicodeString log ;
if ( DATA [ i + 0 ] ! = " " ) {
2001-10-05 00:00:28 +00:00
log = s + " + " + DATA [ i ] + " -> " ;
2000-08-10 00:28:31 +00:00
index . contextStart = getInt ( DATA [ i + 2 ] ) ;
index . contextLimit = index . limit = getInt ( DATA [ i + 3 ] ) ;
index . start = getInt ( DATA [ i + 4 ] ) ;
t - > transliterate ( s , index , DATA [ i + 0 ] , status ) ;
if ( U_FAILURE ( status ) ) {
errln ( " FAIL: " + t - > getID ( ) + " .transliterate(Replaceable, int32_t[], UnicodeString, UErrorCode)--> " + ( UnicodeString ) u_errorName ( status ) ) ;
continue ;
}
log = s + " => " ;
t - > finishTransliteration ( s , index ) ;
2000-01-18 02:34:41 +00:00
}
2000-08-10 00:28:31 +00:00
// Show the start index '{' and the cursor '|'
displayOutput ( s , DATA [ i + 1 ] , log , index ) ;
}
2000-01-18 02:34:41 +00:00
}
2000-06-27 19:06:15 +00:00
void TransliteratorAPITest : : displayOutput ( const UnicodeString & got , const UnicodeString & expected , UnicodeString & log , UTransPosition & index ) {
2000-08-10 00:28:31 +00:00
// Show the start index '{' and the cursor '|'
2001-10-05 00:00:28 +00:00
UnicodeString a , b , c , d , e ;
2000-08-10 00:28:31 +00:00
got . extractBetween ( 0 , index . contextStart , a ) ;
got . extractBetween ( index . contextStart , index . start , b ) ;
2001-10-05 00:00:28 +00:00
got . extractBetween ( index . start , index . limit , c ) ;
got . extractBetween ( index . limit , index . contextLimit , d ) ;
got . extractBetween ( index . contextLimit , got . length ( ) , e ) ;
2000-08-10 00:28:31 +00:00
log . append ( a ) .
append ( ( UChar ) 0x7b /*{*/ ) .
append ( b ) .
append ( ( UChar ) 0x7c /*|*/ ) .
2001-10-05 00:00:28 +00:00
append ( c ) .
append ( ( UChar ) 0x7c ) .
append ( d ) .
append ( ( UChar ) 0x7d /*}*/ ) .
append ( e ) ;
2000-08-10 00:28:31 +00:00
if ( got = = expected )
logln ( " OK: " + prettify ( log ) ) ;
else
errln ( " FAIL: " + prettify ( log ) + " , expected " + prettify ( expected ) ) ;
2000-01-18 02:34:41 +00:00
}
/*Internal Functions used*/
void TransliteratorAPITest : : doTest ( const UnicodeString & message , const UnicodeString & result , const UnicodeString & expected ) {
2001-08-31 03:24:28 +00:00
if ( prettify ( result ) = = prettify ( expected ) )
2000-08-10 00:28:31 +00:00
logln ( ( UnicodeString ) " Ok: " + prettify ( message ) + " passed \" " + prettify ( expected ) + " \" " ) ;
else
2009-06-12 19:34:21 +00:00
dataerrln ( ( UnicodeString ) " FAIL: " + message + " failed Got--> " + prettify ( result ) + " , Expected--> " + prettify ( expected ) ) ;
2000-08-10 00:28:31 +00:00
}
2000-01-18 02:34:41 +00:00
2003-12-09 01:41:43 +00:00
//
// 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 ;
}
2005-01-08 06:50:46 +00:00
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 * ) { }
} ;
2003-12-09 01:41:43 +00:00
2005-01-08 06:50:46 +00:00
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 " ) ;
}
}
2003-12-09 01:41:43 +00:00
2002-09-21 00:43:14 +00:00
# endif /* #if !UCONFIG_NO_TRANSLITERATION */