1 /********************************************************************
2 * Copyright (c) 2001-2009, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 *********************************************************************
5 * This test program is intended for testing error conditions of the
6 * transliterator APIs to make sure the exceptions are raised where
9 * Date Name Description
10 * 11/14/2001 hshih Creation.
12 ********************************************************************/
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_TRANSLITERATION
20 #include "unicode/utypes.h"
21 #include "unicode/translit.h"
22 #include "unicode/uniset.h"
23 #include "unicode/unifilt.h"
28 #include "unicode/rep.h"
29 #include "unicode/locid.h"
31 //---------------------------------------------
33 //---------------------------------------------
36 TransliteratorErrorTest::runIndexedTest(int32_t index
, UBool exec
,
37 const char* &name
, char* /*par*/) {
39 TESTCASE(0,TestTransliteratorErrors
);
40 TESTCASE(1, TestUnicodeSetErrors
);
41 TESTCASE(2, TestRBTErrors
);
42 TESTCASE(3, TestCoverage
);
43 //TESTCASE(3, TestUniToHexErrors);
44 //TESTCASE(4, TestHexToUniErrors);
45 // TODO: Add a subclass to test clone().
46 default: name
= ""; break;
51 void TransliteratorErrorTest::TestTransliteratorErrors() {
52 UnicodeString trans
="Latin-Greek";
53 UnicodeString bogusID
="LATINGREEK-GREEKLATIN";
54 UnicodeString newID
="Bogus-Latin";
55 UnicodeString newIDRules
="zzz > Z; f <> ph";
56 UnicodeString bogusRules
="a } [b-g m-p ";
57 UParseError parseError
;
58 UErrorCode status
= U_ZERO_ERROR
;
59 UnicodeString testString
="A quick fox jumped over the lazy dog.";
60 UnicodeString insertString
="cats and dogs";
61 int32_t stoppedAt
= 0, len
;
64 Transliterator
* t
= Transliterator::createInstance(trans
, UTRANS_FORWARD
, parseError
, status
);
65 if(t
==0 || U_FAILURE(status
)){
66 dataerrln("FAIL: construction of Latin-Greek - %s", u_errorName(status
));
73 len
= testString
.length();
74 stoppedAt
= t
->transliterate(testString
, 0, 100);
75 if (stoppedAt
!= -1) {
76 errln("FAIL: Out of bounds check failed (1).");
77 } else if (testString
.length() != len
) {
78 testString
="A quick fox jumped over the lazy dog.";
79 errln("FAIL: Transliterate fails and the target string was modified.");
81 stoppedAt
= t
->transliterate(testString
, 100, testString
.length()-1);
83 errln("FAIL: Out of bounds check failed (2).");
84 else if (testString
.length() != len
) {
85 testString
="A quick fox jumped over the lazy dog.";
86 errln("FAIL: Transliterate fails and the target string was modified.");
89 pos
.limit
= testString
.length();
90 t
->transliterate(testString
, pos
, status
);
91 if (U_SUCCESS(status
)) {
92 errln("FAIL: Start offset is out of bounds, error not reported.\n");
94 status
= U_ZERO_ERROR
;
97 t
->transliterate(testString
, pos
, status
);
98 if (U_SUCCESS(status
)) {
99 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
101 status
= U_ZERO_ERROR
;
102 len
= pos
.contextLimit
= testString
.length();
103 pos
.contextStart
= 0;
106 t
->transliterate(testString
, pos
, insertString
, status
);
107 if (len
== pos
.limit
) {
108 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
109 if (U_SUCCESS(status
)) {
110 errln("FAIL: Error code wasn't set either.");
113 status
= U_ZERO_ERROR
;
114 pos
.contextStart
= 0;
115 pos
.contextLimit
= testString
.length();
116 pos
.limit
= testString
.length() -1;
118 t
->transliterate(testString
, pos
, (UChar32
)0x0061, status
);
119 if (len
== pos
.limit
) {
120 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
121 if (U_SUCCESS(status
)) {
122 errln("FAIL: Error code wasn't set either.");
125 status
= U_ZERO_ERROR
;
126 len
= pos
.limit
= testString
.length();
127 pos
.contextStart
= 0;
128 pos
.contextLimit
= testString
.length() - 1;
130 t
->transliterate(testString
, pos
, insertString
, status
);
131 if (U_SUCCESS(status
)) {
132 errln("FAIL: Out of bounds check failed (3).");
133 if (testString
.length() != len
)
134 errln("FAIL: The input string was modified though the offsets were out of bounds.");
136 Transliterator
* t1
= Transliterator::createInstance(bogusID
, UTRANS_FORWARD
, parseError
, status
);
137 if(t1
!=0 || U_SUCCESS(status
)){
139 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
141 status
= U_ZERO_ERROR
;
142 Transliterator
* t2
= Transliterator::createFromRules(newID
, newIDRules
, UTRANS_FORWARD
, parseError
, status
);
143 if (U_SUCCESS(status
)) {
144 Transliterator
* t3
= t2
->createInverse(status
);
145 if (U_SUCCESS(status
)) {
147 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
152 status
= U_ZERO_ERROR
;
153 Transliterator
* t4
= Transliterator::createFromRules(newID
, bogusRules
, UTRANS_FORWARD
, parseError
, status
);
154 if (t4
!= NULL
|| U_SUCCESS(status
)) {
155 errln("FAIL: The rules is malformed but error was not reported.");
156 if (parseError
.offset
!= -1) {
157 errln("FAIL: The parse error offset isn't set correctly when fails.");
158 } else if (parseError
.postContext
[0] == 0 || parseError
.preContext
[0] == 0) {
159 errln("FAIL: The parse error pre/post context isn't reset properly.");
167 void TransliteratorErrorTest::TestUnicodeSetErrors() {
168 UnicodeString badPattern
="[[:L:]-[0x0300-0x0400]";
170 UErrorCode status
= U_ZERO_ERROR
;
171 UnicodeString result
;
173 if (!set
.isEmpty()) {
174 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
176 set
.applyPattern(badPattern
, status
);
177 if (U_SUCCESS(status
)) {
178 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
180 status
= U_ZERO_ERROR
;
181 UnicodeSet
*set1
= new UnicodeSet(badPattern
, status
);
182 if (U_SUCCESS(status
)) {
183 errln("FAIL: Created a UnicodeSet based on bad patterns.");
188 //void TransliteratorErrorTest::TestUniToHexErrors() {
189 // UErrorCode status = U_ZERO_ERROR;
190 // Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status);
191 // if (U_SUCCESS(status)) {
192 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
196 // status = U_ZERO_ERROR;
197 // t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status);
198 // if (U_SUCCESS(status)) {
199 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
203 // status = U_ZERO_ERROR;
204 // t = new UnicodeToHexTransliterator();
205 // ((UnicodeToHexTransliterator*)t)->applyPattern("\\x", status);
206 // if (U_SUCCESS(status)) {
207 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
212 void TransliteratorErrorTest::TestRBTErrors() {
214 UnicodeString rules
="ab>y";
215 UnicodeString id
="MyRandom-YReverse";
216 //UnicodeString goodPattern="[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
217 UErrorCode status
= U_ZERO_ERROR
;
218 UParseError parseErr
;
219 /*UnicodeSet *set = new UnicodeSet(goodPattern, status);
220 if (U_FAILURE(status)) {
221 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
224 Transliterator
*t
= Transliterator::createFromRules(id
, rules
, UTRANS_REVERSE
, parseErr
, status
);
225 if (U_FAILURE(status
)) {
226 errln("FAIL: Was not able to create a good RBT to test registration.");
230 Transliterator::registerInstance(t
);
231 Transliterator::unregister(id
);
232 status
= U_ZERO_ERROR
;
233 Transliterator
* t1
= Transliterator::createInstance(id
, UTRANS_REVERSE
, parseErr
, status
);
234 if(U_SUCCESS(status
)){
236 errln("FAIL: construction of unregistered ID failed.");
240 //void TransliteratorErrorTest::TestHexToUniErrors() {
241 // UErrorCode status = U_ZERO_ERROR;
242 // Transliterator *t = new HexToUnicodeTransliterator("", NULL, status);
243 // if (U_FAILURE(status)) {
244 // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
247 // status = U_ZERO_ERROR;
248 // t = new HexToUnicodeTransliterator("\\x", NULL, status);
249 // if (U_SUCCESS(status)) {
250 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
253 // status = U_ZERO_ERROR;
254 // t = new HexToUnicodeTransliterator();
255 // ((HexToUnicodeTransliterator*)t)->applyPattern("\\x", status);
256 // if (U_SUCCESS(status)) {
257 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
262 class StubTransliterator
: public Transliterator
{
264 StubTransliterator(): Transliterator(UNICODE_STRING_SIMPLE("Any-Null"), 0) {}
265 virtual void handleTransliterate(Replaceable
& ,UTransPosition
& offsets
,UBool
) const {
266 offsets
.start
= offsets
.limit
;
269 virtual UClassID
getDynamicClassID() const{
270 static char classID
= 0;
271 return (UClassID
)&classID
;
275 void TransliteratorErrorTest::TestCoverage() {
276 StubTransliterator stub
;
278 if (stub
.clone() != NULL
){
279 errln("FAIL: default Transliterator::clone() should return NULL");
283 #endif /* #if !UCONFIG_NO_TRANSLITERATION */