1 /********************************************************************
3 * Copyright (c) 2001-2005, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * This test program is intended for testing error conditions of the
8 * transliterator APIs to make sure the exceptions are raised where
11 * Date Name Description
12 * 11/14/2001 hshih Creation.
14 ************************************************************************/
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_TRANSLITERATION
22 #include "unicode/utypes.h"
23 #include "unicode/translit.h"
24 #include "unicode/uniset.h"
26 #include "unicode/unifilt.h"
32 #include "unicode/rep.h"
33 #include "unicode/locid.h"
35 //---------------------------------------------
37 //---------------------------------------------
40 TransliteratorErrorTest::runIndexedTest(int32_t index
, UBool exec
,
41 const char* &name
, char* /*par*/) {
43 TESTCASE(0,TestTransliteratorErrors
);
44 TESTCASE(1, TestUnicodeSetErrors
);
45 TESTCASE(2, TestRBTErrors
);
46 TESTCASE(3, TestCoverage
);
47 //TESTCASE(3, TestUniToHexErrors);
48 //TESTCASE(4, TestHexToUniErrors);
49 // TODO: Add a subclass to test clone().
50 default: name
= ""; break;
55 void TransliteratorErrorTest::TestTransliteratorErrors() {
56 UnicodeString trans
="Latin-Greek";
57 UnicodeString bogusID
="LATINGREEK-GREEKLATIN";
58 UnicodeString newID
="Bogus-Latin";
59 UnicodeString newIDRules
="zzz > Z; f <> ph";
60 UnicodeString bogusRules
="a } [b-g m-p ";
61 UParseError parseError
;
62 UErrorCode status
= U_ZERO_ERROR
;
63 UnicodeString testString
="A quick fox jumped over the lazy dog.";
64 UnicodeString insertString
="cats and dogs";
65 int32_t stoppedAt
= 0, len
;
68 Transliterator
* t
= Transliterator::createInstance(trans
, UTRANS_FORWARD
, parseError
, status
);
69 if(t
==0 || U_FAILURE(status
)){
70 errln("FAIL: construction of Latin-Greek");
77 len
= testString
.length();
78 stoppedAt
= t
->transliterate(testString
, 0, 100);
79 if (stoppedAt
!= -1) {
80 errln("FAIL: Out of bounds check failed (1).");
81 } else if (testString
.length() != len
) {
82 testString
="A quick fox jumped over the lazy dog.";
83 errln("FAIL: Transliterate fails and the target string was modified.");
85 stoppedAt
= t
->transliterate(testString
, 100, testString
.length()-1);
87 errln("FAIL: Out of bounds check failed (2).");
88 else if (testString
.length() != len
) {
89 testString
="A quick fox jumped over the lazy dog.";
90 errln("FAIL: Transliterate fails and the target string was modified.");
93 pos
.limit
= testString
.length();
94 t
->transliterate(testString
, pos
, status
);
95 if (U_SUCCESS(status
)) {
96 errln("FAIL: Start offset is out of bounds, error not reported.\n");
98 status
= U_ZERO_ERROR
;
101 t
->transliterate(testString
, pos
, status
);
102 if (U_SUCCESS(status
)) {
103 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
105 status
= U_ZERO_ERROR
;
106 len
= pos
.contextLimit
= testString
.length();
107 pos
.contextStart
= 0;
110 t
->transliterate(testString
, pos
, insertString
, status
);
111 if (len
== pos
.limit
) {
112 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
113 if (U_SUCCESS(status
)) {
114 errln("FAIL: Error code wasn't set either.");
117 status
= U_ZERO_ERROR
;
118 pos
.contextStart
= 0;
119 pos
.contextLimit
= testString
.length();
120 pos
.limit
= testString
.length() -1;
122 t
->transliterate(testString
, pos
, (UChar32
)0x0061, status
);
123 if (len
== pos
.limit
) {
124 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
125 if (U_SUCCESS(status
)) {
126 errln("FAIL: Error code wasn't set either.");
129 status
= U_ZERO_ERROR
;
130 len
= pos
.limit
= testString
.length();
131 pos
.contextStart
= 0;
132 pos
.contextLimit
= testString
.length() - 1;
134 t
->transliterate(testString
, pos
, insertString
, status
);
135 if (U_SUCCESS(status
)) {
136 errln("FAIL: Out of bounds check failed (3).");
137 if (testString
.length() != len
)
138 errln("FAIL: The input string was modified though the offsets were out of bounds.");
140 Transliterator
* t1
= Transliterator::createInstance(bogusID
, UTRANS_FORWARD
, parseError
, status
);
141 if(t1
!=0 || U_SUCCESS(status
)){
143 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
147 status
= U_ZERO_ERROR
;
148 Transliterator
* t2
= new RuleBasedTransliterator(newID
, newIDRules
, UTRANS_FORWARD
, status
);
149 if (U_SUCCESS(status
)) {
150 Transliterator
* t3
= t2
->createInverse(status
);
151 if (U_SUCCESS(status
)) {
153 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
158 status
= U_ZERO_ERROR
;
159 Transliterator
* t4
= Transliterator::createFromRules(newID
, bogusRules
, UTRANS_FORWARD
, parseError
, status
);
160 if (t4
!= NULL
|| U_SUCCESS(status
)) {
161 errln("FAIL: The rules is malformed but error was not reported.");
162 if (parseError
.offset
!= -1) {
163 errln("FAIL: The parse error offset isn't set correctly when fails.");
164 } else if (parseError
.postContext
[0] == 0 || parseError
.preContext
[0] == 0) {
165 errln("FAIL: The parse error pre/post context isn't reset properly.");
173 void TransliteratorErrorTest::TestUnicodeSetErrors() {
174 UnicodeString badPattern
="[[:L:]-[0x0300-0x0400]";
176 UErrorCode status
= U_ZERO_ERROR
;
177 UnicodeString result
;
179 if (!set
.isEmpty()) {
180 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
182 set
.applyPattern(badPattern
, status
);
183 if (U_SUCCESS(status
)) {
184 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
186 status
= U_ZERO_ERROR
;
187 UnicodeSet
*set1
= new UnicodeSet(badPattern
, status
);
188 if (U_SUCCESS(status
)) {
189 errln("FAIL: Created a UnicodeSet based on bad patterns.");
194 //void TransliteratorErrorTest::TestUniToHexErrors() {
195 // UErrorCode status = U_ZERO_ERROR;
196 // Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status);
197 // if (U_SUCCESS(status)) {
198 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
202 // status = U_ZERO_ERROR;
203 // t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status);
204 // if (U_SUCCESS(status)) {
205 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
209 // status = U_ZERO_ERROR;
210 // t = new UnicodeToHexTransliterator();
211 // ((UnicodeToHexTransliterator*)t)->applyPattern("\\x", status);
212 // if (U_SUCCESS(status)) {
213 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
218 void TransliteratorErrorTest::TestRBTErrors() {
220 UnicodeString rules
="ab>y";
221 UnicodeString id
="MyRandom-YReverse";
222 UnicodeString goodPattern
="[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
223 UErrorCode status
= U_ZERO_ERROR
;
224 UParseError parseErr
;
225 UnicodeSet
*set
= new UnicodeSet(goodPattern
, status
);
226 if (U_FAILURE(status
)) {
227 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
230 RuleBasedTransliterator
*t
= new RuleBasedTransliterator(id
, rules
, UTRANS_REVERSE
, set
, parseErr
, status
);
231 if (U_FAILURE(status
)) {
232 errln("FAIL: Was not able to create a good RBT to test registration.");
236 Transliterator::registerInstance(t
);
237 Transliterator::unregister(id
);
238 status
= U_ZERO_ERROR
;
239 Transliterator
* t1
= Transliterator::createInstance(id
, UTRANS_REVERSE
, parseErr
, status
);
240 if(U_SUCCESS(status
)){
242 errln("FAIL: construction of unregistered ID failed.");
246 //void TransliteratorErrorTest::TestHexToUniErrors() {
247 // UErrorCode status = U_ZERO_ERROR;
248 // Transliterator *t = new HexToUnicodeTransliterator("", NULL, status);
249 // if (U_FAILURE(status)) {
250 // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
253 // status = U_ZERO_ERROR;
254 // t = new HexToUnicodeTransliterator("\\x", NULL, status);
255 // if (U_SUCCESS(status)) {
256 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
259 // status = U_ZERO_ERROR;
260 // t = new HexToUnicodeTransliterator();
261 // ((HexToUnicodeTransliterator*)t)->applyPattern("\\x", status);
262 // if (U_SUCCESS(status)) {
263 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
268 class StubTransliterator
: public Transliterator
{
270 StubTransliterator(): Transliterator(UNICODE_STRING_SIMPLE("Any-Null"), 0) {}
271 virtual void handleTransliterate(Replaceable
& ,UTransPosition
& offsets
,UBool
) const {
272 offsets
.start
= offsets
.limit
;
275 virtual UClassID
getDynamicClassID() const{
276 static char classID
= 0;
277 return (UClassID
)&classID
;
281 void TransliteratorErrorTest::TestCoverage() {
282 StubTransliterator stub
;
284 if (stub
.clone() != NULL
){
285 errln("FAIL: default Transliterator::clone() should return NULL");
289 #endif /* #if !UCONFIG_NO_TRANSLITERATION */