1 /********************************************************************
3 * Copyright (c) 2001-2004, 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, TestUniToHexErrors);
47 //TESTCASE(4, TestHexToUniErrors);
48 // TODO: Add a subclass to test clone().
49 default: name
= ""; break;
54 void TransliteratorErrorTest::TestTransliteratorErrors() {
55 UnicodeString trans
="Latin-Greek";
56 UnicodeString bogusID
="LATINGREEK-GREEKLATIN";
57 UnicodeString newID
="Bogus-Latin";
58 UnicodeString newIDRules
="zzz > Z; f <> ph";
59 UnicodeString bogusRules
="a } [b-g m-p ";
60 UParseError parseError
;
61 UErrorCode status
= U_ZERO_ERROR
;
62 UnicodeString testString
="A quick fox jumped over the lazy dog.";
63 UnicodeString insertString
="cats and dogs";
64 int32_t stoppedAt
= 0, len
;
67 Transliterator
* t
= Transliterator::createInstance(trans
, UTRANS_FORWARD
, parseError
, status
);
68 if(t
==0 || U_FAILURE(status
)){
69 errln("FAIL: construction of Latin-Greek");
76 len
= testString
.length();
77 stoppedAt
= t
->transliterate(testString
, 0, 100);
78 if (stoppedAt
!= -1) {
79 errln("FAIL: Out of bounds check failed (1).");
80 } else if (testString
.length() != len
) {
81 testString
="A quick fox jumped over the lazy dog.";
82 errln("FAIL: Transliterate fails and the target string was modified.");
84 stoppedAt
= t
->transliterate(testString
, 100, testString
.length()-1);
86 errln("FAIL: Out of bounds check failed (2).");
87 else if (testString
.length() != len
) {
88 testString
="A quick fox jumped over the lazy dog.";
89 errln("FAIL: Transliterate fails and the target string was modified.");
92 pos
.limit
= testString
.length();
93 t
->transliterate(testString
, pos
, status
);
94 if (U_SUCCESS(status
)) {
95 errln("FAIL: Start offset is out of bounds, error not reported.\n");
97 status
= U_ZERO_ERROR
;
100 t
->transliterate(testString
, pos
, status
);
101 if (U_SUCCESS(status
)) {
102 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
104 status
= U_ZERO_ERROR
;
105 len
= pos
.contextLimit
= testString
.length();
106 pos
.contextStart
= 0;
109 t
->transliterate(testString
, pos
, insertString
, status
);
110 if (len
== pos
.limit
) {
111 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
112 if (U_SUCCESS(status
)) {
113 errln("FAIL: Error code wasn't set either.");
116 status
= U_ZERO_ERROR
;
117 pos
.contextStart
= 0;
118 pos
.contextLimit
= testString
.length();
119 pos
.limit
= testString
.length() -1;
121 t
->transliterate(testString
, pos
, (UChar32
)0x0061, status
);
122 if (len
== pos
.limit
) {
123 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
124 if (U_SUCCESS(status
)) {
125 errln("FAIL: Error code wasn't set either.");
128 status
= U_ZERO_ERROR
;
129 len
= pos
.limit
= testString
.length();
130 pos
.contextStart
= 0;
131 pos
.contextLimit
= testString
.length() - 1;
133 t
->transliterate(testString
, pos
, insertString
, status
);
134 if (U_SUCCESS(status
)) {
135 errln("FAIL: Out of bounds check failed (3).");
136 if (testString
.length() != len
)
137 errln("FAIL: The input string was modified though the offsets were out of bounds.");
139 Transliterator
* t1
= Transliterator::createInstance(bogusID
, UTRANS_FORWARD
, parseError
, status
);
140 if(t1
!=0 || U_SUCCESS(status
)){
142 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
146 status
= U_ZERO_ERROR
;
147 Transliterator
* t2
= new RuleBasedTransliterator(newID
, newIDRules
, UTRANS_FORWARD
, status
);
148 if (U_SUCCESS(status
)) {
149 Transliterator
* t3
= t2
->createInverse(status
);
150 if (U_SUCCESS(status
)) {
152 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
157 status
= U_ZERO_ERROR
;
158 Transliterator
* t4
= Transliterator::createFromRules(newID
, bogusRules
, UTRANS_FORWARD
, parseError
, status
);
159 if (t4
!= NULL
|| U_SUCCESS(status
)) {
160 errln("FAIL: The rules is malformed but error was not reported.");
161 if (parseError
.offset
!= -1) {
162 errln("FAIL: The parse error offset isn't set correctly when fails.");
163 } else if (parseError
.postContext
[0] == 0 || parseError
.preContext
[0] == 0) {
164 errln("FAIL: The parse error pre/post context isn't reset properly.");
172 void TransliteratorErrorTest::TestUnicodeSetErrors() {
173 UnicodeString badPattern
="[[:L:]-[0x0300-0x0400]";
175 UErrorCode status
= U_ZERO_ERROR
;
176 UnicodeString result
;
178 if (!set
.isEmpty()) {
179 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
181 set
.applyPattern(badPattern
, status
);
182 if (U_SUCCESS(status
)) {
183 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
185 status
= U_ZERO_ERROR
;
186 UnicodeSet
*set1
= new UnicodeSet(badPattern
, status
);
187 if (U_SUCCESS(status
)) {
188 errln("FAIL: Created a UnicodeSet based on bad patterns.");
193 //void TransliteratorErrorTest::TestUniToHexErrors() {
194 // UErrorCode status = U_ZERO_ERROR;
195 // Transliterator *t = new UnicodeToHexTransliterator("", TRUE, NULL, status);
196 // if (U_SUCCESS(status)) {
197 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
201 // status = U_ZERO_ERROR;
202 // t = new UnicodeToHexTransliterator("\\x", TRUE, NULL, status);
203 // if (U_SUCCESS(status)) {
204 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
208 // status = U_ZERO_ERROR;
209 // t = new UnicodeToHexTransliterator();
210 // ((UnicodeToHexTransliterator*)t)->applyPattern("\\x", status);
211 // if (U_SUCCESS(status)) {
212 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
217 void TransliteratorErrorTest::TestRBTErrors() {
219 UnicodeString rules
="ab>y";
220 UnicodeString id
="MyRandom-YReverse";
221 UnicodeString goodPattern
="[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
222 UErrorCode status
= U_ZERO_ERROR
;
223 UParseError parseErr
;
224 UnicodeSet
*set
= new UnicodeSet(goodPattern
, status
);
225 if (U_FAILURE(status
)) {
226 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
229 RuleBasedTransliterator
*t
= new RuleBasedTransliterator(id
, rules
, UTRANS_REVERSE
, set
, parseErr
, status
);
230 if (U_FAILURE(status
)) {
231 errln("FAIL: Was not able to create a good RBT to test registration.");
235 Transliterator::registerInstance(t
);
236 Transliterator::unregister(id
);
237 status
= U_ZERO_ERROR
;
238 Transliterator
* t1
= Transliterator::createInstance(id
, UTRANS_REVERSE
, parseErr
, status
);
239 if(U_SUCCESS(status
)){
241 errln("FAIL: construction of unregistered ID failed.");
245 //void TransliteratorErrorTest::TestHexToUniErrors() {
246 // UErrorCode status = U_ZERO_ERROR;
247 // Transliterator *t = new HexToUnicodeTransliterator("", NULL, status);
248 // if (U_FAILURE(status)) {
249 // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
252 // status = U_ZERO_ERROR;
253 // t = new HexToUnicodeTransliterator("\\x", NULL, status);
254 // if (U_SUCCESS(status)) {
255 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
258 // status = U_ZERO_ERROR;
259 // t = new HexToUnicodeTransliterator();
260 // ((HexToUnicodeTransliterator*)t)->applyPattern("\\x", status);
261 // if (U_SUCCESS(status)) {
262 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
267 #endif /* #if !UCONFIG_NO_TRANSLITERATION */